Browse Source

Add: bit32.btest

AnnulusGames 1 year ago
parent
commit
1416b9f93b
2 changed files with 28 additions and 0 deletions
  1. 27 0
      src/Lua/Standard/Bitwise/BtestFunction.cs
  2. 1 0
      src/Lua/Standard/OpenLibExtensions.cs

+ 27 - 0
src/Lua/Standard/Bitwise/BtestFunction.cs

@@ -0,0 +1,27 @@
+namespace Lua.Standard.Bitwise;
+
+public sealed class BtestFunction : LuaFunction
+{
+    public override string Name => "btest";
+    public static readonly BtestFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        if (context.ArgumentCount == 0)
+        {
+            buffer.Span[0] = true;
+            return new(1);
+        }
+
+        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+
+        for (int i = 1; i < context.ArgumentCount; i++)
+        {
+            var v = Bit32Helper.ToUInt32(context.GetArgument<double>(i));
+            value &= v;
+        }
+
+        buffer.Span[0] = value != 0;
+        return new(1);
+    }
+}

+ 1 - 0
src/Lua/Standard/OpenLibExtensions.cs

@@ -104,6 +104,7 @@ public static class OpenLibExtensions
         ArshiftFunction.Instance,
         ArshiftFunction.Instance,
         BandFunction.Instance,
         BandFunction.Instance,
         BorFunction.Instance,
         BorFunction.Instance,
+        BtestFunction.Instance,
         BxorFunction.Instance,
         BxorFunction.Instance,
     ];
     ];