From eb7170c481e79fa4e81111d5f914c0fb1b722049 Mon Sep 17 00:00:00 2001 From: legendecas Date: Fri, 10 Nov 2023 00:21:03 +0800 Subject: [PATCH] src: create worker per isolate properties PR-URL: https://github.com/nodejs/node/pull/48655 Backport-PR-URL: https://github.com/nodejs/node/pull/51239 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- src/node_binding.h | 5 ++-- src/node_messaging.cc | 70 ++++++++++++++++++++++++------------------- src/node_messaging.h | 2 +- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/node_binding.h b/src/node_binding.h index 9f0692ca4e190b..b09b8a5e521b18 100644 --- a/src/node_binding.h +++ b/src/node_binding.h @@ -37,10 +37,11 @@ static_assert(static_cast(NM_F_LINKED) == V(contextify) \ V(encoding_binding) \ V(fs) \ + V(messaging) \ V(mksnapshot) \ - V(timers) \ - V(process_methods) \ V(performance) \ + V(process_methods) \ + V(timers) \ V(url) \ V(worker) \ NODE_BUILTIN_ICU_BINDINGS(V) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 04b998bb28ce4b..6c9a23e669b29d 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -30,6 +30,7 @@ using v8::Maybe; using v8::MaybeLocal; using v8::Nothing; using v8::Object; +using v8::ObjectTemplate; using v8::SharedArrayBuffer; using v8::SharedValueConveyor; using v8::String; @@ -707,7 +708,8 @@ MessagePort* MessagePort::New( std::unique_ptr data, std::shared_ptr sibling_group) { Context::Scope context_scope(context); - Local ctor_templ = GetMessagePortConstructorTemplate(env); + Local ctor_templ = + GetMessagePortConstructorTemplate(env->isolate_data()); // Construct a new instance, then assign the listener instance and possibly // the MessagePortData to it. @@ -1107,7 +1109,8 @@ void MessagePort::Stop(const FunctionCallbackInfo& args) { void MessagePort::CheckType(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); args.GetReturnValue().Set( - GetMessagePortConstructorTemplate(env)->HasInstance(args[0])); + GetMessagePortConstructorTemplate(env->isolate_data()) + ->HasInstance(args[0])); } void MessagePort::Drain(const FunctionCallbackInfo& args) { @@ -1184,28 +1187,30 @@ void MessagePort::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackField("emit_message_fn", emit_message_fn_); } -Local GetMessagePortConstructorTemplate(Environment* env) { +Local GetMessagePortConstructorTemplate( + IsolateData* isolate_data) { // Factor generating the MessagePort JS constructor into its own piece // of code, because it is needed early on in the child environment setup. - Local templ = env->message_port_constructor_template(); + Local templ = + isolate_data->message_port_constructor_template(); if (!templ.IsEmpty()) return templ; { - Isolate* isolate = env->isolate(); + Isolate* isolate = isolate_data->isolate(); Local m = NewFunctionTemplate(isolate, MessagePort::New); - m->SetClassName(env->message_port_constructor_string()); + m->SetClassName(isolate_data->message_port_constructor_string()); m->InstanceTemplate()->SetInternalFieldCount( MessagePort::kInternalFieldCount); - m->Inherit(HandleWrap::GetConstructorTemplate(env)); + m->Inherit(HandleWrap::GetConstructorTemplate(isolate_data)); SetProtoMethod(isolate, m, "postMessage", MessagePort::PostMessage); SetProtoMethod(isolate, m, "start", MessagePort::Start); - env->set_message_port_constructor_template(m); + isolate_data->set_message_port_constructor_template(m); } - return GetMessagePortConstructorTemplate(env); + return GetMessagePortConstructorTemplate(isolate_data); } JSTransferable::JSTransferable(Environment* env, Local obj) @@ -1573,15 +1578,12 @@ static void BroadcastChannel(const FunctionCallbackInfo& args) { } } -static void InitMessaging(Local target, - Local unused, - Local context, - void* priv) { - Environment* env = Environment::GetCurrent(context); - Isolate* isolate = env->isolate(); +static void CreatePerIsolateProperties(IsolateData* isolate_data, + Local target) { + Isolate* isolate = isolate_data->isolate(); { - SetConstructorFunction(context, + SetConstructorFunction(isolate, target, "MessageChannel", NewFunctionTemplate(isolate, MessageChannel)); @@ -1594,31 +1596,36 @@ static void InitMessaging(Local target, JSTransferable::kInternalFieldCount); t->SetClassName(OneByteString(isolate, "JSTransferable")); SetConstructorFunction( - context, target, "JSTransferable", t, SetConstructorFunctionFlag::NONE); + isolate, target, "JSTransferable", t, SetConstructorFunctionFlag::NONE); } - SetConstructorFunction(context, + SetConstructorFunction(isolate, target, - env->message_port_constructor_string(), - GetMessagePortConstructorTemplate(env), - SetConstructorFunctionFlag::NONE); + isolate_data->message_port_constructor_string(), + GetMessagePortConstructorTemplate(isolate_data)); // These are not methods on the MessagePort prototype, because // the browser equivalents do not provide them. - SetMethod(context, target, "stopMessagePort", MessagePort::Stop); - SetMethod(context, target, "checkMessagePort", MessagePort::CheckType); - SetMethod(context, target, "drainMessagePort", MessagePort::Drain); + SetMethod(isolate, target, "stopMessagePort", MessagePort::Stop); + SetMethod(isolate, target, "checkMessagePort", MessagePort::CheckType); + SetMethod(isolate, target, "drainMessagePort", MessagePort::Drain); SetMethod( - context, target, "receiveMessageOnPort", MessagePort::ReceiveMessage); + isolate, target, "receiveMessageOnPort", MessagePort::ReceiveMessage); SetMethod( - context, target, "moveMessagePortToContext", MessagePort::MoveToContext); - SetMethod(context, + isolate, target, "moveMessagePortToContext", MessagePort::MoveToContext); + SetMethod(isolate, target, "setDeserializerCreateObjectFunction", SetDeserializerCreateObjectFunction); - SetMethod(context, target, "broadcastChannel", BroadcastChannel); - SetMethod(context, target, "structuredClone", StructuredClone); + SetMethod(isolate, target, "broadcastChannel", BroadcastChannel); + SetMethod(isolate, target, "structuredClone", StructuredClone); +} +static void CreatePerContextProperties(Local target, + Local unused, + Local context, + void* priv) { + Environment* env = Environment::GetCurrent(context); { Local domexception = GetDOMException(context).ToLocalChecked(); target @@ -1650,6 +1657,9 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { } // namespace worker } // namespace node -NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging, node::worker::InitMessaging) +NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging, + node::worker::CreatePerContextProperties) +NODE_BINDING_PER_ISOLATE_INIT(messaging, + node::worker::CreatePerIsolateProperties) NODE_BINDING_EXTERNAL_REFERENCE(messaging, node::worker::RegisterExternalReferences) diff --git a/src/node_messaging.h b/src/node_messaging.h index 1c2a564d8d58e1..2207a7fd2a0ef9 100644 --- a/src/node_messaging.h +++ b/src/node_messaging.h @@ -366,7 +366,7 @@ class JSTransferable : public BaseObject { }; v8::Local GetMessagePortConstructorTemplate( - Environment* env); + IsolateData* isolate_data); } // namespace worker } // namespace node