Skip to content

Commit 0dc40ad

Browse files
committed
src: add worker per-isolate binding initialization
Create worker binding templates in the per-isolate initialization.
1 parent 8f9d60d commit 0dc40ad

8 files changed

+84
-23
lines changed

src/async_wrap-inl.h

+6
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

+8-6
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

+2
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

+1
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

+3-1
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

+23-12
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;
@@ -882,19 +883,17 @@ void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
882883
}
883884
}
884885

885-
void InitWorker(Local<Object> target,
886-
Local<Value> unused,
887-
Local<Context> context,
888-
void* priv) {
889-
Environment* env = Environment::GetCurrent(context);
890-
Isolate* isolate = env->isolate();
886+
void CreateWorkerPerIsolateProperties(IsolateData* isolate_data,
887+
Local<FunctionTemplate> target) {
888+
Isolate* isolate = isolate_data->isolate();
889+
Local<ObjectTemplate> proto = target->PrototypeTemplate();
891890

892891
{
893892
Local<FunctionTemplate> w = NewFunctionTemplate(isolate, Worker::New);
894893

895894
w->InstanceTemplate()->SetInternalFieldCount(
896895
Worker::kInternalFieldCount);
897-
w->Inherit(AsyncWrap::GetConstructorTemplate(env));
896+
w->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
898897

899898
SetProtoMethod(isolate, w, "startThread", Worker::StartThread);
900899
SetProtoMethod(isolate, w, "stopThread", Worker::StopThread);
@@ -906,23 +905,32 @@ void InitWorker(Local<Object> target,
906905
SetProtoMethod(isolate, w, "loopIdleTime", Worker::LoopIdleTime);
907906
SetProtoMethod(isolate, w, "loopStartTime", Worker::LoopStartTime);
908907

909-
SetConstructorFunction(context, target, "Worker", w);
908+
SetConstructorFunction(isolate, proto, "Worker", w);
910909
}
911910

912911
{
913912
Local<FunctionTemplate> wst = NewFunctionTemplate(isolate, nullptr);
914913

915914
wst->InstanceTemplate()->SetInternalFieldCount(
916915
WorkerHeapSnapshotTaker::kInternalFieldCount);
917-
wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
916+
wst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
918917

919918
Local<String> wst_string =
920919
FIXED_ONE_BYTE_STRING(isolate, "WorkerHeapSnapshotTaker");
921920
wst->SetClassName(wst_string);
922-
env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
921+
isolate_data->set_worker_heap_snapshot_taker_template(
922+
wst->InstanceTemplate());
923923
}
924924

925-
SetMethod(context, target, "getEnvMessagePort", GetEnvMessagePort);
925+
SetMethod(isolate, proto, "getEnvMessagePort", GetEnvMessagePort);
926+
}
927+
928+
void CreateWorkerPerContextProperties(Local<Object> target,
929+
Local<Value> unused,
930+
Local<Context> context,
931+
void* priv) {
932+
Environment* env = Environment::GetCurrent(context);
933+
Isolate* isolate = env->isolate();
926934

927935
target
928936
->Set(env->context(),
@@ -975,6 +983,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
975983
} // namespace worker
976984
} // namespace node
977985

978-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)
986+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
987+
worker, node::worker::CreateWorkerPerContextProperties)
988+
NODE_BINDING_PER_ISOLATE_INIT(worker,
989+
node::worker::CreateWorkerPerIsolateProperties)
979990
NODE_BINDING_EXTERNAL_REFERENCE(worker,
980991
node::worker::RegisterExternalReferences)

src/util.cc

+27-4
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,14 +486,33 @@ 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 node

src/util.h

+14
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,20 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
927927
SetConstructorFunctionFlag flag =
928928
SetConstructorFunctionFlag::SET_CLASS_NAME);
929929

930+
void SetConstructorFunction(v8::Isolate* isolate,
931+
v8::Local<v8::Template> that,
932+
const char* name,
933+
v8::Local<v8::FunctionTemplate> tmpl,
934+
SetConstructorFunctionFlag flag =
935+
SetConstructorFunctionFlag::SET_CLASS_NAME);
936+
937+
void SetConstructorFunction(v8::Isolate* isolate,
938+
v8::Local<v8::Template> that,
939+
v8::Local<v8::String> name,
940+
v8::Local<v8::FunctionTemplate> tmpl,
941+
SetConstructorFunctionFlag flag =
942+
SetConstructorFunctionFlag::SET_CLASS_NAME);
943+
930944
} // namespace node
931945

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

0 commit comments

Comments
 (0)