Spidermonkey source code download: http://ftp.mozilla.org/pub/mozilla.org/js/
The testing system Ubuntu 12.04, JS 1.7.0, and JS are decompressed under the/opt/JS path.
tar -zxvf js-1.7.0.tar.gz -C /opt
Spidermonkey compilation steps:
1. log on to the source code directory
cd /opt/js/src
2 compile
make -f Makefile.req
After compilation, the compiled file will be in/opt/JS/src/linux_all_dbg.obj
JS is a JS Interactive Client.
Libjs. So libjs. A is a dynamic and static library.
We also need to manually move a header file, which is probably wrong in their source code.
mv /opt/js/src/Linux_All_DBG.OBJ/jsautocfg.h /opt/js/src/
Next we will write a hello World Program for spidermonkey.
#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 \n%s\n", 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 \n%d\n",JSVAL_TO_INT(rval));JS_DestroyContext(context);JS_DestroyRuntime(runtime);JS_ShutDown();return EXIT_SUCCESS;}void usage(){ printf("example1 script_content\n"); printf("for example:./example1 \"var a=1;b=2;a+b\"\n");}
The program is extracted from the Internet and searched for spidermonkey for learning. Most of them use this program.
GCC compilation command
gcc -DXP_UNIX -I/opt/js/src -o excample test.c -L/opt/js/src/Linux_All_DBG.OBJ -ljs -lm
Compile an excample Program
To run the program, you must also add the system environment variables.
export LD_LIBRARY_PATH=/opt/js/src/Linux_All_DBG.OBJ
Test
./excample "var a=1;var b=2;a+b"
Output
script isvar a=1;var b=2;a+bthe script‘s result is3