Subj : Re: server-side compilation using spidermonkey To : wyl_lyf From : Brendan Eich Date : Thu Jan 06 2005 11:40 am wyl_lyf@yahoo.com wrote: > Hello, > > thanks for the information. It would be nice to execute the script in > runtime. But I'm not sure how to do this without running apache or > opening the browser. > > Example: > Given that I only have a javascript with the response.write (".."). > So I will have to create a program that links with spidermonkey > libraries pass it my javascript server response and it will be able to > check syntax and compile? > > is this correct? > > many thanks! Sure, it's this easy: #include #include #include "jsapi.h" JSClass global_class = { "global", JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub JSCLASS_NO_OPTIONAL_MEMBERS }; int main(int argc, char **argv) { JSRuntime *rt; JSContext *cx; JSObject *global; JSBool ok; if (argc == 1) { fprintf(stderr, "usage: %s source\n", argv[0]); return -1; } rt = JS_NewRuntime(4 * 1024 * 1024); if (!rt) return 2; cx = JS_NewContext(rt, 8192); if (!cx) return 2; global = JS_NewObject(cx, &global_class, NULL, NULL); if (!global) return 2; if (!JS_InitStandardClasses(cx, global)) return 2; ok = JS_BufferIsCompilableUnit(cx, global, argv[1], strlen(argv[1])); return ok ? 0 : 1; } This program exits -1 on usage error, 2 on out of memory or similar problems, 1 on syntax error in argv[1], and 0 if argv[1] is a well-formed JS program. /be .