Code analysis 使用spidermonkey将javascript转换为操作码

Code analysis 使用spidermonkey将javascript转换为操作码,code-analysis,spidermonkey,Code Analysis,Spidermonkey,我对spider monkey不太熟悉,希望使用它将java脚本文件转换为字节码序列。 我得到了spider monkey并在调试模式下构建它 我想在jsapi.h中使用JS_CompileScript函数来编译javascript代码并进行分析以获得字节码,但是在编译下面的代码并运行它时,我得到了运行时错误。 错误是“spiderMonkeyTest.exe中0x0f55c020(mozjs185-1.0.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x00000d4c”。我

我对spider monkey不太熟悉,希望使用它将java脚本文件转换为字节码序列。 我得到了spider monkey并在调试模式下构建它

我想在jsapi.h中使用JS_CompileScript函数来编译javascript代码并进行分析以获得字节码,但是在编译下面的代码并运行它时,我得到了运行时错误。 错误是“spiderMonkeyTest.exe中0x0f55c020(mozjs185-1.0.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x00000d4c”。我没有解决此问题

任何人都可以帮助我解决这个问题,或者介绍其他解决方案,使用spider monkey从javascript代码中获取字节码

     // spiderMonkeyTest.cpp : Defines the entry point for the console application.
    //
    #define XP_WIN
    #include <iostream>
    #include <fstream>
    #include "stdafx.h"
    #include "jsapi.h"
    #include "jsanalyze.h"
    using namespace std;
    using namespace js;


    static JSClass global_class = { "global",
                                    JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
                                    JS_PropertyStub,
                                    NULL,
                                    JS_PropertyStub,
                                    JS_StrictPropertyStub,
                                    JS_EnumerateStub,
                                JS_ResolveStub,
                                JS_ConvertStub,
                                NULL,
                                JSCLASS_NO_OPTIONAL_MEMBERS
};





int _tmain(int argc, _TCHAR* argv[]) {

    /* Create a JS runtime. */
    JSRuntime *rt = JS_NewRuntime(16L * 1024L * 1024L);
    if (rt == NULL)
       return 1;

    /* Create a context. */
    JSContext *cx = JS_NewContext(rt, 8192);
    if (cx == NULL)
       return 1;
    JS_SetOptions(cx, JSOPTION_VAROBJFIX);

    JSScript *script;
    JSObject *obj;
    const char *js = "function a() { var tmp; tmp = 1 + 2; temp = temp * 2; alert(tmp); return 1; }";
    obj = JS_CompileScript(cx,JS_GetGlobalObject(cx),js,strlen(js),"code.js",NULL);
    script = obj->getScript();
    if (script == NULL)
       return JS_FALSE;   /* compilation error */
    js::analyze::Script *sc = new js::analyze::Script();
    sc->analyze(cx,script);

    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);

    /* Shut down the JS engine. */
    JS_ShutDown();

    return 1;
}
//spiderMonkeyTest.cpp:定义控制台应用程序的入口点。
//
#定义XP_WIN
#包括
#包括
#包括“stdafx.h”
#包括“jsapi.h”
#包括“jsanalyze.h”
使用名称空间std;
使用名称空间js;
静态JSClass全局_类={“全局”,
JSCLASS_新建_解析| JSCLASS_全局_标志,
JS_PropertySub,
无效的
JS_PropertySub,
JS_StricPropertySub,
JS_,
JS_,
JS_,
无效的
JSCLASS\u无\u可选\u成员
};
int _tmain(int argc,_TCHAR*argv[]{
/*创建一个JS运行时*/
JSRuntime*rt=JS_NewRuntime(16L*1024L*1024L);
if(rt==NULL)
返回1;
/*创建一个上下文*/
JSContext*cx=JS_NewContext(rt,8192);
if(cx==NULL)
返回1;
JS_SetOptions(cx、JSOPTION_VAROBJFIX);
JSScript*脚本;
JSObject*obj;
const char*js=“函数a(){var tmp;tmp=1+2;temp=temp*2;警报(tmp);返回1;}”;
obj=JS_CompileScript(cx,JS_GetGlobalObject(cx),JS,strlen(JS),“code.JS”,NULL);
script=obj->getScript();
如果(脚本==NULL)
返回JS_FALSE;/*编译错误*/
js::analyze::Script*sc=新的js::analyze::Script();
sc->analyze(cx,脚本);
JS_(cx);
JS_运行时(rt);
/*关闭JS引擎*/
JS_ShutDown();
返回1;
}

您正在使用哪个版本的Spidermonkey?我正在使用Firefox10附带的API,因此API可能会有所不同

在编译脚本之前,您应该创建一个新的全局对象,并通过调用
JS_NewCompartmentAndGlobalObject
()和
JS_InitStandardClasses
()对其进行初始化:

.....
 /*
 * Create the global object in a new compartment.
 * You always need a global object per context.
 */
global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
if (global == NULL)
    return 1;
/*
 * Populate the global object with the standard JavaScript
 * function and object classes, such as Object, Array, Date.
 */
if (!JS_InitStandardClasses(cx, global))
  return 1;
......
注意,函数
JS\u NewCompartmentAndGlobalObject()
现在已经过时,请查看最新的JSAPI文档以了解您使用的版本。您的
JS\u CompileScript()
调用只是尝试检索尚未创建的全局对象,这可能会导致异常。

使用函数“SaveCompiled”怎么样?它将对象/操作代码(编译的javascript)保存到文件中