DoFileFunction.cs 848 B

12345678910111213141516171819202122
  1. using Lua.CodeAnalysis.Compilation;
  2. using Lua.Runtime;
  3. namespace Lua.Standard.Basic;
  4. public sealed class DoFileFunction : LuaFunction
  5. {
  6. public override string Name => "dofile";
  7. public static readonly DoFileFunction Instance = new();
  8. protected override async ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  9. {
  10. var arg0 = context.GetArgument<string>(0);
  11. // do not use LuaState.DoFileAsync as it uses the new LuaFunctionExecutionContext
  12. var text = await File.ReadAllTextAsync(arg0, cancellationToken);
  13. var fileName = Path.GetFileName(arg0);
  14. var chunk = LuaCompiler.Default.Compile(text, fileName);
  15. return await new Closure(context.State, chunk).InvokeAsync(context, buffer, cancellationToken);
  16. }
  17. }