Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Lua/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@ public static void BadArgument(Traceback traceback, int argumentId, string funct
throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({expected} expected, got {actual})");
}

public static void BadArgumentNumberIsNotInteger(Traceback traceback, int argumentId, string functionName)
{
throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' (number has no integer representation)");
}

public static void ThrowBadArgumentIfNumberIsNotInteger(LuaState state, string functionName, int argumentId, double value)
{
if (!MathEx.IsInteger(value))
{
throw new LuaRuntimeException(state.GetTraceback(), $"bad argument #{argumentId} to '{functionName}' (number has no integer representation)");
BadArgumentNumberIsNotInteger(state.GetTraceback(), argumentId, functionName);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/Lua/LuaFunctionExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ public T GetArgument<T>(int index)
var arg = Arguments[index];
if (!arg.TryRead<T>(out var argValue))
{
if (LuaValue.TryGetLuaValueType(typeof(T), out var type))
var t = typeof(T);
if ((t == typeof(int) || t == typeof(long)) && arg.TryReadNumber(out _))
{
LuaRuntimeException.BadArgumentNumberIsNotInteger(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name);
}
else if (LuaValue.TryGetLuaValueType(t, out var type))
{
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, type.ToString(), arg.Type.ToString());
}
else
{
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, typeof(T).Name, arg.Type.ToString());
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, t.Name, arg.Type.ToString());
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Lua/LuaValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public bool TryRead<T>(out T result)
}
else if (t == typeof(int))
{
if (!MathEx.IsInteger(value)) break;
var v = (int)value;
result = Unsafe.As<int, T>(ref v);
return true;
}
else if (t == typeof(long))
{
if (!MathEx.IsInteger(value)) break;
var v = (long)value;
result = Unsafe.As<long, T>(ref v);
return true;
Expand Down Expand Up @@ -144,8 +146,8 @@ public bool TryRead<T>(out T result)
result = tValue;
return true;
}
result = default!;
return false;

break;
}
else if (t == typeof(object))
{
Expand Down
9 changes: 1 addition & 8 deletions src/Lua/Standard/BasicLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,8 @@ public ValueTask<int> Select(LuaFunctionExecutionContext context, Memory<LuaValu
{
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)
{
throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #1 to 'select' (index out of range)");
Expand Down
16 changes: 3 additions & 13 deletions src/Lua/Standard/FileHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,14 @@ public void Close()
? context.GetArgument<string>(1)
: "cur";
var offset = context.HasArgument(2)
? context.GetArgument<double>(2)
? context.GetArgument<int>(2)
: 0;

if (whence is not ("set" or "cur" or "end"))
{
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
{
buffer.Span[0] = file.Seek(whence, (long)offset);
Expand All @@ -233,15 +228,10 @@ public void Close()
var file = context.GetArgument<FileHandle>(0);
var mode = context.GetArgument<string>(1);
var size = context.HasArgument(2)
? context.GetArgument<double>(2)
? context.GetArgument<int>(2)
: -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;
return new(1);
Expand Down
8 changes: 1 addition & 7 deletions src/Lua/Standard/Internal/IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,8 @@ public static int Read(LuaState state, FileHandle file, string name, int startAr
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);

for (int j = 0; j < count; j++)
Expand Down
9 changes: 2 additions & 7 deletions src/Lua/Standard/OperatingSystemLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,9 @@ public ValueTask<int> Exit(LuaFunctionExecutionContext context, Memory<LuaValue>
{
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
{
Expand Down