Skip to content

Commit b95fd3b

Browse files
committed
передача ссылки в массив параметров, убрано обратное копирование; восстановлена проверка
Теперь Объект.Метод(Аргументы[0], Аргументы[1]...) и Рефлектор.ВызватьМетод(Объект, "Метод", Аргументы) работают одинаковопередача ссылки на массив параметров, убрано копирование; восстановлена проверка Теперь Объект.Метод(Аргументы[0], Аргументы[1]...) и Рефлектор.ВызватьМетод(Объект, "Метод", Аргументы) работают одинаково
1 parent c5cf643 commit b95fd3b

2 files changed

Lines changed: 16 additions & 52 deletions

File tree

‎src/OneScript.StandardLibrary/Reflector.cs‎

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,57 +52,28 @@ public IValue CallMethod(IBslProcess process, IRuntimeContextInstance target, st
5252
var methInfo = target.GetMethodInfo(methodIdx);
5353

5454
IValue[] argsToPass;
55-
if (arguments is not null)
56-
{
57-
if (target.DynamicMethodSignatures)
58-
{
59-
argsToPass = arguments.ToArray();
60-
}
61-
else
62-
{
63-
argsToPass = GetArgsToPass(arguments, methInfo.CallParameters);
64-
}
65-
}
55+
if (target.DynamicMethodSignatures)
56+
argsToPass = arguments?.ToArray() ?? [];
6657
else
67-
argsToPass = [];
58+
argsToPass = GetArgsToPass(arguments, methInfo.CallParameters);
6859

6960

7061
IValue retValue = BslUndefinedValue.Instance;
71-
try
72-
{
73-
if (methInfo.IsFunction())
74-
{
75-
target.CallAsFunction(methodIdx, argsToPass, out retValue, process);
76-
}
77-
else
78-
{
79-
target.CallAsProcedure(methodIdx, argsToPass, process);
80-
}
81-
}
82-
catch (Exception)
62+
if (methInfo.IsFunction())
8363
{
84-
CopyArgsBack(argsToPass, arguments);
85-
throw;
64+
target.CallAsFunction(methodIdx, argsToPass, out retValue, process);
8665
}
66+
else
67+
{
68+
target.CallAsProcedure(methodIdx, argsToPass, process);
69+
}
8770

88-
CopyArgsBack(argsToPass, arguments);
8971
return retValue;
9072
}
9173

92-
private static void CopyArgsBack(IValue[] argsToPass, ArrayImpl arguments)
93-
{
94-
if (arguments == null)
95-
return;
96-
97-
for (int i = 0; i < arguments.Count(); i++)
98-
{
99-
arguments.Set_unchecked(i, argsToPass[i] is IValueReference r ? r.Value : argsToPass[i]);
100-
}
101-
}
102-
10374
private static IValue[] GetArgsToPass(ArrayImpl arguments, ReadOnlySpan<BslCallParameter> parameters)
10475
{
105-
var argValues = arguments.ToArray();
76+
var argValues = arguments?.ToArray() ?? [];
10677
// ArrayImpl не может (не должен!) содержать null или NotAValidValue
10778

10879
int parCount = parameters.Length;
@@ -126,7 +97,7 @@ private static IValue[] GetArgsToPass(ArrayImpl arguments, ReadOnlySpan<BslCallP
12697
for (int i = 0; i < argCount; i++)
12798
{
12899
if (parameters[i].IsByRef)
129-
argsToPass[i] = Variable.Create(argValues[i], "");
100+
argsToPass[i] = Variable.CreateIndexedPropertyReference(arguments, ValueFactory.Create(i), "");
130101
}
131102

132103
return argsToPass;

‎src/ScriptEngine/Machine/MachineInstance.cs‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ private IValue[] PopArguments()
904904

905905
for (--argCount; argCount >= 0; --argCount)
906906
{
907-
args[argCount] = _operationStack.Pop();
907+
var arg = _operationStack.Pop();
908+
args[argCount] = arg.IsSkippedArgument() ? null : arg;
908909
}
909910
return args;
910911
}
@@ -1048,15 +1049,7 @@ private void PrepareContextCallArguments(int arg, out IRuntimeContextInstance co
10481049

10491050
if (context.DynamicMethodSignatures)
10501051
{
1051-
for (int i = 0; i < argCount; i++)
1052-
{
1053-
if (argValues[i].IsSkippedArgument())
1054-
{
1055-
argValues[i] = null;
1056-
}
1057-
}
1058-
1059-
return;
1052+
return;
10601053
}
10611054

10621055
var methodInfo = context.GetMethodInfo(methodId);
@@ -1079,7 +1072,7 @@ private void PrepareContextCallArguments(int arg, out IRuntimeContextInstance co
10791072
for (int i = 0; i < argCount; i++)
10801073
{
10811074
var argValue = factArgs[i];
1082-
if (!argValue.IsSkippedArgument())
1075+
if (argValue is not null)
10831076
{
10841077
if (methodParams[i].IsByRef)
10851078
{
@@ -1091,7 +1084,7 @@ private void PrepareContextCallArguments(int arg, out IRuntimeContextInstance co
10911084
argValues[i] = r.Value;
10921085
}
10931086
else if (!methodParams[i].HasDefaultValue)
1094-
throw RuntimeException.MissedArgument();
1087+
throw RuntimeException.MissedArgument();
10951088
}
10961089
}
10971090

0 commit comments

Comments
 (0)