Boinst Boinst - 1 year ago 54
C++ Question

How can I make an object immutable in the Google V8 Javascript engine?

Is it possible to make an object immutable in the V8 Javascript Engine? V8 is embedded in a C++ application.

In my case I've created and populated an Array (code is simplified)

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

I'd like to make the resulting object "read-only" (as you might get by calling Object.freeze) before passing it to a script. One of my script authors got themselves in a confusing situation by trying to re-use this object is a convoluted way, and I'd like to make it harder for this to happen by making the object immutable.

I understand that I can do this in Javascript (Object.freeze), but I would like to be able to do it in C++ if possible.

Answer Source

This approach works, although it's a little inelegant. Essentially, I'm calling "Object.freeze" directly in Javascript, as I couldn't find a way to invoke this functionality from C++. I'm less than fluent in V8, so my code may be unnecessarily verbose.

 * Make an object immutable by calling "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);