Subj : SpiderMonkey: Run script from within script To : netscape.public.mozilla.jseng From : "Franky Braem" Date : Wed Mar 19 2003 10:16 pm Hi, In my embedding project (wxJS), I want to create a wxScript-object which give the developer the possible to load and run a script from another script. I provide 2 ways of running a script: 1. Using the wxScript constructor function The script is compiled, executed and destroyed 2. Using the wxScript object The source of the script is loaded on construction. The script is executed by calling the run-method of wxScript. The first time run is called, the script is compiled and store it in a script object. The next runs reuse the compiled script. 1 works fine. 2 gives me memory leaks when I exit the application. The code is as follows: JSBool wxJSScript::run(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxJSScript *p = (wxJSScript *) JS_GetPrivate(cx, obj); p->Execute(cx, rval); return JS_TRUE; } JSBool wxJSScript::Execute(JSContext *cx, jsval *rval) { JSObject *objGlobal = JS_GetGlobalObject(cx); if ( m_script == NULL ) // Not compiled yet { // Compile script m_script = JS_CompileScript(cx, objGlobal, m_source.c_str(), m_source.length(), m_url.c_str(), 0); if ( m_script == NULL ) { *rval = JSVAL_FALSE; return JS_FALSE; } else // On Success root it ! { m_objScript = JS_NewScriptObject(cx, m_script); JS_AddNamedRoot(cx, &m_objScript, "script"); } } // Each script needs its own context. // When performance is a problem, we probably need a context pool if ( m_cx == NULL ) { m_cx = JS_NewContext(JS_GetRuntime(cx), WXJS_CONTEXT_SIZE); JS_SetGlobalObject(m_cx, objGlobal); } JS_ExecuteScript(m_cx, objGlobal, m_script, rval); if ( m_cx ) { JS_DestroyContext(m_cx); m_cx = NULL; } return JS_TRUE; } The root is removed in the destructor callback of wxScript. Hope somebody can help me to solve this. Franky. .