LuaFunction.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Lua.Runtime;
  2. namespace Lua;
  3. public abstract partial class LuaFunction
  4. {
  5. public virtual string Name => GetType().Name;
  6. public async ValueTask<int> InvokeAsync(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  7. {
  8. var state = context.State;
  9. var thread = state.CurrentThread;
  10. var frame = new CallStackFrame
  11. {
  12. Base = context.StackPosition == null ? thread.Stack.Count - context.ArgumentCount : context.StackPosition.Value,
  13. CallPosition = context.SourcePosition,
  14. ChunkName = context.ChunkName ?? LuaState.DefaultChunkName,
  15. RootChunkName = context.RootChunkName ?? LuaState.DefaultChunkName,
  16. VariableArgumentCount = this is Closure closure ? context.ArgumentCount - closure.Proto.ParameterCount : 0,
  17. Function = this,
  18. };
  19. thread.PushCallStackFrame(frame);
  20. try
  21. {
  22. return await InvokeAsyncCore(context, buffer, cancellationToken);
  23. }
  24. finally
  25. {
  26. thread.PopCallStackFrame();
  27. }
  28. }
  29. protected abstract ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken);
  30. }