AssertFunction.cs 799 B

1234567891011121314151617181920212223242526
  1. namespace Lua.Standard.Basic;
  2. public sealed class AssertFunction : LuaFunction
  3. {
  4. public override string Name => "assert";
  5. public static readonly AssertFunction Instance = new();
  6. protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  7. {
  8. var arg0 = context.GetArgument(0);
  9. if (!arg0.ToBoolean())
  10. {
  11. var message = "assertion failed!";
  12. if (context.HasArgument(1))
  13. {
  14. message = context.GetArgument<string>(1);
  15. }
  16. throw new LuaAssertionException(context.State.GetTraceback(), message);
  17. }
  18. context.Arguments.CopyTo(buffer.Span);
  19. return new(context.ArgumentCount);
  20. }
  21. }