繁体   English   中英

V8 console.log无法打印

[英]V8 console.log does not print

我正在尝试将v8嵌入到我的应用程序中,弄乱了V8环境中包含的内容(duktape不包含控制台实现),并且v8似乎包含了一个实现,但是当我调用console.log时却没有打印任何内容,而是仅打印未定义的内容(我假设这是console.log的返回值),那么如何将默认的std::cout输出与console.log链接起来。

这是我目前的代码,我正在使用默认的hello world代码进行非常小的修改。

int main(int argc, char* argv[]) {
    // Initialize V8.
    v8::V8::InitializeICUDefaultLocation(argv[0]);
    v8::V8::InitializeExternalStartupData(argv[0]);
    std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
    v8::V8::InitializePlatform(platform.get());
    v8::V8::Initialize();
    // Create a new Isolate and make it the current one.
    v8::Isolate::CreateParams create_params;
    create_params.array_buffer_allocator =
        v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    v8::Isolate* isolate = v8::Isolate::New(create_params);
    {
        v8::Isolate::Scope isolate_scope(isolate);
        // Create a stack-allocated handle scope.
        v8::HandleScope handle_scope(isolate);
        // Create a new context.
        v8::Local<v8::Context> context = v8::Context::New(isolate);
        // Enter the context for compiling and running the hello world script.
        v8::Context::Scope context_scope(context);
        {
            // Create a string containing the JavaScript source code.
            v8::Local<v8::String> source =
                v8::String::NewFromUtf8(isolate, R"(
                    console.log("does not print?")
                )",
                v8::NewStringType::kNormal)
                .ToLocalChecked();
            // Compile the source code.
            v8::Local<v8::Script> script =
                v8::Script::Compile(context, source).ToLocalChecked();
            // Run the script to get the result.
            v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
            // Convert the result to an UTF8 string and print it.
            v8::String::Utf8Value utf8(isolate, result);
            printf("%s\n", *utf8);
        }
    }
    // Dispose the isolate and tear down V8.
    isolate->Dispose();
    v8::V8::Dispose();
    v8::V8::ShutdownPlatform();
    delete create_params.array_buffer_allocator;

    std::cin.get();
    return 0;
}

我在这里使用预构建的v8二进制文件

请尝试以下操作:

  • #include "src/debug/interface-types.h"
  • 定义您自己的“控制台代表”类,该类源自debug::ConsoleDelegate
  • 重写您感兴趣的任何方法,例如void Log(const debug::ConsoleCallArguments& args, const v8::debug::ConsoleContext&) override;
  • 实例化它并调用debug::SetConsoleDelegate(isolate, &your_console_delegate); 创建Isolate

要查看示例,请从https://cs.chromium.org/chromium/src/v8/src/d8/d8-console.h?l=14&gsn=D8Console开始并跟踪其使用位置。

因此,对于将来要处理此问题的任何人,这就是我用来修复它的过程。

  • 此处下载源代码,仅需要src文件夹。
  • 提取它并将其链接到您的项目中,除了捆绑包之外,还将供应商代码放在其中。
  • 将其放在src文件夹中,因为否则它的包含项不起作用
  • 您将需要创建一堆include目录才能进行编译,包括include v8/srcv8
  • 确保将其与nuget软件包链接,您可能不必这样做,一台计算机需要它,而另一台则不需要。
  • 您不需要生成builtins-generated/bytecodes-builtins-list.h

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM