Spriggs, Jim | 23 Jun 15:09
Picon
Favicon

Help needed in grokking property getter idval arg (for native array wrapping).

Hi Guys,

Goal
----
In my embedding of Spidermonkey 1.7, I'm trying to wrap a simple native array 
which is (ultimately) declared similarly to this:

	double nativeArray[256];

What I've coded so far:
-----------------------

// the property getter
JSBool GetArrayElement(JSContext* cx, JSObject* obj, jsval idval, jsval* rval) 
{
	int32 index = 0;
	JS_ValueToInt32( cx, idval, &index );

	return rvalFromDouble(cx, GetNativeArrayElement(index), rval);
}

main(...)
{

	// initialisation blah - Runtime, Context, etc

    JSObject* arrObj = JS_NewArrayObject(cx, 0, NULL);

	// define arr property with explicit getter
    JS_DefineProperty(cx, global, "arr", OBJECT_TO_JSVAL(arrObj), GetArrayElement, NULL,
JSPROP_SHARED | JSPROP_READONLY);

	// etc 
	// then drop into read/execute/print loop

}

Test Script:
------------

	var e = arr[7];

Expected result:
----------------

On entering GetArrayElement(...):
	
	JSVAL_IS_INT(idval) == true
and
	JSVAL_TO_INT(idval) == 7

In other words, id would actually represent the index of the required element ( 7, in this case), even if it
doesn't yet exist.

Actual result:
--------------

On entering GetArrayElement(...):

	JSVAL_IS_STRING(idval) == true
and
    	JS_GetStringBytes(JSVAL_TO_STRING(idval)) == "arr"

i.e., apparently unrelated to the index given in the script.

Source of information:
----------------------
JSAPI Reference: JSPropertyOp:

"idval 	jsval 	The name or index of the property being accessed. This is either a string (Unicode property
identifier) or an integer (element index)."

What am I missing?

Thanks!
--

-- 
Jim Spriggs
Senior Software Engineer
Ext./Durchwahl 25

Gmane