基於 C 語言的 JavaScript 引擎探索
http://www.ibm.com/developerworks/cn/linux/l-cn-spidermonkey/
https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_User_Guide
http://zh.wikipedia.org/wiki/SpiderMonkey
下載地址:
http://ftp.mozilla.org/pub/mozilla.org/js/
SpiderMonkey-讓你的C++程序支持JavaScript腳本
http://blog.csdn.net/singlerace/article/details/1370215
使用 SpiderMonkey 腳本化您的應用
JavaScript 語言具有動態性,支持函數式編程,動態弱類型等等優點。作為一個腳本語言,可以很方便的腳本化需要高度可定制的應用程序。本文介紹基於 C 語言的 JavaScript 引擎 SpiderMonkey,詳細討論如何通過該引擎,使得 C 語言和 JavaScript 語言進行交互。
基礎知識
SpiderMonkey 簡介
和其他的 JavaScript 引擎一樣,SpiderMonkey 不直接提供像 DOM 這樣的對象,而是提供解析,執行 JavaSccript 代碼,垃圾回收等機制。SpidlerMonkey 是一個在 Mozilla 之下的開源項目,要使用 SpiderMonkey,需要下載其源碼,然后編譯為靜態 / 動態庫使用。
要在自己的應用程序中使用 SpiderMonkey,首先需要了解以下三個核心概念:
運行時環境運行時環境是所有 JavaScript 變量,對象,腳本以及代碼的上下文所存在的空間。每一個上下文對象,以及所有的對象均存在於此。一般應用僅需要一個運行時即可。
上下文上 下文即腳本執行的環境,在 SpiderMonkey 中,上下文可以編譯執行腳本,可以存取對象的屬性,調用 JavaScript 的函數,轉換類型,創建 / 維護對象等。幾乎所有的 SpiderMonkey 函數都需要上下文作為其第一個參數 (JSContext *)。
上下文與線程密不可分,一般來講,單線程應用可以使用一個上下文來完成所有的操作,每一個上下文每次只能完成一個操作,所有在多線程應用中,同一時刻只能有一個線程來使用上下文對象。一般而言,多線程應用中,每個線程對應一個上下文。
全局對象全局對象包含 JavaScript 代碼所用到的所有類,函數,變量。在 DOM 操作中,我們使用的:
alter("something");
事實上使用的是全局變量 window 的一個屬性 alter( 這個屬性正好是一個函數 ),事實上上邊的語句在執行時會別解釋為:
window.alter("something");
Error while compiling an embedded SpiderMonkey program
http://stackoverflow.com/questions/10205202/error-while-compiling-an-embedded-spidermonkey-program
helloworld.cpp:In function ‘int main(int,constchar**)’: helloworld.cpp:74:20: warning: deprecated conversion from string constant to ‘char*’[-Wwrite-strings] helloworld.cpp:83:17: warning: NULL used in arithmetic [-Wpointer-arith]/tmp/ccUU9may.o:In function `main': helloworld.cpp:(.text+0x6e): undefined reference to `JS_Init' helloworld.cpp:(.text+0x94): undefined reference to `JS_NewContext' helloworld.cpp:(.text+0xba): undefined reference to `JS_SetOptions' helloworld.cpp:(.text+0xcb): undefined reference to `JS_SetVersion' helloworld.cpp:(.text+0xdc): undefined reference to `JS_SetErrorReporter'
https://bugzilla.mozilla.org/show_bug.cgi?id=547715
c++ -o jsapi-tests -fno-rtti -fno-exceptions -Wall -Wpointer-arith -Woverloaded-virtual -Wsynth -Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -Wcast-align -Wno-invalid-offsetof -Wno-variadic-macros -Wno-long-long -g -fno-strict-aliasing -pthread -pipe -DNDEBUG -DTRIMMED -Os -freorder-blocks -fno-reorder-functions tests.o selfTest.o testPropCache.o testXDR.o testIntString.o testIsAboutToBeFinalized.o testSameValue.o testDebugger.o testDefineGetterSetterNonEnumerable.o testExtendedEq.o -Wl,--as-needed -lpthread -Wl,-rpath-link,/bin -Wl,-rpath-link,/lib -L../../../dist/bin -L../../../dist/lib -L/usr/lib -lplds4 -lplc4 -lnspr4 -lpthread -ldl ../libjs_static.a -ldl -lm ../libjs_static.a(jsapi.o): In function `JS_ClearContextThread': /tmp/xulrunner/js/src/jsapi.cpp:5901: undefined reference to `PR_Lock' /tmp/xulrunner/js/src/jsapi.cpp:5904: undefined reference to `PR_Unlock' ../libjs_static.a(jsapi.o): In function `JS_SetContextThread': /tmp/xulrunner/js/src/jsapi.cpp:5876: undefined reference to `PR_Unlock' ../libjs_static.a(jsapi.o): In function `JS_DropPrincipals': /tmp/xulrunner/js/src/jsapi.cpp:4184: undefined reference to `PR_AtomicDecrement' ../libjs_static.a(jsapi.o): In function `JS_ToggleOptions': /tmp/xulrunner/js/src/jsapi.cpp:1184: undefined reference to `PR_Lock' /tmp/xulrunner/js/src/jsapi.cpp:1189: undefined reference to `PR_Unlock' (snip) This is because the static library comes after the nspr link flags.