Browse Source

Add: bit32.band/bor/bxor

AnnulusGames 1 year ago
parent
commit
9262e16cb4

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

@@ -0,0 +1,27 @@
+namespace Lua.Standard.Bitwise;
+
+public sealed class BandFunction : LuaFunction
+{
+    public override string Name => "band";
+    public static readonly BandFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        if (context.ArgumentCount == 0)
+        {
+            buffer.Span[0] = uint.MaxValue;
+            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;
+        return new(1);
+    }
+}

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

@@ -0,0 +1,27 @@
+namespace Lua.Standard.Bitwise;
+
+public sealed class BorFunction : LuaFunction
+{
+    public override string Name => "bor";
+    public static readonly BorFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        if (context.ArgumentCount == 0)
+        {
+            buffer.Span[0] = uint.MaxValue;
+            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;
+        return new(1);
+    }
+}

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

@@ -0,0 +1,27 @@
+namespace Lua.Standard.Bitwise;
+
+public sealed class BxorFunction : LuaFunction
+{
+    public override string Name => "bxor";
+    public static readonly BxorFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        if (context.ArgumentCount == 0)
+        {
+            buffer.Span[0] = uint.MaxValue;
+            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;
+        return new(1);
+    }
+}

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

@@ -102,6 +102,9 @@ public static class OpenLibExtensions
 
 
     static readonly LuaFunction[] bit32Functions = [
     static readonly LuaFunction[] bit32Functions = [
         ArshiftFunction.Instance,
         ArshiftFunction.Instance,
+        BandFunction.Instance,
+        BorFunction.Instance,
+        BxorFunction.Instance,
     ];
     ];
 
 
     public static void OpenBasicLibrary(this LuaState state)
     public static void OpenBasicLibrary(this LuaState state)