[英]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
文件夾中,因為否則它的包含項不起作用 v8/src
和v8
builtins-generated/bytecodes-builtins-list.h
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.