Skip to content

Commit c3b9e66

Browse files
committed
src: add worker per-isolate binding initialization
Create worker binding templates in the per-isolate initialization. PR-URL: nodejs#45547 Refs: nodejs#42528 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 396b766 commit c3b9e66

File tree

8 files changed

+84
-23
lines changed

8 files changed

+84
-23
lines changed

src/async_wrap-inl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
8080
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
8181
}
8282

83+
// static
84+
inline v8::Local<v8::FunctionTemplate> AsyncWrap::GetConstructorTemplate(
85+
Environment* env) {
86+
return GetConstructorTemplate(env->isolate_data());
87+
}
88+
8389
} // namespace node
8490

8591
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/async_wrap.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,20 @@ void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
334334
}
335335
}
336336

337-
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
338-
Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template();
337+
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
338+
IsolateData* isolate_data) {
339+
Local<FunctionTemplate> tmpl = isolate_data->async_wrap_ctor_template();
339340
if (tmpl.IsEmpty()) {
340-
Isolate* isolate = env->isolate();
341+
Isolate* isolate = isolate_data->isolate();
341342
tmpl = NewFunctionTemplate(isolate, nullptr);
342-
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
343-
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
343+
tmpl->SetClassName(
344+
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "AsyncWrap"));
345+
tmpl->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
344346
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
345347
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
346348
SetProtoMethod(
347349
isolate, tmpl, "getProviderType", AsyncWrap::GetProviderType);
348-
env->set_async_wrap_ctor_template(tmpl);
350+
isolate_data->set_async_wrap_ctor_template(tmpl);
349351
}
350352
return tmpl;
351353
}

src/async_wrap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class AsyncWrap : public BaseObject {
138138
static constexpr double kInvalidAsyncId = -1;
139139

140140
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
141+
IsolateData* isolate_data);
142+
inline static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
141143
Environment* env);
142144

143145
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

src/inspector_js_api.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "async_wrap-inl.h"
12
#include "base_object-inl.h"
23
#include "inspector_agent.h"
34
#include "inspector_io.h"

src/node_binding.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ static_assert(static_cast<int>(NM_F_LINKED) ==
2424
static_cast<int>(node::ModuleFlags::kLinked),
2525
"NM_F_LINKED != node::ModuleFlags::kLinked");
2626

27-
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) V(builtins)
27+
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
28+
V(builtins) \
29+
V(worker)
2830

2931
#define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
3032
static node::node_module _module = { \

src/node_worker.cc

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using v8::MaybeLocal;
3434
using v8::Null;
3535
using v8::Number;
3636
using v8::Object;
37+
using v8::ObjectTemplate;
3738
using v8::ResourceConstraints;
3839
using v8::SealHandleScope;
3940
using v8::String;
@@ -858,19 +859,17 @@ void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
858859
}
859860
}
860861

861-
void InitWorker(Local<Object> target,
862-
Local<Value> unused,
863-
Local<Context> context,
864-
void* priv) {
865-
Environment* env = Environment::GetCurrent(context);
866-
Isolate* isolate = env->isolate();
862+
void CreateWorkerPerIsolateProperties(IsolateData* isolate_data,
863+
Local<FunctionTemplate> target) {
864+
Isolate* isolate = isolate_data->isolate();
865+
Local<ObjectTemplate> proto = target->PrototypeTemplate();
867866

868867
{
869868
Local<FunctionTemplate> w = NewFunctionTemplate(isolate, Worker::New);
870869

871870
w->InstanceTemplate()->SetInternalFieldCount(
872871
Worker::kInternalFieldCount);
873-
w->Inherit(AsyncWrap::GetConstructorTemplate(env));
872+
w->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
874873

875874
SetProtoMethod(isolate, w, "startThread", Worker::StartThread);
876875
SetProtoMethod(isolate, w, "stopThread", Worker::StopThread);
@@ -882,23 +881,32 @@ void InitWorker(Local<Object> target,
882881
SetProtoMethod(isolate, w, "loopIdleTime", Worker::LoopIdleTime);
883882
SetProtoMethod(isolate, w, "loopStartTime", Worker::LoopStartTime);
884883

885-
SetConstructorFunction(context, target, "Worker", w);
884+
SetConstructorFunction(isolate, proto, "Worker", w);
886885
}
887886

888887
{
889888
Local<FunctionTemplate> wst = NewFunctionTemplate(isolate, nullptr);
890889

891890
wst->InstanceTemplate()->SetInternalFieldCount(
892891
WorkerHeapSnapshotTaker::kInternalFieldCount);
893-
wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
892+
wst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
894893

895894
Local<String> wst_string =
896895
FIXED_ONE_BYTE_STRING(isolate, "WorkerHeapSnapshotTaker");
897896
wst->SetClassName(wst_string);
898-
env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
897+
isolate_data->set_worker_heap_snapshot_taker_template(
898+
wst->InstanceTemplate());
899899
}
900900

901-
SetMethod(context, target, "getEnvMessagePort", GetEnvMessagePort);
901+
SetMethod(isolate, proto, "getEnvMessagePort", GetEnvMessagePort);
902+
}
903+
904+
void CreateWorkerPerContextProperties(Local<Object> target,
905+
Local<Value> unused,
906+
Local<Context> context,
907+
void* priv) {
908+
Environment* env = Environment::GetCurrent(context);
909+
Isolate* isolate = env->isolate();
902910

903911
target
904912
->Set(env->context(),
@@ -951,6 +959,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
951959
} // namespace worker
952960
} // namespace node
953961

954-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)
962+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
963+
worker, node::worker::CreateWorkerPerContextProperties)
964+
NODE_BINDING_PER_ISOLATE_INIT(worker,
965+
node::worker::CreateWorkerPerIsolateProperties)
955966
NODE_BINDING_EXTERNAL_REFERENCE(worker,
956967
node::worker::RegisterExternalReferences)

src/util.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames.
5656
namespace node {
5757

5858
using v8::ArrayBufferView;
59+
using v8::Context;
60+
using v8::FunctionTemplate;
5961
using v8::Isolate;
6062
using v8::Local;
63+
using v8::Object;
6164
using v8::String;
65+
using v8::Template;
6266
using v8::Value;
6367

6468
template <typename T>
@@ -482,16 +486,35 @@ void SetConstructorFunction(Local<v8::Context> context,
482486
context, that, OneByteString(isolate, name), tmpl, flag);
483487
}
484488

485-
void SetConstructorFunction(Local<v8::Context> context,
486-
Local<v8::Object> that,
487-
Local<v8::String> name,
488-
Local<v8::FunctionTemplate> tmpl,
489+
void SetConstructorFunction(Local<Context> context,
490+
Local<Object> that,
491+
Local<String> name,
492+
Local<FunctionTemplate> tmpl,
489493
SetConstructorFunctionFlag flag) {
490494
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
491495
tmpl->SetClassName(name);
492496
that->Set(context, name, tmpl->GetFunction(context).ToLocalChecked()).Check();
493497
}
494498

499+
void SetConstructorFunction(Isolate* isolate,
500+
Local<Template> that,
501+
const char* name,
502+
Local<FunctionTemplate> tmpl,
503+
SetConstructorFunctionFlag flag) {
504+
SetConstructorFunction(
505+
isolate, that, OneByteString(isolate, name), tmpl, flag);
506+
}
507+
508+
void SetConstructorFunction(Isolate* isolate,
509+
Local<Template> that,
510+
Local<String> name,
511+
Local<FunctionTemplate> tmpl,
512+
SetConstructorFunctionFlag flag) {
513+
if (LIKELY(flag == SetConstructorFunctionFlag::SET_CLASS_NAME))
514+
tmpl->SetClassName(name);
515+
that->Set(name, tmpl);
516+
}
517+
495518
namespace {
496519

497520
class NonOwningExternalOneByteResource

src/util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,20 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
931931
SetConstructorFunctionFlag flag =
932932
SetConstructorFunctionFlag::SET_CLASS_NAME);
933933

934+
void SetConstructorFunction(v8::Isolate* isolate,
935+
v8::Local<v8::Template> that,
936+
const char* name,
937+
v8::Local<v8::FunctionTemplate> tmpl,
938+
SetConstructorFunctionFlag flag =
939+
SetConstructorFunctionFlag::SET_CLASS_NAME);
940+
941+
void SetConstructorFunction(v8::Isolate* isolate,
942+
v8::Local<v8::Template> that,
943+
v8::Local<v8::String> name,
944+
v8::Local<v8::FunctionTemplate> tmpl,
945+
SetConstructorFunctionFlag flag =
946+
SetConstructorFunctionFlag::SET_CLASS_NAME);
947+
934948
} // namespace node
935949

936950
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)