今天终于利用spiderMonkey执行出来了一小段代码,记录如下:
1、下载spiderMonkey1.6
下载地址: http://ftp.mozilla.org/pub/mozilla.org/js/
将×××后进行解压缩,比如我下载到 /home/ydl/spiderMonkey-AST/ 下面,解压后就产生一个文件夹js,源文件全部在js/src中,进入js/src,进行编译(请参考README)。
以上所用的命令有:
$tar xvzf js-1.60.tar.gz
$cd js/src
$make -f Makefile.ref

2、运行一个实例程序
编译过后就会生成javascript引擎的静态库和动态库,分别是libjs.a和libjs.so,在js/src /Linux_All_DBG.OBJ/目录下面.进入这个目录就可以看见.如果要使用javascript引擎,只要在头文件中包含jsapi.h 同时在编译的时候链接这些库就可以了.
在Linux_ALL_DBG.OBJ目录下面还有一个可执行程序 js, 运行这个程序将产生类似shell的东西,可以在下面运行javascript代码.
在Linux_ALL_OBJ/目录下面还有一个头文件jasutocfg.h,编译的时候是需要用到的,否则会说找不到这个头文件,我就是在这里大意了

3、简单利用spiderMonkey进行应用程序开发
InBlock.gif /**
InBlock.gif*程序 simpleTest.c
InBlock.gif*执行一段javascript代码
InBlock.gif*/

InBlock.gif     /*包含JS引擎的API头文件*/
InBlock.gif#include "jsapi.h"
InBlock.gif /* EXIT_FAILURE and EXIT_SUCCESS */
InBlock.gif#include "stdlib.h"
InBlock.gif /* strlen */
InBlock.gif#include "string.h"
InBlock.gif#include "stdio.h"     //
InBlock.gif static void usage();
InBlock.gif int main( int argc, const char* argv[])
InBlock.gif{
InBlock.gif         /* pointer to our runtime object */
InBlock.gif         if(argc!=2){
InBlock.gif                        usage();
InBlock.gif                        exit(-1);
InBlock.gif        }
InBlock.gif        JSRuntime *runtime = NULL;
InBlock.gif         /* pointer to our context */
InBlock.gif        JSContext *context = NULL;
InBlock.gif         /* pointer to our global JavaScript object */
InBlock.gif        JSObject *global = NULL;
InBlock.gif         /* script to run (should return 100) */
InBlock.gif         const char *script = argv[1];
InBlock.gif         /* JavaScript value to store the result of the script */
InBlock.gif        jsval rval;
InBlock.gif         /* create new runtime, new context, global object */
InBlock.gif         if ((!(runtime = JS_NewRuntime(1024L * 1024L))) //初始化JS Runtime,返回结果给runtime,且不为空
InBlock.gif                || (!(context = JS_NewContext(runtime, 8192))) //创建一个Context,并将其于JS Rumtime关联起来,且不为空
InBlock.gif                || (!(global = JS_NewObject(context, NULL, NULL, NULL))) //创建全局对象
InBlock.gif                )
InBlock.gif                 return EXIT_FAILURE;
InBlock.gif         /* set global object of context and initialize standard ECMAScript
InBlock.gif         *            objects (Math, Date, ...) within this global object scope */

InBlock.gif         if (!JS_InitStandardClasses(context, global)) //实例化内置对象和全局对象
InBlock.gif                 return EXIT_FAILURE;
InBlock.gif         /* now we are ready to run our script */
InBlock.gif         if (!JS_EvaluateScript(context, global, script, strlen(script), "script", 1, &rval)) //执行命令中带有的javascript语句
InBlock.gif                 return EXIT_FAILURE;
InBlock.gif        printf( "the script's result is \n%d\n",JSVAL_TO_INT(rval));
InBlock.gif         /* clean up */
InBlock.gif        JS_DestroyContext(context); //销毁上下文环境
InBlock.gif        JS_DestroyRuntime(runtime); //销毁运行环境
InBlock.gif        JS_ShutDown();
InBlock.gif         return EXIT_SUCCESS;
InBlock.gif}
InBlock.gif void usage()
InBlock.gif{
InBlock.gif                printf( "example1 script_content\n");
InBlock.gif                printf( "for example:./example1 \"var a=1;b=2;a+b\"\n");
InBlock.gif}
InBlock.gif /**
InBlock.gif*程序结束
InBlock.gif*/

以上程序写好以后就要编译了,编译的时候要注意增加一些编译参数。这里我写了一个简单的Makefile
InBlock.gifCFLAGS = -DXP_UNIX -DJS_THREADSAFE -I$(HOME)/spiderMonkey/spiderMonkey-AST/js/src/Linux_All_DBG.OBJ        -I$(HOME)/spiderMonkey/spiderMonkey-AST/js/src/
InBlock.gifLDFLAGS = -L/home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/Linux_All_DBG.OBJ/
InBlock.gifLIBS = -ljs
InBlock.gifLIB = -lm
InBlock.gifmain:test1.c
InBlock.gif    gcc - static -DXP_UNIX -DJS_THREADSAFE -I/home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/Linux_All_DBG.OBJ        -I/home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/ -o test1 test1.c $(LDFLAGS) -ljs -lm
需要注意的是CFLAGS和DFLAGS,针对自己的路径要做修改。
-ljs -- 链接的时候增加libjs.so库
-DXP_UNIX -- 告诉编译器define XP_UNIX,使得javascript引擎知道操作系统是Unix或类似于Unix系统(这个参数是必需的,否则编译出错,我就在这里碰了跟头)
-O0 -- 不让编译器进行优化
有了以上步骤就可以进行编译运行了。初步的工作已经完成。

注:以上办法是在链接时将静态函数库编译了进去才调试通的,如果想使用动态链接库的话则需要如下步骤:
1)在目录/usr/lib下面创建/Linux_All_DBG>OBJ/libjs.so的软链接
    ln -s /home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/Linux_All_DBG.OBJ/libjs.so /usr/lib/libjs.so   ——此处无法创建硬链接,那就跨设备了
2)运行命令gcc -DXP_UNIX -DJS_THREADSAFE -I/home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/Linux_All_DBG.OBJ  -I/home/ydl/SpiderMonkey/spiderMonkey-AST/js/src/ -o test1 test1.c -ljs -lm
因为我的操作系统是CentOS自带Selinux,所以此时运行程序时出现了Permission denied的信息,处理方法为关闭SeLinux即可。打开/etc/selinux/config
把SELINUX=enforcing 用#注释掉
然后新加入一行为
SELINUX=disabled
保存、关闭重启系统即可运行程序