moonsharp-devs / moonsharp

An interpreter for the Lua language, written entirely in C# for the .NET, Mono, Xamarin and Unity3D platforms, including handy remote debugger facilities.
http://www.moonsharp.org
Other
1.41k stars 213 forks source link

Numeric conversion doesn't fail if number is out of bounds #238

Closed ixjf closed 5 years ago

ixjf commented 5 years ago

Say a Lua binding in C# takes a parameter of type byte:

void MyFunction(byte i)
{
}

Since numbers in Lua are all doubles, MoonSharp attempts a conversion from double to byte. It does this by simply type casting one type to another. This doesn't work because if the value passed in Lua is out of bounds for a byte, say:

MyFunction(300) -- Lua call to MyFunction with value '300', which is out of range for a byte (0-255)

Then it will overflow and the value you get in C# will lead to unexpected behaviour. Instead, numeric conversion should fail in the case the value is out of bounds.

ixjf commented 5 years ago

@xanathar How dead is MoonSharp? I have this and one or two other changes that I'd like reviewed and possibly merged into MoonSharp.

I have 1) a fix for this, as you can see in the reference above, 2) a fix for #237 (Lua 'error' function not implementing the second parameter, 'level') - which also fixes a problem with the call stack not getting the correct source ref and hence, e.g. debug.traceback giving no position information -, and 3) an additional, bigger change to the async extensions, which is to replace the current method of using coroutines to be able to abort a running script. They are all in my fork of MoonSharp and you can see the details in the closed issues page.