Subj : Re: GetProperty To : netscape.public.mozilla.jseng From : Brendan Eich Date : Tue Feb 10 2004 05:45 pm Brendan Eich wrote: >> JSBool ScrSFVec3f::JSGetProperty(JSContext* iContext, JSObject* iObject, >> jsval id, jsval *vp) >> { >> if (JSVAL_IS_INT (id)) >> { >> SoSFVec3f* lVector = (SoSFVec3f*) JS_GetPrivate(iContext, >> iObject); >> >> static double lValue; >> lValue = lVector->getValue()[JSVAL_TO_INT(id)]; >> *vp = DOUBLE_TO_JSVAL(&lValue); >> } >> return JS_TRUE; >> } >> >> >> Now, my question is if the Javascript engine makes a copy of the >> double, or does it come back to the memory location of lValue >> whenever it needs to read the value of the double. This is important >> for the following javascript > > > It's important for the above example too -- you're returning the > address of a stack temporary that goes out of scope as soon as > ScrSFVec3f::JSGetProperty returns! > > Be careful with pointers (addresses) in C ;-). And I should be careful with my skimming of your code! Sorry, I missed the "static" storage class on double lValue (need my eyes examined). But my final point stands. You can't take any old jsdouble address and tag it as a jsval and store it in a property or other place known to the GC. Only GC-allocated jsdoubles can be so tagged. Hope this is all clear now. /be .