Skip to content

Commit 10aa8e1

Browse files
committed
вынесено создание объекта по имени типа
1 parent 0ff7ea4 commit 10aa8e1

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ private void MakeNewObjectDynamic(NewObjectNode node)
10511051
var argsPassed = node.ConstructorArguments.Children.Count;
10521052
if (argsPassed == 1)
10531053
{
1054-
PushArgumentsList(node.ConstructorArguments);
1054+
VisitCallArgument(node.ConstructorArguments.Children[0]); ;
10551055
}
10561056
else if (argsPassed > 1)
10571057
{

src/ScriptEngine/Machine/MachineInstance.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This Source Code Form is subject to the terms of the
2424
using OneScript.Values;
2525
using ScriptEngine.Compiler;
2626
using ScriptEngine.Machine.Debugger;
27+
using System.Dynamic;
2728

2829
namespace ScriptEngine.Machine
2930
{
@@ -1182,22 +1183,6 @@ private void Inc(int arg)
11821183

11831184
private void NewInstance(int arg)
11841185
{
1185-
var typeName = _module.Identifiers[arg];
1186-
if (!_typeManager.TryGetType(typeName, out var type))
1187-
{
1188-
throw RuntimeException.TypeIsNotDefined(typeName);
1189-
}
1190-
1191-
// TODO убрать cast после рефакторинга ITypeFactory
1192-
var factory = (TypeFactory)_typeManager.GetFactoryFor(type);
1193-
var context = new TypeActivationContext
1194-
{
1195-
TypeName = typeName,
1196-
TypeManager = _typeManager,
1197-
Services = _process.Services,
1198-
CurrentProcess = _process
1199-
};
1200-
12011186
int argCount = (int)_operationStack.Pop().AsNumber();
12021187
IValue[] argValues = new IValue[argCount];
12031188
// fact args
@@ -1206,10 +1191,10 @@ private void NewInstance(int arg)
12061191
var argValue = _operationStack.Pop();
12071192
if(!argValue.IsSkippedArgument())
12081193
argValues[i] = RawValue(argValue);
1209-
}
1210-
1211-
var instance = (IValue)factory.Activate(context, argValues);
1212-
_operationStack.Push(instance);
1194+
}
1195+
1196+
var typeName = _module.Identifiers[arg];
1197+
_operationStack.Push(CreateInstance(typeName, argValues));
12131198
NextInstruction();
12141199
}
12151200

@@ -2396,13 +2381,19 @@ private void NewFunc(int argCount)
23962381
else
23972382
argValues = Array.Empty<IValue>();
23982383
}
2399-
2384+
24002385
var typeName = PopRawBslValue().ToString(_process);
2386+
_operationStack.Push(CreateInstance(typeName, argValues));
2387+
NextInstruction();
2388+
}
2389+
2390+
private IValue CreateInstance(string typeName, IValue[] args)
2391+
{
24012392
if (!_typeManager.TryGetType(typeName, out var type))
24022393
{
24032394
throw RuntimeException.TypeIsNotDefined(typeName);
24042395
}
2405-
2396+
24062397
// TODO убрать cast после рефакторинга ITypeFactory
24072398
var factory = (TypeFactory)_typeManager.GetFactoryFor(type);
24082399
var context = new TypeActivationContext
@@ -2411,17 +2402,14 @@ private void NewFunc(int argCount)
24112402
TypeManager = _typeManager,
24122403
Services = _process.Services,
24132404
CurrentProcess = _process
2414-
};
2415-
2416-
var instance = factory.Activate(context, argValues);
2417-
_operationStack.Push(instance);
2418-
NextInstruction();
2405+
};
2406+
return factory.Activate(context, args);
24192407
}
24202408

24212409
#endregion
2422-
2410+
24232411
#endregion
2424-
2412+
24252413
private StackRuntimeModule CompileExpressionModule(string expression)
24262414
{
24272415
var entryId = CurrentCodeEntry().ToString();

0 commit comments

Comments
 (0)