Subj : Re: Stuck with the basics To : netscape.public.mozilla.jseng From : "Stu" Date : Fri May 23 2003 10:51 am "Brendan Eich" wrote in message news:3ECD0616.8010407@meer.net... > > > 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 THANK YOU! You do not know how relieved I am! There is NO documentation of this anywhere! If you go down the wrong path the first time you're screwed for ever more. cheers again, Stu .