Browse Source

Fix: lshift/rshift

AnnulusGames 1 year ago
parent
commit
1f3c68c373

+ 6 - 2
src/Lua/Standard/Bitwise/LShiftFunction.cs

@@ -13,10 +13,14 @@ public sealed class LShiftFunction : LuaFunction
         LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, x);
         LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 2, disp);
 
-        var v = Bit32Helper.ToInt32(x);
+        var v = Bit32Helper.ToUInt32(x);
         var a = (int)disp;
 
-        if (a < 0)
+        if (Math.Abs(a) >= 32)
+        {
+            v = 0;
+        }
+        else if (a < 0)
         {
             v >>= -a;
         }

+ 6 - 2
src/Lua/Standard/Bitwise/RShiftFunction.cs

@@ -13,10 +13,14 @@ public sealed class RShiftFunction : LuaFunction
         LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, x);
         LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 2, disp);
 
-        var v = Bit32Helper.ToInt32(x);
+        var v = Bit32Helper.ToUInt32(x);
         var a = (int)disp;
 
-        if (a < 0)
+        if (Math.Abs(a) >= 32)
+        {
+            v = 0;
+        }
+        else if (a < 0)
         {
             v <<= -a;
         }