BtestFunction.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. namespace Lua.Standard.Bitwise;
  2. public sealed class BtestFunction : LuaFunction
  3. {
  4. public override string Name => "btest";
  5. public static readonly BtestFunction Instance = new();
  6. protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  7. {
  8. if (context.ArgumentCount == 0)
  9. {
  10. buffer.Span[0] = true;
  11. return new(1);
  12. }
  13. var arg0 = context.GetArgument<double>(0);
  14. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
  15. var value = Bit32Helper.ToUInt32(arg0);
  16. for (int i = 1; i < context.ArgumentCount; i++)
  17. {
  18. var arg = context.GetArgument<double>(i);
  19. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1 + i, arg);
  20. var v = Bit32Helper.ToUInt32(arg);
  21. value &= v;
  22. }
  23. buffer.Span[0] = value != 0;
  24. return new(1);
  25. }
  26. }