SpiderMonkey js引擎的静态编译与使用, SpiderMonkey的使用 SpiderMonkey js引擎的静态编译与使用 SpiderMonkey的使用 基于 C 语言的 JavaScript 引擎探索 SpiderMonkey-让你的C++程序支持JavaScript脚本 Error while compiling an embedded SpiderMonkey program

原文出处: http://yaolixing.oltag.com/gns-8ABFFE2D-EB1E-44FA-9118-217ED7959536.html

几百KB的跨平台js引擎,是不是你心之所想呢?油猴spiderMonkey 1.6静态编译生成的js.exe,可以执行一些脚本,以方便工作之用。
你也可以修改其中代码,添加你想要的黑科技。阅读和修改引擎代码,也能帮助你学习js解释器方面的知识。
SpiderMonkey Static Build:   http://yaolixing.oltag.com/gn-2E5A3287-88A2-4532-96C7-5BB3C4F6B509.html 
源码(8M): http://oltag.com:8080/yaolixing/18/00/12/06/00/SpiderMonkey1.6.rar 
js.exe( 378kb):  http://oltag.com:8080/yaolixing/18/00/12/06/00/js.rar
https://wiki.freeswitch.org/wiki/File
同样生成其它平台也不是难事。工程中js32.lib用于jsshell,照猫画虎同样也可以嵌入到其他c/c++项目中。
SpiderMonkey js引擎的静态编译与使用,  SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用
SpiderMonkey的使用
基于 C 语言的 JavaScript 引擎探索
SpiderMonkey-让你的C++程序支持JavaScript脚本
Error while compiling an embedded SpiderMonkey program
示例:
print("erffsdf");
var file = new XML("<n1><n2></n2></n1>");
print(file);

var file =new File("xx.txt");
file.open("write,create", "text");
file.writeln("The quick brown fox jumped over the lazy dogs");
file.mkdir('./abc/');
file.close();
执行结果:
SpiderMonkey js引擎的静态编译与使用,  SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用
SpiderMonkey的使用
基于 C 语言的 JavaScript 引擎探索
SpiderMonkey-让你的C++程序支持JavaScript脚本
Error while compiling an embedded SpiderMonkey program

    • SpiderMonkey是Mozilla项目的一部分,是一个用C语言实现的JavaScript脚本引擎,另外还有一个叫做Rhino的Java版本。SpiderMonkeyisMozilla'sJavaScriptenginewritteninCandC++.ItisusedinvariousMozillaproducts,includingFirefox,andisavailableundertheMPL2.它是采用C和C++编写的,可以把这个引擎嵌入到自己的应用程序里。要使
    • SpiderMonkey是Mozilla项目的一部分,是一个用C语言实现的JavaScript脚本引擎,另外还有一个叫做Rhino的Java版本。

      SpiderMonkey is Mozilla's JavaScript engine written in C and C++. It is used in various Mozilla products, including Firefox, and is available under the MPL2.

      它是采用C和C++编写的,可以把这个引擎嵌入到自己的应用程序里。

      要使用这个引擎,先要下载源代码mozjs-38.2.1.rc0.tar.bz2,编译工具vc2015, MozillaBuildSetup-2.2.0.exe,以及nspr-4.12.tar.gz源码。

      MozillaBuildSetup不要下载最新的版本,否则使用不了VC2015来编译。

      在MozillaBuildSetup安装的目录下,找到start-shell-msvc2015.bat文件,然后运行,再根据命令进行编译。

      其它就可以参考:

      http://blog.csdn.net/herorazor/article/details/45560731

       

      1.你也能动手修改C编译器 http://edu.csdn.net/course/detail/5582
      2.纸牌游戏开发http://edu.csdn.net/course/detail/5538 3. 五子棋游戏开发
      http://edu.csdn.net/course/detail/5487
      4. RPG游戏从入门到精通
      http://edu.csdn.net/course/detail/5246
      5. WiX安装工具的使用
      http://edu.csdn.net/course/detail/5207
      6. 俄罗斯方块游戏开发
      http://edu.csdn.net/course/detail/5110
      7. boost库入门基础
      http://edu.csdn.net/course/detail/5029
      8.Arduino入门基础
      http://edu.csdn.net/course/detail/4931
      9.Unity5.x游戏基础入门
      http://edu.csdn.net/course/detail/4810
      10. TensorFlow API攻略
      http://edu.csdn.net/course/detail/4495
      11. TensorFlow入门基本教程
      http://edu.csdn.net/course/detail/4369
      12. C++标准模板库从入门到精通 
      http://edu.csdn.net/course/detail/3324
      13.跟老菜鸟学C++
      http://edu.csdn.net/course/detail/2901
      14. 跟老菜鸟学python
      http://edu.csdn.net/course/detail/2592
      15. 在VC2015里学会使用tinyxml库
      http://edu.csdn.net/course/detail/2590
      16. 在Windows下SVN的版本管理与实战 
      http://edu.csdn.net/course/detail/2579
      17.Visual Studio 2015开发C++程序的基本使用 
      http://edu.csdn.net/course/detail/2570
      18.在VC2015里使用protobuf协议
      http://edu.csdn.net/course/detail/2582
      19.在VC2015里学会使用MySQL数据库
      http://edu.csdn.net/course/detail/2672

spidermonkey 源码下载:http://ftp.mozilla.org/pub/mozilla.org/js/

测试系统 Ubuntu 12.04, js 1.7.0, js 解压在/opt/js 路径下

tar -zxvf js-1.7.0.tar.gz -C /opt

SpiderMonkey 编译步骤:

1 登录源码目录

cd /opt/js/src

2 编译

make -f Makefile.req

编译好之后,编译文件会在/opt/js/src/Linux_All_DBG.OBJ

其中js 是一个js 的交互式客户端

libjs.so libjs.a 是动态库与静态库

我们还需要手工移动一下一个头文件,估计是他们源码里写错了

 mv /opt/js/src/Linux_All_DBG.OBJ/jsautocfg.h /opt/js/src/

下面我们写一个spidermonkey 的Hello World 程序

SpiderMonkey js引擎的静态编译与使用,  SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用
SpiderMonkey的使用
基于 C 语言的 JavaScript 引擎探索
SpiderMonkey-让你的C++程序支持JavaScript脚本
Error while compiling an embedded SpiderMonkey program
#include "jsapi.h"

#include "stdlib.h"

#include "string.h"
static void usage();
int main(int argc,const char* argv[])
{

if(argc!=2){
  usage();
  exit(-1);
}
JSRuntime *runtime = NULL;

JSContext *context = NULL;

JSObject *global = NULL;

const char *script = argv[1];

printf("script is 
%s
", script);

jsval rval;

if (
  (!(runtime = JS_NewRuntime(1024L * 1024L)))
  || (!(context = JS_NewContext(runtime, 8192)))
  || (!(global = JS_NewObject(context, NULL, NULL, NULL)))
)
return EXIT_FAILURE;

if (!JS_InitStandardClasses(context, global))
   return EXIT_FAILURE;

if (!JS_EvaluateScript(context, global, script, strlen(script), "script", 1, &rval))
   return EXIT_FAILURE;

printf("the script's result is 
%d
",JSVAL_TO_INT(rval));

JS_DestroyContext(context);
JS_DestroyRuntime(runtime);
JS_ShutDown();
return EXIT_SUCCESS;
}
void usage()
{
   printf("example1 script_content
");
   printf("for example:./example1 "var a=1;b=2;a+b"
");
}
SpiderMonkey js引擎的静态编译与使用,  SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用
SpiderMonkey的使用
基于 C 语言的 JavaScript 引擎探索
SpiderMonkey-让你的C++程序支持JavaScript脚本
Error while compiling an embedded SpiderMonkey program

程序是摘自网上,搜索SpiderMonkey 学习,大部分都是将这个程序。

gcc 编译命令

gcc -DXP_UNIX -I/opt/js/src -o excample test.c  -L/opt/js/src/Linux_All_DBG.OBJ -ljs -lm

编译出来一个excample 的程序

要运行,还需添加系统的环境变量

export LD_LIBRARY_PATH=/opt/js/src/Linux_All_DBG.OBJ

测试一下

./excample "var a=1;var b=2;a+b"

输出

script is
var a=1;var b=2;a+b
the script's result is
3

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://*.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.
SpiderMonkey是Mozilla项目的一部分,是一个用C语言实现的JavaScript脚本引擎,另外还有一个叫做Rhino的Java版 本。
 
 

基于 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://*.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.