Subj : Re: Asynchronous script execution To : Michael Putters From : Brendan Eich Date : Thu Nov 20 2003 07:35 pm Michael Putters wrote: >Basically, my (C) application starts. At some point, everything is loaded, >and I can execute the script (JS_ExecuteScript). If that happens, the script >does its stuff, then exits. > How does it exit? > What I'd like is that the script remains in >memory, > Do you mean continue running, perhaps "in the background"? The JSScript remains in memory already, although you need to protect it with JS_NewScriptObject and a GC root. So that can't be the issue. > and that I can just call events. For example, assuming such >functions would exist : > >void startscript( void ) >{ > handle = JS_ExecuteScriptAsync( context, somescript ); > JS_CallFunction( context, global, "onStart", .. ); >} > >void js_exit( void ) /* some function that can be called from the script >{ > JS_StopScript( handle ); >} > >Hope this explains a bit better. > You have to use threads. The API and engine support threads (define JS_THREADSAFE, read the docs, especially about JS_BeginRequest and friends). It's up to you to figure out how to synchronize between the thread calling startscript and the event handlers, and the thread that actually executes the script. Be careful that you don't impose multi-threaded programming on the people who write scripts for your embedding. > Basically, I'm wonder how a web browser can >call onLoad once the script is running.. (since the execution is blocked at >the JS_ExecuteScript call) > Browser embedding execution is run-to-completion, apparently single-threaded, precisely so web JS authors don't have to be MT programmers. There may be threads under the hood, but they're hidden from scripters. /be .