Skip to content

Commit 5c58341

Browse files
legendecastargos
authored andcommitted
src: create worker per isolate properties
PR-URL: #48655 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 4a1ce45 commit 5c58341

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;
@@ -717,7 +718,8 @@ MessagePort* MessagePort::New(
717718
std::unique_ptr<MessagePortData> data,
718719
std::shared_ptr<SiblingGroup> sibling_group) {
719720
Context::Scope context_scope(context);
720-
Local<FunctionTemplate> ctor_templ = GetMessagePortConstructorTemplate(env);
721+
Local<FunctionTemplate> ctor_templ =
722+
GetMessagePortConstructorTemplate(env->isolate_data());
721723

722724
// Construct a new instance, then assign the listener instance and possibly
723725
// the MessagePortData to it.
@@ -1116,7 +1118,8 @@ void MessagePort::Stop(const FunctionCallbackInfo<Value>& args) {
11161118
void MessagePort::CheckType(const FunctionCallbackInfo<Value>& args) {
11171119
Environment* env = Environment::GetCurrent(args);
11181120
args.GetReturnValue().Set(
1119-
GetMessagePortConstructorTemplate(env)->HasInstance(args[0]));
1121+
GetMessagePortConstructorTemplate(env->isolate_data())
1122+
->HasInstance(args[0]));
11201123
}
11211124

11221125
void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
@@ -1193,28 +1196,30 @@ void MessagePort::MemoryInfo(MemoryTracker* tracker) const {
11931196
tracker->TrackField("emit_message_fn", emit_message_fn_);
11941197
}
11951198

1196-
Local<FunctionTemplate> GetMessagePortConstructorTemplate(Environment* env) {
1199+
Local<FunctionTemplate> GetMessagePortConstructorTemplate(
1200+
IsolateData* isolate_data) {
11971201
// Factor generating the MessagePort JS constructor into its own piece
11981202
// of code, because it is needed early on in the child environment setup.
1199-
Local<FunctionTemplate> templ = env->message_port_constructor_template();
1203+
Local<FunctionTemplate> templ =
1204+
isolate_data->message_port_constructor_template();
12001205
if (!templ.IsEmpty())
12011206
return templ;
12021207

12031208
{
1204-
Isolate* isolate = env->isolate();
1209+
Isolate* isolate = isolate_data->isolate();
12051210
Local<FunctionTemplate> m = NewFunctionTemplate(isolate, MessagePort::New);
1206-
m->SetClassName(env->message_port_constructor_string());
1211+
m->SetClassName(isolate_data->message_port_constructor_string());
12071212
m->InstanceTemplate()->SetInternalFieldCount(
12081213
MessagePort::kInternalFieldCount);
1209-
m->Inherit(HandleWrap::GetConstructorTemplate(env));
1214+
m->Inherit(HandleWrap::GetConstructorTemplate(isolate_data));
12101215

12111216
SetProtoMethod(isolate, m, "postMessage", MessagePort::PostMessage);
12121217
SetProtoMethod(isolate, m, "start", MessagePort::Start);
12131218

1214-
env->set_message_port_constructor_template(m);
1219+
isolate_data->set_message_port_constructor_template(m);
12151220
}
12161221

1217-
return GetMessagePortConstructorTemplate(env);
1222+
return GetMessagePortConstructorTemplate(isolate_data);
12181223
}
12191224

12201225
// static
@@ -1636,15 +1641,12 @@ static void BroadcastChannel(const FunctionCallbackInfo<Value>& args) {
16361641
}
16371642
}
16381643

1639-
static void InitMessaging(Local<Object> target,
1640-
Local<Value> unused,
1641-
Local<Context> context,
1642-
void* priv) {
1643-
Environment* env = Environment::GetCurrent(context);
1644-
Isolate* isolate = env->isolate();
1644+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
1645+
Local<ObjectTemplate> target) {
1646+
Isolate* isolate = isolate_data->isolate();
16451647

16461648
{
1647-
SetConstructorFunction(context,
1649+
SetConstructorFunction(isolate,
16481650
target,
16491651
"MessageChannel",
16501652
NewFunctionTemplate(isolate, MessageChannel));
@@ -1655,31 +1657,36 @@ static void InitMessaging(Local<Object> target,
16551657
t->InstanceTemplate()->SetInternalFieldCount(
16561658
JSTransferable::kInternalFieldCount);
16571659
t->SetClassName(OneByteString(isolate, "JSTransferable"));
1658-
env->isolate_data()->set_js_transferable_constructor_template(t);
1660+
isolate_data->set_js_transferable_constructor_template(t);
16591661
}
16601662

1661-
SetConstructorFunction(context,
1663+
SetConstructorFunction(isolate,
16621664
target,
1663-
env->message_port_constructor_string(),
1664-
GetMessagePortConstructorTemplate(env),
1665-
SetConstructorFunctionFlag::NONE);
1665+
isolate_data->message_port_constructor_string(),
1666+
GetMessagePortConstructorTemplate(isolate_data));
16661667

16671668
// These are not methods on the MessagePort prototype, because
16681669
// the browser equivalents do not provide them.
1669-
SetMethod(context, target, "stopMessagePort", MessagePort::Stop);
1670-
SetMethod(context, target, "checkMessagePort", MessagePort::CheckType);
1671-
SetMethod(context, target, "drainMessagePort", MessagePort::Drain);
1670+
SetMethod(isolate, target, "stopMessagePort", MessagePort::Stop);
1671+
SetMethod(isolate, target, "checkMessagePort", MessagePort::CheckType);
1672+
SetMethod(isolate, target, "drainMessagePort", MessagePort::Drain);
16721673
SetMethod(
1673-
context, target, "receiveMessageOnPort", MessagePort::ReceiveMessage);
1674+
isolate, target, "receiveMessageOnPort", MessagePort::ReceiveMessage);
16741675
SetMethod(
1675-
context, target, "moveMessagePortToContext", MessagePort::MoveToContext);
1676-
SetMethod(context,
1676+
isolate, target, "moveMessagePortToContext", MessagePort::MoveToContext);
1677+
SetMethod(isolate,
16771678
target,
16781679
"setDeserializerCreateObjectFunction",
16791680
SetDeserializerCreateObjectFunction);
1680-
SetMethod(context, target, "broadcastChannel", BroadcastChannel);
1681-
SetMethod(context, target, "structuredClone", StructuredClone);
1681+
SetMethod(isolate, target, "broadcastChannel", BroadcastChannel);
1682+
SetMethod(isolate, target, "structuredClone", StructuredClone);
1683+
}
16821684

1685+
static void CreatePerContextProperties(Local<Object> target,
1686+
Local<Value> unused,
1687+
Local<Context> context,
1688+
void* priv) {
1689+
Environment* env = Environment::GetCurrent(context);
16831690
{
16841691
Local<Function> domexception = GetDOMException(context).ToLocalChecked();
16851692
target
@@ -1710,6 +1717,9 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
17101717
} // namespace worker
17111718
} // namespace node
17121719

1713-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging, node::worker::InitMessaging)
1720+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(messaging,
1721+
node::worker::CreatePerContextProperties)
1722+
NODE_BINDING_PER_ISOLATE_INIT(messaging,
1723+
node::worker::CreatePerIsolateProperties)
17141724
NODE_BINDING_EXTERNAL_REFERENCE(messaging,
17151725
node::worker::RegisterExternalReferences)

src/node_messaging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class JSTransferable : public BaseObject {
379379
};
380380

381381
v8::Local<v8::FunctionTemplate> GetMessagePortConstructorTemplate(
382-
Environment* env);
382+
IsolateData* isolate_data);
383383

384384
} // namespace worker
385385
} // namespace node

0 commit comments

Comments
 (0)