Subj : Re: When to push/pop context on nsIJSContextStack To : Lukas Ramach From : Brendan Eich Date : Tue Nov 04 2003 11:33 am Lukas Ramach wrote: > Hi, > > I'm pre-compiling multiple javascripts which I later want to execute > (multiple times, one after the other). Each script accesses an > XPConnect wrapped XPCOM object which I set with JS_SetUCProperty > before calling JS_ExecuteScript. It works fine, I have only two > questions left: I'm curious what your code does. It would help to know, in order to answer your questions. > (*) I copied parts of initialization code from xpcshell.cpp. There I > found an nsIJSContextStack which I don't understand: when do I need to > push my context on the stack? I'm currently pushing before calling > JS_ExecuteScript and popping after. Do I also have to push/pop when > calling JS_SetUCProperty? Suppose some JS, running on a JSContext *cx associated with a DOM window, calls into C++ code. The cx parameter is lost. If the C++ code calls through an XPCOM interface that happens to be implemented in JS, we want to reenter the JS engine using the same cx, in order to preserve the window/cx affinity. > (*) Do I need to create a new context for each compiled javascript? No. You should study the API docs a bit more, and the embedding guides and examples. There is no example or advice of the form: use a context per script. It doesn't make sense to do that. > From previous newsgroup entries I think I should not. But I only could > get it running with separate contexts. I compile the scripts with > JS_CompileUCScript, create an object from the return value with > JS_NewScriptObject and root the object with JS_AddRoot. How do you root the object? Have you read http://www.mozilla.org/js/spidermonkey/gctips.html yet? /be > > Thanks, > Lukas R > .