这个 `context_.Reset` 在 react-native js-runtime 的 v8 运行时实现中做了什么?
what does this `context_.Reset` do in v8 runtime implemation of react-native js-runtime?
我是 c++ 和 v8 引擎的菜鸟。当我查看 v8 JSI 实现的源代码时,我发现我无法理解: 这是 v8 运行时的构造函数:(请参阅下面的注释行)
V8Runtime::V8Runtime(const std::string &timezoneId) {
if (!s_platform) {
s_platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializeICU();
v8::V8::InitializePlatform(s_platform.get());
v8::V8::Initialize();
}
arrayBufferAllocator_.reset(
v8::ArrayBuffer::Allocator::NewDefaultAllocator());
v8::Isolate::CreateParams createParams;
createParams.array_buffer_allocator = arrayBufferAllocator_.get();
isolate_ = v8::Isolate::New(createParams);
#if defined(__ANDROID__)
if (!timezoneId.empty()) {
isolate_->DateTimeConfigurationChangeNotification(
v8::Isolate::TimeZoneDetection::kCustom, timezoneId.c_str());
}
#endif
isolate_->Enter();
v8::HandleScope scopedIsolate(isolate_);
context_.Reset(isolate_, CreateGlobalContext(isolate_));
//what does this reset for? why I need to reset the context just after I create the global context of it?
context_.Get(isolate_)->Enter();
}
下面是CreateGlobalContext
的代码:
v8::Local<v8::Context> V8Runtime::CreateGlobalContext(v8::Isolate *isolate) {
v8::HandleScope scopedIsolate(isolate);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_);
global->Set(
v8::String::NewFromUtf8(isolate, "_v8runtime", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, V8Runtime::GetRuntimeInfo));
return v8::Context::New(isolate_, nullptr, global);
}
repo 的来源是 here,一个 react-native js-runtime 的 v8 实现。
提前致谢!
context_
是一个全局句柄 (v8::Global
),它的 Reset(...)
方法是你如何分配给它包装的指针。因此,它正在从未初始化状态“重置”为在此处存储新创建的全局上下文。
This is probably just defensive programming. – Botje
不,防御性编程与它无关。
Just treat it as an assignment. – Botje
是的。如果 context_
是 v8::Local
(由于其他原因这会成为问题),那么您只会在此处看到 context_ = CreateGlobalContext(...)
。
我是 c++ 和 v8 引擎的菜鸟。当我查看 v8 JSI 实现的源代码时,我发现我无法理解: 这是 v8 运行时的构造函数:(请参阅下面的注释行)
V8Runtime::V8Runtime(const std::string &timezoneId) {
if (!s_platform) {
s_platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializeICU();
v8::V8::InitializePlatform(s_platform.get());
v8::V8::Initialize();
}
arrayBufferAllocator_.reset(
v8::ArrayBuffer::Allocator::NewDefaultAllocator());
v8::Isolate::CreateParams createParams;
createParams.array_buffer_allocator = arrayBufferAllocator_.get();
isolate_ = v8::Isolate::New(createParams);
#if defined(__ANDROID__)
if (!timezoneId.empty()) {
isolate_->DateTimeConfigurationChangeNotification(
v8::Isolate::TimeZoneDetection::kCustom, timezoneId.c_str());
}
#endif
isolate_->Enter();
v8::HandleScope scopedIsolate(isolate_);
context_.Reset(isolate_, CreateGlobalContext(isolate_));
//what does this reset for? why I need to reset the context just after I create the global context of it?
context_.Get(isolate_)->Enter();
}
下面是CreateGlobalContext
的代码:
v8::Local<v8::Context> V8Runtime::CreateGlobalContext(v8::Isolate *isolate) {
v8::HandleScope scopedIsolate(isolate);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_);
global->Set(
v8::String::NewFromUtf8(isolate, "_v8runtime", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, V8Runtime::GetRuntimeInfo));
return v8::Context::New(isolate_, nullptr, global);
}
repo 的来源是 here,一个 react-native js-runtime 的 v8 实现。 提前致谢!
context_
是一个全局句柄 (v8::Global
),它的 Reset(...)
方法是你如何分配给它包装的指针。因此,它正在从未初始化状态“重置”为在此处存储新创建的全局上下文。
This is probably just defensive programming. – Botje
不,防御性编程与它无关。
Just treat it as an assignment. – Botje
是的。如果 context_
是 v8::Local
(由于其他原因这会成为问题),那么您只会在此处看到 context_ = CreateGlobalContext(...)
。