Subj : Re: question about javascript functions To : netscape.public.mozilla.jseng From : Béatrice Philippe Date : Tue Feb 08 2005 10:02 pm 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. -- Béatrice Philippe-Derbez "Ludovic Delabre" a écrit dans le message de news: 42091608$0$532$626a14ce@news.free.fr... > > Béatrice Philippe wrote: >> yes, your example is working ! >> but i'm trying to do this in my C++ application, using jsapi. And here is >> the problem... > Yeah I understood you wanted to play with jsapi but I first needed to get > your problem... :-p > >> >> I'm interrested in modifying the body of an existing javascript function >> in its global object. > I just played a bit with jsapi using JS_CompileFunction() to dynamically > changes a function at runtime and ... no problem !? > BTW, it also works fine for me without calling deleteProperty()... > >> So for the moment, i'm currently trying to delete my old function 'fun1' >> with 'js_deleteProperty()' in the global object, and then i create a new >> function with 'js_compileFunction()' method with the same name 'fun1'. >> If my application tries to call the new fun1 with 'js_callFunction..()', >> it's working. But if the new fun1 function is called by others existing >> functions, it crashes. >> I think when i try to delete a function fun1 with 'js_deleteProperty()', >> all functions that referenced this function fun1 are not notified about >> this change. They don't know fun1 was deleted and replaced by a new fun1 >> function. > From what I know, there're no hidden reference lying around, everythin' is > evaluated at runtime (but I might be mistaken?). > >> >> My question is: >> is there any way to change dynamically the body of an existing function >> in a C++ application, using jsapi ? >> or if no, is there any solution to create a new function that would >> replace the old one ? > >> Thank you very much for your help. > Well I don't know why it doesn't work for you when it works fine for me... > What are you doing exactly ? > > See ya, > Ludovic. .