Skip to content

Commit 0ff7ea4

Browse files
committed
типы конструкторов - в идентификаторы, вызов как у методов
1 parent 4ba9eb7 commit 0ff7ea4

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,13 @@ private void GlobalCall(CallNode call, bool asFunction)
856856
else
857857
{
858858
// can be defined later
859-
var forwarded = new ForwardedMethodDecl();
860-
forwarded.identifier = identifier;
861-
forwarded.asFunction = asFunction;
862-
forwarded.location = identifierNode.Location;
863-
forwarded.factArguments = argList;
859+
var forwarded = new ForwardedMethodDecl
860+
{
861+
identifier = identifier,
862+
asFunction = asFunction,
863+
location = identifierNode.Location,
864+
factArguments = argList
865+
};
864866

865867
PushCallArguments(call.ArgumentList);
866868

@@ -878,17 +880,17 @@ private void PushCallArguments(BslSyntaxNode argList)
878880

879881
private void PushArgumentsList(BslSyntaxNode argList)
880882
{
881-
for (int i = 0; i < argList.Children.Count; i++)
883+
var arguments = argList.Children;
884+
for (int i = 0; i < arguments.Count; i++)
882885
{
883-
var passedArg = argList.Children[i];
884-
VisitCallArgument(passedArg);
886+
VisitCallArgument(arguments[i]);
885887
}
886888
}
887889

888890
[MethodImpl(MethodImplOptions.AggressiveInlining)]
889891
private void VisitCallArgument(BslSyntaxNode passedArg)
890892
{
891-
if (passedArg.Children.Count > 0)
893+
if (passedArg.Children.Count != 0)
892894
{
893895
VisitExpression(passedArg.Children[0]);
894896
}
@@ -1061,21 +1063,17 @@ private void MakeNewObjectDynamic(NewObjectNode node)
10611063

10621064
private void MakeNewObjectStatic(NewObjectNode node)
10631065
{
1064-
var cDef = new ConstDefinition()
1065-
{
1066-
Type = DataType.String,
1067-
Presentation = node.TypeNameNode.GetIdentifier()
1068-
};
1069-
AddCommand(OperationCode.PushConst, GetConstNumber(cDef));
1070-
1071-
var callArgs = 0;
10721066
if (node.ConstructorArguments != default)
10731067
{
1074-
PushArgumentsList(node.ConstructorArguments);
1075-
callArgs = node.ConstructorArguments.Children.Count;
1068+
PushCallArguments(node.ConstructorArguments);
1069+
}
1070+
else
1071+
{
1072+
AddCommand(OperationCode.ArgNum, 0);
10761073
}
10771074

1078-
AddCommand(OperationCode.NewInstance, callArgs);
1075+
var idNum = GetIdentNumber(node.TypeNameNode.GetIdentifier());
1076+
AddCommand(OperationCode.NewInstance, idNum);
10791077
}
10801078

10811079
private void ExitTryBlocks()
@@ -1322,8 +1320,7 @@ private int GetConstNumber(in ConstDefinition cDef)
13221320
}
13231321

13241322
private int GetIdentNumber(string ident)
1325-
{
1326-
1323+
{
13271324
var idx = _module.Identifiers.IndexOf(ident);
13281325
if (idx < 0)
13291326
{

src/ScriptEngine/Machine/MachineInstance.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,23 +1180,14 @@ private void Inc(int arg)
11801180
NextInstruction();
11811181
}
11821182

1183-
private void NewInstance(int argCount)
1184-
{
1185-
IValue[] argValues = new IValue[argCount];
1186-
// fact args
1187-
for (int i = argCount - 1; i >= 0; i--)
1188-
{
1189-
var argValue = _operationStack.Pop();
1190-
if(!argValue.IsSkippedArgument())
1191-
argValues[i] = RawValue(argValue);
1192-
}
1193-
1194-
var typeName = _operationStack.Pop().ToString(); // is BslStringValue by code generation
1183+
private void NewInstance(int arg)
1184+
{
1185+
var typeName = _module.Identifiers[arg];
11951186
if (!_typeManager.TryGetType(typeName, out var type))
11961187
{
11971188
throw RuntimeException.TypeIsNotDefined(typeName);
1198-
}
1199-
1189+
}
1190+
12001191
// TODO убрать cast после рефакторинга ITypeFactory
12011192
var factory = (TypeFactory)_typeManager.GetFactoryFor(type);
12021193
var context = new TypeActivationContext
@@ -1205,8 +1196,18 @@ private void NewInstance(int argCount)
12051196
TypeManager = _typeManager,
12061197
Services = _process.Services,
12071198
CurrentProcess = _process
1208-
};
1209-
1199+
};
1200+
1201+
int argCount = (int)_operationStack.Pop().AsNumber();
1202+
IValue[] argValues = new IValue[argCount];
1203+
// fact args
1204+
for (int i = argCount - 1; i >= 0; i--)
1205+
{
1206+
var argValue = _operationStack.Pop();
1207+
if(!argValue.IsSkippedArgument())
1208+
argValues[i] = RawValue(argValue);
1209+
}
1210+
12101211
var instance = (IValue)factory.Activate(context, argValues);
12111212
_operationStack.Push(instance);
12121213
NextInstruction();

0 commit comments

Comments
 (0)