Skip to content

Commit a276ff9

Browse files
joyeecheungCeres6
authored andcommitted
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: nodejs#47913 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7c5ddf7 commit a276ff9

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

0 commit comments

Comments
 (0)