Subj : Re: Can't get JS_CallFunction to work To : c.ibrahim From : Brendan Eich Date : Mon Oct 25 2004 12:17 pm c.ibrahim wrote: > Hi, > > I saw a similar question posted like 3-4 months ago, but I cannot get > much from the answer so I post this question again. > > I need to use a function many a times so I decide to use > JS_CompileFunction once and call the function many times using > JS_CallFunction. I wanna make sure the idea works so I implement a > simple test code, which to my dismay, does not work.. > > > the code snippet: > > //somewhere in class constructor > ... > JSContext* m_context = JSNewContext( > JSObject* m_global = JS_NewObject( > > > //in class function that contains the simple test code > ... > long grade = -1; > > //compile function > const char* Grade = "function grade() { return 10 }"; > const char* filename = 0; > uintN lineno = 0; > JSFunction* func = JS_CompileFunction(m_context, m_global, "grade", 0, > 0, Grade, strlen(Grade), filename, lineno); You just compiled a function named "grade" whose body is the useless inner function declaration "function grade() { return 10 }". That's the same as if you wrote it all in JS: function grade() { function grade() { return 10 } } The outer function contains no return statement, so returns undefined. Just pass the "return 10" body of the function to JS_CompileFunction as the "body" argument. /be .