|
|
@@ -1,29 +1,137 @@
|
|
|
-using Lua.Internal;
|
|
|
using Lua.Runtime;
|
|
|
+using System.Runtime.CompilerServices;
|
|
|
|
|
|
namespace Lua;
|
|
|
|
|
|
public static class LuaThreadExtensions
|
|
|
{
|
|
|
- public static async ValueTask<LuaResult> RunAsync(this LuaThread thread, LuaClosure closure, int argumentCount, CancellationToken cancellationToken = default)
|
|
|
+ public static UseThreadLease RentUseThread(this LuaThread thread)
|
|
|
{
|
|
|
- var top = thread.CoreData.Stack.Count;
|
|
|
- await closure.InvokeAsync(new()
|
|
|
- {
|
|
|
- Thread = thread, ArgumentCount = argumentCount, ReturnFrameBase = top - argumentCount, SourceLine = null,
|
|
|
- }, cancellationToken);
|
|
|
+ return new(LuaUserThread.Create(thread));
|
|
|
+ }
|
|
|
|
|
|
- return new LuaResult(thread.Stack, top);
|
|
|
+ public static CoroutineLease RentCoroutine(this LuaThread thread, LuaFunction function, bool isProtectedMode = false)
|
|
|
+ {
|
|
|
+ return new(LuaCoroutine.Create(thread, function, isProtectedMode));
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- public static UseThreadLease RentUseThread(this LuaThread thread)
|
|
|
+
|
|
|
+
|
|
|
+ public static async ValueTask<int> DoStringAsync(this LuaThread thread, string source, Memory<LuaValue> buffer, string? chunkName = null, CancellationToken cancellationToken = default)
|
|
|
+ {
|
|
|
+ var closure = thread.State.Load(source, chunkName ?? source);
|
|
|
+ var count = await thread.RunAsync(closure, cancellationToken);
|
|
|
+ using var results = thread.ReadReturnValues(count);
|
|
|
+ results.AsSpan()[..Math.Min(buffer.Length, count)].CopyTo(buffer.Span);
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async ValueTask<LuaValue[]> DoStringAsync(this LuaThread thread, string source, string? chunkName = null, CancellationToken cancellationToken = default)
|
|
|
+ {
|
|
|
+ var closure = thread.State.Load(source, chunkName ?? source);
|
|
|
+ var count = await thread.RunAsync(closure, cancellationToken);
|
|
|
+ using var results = thread.ReadReturnValues(count);
|
|
|
+ return results.AsSpan().ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async ValueTask<int> DoFileAsync(this LuaThread thread, string path, Memory<LuaValue> buffer, CancellationToken cancellationToken = default)
|
|
|
{
|
|
|
- return new UseThreadLease(LuaUserThread.Create(thread));
|
|
|
+ var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
|
|
|
+ var fileName = "@" + Path.GetFileName(path);
|
|
|
+ var closure = thread.State.Load(bytes, fileName);
|
|
|
+ var count = await thread.RunAsync(closure, cancellationToken);
|
|
|
+ using var results = thread.ReadReturnValues(count);
|
|
|
+ results.AsSpan()[..Math.Min(buffer.Length, results.Length)].CopyTo(buffer.Span);
|
|
|
+ return results.Count;
|
|
|
}
|
|
|
|
|
|
- public static CoroutineLease RentCoroutine(this LuaThread thread,LuaFunction function ,bool isProtectedMode = false)
|
|
|
+ public static async ValueTask<LuaValue[]> DoFileAsync(this LuaThread thread, string path, CancellationToken cancellationToken = default)
|
|
|
{
|
|
|
- return new CoroutineLease(LuaCoroutine.Create(thread, function, isProtectedMode));
|
|
|
+ var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
|
|
|
+ var fileName = "@" + Path.GetFileName(path);
|
|
|
+ var closure = thread.State.Load(bytes, fileName);
|
|
|
+ var count = await thread.RunAsync(closure, cancellationToken);
|
|
|
+ using var results = thread.ReadReturnValues(count);
|
|
|
+ return results.AsSpan().ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void Push(this LuaThread thread, LuaValue value)
|
|
|
+ {
|
|
|
+ thread.CoreData!.Stack.Push(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Push(this LuaThread thread, params ReadOnlySpan<LuaValue> span)
|
|
|
+ {
|
|
|
+ thread.CoreData!.Stack.PushRange(span);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Pop(this LuaThread thread, int count)
|
|
|
+ {
|
|
|
+ thread.CoreData!.Stack.Pop(count);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static LuaValue Pop(this LuaThread thread)
|
|
|
+ {
|
|
|
+ return thread.CoreData!.Stack.Pop();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static LuaReturnValuesReader ReadReturnValues(this LuaThread thread, int argumentCount)
|
|
|
+ {
|
|
|
+ var stack = thread.CoreData!.Stack;
|
|
|
+ return new LuaReturnValuesReader(stack, stack.Count - argumentCount);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static ref readonly CallStackFrame GetCurrentFrame(this LuaThread thread)
|
|
|
+ {
|
|
|
+ return ref thread.CoreData!.CallStack.PeekRef();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ReadOnlySpan<LuaValue> GetStackValues(this LuaThread thread)
|
|
|
+ {
|
|
|
+ return thread.CoreData!.Stack.AsSpan();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ReadOnlySpan<CallStackFrame> GetCallStackFrames(this LuaThread thread)
|
|
|
+ {
|
|
|
+ return thread.CoreData!.CallStack.AsSpan();
|
|
|
+ }
|
|
|
+
|
|
|
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
+ internal static void PushCallStackFrame(this LuaThread thread, in CallStackFrame frame)
|
|
|
+ {
|
|
|
+ thread.CoreData!.CallStack.Push(frame);
|
|
|
+ }
|
|
|
+
|
|
|
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
+ internal static void PopCallStackFrameWithStackPop(this LuaThread thread)
|
|
|
+ {
|
|
|
+ var coreData = thread.CoreData!;
|
|
|
+
|
|
|
+ coreData.Stack.PopUntil(coreData!.CallStack.Pop().Base);
|
|
|
+ }
|
|
|
+
|
|
|
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
+ internal static void PopCallStackFrameWithStackPop(this LuaThread thread, int frameBase)
|
|
|
+ {
|
|
|
+ var coreData = thread.CoreData!;
|
|
|
+ coreData!.CallStack.Pop();
|
|
|
+ {
|
|
|
+ coreData.Stack.PopUntil(frameBase);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
+ internal static void PopCallStackFrame(this LuaThread thread)
|
|
|
+ {
|
|
|
+ var coreData = thread.CoreData!;
|
|
|
+ coreData!.CallStack.Pop();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Traceback GetTraceback(this LuaThread thread)
|
|
|
+ {
|
|
|
+ var frames = thread.GetCallStackFrames();
|
|
|
+
|
|
|
+ return new(thread.State) { RootFunc = frames[0].Function, StackFrames = thread.GetCallStackFrames()[1..].ToArray() };
|
|
|
}
|
|
|
}
|