Re: How do I trace objects on the heap with JS 59
James Stortz <[email protected]> Fri, 10 Aug 2018 05:34:49 -0400
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <CAE0WHd9wXMzA4pSQF7cgGKod7nbH_mqtcT=3WYJUxNoQ6TKgZw@mail.gmail.com> |
So, here's an actual example to clarify. Please tell me if this simple
concept is correct.
1.) On my backend objects, I set a pointer to a `new FrontEnd(cx,
rooted_obj_js)`
//----------------------------------------------
void trace_obj(JSTracer* tracer, void* data) {
JS::TraceEdge(tracer, (JS::Heap<JSObject*>*)data, "obj_js");
};
struct FrontEnd {
FrontEnd(JSContext *cx, JS::HandleObject obj) {
this->cx = cx;
this->obj = obj;
if (this->obj.get())
JS_AddExtraGCRootsTracer(this->cx, trace_obj,
&this->obj);
};
~FrontEnd() {
if (this->obj.get())
JS_RemoveExtraGCRootsTracer(this->cx, trace_obj,
&this->obj);
};
JSContext *cx;
JS::Heap<JSObject*> obj;
};
//----------------------------------------------
2.) back in the frontend (JS), I root that JSObject again like so:
//----------------------------------------------
JS::RootedObject obj(cx, ((FrontEnd*)backend_obj->frontend)->obj);
//----------------------------------------------
3.) and in the finalizeOp of my frontend object (JS), I delete the FrontEnd
(and BackEnd) wrappers.
//----------------------------------------------
void obj_finalize(JSFreeOp *fop, JSObject *obj) {
auto *backend = (BackEnd*)JS_GetPrivate(obj);
auto *frontend = (FrontEnd*)backend->obj->frontend;
delete frontend;
delete backend;
};
//----------------------------------------------
Am I using the GC/tracer API correctly? I don't want to derive from
JSObject, just link the frontend and backend with wrappers. Want to know if
`JS_AddExtraGCRootsTracer`,`JS_RemmoveExtraGCRootsTracer`, and
`JS::TraceEdge` are used appropriately for this.
Hopefully this will help Miles too.
Thanks,
James