RShiftFunction.cs 957 B

1234567891011121314151617181920212223242526272829303132333435
  1. namespace Lua.Standard.Bitwise;
  2. public sealed class RShiftFunction : LuaFunction
  3. {
  4. public override string Name => "rshift";
  5. public static readonly RShiftFunction Instance = new();
  6. protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  7. {
  8. var x = context.GetArgument<double>(0);
  9. var disp = context.GetArgument<double>(1);
  10. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, x);
  11. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 2, disp);
  12. var v = Bit32Helper.ToUInt32(x);
  13. var a = (int)disp;
  14. if (Math.Abs(a) >= 32)
  15. {
  16. v = 0;
  17. }
  18. else if (a < 0)
  19. {
  20. v <<= -a;
  21. }
  22. else
  23. {
  24. v >>= a;
  25. }
  26. buffer.Span[0] = v;
  27. return new(1);
  28. }
  29. }