DSP

VC下嵌入SpiderMonkey引擎的编译详解

2019-07-13 14:46发布

/* 摘要:在你的代码中嵌入JS引擎 */ // 下载获得js-1.60.tar.gz的tarball并解压,下载地址:http://ftp.mozilla.org/pub/mozilla.org/js/ // 用vc6打开src目录下的js.mdp,vc6会提示你转换到新版的dsp,确定会得到3个dsp工程文件; // 设定这三个工程为活动工程,从上往下先设定Active Project为JS Win32 release,后编译,一个一个编译一遍,就在debug目录下得到js32.dll和js32.lib, // 把上述2个文件以及src下的*.h复制到你的应用程序目录下(或者通过选项卡设定Project的头文件包含路径) // 新建"Win32 console applation"型的工程,新建一个cpp文件,编写C代码进行测试,如下: //--------------------------------------------------------------------------- #include "stdafx.h" #include //for strlen #include #define XP_WIN #include "jsapi.h" #pragma comment(lib, "js32.lib") //--------------------------------------------------------------------------- static JSBool my_abs(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval) {       jsdouble x, z;       if (!JS_ValueToNumber(cx, argv[0], &x)) return JS_FALSE;       z = (x<0) ? -x : x;       return JS_NewDoubleValue(cx, z, rval); } static JSFunctionSpec my_functions[] = {       {"abs", my_abs, 1}, {0} }; char* VersionToString(JSVersion v) {       switch(v) {       case JSVERSION_1_0: return "JavaScript 1.0";       case JSVERSION_1_1: return "JavaScript 1.1";       case JSVERSION_1_2: return "JavaScript 1.2";       case JSVERSION_1_3: return "JavaScript 1.3";       case JSVERSION_1_4: return "JavaScript 1.4";       case JSVERSION_ECMA_3: return "ECMA 262 Edition 3";       case JSVERSION_1_5: return "JavaScript 1.5";       case JSVERSION_DEFAULT: return "Default JavaScript Version";       case JSVERSION_UNKNOWN: return "Unknown JavaScript version";       default: return "Unknown JavaScript version";       } } int main(int agc, const char* agv[]) {       JSVersion version; // 版本号       JSRuntime *rt; // 运行时的内存空间       JSContext *cx; // 环境句柄       JSObject *glob; // 对象(函数,类等)       JSBool builtins;       JSClass global_class = {// 全局对象      "global",0,      JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,      JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub       };       rt = JS_NewRuntime(8L * 1024L * 1024L); //8M       if (!rt) return 1;       cx = JS_NewContext(rt, 8192); //0x2000       if (cx == NULL) return 1;       version = JS_GetVersion(cx);       printf("Script Version: %s ", VersionToString(version));       /* 初始化全局对象和标准类 */       glob = JS_NewObject(cx, &global_class, NULL, NULL);       builtins = JS_InitStandardClasses(cx, glob);       /* 定义全局对象的方法 */       JS_DefineFunctions(cx, glob, my_functions);       //JS_DefineFunction(cx, glob, "abs", my_abs, DOIT_MINARGS, 0);       /* 执行一个外部js文件 */       /*       FILE *fp = fopen("hello.js", "rb");       char szScript[512];       fread(buff, 1, 510, fp);       fclose(fp);       */       /* 执行一段js代码 */       char* szScript = "var today=Date(); today.toString();";// var n=2001; n=n+1; var a=-305; abs(a);";       jsval rval;       JSString *str;       char* pFileName = NULL;       int nLines = 0;       if (JS_EvaluateScript(cx, glob, szScript, strlen(szScript), pFileName, nLines, &rval) == JS_FALSE) {             printf("Error Occured while Run Script Engine. ");           return 1;       }       str = JS_ValueToString(cx, rval); //rval返回脚本最后执行的那个变量的值       printf("Script Result: %s ", JS_GetStringBytes(str));       JS_DestroyContext(cx);       JS_DestroyRuntime(rt);       JS_ShutDown();       return 0; }