如何在Google V8 Javascript引擎中使对象不可变?

时间:2016-04-19 01:28:36

标签: c++ v8 embedded-v8

是否可以在V8 Javascript引擎中使对象不可变? V8嵌入在C ++应用程序中。

在我的情况下,我创建并填充了一个数组(代码已简化)

auto arr = v8::Array::New(isolate, 10);
for (auto i = 0; i < 10; ++i)
{
    arr->Set(context, i, v8::Integer::New(isolate, i));
}

我想在将结果传递给脚本之前将结果对象设置为“只读”(通过调用Object.freeze得到)。我的一个脚本作者通过尝试重新使用这个对象而陷入困惑的情况是一种令人费解的方式,我想通过使对象不可变来更难以实现这一点。

我知道我可以在Javascript(Object.freeze)中执行此操作,但如果可能,我希望能够在C ++中执行此操作。

1 个答案:

答案 0 :(得分:1)

这种方法很有效,虽然它有点不优雅。从本质上讲,我正在调用&#34; Object.freeze&#34;直接在Javascript中,因为我无法找到从C ++调用此功能的方法。我对V8的说法不太流利,所以我的代码可能不必要地冗长。

/**
 * Make an object immutable by calling "Object.freeze".
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
 **/
void ezv8::utility::MakeImmutable(v8::Isolate * isolate, v8::Local<v8::Object> object)
{
    ezv8::Ezv8 ezv8(isolate);
    auto globalTmpl = v8::ObjectTemplate::New(isolate);
    auto context = v8::Context::New(isolate, nullptr, globalTmpl);

    v8::Isolate::Scope scope(isolate);
    v8::Locker locker(isolate);
    v8::HandleScope scope(ezv8.getIsolate());
    v8::Context::Scope context_scope(context);

    // Define function "deepFreeze" as listed on the "Object.freeze" documentation page cited above.
    std::string code(
        "function deepFreeze(obj) {\n"
        "    var propNames = Object.getOwnPropertyNames(obj);\n"
        "    propNames.forEach(function(name) {\n"
        "        var prop = obj[name];\n"
        "        if (typeof prop == 'object' && prop !== null)\n"
        "            deepFreeze(prop);\n"
        "    });\n"
        "    return Object.freeze(obj);\n"
        "};");


    v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, code.c_str());

    v8::Local<v8::Script> compiled_script(v8::Script::Compile(source));

    // Run the script!
    v8::Local<v8::Value> result = compiled_script->Run();

    v8::Handle<v8::Value> argv[]{ object };

    v8::Handle<v8::String> process_name = v8::String::NewFromUtf8(isolate, "deepFreeze");
    v8::Handle<v8::Value> process_val = context->Global()->Get(process_name);


    v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val);
    v8::Local<v8::Function> process = v8::Local<v8::Function>::New(isolate, process_fun);

    // Call the script.
    v8::Local<v8::Value> rv = process->Call(context->Global(), 1, argv);
}