Skip to content

Commit 14cc3b9

Browse files
legendecasUlisesGascon
authored andcommitted
src: create worker per isolate properties
PR-URL: #48655 Backport-PR-URL: #51239 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 621c4d6 commit 14cc3b9

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

src/node_binding.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ static_assert(static_cast<int>(NM_F_LINKED) ==
3737
V(contextify) \
3838
V(encoding_binding) \
3939
V(fs) \
40+
V(messaging) \
4041
V(mksnapshot) \
41-
V(timers) \
42-
V(process_methods) \
4342
V(performance) \
43+
V(process_methods) \
44+
V(timers) \
4445
V(url) \
4546
V(worker) \
4647
NODE_BUILTIN_ICU_BINDINGS(V)

src/node_messaging.cc

+40-30
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using v8::Maybe;
3030
using v8::MaybeLocal;
3131
using v8::Nothing;
3232
using v8::Object;
33+
using v8::ObjectTemplate;
3334
using v8::SharedArrayBuffer;
3435
using v8::SharedValueConveyor;
3536
using v8::String;
@@ -707,7 +708,8 @@ MessagePort* MessagePort::New(
707708
std::unique_ptr<MessagePortData> data,
708709
std::shared_ptr<SiblingGroup> sibling_group) {
709710
Context::Scope context_scope(context);
710-
Local<FunctionTemplate> ctor_templ = GetMessagePortConstructorTemplate(env);
711+
Local<FunctionTemplate> ctor_templ =
712+
GetMessagePortConstructorTemplate(env->isolate_data());
711713

712714
// Construct a new instance, then assign the listener instance and possibly
713715
// the MessagePortData to it.
@@ -1107,7 +1109,8 @@ void MessagePort::Stop(const FunctionCallbackInfo<Value>& args) {
11071109
void MessagePort::CheckType(const FunctionCallbackInfo<Value>& args) {
11081110
Environment* env = Environment::GetCurrent(args);
11091111
args.GetReturnValue().Set(
1110-
GetMessagePortConstructorTemplate(env)->HasInstance(args[0]));
1112+
GetMessagePortConstructorTemplate(env->isolate_data())
1113+
->HasInstance(args[0]));
11111114
}
11121115

11131116
void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
@@ -1184,28 +1187,30 @@ void MessagePort::MemoryInfo(MemoryTracker* tracker) const {
11841187
tracker->TrackField("emit_message_fn", emit_message_fn_);
11851188
}
11861189

1187-
Local<FunctionTemplate> GetMessagePortConstructorTemplate(Environment* env) {
1190+
Local<FunctionTemplate> GetMessagePortConstructorTemplate(
1191+
IsolateData* isolate_data) {
11881192
// Factor generating the MessagePort JS constructor into its own piece
11891193
// of code, because it is needed early on in the child environment setup.
1190-
Local<FunctionTemplate> templ = env->message_port_constructor_template();
1194+
Local<FunctionTemplate> templ =
1195+
isolate_data->message_port_constructor_template();
11911196
if (!templ.IsEmpty())
11921197
return templ;
11931198

11941199
{
1195-
Isolate* isolate = env->isolate();
1200+
Isolate* isolate = isolate_data->isolate();
11961201
Local<FunctionTemplate> m = NewFunctionTemplate(isolate, MessagePort::New);
1197-
m->SetClassName(env->message_port_constructor_string());
1202+
m->SetClassName(isolate_data->message_port_constructor_string());
11981203
m->InstanceTemplate()->SetInternalFieldCount(
11991204
MessagePort::kInternalFieldCount);
1200-
m->Inherit(HandleWrap::GetConstructorTemplate(env));
1205+
m->Inherit(HandleWrap::GetConstructorTemplate(isolate_data));
12011206

12021207
SetProtoMethod(isolate, m, "postMessage", MessagePort::PostMessage);
12031208
SetProtoMethod(isolate, m, "start", MessagePort::Start);
12041209

1205-
env->set_message_port_constructor_template(m);
1210+
isolate_data->set_message_port_constructor_template(m);
12061211
}
12071212

1208-
return GetMessagePortConstructorTemplate(env);
1213+
return GetMessagePortConstructorTemplate(isolate_data);
12091214
}
12101215

12111216
JSTransferable::JSTransferable(Environment* env, Local<Object> obj)
@@ -1573,15 +1578,12 @@ static void BroadcastChannel(const FunctionCallbackInfo<Value>& args) {
15731578
}
15741579
}
15751580

1576-
static void InitMessaging(Local<Object> target,
1577-
Local<Value> unused,
1578-
Local<Context> context,
1579-
void* priv) {
1580-
Environment* env = Environment::GetCurrent(context);
1581-
Isolate* isolate = env->isolate();
1581+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
1582+
Local<ObjectTemplate> target) {
1583+
Isolate* isolate = isolate_data->isolate();
15821584

15831585
{
1584-
SetConstructorFunction(context,
1586+
SetConstructorFunction(isolate,
15851587
target,
15861588
"MessageChannel",
15871589
NewFunctionTemplate(isolate, MessageChannel));
@@ -1594,31 +1596,36 @@ static void InitMessaging(Local<Object> target,
15941596
JSTransferable::kInternalFieldCount);
15951597
t->SetClassName(OneByteString(isolate, "JSTransferable"));
15961598
SetConstructorFunction(
1597-
context, target, "JSTransferable", t, SetConstructorFunctionFlag::NONE);
1599+
isolate, target, "JSTransferable", t, SetConstructorFunctionFlag::NONE);
15981600
}
15991601

1600-
SetConstructorFunction(context,
1602+
SetConstructorFunction(isolate,
16011603
target,
1602-
env->message_port_constructor_string(),
1603-
GetMessagePortConstructorTemplate(env),
1604-
SetConstructorFunctionFlag::NONE);
1604+
isolate_data->message_port_constructor_string(),
1605+
GetMessagePortConstructorTemplate(isolate_data));
16051606

16061607
// These are not methods on the MessagePort prototype, because
16071608
// the browser equivalents do not provide them.
1608-
SetMethod(context, target, "stopMessagePort", MessagePort::Stop);
1609-
SetMethod(context, target, "checkMessagePort", MessagePort::CheckType);
1610-
SetMethod(context, target, "drainMessagePort", MessagePort::Drain);
1609+
SetMethod(isolate, target, "stopMessagePort", MessagePort::Stop);
1610+
SetMethod(isolate, target, "checkMessagePort", MessagePort::CheckType);
1611+
SetMethod(isolate, target, "drainMessagePort", MessagePort::Drain);
16111612
SetMethod(
1612-
context, target, "receiveMessageOnPort", MessagePort::ReceiveMessage);
1613+
isolate, target, "receiveMessageOnPort", MessagePort::ReceiveMessage);
16131614
SetMethod(
1614-
context, target, "moveMessagePortToContext", MessagePort::MoveToContext);
1615-
SetMethod(context,
1615+
isolate, target, "moveMessagePortToContext", MessagePort::MoveToContext);
1616+
SetMethod(isolate,
16161617
target,
16171618
"setDeserializerCreateObjectFunction",
16181619
SetDeserializerCreateObjectFunction);
1619-
SetMethod(context, target, "broadcastChannel", BroadcastChannel);
1620-
SetMethod(context, target, "structuredClone", StructuredClone);
1620+
SetMethod(isolate, target, "broadcastChannel", BroadcastChannel);
1621+
SetMethod(isolate, target, "structuredClone", StructuredClone);
1622+
}
16211623

1624+
static void CreatePerContextProperties(Local<Object> target,
1625+
Local<Value> unused,
1626+
Local<Context> context,
1627+
void* priv) {
1628+
Environment* env = Environment::GetCurrent(context);
16221629
{
16231630
Local<Function> domexception = GetDOMException(context).ToLocalChecked();
16241631
target
@@ -1650,6 +1657,9 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
16501657
} // namespace worker
16511658
} // namespace node
16521659

1653-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging, node::worker::InitMessaging)
1660+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging,
1661+
node::worker::CreatePerContextProperties)
1662+
NODE_BINDING_PER_ISOLATE_INIT(messaging,
1663+
node::worker::CreatePerIsolateProperties)
16541664
NODE_BINDING_EXTERNAL_REFERENCE(messaging,
16551665
node::worker::RegisterExternalReferences)

src/node_messaging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ class JSTransferable : public BaseObject {
366366
};
367367

368368
v8::Local<v8::FunctionTemplate> GetMessagePortConstructorTemplate(
369-
Environment* env);
369+
IsolateData* isolate_data);
370370

371371
} // namespace worker
372372
} // namespace node

0 commit comments

Comments
 (0)