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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)