Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can a script unroot itself from GC while executing?

6 views
Skip to first unread message

Al

unread,
Jun 9, 2008, 3:05:00 AM6/9/08
to
Hi there,

Is it safe for a JSScript* to remove itself (or rather, the JSObject*
that is wrapping it) as a root from its JSContext, while it is
executing? (Assuming the script will no longer be used after it completes).

E.g.:

a: JS_ExecuteScript(... foo ...)
b: (call to native function)
c: JS_RemoveRoot(... foo ...)

This is useful in case you want to expose a Quit function to JS, for
instance, or something similar. After reading some source comments, I am
vaguely under the impression that JS_ExecuteScript temporarily protects
the script from GC, so it should be possible in theory. Is that the case?

Thanks a ton!
Al.

Jason Orendorff

unread,
Jun 9, 2008, 11:01:46 AM6/9/08
to
On Jun 9, 2:05 am, Al <t...@haik.us> wrote:
>         Hi there,
>
> Is it safe for a JSScript* to remove itself (or rather, the JSObject*
> that is wrapping it) as a root from its JSContext, while it is
> executing? (Assuming the script will no longer be used after it completes).

Yes, this is safe.

The garbage collector won't collect a script that's executing.

-j

Jason Orendorff

unread,
Jun 9, 2008, 11:54:49 AM6/9/08
to
On Jun 9, 10:01 am, Jason Orendorff <jason.orendo...@gmail.com> wrote:
> On Jun 9, 2:05 am, Al <t...@haik.us> wrote:
> > Is it safe for a JSScript* to remove itself (or rather, the JSObject*
> > that is wrapping it) as a root from its JSContext, while it is
> > executing? (Assuming the script will no longer be used after it completes).
>
> Yes, this is--

Wait--I think this is unsafe after all! Sorry for the wrong answer.

It seems like it would be a two-line change to SpiderMonkey to make
this safe, though (in js_TraceScript, also trace script->object if
it's non-null).

-j

Al

unread,
Jun 9, 2008, 2:48:35 PM6/9/08
to
Jason Orendorff wrote:
> On Jun 9, 10:01 am, Jason Orendorff <jason.orendo...@gmail.com> wrote:
>> On Jun 9, 2:05 am, Al <t...@haik.us> wrote:
>>> Is it safe for a JSScript* to remove itself (or rather, the JSObject*
>>> that is wrapping it) as a root from its JSContext, while it is
>>> executing? (Assuming the script will no longer be used after it completes).
>> Yes, this is--
>
> Wait--I think this is unsafe after all! Sorry for the wrong answer.

No worries :).

> It seems like it would be a two-line change to SpiderMonkey to make
> this safe, though (in js_TraceScript, also trace script->object if
> it's non-null).
>
> -j

Ah, I see. Any chance of these changes being committed? I think it'd be
a good property to have. I would try to do it myself, but I'm not all
that comfortable with the engine source yet, so I'd probably mess
something up :P.

Thanks again,
Al.

0 new messages