mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
src: add worker per-isolate binding initialization
Create worker binding templates in the per-isolate initialization. PR-URL: https://github.com/nodejs/node/pull/45547 Refs: https://github.com/nodejs/node/issues/42528 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
db535b6caa
commit
6c5b7e660b
8 files changed
+84
-23
No files matched your search
@@ -80,6 +80,12 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
|
||||
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
|
||||
}
|
||||
|
||||
// static
|
||||
inline v8::Local<v8::FunctionTemplate> AsyncWrap::GetConstructorTemplate(
|
||||
Environment* env) {
|
||||
return GetConstructorTemplate(env->isolate_data());
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
+8
-6
@@ -334,18 +334,20 @@ void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
}
|
||||
|
||||
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
|
||||
Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template();
|
||||
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
|
||||
IsolateData* isolate_data) {
|
||||
Local<FunctionTemplate> tmpl = isolate_data->async_wrap_ctor_template();
|
||||
if (tmpl.IsEmpty()) {
|
||||
Isolate* isolate = env->isolate();
|
||||
Isolate* isolate = isolate_data->isolate();
|
||||
tmpl = NewFunctionTemplate(isolate, nullptr);
|
||||
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
|
||||
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
|
||||
tmpl->SetClassName(
|
||||
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "AsyncWrap"));
|
||||
tmpl->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
|
||||
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
|
||||
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
|
||||
SetProtoMethod(
|
||||
isolate, tmpl, "getProviderType", AsyncWrap::GetProviderType);
|
||||
env->set_async_wrap_ctor_template(tmpl);
|
||||
isolate_data->set_async_wrap_ctor_template(tmpl);
|
||||
}
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
@@ -138,6 +138,8 @@ class AsyncWrap : public BaseObject {
|
||||
static constexpr double kInvalidAsyncId = -1;
|
||||
|
||||
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
|
||||
IsolateData* isolate_data);
|
||||
inline static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
|
||||
Environment* env);
|
||||
|
||||
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "async_wrap-inl.h"
|
||||
#include "base_object-inl.h"
|
||||
#include "inspector_agent.h"
|
||||
#include "inspector_io.h"
|
||||
|
||||
+3
-1
@@ -24,7 +24,9 @@ static_assert(static_cast<int>(NM_F_LINKED) ==
|
||||
static_cast<int>(node::ModuleFlags::kLinked),
|
||||
"NM_F_LINKED != node::ModuleFlags::kLinked");
|
||||
|
||||
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) V(builtins)
|
||||
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
|
||||
V(builtins) \
|
||||
V(worker)
|
||||
|
||||
#define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
|
||||
static node::node_module _module = { \
|
||||
|
||||
+23
-12
@@ -34,6 +34,7 @@ using v8::MaybeLocal;
|
||||
using v8::Null;
|
||||
using v8::Number;
|
||||
using v8::Object;
|
||||
using v8::ObjectTemplate;
|
||||
using v8::ResourceConstraints;
|
||||
using v8::SealHandleScope;
|
||||
using v8::String;
|
||||
@@ -876,19 +877,17 @@ void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
}
|
||||
|
||||
void InitWorker(Local<Object> target,
|
||||
Local<Value> unused,
|
||||
Local<Context> context,
|
||||
void* priv) {
|
||||
Environment* env = Environment::GetCurrent(context);
|
||||
Isolate* isolate = env->isolate();
|
||||
void CreateWorkerPerIsolateProperties(IsolateData* isolate_data,
|
||||
Local<FunctionTemplate> target) {
|
||||
Isolate* isolate = isolate_data->isolate();
|
||||
Local<ObjectTemplate> proto = target->PrototypeTemplate();
|
||||
|
||||
{
|
||||
Local<FunctionTemplate> w = NewFunctionTemplate(isolate, Worker::New);
|
||||
|
||||
w->InstanceTemplate()->SetInternalFieldCount(
|
||||
Worker::kInternalFieldCount);
|
||||
w->Inherit(AsyncWrap::GetConstructorTemplate(env));
|
||||
w->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
|
||||
|
||||
SetProtoMethod(isolate, w, "startThread", Worker::StartThread);
|
||||
SetProtoMethod(isolate, w, "stopThread", Worker::StopThread);
|
||||
@@ -900,7 +899,7 @@ void InitWorker(Local<Object> target,
|
||||
SetProtoMethod(isolate, w, "loopIdleTime", Worker::LoopIdleTime);
|
||||
SetProtoMethod(isolate, w, "loopStartTime", Worker::LoopStartTime);
|
||||
|
||||
SetConstructorFunction(context, target, "Worker", w);
|
||||
SetConstructorFunction(isolate, proto, "Worker", w);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -908,15 +907,24 @@ void InitWorker(Local<Object> target,
|
||||
|
||||
wst->InstanceTemplate()->SetInternalFieldCount(
|
||||
WorkerHeapSnapshotTaker::kInternalFieldCount);
|
||||
wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
|
||||
wst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
|
||||
|
||||
Local<String> wst_string =
|
||||
FIXED_ONE_BYTE_STRING(isolate, "WorkerHeapSnapshotTaker");
|
||||
wst->SetClassName(wst_string);
|
||||
env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
|
||||
isolate_data->set_worker_heap_snapshot_taker_template(
|
||||
wst->InstanceTemplate());
|
||||
}
|
||||
|
||||
SetMethod(context, target, "getEnvMessagePort", GetEnvMessagePort);
|
||||
SetMethod(isolate, proto, "getEnvMessagePort", GetEnvMessagePort);
|
||||
}
|
||||
|
||||
void CreateWorkerPerContextProperties(Local<Object> target,
|
||||
Local<Value> unused,
|
||||
Local<Context> context,
|
||||
void* priv) {
|
||||
Environment* env = Environment::GetCurrent(context);
|
||||
Isolate* isolate = env->isolate();
|
||||
|
||||
target
|
||||
->Set(env->context(),
|
||||
@@ -969,6 +977,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
} // namespace worker
|
||||
} // namespace node
|
||||
|
||||
NODE_BINDING_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)
|
||||
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
|
||||
worker, node::worker::CreateWorkerPerContextProperties)
|
||||
NODE_BINDING_PER_ISOLATE_INIT(worker,
|
||||
node::worker::CreateWorkerPerIsolateProperties)
|
||||
NODE_BINDING_EXTERNAL_REFERENCE(worker,
|
||||
node::worker::RegisterExternalReferences)
|
||||
+27
-4
@@ -56,9 +56,13 @@ static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames.
|
||||
namespace node {
|
||||
|
||||
using v8::ArrayBufferView;
|
||||
using v8::Context;
|
||||
using v8::FunctionTemplate;
|
||||
using v8::Isolate;
|
||||
using v8::Local;
|
||||
using v8::Object;
|
||||
using v8::String;
|
||||
using v8::Template;
|
||||
using v8::Value;
|
||||
|
||||
template <typename T>
|
||||
@@ -482,14 +486,33 @@ void SetConstructorFunction(Local<v8::Context> context,
|
||||
context, that, OneByteString(isolate, name), tmpl, flag);
|
||||
}
|
||||
|
||||
void SetConstructorFunction(Local<v8::Context> context,
|
||||
Local<v8::Object> that,
|
||||
Local<v8::String> name,
|
||||
Local<v8::FunctionTemplate> tmpl,
|
||||
void SetConstructorFunction(Local<Context> context,
|
||||
Local<Object> that,
|
||||
Local<String> name,
|
||||
Local<FunctionTemplate> tmpl,
|
||||
SetConstructorFunctionFlag flag) {
|
||||
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
|
||||
tmpl->SetClassName(name);
|
||||
that->Set(context, name, tmpl->GetFunction(context).ToLocalChecked()).Check();
|
||||
}
|
||||
|
||||
void SetConstructorFunction(Isolate* isolate,
|
||||
Local<Template> that,
|
||||
const char* name,
|
||||
Local<FunctionTemplate> tmpl,
|
||||
SetConstructorFunctionFlag flag) {
|
||||
SetConstructorFunction(
|
||||
isolate, that, OneByteString(isolate, name), tmpl, flag);
|
||||
}
|
||||
|
||||
void SetConstructorFunction(Isolate* isolate,
|
||||
Local<Template> that,
|
||||
Local<String> name,
|
||||
Local<FunctionTemplate> tmpl,
|
||||
SetConstructorFunctionFlag flag) {
|
||||
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
|
||||
tmpl->SetClassName(name);
|
||||
that->Set(name, tmpl);
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
+14
@@ -932,6 +932,20 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
|
||||
SetConstructorFunctionFlag flag =
|
||||
SetConstructorFunctionFlag::SET_CLASS_NAME);
|
||||
|
||||
void SetConstructorFunction(v8::Isolate* isolate,
|
||||
v8::Local<v8::Template> that,
|
||||
const char* name,
|
||||
v8::Local<v8::FunctionTemplate> tmpl,
|
||||
SetConstructorFunctionFlag flag =
|
||||
SetConstructorFunctionFlag::SET_CLASS_NAME);
|
||||
|
||||
void SetConstructorFunction(v8::Isolate* isolate,
|
||||
v8::Local<v8::Template> that,
|
||||
v8::Local<v8::String> name,
|
||||
v8::Local<v8::FunctionTemplate> tmpl,
|
||||
SetConstructorFunctionFlag flag =
|
||||
SetConstructorFunctionFlag::SET_CLASS_NAME);
|
||||
|
||||
} // namespace node
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
Reference in new issue
Block a user