Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Рефлектор возвращает приватные поля и методы #1242

Merged
merged 8 commits into from
Jan 12, 2023
22 changes: 21 additions & 1 deletion src/NUnitTests/CompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,25 @@ Если Ложь Тогда
}
Assert.IsTrue(exceptionThrown, "Отсутствует точка с запятой между операторами!");
}
}

[Test]
public void TestLocalExportVar()
{
var moduleSource = host.Engine.Loader.FromString(
@"Процедура Проц1()
Перем Переменная Экспорт;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Съехали отступы (лучше использовать пробелы, меньше проблем)

КонецПроцедуры");

bool exceptionThrown = false;
try
{
_ = host.Engine.GetCompilerService().Compile(moduleSource);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Странно, что здесь присваивание. Ведь можно же просто вызвать функцию, как процедуру

host.Engine.GetCompilerService().Compile(moduleSource);

}
catch (CompilerException)
{
exceptionThrown = true;
}
Assert.IsTrue(exceptionThrown, "В теле процедуры или функции не может быть объявлена экспортная переменная!");
}
}
}
17 changes: 9 additions & 8 deletions src/ScriptEngine/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,18 @@ private void BuildVariableDefinitions()
var symbolicName = _lastExtractedLexem.Content;
var annotations = ExtractAnnotations();
var definition = _ctx.DefineVariable(symbolicName);
var variableInfo = new VariableInfo();
NextToken();
if (_inMethodScope)
{
if (_isStatementsDefined)
{
throw CompilerException.LateVarDefinition();
}

if(_lastExtractedLexem.Token == Token.Export)
{
throw CompilerException.LocalExportVar();
}
}
else
{
Expand All @@ -341,29 +346,25 @@ private void BuildVariableDefinitions()
}

_module.VariableRefs.Add(definition);
variableInfo = new VariableInfo()
_module.Variables.Add(new VariableInfo()
{
Identifier = symbolicName,
Annotations = annotations,
CanGet = true,
CanSet = true,
Index = definition.CodeIndex,
IsExport = false
};
IsExport = _lastExtractedLexem.Token == Token.Export
});
}
NextToken();
if (_lastExtractedLexem.Token == Token.Export)
{
_module.ExportedProperties.Add(new ExportedSymbol()
{
SymbolicName = symbolicName,
Index = definition.CodeIndex
});
variableInfo.IsExport = true;
NextToken();
}
if(variableInfo.Identifier != null)
_module.Variables.Add(variableInfo);
if (_lastExtractedLexem.Token == Token.Comma)
{
NextToken();
Expand Down
6 changes: 6 additions & 0 deletions src/ScriptEngine/Compiler/CompilerExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ internal static CompilerException LateVarDefinition()
+ "en='Variable declarations must be placed at beginning of module, procedure, or function'"));
}

internal static CompilerException LocalExportVar()
{
return new CompilerException(Locale.NStr("ru='В теле процедуры или функции не может быть объявлена экспортная переменная';"
+ "en='An export variable cannot be declared in the body of a procedure or function'"));
}

internal static CompilerException TokenExpected(params Token[] expected)
{
var names = expected.Select(x => Enum.GetName(typeof(Token), x));
Expand Down