Subj : Re: Custom finalization for functions To : netscape.public.mozilla.jseng From : John Bandhauer Date : Tue May 24 2005 12:35 am Alain Frisch wrote: >>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. > > > I need to be able to detect when a JSFunction that I created is > finalized. I don't see immediatly how to do that with JS_SetGCCallback. > Is there a way to retrieve all the objects just before they are finalized? You are looking for JS_IsAboutToBeFinalized. This needs to be called in a GC callback when status==JSGC_MARK_END. You can see how XPConnect does this by grep'ing around a bit. Look in XPCJSRuntime::GCCallback. There is a place where a map is enumerated with the callback function 'NativeInterfaceGC'. That callback calls something called 'DealWithDyingGCThings'. And that ends up using JS_IsAboutToBeFinalized. Some of this stuff in inlined in header files, so you'll have to dig in ..cpp and .h files to track this. The basic idea is that you need to walk through all the function objects of interest and call JS_IsAboutToBeFinalized() on each. This can only be done from a gc callback after the mark phase and before the collection phase. You need to be sure you are working on valid gcthings - there is no safety net. It is also a very good idea to have your gc callback pass control to any gc callback that was in place when your's hooked in. John. .