Browse Source

Add: integer check

AnnulusGames 1 year ago
parent
commit
4e52a39a8a

+ 2 - 4
src/Lua/Standard/Bitwise/ArshiftFunction.cs

@@ -10,10 +10,8 @@ public sealed class ArshiftFunction : LuaFunction
         var x = context.GetArgument<double>(0);
         var disp = context.GetArgument<double>(1);
 
-        if (!MathEx.IsInteger(disp))
-        {
-            throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #2 to 'arshift' (number has no integer representation)");
-        }
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, x);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 2, disp);
 
         var v = Bit32Helper.ToInt32(x);
         var a = (int)disp;

+ 8 - 2
src/Lua/Standard/Bitwise/BandFunction.cs

@@ -13,11 +13,17 @@ public sealed class BandFunction : LuaFunction
             return new(1);
         }
 
-        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+        var arg0 = context.GetArgument<double>(0);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
+
+        var value = Bit32Helper.ToUInt32(arg0);
 
         for (int i = 1; i < context.ArgumentCount; i++)
         {
-            var v = Bit32Helper.ToUInt32(context.GetArgument<double>(i));
+            var arg = context.GetArgument<double>(i);
+            LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1 + i, arg);
+
+            var v = Bit32Helper.ToUInt32(arg);
             value &= v;
         }
 

+ 4 - 1
src/Lua/Standard/Bitwise/BnotFunction.cs

@@ -7,7 +7,10 @@ public sealed class BnotFunction : LuaFunction
 
     protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
     {
-        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+        var arg0 = context.GetArgument<double>(0);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
+
+        var value = Bit32Helper.ToUInt32(arg0);
         buffer.Span[0] = ~value;
         return new(1);
     }

+ 8 - 2
src/Lua/Standard/Bitwise/BorFunction.cs

@@ -13,11 +13,17 @@ public sealed class BorFunction : LuaFunction
             return new(1);
         }
 
-        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+        var arg0 = context.GetArgument<double>(0);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
+
+        var value = Bit32Helper.ToUInt32(arg0);
 
         for (int i = 1; i < context.ArgumentCount; i++)
         {
-            var v = Bit32Helper.ToUInt32(context.GetArgument<double>(i));
+            var arg = context.GetArgument<double>(i);
+            LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1 + i, arg);
+
+            var v = Bit32Helper.ToUInt32(arg);
             value |= v;
         }
 

+ 8 - 2
src/Lua/Standard/Bitwise/BtestFunction.cs

@@ -13,11 +13,17 @@ public sealed class BtestFunction : LuaFunction
             return new(1);
         }
 
-        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+        var arg0 = context.GetArgument<double>(0);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
+
+        var value = Bit32Helper.ToUInt32(arg0);
 
         for (int i = 1; i < context.ArgumentCount; i++)
         {
-            var v = Bit32Helper.ToUInt32(context.GetArgument<double>(i));
+            var arg = context.GetArgument<double>(i);
+            LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1 + i, arg);
+
+            var v = Bit32Helper.ToUInt32(arg);
             value &= v;
         }
 

+ 8 - 2
src/Lua/Standard/Bitwise/BxorFunction.cs

@@ -13,11 +13,17 @@ public sealed class BxorFunction : LuaFunction
             return new(1);
         }
 
-        var value = Bit32Helper.ToUInt32(context.GetArgument<double>(0));
+        var arg0 = context.GetArgument<double>(0);
+        LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, arg0);
+
+        var value = Bit32Helper.ToUInt32(arg0);
 
         for (int i = 1; i < context.ArgumentCount; i++)
         {
-            var v = Bit32Helper.ToUInt32(context.GetArgument<double>(i));
+            var arg = context.GetArgument<double>(i);
+            LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1 + i, arg);
+
+            var v = Bit32Helper.ToUInt32(arg);
             value ^= v;
         }