Subj : Stuck on the basics - HELP! To : netscape.public.mozilla.jseng From : "Stu" Date : Thu May 22 2003 01:45 pm Hi, I'm trying to call a Javascript function from C/ C++ with the Spidermonkey API I've verified that I can call a basic script with a script of var hi = "hello"; hi.toString(); This script successfully return "hello" with EvaluateScript, or CompileScript and ExecuteScript However once I put it inside a JavaScript style function i.e function Hello() {var hi = "hello"; hi.toString();} I then try to use JS_CompileFunction which Compiles OK and then JS_CallFunction which returns successfully but gives me an "Undefined" string back via rval. I'd be really gratefully if someone could take a quick look at these few lines of code i'm sure its just something simple i'm missing but I really can't find any examples of anyone trying to do this! Thanks all i've been banging my head on the desk over this for days. Stu Heres the code.... errored returns removed for simplicity JSRuntime *rt =0; JSContext *cx =0; JSObject *glob =0; JSObject *localobj; JSBool builtins =0; jsval jsargv[2]; jsval rval; JSString *str; uintN lineno = 0; JSBool executed =0; JSFunction *functionptr; char functionscript[4000]; char *argnames; rt = JS_NewRuntime (10000000L); cx = JS_NewContext(rt, 8192); JS_SetErrorReporter(cx, my_ErrorReporter); glob = JS_NewObject(cx, &global_class, 0, 0); builtins = JS_InitStandardClasses(cx, glob); /* * Define an object named in the global scope that can be enumerated by * for/in loops. The parent object is passed as the second argument, as * with all other API calls that take an object/name pair. The prototype * passed in is null, so the default object prototype will be used. */ localobj = JS_DefineObject(cx, glob, "localObject", &its_class, 0, JSPROP_ENUMERATE); if (!JS_DefineProperties(cx, localobj, its_props)) return 1; strcpy (functionscript, "function Hello() {var hi = \"Hello World\"; return hi.toString();}"); lineno = 0; functionptr = JS_CompileFunction(cx, glob, "Hello()", 0, &argnames, functionscript, strlen(functionscript), "default.pac", lineno); rval =0; executed = JS_CallFunction(cx, glob, functionptr, 0, jsargv, &rval); str = JS_ValueToString(cx, rval); JS_AddRoot(cx, &str); printf("Call function script result: %s\n", JS_GetStringBytes(str)); this prints Call function script result: undefined to the console. .