Subj : Re: [spidermonkey] JS_CallFunction in Finalize To : netscape.public.mozilla.jseng From : Franck Date : Mon Aug 29 2005 12:26 am "Brendan Eich" a écrit dans le message de news: det4m6$7o22@ripley.aoltw.net... > 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. > I need to do this because I define a 'Close' function on my Module class :/ > Other conversions and explicit helpers could probably also be avoided too, > in the interest of readability and economy of expression for your > scripting audience. The only way to do this is to split the call into two steps: one to define the prototype and the other for calling the function with arguments. For the moment I mix them and I use some helpers functions : function DWORD( value ) { var nat = new NativeData().PU32; nat.Alloc()[0] = value; return nat; } function SZ( value ) { var nat = new NativeData; nat.String = value; return nat.PP; } function VOID( value ) { return new NativeData().VOID; } .... > > >> 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? Yes, In my mind, my library is just a js-side wrapper tool for native DLL, to avoid people to write any C/C++ code to make a binding with a DLL. > > >> 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 know that the best way to use a library like wxJS is to define LoadWxJs() and CloseWxJs() functions. But I want people that use LoadWxJs() (without knowing anything about its content) do not have to call CloseWxJs() ( ok, this is sugar !!, a fine thing ) When I create a javascript object with new, I do not have to call delete to free it ( thanks to the GC ) An other way to do this is to define a global/rooted array of functions that are called juste before the context is destroyed. eg : function LoadWxJs() { var libWxJS = new ... ... onBeforeJS_DestroyContext.push( libWxJS.Proc('wxJS_Destroy')( VOID() ) ); } But I don't like global things :) > > 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). A user of my library use it to load wxJS and use it in its script. eg. -- start -- LoadWxJs(); dlg = new wxDialog(null, -1, "Title", wxDefaultPosition, new wxSize( 400, 400 )); new wxStaticText( dlg, -1, 'test', wxDefaultPosition, dlg.size, wxStaticText.ALIGN_CENTER ); new wxListBox( dlg, -1 ); .... dlg.showModal(); -- end -- > > /be > > .