Hi all, question about garbage collection and JSString objects:
I am using SpiderMonkey to expose some C structure members (mostly
char* strings) to a JS scripting engine. As the data values of these C
structures may change often, I have their equivalent properties in my
JSClass definitions flagged as JSPROP_SHARED, which according to
documentation tells SpiderMonkey not to use a stored value slot for
the property, and call my accessor each time to get it.
My question relates to my accessor's behavior. I set the return jsval
to something like:
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(context, cstruct->charptr));
Will this work? It would be completely impractical (and a waste of
space) to keep rooted JSString copies of all C strings in my program,
so the behavior I want is for the JSString to be created by my
accessor when the property is accessed, and garbage collected soon
after once the code has done what it needs with it, assuming it's not
linked to some rooted object.
The code case I'm concerned about is something like this:
function foo()
{
// Get property as a local-scope variable
var str = bar.property;
// Execute a function call so we branch and garbage collector runs.
baz();
// Attempt to access 'str'.
alert("str is: " + str);
}
Will this indeed work, or is there a chance that my JSString
(referenced by 'str') will be GC'd before it is used?
function foo()
{
var str = "string 1 " + "string 2!";
baz();
alert("str is: " + str);
}
For this to work, the JSString created to hold the result of the
constant string concatenation would have to be rooted in the local
scope (probably as a result of JS_EnterLocalRootScope() being called).
Thus, it would be safe from GC until the function execution ended, at
which point if it wasn't rooted elsewhere, it would be eligible for
GC.
I would think, if during the execution of a function, the JS engine
enters a local root scope, then any objects created (or at least
returned) by my property get operation would be protected in that
local root scope.
I'm wondering if this is indeed true, or do I need to have my property
get operation enter its own local root scope, and
JS_LeaveLocalRootScopeWithResult() with my string, to ensure its
guaranteed survival until it's no longer needed.
Thanks for taking the time to read this and help me out!
--Alex
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine