簡體   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