fholm / IronJS

IronJS - A JavaScript implementation for .NET
http://ironjs.wordpress.com
Apache License 2.0
680 stars 79 forks source link

BoxedValue.Box(object value) is boxing int to int, instead of int to double? #78

Closed fholm closed 13 years ago

fholm commented 13 years ago

If you look at line 176 (https://github.com/fholm/IronJS/blob/master/Src/IronJS.Runtime/BoxedValue.cs#L176) in BoxedValue.cs you will see this code:

if (value is int)
    return Box((int)value);

This will call the Box(object value) overload again which would recurse infinitely, since we don't have an int-overload for Box. Also in my mind this should convert value to a double, and call the Box(double) overload.

I didn't write this specific line so not sure on what the reasoning is here.

ChaosPandion commented 13 years ago

We may be better off taking advantage of Type.GetTypeCode for this and perform appropriate type conversions on the numeric types.

fholm commented 13 years ago

@ChaosPandion: Well, we need to box all numbers do doubles for them to work properly, so there really isn't any choice here since JS requires all numbers to be doubles.

ChaosPandion commented 13 years ago

@fholm - That is what I meant except we do it for more than just Int32.

// check for nulls
switch (Type.GetTypeCode(value.GetType())) 
{
    case TypeCode.Int32:
    case TypeCode.Int16:
    case TypeCode.Single:
        return Box((double)value);
}
fholm commented 13 years ago

aha, clever. I will implement it right away.

fholm commented 13 years ago

Fixed in 5dc5b14afaf9cb4efbdeb98623fe187c242daa4b