|
|
@@ -11,7 +11,8 @@ public sealed class BasicLibrary
|
|
|
|
|
|
public BasicLibrary()
|
|
|
{
|
|
|
- Functions = [
|
|
|
+ Functions =
|
|
|
+ [
|
|
|
new("assert", Assert),
|
|
|
new("collectgarbage", CollectGarbage),
|
|
|
new("dofile", DoFile),
|
|
|
@@ -36,7 +37,7 @@ public sealed class BasicLibrary
|
|
|
new("xpcall", XPCall),
|
|
|
];
|
|
|
|
|
|
- IPairsIterator = new("iterator", (context, buffer, cancellationToken) =>
|
|
|
+ IPairsIterator = new("iterator", (context, cancellationToken) =>
|
|
|
{
|
|
|
var table = context.GetArgument<LuaTable>(0);
|
|
|
var i = context.GetArgument<double>(1);
|
|
|
@@ -44,16 +45,12 @@ public sealed class BasicLibrary
|
|
|
i++;
|
|
|
if (table.TryGetValue(i, out var value))
|
|
|
{
|
|
|
- buffer.Span[0] = i;
|
|
|
- buffer.Span[1] = value;
|
|
|
+ return new(context.Return(i, value));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
- buffer.Span[1] = LuaValue.Nil;
|
|
|
+ return new(context.Return(LuaValue.Nil, LuaValue.Nil));
|
|
|
}
|
|
|
-
|
|
|
- return new(2);
|
|
|
});
|
|
|
|
|
|
PairsIterator = new("iterator", Next);
|
|
|
@@ -63,7 +60,7 @@ public sealed class BasicLibrary
|
|
|
readonly LuaFunction IPairsIterator;
|
|
|
readonly LuaFunction PairsIterator;
|
|
|
|
|
|
- public ValueTask<int> Assert(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Assert(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
|
|
|
@@ -78,49 +75,43 @@ public sealed class BasicLibrary
|
|
|
throw new LuaAssertionException(context.State.GetTraceback(), message);
|
|
|
}
|
|
|
|
|
|
- context.Arguments.CopyTo(buffer.Span);
|
|
|
- return new(context.ArgumentCount);
|
|
|
+ return new(context.Return(context.Arguments));
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> CollectGarbage(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> CollectGarbage(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
GC.Collect();
|
|
|
- return new(0);
|
|
|
+ return new(context.Return());
|
|
|
}
|
|
|
|
|
|
- public async ValueTask<int> DoFile(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public async ValueTask<int> DoFile(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<string>(0);
|
|
|
|
|
|
// do not use LuaState.DoFileAsync as it uses the newExecutionContext
|
|
|
var text = await File.ReadAllTextAsync(arg0, cancellationToken);
|
|
|
- var fileName = Path.GetFileName(arg0);
|
|
|
- var chunk = LuaCompiler.Default.Compile(text, "@"+fileName);
|
|
|
+ var fileName = "@" + Path.GetFileName(arg0);
|
|
|
+ var chunk = LuaCompiler.Default.Compile(text, fileName);
|
|
|
|
|
|
- return await new LuaClosure(context.State, chunk).InvokeAsync(context, buffer, cancellationToken);
|
|
|
+ return await new LuaClosure(context.State, chunk).InvokeAsync(context, cancellationToken);
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Error(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Error(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
- var value = context.ArgumentCount == 0 || context.Arguments[0].Type is LuaValueType.Nil
|
|
|
- ? "(error object is a nil value)"
|
|
|
+ var value = context.ArgumentCount == 0
|
|
|
+ ? LuaValue.Nil
|
|
|
: context.Arguments[0];
|
|
|
|
|
|
- Traceback t;
|
|
|
- try
|
|
|
- {
|
|
|
- t = context.State.GetTraceback(context.Thread);
|
|
|
-
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
+ var traceback = context.State.GetTraceback();
|
|
|
+ if (value.TryReadString(out var str))
|
|
|
{
|
|
|
- Console.WriteLine(e);
|
|
|
- throw;
|
|
|
+ value = $"{traceback.RootChunkName}:{traceback.LastPosition.Line}: {str}";
|
|
|
}
|
|
|
- throw new LuaRuntimeException(t, value);
|
|
|
+
|
|
|
+ throw new LuaRuntimeException(traceback, value);
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> GetMetatable(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> GetMetatable(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
|
|
|
@@ -128,26 +119,26 @@ public sealed class BasicLibrary
|
|
|
{
|
|
|
if (table.Metatable == null)
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
+ context.Return(LuaValue.Nil);
|
|
|
}
|
|
|
else if (table.Metatable.TryGetValue(Metamethods.Metatable, out var metatable))
|
|
|
{
|
|
|
- buffer.Span[0] = metatable;
|
|
|
+ context.Return(metatable);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- buffer.Span[0] = table.Metatable;
|
|
|
+ context.Return(table.Metatable);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
+ context.Return(LuaValue.Nil);
|
|
|
}
|
|
|
|
|
|
- return new(1);
|
|
|
+ return default;
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> IPairs(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> IPairs(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
|
|
|
@@ -159,16 +150,13 @@ public sealed class BasicLibrary
|
|
|
LuaRuntimeException.AttemptInvalidOperation(context.State.GetTraceback(), "call", metamethod);
|
|
|
}
|
|
|
|
|
|
- return function.InvokeAsync(context, buffer, cancellationToken);
|
|
|
+ return function.InvokeAsync(context, cancellationToken);
|
|
|
}
|
|
|
|
|
|
- buffer.Span[0] = IPairsIterator;
|
|
|
- buffer.Span[1] = arg0;
|
|
|
- buffer.Span[2] = 0;
|
|
|
- return new(3);
|
|
|
+ return new(context.Return(IPairsIterator, arg0, 0));
|
|
|
}
|
|
|
|
|
|
- public async ValueTask<int> LoadFile(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public async ValueTask<int> LoadFile(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
// Lua-CSharp does not support binary chunks, the mode argument is ignored.
|
|
|
var arg0 = context.GetArgument<string>(0);
|
|
|
@@ -180,20 +168,17 @@ public sealed class BasicLibrary
|
|
|
try
|
|
|
{
|
|
|
var text = await File.ReadAllTextAsync(arg0, cancellationToken);
|
|
|
- var fileName = Path.GetFileName(arg0);
|
|
|
+ var fileName = "@" + Path.GetFileName(arg0);
|
|
|
var chunk = LuaCompiler.Default.Compile(text, fileName);
|
|
|
- buffer.Span[0] = new LuaClosure(context.State, chunk, arg2);
|
|
|
- return 1;
|
|
|
+ return context.Return(new LuaClosure(context.State, chunk, arg2));
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
- buffer.Span[1] = ex.Message;
|
|
|
- return 2;
|
|
|
+ return context.Return(LuaValue.Nil, ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Load(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Load(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
// Lua-CSharp does not support binary chunks, the mode argument is ignored.
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
@@ -212,8 +197,7 @@ public sealed class BasicLibrary
|
|
|
if (arg0.TryRead<string>(out var str))
|
|
|
{
|
|
|
var chunk = LuaCompiler.Default.Compile(str, arg1 ?? str);
|
|
|
- buffer.Span[0] = new LuaClosure(context.State, chunk, arg3);
|
|
|
- return new(1);
|
|
|
+ return new(context.Return(new LuaClosure(context.State, chunk, arg3)));
|
|
|
}
|
|
|
else if (arg0.TryRead<LuaFunction>(out var function))
|
|
|
{
|
|
|
@@ -228,31 +212,26 @@ public sealed class BasicLibrary
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
- buffer.Span[1] = ex.Message;
|
|
|
- return new(2);
|
|
|
+ return new(context.Return(LuaValue.Nil, ex.Message));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Next(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Next(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
var arg1 = context.HasArgument(1) ? context.Arguments[1] : LuaValue.Nil;
|
|
|
|
|
|
if (arg0.TryGetNext(arg1, out var kv))
|
|
|
{
|
|
|
- buffer.Span[0] = kv.Key;
|
|
|
- buffer.Span[1] = kv.Value;
|
|
|
- return new(2);
|
|
|
+ return new(context.Return(kv.Key, kv.Value));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- buffer.Span[0] = LuaValue.Nil;
|
|
|
- return new(1);
|
|
|
+ return new(context.Return(LuaValue.Nil));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Pairs(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Pairs(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
|
|
|
@@ -264,115 +243,94 @@ public sealed class BasicLibrary
|
|
|
LuaRuntimeException.AttemptInvalidOperation(context.State.GetTraceback(), "call", metamethod);
|
|
|
}
|
|
|
|
|
|
- return function.InvokeAsync(context, buffer, cancellationToken);
|
|
|
+ return function.InvokeAsync(context, cancellationToken);
|
|
|
}
|
|
|
|
|
|
- buffer.Span[0] = PairsIterator;
|
|
|
- buffer.Span[1] = arg0;
|
|
|
- buffer.Span[2] = LuaValue.Nil;
|
|
|
- return new(3);
|
|
|
+ return new(context.Return(PairsIterator, arg0, LuaValue.Nil));
|
|
|
}
|
|
|
|
|
|
- public async ValueTask<int> PCall(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public async ValueTask<int> PCall(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaFunction>(0);
|
|
|
-
|
|
|
try
|
|
|
{
|
|
|
- using var methodBuffer = new PooledArray<LuaValue>(1024);
|
|
|
+ var count = await arg0.InvokeAsync(context with { State = context.State, ArgumentCount = context.ArgumentCount - 1, FrameBase = context.FrameBase + 1, ReturnFrameBase = context.ReturnFrameBase + 1 }, cancellationToken);
|
|
|
|
|
|
- var resultCount = await arg0.InvokeAsync(context with
|
|
|
- {
|
|
|
- State = context.State,
|
|
|
- ArgumentCount = context.ArgumentCount - 1,
|
|
|
- FrameBase = context.FrameBase + 1,
|
|
|
- }, methodBuffer.AsMemory(), cancellationToken);
|
|
|
-
|
|
|
- buffer.Span[0] = true;
|
|
|
- methodBuffer.AsSpan()[..resultCount].CopyTo(buffer.Span[1..]);
|
|
|
-
|
|
|
- return resultCount + 1;
|
|
|
+ context.Thread.Stack.Get(context.ReturnFrameBase) = true;
|
|
|
+ return count + 1;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- buffer.Span[0] = false;
|
|
|
if (ex is LuaRuntimeException { ErrorObject: not null } luaEx)
|
|
|
{
|
|
|
- buffer.Span[1] = luaEx.ErrorObject.Value;
|
|
|
+ return context.Return(false, luaEx.ErrorObject.Value);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- buffer.Span[1] = ex.Message;
|
|
|
+ return context.Return(false, ex.Message);
|
|
|
}
|
|
|
-
|
|
|
- return 2;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public async ValueTask<int> Print(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public async ValueTask<int> Print(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
- using var methodBuffer = new PooledArray<LuaValue>(1);
|
|
|
-
|
|
|
for (int i = 0; i < context.ArgumentCount; i++)
|
|
|
{
|
|
|
- await context.Arguments[i].CallToStringAsync(context, methodBuffer.AsMemory(), cancellationToken);
|
|
|
- Console.Write(methodBuffer[0]);
|
|
|
+ var top = context.Thread.Stack.Count;
|
|
|
+ await context.Arguments[i].CallToStringAsync(context, cancellationToken);
|
|
|
+ Console.Write(context.Thread.Stack.Get(top).ToString());
|
|
|
Console.Write('\t');
|
|
|
}
|
|
|
|
|
|
Console.WriteLine();
|
|
|
- return 0;
|
|
|
+ return context.Return();
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> RawEqual(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> RawEqual(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
var arg1 = context.GetArgument(1);
|
|
|
|
|
|
- buffer.Span[0] = arg0 == arg1;
|
|
|
- return new(1);
|
|
|
+ return new(context.Return(arg0 == arg1));
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> RawGet(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> RawGet(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
var arg1 = context.GetArgument(1);
|
|
|
-
|
|
|
- buffer.Span[0] = arg0[arg1];
|
|
|
- return new(1);
|
|
|
+ return new(context.Return(arg0[arg1]));
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> RawLen(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> RawLen(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
|
|
|
if (arg0.TryRead<LuaTable>(out var table))
|
|
|
{
|
|
|
- buffer.Span[0] = table.ArrayLength;
|
|
|
+ return new(context.Return(table.ArrayLength));
|
|
|
}
|
|
|
else if (arg0.TryRead<string>(out var str))
|
|
|
{
|
|
|
- buffer.Span[0] = str.Length;
|
|
|
+ return new(context.Return(str.Length));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
LuaRuntimeException.BadArgument(context.State.GetTraceback(), 2, "rawlen", [LuaValueType.String, LuaValueType.Table]);
|
|
|
+ return default;
|
|
|
}
|
|
|
-
|
|
|
- return new(1);
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> RawSet(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> RawSet(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
var arg1 = context.GetArgument(1);
|
|
|
var arg2 = context.GetArgument(2);
|
|
|
|
|
|
arg0[arg1] = arg2;
|
|
|
- return new(0);
|
|
|
+ return new(context.Return());
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Select(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Select(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
|
|
|
@@ -387,14 +345,11 @@ public sealed class BasicLibrary
|
|
|
? context.Arguments[index..]
|
|
|
: context.Arguments[(context.ArgumentCount + index)..];
|
|
|
|
|
|
- span.CopyTo(buffer.Span);
|
|
|
-
|
|
|
- return new(span.Length);
|
|
|
+ return new(context.Return(span));
|
|
|
}
|
|
|
else if (arg0.TryRead<string>(out var str) && str == "#")
|
|
|
{
|
|
|
- buffer.Span[0] = context.ArgumentCount - 1;
|
|
|
- return new(1);
|
|
|
+ return new(context.Return(context.ArgumentCount - 1));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -403,7 +358,7 @@ public sealed class BasicLibrary
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> SetMetatable(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> SetMetatable(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaTable>(0);
|
|
|
var arg1 = context.GetArgument(1);
|
|
|
@@ -426,11 +381,11 @@ public sealed class BasicLibrary
|
|
|
arg0.Metatable = arg1.Read<LuaTable>();
|
|
|
}
|
|
|
|
|
|
- buffer.Span[0] = arg0;
|
|
|
- return new(1);
|
|
|
+
|
|
|
+ return new(context.Return(arg0));
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> ToNumber(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> ToNumber(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var e = context.GetArgument(0);
|
|
|
int? toBase = context.HasArgument(1)
|
|
|
@@ -477,6 +432,7 @@ public sealed class BasicLibrary
|
|
|
{
|
|
|
span = span[1..];
|
|
|
}
|
|
|
+
|
|
|
if (span.Length == 0) goto END;
|
|
|
|
|
|
if (toBase == 16 && span.Length > 2 && span[0] is '0' && span[1] is 'x' or 'X')
|
|
|
@@ -500,13 +456,13 @@ public sealed class BasicLibrary
|
|
|
}
|
|
|
|
|
|
END:
|
|
|
- if (value != null && double.IsNaN(value.Value))
|
|
|
+ if (value is double.NaN)
|
|
|
{
|
|
|
value = null;
|
|
|
}
|
|
|
|
|
|
- buffer.Span[0] = value == null ? LuaValue.Nil : value.Value;
|
|
|
- return new(1);
|
|
|
+
|
|
|
+ return new(context.Return(value ?? LuaValue.Nil));
|
|
|
}
|
|
|
|
|
|
static double StringToDouble(ReadOnlySpan<char> text, int toBase)
|
|
|
@@ -566,17 +522,18 @@ public sealed class BasicLibrary
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> ToString(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> ToString(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
- return arg0.CallToStringAsync(context, buffer, cancellationToken);
|
|
|
+ context.Return();
|
|
|
+ return arg0.CallToStringAsync(context, cancellationToken);
|
|
|
}
|
|
|
|
|
|
- public ValueTask<int> Type(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public ValueTask<int> Type(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument(0);
|
|
|
|
|
|
- buffer.Span[0] = arg0.Type switch
|
|
|
+ return new(context.Return(arg0.Type switch
|
|
|
{
|
|
|
LuaValueType.Nil => "nil",
|
|
|
LuaValueType.Boolean => "boolean",
|
|
|
@@ -584,56 +541,35 @@ public sealed class BasicLibrary
|
|
|
LuaValueType.Number => "number",
|
|
|
LuaValueType.Function => "function",
|
|
|
LuaValueType.Thread => "thread",
|
|
|
+ LuaValueType.LightUserData => "userdata",
|
|
|
LuaValueType.UserData => "userdata",
|
|
|
LuaValueType.Table => "table",
|
|
|
_ => throw new NotImplementedException(),
|
|
|
- };
|
|
|
-
|
|
|
- return new(1);
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
- public async ValueTask<int> XPCall(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
|
|
|
+ public async ValueTask<int> XPCall(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var arg0 = context.GetArgument<LuaFunction>(0);
|
|
|
var arg1 = context.GetArgument<LuaFunction>(1);
|
|
|
|
|
|
- using var methodBuffer = new PooledArray<LuaValue>(1024);
|
|
|
- methodBuffer.AsSpan().Clear();
|
|
|
-
|
|
|
try
|
|
|
{
|
|
|
- var resultCount = await arg0.InvokeAsync(context with
|
|
|
- {
|
|
|
- State = context.State,
|
|
|
- ArgumentCount = context.ArgumentCount - 2,
|
|
|
- FrameBase = context.FrameBase + 2,
|
|
|
- }, methodBuffer.AsMemory(), cancellationToken);
|
|
|
-
|
|
|
- buffer.Span[0] = true;
|
|
|
- methodBuffer.AsSpan()[..resultCount].CopyTo(buffer.Span[1..]);
|
|
|
+ var count = await arg0.InvokeAsync(context with { State = context.State, ArgumentCount = context.ArgumentCount - 2, FrameBase = context.FrameBase + 2, ReturnFrameBase = context.ReturnFrameBase + 1 }, cancellationToken);
|
|
|
|
|
|
- return resultCount + 1;
|
|
|
+ context.Thread.Stack.Get(context.ReturnFrameBase) = true;
|
|
|
+ return count + 1;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- methodBuffer.AsSpan().Clear();
|
|
|
var error = ex is LuaRuntimeException { ErrorObject: not null } luaEx ? luaEx.ErrorObject.Value : ex.Message;
|
|
|
|
|
|
context.State.Push(error);
|
|
|
|
|
|
// invoke error handler
|
|
|
- await arg1.InvokeAsync(context with
|
|
|
- {
|
|
|
- State = context.State,
|
|
|
- ArgumentCount = 1,
|
|
|
- FrameBase = context.Thread.Stack.Count - 1,
|
|
|
- }, methodBuffer.AsMemory(), cancellationToken);
|
|
|
-
|
|
|
- buffer.Span[0] = false;
|
|
|
- buffer.Span[1] = methodBuffer[0];
|
|
|
-
|
|
|
-
|
|
|
- return 2;
|
|
|
+ var count = await arg1.InvokeAsync(context with { State = context.State, ArgumentCount = 1, FrameBase = context.Thread.Stack.Count - 1, ReturnFrameBase = context.ReturnFrameBase + 1 }, cancellationToken);
|
|
|
+ context.Thread.Stack.Get(context.ReturnFrameBase) = false;
|
|
|
+ return count + 1;
|
|
|
}
|
|
|
}
|
|
|
}
|