diff --git a/src/async-wrap.cc b/src/async-wrap.cc index 71b1e256592b08..468ac975247829 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -475,6 +475,13 @@ void AsyncWrap::QueueDestroyId(const FunctionCallbackInfo& args) { PushBackDestroyId(Environment::GetCurrent(args), args[0]->NumberValue()); } +void AsyncWrap::AddWrapMethods(Environment* env, + Local constructor, + int flag) { + env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId); + if (flag & kFlagHasReset) + env->SetProtoMethod(constructor, "asyncReset", AsyncWrap::AsyncReset); +} void AsyncWrap::Initialize(Local target, Local unused, diff --git a/src/async-wrap.h b/src/async-wrap.h index 10d0150844a7ab..a4c42d01b73d36 100644 --- a/src/async-wrap.h +++ b/src/async-wrap.h @@ -88,6 +88,11 @@ class AsyncWrap : public BaseObject { PROVIDERS_LENGTH, }; + enum Flags { + kFlagNone = 0x0, + kFlagHasReset = 0x1 + }; + AsyncWrap(Environment* env, v8::Local object, ProviderType provider, @@ -95,6 +100,10 @@ class AsyncWrap : public BaseObject { virtual ~AsyncWrap(); + static void AddWrapMethods(Environment* env, + v8::Local constructor, + int flags = kFlagNone); + static void Initialize(v8::Local target, v8::Local unused, v8::Local context); diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 976cbfc759ff53..e800e0f2fee260 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -2166,7 +2166,7 @@ void Initialize(Local target, Local aiw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); aiw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, aiw); Local addrInfoWrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"); aiw->SetClassName(addrInfoWrapString); @@ -2175,7 +2175,7 @@ void Initialize(Local target, Local niw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); niw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, niw); Local nameInfoWrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"); niw->SetClassName(nameInfoWrapString); @@ -2184,7 +2184,7 @@ void Initialize(Local target, Local qrw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); qrw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, qrw); Local queryWrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"); qrw->SetClassName(queryWrapString); @@ -2193,7 +2193,7 @@ void Initialize(Local target, Local channel_wrap = env->NewFunctionTemplate(ChannelWrap::New); channel_wrap->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(channel_wrap, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, channel_wrap); env->SetProtoMethod(channel_wrap, "queryAny", Query); env->SetProtoMethod(channel_wrap, "queryA", Query); diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 228c3a344edf3c..8ec8dd6dcfbd76 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -94,7 +94,7 @@ void FSEventWrap::Initialize(Local target, t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(fsevent_string); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "start", Start); env->SetProtoMethod(t, "close", Close); diff --git a/src/js_stream.cc b/src/js_stream.cc index caef92f1cadb98..2e7f082e289918 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -218,7 +218,7 @@ void JSStream::Initialize(Local target, t->SetClassName(jsStreamString); t->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "doAlloc", DoAlloc); env->SetProtoMethod(t, "doRead", DoRead); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index f279cb89f6f2c9..e935490971c5b2 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2729,7 +2729,7 @@ void Connection::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Connection")); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "encIn", Connection::EncIn); env->SetProtoMethod(t, "clearOut", Connection::ClearOut); env->SetProtoMethod(t, "clearIn", Connection::ClearIn); @@ -6129,14 +6129,14 @@ void InitCrypto(Local target, Local pb = FunctionTemplate::New(env->isolate()); pb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PBKDF2")); - env->SetProtoMethod(pb, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, pb); Local pbt = pb->InstanceTemplate(); pbt->SetInternalFieldCount(1); env->set_pbkdf2_constructor_template(pbt); Local rb = FunctionTemplate::New(env->isolate()); rb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "RandomBytes")); - env->SetProtoMethod(rb, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, rb); Local rbt = rb->InstanceTemplate(); rbt->SetInternalFieldCount(1); env->set_randombytes_constructor_template(rbt); diff --git a/src/node_file.cc b/src/node_file.cc index 407e692f99bb23..ee69915ddcaf70 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1483,7 +1483,7 @@ void InitFs(Local target, Local fst = FunctionTemplate::New(env->isolate(), NewFSReqWrap); fst->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, fst); Local wrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"); fst->SetClassName(wrapString); diff --git a/src/node_http2.cc b/src/node_http2.cc index 70dcc5e00d1746..a8eb0a37bf8062 100755 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1212,7 +1212,7 @@ void Initialize(Local target, env->NewFunctionTemplate(Http2Session::New); session->SetClassName(http2SessionClassName); session->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(session, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, session); env->SetProtoMethod(session, "consume", Http2Session::Consume); env->SetProtoMethod(session, "destroy", diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index c897a771a5225b..dfe002814773c8 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -794,7 +794,7 @@ void InitHttpParser(Local target, #undef V target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "methods"), methods); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "close", Parser::Close); env->SetProtoMethod(t, "execute", Parser::Execute); env->SetProtoMethod(t, "finish", Parser::Finish); diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 3f9354efcef3ed..4b2e4db7bc418c 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -52,7 +52,7 @@ void StatWatcher::Initialize(Environment* env, Local target) { FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"); t->SetClassName(statWatcherString); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "start", StatWatcher::Start); env->SetProtoMethod(t, "stop", StatWatcher::Stop); diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 2e83291c9f3375..e48482cdc752f3 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -684,7 +684,7 @@ void InitZlib(Local target, z->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(z, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, z); env->SetProtoMethod(z, "write", ZCtx::Write); env->SetProtoMethod(z, "writeSync", ZCtx::Write); env->SetProtoMethod(z, "init", ZCtx::Init); diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 0daec0b1449d00..e29cf6d70f1a74 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -72,7 +72,7 @@ void PipeWrap::Initialize(Local target, t->SetClassName(pipeString); t->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "close", HandleWrap::Close); env->SetProtoMethod(t, "unref", HandleWrap::Unref); @@ -104,7 +104,7 @@ void PipeWrap::Initialize(Local target, }; auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, cwt); Local wrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"); cwt->SetClassName(wrapString); diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 26e233b2bd0ee2..7accc8c129ecbc 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -57,7 +57,7 @@ class ProcessWrap : public HandleWrap { FIXED_ONE_BYTE_STRING(env->isolate(), "Process"); constructor->SetClassName(processString); - env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, constructor); env->SetProtoMethod(constructor, "close", HandleWrap::Close); diff --git a/src/signal_wrap.cc b/src/signal_wrap.cc index eed010b388d82f..048a3de4beaece 100644 --- a/src/signal_wrap.cc +++ b/src/signal_wrap.cc @@ -54,7 +54,7 @@ class SignalWrap : public HandleWrap { FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"); constructor->SetClassName(signalString); - env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, constructor); env->SetProtoMethod(constructor, "close", HandleWrap::Close); env->SetProtoMethod(constructor, "ref", HandleWrap::Ref); env->SetProtoMethod(constructor, "unref", HandleWrap::Unref); diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 57c93ac489ba2f..1e3863cad24f29 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -70,7 +70,7 @@ void StreamWrap::Initialize(Local target, Local wrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"); sw->SetClassName(wrapString); - env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, sw); target->Set(wrapString, sw->GetFunction()); Local ww = @@ -79,7 +79,7 @@ void StreamWrap::Initialize(Local target, Local writeWrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"); ww->SetClassName(writeWrapString); - env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, ww); target->Set(writeWrapString, ww->GetFunction()); env->set_write_wrap_constructor_function(ww->GetFunction()); } diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index e721108caecb83..2c2aeab6dd9356 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -82,8 +82,7 @@ void TCPWrap::Initialize(Local target, t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate())); t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate())); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); - env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset); + AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset); env->SetProtoMethod(t, "close", HandleWrap::Close); @@ -120,7 +119,7 @@ void TCPWrap::Initialize(Local target, }; auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, cwt); Local wrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"); cwt->SetClassName(wrapString); diff --git a/src/timer_wrap.cc b/src/timer_wrap.cc index 03e7ad32626c61..e398a471e74242 100644 --- a/src/timer_wrap.cc +++ b/src/timer_wrap.cc @@ -59,7 +59,7 @@ class TimerWrap : public HandleWrap { env->SetTemplateMethod(constructor, "now", Now); - env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, constructor); env->SetProtoMethod(constructor, "close", HandleWrap::Close); env->SetProtoMethod(constructor, "ref", HandleWrap::Ref); diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 4e9eb1993037fe..77d8ce7b5ea447 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -946,8 +946,7 @@ void TLSWrap::Initialize(Local target, t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(tlsWrapString); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); - env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset); + AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset); env->SetProtoMethod(t, "receive", Receive); env->SetProtoMethod(t, "start", Start); env->SetProtoMethod(t, "setVerifyMode", SetVerifyMode); diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 88e9a9826499ab..ee793056c845d8 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -56,7 +56,7 @@ void TTYWrap::Initialize(Local target, t->SetClassName(ttyString); t->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); env->SetProtoMethod(t, "close", HandleWrap::Close); env->SetProtoMethod(t, "unref", HandleWrap::Unref); diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 702a5263c89956..37396602baac5b 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -139,7 +139,7 @@ void UDPWrap::Initialize(Local target, env->SetProtoMethod(t, "unref", HandleWrap::Unref); env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef); - env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, t); target->Set(udpString, t->GetFunction()); env->set_udp_constructor_function(t->GetFunction()); @@ -148,7 +148,7 @@ void UDPWrap::Initialize(Local target, Local swt = FunctionTemplate::New(env->isolate(), NewSendWrap); swt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(swt, "getAsyncId", AsyncWrap::GetAsyncId); + AsyncWrap::AddWrapMethods(env, swt); Local sendWrapString = FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"); swt->SetClassName(sendWrapString);