SpiderMonkey 1.8 rc1をMacに入れる
まず,NSPRを入れる.
チェックアウト $ cvs cvs -q -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -r NSPR_4_8_4_RTM mozilla/nsprpub $ cd mozilla/nsprpub コンパイル&インストール $ ./configure $ sudo make install
/usr/local以下にインストールされる
$ ls /usr/local/include/nspr $ ls /usr/local/lib/libnspr*
次に,SpiderMonkeyを入れる.
ダウンロード $ curl -O http://ftp.mozilla.org/pub/mozilla.org/js/js-1.8.0-rc1.tar.gz $ tar xzfv js-1.8.0-rc1.tar.gz $ cd js/src コンパイル $ make JS_DIST=/usr/local JS_THREADSAFE=1 BUILD_OPT=1 -f Makefile.ref /usr/local以下に手動インストール $ sudo mkdir /usr/local/include/js $ sudo cp jsapi.h jspubtd.h jsutil.h jstypes.h jscpucfg.h jsotypes.h /usr/local/include/js $ sudo cp jscompat.h jsproto.tbl jsconfig.h jslong.h Darwin_OPT.OBJ/jsautocfg.h /usr/local/include/js $ sudo cp Darwin_OPT.OBJ/libjs.a Darwin_OPT.OBJ/libjs.a /usr/local/lib
下記サンプルをコンパイルしてみる.
/* smtest.cpp */ #include <js/jsapi.h> #include <stdio.h> #include <iostream> static JSClass global_class = { "global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS }; // The error reporter callback. void reportError(JSContext *cx, const char *message, JSErrorReport *report) { fprintf(stderr, "%s:%u: %s\n", report->filename ? report->filename : "<no filename>", (unsigned int) report->lineno, message); } int main(int argc, char *argv[]) { // JS variables. JSRuntime *rt; JSContext *cx; JSObject *global; /* * Create a JS runtime. * * JSRuntime * JS_NewRuntime(uint32 maxbytes); * maxbytes: Maximum number of allocated bytes after which garbage * collection is run. */ rt = JS_NewRuntime(8L * 1024L * 1024L); if (rt == NULL) { std::cerr << "failed to create a JS runtime" << std::endl; return 1; } /* * Create a context. * * JSContext * JS_NewContext(JSRuntime *rt, size_t stackchunksize); * rt: Parent runtime for the new context. JavaScript objects, * functions, strings, and numbers may be shared among the * contexts in a JSRuntime, but they cannot be shared across * JSRuntimes. * stackchunksize: The size, in bytes, of each "stack chunk". This is * a memory management tuning parameter which most * users should not adjust. 8192 is a good default * value. */ cx = JS_NewContext(rt, 8192); if (cx == NULL) { std::cerr << "failed to create a context" << std::endl; return 1; } JS_SetOptions(cx, JSOPTION_VAROBJFIX); JS_SetVersion(cx, JSVERSION_LATEST); JS_SetErrorReporter(cx, reportError); /* * Create the global object. * * JSObject * JS_NewObject(JSContext *cx, JSClass *clasp, * JSObject *proto, JSObject *parent); * cx: The context in which to create the new object. Requires * request. * clasp: Pointer to the class to use for the new object. If this is * NULL, an ordinary JavaScript Object is created. * proto: Pointer to the prototype object to use for the new object. * The new object will inherit all of the prototype object's * properties and methods, and the new object's __proto__ * property will be a reference to the prototype object. * parent: Pointer to the parent of the new object. The new object's * __parent__ property will be a reference to this object. If * parent is NULL, a default parent object is used. */ global = JS_NewObject(cx, &global_class, NULL, NULL); if (global == NULL) { std::cerr << "failed to create the global object" << std::endl; return 1; } /* Populate the global object with the standard globals, like Object and Array. */ if (! JS_InitStandardClasses(cx, global)) { std::cerr << "failed to populate the global object." << std::endl; return 1; } JS_DestroyContext(cx); JS_DestroyRuntime(rt); JS_ShutDown(); return 0; }
コンパイルと実行.
$ g++ -DXP_UNIX -ljs -lnspr4 smtest.cpp $ ./a.out
インクルードファイルとライブラリのパス設定を忘れずに.