Subj : Re: question about javascript functions To : netscape.public.mozilla.jseng From : Ludovic Delabre Date : Wed Feb 09 2005 01:15 am Well if I try from what you gave me, I got: Assertion failure: root_points_to_gcArenaPool, at \js-1.5-rc6a\js\src\jsgc.c:972 Is that it ? I can get rid of the crash by taking out the call to JS_AddRoot()... hum... but somehow I got the idea working using "only" JS_CompileFunction ? And from what I read from spidermonkey's source, the define property is already done there... so why all the extra code ? But maybe I miss somethin'... :-) And why can't you call evaluateScript() anymore ? Works fine here... Good night, Ludovic Béatrice Philippe wrote: > what i'm doing is : > 1. reading each function body > 2. if a function contains some javascript statements my app doesn't want, my > app modifies the javascript function body (that's what i try to). > > The purpose is : > when my app changes the body of a javascript function, this function must be > called everywhere and works. > > I tried this example : > here are two functions : > function fun1() { > var i = 'hello'; > return i.toString(); > } > > function fun2() { > var j = 'hello from fun2'; > var x = fun1(); > return x.toString(); > } > somwhere in the html document: > > if we do : 'js_evaluateScript()' for the onclick event: it works. > > now, my app wants to change fun1 like this: > JSstring aBody = js_decompileFunctionBody(...); > char * aBody_char = js_GetStringBytes(...aBody); > .... here we change the content of aBody_char.... > JSFunction *new_fun = js_compileFunction(contextOfGlobalObject, > GlobalObject, aBody_char, strlen(aBody_char),.....); > JSObject * childObject = JS_GetFunctionObject(new_fun); > JS_AddRoot(contextOfGlobalObject,&childObject); > > jsval rval = OBJECT_TO_JSVAL(childObject); > JSObject *clonedFunc = JS_CloneFunctionObject(contextOfGlobalObject, > childObject, GlobalObject); > JS_DefineProperty(contextOfGlobalObject, GlobalObject, "fun1", > OBJECT_TO_JSVAL(childObject), nsnull, nsnull, > JSPROP_ENUMERATE | JSPROP_PERMANENT)); > > here, the function fun1 has been changed. Now, We can' t use > js_evaluateScript() anymore to play the 'onclick' event, but we must use > js_callFunctionValue() (or js_callFunction()) to call fun1 (and for that, we > must know that the onclick event is a function call :-)). This solution > works. > > BUT: if we want to call fun2 function: it crashes since fun1 has changed and > spidermonkey cannot resolve > "x=fun1()" statement in fun2. Why ????? pointer lost ? > if we don't change fun1 body, calling fun2 works well ! strange !!!! > > Any idea ? > Thank you. .