Long/Int64 .NET Data types

Feb 8, 2014 at 12:24 AM
Edited Feb 8, 2014 at 1:16 AM
I'm having some trouble passing large numbers from .NET to the JScriptEngine. Here is what I'm trying to do:
        JScriptEngine engine = new JScriptEngine();

        engine.AddHostObject( "bigNumber", (long)1400000000000 );
        var result = engine.Evaluate( "'BigNumber: ' + bigNumber" );

        Console.WriteLine( "Result: " + result.ToString() );
I'm getting an error on the AddHostObject call. I also tried replacing the engine.AdHostObject() with engine.Script reference:
        JScriptEngine engine = new JScriptEngine();

        engine.Script.bigNumber = (long)1400000000000;
        var result = engine.Evaluate( "'BigNumber: ' + bigNumber" );

        Console.WriteLine( "Result: " + result.ToString() );
In this case the result is "Result: ". Is it possible to pass long/Int64 data types from .NET to the JScript engine?

Thanks,

Jamin
Coordinator
Feb 8, 2014 at 12:07 PM
Edited Feb 8, 2014 at 12:08 PM
Hi Jamin,

I'm getting an error on the AddHostObject call.

That's correct. That method can't be used with simple values that are automatically converted to native script language datatypes, such as strings and numbers.

I also tried replacing the engine.AddHostObject() with engine.Script reference

That's the correct approach, but you've found an odd case. JavaScript (the language) doesn't support 64-bit integers; its only numeric datatype is 64-bit floating point. On the other hand, JScript (the engine) is implemented as a COM automation component and should therefore be able to handle 64-bit integer arguments.

This is where JScript appears to have a bug; instead of converting 64-bit integers to floating point, it imports them directly and treats them as "unrecognized data". In your example, typeof bigNumber evaluates to "unknown", which isn't even legal JavaScript. Very strange!

For now all we can recommend is that you avoid this situation. Convert 64-bit integers to double before passing them to JScript. We'll investigate this issue and consider making this conversion automatic in a future release.

Thanks for bringing this to our attention!
Feb 10, 2014 at 4:01 PM
Thank you for the workaround. For those that may run into this issue I was able to change the long to a double:
        JScriptEngine engine = new JScriptEngine();

        engine.Script.bigNumber = (double)1400000000000;
        var result = engine.Evaluate( "'BigNumber: ' + bigNumber" );

        Console.WriteLine( "Result: " + result.ToString() );
The number was output correctly.

Thank you!