3 title: SpiderMonkey是mozilla开发的js引擎
4 description: SpiderMonkey HelloWorld
6 tags: [SpiderMonkey, js]
12 $ brew install SpiderMonkey
15 <pre class="prettyprint">
18 static JSClass global_class = {
19 "global", JSCLASS_GLOBAL_FLAGS,
20 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
21 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
22 JSCLASS_NO_OPTIONAL_MEMBERS
25 void reportError(JSContext *cx, const char *message, JSErrorReport *report)
27 fprintf(stderr, "%s:%u:%s\n",
28 report->filename ? report->filename : "<no filename=\"filename\">",
29 (unsigned int) report->lineno,
33 int main(int argc, const char *argv[])
40 rt = JS_NewRuntime(8 * 1024 * 1024);
45 cx = JS_NewContext(rt, 8192);
49 JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
50 JS_SetVersion(cx, JSVERSION_LATEST);
51 JS_SetErrorReporter(cx, reportError);
53 global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
58 if (!JS_InitStandardClasses(cx, global))
61 const char *script = "'Hello ' + 'World!'";
65 const char *filename = "noname";
68 ok = JS_EvaluateScript(cx, global, script, strlen(script),
69 filename, lineno, &rval);
70 if (!rval | rval == JS_FALSE)
73 str = JS_ValueToString(cx, rval);
74 printf("%s\n", JS_EncodeString(cx, str));
77 JS_DestroyContext(cx);
78 JS_DestroyRuntime(rt);
85 $ g++ -o bin/HelloJS -I/usr/local/include/js -lmozjs185 src/HelloJS.cpp
88 <p>4. 看来以后有时间还是要学习javascript</p>