returning Jint.Native.JsInstance objects from c#

Dec 6, 2012 at 6:37 PM
Edited Dec 6, 2012 at 6:39 PM

Some background:

I compile a given script and then run it multiple times. When I initially build the JintEngine for this script, I make a c# object accessible through SetParameter("$MYOBJECT", MyObject) and I also make the IGlobal object accessible with SetParameter("$IGLOBAL", engine.Global).

Inside my script, I want to call a method of MyObject and have a native type returned. For instance, var somearray = MyObject.MakeArray($IGLOBAL). The implementation of MakeArray follows:


public JsInstance MakeArray(IGlobal global)
    JsArray jintarr = new JsArray(new Jint.Native.JsArrayConstructor(global).PrototypeProperty);
    for (int i = 0; i < 5; i++)
        jintarr.put(i, new Jint.Native.JsNumberConstructor(global).New((double)i);

    return jintarr;

I do this because I don't want the overhead of writing to string and calling eval often, though eval works, of course. When I use MakeArray, though, and try to call something like somearray.length I get "undefined". My question, then, is how to go about returning native Jint types to a script. I am sure that MakeArray works internally (i.e. if I use the JsArray methods to iterate over the JsArray and print them to console, they give the correct values).

Dec 20, 2012 at 9:20 PM

var jintarr = global.ArrayClass.New(); solved the problem.