Skip to content

Commit c6ac510

Browse files
authored
Merge pull request #1242 from Absolemus/latest
Рефлектор возвращает приватные поля и методы
2 parents c3a7e70 + 5b71661 commit c6ac510

File tree

7 files changed

+173
-40
lines changed

7 files changed

+173
-40
lines changed

src/NUnitTests/CompilerTests.cs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void TestNoSemicolonBeforeEndProcedure()
3535
Возврат
3636
КонецПроцедуры");
3737

38-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
38+
host.Engine.GetCompilerService().Compile(moduleSource);
3939
}
4040

4141
[Test]
@@ -46,7 +46,7 @@ public void TestNoSemicolonBeforeEndFunction()
4646
Возврат 4
4747
КонецФункции");
4848

49-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
49+
host.Engine.GetCompilerService().Compile(moduleSource);
5050
}
5151

5252
[Test]
@@ -57,7 +57,7 @@ public void TestNoSemicolonBeforeEndDo()
5757
Прервать
5858
КонецЦикла");
5959

60-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
60+
host.Engine.GetCompilerService().Compile(moduleSource);
6161
}
6262

6363
[Test]
@@ -72,7 +72,7 @@ ИначеЕсли Истина Тогда
7272
Ф = 3
7373
КонецЕсли");
7474

75-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
75+
host.Engine.GetCompilerService().Compile(moduleSource);
7676
}
7777

7878
[Test]
@@ -85,7 +85,7 @@ public void TestNoSemicolonBeforeExceptionOrEndTry()
8585
ВызватьИсключение
8686
КонецПопытки");
8787

88-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
88+
host.Engine.GetCompilerService().Compile(moduleSource);
8989
}
9090

9191

@@ -98,7 +98,7 @@ public void TestNoSemicolonBeforeEndOfText()
9898
КонецПроцедуры
9999
Ф = 0");
100100

101-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
101+
host.Engine.GetCompilerService().Compile(moduleSource);
102102
}
103103
#endregion
104104

@@ -112,7 +112,7 @@ public void TestSemicolonBeforeEndProcedure()
112112
Возврат;
113113
КонецПроцедуры");
114114

115-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
115+
host.Engine.GetCompilerService().Compile(moduleSource);
116116
}
117117

118118
[Test]
@@ -123,7 +123,7 @@ public void TestSemicolonBeforeEndFunction()
123123
Возврат 4;
124124
КонецФункции");
125125

126-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
126+
host.Engine.GetCompilerService().Compile(moduleSource);
127127
}
128128

129129
[Test]
@@ -134,7 +134,7 @@ public void TestSemicolonBeforeEndDo()
134134
Прервать;
135135
КонецЦикла");
136136

137-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
137+
host.Engine.GetCompilerService().Compile(moduleSource);
138138
}
139139

140140
[Test]
@@ -149,7 +149,7 @@ ИначеЕсли Истина Тогда
149149
Ф = 3;
150150
КонецЕсли");
151151

152-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
152+
host.Engine.GetCompilerService().Compile(moduleSource);
153153
}
154154

155155
[Test]
@@ -162,7 +162,7 @@ public void TestSemicolonBeforeExceptionOrEndTry()
162162
ВызватьИсключение;
163163
КонецПопытки");
164164

165-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
165+
host.Engine.GetCompilerService().Compile(moduleSource);
166166
}
167167

168168

@@ -175,7 +175,7 @@ public void TestSemicolonBeforeEndOfText()
175175
КонецПроцедуры
176176
Ф = 0;");
177177

178-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
178+
host.Engine.GetCompilerService().Compile(moduleSource);
179179
}
180180
#endregion
181181

@@ -192,7 +192,7 @@ public void TestEndFunctionDoesNotEndIf()
192192
bool exceptionThrown = false;
193193
try
194194
{
195-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
195+
host.Engine.GetCompilerService().Compile(moduleSource);
196196
}
197197
catch (CompilerException)
198198
{
@@ -212,7 +212,7 @@ public void TestEndDoDoesNotEndIf()
212212
bool exceptionThrown = false;
213213
try
214214
{
215-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
215+
host.Engine.GetCompilerService().Compile(moduleSource);
216216
}
217217
catch (CompilerException)
218218
{
@@ -232,7 +232,7 @@ public void TestEndIfDoesNotEndDo()
232232
bool exceptionThrown = false;
233233
try
234234
{
235-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
235+
host.Engine.GetCompilerService().Compile(moduleSource);
236236
}
237237
catch (CompilerException)
238238
{
@@ -251,7 +251,7 @@ public void TestEndFunctionDoesNotEndProcedure()
251251
bool exceptionThrown = false;
252252
try
253253
{
254-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
254+
host.Engine.GetCompilerService().Compile(moduleSource);
255255
}
256256
catch (CompilerException)
257257
{
@@ -271,7 +271,7 @@ Возврат 0
271271
bool exceptionThrown = false;
272272
try
273273
{
274-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
274+
host.Engine.GetCompilerService().Compile(moduleSource);
275275
}
276276
catch (CompilerException)
277277
{
@@ -291,7 +291,7 @@ public void TestElseifDoesNotEndProcedure()
291291
bool exceptionThrown = false;
292292
try
293293
{
294-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
294+
host.Engine.GetCompilerService().Compile(moduleSource);
295295
}
296296
catch (CompilerException)
297297
{
@@ -311,7 +311,7 @@ public void TestEndTryDoesNotEndProcedure()
311311
bool exceptionThrown = false;
312312
try
313313
{
314-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
314+
host.Engine.GetCompilerService().Compile(moduleSource);
315315
}
316316
catch (CompilerException)
317317
{
@@ -339,7 +339,7 @@ Функция Ф1(П1, П2 = Неопределено, П3 = Неопредел
339339
Р = Ф1(,,) + Ф1(,);
340340
");
341341

342-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
342+
host.Engine.GetCompilerService().Compile(moduleSource);
343343
}
344344

345345
[Test(Description = "Не компилируется вызов метода вообще без параметров")]
@@ -355,7 +355,7 @@ public void TestCantCompileCallWithoutParams()
355355
bool exceptionThrown = false;
356356
try
357357
{
358-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
358+
host.Engine.GetCompilerService().Compile(moduleSource);
359359
}
360360
catch (CompilerException)
361361
{
@@ -374,7 +374,7 @@ public void TestReturnBeforeException()
374374
Исключение
375375
КонецПопытки
376376
КонецПроцедуры");
377-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
377+
host.Engine.GetCompilerService().Compile(moduleSource);
378378
}
379379

380380

@@ -389,7 +389,7 @@ ДобавитьОбработчик ЭтотОбъект.Событие Ина
389389
bool exceptionThrown = false;
390390
try
391391
{
392-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
392+
host.Engine.GetCompilerService().Compile(moduleSource);
393393
}
394394
catch (CompilerException)
395395
{
@@ -411,7 +411,7 @@ Процедура Проц1()
411411
bool exceptionThrown = false;
412412
try
413413
{
414-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
414+
host.Engine.GetCompilerService().Compile(moduleSource);
415415
}
416416
catch (CompilerException)
417417
{
@@ -434,7 +434,7 @@ Если Истина Тогда
434434
bool exceptionThrown = false;
435435
try
436436
{
437-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
437+
host.Engine.GetCompilerService().Compile(moduleSource);
438438
}
439439
catch (CompilerException)
440440
{
@@ -457,7 +457,7 @@ Процедура Проц2()
457457
bool exceptionThrown = false;
458458
try
459459
{
460-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
460+
host.Engine.GetCompilerService().Compile(moduleSource);
461461
}
462462
catch (CompilerException)
463463
{
@@ -479,7 +479,7 @@ Если Ложь Тогда
479479
bool exceptionThrown = false;
480480
try
481481
{
482-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
482+
host.Engine.GetCompilerService().Compile(moduleSource);
483483
}
484484
catch (CompilerException)
485485
{
@@ -501,7 +501,7 @@ Если Ложь Тогда
501501
bool exceptionThrown = false;
502502
try
503503
{
504-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
504+
host.Engine.GetCompilerService().Compile(moduleSource);
505505
}
506506
catch (CompilerException)
507507
{
@@ -524,13 +524,33 @@ Если Ложь Тогда
524524
bool exceptionThrown = false;
525525
try
526526
{
527-
_ = host.Engine.GetCompilerService().Compile(moduleSource);
527+
host.Engine.GetCompilerService().Compile(moduleSource);
528528
}
529529
catch (CompilerException)
530530
{
531531
exceptionThrown = true;
532532
}
533533
Assert.IsTrue(exceptionThrown, "Отсутствует точка с запятой между операторами!");
534534
}
535-
}
535+
536+
[Test]
537+
public void TestLocalExportVar()
538+
{
539+
var moduleSource = host.Engine.Loader.FromString(
540+
@"Процедура Проц1()
541+
Перем Переменная Экспорт;
542+
КонецПроцедуры");
543+
544+
bool exceptionThrown = false;
545+
try
546+
{
547+
host.Engine.GetCompilerService().Compile(moduleSource);
548+
}
549+
catch (CompilerException)
550+
{
551+
exceptionThrown = true;
552+
}
553+
Assert.IsTrue(exceptionThrown, "В теле процедуры или функции не может быть объявлена экспортная переменная!");
554+
}
555+
}
536556
}

src/ScriptEngine.HostedScript/Library/Reflector.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private static AnnotationDefinition[] GetAnnotations(IEnumerable<UserAnnotationA
274274
return attributes.Select(x => x.Annotation).ToArray();
275275
}
276276

277-
private static void FillPropertiesTableForType(TypeTypeValue type, ValueTable.ValueTable result)
277+
private static void FillPropertiesTableForType(TypeTypeValue type, ValueTable.ValueTable result, bool withPrivate = false)
278278
{
279279
var clrType = GetReflectableClrType(type);
280280
var nativeProps = clrType.GetProperties()
@@ -299,14 +299,19 @@ private static void FillPropertiesTableForType(TypeTypeValue type, ValueTable.Va
299299

300300
if (clrType.BaseType == typeof(ScriptDrivenObject))
301301
{
302-
var nativeFields = clrType.GetFields();
303-
foreach(var field in nativeFields)
302+
var flags = BindingFlags.Public;
303+
if (withPrivate)
304+
flags |= BindingFlags.NonPublic;
305+
306+
var nativeFields = clrType.GetFields(flags);
307+
foreach (var field in nativeFields)
304308
{
305309
var info = new VariableInfo();
306310
info.Type = SymbolType.ContextProperty;
307311
info.Index = indices++;
308312
info.Identifier = field.Name;
309313
info.Annotations = GetAnnotations(field.GetCustomAttributes<UserAnnotationAttribute>());
314+
info.IsExport = field.IsPublic;
310315
infos.Add(info);
311316
}
312317
}
@@ -365,18 +370,19 @@ private static void FillMethodsTable(ValueTable.ValueTable result, IEnumerable<M
365370
/// Получает таблицу свойств для переданного объекта..
366371
/// </summary>
367372
/// <param name="target">Объект, из которого получаем таблицу свойств.</param>
373+
/// <param name="withPrivate">Включая приватные</param>
368374
/// <returns>Таблица значений с колонками - Имя, Аннотации</returns>
369375
[ContextMethod("ПолучитьТаблицуСвойств", "GetPropertiesTable")]
370-
public ValueTable.ValueTable GetPropertiesTable(IValue target)
376+
public ValueTable.ValueTable GetPropertiesTable(IValue target, bool withPrivate = false)
371377
{
372378
ValueTable.ValueTable result = new ValueTable.ValueTable();
373379

374380
if(target.DataType == DataType.Object)
375-
FillPropertiesTable(result, target.AsObject().GetProperties());
381+
FillPropertiesTable(result, target.AsObject().GetProperties(withPrivate));
376382
else if (target.DataType == DataType.Type)
377383
{
378384
var type = target.GetRawValue() as TypeTypeValue;
379-
FillPropertiesTableForType(type, result);
385+
FillPropertiesTableForType(type, result, withPrivate);
380386
}
381387
else
382388
throw RuntimeException.InvalidArgumentType();
@@ -422,6 +428,7 @@ private static void FillPropertiesTable(ValueTable.ValueTable result, IEnumerabl
422428
{
423429
var nameColumn = result.Columns.Add("Имя", TypeDescription.StringType(), "Имя");
424430
var annotationsColumn = result.Columns.Add("Аннотации", new TypeDescription(), "Аннотации");
431+
var exportColumn = result.Columns.Add("Экспорт", TypeDescription.BooleanType(), "Экспорт");
425432
var systemVarNames = new string[] { "этотобъект", "thisobject" };
426433

427434
foreach (var propInfo in properties)
@@ -432,6 +439,7 @@ private static void FillPropertiesTable(ValueTable.ValueTable result, IEnumerabl
432439
new_row.Set(nameColumn, ValueFactory.Create(propInfo.Identifier));
433440

434441
new_row.Set(annotationsColumn, propInfo.AnnotationsCount != 0 ? CreateAnnotationTable(propInfo.Annotations) : EmptyAnnotationsTable());
442+
new_row.Set(exportColumn, ValueFactory.Create(propInfo.IsExport));
435443
}
436444
}
437445

src/ScriptEngine/Compiler/Compiler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,18 @@ private void BuildVariableDefinitions()
325325
var symbolicName = _lastExtractedLexem.Content;
326326
var annotations = ExtractAnnotations();
327327
var definition = _ctx.DefineVariable(symbolicName);
328+
NextToken();
328329
if (_inMethodScope)
329330
{
330331
if (_isStatementsDefined)
331332
{
332333
throw CompilerException.LateVarDefinition();
333334
}
335+
336+
if(_lastExtractedLexem.Token == Token.Export)
337+
{
338+
throw CompilerException.LocalExportVar();
339+
}
334340
}
335341
else
336342
{
@@ -346,10 +352,10 @@ private void BuildVariableDefinitions()
346352
Annotations = annotations,
347353
CanGet = true,
348354
CanSet = true,
349-
Index = definition.CodeIndex
355+
Index = definition.CodeIndex,
356+
IsExport = _lastExtractedLexem.Token == Token.Export
350357
});
351358
}
352-
NextToken();
353359
if (_lastExtractedLexem.Token == Token.Export)
354360
{
355361
_module.ExportedProperties.Add(new ExportedSymbol()

0 commit comments

Comments
 (0)