Browse Source

Change: use GetArgument<int>() more aggressively

AnnulusGames 11 months ago
parent
commit
a828a9d309

+ 1 - 8
src/Lua/Standard/BasicLibrary.cs

@@ -365,15 +365,8 @@ public sealed class BasicLibrary
     {
     {
         var arg0 = context.GetArgument(0);
         var arg0 = context.GetArgument(0);
 
 
-        if (arg0.TryRead<double>(out var d))
+        if (arg0.TryRead<int>(out var index))
         {
         {
-            if (!MathEx.IsInteger(d))
-            {
-                throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #1 to 'select' (number has no integer representation)");
-            }
-
-            var index = (int)d;
-
             if (Math.Abs(index) > context.ArgumentCount)
             if (Math.Abs(index) > context.ArgumentCount)
             {
             {
                 throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #1 to 'select' (index out of range)");
                 throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #1 to 'select' (index out of range)");

+ 3 - 13
src/Lua/Standard/FileHandle.cs

@@ -201,7 +201,7 @@ public class FileHandle : ILuaUserData
             ? context.GetArgument<string>(1)
             ? context.GetArgument<string>(1)
             : "cur";
             : "cur";
         var offset = context.HasArgument(2)
         var offset = context.HasArgument(2)
-            ? context.GetArgument<double>(2)
+            ? context.GetArgument<int>(2)
             : 0;
             : 0;
 
 
         if (whence is not ("set" or "cur" or "end"))
         if (whence is not ("set" or "cur" or "end"))
@@ -209,11 +209,6 @@ public class FileHandle : ILuaUserData
             throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #2 to 'seek' (invalid option '{whence}')");
             throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #2 to 'seek' (invalid option '{whence}')");
         }
         }
 
 
-        if (!MathEx.IsInteger(offset))
-        {
-            throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #3 to 'seek' (number has no integer representation)");
-        }
-
         try
         try
         {
         {
             buffer.Span[0] = file.Seek(whence, (long)offset);
             buffer.Span[0] = file.Seek(whence, (long)offset);
@@ -233,15 +228,10 @@ public class FileHandle : ILuaUserData
         var file = context.GetArgument<FileHandle>(0);
         var file = context.GetArgument<FileHandle>(0);
         var mode = context.GetArgument<string>(1);
         var mode = context.GetArgument<string>(1);
         var size = context.HasArgument(2)
         var size = context.HasArgument(2)
-            ? context.GetArgument<double>(2)
+            ? context.GetArgument<int>(2)
             : -1;
             : -1;
 
 
-        if (!MathEx.IsInteger(size))
-        {
-            throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #3 to 'setvbuf' (number has no integer representation)");
-        }
-
-        file.SetVBuf(mode, (int)size);
+        file.SetVBuf(mode, size);
 
 
         buffer.Span[0] = true;
         buffer.Span[0] = true;
         return new(1);
         return new(1);

+ 1 - 7
src/Lua/Standard/Internal/IOHelper.cs

@@ -117,14 +117,8 @@ internal static class IOHelper
                             break;
                             break;
                     }
                     }
                 }
                 }
-                else if (format.TryRead<double>(out var d))
+                else if (format.TryRead<int>(out var count))
                 {
                 {
-                    if (!MathEx.IsInteger(d))
-                    {
-                        throw new LuaRuntimeException(state.GetTraceback(), $"bad argument #{i + startArgumentIndex} to 'read' (number has no integer representation)");
-                    }
-
-                    var count = (int)d;
                     using var byteBuffer = new PooledArray<byte>(count);
                     using var byteBuffer = new PooledArray<byte>(count);
 
 
                     for (int j = 0; j < count; j++)
                     for (int j = 0; j < count; j++)

+ 2 - 7
src/Lua/Standard/OperatingSystemLibrary.cs

@@ -119,14 +119,9 @@ public sealed class OperatingSystemLibrary
             {
             {
                 Environment.Exit(b ? 0 : 1);
                 Environment.Exit(b ? 0 : 1);
             }
             }
-            else if (code.TryRead<double>(out var d))
+            else if (code.TryRead<int>(out var d))
             {
             {
-                if (!MathEx.IsInteger(d))
-                {
-                    throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #1 to 'exit' (number has no integer representation)");
-                }
-
-                Environment.Exit((int)d);
+                Environment.Exit(d);
             }
             }
             else
             else
             {
             {