3

I am using IronJs's latest version (0.2.0.1) and my js scripts do not properly retrieve the length of an array that has been set to the js engine using an IronJs.Runtime.ArrayObject. However, my variable is well recognized as an array, as shown in C# code below.

var jsCode = @"myArray.length;";
var javascriptEngine = new IronJS.Hosting.CSharp.Context();
var array = new ArrayObject(javascriptEngine.Environment, 2);//array of size 2
array.Put(0, 12.0);//mock values
array.Put(1, 45.1);

javascriptEngine.SetGlobal<ArrayObject>("myArray", array);

var result = javascriptEngine.Execute(jsCode);
Console.WriteLine(result);

var jsCode2 = @"myArray instanceof Array;";
var result2 = javascriptEngine.Execute<bool>(jsCode2);
Console.WriteLine(result2);

We get the following output

undefined
True
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Benoit Patra
  • 4,355
  • 5
  • 30
  • 53

1 Answers1

4

This is a bug in IronJS Runtime. You should open an issue in the appropriate GitHub repository : https://github.com/fholm/IronJS/

A workaround is to force a reallocation of the whole array. In that case, the .length property seems to be correctly set. A hackish way to accomplish that is to create a smaller than needed ArrayObject (e.g. a 0-sized ArrayObject), then put some values in it. The following test passes :

    [TestMethod]
    public void TestWithZeroSizedArray()
    {
        string jsCode = @"myArray.length;";
        var javascriptEngine = new IronJS.Hosting.CSharp.Context();
        var array = new ArrayObject(javascriptEngine.Environment, 0); // Creates a 0-sized Array
        array.Put(0, 12.0);
        array.Put(1, 45.1);

        javascriptEngine.SetGlobal<ArrayObject>("myArray", array);

        var result = javascriptEngine.Execute(jsCode);
        Assert.AreEqual(2, result);
    }

Keep in mind that the multiple copy/reallocations of the underlying .NET arrays will lead to performance issues.

Gabriel Boya
  • 761
  • 8
  • 17
  • 2
    Thanks for this work around. Let me just mention that it does not seem to work if you use your ArrayObject as a dictionary, i.e., using an other overload of Put method e.g.: `Put(string,double)` – Benoit Patra Sep 06 '12 at 12:43
  • 2
    This issue has been fixed on Iron JS repository on [Github](https://github.com/fholm/IronJS/pull/96). – Gabriel Boya May 21 '13 at 16:59