fholm / IronJS

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

Working with C# objects and classes #106

Closed jchristn closed 5 years ago

jchristn commented 9 years ago

Hello, first thank you for this great contribution. I'm really excited for what your software can/will enable. I'm having some issues with using IronJS and classes defined in C#. For instance, the following works:

        var context = new CSharp.Context();
        string program = "var foo = function(a, b) { return 'Hello, ' + a + ' ' + b; }; foo();";
        context.Execute(program);
        var foo = context.Globals.GetT<FunctionObject>("foo");
        var result = foo.Call(context.Globals, "First", "Last").Unbox<string>();

But, the following fails:

        string program = "var foo = function(a) { return a.length; }; foo();";
        List<int> int_list = new List<int>();
        int_list.Add(1);
        int_list.Add(1);
        int_list.Add(2);
        int_list.Add(3);
        context.Execute(program);
        var foo = context.Globals.GetT<FunctionObject>("foo");
        var result = foo.Call(context.Globals, int_list).Unbox<int>();

The error is:

IronJS.UserError was unhandled by user code Message: An exception of type 'IronJS.UserError' occurred in IronJS.dll but was not handled in user code Additional information: TypeError: Can't convert Undefined, Null or CLR to Object

A 'Source Not Found' page/tab is then displayed in Visual Studio with the following: Runtime.fs not found Locating source for 'C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs'. (No checksum.) The file 'C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs' does not exist. Looking in script documents for 'C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs'... Looking in the projects for 'C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs'. The file was not found in a project. Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\vccorlib\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\mfc\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\atl\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include'... The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs. The debugger could not locate the source file 'C:\Users\otac0n\Projects\IronJS\Src\IronJS\Runtime.fs'.

jchristn commented 9 years ago

I also tried converting to an array, same issue. var result = foo.Call(context.Globals, int_list.ToArray()).Unbox();

jchristn commented 9 years ago

Any help?