Skip to content

Commit 4ba9eb7

Browse files
committed
VM: имена свойств и методов в отдельный список
1 parent 7e7de3a commit 4ba9eb7

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/ScriptEngine/Compiler/ModuleDumpWriter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ private void WriteImage(TextWriter output, StackRuntimeModule module)
129129
output.WriteLine(
130130
$"{i,-3}:type: {item.SystemType.Alias}, val: {item}");
131131
}
132+
output.WriteLine(".identifiers");
133+
for (int i = 0; i < module.Identifiers.Count; i++)
134+
{
135+
var item = module.Identifiers[i];
136+
output.WriteLine(
137+
$"{i,-3}: {item}");
138+
}
132139
output.WriteLine(".code");
133140
for (int i = 0; i < module.Code.Count; i++)
134141
{

src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ protected override void VisitReturnNode(BslSyntaxNode node)
489489
protected override void VisitRaiseNode(BslSyntaxNode node)
490490
{
491491
int arg = -1;
492-
if (node.Children.Any())
492+
if (node.Children.Count != 0)
493493
{
494494
VisitExpression(node.Children[0]);
495495
arg = 0;
@@ -728,24 +728,17 @@ private void ResolveObjectMethod(BslSyntaxNode callNode, bool asFunction)
728728

729729
PushCallArguments(args);
730730

731-
var cDef = new ConstDefinition();
732-
cDef.Type = DataType.String;
733-
cDef.Presentation = name.GetIdentifier();
734-
int lastIdentifierConst = GetConstNumber(cDef);
731+
int lastIdentifierIndex = GetIdentNumber(name.GetIdentifier());
735732

736733
if (asFunction)
737-
AddCommand(OperationCode.ResolveMethodFunc, lastIdentifierConst);
734+
AddCommand(OperationCode.ResolveMethodFunc, lastIdentifierIndex);
738735
else
739-
AddCommand(OperationCode.ResolveMethodProc, lastIdentifierConst);
736+
AddCommand(OperationCode.ResolveMethodProc, lastIdentifierIndex);
740737
}
741738

742739
private void ResolveProperty(string identifier)
743740
{
744-
var cDef = new ConstDefinition();
745-
cDef.Type = DataType.String;
746-
cDef.Presentation = identifier;
747-
var identifierConstIndex = GetConstNumber(cDef);
748-
AddCommand(OperationCode.ResolveProp, identifierConstIndex);
741+
AddCommand(OperationCode.ResolveProp, GetIdentNumber(identifier));
749742
}
750743

751744
private int PushVariable(TerminalNode node)
@@ -1328,6 +1321,19 @@ private int GetConstNumber(in ConstDefinition cDef)
13281321
return idx;
13291322
}
13301323

1324+
private int GetIdentNumber(string ident)
1325+
{
1326+
1327+
var idx = _module.Identifiers.IndexOf(ident);
1328+
if (idx < 0)
1329+
{
1330+
idx = _module.Identifiers.Count;
1331+
_module.Identifiers.Add(ident);
1332+
}
1333+
return idx;
1334+
}
1335+
1336+
13311337
private int GetMethodRefNumber(in SymbolBinding methodBinding)
13321338
{
13331339
var descriptor = _ctx.GetBinding(methodBinding.ScopeNumber);

src/ScriptEngine/Machine/MachineInstance.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ private void PushConst(int arg)
680680
{
681681
_operationStack.Push(_module.Constants[arg]);
682682
NextInstruction();
683-
}
684-
683+
}
684+
685685
private void PushBool(int arg)
686686
{
687687
_operationStack.Push(BslBooleanValue.Create(arg == 1));
@@ -1011,7 +1011,7 @@ private void ResolveProp(int arg)
10111011
var objIValue = _operationStack.Pop();
10121012

10131013
var context = objIValue.AsObject();
1014-
var propName = _module.Constants[arg].ToString(_process);
1014+
var propName = _module.Identifiers[arg];
10151015
var propNum = context.GetPropertyNumber(propName);
10161016

10171017
var propReference = Variable.CreateContextPropertyReference(context, propNum, "stackvar");
@@ -1048,7 +1048,7 @@ private void PrepareContextCallArguments(int arg, out IRuntimeContextInstance co
10481048

10491049
var objIValue = _operationStack.Pop();
10501050
context = objIValue.AsObject();
1051-
var methodName = _module.Constants[arg].ToString(_process);
1051+
var methodName = _module.Identifiers[arg];
10521052
methodId = context.GetMethodNumber(methodName);
10531053

10541054
if (context.DynamicMethodSignatures)
@@ -1191,7 +1191,7 @@ private void NewInstance(int argCount)
11911191
argValues[i] = RawValue(argValue);
11921192
}
11931193

1194-
var typeName = PopRawBslValue().ToString(_process);
1194+
var typeName = _operationStack.Pop().ToString(); // is BslStringValue by code generation
11951195
if (!_typeManager.TryGetType(typeName, out var type))
11961196
{
11971197
throw RuntimeException.TypeIsNotDefined(typeName);

src/ScriptEngine/Machine/StackRuntimeModule.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ public StackRuntimeModule(Type ownerType)
2626
public int EntryMethodIndex { get; set; } = -1;
2727

2828
public List<BslPrimitiveValue> Constants { get; } = new List<BslPrimitiveValue>();
29+
30+
internal List<string> Identifiers { get; } = new List<string>();
2931

30-
internal IList<ModuleSymbolBinding> VariableRefs { get; } = new List<ModuleSymbolBinding>();
32+
internal List<ModuleSymbolBinding> VariableRefs { get; } = new List<ModuleSymbolBinding>();
3133

32-
internal IList<ModuleSymbolBinding> MethodRefs { get; } = new List<ModuleSymbolBinding>();
34+
internal List<ModuleSymbolBinding> MethodRefs { get; } = new List<ModuleSymbolBinding>();
3335

3436
#region IExecutableModule members
3537

@@ -52,7 +54,7 @@ public BslScriptMethodInfo ModuleBody
5254

5355
public IList<BslScriptMethodInfo> Methods { get; } = new List<BslScriptMethodInfo>();
5456

55-
public IList<Command> Code { get; } = new List<Command>(512);
57+
public List<Command> Code { get; } = new List<Command>(512);
5658

5759
public SourceCode Source { get; set; }
5860

0 commit comments

Comments
 (0)