I am embedding Spidermonkey in my C++ application. I need to implementing some custom Javascript functions in native C++ that pass around a jsval. I need to guard the jsval against accidental garbage collection. Is it proper for me to do this:
(1) In an init routine:
static jsval vp; // a STATIC variable, value unknown
JSBool init((JSContext *cx, uintN argc, jsval *vp) {
JS_AddValueRoot(cx, &vp);
}
(2) In one c++ function implementing the Javascript function setter():
JSBool setter(JSContext *cx, uintN argc, jsval *vp) {
...
vp=...;// set to some JSObject and hopefully makes any previous JSObject available for gc
}
(3) In a second C++ function call within the same compilation unit implementing the Javascript function getter():
JSBool getter(JSContext *cx, uintN argc, jsval *vp) {
jsval somethingelse = vp; //directly retrieve the static value stored by setter()
....
}
My Javascript script uses the function calls like this:
init();
setter(...);
some_other_function_call_that_causes_gc();
getter();
setter(...);
some_other_function_call_that_causes_gc();
getter();
....
some_other_function_call_that_causes_gc();
setter(...);
some_other_function_call_that_causes_gc();
getter();
Note that I never call JS_RemoveRoot() since the static jsval vp is a permanent storage for my jsval passed between 2 function calls. And, I keep setting new values into the gc rooted static variable vp in my setter(), assuming any previouis JSObject stored in jsval will be available for garbage collection.
Are these proper ways to create gc rooted temporary variables that can be passed across function call? In particular, is the way my setter() replacing a previous JSObject a proper way to make an existing JSObject available for gc (i.e. no memory-leak/crash).
EDIT: The reason why I believe Garbage collection is an issue is that:
https://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide
Under Section JSAPI Concepts, Javascript Values:
a jsval by itself does not protect its referent from the garbage collector
https://developer.mozilla.org/en/SpiderMonkey_Garbage_Collection_Tips
Example 3 says "Root as you go" and shows how a jsval must be assigned to a root.