Skip to content

Commit c0365fd

Browse files
committed
src: avoid prototype access in binding templates
This patch makes the binding templates ObjectTemplates, since we don't actually need the constructor function. This also avoids setting the properties on prototype, and instead initializes them directly on the object template. Previously the initialization was similar to: ``` function Binding() {} Binding.prototype.property = ...; module.exports = new Binding; ``` Now it's similar to: ``` module.exports = { property: ... }; ``` PR-URL: #47913 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 21153a8 commit c0365fd

27 files changed

+98
-121
lines changed

src/async_wrap.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(
353353
}
354354

355355
void AsyncWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
356-
Local<FunctionTemplate> ctor) {
356+
Local<ObjectTemplate> target) {
357357
Isolate* isolate = isolate_data->isolate();
358-
Local<ObjectTemplate> target = ctor->InstanceTemplate();
359358

360359
SetMethod(isolate, target, "setupHooks", SetupHooks);
361360
SetMethod(isolate, target, "setCallbackTrampoline", SetCallbackTrampoline);

src/async_wrap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class AsyncWrap : public BaseObject {
151151
v8::Local<v8::Value> unused,
152152
v8::Local<v8::Context> context,
153153
void* priv);
154-
static void CreatePerIsolateProperties(
155-
IsolateData* isolate_data, v8::Local<v8::FunctionTemplate> target);
154+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
155+
v8::Local<v8::ObjectTemplate> target);
156156

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

src/encoding_binding.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ using v8::ArrayBuffer;
1616
using v8::BackingStore;
1717
using v8::Context;
1818
using v8::FunctionCallbackInfo;
19-
using v8::FunctionTemplate;
2019
using v8::Isolate;
2120
using v8::Local;
2221
using v8::MaybeLocal;
@@ -219,9 +218,8 @@ void BindingData::ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args) {
219218
}
220219

221220
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
222-
Local<FunctionTemplate> ctor) {
221+
Local<ObjectTemplate> target) {
223222
Isolate* isolate = isolate_data->isolate();
224-
Local<ObjectTemplate> target = ctor->InstanceTemplate();
225223
SetMethod(isolate, target, "encodeInto", EncodeInto);
226224
SetMethodNoSideEffect(isolate, target, "encodeUtf8String", EncodeUtf8String);
227225
SetMethodNoSideEffect(isolate, target, "decodeUTF8", DecodeUTF8);

src/encoding_binding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class BindingData : public SnapshotableObject {
3636
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
3737

3838
static void CreatePerIsolateProperties(IsolateData* isolate_data,
39-
v8::Local<v8::FunctionTemplate> ctor);
39+
v8::Local<v8::ObjectTemplate> target);
4040
static void CreatePerContextProperties(v8::Local<v8::Object> target,
4141
v8::Local<v8::Value> unused,
4242
v8::Local<v8::Context> context,

src/env-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ void Environment::set_process_exit_handler(
807807
#undef VY
808808
#undef VP
809809

810-
#define VM(PropertyName) V(PropertyName##_binding, v8::FunctionTemplate)
810+
#define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
811811
#define V(PropertyName, TypeName) \
812812
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
813813
return PropertyName##_.Get(isolate_); \

src/env.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ using v8::Context;
3838
using v8::EmbedderGraph;
3939
using v8::EscapableHandleScope;
4040
using v8::Function;
41-
using v8::FunctionTemplate;
4241
using v8::HandleScope;
4342
using v8::HeapProfiler;
4443
using v8::HeapSpaceStatistics;
@@ -49,6 +48,7 @@ using v8::MaybeLocal;
4948
using v8::NewStringType;
5049
using v8::Number;
5150
using v8::Object;
51+
using v8::ObjectTemplate;
5252
using v8::Private;
5353
using v8::Script;
5454
using v8::SnapshotCreator;
@@ -326,7 +326,7 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
326326
info.primitive_values.push_back(creator->AddData(async_wrap_provider(i)));
327327

328328
uint32_t id = 0;
329-
#define VM(PropertyName) V(PropertyName##_binding, FunctionTemplate)
329+
#define VM(PropertyName) V(PropertyName##_binding_template, ObjectTemplate)
330330
#define V(PropertyName, TypeName) \
331331
do { \
332332
Local<TypeName> field = PropertyName(); \
@@ -390,7 +390,7 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
390390
const std::vector<PropInfo>& values = info->template_values;
391391
i = 0; // index to the array
392392
uint32_t id = 0;
393-
#define VM(PropertyName) V(PropertyName##_binding, FunctionTemplate)
393+
#define VM(PropertyName) V(PropertyName##_binding_template, ObjectTemplate)
394394
#define V(PropertyName, TypeName) \
395395
do { \
396396
if (values.size() > i && id == values[i].id) { \
@@ -491,10 +491,9 @@ void IsolateData::CreateProperties() {
491491
NODE_ASYNC_PROVIDER_TYPES(V)
492492
#undef V
493493

494-
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
495-
templ->InstanceTemplate()->SetInternalFieldCount(
496-
BaseObject::kInternalFieldCount);
497-
set_binding_data_ctor_template(templ);
494+
Local<ObjectTemplate> templ = ObjectTemplate::New(isolate());
495+
templ->SetInternalFieldCount(BaseObject::kInternalFieldCount);
496+
set_binding_data_default_template(templ);
498497
binding::CreateInternalBindingTemplates(this);
499498

500499
contextify::ContextifyContext::InitializeGlobalTemplates(this);

src/env.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
166166
#undef VS
167167
#undef VP
168168

169-
#define VM(PropertyName) V(PropertyName##_binding, v8::FunctionTemplate)
169+
#define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
170170
#define V(PropertyName, TypeName) \
171171
inline v8::Local<TypeName> PropertyName() const; \
172172
inline void set_##PropertyName(v8::Local<TypeName> value);
@@ -194,7 +194,7 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
194194
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
195195
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
196196
#define VR(PropertyName, TypeName) V(v8::Private, per_realm_##PropertyName)
197-
#define VM(PropertyName) V(v8::FunctionTemplate, PropertyName##_binding)
197+
#define VM(PropertyName) V(v8::ObjectTemplate, PropertyName##_binding_template)
198198
#define VT(PropertyName, TypeName) V(TypeName, PropertyName)
199199
#define V(TypeName, PropertyName) \
200200
v8::Eternal<TypeName> PropertyName ## _;

src/env_properties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
#define PER_ISOLATE_TEMPLATE_PROPERTIES(V) \
334334
V(async_wrap_ctor_template, v8::FunctionTemplate) \
335335
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
336-
V(binding_data_ctor_template, v8::FunctionTemplate) \
336+
V(binding_data_default_template, v8::ObjectTemplate) \
337337
V(blob_constructor_template, v8::FunctionTemplate) \
338338
V(blob_reader_constructor_template, v8::FunctionTemplate) \
339339
V(blocklist_constructor_template, v8::FunctionTemplate) \

src/node_binding.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ NODE_BUILTIN_BINDINGS(V)
101101

102102
#define V(modname) \
103103
void _register_isolate_##modname(node::IsolateData* isolate_data, \
104-
v8::Local<v8::FunctionTemplate> target);
104+
v8::Local<v8::ObjectTemplate> target);
105105
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
106106
#undef V
107107

@@ -235,11 +235,11 @@ using v8::Context;
235235
using v8::EscapableHandleScope;
236236
using v8::Exception;
237237
using v8::FunctionCallbackInfo;
238-
using v8::FunctionTemplate;
239238
using v8::HandleScope;
240239
using v8::Isolate;
241240
using v8::Local;
242241
using v8::Object;
242+
using v8::ObjectTemplate;
243243
using v8::String;
244244
using v8::Value;
245245

@@ -572,12 +572,11 @@ inline struct node_module* FindModule(struct node_module* list,
572572
void CreateInternalBindingTemplates(IsolateData* isolate_data) {
573573
#define V(modname) \
574574
do { \
575-
Local<FunctionTemplate> templ = \
576-
FunctionTemplate::New(isolate_data->isolate()); \
577-
templ->InstanceTemplate()->SetInternalFieldCount( \
578-
BaseObject::kInternalFieldCount); \
575+
Local<ObjectTemplate> templ = \
576+
ObjectTemplate::New(isolate_data->isolate()); \
577+
templ->SetInternalFieldCount(BaseObject::kInternalFieldCount); \
579578
_register_isolate_##modname(isolate_data, templ); \
580-
isolate_data->set_##modname##_binding(templ); \
579+
isolate_data->set_##modname##_binding_template(templ); \
581580
} while (0);
582581
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
583582
#undef V
@@ -586,21 +585,20 @@ void CreateInternalBindingTemplates(IsolateData* isolate_data) {
586585
static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data,
587586
const char* mod_name,
588587
Local<Context> context) {
589-
Local<FunctionTemplate> ctor;
588+
Local<ObjectTemplate> templ;
589+
590590
#define V(name) \
591591
if (strcmp(mod_name, #name) == 0) { \
592-
ctor = isolate_data->name##_binding(); \
592+
templ = isolate_data->name##_binding_template(); \
593593
} else // NOLINT(readability/braces)
594594
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
595595
#undef V
596596
{
597-
ctor = isolate_data->binding_data_ctor_template();
597+
// Default template.
598+
templ = isolate_data->binding_data_default_template();
598599
}
599600

600-
Local<Object> obj = ctor->GetFunction(context)
601-
.ToLocalChecked()
602-
->NewInstance(context)
603-
.ToLocalChecked();
601+
Local<Object> obj = templ->NewInstance(context).ToLocalChecked();
604602
return obj;
605603
}
606604

src/node_binding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace node {
8484
// list.
8585
#define NODE_BINDING_PER_ISOLATE_INIT(modname, per_isolate_func) \
8686
void _register_isolate_##modname(node::IsolateData* isolate_data, \
87-
v8::Local<v8::FunctionTemplate> target) { \
87+
v8::Local<v8::ObjectTemplate> target) { \
8888
per_isolate_func(isolate_data, target); \
8989
}
9090

src/node_blob.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
109109
} // namespace
110110

111111
void Blob::CreatePerIsolateProperties(IsolateData* isolate_data,
112-
Local<FunctionTemplate> ctor) {
112+
Local<ObjectTemplate> target) {
113113
Isolate* isolate = isolate_data->isolate();
114-
Local<ObjectTemplate> target = ctor->InstanceTemplate();
115114

116115
SetMethod(isolate, target, "createBlob", New);
117116
SetMethod(isolate, target, "storeDataObject", StoreDataObject);

src/node_blob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Blob : public BaseObject {
2727
ExternalReferenceRegistry* registry);
2828

2929
static void CreatePerIsolateProperties(IsolateData* isolate_data,
30-
v8::Local<v8::FunctionTemplate> ctor);
30+
v8::Local<v8::ObjectTemplate> target);
3131
static void CreatePerContextProperties(v8::Local<v8::Object> target,
3232
v8::Local<v8::Value> unused,
3333
v8::Local<v8::Context> context,

src/node_builtins.cc

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ using v8::DEFAULT;
1515
using v8::EscapableHandleScope;
1616
using v8::Function;
1717
using v8::FunctionCallbackInfo;
18-
using v8::FunctionTemplate;
1918
using v8::IntegrityLevel;
2019
using v8::Isolate;
2120
using v8::Local;
@@ -663,38 +662,37 @@ void BuiltinLoader::CopySourceAndCodeCacheReferenceFrom(
663662
}
664663

665664
void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data,
666-
Local<FunctionTemplate> target) {
665+
Local<ObjectTemplate> target) {
667666
Isolate* isolate = isolate_data->isolate();
668-
Local<ObjectTemplate> proto = target->PrototypeTemplate();
669-
670-
proto->SetAccessor(isolate_data->config_string(),
671-
ConfigStringGetter,
672-
nullptr,
673-
Local<Value>(),
674-
DEFAULT,
675-
None,
676-
SideEffectType::kHasNoSideEffect);
677-
678-
proto->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
679-
BuiltinIdsGetter,
680-
nullptr,
681-
Local<Value>(),
682-
DEFAULT,
683-
None,
684-
SideEffectType::kHasNoSideEffect);
685-
686-
proto->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
687-
GetBuiltinCategories,
688-
nullptr,
689-
Local<Value>(),
690-
DEFAULT,
691-
None,
692-
SideEffectType::kHasNoSideEffect);
693-
694-
SetMethod(isolate, proto, "getCacheUsage", BuiltinLoader::GetCacheUsage);
695-
SetMethod(isolate, proto, "compileFunction", BuiltinLoader::CompileFunction);
696-
SetMethod(isolate, proto, "hasCachedBuiltins", HasCachedBuiltins);
697-
SetMethod(isolate, proto, "setInternalLoaders", SetInternalLoaders);
667+
668+
target->SetAccessor(isolate_data->config_string(),
669+
ConfigStringGetter,
670+
nullptr,
671+
Local<Value>(),
672+
DEFAULT,
673+
None,
674+
SideEffectType::kHasNoSideEffect);
675+
676+
target->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
677+
BuiltinIdsGetter,
678+
nullptr,
679+
Local<Value>(),
680+
DEFAULT,
681+
None,
682+
SideEffectType::kHasNoSideEffect);
683+
684+
target->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
685+
GetBuiltinCategories,
686+
nullptr,
687+
Local<Value>(),
688+
DEFAULT,
689+
None,
690+
SideEffectType::kHasNoSideEffect);
691+
692+
SetMethod(isolate, target, "getCacheUsage", BuiltinLoader::GetCacheUsage);
693+
SetMethod(isolate, target, "compileFunction", BuiltinLoader::CompileFunction);
694+
SetMethod(isolate, target, "hasCachedBuiltins", HasCachedBuiltins);
695+
SetMethod(isolate, target, "setInternalLoaders", SetInternalLoaders);
698696
}
699697

700698
void BuiltinLoader::CreatePerContextProperties(Local<Object> target,

src/node_builtins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
8484
BuiltinLoader& operator=(const BuiltinLoader&) = delete;
8585

8686
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
87-
static void CreatePerIsolateProperties(
88-
IsolateData* isolate_data, v8::Local<v8::FunctionTemplate> target);
87+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
88+
v8::Local<v8::ObjectTemplate> target);
8989
static void CreatePerContextProperties(v8::Local<v8::Object> target,
9090
v8::Local<v8::Value> unused,
9191
v8::Local<v8::Context> context,

src/node_contextify.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,9 +1393,9 @@ void MicrotaskQueueWrap::RegisterExternalReferences(
13931393
}
13941394

13951395
void CreatePerIsolateProperties(IsolateData* isolate_data,
1396-
Local<FunctionTemplate> ctor) {
1396+
Local<ObjectTemplate> target) {
13971397
Isolate* isolate = isolate_data->isolate();
1398-
Local<ObjectTemplate> target = ctor->InstanceTemplate();
1398+
13991399
ContextifyContext::CreatePerIsolateProperties(isolate_data, target);
14001400
ContextifyScript::CreatePerIsolateProperties(isolate_data, target);
14011401
MicrotaskQueueWrap::CreatePerIsolateProperties(isolate_data, target);

src/node_file.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,9 +2827,8 @@ InternalFieldInfoBase* BindingData::Serialize(int index) {
28272827
}
28282828

28292829
static void CreatePerIsolateProperties(IsolateData* isolate_data,
2830-
Local<FunctionTemplate> ctor) {
2830+
Local<ObjectTemplate> target) {
28312831
Isolate* isolate = isolate_data->isolate();
2832-
Local<ObjectTemplate> target = ctor->InstanceTemplate();
28332832

28342833
SetMethod(isolate, target, "access", Access);
28352834
SetMethod(isolate, target, "close", Close);
@@ -2873,7 +2872,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
28732872

28742873
SetMethod(isolate, target, "mkdtemp", Mkdtemp);
28752874

2876-
StatWatcher::CreatePerIsolateProperties(isolate_data, ctor);
2875+
StatWatcher::CreatePerIsolateProperties(isolate_data, target);
28772876

28782877
target->Set(
28792878
FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"),

src/node_i18n.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -860,17 +860,16 @@ static void GetStringWidth(const FunctionCallbackInfo<Value>& args) {
860860
}
861861

862862
static void CreatePerIsolateProperties(IsolateData* isolate_data,
863-
Local<FunctionTemplate> target) {
863+
Local<ObjectTemplate> target) {
864864
Isolate* isolate = isolate_data->isolate();
865-
Local<ObjectTemplate> proto = target->PrototypeTemplate();
866865

867-
SetMethod(isolate, proto, "toUnicode", ToUnicode);
868-
SetMethod(isolate, proto, "toASCII", ToASCII);
869-
SetMethod(isolate, proto, "getStringWidth", GetStringWidth);
866+
SetMethod(isolate, target, "toUnicode", ToUnicode);
867+
SetMethod(isolate, target, "toASCII", ToASCII);
868+
SetMethod(isolate, target, "getStringWidth", GetStringWidth);
870869

871870
// One-shot converters
872-
SetMethod(isolate, proto, "icuErrName", ICUErrorName);
873-
SetMethod(isolate, proto, "transcode", Transcode);
871+
SetMethod(isolate, target, "icuErrName", ICUErrorName);
872+
SetMethod(isolate, target, "transcode", Transcode);
874873

875874
// ConverterObject
876875
{
@@ -883,9 +882,9 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
883882
isolate_data->set_i18n_converter_template(t->InstanceTemplate());
884883
}
885884

886-
SetMethod(isolate, proto, "getConverter", ConverterObject::Create);
887-
SetMethod(isolate, proto, "decode", ConverterObject::Decode);
888-
SetMethod(isolate, proto, "hasConverter", ConverterObject::Has);
885+
SetMethod(isolate, target, "getConverter", ConverterObject::Create);
886+
SetMethod(isolate, target, "decode", ConverterObject::Decode);
887+
SetMethod(isolate, target, "hasConverter", ConverterObject::Has);
889888
}
890889

891890
void CreatePerContextProperties(Local<Object> target,

0 commit comments

Comments
 (0)