Subj : Re: Custom finalization for functions To : Alain Frisch From : Brendan Eich Date : Mon May 23 2005 11:52 am Alain Frisch wrote: > Hello, > > I'm trying to embed SpiderMonkey in a language with a GC. I'd like to > attach a custom finalization function to JS function objects (created > e.g. by JS_NewFunction) in order to tell the host language's GC that the > underlying functional object can be released. Is it possible to do so? > > I was considering to create a variant of js_FunctionClass but then the > objects of this new class wouln't be recognized as valid function > objects by JS, right? Yeah, don't do that. What is the strong reference into the other GC's heap that is stored in a Function object (either JSFunction.u.native or a reserved slot, I am guessing)? What keeps the peer function object that you reference in this way alive, if it's GC-allocated? Don't you have to mark or scan this reference, or otherwise root or protect it? If the other language really uses GC, then you don't want to finalize the peer function object, unless you can be 100% certain that no strong refs to it exist apart from the one stored in, or associated with, the JS function object. You simply want to stop marking that peer function object. XPConnect uses JS_SetGCCallback to run its own mark/sweep GC on top of the SpiderMonkey one. You might do the same. You need to keep track of JS functions that have peers in the other language's runtime. /be .