CLR Array types to Jint

May 1, 2011 at 8:45 PM

Does anybody know how to marshall the CLR array type back and forth between C# and Jint?

I have a native C# object that has a method that returns an array:

public class MyObject
   object[] GetABunchOfValues() { ... }

I then attempt to run it from JInt:

engine.Run("function sum(values) { if (values==null) { return 0; } ...; }");

var results1=engine.Run("return instanceOfMyObject.GetABunchOfValues();");
var results2=engine.Run("return sum(instanceOfMyObject.GetABunchOfValues());");

Though results1 returns the expected array of object values, result2 always comes out to 0... I have confirmat that inside the javascript function, the "values" argument equals null.

So the question is, how do I get CLR arrays into my Jint programs??


May 3, 2011 at 3:22 PM

There was a bug in comparison of clr objects to null in jint. What version do you use? if you are using Jint from sources please specify a revision number.

May 3, 2011 at 11:53 PM

Hi Cin,

This is from the 0.9.0 source.

May 4, 2011 at 4:24 PM

Try the latest sources from the repository, this problem should be fixed.

May 5, 2011 at 7:47 PM
Edited May 5, 2011 at 8:06 PM

Hi Cin,

Yes, 0.9.1 fixed the null check problem.  Now what I'm seeing is that values.constructor.toString() is giving me "System.Object[]" instead of Array, and values.length returns null, yet values.Length returns the correct number of items.

How do I convert a native array into a javascript array?

May 6, 2011 at 6:12 AM

They are absolutely different, you can convert them only by making a copy.

For example a for-in statement on JS array will iterate over indexes, but on CLR array it will iterate over values. A JS array will automatically grow if you set an element beside the current length, a CLR array will throw an exception, and you can't resize it.