Subj : Re: Stuck with the basics To : Stu From : Brendan Eich Date : Thu May 22 2003 11:17 am > char functionscript[4000]; > You don't need to copy a string literal into a way-oversized array. > char *argnames; > Should be const char *argnames[1]; or something like that. >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); > You've just compiled a function whose *body* is 'function Hello() {var hi = "Hello World"; return hi.toString();}' -- in other words, you've just compiled a function Hello that contains another function named Hello, as if you typed this into the JS shell: function Hello() { function Hello() { var hi = "Hello World"; return hi.toString(); } } > rval =0; > executed = JS_CallFunction(cx, glob, functionptr, 0, jsargv, &rval); > You just called the outer Hello() and it returned undefined, because it contains no statements, only an inner function declaration. > >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. > That's the correct result. /be > > > > > .