Skip to content

Commit

Permalink
Fix #238
Browse files Browse the repository at this point in the history
  • Loading branch information
ixjf committed Feb 24, 2019
1 parent 140e136 commit 51ea3b7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
20 changes: 20 additions & 0 deletions src/MoonSharp.Interpreter.Tests/EndToEnd/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,5 +1532,25 @@ function myFunc()
//#endif
// }

[Test]
public void NumericConversionFailsIfOutOfBounds()
{
Script S = new Script();

S.Globals["my_function_takes_byte"] = (Action<byte>)(p => { });

try
{
S.DoString("my_function_takes_byte(2010191) -- a huge number that is definitely not a byte");

Assert.Fail(); // ScriptRuntimeException should have been thrown, if it doesn't Assert.Fail should execute
}
catch (ScriptRuntimeException e)
{
//Assert.Pass(e.DecoratedMessage);
}
}


}
}
58 changes: 35 additions & 23 deletions src/MoonSharp.Interpreter/Interop/Converters/NumericConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ internal static object DoubleToType(Type type, double d)
{
type = Nullable.GetUnderlyingType(type) ?? type;

if (type == typeof(double)) return d;
if (type == typeof(sbyte)) return (sbyte)d;
if (type == typeof(byte)) return (byte)d;
if (type == typeof(short)) return (short)d;
if (type == typeof(ushort)) return (ushort)d;
if (type == typeof(int)) return (int)d;
if (type == typeof(uint)) return (uint)d;
if (type == typeof(long)) return (long)d;
if (type == typeof(ulong)) return (ulong)d;
if (type == typeof(float)) return (float)d;
if (type == typeof(decimal)) return (decimal)d;
try
{
if (type == typeof(double)) return d;
if (type == typeof(sbyte)) return Convert.ToSByte(d);
if (type == typeof(byte)) return Convert.ToByte(d);
if (type == typeof(short)) return Convert.ToInt16(d);
if (type == typeof(ushort)) return Convert.ToUInt16(d);
if (type == typeof(int)) return Convert.ToInt32(d);
if (type == typeof(uint)) return Convert.ToUInt32(d);
if (type == typeof(long)) return Convert.ToInt64(d);
if (type == typeof(ulong)) return Convert.ToUInt64(d);
if (type == typeof(float)) return Convert.ToSingle(d);
if (type == typeof(decimal)) return Convert.ToDecimal(d);
}
catch (Exception)
{

}

return d;
}

Expand All @@ -62,18 +70,22 @@ internal static object DoubleToType(Type type, double d)
/// </summary>
internal static double TypeToDouble(Type type, object d)
{
if (type == typeof(double)) return (double)d;
if (type == typeof(sbyte)) return (double)(sbyte)d;
if (type == typeof(byte)) return (double)(byte)d;
if (type == typeof(short)) return (double)(short)d;
if (type == typeof(ushort)) return (double)(ushort)d;
if (type == typeof(int)) return (double)(int)d;
if (type == typeof(uint)) return (double)(uint)d;
if (type == typeof(long)) return (double)(long)d;
if (type == typeof(ulong)) return (double)(ulong)d;
if (type == typeof(float)) return (double)(float)d;
if (type == typeof(decimal)) return (double)(decimal)d;
return (double)d;
if (type != typeof(double) &&
type != typeof(sbyte) &&
type != typeof(byte) &&
type != typeof(short) &&
type != typeof(ushort) &&
type != typeof(int) &&
type != typeof(uint) &&
type != typeof(long) &&
type != typeof(ulong) &&
type != typeof(float) &&
type != typeof(decimal))
{
return (double)d;
}

return Convert.ToDouble(d);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ internal static object DynValueToObjectOfType(DynValue value, Type desiredType,
Type underType = Enum.GetUnderlyingType(desiredType);
return NumericConversions.DoubleToType(underType, value.Number);
}
if (NumericConversions.NumericTypes.Contains(desiredType))
return NumericConversions.DoubleToType(desiredType, value.Number);
if (NumericConversions.NumericTypes.Contains(desiredType))
{
object d = NumericConversions.DoubleToType(desiredType, value.Number);
if (d.GetType() == desiredType)
return d;
break;
}
if (stringSubType != StringConversions.StringSubtype.None)
str = value.Number.ToString();
break;
Expand Down

0 comments on commit 51ea3b7

Please sign in to comment.