V8 无法设置名为“console"的 ObjectTemplate; [英] V8 Cannot set ObjectTemplate with name "console"

查看:26
本文介绍了V8 无法设置名为“console"的 ObjectTemplate;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置一个名为 console 的全局对象,但该特定名称会导致崩溃,对我而言,它似乎告诉我该对象已经存在.我可以将它设置为其他任何东西.为什么我不能将对象设置为控制台"?

I try to set a global object named console, but that specific name causes a crash and to me it seems like it's telling me that the object already exists. I can though set it to anything else. Why can I not set the object to name "console"?

V8 版本为 6.5.254.6

V8 Version is 6.5.254.6

错误

# Fatal error in ../../src/objects.cc, line 6007
# Debug check failed: !it.IsFound().

代码片段

isolate->Enter();
v8::HandleScope handle_scope(isolate);

// Create globals
auto globalObj = v8::ObjectTemplate::New(isolate);

// Create console object
auto consoleObj = v8::ObjectTemplate::New(isolate);

// Log
auto logcb = [](V8CallbackArgs args) {
    auto& log = Log::Instance();
    for (size_t i = 1; i < args.Length(); i++)
        log << *v8::String::Utf8Value(args[i]);
    log << std::endl;
};
consoleObj->Set(v8::String::NewFromUtf8(isolate, "log"), v8::FunctionTemplate::New(isolate, logcb));

// Set global object
globalObj->Set(v8::String::NewFromUtf8(isolate, "console"), consoleObj); // nonono cannot have it console, con is ok though

// Create script context
context = v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context>>(isolate, v8::Context::New(isolate, nullptr, globalObj));

{
    v8::Context::Scope context_scope(context.Get(isolate));
    v8::TryCatch tc(isolate);

    auto source = v8::String::NewFromUtf8(src.c_str());
    auto script = v8::Script::Compile(source);

    if (script->Run().IsEmpty() && CV8ScriptRuntime::isDebug) {
        v8loge "Error loading script \"" << filepath << "\n" << std::endl
            << "Exception: " << *v8::String::Utf8Value(tc.Exception()) << std::endl
            << "Stack trace: " << *v8::String::Utf8Value(tc.StackTrace()) << std::endl << Log::White;
    }
}

推荐答案

您的观察是正确的.当 V8 初始化一个 Context 时,它会在其中安装一个全局的 console 对象,并假设(由检查保护)具有该名称的属性尚不存在.当您提供自己的控制台"时,该检查将失败.

Your observation is correct. When V8 initializes a Context, it installs a global console object into it, and assumes (guarded by a check) that a property with that name does not exist yet. When you provide your own "console", that check fails.

如果你想覆盖 V8 的内置 console,你必须首先创建 Context(通过 v8::Context::New),然后删除(或覆盖)其全局对象上的 console 属性.

If you want to overwrite V8's built-in console, you'll have to create the Context first (via v8::Context::New), then delete (or overwrite) the console property on its global object.

如果您只想在您的嵌入器中拥有一个 console 对象,就像您从浏览器中知道的那个对象一样,那么我很高兴地通知您,这项工作已经完成,您可以简单地使用它:-)

If all you wanted was to have a console object in your embedder just like the one you know from browsers, then I'm happy to inform you that this work has already been done and you can simply use it :-)

这篇关于V8 无法设置名为“console"的 ObjectTemplate;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆