LuaFunction.cs 1.2 KB

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