v8::Locker 退出隔离中的当前上下文



我正在尝试将 V8 嵌入到多线程应用程序中。我的计划是让 V8 代码在当前需要执行 JS 代码的任何线程中内联执行。 因此,在我的执行上下文中,我正在获取一个v8::Locker并在随机线程中输入隔离。我注意到在获取v8::Locker时,隔离会丢失其当前上下文。 在下面的代码片段中,我将其归结为问题的本质。如果我在 v8::Locker 下Enter上下文,请退出该存储库并输入一个新存储库,则隔离将丢失其上下文。请注意,我从未明确Exit上下文。 这是正确的用法吗?我错过了什么吗?

在下面的代码片段中,您可以看到该问题的单线程虚拟示例。 断言12通过,断言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();

v8::Locker析构函数假定当前线程已完成它正在执行的操作,并在之后进行清理;特别是这意味着它取消设置当前上下文。在下一个线程上创建下一个 Locker 后,您必须(重新(输入正确的上下文。v8::Context::Scope使这很方便。

最新更新