Skip to content

Commit 6c5b7e6

Browse files
legendecasRafaelGSS
authored andcommitted
src: add worker per-isolate binding initialization
Create worker binding templates in the per-isolate initialization. PR-URL: #45547 Refs: #42528 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent db535b6 commit 6c5b7e6

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;
@@ -876,19 +877,17 @@ void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
876877
}
877878
}
878879

879-
void InitWorker(Local<Object> target,
880-
Local<Value> unused,
881-
Local<Context> context,
882-
void* priv) {
883-
Environment* env = Environment::GetCurrent(context);
884-
Isolate* isolate = env->isolate();
880+
void CreateWorkerPerIsolateProperties(IsolateData* isolate_data,
881+
Local<FunctionTemplate> target) {
882+
Isolate* isolate = isolate_data->isolate();
883+
Local<ObjectTemplate> proto = target->PrototypeTemplate();
885884

886885
{
887886
Local<FunctionTemplate> w = NewFunctionTemplate(isolate, Worker::New);
888887

889888
w->InstanceTemplate()->SetInternalFieldCount(
890889
Worker::kInternalFieldCount);
891-
w->Inherit(AsyncWrap::GetConstructorTemplate(env));
890+
w->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
892891

893892
SetProtoMethod(isolate, w, "startThread", Worker::StartThread);
894893
SetProtoMethod(isolate, w, "stopThread", Worker::StopThread);
@@ -900,23 +899,32 @@ void InitWorker(Local<Object> target,
900899
SetProtoMethod(isolate, w, "loopIdleTime", Worker::LoopIdleTime);
901900
SetProtoMethod(isolate, w, "loopStartTime", Worker::LoopStartTime);
902901

903-
SetConstructorFunction(context, target, "Worker", w);
902+
SetConstructorFunction(isolate, proto, "Worker", w);
904903
}
905904

906905
{
907906
Local<FunctionTemplate> wst = NewFunctionTemplate(isolate, nullptr);
908907

909908
wst->InstanceTemplate()->SetInternalFieldCount(
910909
WorkerHeapSnapshotTaker::kInternalFieldCount);
911-
wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
910+
wst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
912911

913912
Local<String> wst_string =
914913
FIXED_ONE_BYTE_STRING(isolate, "WorkerHeapSnapshotTaker");
915914
wst->SetClassName(wst_string);
916-
env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
915+
isolate_data->set_worker_heap_snapshot_taker_template(
916+
wst->InstanceTemplate());
917917
}
918918

919-
SetMethod(context, target, "getEnvMessagePort", GetEnvMessagePort);
919+
SetMethod(isolate, proto, "getEnvMessagePort", GetEnvMessagePort);
920+
}
921+
922+
void CreateWorkerPerContextProperties(Local<Object> target,
923+
Local<Value> unused,
924+
Local<Context> context,
925+
void* priv) {
926+
Environment* env = Environment::GetCurrent(context);
927+
Isolate* isolate = env->isolate();
920928

921929
target
922930
->Set(env->context(),
@@ -969,6 +977,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
969977
} // namespace worker
970978
} // namespace node
971979

972-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)
980+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
981+
worker, node::worker::CreateWorkerPerContextProperties)
982+
NODE_BINDING_PER_ISOLATE_INIT(worker,
983+
node::worker::CreateWorkerPerIsolateProperties)
973984
NODE_BINDING_EXTERNAL_REFERENCE(worker,
974985
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
@@ -932,6 +932,20 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
932932
SetConstructorFunctionFlag flag =
933933
SetConstructorFunctionFlag::SET_CLASS_NAME);
934934

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

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

0 commit comments

Comments
 (0)