Subj : Re: [spidermonkey] JS_CallFunction in Finalize To : netscape.public.mozilla.jseng From : Brendan Eich Date : Sun Aug 28 2005 01:52 pm Franck wrote: > Currently I'm working on a wrapper to libffi ( a kind of JNI for > javascript ). > The current state of my project allows me to do such things: > > function Alert( text, caption ) { > > var ret = new NativeData().PU32.Alloc(1); > new NativeModule('C:\\WINDOWS\\SYSTEM32\\User32').Proc('MessageBoxA')( > ret, DWORD( 0 ), SZ(text), SZ( caption || "Alert" ), DWORD( 1 ) ); BTW, there's no need to require .Proc('MessageBoxA') if you would rather allow just .MessageBoxA instead. Other conversions and explicit helpers could probably also be avoided too, in the interest of readability and economy of expression for your scripting audience. > return ret[0]; > } > > The 'Module' class is the one that manages the loaded dll/so file. > This class have a 'Close' method and a 'Finalize hook' that both release the > dll/so handler ( if the handler is not released by the user, it is done when > GC is called ) Ok. So far, so good. > Later, I tried to use wxJS with my project. The order of the wxJS use is > important: > > load wxJS library; > JS_NewRuntime, JS_NewContext; > wxJS_Init; > JS_ExecuteScript; > wxJS_Destroy; > JS_DestroyContext, JS_DestroyRuntime; > free wxJS library; > > My first problem is that libraries like wxJS use the SpiderMonkey's API, so > it is not possible to close them at a random ( GC ) time but only after the > context is destroyed. Must you share the same *JSRuntime* with wxJS? > My second problem is that 'wxJS_Destroy' has to be called before the context > is destroyed. > > so the only way I found is a kind of 'onClose' property that has to be > defined like this: > > function LoadWxJs() { > > var libWxJS = new NativeModule('...\\wxJS' ); // .dll/.so > libWxJS.onClose = function() { libWxJS.Proc('wxJS_Destroy')( > VOID() ); }; > var ret = new NativeData().PS32.Alloc(1); > libWxJS.Proc('wxJS_Init')( ret, MAKEPTR( JSContextToPtr() ), MAKEPTR( > JSObjectToPtr( this ) ) ); > } > > My wish is that people that use LoadWxJs has not to care about any > 'wxJS_Destroy' ... Since wxJS_Destroy is a native function, you should not expose it and the requirement to call it on your script authors. I.e., no onClose should be reflected into JS -- on the contrary, you want stronger APIs from wxJS and/or SpiderMonkey to support the mandatory destroy calling protocol. I don't know enough about wxJS to comment further, except to repeat that if you don't need to share objects from its JS engine with yours, then do not use its JSRuntime -- make your own and you should be ok (so long as only one of you calls JS_ShutDown as the very last JS API call before the app exits). /be .