JSScript *
FreezeThaw(JSContext *cx, JSScript *script)
{
    // freeze
    uint32_t nbytes;
    void *memory = JS_EncodeScript(cx, script, &nbytes);
    if (!memory)
        return NULL;

    // thaw
    script = JS_DecodeScript(cx, memory, nbytes, script->principals, script->originPrincipals);
    js_free(memory);
    return script;
}
示例#2
0
bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) {
    bool result = false;
    std::string ofp;
    if (!outputFilePath.empty()) {
        ofp = outputFilePath;
    }
    else {
        ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT;
    }
    std::cout << "Input file: " << inputFilePath << std::endl;
    JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS);
    JSContext *context = JS_NewContext(runtime, 10240);
    JS_SetOptions(context, JSOPTION_TYPE_INFERENCE);
    JS_SetVersion(context, JSVERSION_LATEST);
    JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT);
    JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT_ALWAYS);
	JSObject* global = JS_NewGlobalObject(context, &GlobalClass, NULL);
    JS_SetErrorReporter(context, &ReportError);
	if (JS_InitStandardClasses(context, global)) {

        JS::CompileOptions options(context);
        options.setUTF8(true);
        options.setSourcePolicy(JS::CompileOptions::NO_SOURCE);
        js::RootedObject rootedObject(context, global);
        std::cout << "Compiling ..." << std::endl;
        JSScript *script = JS::Compile(context, rootedObject, options, inputFilePath.c_str());
        if (script) {
            void *data = NULL;
            uint32_t length = 0;
            std::cout << "Encoding ..." << std::endl;
            data = JS_EncodeScript(context, script, &length);
            if (data) {
                if (WriteFile(ofp, data, length)) {
                    std::cout << "Done! " << "Output file: " << ofp << std::endl;
                    result = true;
                }
            }
        }
        
    }
    if (context) {
        JS_DestroyContext(context);
        context = NULL;
    }
    if (runtime) {
        JS_DestroyRuntime(runtime);
        runtime = NULL;
    }
    return result;
}
示例#3
0
static JSScript *
FreezeThaw(JSContext *cx, JS::HandleScript script)
{
    // freeze
    uint32_t nbytes;
    void *memory = JS_EncodeScript(cx, script, &nbytes);
    if (!memory)
        return nullptr;

    // thaw
    JSScript *script2 = JS_DecodeScript(cx, memory, nbytes,
                                        script->principals(), script->originPrincipals());
    js_free(memory);
    return script2;
}
示例#4
0
文件: main.cpp 项目: 6520874/pipiGame
bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) {
    bool result = false;
    std::string ofp;
    if (!outputFilePath.empty()) {
        ofp = outputFilePath;
    }
    else {
        ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT;
    }
    
    if (!JS_Init())
        return false;
    
    std::cout << "Input file: " << inputFilePath << std::endl;
    JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS);

    JSContext *cx = JS_NewContext(runtime, 10240);
    JS_SetOptions(cx, JSOPTION_TYPE_INFERENCE);
    
    JS::CompartmentOptions options;
    options.setVersion(JSVERSION_LATEST);
    
    JS::RootedObject global(cx, JS_NewGlobalObject(cx, &GlobalClass, NULL, JS::DontFireOnNewGlobalHook, options));
    
    JS_SetErrorReporter(cx, &ReportError);
    
    {
        JSAutoCompartment ac(cx, global);
    
        if (JS_InitStandardClasses(cx, global)) {
            
            JS_InitReflect(cx, global);
            
            JS_FireOnNewGlobalObject(cx, global);
            
            JS::CompileOptions options(cx);
            options.setUTF8(true);
            options.setSourcePolicy(JS::CompileOptions::NO_SOURCE);
            std::cout << "Compiling ..." << std::endl;
            
            JS::RootedScript script(cx, JS::Compile(cx, global, options, inputFilePath.c_str()));
            
            if (script) {
                void *data = NULL;
                uint32_t length = 0;
                std::cout << "Encoding ..." << std::endl;
                data = JS_EncodeScript(cx, script, &length);
                
                if (data) {
                    if (WriteFile(ofp, data, length)) {
                        std::cout << "Done! " << "Output file: " << ofp << std::endl;
                        result = true;
                    }
                }
            }
            else
            {
                std::cout << "Compiled " << inputFilePath << " fails!" << std::endl;
            }
        }
        else
        {
            std::cout << "JS_InitStandardClasses failed! " << std::endl;
        }
    }
    if (cx) {
        JS_DestroyContext(cx);
        cx = NULL;
    }
    if (runtime) {
        JS_DestroyRuntime(runtime);
        runtime = NULL;
    }
    
    JS_ShutDown();
    
    return result;
}