SpiderMonkey的使用

基于 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.

 
原文地址:https://www.cnblogs.com/jingzhishen/p/3600855.html