Search code examples
javascriptc++spidermonkeyjavascript-engine

Spider monkey : Why JS array is not inheriting default properties like length, splice etc


I am actually new to both spider monkey api and this mailing list. Actually I was trying to create a Array like objectA.arrayA and the call back code goes like this.

    char *value[] = {"abc", "xyz", "efg"};
    int count = 0;
    JSObject* val = JS_NewArrayObject(pContext, 0, NULL);

    while(count < 3) {
        jstr = JS_NewStringCopyZ(pContext, value[count]);

        JS_DefineElement(pContext, val, count++, STRING_TO_JSVAL(jstr),
                        NULL, NULL, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
    }

    vJs->DefineProperty(pObject, "arrayA", OBJECT_TO_JSVAL(val));

I am getting the proper value for the objectA.arrayA but when I do objectA.arrayA.length, it says arrayA does not have ay property. Can you tell what i am doing wrong. I am facing the same even when I am creating a sting.


Solution

  • Your first apparent problem is:

    JS_NewArrayObject(pContext, 0, NULL);

    Where you have ZERO should be the desired length of your array.

    It is pretty apparent to me that you don't know how to use the API. I believe the documentation relavent to your question can be found at:

    https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_NewArrayObject

    https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_DefineProperty

    https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_DefineElement

    and: https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JSClass.addProperty

    https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_PropertyStub

    These five pages have all the info you should need to crack the code.