Skip to content

Commit 7c69ca5

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: move handle properties to prototype
Reduce the size of wrap objects by moving a couple of accessors from the instance template to the prototype template. They occupied one slot per instance instead of one slot per class. This commit fixes some instances of unwrapping twice since that code had to be updated anyway to use `args.This()` instead of `args.Holder()`. PR-URL: #16482 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent c87a620 commit 7c69ca5

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

src/stream_base-inl.h

+27-31
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ void StreamBase::AddMethods(Environment* env,
3030

3131
enum PropertyAttribute attributes =
3232
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
33-
t->InstanceTemplate()->SetAccessor(env->fd_string(),
34-
GetFD<Base>,
35-
nullptr,
36-
env->as_external(),
37-
v8::DEFAULT,
38-
attributes);
39-
40-
t->InstanceTemplate()->SetAccessor(env->external_stream_string(),
41-
GetExternal<Base>,
42-
nullptr,
43-
env->as_external(),
44-
v8::DEFAULT,
45-
attributes);
46-
47-
t->InstanceTemplate()->SetAccessor(env->bytes_read_string(),
48-
GetBytesRead<Base>,
49-
nullptr,
50-
env->as_external(),
51-
v8::DEFAULT,
52-
attributes);
33+
t->PrototypeTemplate()->SetAccessor(env->fd_string(),
34+
GetFD<Base>,
35+
nullptr,
36+
env->as_external(),
37+
v8::DEFAULT,
38+
attributes);
39+
40+
t->PrototypeTemplate()->SetAccessor(env->external_stream_string(),
41+
GetExternal<Base>,
42+
nullptr,
43+
env->as_external(),
44+
v8::DEFAULT,
45+
attributes);
46+
47+
t->PrototypeTemplate()->SetAccessor(env->bytes_read_string(),
48+
GetBytesRead<Base>,
49+
nullptr,
50+
env->as_external(),
51+
v8::DEFAULT,
52+
attributes);
5353

5454
env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
5555
env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
@@ -78,11 +78,10 @@ void StreamBase::AddMethods(Environment* env,
7878
template <class Base>
7979
void StreamBase::GetFD(Local<String> key,
8080
const PropertyCallbackInfo<Value>& args) {
81-
Base* handle = Unwrap<Base>(args.Holder());
82-
8381
// Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
82+
Base* handle;
8483
ASSIGN_OR_RETURN_UNWRAP(&handle,
85-
args.Holder(),
84+
args.This(),
8685
args.GetReturnValue().Set(UV_EINVAL));
8786

8887
StreamBase* wrap = static_cast<StreamBase*>(handle);
@@ -96,11 +95,10 @@ void StreamBase::GetFD(Local<String> key,
9695
template <class Base>
9796
void StreamBase::GetBytesRead(Local<String> key,
9897
const PropertyCallbackInfo<Value>& args) {
99-
Base* handle = Unwrap<Base>(args.Holder());
100-
10198
// The handle instance hasn't been set. So no bytes could have been read.
99+
Base* handle;
102100
ASSIGN_OR_RETURN_UNWRAP(&handle,
103-
args.Holder(),
101+
args.This(),
104102
args.GetReturnValue().Set(0));
105103

106104
StreamBase* wrap = static_cast<StreamBase*>(handle);
@@ -112,9 +110,8 @@ void StreamBase::GetBytesRead(Local<String> key,
112110
template <class Base>
113111
void StreamBase::GetExternal(Local<String> key,
114112
const PropertyCallbackInfo<Value>& args) {
115-
Base* handle = Unwrap<Base>(args.Holder());
116-
117-
ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
113+
Base* handle;
114+
ASSIGN_OR_RETURN_UNWRAP(&handle, args.This());
118115

119116
StreamBase* wrap = static_cast<StreamBase*>(handle);
120117
Local<External> ext = External::New(args.GetIsolate(), wrap);
@@ -125,8 +122,7 @@ void StreamBase::GetExternal(Local<String> key,
125122
template <class Base,
126123
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
127124
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
128-
Base* handle = Unwrap<Base>(args.Holder());
129-
125+
Base* handle;
130126
ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
131127

132128
StreamBase* wrap = static_cast<StreamBase*>(handle);

src/udp_wrap.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ void UDPWrap::Initialize(Local<Object> target,
7979

8080
enum PropertyAttribute attributes =
8181
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
82-
t->InstanceTemplate()->SetAccessor(env->fd_string(),
83-
UDPWrap::GetFD,
84-
nullptr,
85-
env->as_external(),
86-
v8::DEFAULT,
87-
attributes);
82+
t->PrototypeTemplate()->SetAccessor(env->fd_string(),
83+
UDPWrap::GetFD,
84+
nullptr,
85+
env->as_external(),
86+
v8::DEFAULT,
87+
attributes);
8888

8989
env->SetProtoMethod(t, "bind", Bind);
9090
env->SetProtoMethod(t, "send", Send);
@@ -137,7 +137,7 @@ void UDPWrap::New(const FunctionCallbackInfo<Value>& args) {
137137
void UDPWrap::GetFD(Local<String>, const PropertyCallbackInfo<Value>& args) {
138138
int fd = UV_EBADF;
139139
#if !defined(_WIN32)
140-
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder());
140+
UDPWrap* wrap = Unwrap<UDPWrap>(args.This());
141141
if (wrap != nullptr)
142142
uv_fileno(reinterpret_cast<uv_handle_t*>(&wrap->handle_), &fd);
143143
#endif

0 commit comments

Comments
 (0)