Subj : Re: Array Object index question To : Izman From : Brendan Eich Date : Wed Oct 22 2003 12:00 pm Izman wrote: >Ok... given the following code and the assumptions that cx, globalObj >and parentObj were defined in earlier code: > >JSObject* myArray = JS_NewArrayObject(cx, 2, NULL); >JS_AddRoot(cx, myArray); > > Bug: you must pass &myArray to JS_AddRoot. >JSObject* myObj1; >JSObject* myObj2; >rval myval1; >rval myval2; > >myObj1 = JSMyObject::JSInit(cx, globalObj, NULL); >myval1 = OBJECT_TO_JSVAL(myObj1); > >myObj2 = JSMyObject::JSInit(cx, globalObj, NULL); >myval2 = OBJECT_TO_JSVAL(myObj2); > >JS_SetElement(cx, myArray, 0, &myval1); >JS_SetElement(cx, myArray, 1, &myval2); > >JS_DefineProperty(cx, parentObj, "myarray", OBJECT_TO_JSVAL(myArray), >NULL, NULL, JSPROP_ENUMERATE); > >What code allows me to determine which array value was used? Does the >getProperty routine for parentObj indicate which index was used? If >not how do I read which myarray index was accessed? > > You created an Array. Its class has getProperty and setProperty hooks, and per-property getters and setters for special cases such as 'length', that see the gets and sets. You thereby gave up that opportunity, and parentObj has nothing to do with myArray, so why do you think parentObj's class getProperty, or any per-property getter, will be called for accesses to myArray? >Reason for the question: I want each of the array objects to be >"bound" to a member of an array in the original program. > What original program? You need to tell the whole story here. > The index of >the myarray object directly correlates with the index of the program's >array. This would eliminate the need for a variable in each object >indicating the index it is associated with and also eliminate code >associated with setting and reading that variable. > You shouldn't use JS_NewArrayObject, if you need a custom array-like class that has getPropery and other hooks that know about a native data structure in "the original program". /be .