我正在尝试将V8嵌入到多线程应用程序中。我的计划是在执行JS代码所需的线程中内联地执行V8代码。因此,在我的执行上下文中,我获取一个v8::Locker并在一个随机线程中输入隔离。我注意到当获取v8::Locker时,隔离器失去了它的CurrentContext。在下面的代码片段中,我将其归结为问题的本质。如果我在v8::Enter下设置了一个上下文,退出该储物柜并输入一个新的,隔离器就会失去它的上下文。请注意,我从未解释过Exit上下文。这个用法正确吗?我是不是遗漏了什么?
在下面的代码片段中,您可以看到这个问题的单线程虚拟示例。断言1和2传递并断言3失败。
v8::V8::InitializeICU(V8_ICU_FILE);
v8::V8::InitializeExternalStartupData(V8_SNAPSHOT_BIN_FILE);
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;
auto allocator = std::unique_ptr<v8::ArrayBuffer::Allocator>(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
create_params.array_buffer_allocator = allocator.get();
v8::Isolate* isolate = v8::Isolate::New(create_params);
v8::HandleScope handle_scope(isolate);
// Create a new context.
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
global->Set(isolate, "log", v8::FunctionTemplate::New(isolate, logCallback));
v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global);
// Enter the context for compiling and running the hello world script.
{
v8::Locker locker(isolate);
v8::Isolate::Scope scope(isolate);
context->Enter(); // Explicitly Enter the context (Which makes it the CurrentContext)
assert(!isolate->GetCurrentContext().IsEmpty()); // 1 - Succeeds
isolate->Exit();
isolate->Enter();
assert(!isolate->GetCurrentContext().IsEmpty()); // 2 - Succeeds
}
{
v8::Locker locker(isolate);
v8::Isolate::Scope scope(isolate);
assert(!isolate->GetCurrentContext().IsEmpty()); // 3 - Assertion Fails
}
// Create a string containing the JavaScript source code.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();发布于 2019-12-20 10:13:27
v8::Locker析构函数假设当前线程是用它正在做的任何事情来完成的,然后清理;特别是这意味着它取消了当前上下文。在下一个线程上创建下一个Locker之后,您必须(重新)输入正确的上下文。v8::Context::Scope使得这很方便。
https://stackoverflow.com/questions/59396111
复制相似问题