AssertFunction.cs 738 B

12345678910111213141516171819202122232425
  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.ReadArgument(0);
  9. if (!arg0.ToBoolean())
  10. {
  11. var message = "assertion failed!";
  12. if (context.ArgumentCount >= 2)
  13. {
  14. message = context.ReadArgument<string>(1);
  15. }
  16. throw new LuaAssertionException(context.State.GetTraceback(), message);
  17. }
  18. return new(0);
  19. }
  20. }