Skip to content

Commit 8cee5d6

Browse files
jasnellMylesBorins
authored andcommitted
src: reduce code duplication
Adds `AsyncWrap::AddWrapMethods()` to add common methods to a `Local<FunctionTemplate>`. Follows same pattern as stream base. PR-URL: #14937 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5a05dfe commit 8cee5d6

20 files changed

+43
-29
lines changed

src/async-wrap.cc

+7
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@ void AsyncWrap::QueueDestroyId(const FunctionCallbackInfo<Value>& args) {
475475
PushBackDestroyId(Environment::GetCurrent(args), args[0]->NumberValue());
476476
}
477477

478+
void AsyncWrap::AddWrapMethods(Environment* env,
479+
Local<FunctionTemplate> constructor,
480+
int flag) {
481+
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
482+
if (flag & kFlagHasReset)
483+
env->SetProtoMethod(constructor, "asyncReset", AsyncWrap::AsyncReset);
484+
}
478485

479486
void AsyncWrap::Initialize(Local<Object> target,
480487
Local<Value> unused,

src/async-wrap.h

+9
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,22 @@ class AsyncWrap : public BaseObject {
8888
PROVIDERS_LENGTH,
8989
};
9090

91+
enum Flags {
92+
kFlagNone = 0x0,
93+
kFlagHasReset = 0x1
94+
};
95+
9196
AsyncWrap(Environment* env,
9297
v8::Local<v8::Object> object,
9398
ProviderType provider,
9499
bool silent = false);
95100

96101
virtual ~AsyncWrap();
97102

103+
static void AddWrapMethods(Environment* env,
104+
v8::Local<v8::FunctionTemplate> constructor,
105+
int flags = kFlagNone);
106+
98107
static void Initialize(v8::Local<v8::Object> target,
99108
v8::Local<v8::Value> unused,
100109
v8::Local<v8::Context> context);

src/cares_wrap.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ void Initialize(Local<Object> target,
21662166
Local<FunctionTemplate> aiw =
21672167
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21682168
aiw->InstanceTemplate()->SetInternalFieldCount(1);
2169-
env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId);
2169+
AsyncWrap::AddWrapMethods(env, aiw);
21702170
Local<String> addrInfoWrapString =
21712171
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
21722172
aiw->SetClassName(addrInfoWrapString);
@@ -2175,7 +2175,7 @@ void Initialize(Local<Object> target,
21752175
Local<FunctionTemplate> niw =
21762176
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21772177
niw->InstanceTemplate()->SetInternalFieldCount(1);
2178-
env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId);
2178+
AsyncWrap::AddWrapMethods(env, niw);
21792179
Local<String> nameInfoWrapString =
21802180
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
21812181
niw->SetClassName(nameInfoWrapString);
@@ -2184,7 +2184,7 @@ void Initialize(Local<Object> target,
21842184
Local<FunctionTemplate> qrw =
21852185
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21862186
qrw->InstanceTemplate()->SetInternalFieldCount(1);
2187-
env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId);
2187+
AsyncWrap::AddWrapMethods(env, qrw);
21882188
Local<String> queryWrapString =
21892189
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
21902190
qrw->SetClassName(queryWrapString);
@@ -2193,7 +2193,7 @@ void Initialize(Local<Object> target,
21932193
Local<FunctionTemplate> channel_wrap =
21942194
env->NewFunctionTemplate(ChannelWrap::New);
21952195
channel_wrap->InstanceTemplate()->SetInternalFieldCount(1);
2196-
env->SetProtoMethod(channel_wrap, "getAsyncId", AsyncWrap::GetAsyncId);
2196+
AsyncWrap::AddWrapMethods(env, channel_wrap);
21972197

21982198
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
21992199
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);

src/fs_event_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void FSEventWrap::Initialize(Local<Object> target,
9494
t->InstanceTemplate()->SetInternalFieldCount(1);
9595
t->SetClassName(fsevent_string);
9696

97-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
97+
AsyncWrap::AddWrapMethods(env, t);
9898
env->SetProtoMethod(t, "start", Start);
9999
env->SetProtoMethod(t, "close", Close);
100100

src/js_stream.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void JSStream::Initialize(Local<Object> target,
218218
t->SetClassName(jsStreamString);
219219
t->InstanceTemplate()->SetInternalFieldCount(1);
220220

221-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
221+
AsyncWrap::AddWrapMethods(env, t);
222222

223223
env->SetProtoMethod(t, "doAlloc", DoAlloc);
224224
env->SetProtoMethod(t, "doRead", DoRead);

src/node_crypto.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -2729,7 +2729,7 @@ void Connection::Initialize(Environment* env, Local<Object> target) {
27292729
t->InstanceTemplate()->SetInternalFieldCount(1);
27302730
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"));
27312731

2732-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
2732+
AsyncWrap::AddWrapMethods(env, t);
27332733
env->SetProtoMethod(t, "encIn", Connection::EncIn);
27342734
env->SetProtoMethod(t, "clearOut", Connection::ClearOut);
27352735
env->SetProtoMethod(t, "clearIn", Connection::ClearIn);
@@ -6129,14 +6129,14 @@ void InitCrypto(Local<Object> target,
61296129

61306130
Local<FunctionTemplate> pb = FunctionTemplate::New(env->isolate());
61316131
pb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PBKDF2"));
6132-
env->SetProtoMethod(pb, "getAsyncId", AsyncWrap::GetAsyncId);
6132+
AsyncWrap::AddWrapMethods(env, pb);
61336133
Local<ObjectTemplate> pbt = pb->InstanceTemplate();
61346134
pbt->SetInternalFieldCount(1);
61356135
env->set_pbkdf2_constructor_template(pbt);
61366136

61376137
Local<FunctionTemplate> rb = FunctionTemplate::New(env->isolate());
61386138
rb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "RandomBytes"));
6139-
env->SetProtoMethod(rb, "getAsyncId", AsyncWrap::GetAsyncId);
6139+
AsyncWrap::AddWrapMethods(env, rb);
61406140
Local<ObjectTemplate> rbt = rb->InstanceTemplate();
61416141
rbt->SetInternalFieldCount(1);
61426142
env->set_randombytes_constructor_template(rbt);

src/node_file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ void InitFs(Local<Object> target,
14831483
Local<FunctionTemplate> fst =
14841484
FunctionTemplate::New(env->isolate(), NewFSReqWrap);
14851485
fst->InstanceTemplate()->SetInternalFieldCount(1);
1486-
env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId);
1486+
AsyncWrap::AddWrapMethods(env, fst);
14871487
Local<String> wrapString =
14881488
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap");
14891489
fst->SetClassName(wrapString);

src/node_http2.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ void Initialize(Local<Object> target,
12121212
env->NewFunctionTemplate(Http2Session::New);
12131213
session->SetClassName(http2SessionClassName);
12141214
session->InstanceTemplate()->SetInternalFieldCount(1);
1215-
env->SetProtoMethod(session, "getAsyncId", AsyncWrap::GetAsyncId);
1215+
AsyncWrap::AddWrapMethods(env, session);
12161216
env->SetProtoMethod(session, "consume",
12171217
Http2Session::Consume);
12181218
env->SetProtoMethod(session, "destroy",

src/node_http_parser.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ void InitHttpParser(Local<Object> target,
794794
#undef V
795795
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "methods"), methods);
796796

797-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
797+
AsyncWrap::AddWrapMethods(env, t);
798798
env->SetProtoMethod(t, "close", Parser::Close);
799799
env->SetProtoMethod(t, "execute", Parser::Execute);
800800
env->SetProtoMethod(t, "finish", Parser::Finish);

src/node_stat_watcher.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
5252
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
5353
t->SetClassName(statWatcherString);
5454

55-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
55+
AsyncWrap::AddWrapMethods(env, t);
5656
env->SetProtoMethod(t, "start", StatWatcher::Start);
5757
env->SetProtoMethod(t, "stop", StatWatcher::Stop);
5858

src/node_zlib.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ void InitZlib(Local<Object> target,
684684

685685
z->InstanceTemplate()->SetInternalFieldCount(1);
686686

687-
env->SetProtoMethod(z, "getAsyncId", AsyncWrap::GetAsyncId);
687+
AsyncWrap::AddWrapMethods(env, z);
688688
env->SetProtoMethod(z, "write", ZCtx::Write<true>);
689689
env->SetProtoMethod(z, "writeSync", ZCtx::Write<false>);
690690
env->SetProtoMethod(z, "init", ZCtx::Init);

src/pipe_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void PipeWrap::Initialize(Local<Object> target,
7272
t->SetClassName(pipeString);
7373
t->InstanceTemplate()->SetInternalFieldCount(1);
7474

75-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
75+
AsyncWrap::AddWrapMethods(env, t);
7676

7777
env->SetProtoMethod(t, "close", HandleWrap::Close);
7878
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
@@ -104,7 +104,7 @@ void PipeWrap::Initialize(Local<Object> target,
104104
};
105105
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
106106
cwt->InstanceTemplate()->SetInternalFieldCount(1);
107-
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
107+
AsyncWrap::AddWrapMethods(env, cwt);
108108
Local<String> wrapString =
109109
FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
110110
cwt->SetClassName(wrapString);

src/process_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ProcessWrap : public HandleWrap {
5757
FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
5858
constructor->SetClassName(processString);
5959

60-
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
60+
AsyncWrap::AddWrapMethods(env, constructor);
6161

6262
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
6363

src/signal_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SignalWrap : public HandleWrap {
5454
FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
5555
constructor->SetClassName(signalString);
5656

57-
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
57+
AsyncWrap::AddWrapMethods(env, constructor);
5858
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
5959
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
6060
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);

src/stream_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void StreamWrap::Initialize(Local<Object> target,
7070
Local<String> wrapString =
7171
FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
7272
sw->SetClassName(wrapString);
73-
env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId);
73+
AsyncWrap::AddWrapMethods(env, sw);
7474
target->Set(wrapString, sw->GetFunction());
7575

7676
Local<FunctionTemplate> ww =
@@ -79,7 +79,7 @@ void StreamWrap::Initialize(Local<Object> target,
7979
Local<String> writeWrapString =
8080
FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
8181
ww->SetClassName(writeWrapString);
82-
env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId);
82+
AsyncWrap::AddWrapMethods(env, ww);
8383
target->Set(writeWrapString, ww->GetFunction());
8484
env->set_write_wrap_constructor_function(ww->GetFunction());
8585
}

src/tcp_wrap.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ void TCPWrap::Initialize(Local<Object> target,
8282
t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
8383
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
8484

85-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
86-
env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset);
85+
AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);
8786

8887
env->SetProtoMethod(t, "close", HandleWrap::Close);
8988

@@ -120,7 +119,7 @@ void TCPWrap::Initialize(Local<Object> target,
120119
};
121120
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
122121
cwt->InstanceTemplate()->SetInternalFieldCount(1);
123-
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
122+
AsyncWrap::AddWrapMethods(env, cwt);
124123
Local<String> wrapString =
125124
FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
126125
cwt->SetClassName(wrapString);

src/timer_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TimerWrap : public HandleWrap {
5959

6060
env->SetTemplateMethod(constructor, "now", Now);
6161

62-
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
62+
AsyncWrap::AddWrapMethods(env, constructor);
6363

6464
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
6565
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);

src/tls_wrap.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,7 @@ void TLSWrap::Initialize(Local<Object> target,
946946
t->InstanceTemplate()->SetInternalFieldCount(1);
947947
t->SetClassName(tlsWrapString);
948948

949-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
950-
env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset);
949+
AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);
951950
env->SetProtoMethod(t, "receive", Receive);
952951
env->SetProtoMethod(t, "start", Start);
953952
env->SetProtoMethod(t, "setVerifyMode", SetVerifyMode);

src/tty_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void TTYWrap::Initialize(Local<Object> target,
5656
t->SetClassName(ttyString);
5757
t->InstanceTemplate()->SetInternalFieldCount(1);
5858

59-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
59+
AsyncWrap::AddWrapMethods(env, t);
6060

6161
env->SetProtoMethod(t, "close", HandleWrap::Close);
6262
env->SetProtoMethod(t, "unref", HandleWrap::Unref);

src/udp_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void UDPWrap::Initialize(Local<Object> target,
139139
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
140140
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
141141

142-
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
142+
AsyncWrap::AddWrapMethods(env, t);
143143

144144
target->Set(udpString, t->GetFunction());
145145
env->set_udp_constructor_function(t->GetFunction());
@@ -148,7 +148,7 @@ void UDPWrap::Initialize(Local<Object> target,
148148
Local<FunctionTemplate> swt =
149149
FunctionTemplate::New(env->isolate(), NewSendWrap);
150150
swt->InstanceTemplate()->SetInternalFieldCount(1);
151-
env->SetProtoMethod(swt, "getAsyncId", AsyncWrap::GetAsyncId);
151+
AsyncWrap::AddWrapMethods(env, swt);
152152
Local<String> sendWrapString =
153153
FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap");
154154
swt->SetClassName(sendWrapString);

0 commit comments

Comments
 (0)