Skip to content

Commit 5466bec

Browse files
joyeecheungnodejs-github-bot
authored andcommittedMay 3, 2023
bootstrap: initialize per-isolate properties of bindings separately
This patch moves the initialization of per-isolate properties of the bindings that are in the embedded snapshot separate from the initialization of their per-context properties. This is necessary for workers to share the isolate snapshot with the main thread and deserialize these properties instead of creating them from scratch. PR-URL: #47768 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 1936606 commit 5466bec

22 files changed

+384
-289
lines changed
 

‎src/async_wrap.cc

+26-16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ using v8::MaybeLocal;
4444
using v8::Nothing;
4545
using v8::Number;
4646
using v8::Object;
47+
using v8::ObjectTemplate;
4748
using v8::PropertyAttribute;
4849
using v8::ReadOnly;
4950
using v8::String;
@@ -351,24 +352,31 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
351352
return tmpl;
352353
}
353354

354-
void AsyncWrap::Initialize(Local<Object> target,
355-
Local<Value> unused,
356-
Local<Context> context,
357-
void* priv) {
355+
void AsyncWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
356+
Local<FunctionTemplate> ctor) {
357+
Isolate* isolate = isolate_data->isolate();
358+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
359+
360+
SetMethod(isolate, target, "setupHooks", SetupHooks);
361+
SetMethod(isolate, target, "setCallbackTrampoline", SetCallbackTrampoline);
362+
SetMethod(isolate, target, "pushAsyncContext", PushAsyncContext);
363+
SetMethod(isolate, target, "popAsyncContext", PopAsyncContext);
364+
SetMethod(isolate, target, "executionAsyncResource", ExecutionAsyncResource);
365+
SetMethod(isolate, target, "clearAsyncIdStack", ClearAsyncIdStack);
366+
SetMethod(isolate, target, "queueDestroyAsyncId", QueueDestroyAsyncId);
367+
SetMethod(isolate, target, "setPromiseHooks", SetPromiseHooks);
368+
SetMethod(isolate, target, "registerDestroyHook", RegisterDestroyHook);
369+
AsyncWrap::GetConstructorTemplate(isolate_data);
370+
}
371+
372+
void AsyncWrap::CreatePerContextProperties(Local<Object> target,
373+
Local<Value> unused,
374+
Local<Context> context,
375+
void* priv) {
358376
Environment* env = Environment::GetCurrent(context);
359377
Isolate* isolate = env->isolate();
360378
HandleScope scope(isolate);
361379

362-
SetMethod(context, target, "setupHooks", SetupHooks);
363-
SetMethod(context, target, "setCallbackTrampoline", SetCallbackTrampoline);
364-
SetMethod(context, target, "pushAsyncContext", PushAsyncContext);
365-
SetMethod(context, target, "popAsyncContext", PopAsyncContext);
366-
SetMethod(context, target, "executionAsyncResource", ExecutionAsyncResource);
367-
SetMethod(context, target, "clearAsyncIdStack", ClearAsyncIdStack);
368-
SetMethod(context, target, "queueDestroyAsyncId", QueueDestroyAsyncId);
369-
SetMethod(context, target, "setPromiseHooks", SetPromiseHooks);
370-
SetMethod(context, target, "registerDestroyHook", RegisterDestroyHook);
371-
372380
PropertyAttribute ReadOnlyDontDelete =
373381
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
374382

@@ -625,7 +633,6 @@ void AsyncWrap::AsyncReset(Local<Object> resource, double execution_async_id,
625633
async_id_, trigger_async_id_);
626634
}
627635

628-
629636
void AsyncWrap::EmitAsyncInit(Environment* env,
630637
Local<Object> object,
631638
Local<String> type,
@@ -710,6 +717,9 @@ Local<Object> AsyncWrap::GetOwner(Environment* env, Local<Object> obj) {
710717

711718
} // namespace node
712719

713-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(async_wrap, node::AsyncWrap::Initialize)
720+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(async_wrap,
721+
node::AsyncWrap::CreatePerContextProperties)
722+
NODE_BINDING_PER_ISOLATE_INIT(async_wrap,
723+
node::AsyncWrap::CreatePerIsolateProperties)
714724
NODE_BINDING_EXTERNAL_REFERENCE(async_wrap,
715725
node::AsyncWrap::RegisterExternalReferences)

‎src/async_wrap.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,12 @@ class AsyncWrap : public BaseObject {
147147
Environment* env);
148148

149149
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
150-
static void Initialize(v8::Local<v8::Object> target,
151-
v8::Local<v8::Value> unused,
152-
v8::Local<v8::Context> context,
153-
void* priv);
150+
static void CreatePerContextProperties(v8::Local<v8::Object> target,
151+
v8::Local<v8::Value> unused,
152+
v8::Local<v8::Context> context,
153+
void* priv);
154+
static void CreatePerIsolateProperties(
155+
IsolateData* isolate_data, v8::Local<v8::FunctionTemplate> target);
154156

155157
static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
156158
static void PushAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args);

‎src/encoding_binding.cc

+23-14
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ using v8::ArrayBuffer;
1616
using v8::BackingStore;
1717
using v8::Context;
1818
using v8::FunctionCallbackInfo;
19+
using v8::FunctionTemplate;
1920
using v8::Isolate;
2021
using v8::Local;
2122
using v8::MaybeLocal;
2223
using v8::Object;
24+
using v8::ObjectTemplate;
2325
using v8::String;
2426
using v8::Uint8Array;
2527
using v8::Value;
@@ -216,20 +218,23 @@ void BindingData::ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args) {
216218
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
217219
}
218220

219-
void BindingData::Initialize(Local<Object> target,
220-
Local<Value> unused,
221-
Local<Context> context,
222-
void* priv) {
221+
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
222+
Local<FunctionTemplate> ctor) {
223+
Isolate* isolate = isolate_data->isolate();
224+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
225+
SetMethod(isolate, target, "encodeInto", EncodeInto);
226+
SetMethodNoSideEffect(isolate, target, "encodeUtf8String", EncodeUtf8String);
227+
SetMethodNoSideEffect(isolate, target, "decodeUTF8", DecodeUTF8);
228+
SetMethodNoSideEffect(isolate, target, "toASCII", ToASCII);
229+
SetMethodNoSideEffect(isolate, target, "toUnicode", ToUnicode);
230+
}
231+
232+
void BindingData::CreatePerContextProperties(Local<Object> target,
233+
Local<Value> unused,
234+
Local<Context> context,
235+
void* priv) {
223236
Realm* realm = Realm::GetCurrent(context);
224-
BindingData* const binding_data =
225-
realm->AddBindingData<BindingData>(context, target);
226-
if (binding_data == nullptr) return;
227-
228-
SetMethod(context, target, "encodeInto", EncodeInto);
229-
SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String);
230-
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
231-
SetMethodNoSideEffect(context, target, "toASCII", ToASCII);
232-
SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode);
237+
realm->AddBindingData<BindingData>(context, target);
233238
}
234239

235240
void BindingData::RegisterTimerExternalReferences(
@@ -245,7 +250,11 @@ void BindingData::RegisterTimerExternalReferences(
245250
} // namespace node
246251

247252
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
248-
encoding_binding, node::encoding_binding::BindingData::Initialize)
253+
encoding_binding,
254+
node::encoding_binding::BindingData::CreatePerContextProperties)
255+
NODE_BINDING_PER_ISOLATE_INIT(
256+
encoding_binding,
257+
node::encoding_binding::BindingData::CreatePerIsolateProperties)
249258
NODE_BINDING_EXTERNAL_REFERENCE(
250259
encoding_binding,
251260
node::encoding_binding::BindingData::RegisterTimerExternalReferences)

‎src/encoding_binding.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class BindingData : public SnapshotableObject {
3535
static void ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
3636
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
3737

38-
static void Initialize(v8::Local<v8::Object> target,
39-
v8::Local<v8::Value> unused,
40-
v8::Local<v8::Context> context,
41-
void* priv);
38+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
39+
v8::Local<v8::FunctionTemplate> ctor);
40+
static void CreatePerContextProperties(v8::Local<v8::Object> target,
41+
v8::Local<v8::Value> unused,
42+
v8::Local<v8::Context> context,
43+
void* priv);
4244
static void RegisterTimerExternalReferences(
4345
ExternalReferenceRegistry* registry);
4446

‎src/handle_wrap.cc

+11-6
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,24 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
154154
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
155155
}
156156
}
157-
158157
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
159-
Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
158+
return GetConstructorTemplate(env->isolate_data());
159+
}
160+
161+
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(
162+
IsolateData* isolate_data) {
163+
Local<FunctionTemplate> tmpl = isolate_data->handle_wrap_ctor_template();
160164
if (tmpl.IsEmpty()) {
161-
Isolate* isolate = env->isolate();
165+
Isolate* isolate = isolate_data->isolate();
162166
tmpl = NewFunctionTemplate(isolate, nullptr);
163-
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
164-
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
167+
tmpl->SetClassName(
168+
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "HandleWrap"));
169+
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
165170
SetProtoMethod(isolate, tmpl, "close", HandleWrap::Close);
166171
SetProtoMethodNoSideEffect(isolate, tmpl, "hasRef", HandleWrap::HasRef);
167172
SetProtoMethod(isolate, tmpl, "ref", HandleWrap::Ref);
168173
SetProtoMethod(isolate, tmpl, "unref", HandleWrap::Unref);
169-
env->set_handle_wrap_ctor_template(tmpl);
174+
isolate_data->set_handle_wrap_ctor_template(tmpl);
170175
}
171176
return tmpl;
172177
}

‎src/handle_wrap.h

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class HandleWrap : public AsyncWrap {
7676
virtual void Close(
7777
v8::Local<v8::Value> close_callback = v8::Local<v8::Value>());
7878

79+
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
80+
IsolateData* isolate_data);
7981
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
8082
Environment* env);
8183
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

‎src/node_binding.h

+8
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ static_assert(static_cast<int>(NM_F_LINKED) ==
3131
#endif
3232

3333
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
34+
V(async_wrap) \
35+
V(blob) \
3436
V(builtins) \
37+
V(contextify) \
38+
V(encoding_binding) \
39+
V(fs) \
40+
V(timers) \
41+
V(process_methods) \
3542
V(performance) \
43+
V(url) \
3644
V(worker) \
3745
NODE_BUILTIN_ICU_BINDINGS(V)
3846

‎src/node_blob.cc

+21-16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ using v8::Int32;
2929
using v8::Isolate;
3030
using v8::Local;
3131
using v8::Object;
32+
using v8::ObjectTemplate;
3233
using v8::String;
3334
using v8::Uint32;
3435
using v8::Undefined;
@@ -107,23 +108,25 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
107108
}
108109
} // namespace
109110

110-
void Blob::Initialize(
111-
Local<Object> target,
112-
Local<Value> unused,
113-
Local<Context> context,
114-
void* priv) {
115-
Realm* realm = Realm::GetCurrent(context);
111+
void Blob::CreatePerIsolateProperties(IsolateData* isolate_data,
112+
Local<FunctionTemplate> ctor) {
113+
Isolate* isolate = isolate_data->isolate();
114+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
116115

117-
BlobBindingData* const binding_data =
118-
realm->AddBindingData<BlobBindingData>(context, target);
119-
if (binding_data == nullptr) return;
116+
SetMethod(isolate, target, "createBlob", New);
117+
SetMethod(isolate, target, "storeDataObject", StoreDataObject);
118+
SetMethod(isolate, target, "getDataObject", GetDataObject);
119+
SetMethod(isolate, target, "revokeObjectURL", RevokeObjectURL);
120+
SetMethod(isolate, target, "concat", Concat);
121+
SetMethod(isolate, target, "createBlobFromFilePath", BlobFromFilePath);
122+
}
120123

121-
SetMethod(context, target, "createBlob", New);
122-
SetMethod(context, target, "storeDataObject", StoreDataObject);
123-
SetMethod(context, target, "getDataObject", GetDataObject);
124-
SetMethod(context, target, "revokeObjectURL", RevokeObjectURL);
125-
SetMethod(context, target, "concat", Concat);
126-
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
124+
void Blob::CreatePerContextProperties(Local<Object> target,
125+
Local<Value> unused,
126+
Local<Context> context,
127+
void* priv) {
128+
Realm* realm = Realm::GetCurrent(context);
129+
realm->AddBindingData<BlobBindingData>(context, target);
127130
}
128131

129132
Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
@@ -562,5 +565,7 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
562565

563566
} // namespace node
564567

565-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(blob, node::Blob::Initialize)
568+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(blob,
569+
node::Blob::CreatePerContextProperties)
570+
NODE_BINDING_PER_ISOLATE_INIT(blob, node::Blob::CreatePerIsolateProperties)
566571
NODE_BINDING_EXTERNAL_REFERENCE(blob, node::Blob::RegisterExternalReferences)

‎src/node_blob.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ class Blob : public BaseObject {
2626
static void RegisterExternalReferences(
2727
ExternalReferenceRegistry* registry);
2828

29-
static void Initialize(
30-
v8::Local<v8::Object> target,
31-
v8::Local<v8::Value> unused,
32-
v8::Local<v8::Context> context,
33-
void* priv);
29+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
30+
v8::Local<v8::FunctionTemplate> ctor);
31+
static void CreatePerContextProperties(v8::Local<v8::Object> target,
32+
v8::Local<v8::Value> unused,
33+
v8::Local<v8::Context> context,
34+
void* priv);
3435

3536
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
3637
static void GetReader(const v8::FunctionCallbackInfo<v8::Value>& args);

‎src/node_contextify.cc

+43-42
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
114114
Local<Object> sandbox_obj,
115115
const ContextOptions& options) {
116116
HandleScope scope(env->isolate());
117-
InitializeGlobalTemplates(env->isolate_data());
118117
Local<ObjectTemplate> object_template = env->contextify_global_template();
119118
DCHECK(!object_template.IsEmpty());
120119
const SnapshotData* snapshot_data = env->isolate_data()->snapshot_data();
@@ -169,9 +168,6 @@ ContextifyContext::~ContextifyContext() {
169168
}
170169

171170
void ContextifyContext::InitializeGlobalTemplates(IsolateData* isolate_data) {
172-
if (!isolate_data->contextify_global_template().IsEmpty()) {
173-
return;
174-
}
175171
DCHECK(isolate_data->contextify_wrapper_template().IsEmpty());
176172
Local<FunctionTemplate> global_func_template =
177173
FunctionTemplate::New(isolate_data->isolate());
@@ -322,11 +318,12 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
322318
return result;
323319
}
324320

325-
void ContextifyContext::Init(Environment* env, Local<Object> target) {
326-
Local<Context> context = env->context();
327-
SetMethod(context, target, "makeContext", MakeContext);
328-
SetMethod(context, target, "isContext", IsContext);
329-
SetMethod(context, target, "compileFunction", CompileFunction);
321+
void ContextifyContext::CreatePerIsolateProperties(
322+
IsolateData* isolate_data, Local<ObjectTemplate> target) {
323+
Isolate* isolate = isolate_data->isolate();
324+
SetMethod(isolate, target, "makeContext", MakeContext);
325+
SetMethod(isolate, target, "isContext", IsContext);
326+
SetMethod(isolate, target, "compileFunction", CompileFunction);
330327
}
331328

332329
void ContextifyContext::RegisterExternalReferences(
@@ -735,11 +732,10 @@ void ContextifyContext::IndexedPropertyDeleterCallback(
735732
args.GetReturnValue().Set(false);
736733
}
737734

738-
void ContextifyScript::Init(Environment* env, Local<Object> target) {
739-
Isolate* isolate = env->isolate();
740-
HandleScope scope(env->isolate());
741-
Local<String> class_name =
742-
FIXED_ONE_BYTE_STRING(env->isolate(), "ContextifyScript");
735+
void ContextifyScript::CreatePerIsolateProperties(
736+
IsolateData* isolate_data, Local<ObjectTemplate> target) {
737+
Isolate* isolate = isolate_data->isolate();
738+
Local<String> class_name = FIXED_ONE_BYTE_STRING(isolate, "ContextifyScript");
743739

744740
Local<FunctionTemplate> script_tmpl = NewFunctionTemplate(isolate, New);
745741
script_tmpl->InstanceTemplate()->SetInternalFieldCount(
@@ -748,11 +744,8 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
748744
SetProtoMethod(isolate, script_tmpl, "createCachedData", CreateCachedData);
749745
SetProtoMethod(isolate, script_tmpl, "runInContext", RunInContext);
750746

751-
Local<Context> context = env->context();
752-
753-
target->Set(context, class_name,
754-
script_tmpl->GetFunction(context).ToLocalChecked()).Check();
755-
env->set_script_context_constructor_template(script_tmpl);
747+
target->Set(isolate, "ContextifyScript", script_tmpl);
748+
isolate_data->set_script_context_constructor_template(script_tmpl);
756749
}
757750

758751
void ContextifyScript::RegisterExternalReferences(
@@ -1382,46 +1375,53 @@ void MicrotaskQueueWrap::New(const FunctionCallbackInfo<Value>& args) {
13821375
new MicrotaskQueueWrap(Environment::GetCurrent(args), args.This());
13831376
}
13841377

1385-
void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) {
1386-
Isolate* isolate = env->isolate();
1378+
void MicrotaskQueueWrap::CreatePerIsolateProperties(
1379+
IsolateData* isolate_data, Local<ObjectTemplate> target) {
1380+
Isolate* isolate = isolate_data->isolate();
13871381
HandleScope scope(isolate);
1388-
Local<Context> context = env->context();
13891382
Local<FunctionTemplate> tmpl = NewFunctionTemplate(isolate, New);
13901383
tmpl->InstanceTemplate()->SetInternalFieldCount(
13911384
ContextifyScript::kInternalFieldCount);
1392-
env->set_microtask_queue_ctor_template(tmpl);
1393-
SetConstructorFunction(context, target, "MicrotaskQueue", tmpl);
1385+
isolate_data->set_microtask_queue_ctor_template(tmpl);
1386+
SetConstructorFunction(isolate, target, "MicrotaskQueue", tmpl);
13941387
}
13951388

13961389
void MicrotaskQueueWrap::RegisterExternalReferences(
13971390
ExternalReferenceRegistry* registry) {
13981391
registry->Register(New);
13991392
}
14001393

1401-
void Initialize(Local<Object> target,
1402-
Local<Value> unused,
1403-
Local<Context> context,
1404-
void* priv) {
1405-
Environment* env = Environment::GetCurrent(context);
1406-
Isolate* isolate = env->isolate();
1407-
ContextifyContext::Init(env, target);
1408-
ContextifyScript::Init(env, target);
1409-
MicrotaskQueueWrap::Init(env, target);
1394+
void CreatePerIsolateProperties(IsolateData* isolate_data,
1395+
Local<FunctionTemplate> ctor) {
1396+
Isolate* isolate = isolate_data->isolate();
1397+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
1398+
ContextifyContext::CreatePerIsolateProperties(isolate_data, target);
1399+
ContextifyScript::CreatePerIsolateProperties(isolate_data, target);
1400+
MicrotaskQueueWrap::CreatePerIsolateProperties(isolate_data, target);
14101401

1411-
SetMethod(context, target, "startSigintWatchdog", StartSigintWatchdog);
1412-
SetMethod(context, target, "stopSigintWatchdog", StopSigintWatchdog);
1402+
SetMethod(isolate, target, "startSigintWatchdog", StartSigintWatchdog);
1403+
SetMethod(isolate, target, "stopSigintWatchdog", StopSigintWatchdog);
14131404
// Used in tests.
14141405
SetMethodNoSideEffect(
1415-
context, target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
1406+
isolate, target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
14161407

14171408
{
1418-
Local<FunctionTemplate> tpl = FunctionTemplate::New(env->isolate());
1419-
tpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "CompiledFnEntry"));
1409+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate);
1410+
tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "CompiledFnEntry"));
14201411
tpl->InstanceTemplate()->SetInternalFieldCount(
14211412
CompiledFnEntry::kInternalFieldCount);
14221413

1423-
env->set_compiled_fn_entry_template(tpl->InstanceTemplate());
1414+
isolate_data->set_compiled_fn_entry_template(tpl->InstanceTemplate());
14241415
}
1416+
SetMethod(isolate, target, "measureMemory", MeasureMemory);
1417+
}
1418+
1419+
static void CreatePerContextProperties(Local<Object> target,
1420+
Local<Value> unused,
1421+
Local<Context> context,
1422+
void* priv) {
1423+
Environment* env = Environment::GetCurrent(context);
1424+
Isolate* isolate = env->isolate();
14251425

14261426
Local<Object> constants = Object::New(env->isolate());
14271427
Local<Object> measure_memory = Object::New(env->isolate());
@@ -1447,8 +1447,6 @@ void Initialize(Local<Object> target,
14471447
READONLY_PROPERTY(constants, "measureMemory", measure_memory);
14481448

14491449
target->Set(context, env->constants_string(), constants).Check();
1450-
1451-
SetMethod(context, target, "measureMemory", MeasureMemory);
14521450
}
14531451

14541452
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
@@ -1464,6 +1462,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
14641462
} // namespace contextify
14651463
} // namespace node
14661464

1467-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(contextify, node::contextify::Initialize)
1465+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
1466+
contextify, node::contextify::CreatePerContextProperties)
1467+
NODE_BINDING_PER_ISOLATE_INIT(contextify,
1468+
node::contextify::CreatePerIsolateProperties)
14681469
NODE_BINDING_EXTERNAL_REFERENCE(contextify,
14691470
node::contextify::RegisterExternalReferences)

‎src/node_contextify.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class MicrotaskQueueWrap : public BaseObject {
1818

1919
const std::shared_ptr<v8::MicrotaskQueue>& microtask_queue() const;
2020

21-
static void Init(Environment* env, v8::Local<v8::Object> target);
21+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
22+
v8::Local<v8::ObjectTemplate> target);
2223
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
2324
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
2425

@@ -58,7 +59,8 @@ class ContextifyContext : public BaseObject {
5859
v8::Local<v8::ObjectTemplate> object_template,
5960
const SnapshotData* snapshot_data,
6061
v8::MicrotaskQueue* queue);
61-
static void Init(Environment* env, v8::Local<v8::Object> target);
62+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
63+
v8::Local<v8::ObjectTemplate> target);
6264
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
6365

6466
static ContextifyContext* ContextFromContextifiedSandbox(
@@ -156,7 +158,8 @@ class ContextifyScript : public BaseObject {
156158
ContextifyScript(Environment* env, v8::Local<v8::Object> object);
157159
~ContextifyScript() override;
158160

159-
static void Init(Environment* env, v8::Local<v8::Object> target);
161+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
162+
v8::Local<v8::ObjectTemplate> target);
160163
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
161164
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
162165
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);

‎src/node_file.cc

+76-80
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ using v8::Object;
7474
using v8::ObjectTemplate;
7575
using v8::Promise;
7676
using v8::String;
77-
using v8::Symbol;
7877
using v8::Undefined;
7978
using v8::Value;
8079

@@ -2827,121 +2826,117 @@ InternalFieldInfoBase* BindingData::Serialize(int index) {
28272826
return info;
28282827
}
28292828

2830-
void Initialize(Local<Object> target,
2831-
Local<Value> unused,
2832-
Local<Context> context,
2833-
void* priv) {
2834-
Realm* realm = Realm::GetCurrent(context);
2835-
Environment* env = realm->env();
2836-
Isolate* isolate = env->isolate();
2837-
BindingData* const binding_data =
2838-
realm->AddBindingData<BindingData>(context, target);
2839-
if (binding_data == nullptr) return;
2840-
2841-
SetMethod(context, target, "access", Access);
2842-
SetMethod(context, target, "close", Close);
2843-
SetMethod(context, target, "open", Open);
2844-
SetMethod(context, target, "openFileHandle", OpenFileHandle);
2845-
SetMethod(context, target, "read", Read);
2846-
SetMethod(context, target, "readBuffers", ReadBuffers);
2847-
SetMethod(context, target, "fdatasync", Fdatasync);
2848-
SetMethod(context, target, "fsync", Fsync);
2849-
SetMethod(context, target, "rename", Rename);
2850-
SetMethod(context, target, "ftruncate", FTruncate);
2851-
SetMethod(context, target, "rmdir", RMDir);
2852-
SetMethod(context, target, "mkdir", MKDir);
2853-
SetMethod(context, target, "readdir", ReadDir);
2854-
SetMethod(context, target, "internalModuleReadJSON", InternalModuleReadJSON);
2855-
SetMethod(context, target, "internalModuleStat", InternalModuleStat);
2856-
SetMethod(context, target, "stat", Stat);
2857-
SetMethod(context, target, "lstat", LStat);
2858-
SetMethod(context, target, "fstat", FStat);
2859-
SetMethod(context, target, "statfs", StatFs);
2860-
SetMethod(context, target, "link", Link);
2861-
SetMethod(context, target, "symlink", Symlink);
2862-
SetMethod(context, target, "readlink", ReadLink);
2863-
SetMethod(context, target, "unlink", Unlink);
2864-
SetMethod(context, target, "writeBuffer", WriteBuffer);
2865-
SetMethod(context, target, "writeBuffers", WriteBuffers);
2866-
SetMethod(context, target, "writeString", WriteString);
2867-
SetMethod(context, target, "realpath", RealPath);
2868-
SetMethod(context, target, "copyFile", CopyFile);
2869-
2870-
SetMethod(context, target, "chmod", Chmod);
2871-
SetMethod(context, target, "fchmod", FChmod);
2872-
2873-
SetMethod(context, target, "chown", Chown);
2874-
SetMethod(context, target, "fchown", FChown);
2875-
SetMethod(context, target, "lchown", LChown);
2876-
2877-
SetMethod(context, target, "utimes", UTimes);
2878-
SetMethod(context, target, "futimes", FUTimes);
2879-
SetMethod(context, target, "lutimes", LUTimes);
2880-
2881-
SetMethod(context, target, "mkdtemp", Mkdtemp);
2882-
2883-
target
2884-
->Set(context,
2885-
FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"),
2886-
Integer::New(
2887-
isolate,
2888-
static_cast<int32_t>(FsStatsOffset::kFsStatsFieldsNumber)))
2889-
.Check();
2890-
2891-
StatWatcher::Initialize(env, target);
2829+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
2830+
Local<FunctionTemplate> ctor) {
2831+
Isolate* isolate = isolate_data->isolate();
2832+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
2833+
2834+
SetMethod(isolate, target, "access", Access);
2835+
SetMethod(isolate, target, "close", Close);
2836+
SetMethod(isolate, target, "open", Open);
2837+
SetMethod(isolate, target, "openFileHandle", OpenFileHandle);
2838+
SetMethod(isolate, target, "read", Read);
2839+
SetMethod(isolate, target, "readBuffers", ReadBuffers);
2840+
SetMethod(isolate, target, "fdatasync", Fdatasync);
2841+
SetMethod(isolate, target, "fsync", Fsync);
2842+
SetMethod(isolate, target, "rename", Rename);
2843+
SetMethod(isolate, target, "ftruncate", FTruncate);
2844+
SetMethod(isolate, target, "rmdir", RMDir);
2845+
SetMethod(isolate, target, "mkdir", MKDir);
2846+
SetMethod(isolate, target, "readdir", ReadDir);
2847+
SetMethod(isolate, target, "internalModuleReadJSON", InternalModuleReadJSON);
2848+
SetMethod(isolate, target, "internalModuleStat", InternalModuleStat);
2849+
SetMethod(isolate, target, "stat", Stat);
2850+
SetMethod(isolate, target, "lstat", LStat);
2851+
SetMethod(isolate, target, "fstat", FStat);
2852+
SetMethod(isolate, target, "statfs", StatFs);
2853+
SetMethod(isolate, target, "link", Link);
2854+
SetMethod(isolate, target, "symlink", Symlink);
2855+
SetMethod(isolate, target, "readlink", ReadLink);
2856+
SetMethod(isolate, target, "unlink", Unlink);
2857+
SetMethod(isolate, target, "writeBuffer", WriteBuffer);
2858+
SetMethod(isolate, target, "writeBuffers", WriteBuffers);
2859+
SetMethod(isolate, target, "writeString", WriteString);
2860+
SetMethod(isolate, target, "realpath", RealPath);
2861+
SetMethod(isolate, target, "copyFile", CopyFile);
2862+
2863+
SetMethod(isolate, target, "chmod", Chmod);
2864+
SetMethod(isolate, target, "fchmod", FChmod);
2865+
2866+
SetMethod(isolate, target, "chown", Chown);
2867+
SetMethod(isolate, target, "fchown", FChown);
2868+
SetMethod(isolate, target, "lchown", LChown);
2869+
2870+
SetMethod(isolate, target, "utimes", UTimes);
2871+
SetMethod(isolate, target, "futimes", FUTimes);
2872+
SetMethod(isolate, target, "lutimes", LUTimes);
2873+
2874+
SetMethod(isolate, target, "mkdtemp", Mkdtemp);
2875+
2876+
StatWatcher::CreatePerIsolateProperties(isolate_data, ctor);
2877+
2878+
target->Set(
2879+
FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"),
2880+
Integer::New(isolate,
2881+
static_cast<int32_t>(FsStatsOffset::kFsStatsFieldsNumber)));
28922882

28932883
// Create FunctionTemplate for FSReqCallback
28942884
Local<FunctionTemplate> fst = NewFunctionTemplate(isolate, NewFSReqCallback);
28952885
fst->InstanceTemplate()->SetInternalFieldCount(
28962886
FSReqBase::kInternalFieldCount);
2897-
fst->Inherit(AsyncWrap::GetConstructorTemplate(env));
2898-
SetConstructorFunction(context, target, "FSReqCallback", fst);
2887+
fst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
2888+
SetConstructorFunction(isolate, target, "FSReqCallback", fst);
28992889

29002890
// Create FunctionTemplate for FileHandleReadWrap. There’s no need
29012891
// to do anything in the constructor, so we only store the instance template.
29022892
Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate);
29032893
fh_rw->InstanceTemplate()->SetInternalFieldCount(
29042894
FSReqBase::kInternalFieldCount);
2905-
fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2895+
fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
29062896
Local<String> fhWrapString =
29072897
FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap");
29082898
fh_rw->SetClassName(fhWrapString);
2909-
env->set_filehandlereadwrap_template(
2910-
fst->InstanceTemplate());
2899+
isolate_data->set_filehandlereadwrap_template(fst->InstanceTemplate());
29112900

29122901
// Create Function Template for FSReqPromise
29132902
Local<FunctionTemplate> fpt = FunctionTemplate::New(isolate);
2914-
fpt->Inherit(AsyncWrap::GetConstructorTemplate(env));
2903+
fpt->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
29152904
Local<String> promiseString =
29162905
FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise");
29172906
fpt->SetClassName(promiseString);
29182907
Local<ObjectTemplate> fpo = fpt->InstanceTemplate();
29192908
fpo->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
2920-
env->set_fsreqpromise_constructor_template(fpo);
2909+
isolate_data->set_fsreqpromise_constructor_template(fpo);
29212910

29222911
// Create FunctionTemplate for FileHandle
29232912
Local<FunctionTemplate> fd = NewFunctionTemplate(isolate, FileHandle::New);
2924-
fd->Inherit(AsyncWrap::GetConstructorTemplate(env));
2913+
fd->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
29252914
SetProtoMethod(isolate, fd, "close", FileHandle::Close);
29262915
SetProtoMethod(isolate, fd, "releaseFD", FileHandle::ReleaseFD);
29272916
Local<ObjectTemplate> fdt = fd->InstanceTemplate();
29282917
fdt->SetInternalFieldCount(FileHandle::kInternalFieldCount);
2929-
StreamBase::AddMethods(env, fd);
2930-
SetConstructorFunction(context, target, "FileHandle", fd);
2931-
env->set_fd_constructor_template(fdt);
2918+
StreamBase::AddMethods(isolate_data, fd);
2919+
SetConstructorFunction(isolate, target, "FileHandle", fd);
2920+
isolate_data->set_fd_constructor_template(fdt);
29322921

29332922
// Create FunctionTemplate for FileHandle::CloseReq
29342923
Local<FunctionTemplate> fdclose = FunctionTemplate::New(isolate);
29352924
fdclose->SetClassName(FIXED_ONE_BYTE_STRING(isolate,
29362925
"FileHandleCloseReq"));
2937-
fdclose->Inherit(AsyncWrap::GetConstructorTemplate(env));
2926+
fdclose->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data));
29382927
Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate();
29392928
fdcloset->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
2940-
env->set_fdclose_constructor_template(fdcloset);
2929+
isolate_data->set_fdclose_constructor_template(fdcloset);
29412930

2942-
target->Set(context,
2943-
FIXED_ONE_BYTE_STRING(isolate, "kUsePromises"),
2944-
env->fs_use_promises_symbol()).Check();
2931+
target->Set(isolate, "kUsePromises", isolate_data->fs_use_promises_symbol());
2932+
}
2933+
2934+
static void CreatePerContextProperties(Local<Object> target,
2935+
Local<Value> unused,
2936+
Local<Context> context,
2937+
void* priv) {
2938+
Realm* realm = Realm::GetCurrent(context);
2939+
realm->AddBindingData<BindingData>(context, target);
29452940
}
29462941

29472942
BindingData* FSReqBase::binding_data() {
@@ -3004,5 +2999,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
30042999

30053000
} // end namespace node
30063001

3007-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(fs, node::fs::Initialize)
3002+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(fs, node::fs::CreatePerContextProperties)
3003+
NODE_BINDING_PER_ISOLATE_INIT(fs, node::fs::CreatePerIsolateProperties)
30083004
NODE_BINDING_EXTERNAL_REFERENCE(fs, node::fs::RegisterExternalReferences)

‎src/node_process.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void PatchProcessObject(const v8::FunctionCallbackInfo<v8::Value>& args);
4848
namespace process {
4949
class BindingData : public SnapshotableObject {
5050
public:
51-
void AddMethods();
51+
static void AddMethods(v8::Isolate* isolate,
52+
v8::Local<v8::ObjectTemplate> target);
5253
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
5354

5455
using InternalFieldInfo = InternalFieldInfoBase;

‎src/node_process_methods.cc

+47-41
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using v8::CFunction;
4141
using v8::Context;
4242
using v8::Float64Array;
4343
using v8::FunctionCallbackInfo;
44+
using v8::FunctionTemplate;
4445
using v8::HeapStatistics;
4546
using v8::Integer;
4647
using v8::Isolate;
@@ -49,6 +50,7 @@ using v8::Maybe;
4950
using v8::NewStringType;
5051
using v8::Number;
5152
using v8::Object;
53+
using v8::ObjectTemplate;
5254
using v8::String;
5355
using v8::Uint32;
5456
using v8::Value;
@@ -478,11 +480,11 @@ BindingData::BindingData(Realm* realm, v8::Local<v8::Object> object)
478480
v8::CFunction BindingData::fast_number_(v8::CFunction::Make(FastNumber));
479481
v8::CFunction BindingData::fast_bigint_(v8::CFunction::Make(FastBigInt));
480482

481-
void BindingData::AddMethods() {
482-
Local<Context> ctx = env()->context();
483-
SetFastMethodNoSideEffect(ctx, object(), "hrtime", SlowNumber, &fast_number_);
483+
void BindingData::AddMethods(Isolate* isolate, Local<ObjectTemplate> target) {
484484
SetFastMethodNoSideEffect(
485-
ctx, object(), "hrtimeBigInt", SlowBigInt, &fast_bigint_);
485+
isolate, target, "hrtime", SlowNumber, &fast_number_);
486+
SetFastMethodNoSideEffect(
487+
isolate, target, "hrtimeBigInt", SlowBigInt, &fast_bigint_);
486488
}
487489

488490
void BindingData::RegisterExternalReferences(
@@ -569,44 +571,45 @@ void BindingData::Deserialize(Local<Context> context,
569571
CHECK_NOT_NULL(binding);
570572
}
571573

572-
static void Initialize(Local<Object> target,
573-
Local<Value> unused,
574-
Local<Context> context,
575-
void* priv) {
576-
Realm* realm = Realm::GetCurrent(context);
577-
Environment* env = realm->env();
578-
BindingData* const binding_data =
579-
realm->AddBindingData<BindingData>(context, target);
580-
if (binding_data == nullptr) return;
581-
binding_data->AddMethods();
574+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
575+
Local<FunctionTemplate> ctor) {
576+
Isolate* isolate = isolate_data->isolate();
577+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
582578

579+
BindingData::AddMethods(isolate, target);
583580
// define various internal methods
584-
if (env->owns_process_state()) {
585-
SetMethod(context, target, "_debugProcess", DebugProcess);
586-
SetMethod(context, target, "abort", Abort);
587-
SetMethod(context, target, "causeSegfault", CauseSegfault);
588-
SetMethod(context, target, "chdir", Chdir);
589-
}
590-
591-
SetMethod(context, target, "umask", Umask);
592-
SetMethod(context, target, "memoryUsage", MemoryUsage);
593-
SetMethod(context, target, "constrainedMemory", GetConstrainedMemory);
594-
SetMethod(context, target, "rss", Rss);
595-
SetMethod(context, target, "cpuUsage", CPUUsage);
596-
SetMethod(context, target, "resourceUsage", ResourceUsage);
597-
598-
SetMethod(context, target, "_debugEnd", DebugEnd);
599-
SetMethod(context, target, "_getActiveRequests", GetActiveRequests);
600-
SetMethod(context, target, "_getActiveHandles", GetActiveHandles);
601-
SetMethod(context, target, "getActiveResourcesInfo", GetActiveResourcesInfo);
602-
SetMethod(context, target, "_kill", Kill);
603-
SetMethod(context, target, "_rawDebug", RawDebug);
604-
605-
SetMethodNoSideEffect(context, target, "cwd", Cwd);
606-
SetMethod(context, target, "dlopen", binding::DLOpen);
607-
SetMethod(context, target, "reallyExit", ReallyExit);
608-
SetMethodNoSideEffect(context, target, "uptime", Uptime);
609-
SetMethod(context, target, "patchProcessObject", PatchProcessObject);
581+
SetMethod(isolate, target, "_debugProcess", DebugProcess);
582+
SetMethod(isolate, target, "abort", Abort);
583+
SetMethod(isolate, target, "causeSegfault", CauseSegfault);
584+
SetMethod(isolate, target, "chdir", Chdir);
585+
586+
SetMethod(isolate, target, "umask", Umask);
587+
SetMethod(isolate, target, "memoryUsage", MemoryUsage);
588+
SetMethod(isolate, target, "constrainedMemory", GetConstrainedMemory);
589+
SetMethod(isolate, target, "rss", Rss);
590+
SetMethod(isolate, target, "cpuUsage", CPUUsage);
591+
SetMethod(isolate, target, "resourceUsage", ResourceUsage);
592+
593+
SetMethod(isolate, target, "_debugEnd", DebugEnd);
594+
SetMethod(isolate, target, "_getActiveRequests", GetActiveRequests);
595+
SetMethod(isolate, target, "_getActiveHandles", GetActiveHandles);
596+
SetMethod(isolate, target, "getActiveResourcesInfo", GetActiveResourcesInfo);
597+
SetMethod(isolate, target, "_kill", Kill);
598+
SetMethod(isolate, target, "_rawDebug", RawDebug);
599+
600+
SetMethodNoSideEffect(isolate, target, "cwd", Cwd);
601+
SetMethod(isolate, target, "dlopen", binding::DLOpen);
602+
SetMethod(isolate, target, "reallyExit", ReallyExit);
603+
SetMethodNoSideEffect(isolate, target, "uptime", Uptime);
604+
SetMethod(isolate, target, "patchProcessObject", PatchProcessObject);
605+
}
606+
607+
static void CreatePerContextProperties(Local<Object> target,
608+
Local<Value> unused,
609+
Local<Context> context,
610+
void* priv) {
611+
Realm* realm = Realm::GetCurrent(context);
612+
realm->AddBindingData<BindingData>(context, target);
610613
}
611614

612615
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
@@ -641,6 +644,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
641644
} // namespace process
642645
} // namespace node
643646

644-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(process_methods, node::process::Initialize)
647+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(process_methods,
648+
node::process::CreatePerContextProperties)
649+
NODE_BINDING_PER_ISOLATE_INIT(process_methods,
650+
node::process::CreatePerIsolateProperties)
645651
NODE_BINDING_EXTERNAL_REFERENCE(process_methods,
646652
node::process::RegisterExternalReferences)

‎src/node_stat_watcher.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ using v8::Integer;
4040
using v8::Isolate;
4141
using v8::Local;
4242
using v8::Object;
43+
using v8::ObjectTemplate;
4344
using v8::Uint32;
4445
using v8::Value;
4546

46-
void StatWatcher::Initialize(Environment* env, Local<Object> target) {
47-
Isolate* isolate = env->isolate();
48-
HandleScope scope(env->isolate());
47+
void StatWatcher::CreatePerIsolateProperties(IsolateData* isolate_data,
48+
Local<FunctionTemplate> ctor) {
49+
Isolate* isolate = isolate_data->isolate();
50+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
4951

5052
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, StatWatcher::New);
5153
t->InstanceTemplate()->SetInternalFieldCount(
5254
StatWatcher::kInternalFieldCount);
53-
t->Inherit(HandleWrap::GetConstructorTemplate(env));
54-
55+
t->Inherit(HandleWrap::GetConstructorTemplate(isolate_data));
5556
SetProtoMethod(isolate, t, "start", StatWatcher::Start);
5657

57-
SetConstructorFunction(env->context(), target, "StatWatcher", t);
58+
SetConstructorFunction(isolate, target, "StatWatcher", t);
5859
}
5960

6061
void StatWatcher::RegisterExternalReferences(

‎src/node_stat_watcher.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class ExternalReferenceRegistry;
3939

4040
class StatWatcher : public HandleWrap {
4141
public:
42-
static void Initialize(Environment* env, v8::Local<v8::Object> target);
42+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
43+
v8::Local<v8::FunctionTemplate> ctor);
4344
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
4445

4546
protected:

‎src/node_url.cc

+24-16
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ using v8::CFunction;
1919
using v8::Context;
2020
using v8::FastOneByteString;
2121
using v8::FunctionCallbackInfo;
22+
using v8::FunctionTemplate;
2223
using v8::HandleScope;
2324
using v8::Isolate;
2425
using v8::Local;
2526
using v8::NewStringType;
2627
using v8::Object;
28+
using v8::ObjectTemplate;
2729
using v8::String;
2830
using v8::Value;
2931

@@ -322,22 +324,25 @@ void BindingData::UpdateComponents(const ada::url_components& components,
322324
"kURLComponentsLength should be up-to-date");
323325
}
324326

325-
void BindingData::Initialize(Local<Object> target,
326-
Local<Value> unused,
327-
Local<Context> context,
328-
void* priv) {
329-
Realm* realm = Realm::GetCurrent(context);
330-
BindingData* const binding_data =
331-
realm->AddBindingData<BindingData>(context, target);
332-
if (binding_data == nullptr) return;
333-
334-
SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII);
335-
SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode);
336-
SetMethodNoSideEffect(context, target, "format", Format);
337-
SetMethod(context, target, "parse", Parse);
338-
SetMethod(context, target, "update", Update);
327+
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
328+
Local<FunctionTemplate> ctor) {
329+
Isolate* isolate = isolate_data->isolate();
330+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
331+
SetMethodNoSideEffect(isolate, target, "domainToASCII", DomainToASCII);
332+
SetMethodNoSideEffect(isolate, target, "domainToUnicode", DomainToUnicode);
333+
SetMethodNoSideEffect(isolate, target, "format", Format);
334+
SetMethod(isolate, target, "parse", Parse);
335+
SetMethod(isolate, target, "update", Update);
339336
SetFastMethodNoSideEffect(
340-
context, target, "canParse", CanParse, &fast_can_parse_);
337+
isolate, target, "canParse", CanParse, &fast_can_parse_);
338+
}
339+
340+
void BindingData::CreatePerContextProperties(Local<Object> target,
341+
Local<Value> unused,
342+
Local<Context> context,
343+
void* priv) {
344+
Realm* realm = Realm::GetCurrent(context);
345+
realm->AddBindingData<BindingData>(context, target);
341346
}
342347

343348
void BindingData::RegisterExternalReferences(
@@ -365,6 +370,9 @@ std::string FromFilePath(const std::string_view file_path) {
365370

366371
} // namespace node
367372

368-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(url, node::url::BindingData::Initialize)
373+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
374+
url, node::url::BindingData::CreatePerContextProperties)
375+
NODE_BINDING_PER_ISOLATE_INIT(
376+
url, node::url::BindingData::CreatePerIsolateProperties)
369377
NODE_BINDING_EXTERNAL_REFERENCE(
370378
url, node::url::BindingData::RegisterExternalReferences)

‎src/node_url.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ class BindingData : public SnapshotableObject {
5656
static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
5757
static void Update(const v8::FunctionCallbackInfo<v8::Value>& args);
5858

59-
static void Initialize(v8::Local<v8::Object> target,
60-
v8::Local<v8::Value> unused,
61-
v8::Local<v8::Context> context,
62-
void* priv);
59+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
60+
v8::Local<v8::FunctionTemplate> ctor);
61+
static void CreatePerContextProperties(v8::Local<v8::Object> target,
62+
v8::Local<v8::Value> unused,
63+
v8::Local<v8::Context> context,
64+
void* priv);
6365
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
6466

6567
private:

‎src/stream_base.cc

+26-9
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,12 @@ Local<Object> StreamBase::GetObject() {
491491
return GetAsyncWrap()->object();
492492
}
493493

494-
void StreamBase::AddMethod(Environment* env,
494+
void StreamBase::AddMethod(Isolate* isolate,
495495
Local<Signature> signature,
496496
enum PropertyAttribute attributes,
497497
Local<FunctionTemplate> t,
498498
JSMethodFunction* stream_method,
499499
Local<String> string) {
500-
Isolate* isolate = env->isolate();
501500
Local<FunctionTemplate> templ =
502501
NewFunctionTemplate(isolate,
503502
stream_method,
@@ -509,19 +508,37 @@ void StreamBase::AddMethod(Environment* env,
509508
}
510509

511510
void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
512-
Isolate* isolate = env->isolate();
511+
AddMethods(env->isolate_data(), t);
512+
}
513+
514+
void StreamBase::AddMethods(IsolateData* isolate_data,
515+
Local<FunctionTemplate> t) {
516+
Isolate* isolate = isolate_data->isolate();
513517
HandleScope scope(isolate);
514518

515519
enum PropertyAttribute attributes =
516520
static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum);
517521
Local<Signature> sig = Signature::New(isolate, t);
518522

519-
AddMethod(env, sig, attributes, t, GetFD, env->fd_string());
520-
AddMethod(
521-
env, sig, attributes, t, GetExternal, env->external_stream_string());
522-
AddMethod(env, sig, attributes, t, GetBytesRead, env->bytes_read_string());
523-
AddMethod(
524-
env, sig, attributes, t, GetBytesWritten, env->bytes_written_string());
523+
AddMethod(isolate, sig, attributes, t, GetFD, isolate_data->fd_string());
524+
AddMethod(isolate,
525+
sig,
526+
attributes,
527+
t,
528+
GetExternal,
529+
isolate_data->external_stream_string());
530+
AddMethod(isolate,
531+
sig,
532+
attributes,
533+
t,
534+
GetBytesRead,
535+
isolate_data->bytes_read_string());
536+
AddMethod(isolate,
537+
sig,
538+
attributes,
539+
t,
540+
GetBytesWritten,
541+
isolate_data->bytes_written_string());
525542
SetProtoMethod(isolate, t, "readStart", JSMethod<&StreamBase::ReadStartJS>);
526543
SetProtoMethod(isolate, t, "readStop", JSMethod<&StreamBase::ReadStopJS>);
527544
SetProtoMethod(isolate, t, "shutdown", JSMethod<&StreamBase::Shutdown>);

‎src/stream_base.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ class StreamBase : public StreamResource {
313313
kInternalFieldCount
314314
};
315315

316+
static void AddMethods(IsolateData* isolate_data,
317+
v8::Local<v8::FunctionTemplate> target);
316318
static void AddMethods(Environment* env,
317319
v8::Local<v8::FunctionTemplate> target);
318320
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
@@ -409,7 +411,7 @@ class StreamBase : public StreamResource {
409411
EmitToJSStreamListener default_listener_;
410412

411413
void SetWriteResult(const StreamWriteResult& res);
412-
static void AddMethod(Environment* env,
414+
static void AddMethod(v8::Isolate* isolate,
413415
v8::Local<v8::Signature> sig,
414416
enum v8::PropertyAttribute attributes,
415417
v8::Local<v8::FunctionTemplate> t,

‎src/timers.cc

+27-16
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ namespace timers {
1212
using v8::Context;
1313
using v8::Function;
1414
using v8::FunctionCallbackInfo;
15+
using v8::FunctionTemplate;
16+
using v8::Isolate;
1517
using v8::Local;
1618
using v8::Number;
1719
using v8::Object;
20+
using v8::ObjectTemplate;
1821
using v8::Value;
1922

2023
void BindingData::SetupTimers(const FunctionCallbackInfo<Value>& args) {
@@ -119,34 +122,40 @@ v8::CFunction BindingData::fast_toggle_timer_ref_(
119122
v8::CFunction BindingData::fast_toggle_immediate_ref_(
120123
v8::CFunction::Make(FastToggleImmediateRef));
121124

122-
void BindingData::Initialize(Local<Object> target,
123-
Local<Value> unused,
124-
Local<Context> context,
125-
void* priv) {
126-
Realm* realm = Realm::GetCurrent(context);
127-
Environment* env = realm->env();
128-
BindingData* const binding_data =
129-
realm->AddBindingData<BindingData>(context, target);
130-
if (binding_data == nullptr) return;
125+
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
126+
Local<FunctionTemplate> ctor) {
127+
Isolate* isolate = isolate_data->isolate();
128+
Local<ObjectTemplate> target = ctor->InstanceTemplate();
131129

132-
SetMethod(context, target, "setupTimers", SetupTimers);
130+
SetMethod(isolate, target, "setupTimers", SetupTimers);
133131
SetFastMethod(
134-
context, target, "getLibuvNow", SlowGetLibuvNow, &fast_get_libuv_now_);
135-
SetFastMethod(context,
132+
isolate, target, "getLibuvNow", SlowGetLibuvNow, &fast_get_libuv_now_);
133+
SetFastMethod(isolate,
136134
target,
137135
"scheduleTimer",
138136
SlowScheduleTimer,
139137
&fast_schedule_timers_);
140-
SetFastMethod(context,
138+
SetFastMethod(isolate,
141139
target,
142140
"toggleTimerRef",
143141
SlowToggleTimerRef,
144142
&fast_toggle_timer_ref_);
145-
SetFastMethod(context,
143+
SetFastMethod(isolate,
146144
target,
147145
"toggleImmediateRef",
148146
SlowToggleImmediateRef,
149147
&fast_toggle_immediate_ref_);
148+
}
149+
150+
void BindingData::CreatePerContextProperties(Local<Object> target,
151+
Local<Value> unused,
152+
Local<Context> context,
153+
void* priv) {
154+
Realm* realm = Realm::GetCurrent(context);
155+
Environment* env = realm->env();
156+
BindingData* const binding_data =
157+
realm->AddBindingData<BindingData>(context, target);
158+
if (binding_data == nullptr) return;
150159

151160
// TODO(joyeecheung): move these into BindingData.
152161
target
@@ -187,7 +196,9 @@ void BindingData::RegisterTimerExternalReferences(
187196

188197
} // namespace node
189198

190-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(timers,
191-
node::timers::BindingData::Initialize)
199+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
200+
timers, node::timers::BindingData::CreatePerContextProperties)
201+
NODE_BINDING_PER_ISOLATE_INIT(
202+
timers, node::timers::BindingData::CreatePerIsolateProperties)
192203
NODE_BINDING_EXTERNAL_REFERENCE(
193204
timers, node::timers::BindingData::RegisterTimerExternalReferences)

‎src/timers.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ class BindingData : public SnapshotableObject {
4545
static void FastToggleImmediateRef(v8::Local<v8::Object> receiver, bool ref);
4646
static void ToggleImmediateRefImpl(BindingData* data, bool ref);
4747

48-
static void Initialize(v8::Local<v8::Object> target,
49-
v8::Local<v8::Value> unused,
50-
v8::Local<v8::Context> context,
51-
void* priv);
48+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
49+
v8::Local<v8::FunctionTemplate> ctor);
50+
static void CreatePerContextProperties(v8::Local<v8::Object> target,
51+
v8::Local<v8::Value> unused,
52+
v8::Local<v8::Context> context,
53+
void* priv);
5254
static void RegisterTimerExternalReferences(
5355
ExternalReferenceRegistry* registry);
5456

0 commit comments

Comments
 (0)
Please sign in to comment.