Re: How do I trace objects on the heap with JS 59

Boris Zbarsky <[email protected]> Mon, 27 Aug 2018 13:13:34 -0400
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
On 8/16/18 10:33 AM, Miles wrote:
> Please can I clarify how tracing interacts with garbage collection?

Tracing is how you tell the GC about edges in the object (well, gcthing) 
graph.

> In my embedding this structure stores the pointer back to the JSObject.

That might be fine, depending.  If you don't need the structure to keep 
the JSObject alive, then you can probably just store a raw pointer here 
and update it when the class objectMovedOp is called.  See documentation 
at 
https://searchfox.org/mozilla-central/rev/e126996d9b0a3d7653de205898517f4f5b632e7f/js/public/Class.h#718-733

> and on my JSClass I now define a traceOp which looks something like

This seems like it would work ok too, to me.

> What does calling JS::TraceEdge do? Does it
> 1. Ensure that jsobject is kept up to date to refer to the correct (JSObject *) pointer

Yes.

> or
> 2. Root the object to stop GC on it.

Yes, if it gets called.

In your case, it'll only get called if the object is getting traced 
anyway (since you're doing it in the class trace hook), so the object is 
known to be alive and only your #1 is really happening.

> Basically, how can I do my two 'normal' and 'special' use cases using tracing? Can I somehow store the JSObject pointer in a structure but still allow it to go out of scope as normal for my 'normal' case and also prevent GC in some circumstances for my 'special' case.

For your 'special' case, rooting things as you used to seems correct.

-Boris