diff --git a/src/async-wrap.cc b/src/async-wrap.cc index 7630de3694df99..de6ff2fceb0489 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -468,6 +468,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 f2fa8f4334f34d..7b427b0904b1fd 100644 --- a/src/async-wrap.h +++ b/src/async-wrap.h @@ -87,6 +87,11 @@ class AsyncWrap : public BaseObject { PROVIDERS_LENGTH, }; + enum Flags { + kFlagNone = 0x0, + kFlagHasReset = 0x1 + }; + AsyncWrap(Environment* env, v8::Local object, ProviderType provider, @@ -94,6 +99,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 f43eb7ed8b54ae..e881ff517313d7 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -2189,34 +2189,34 @@ void Initialize(Local target, Local aiw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); aiw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId); - aiw->SetClassName( - FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"), - aiw->GetFunction()); + AsyncWrap::AddWrapMethods(env, aiw); + Local addrInfoWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"); + aiw->SetClassName(addrInfoWrapString); + target->Set(addrInfoWrapString, aiw->GetFunction()); Local niw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); niw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId); - niw->SetClassName( - FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"), - niw->GetFunction()); + AsyncWrap::AddWrapMethods(env, niw); + Local nameInfoWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"); + niw->SetClassName(nameInfoWrapString); + target->Set(nameInfoWrapString, niw->GetFunction()); Local qrw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); qrw->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId); - qrw->SetClassName( - FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"), - qrw->GetFunction()); + AsyncWrap::AddWrapMethods(env, qrw); + Local queryWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"); + qrw->SetClassName(queryWrapString); + target->Set(queryWrapString, qrw->GetFunction()); 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); @@ -2235,10 +2235,10 @@ void Initialize(Local target, env->SetProtoMethod(channel_wrap, "setServers", SetServers); env->SetProtoMethod(channel_wrap, "cancel", Cancel); - channel_wrap->SetClassName( - FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"), - channel_wrap->GetFunction()); + Local channelWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"); + channel_wrap->SetClassName(channelWrapString); + target->Set(channelWrapString, channel_wrap->GetFunction()); } } // anonymous namespace 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 d88cc853c800cc..2e7f082e289918 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -18,6 +18,7 @@ using v8::HandleScope; using v8::Local; using v8::MaybeLocal; using v8::Object; +using v8::String; using v8::Value; @@ -212,10 +213,12 @@ void JSStream::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream")); + Local jsStreamString = + FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"); + 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); @@ -226,8 +229,7 @@ void JSStream::Initialize(Local target, env->SetProtoMethod(t, "emitEOF", EmitEOF); StreamBase::AddMethods(env, t, StreamBase::kFlagHasWritev); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"), - t->GetFunction()); + target->Set(jsStreamString, t->GetFunction()); env->set_jsstream_constructor_template(t); } diff --git a/src/node_config.cc b/src/node_config.cc index a2d980d793fc2e..87110dd8c644f7 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -105,7 +105,7 @@ static void InitConfig(Local target, debugOptions->DefineOwnProperty( context, - FIXED_ONE_BYTE_STRING(isolate, "port"), + env->port_string(), Integer::New(isolate, debug_options.port()), ReadOnly).FromJust(); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 664bf1a72c7e8c..e935490971c5b2 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -312,7 +312,9 @@ bool EntropySource(unsigned char* buffer, size_t length) { void SecureContext::Initialize(Environment* env, Local target) { Local t = env->NewFunctionTemplate(SecureContext::New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext")); + Local secureContextString = + FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"); + t->SetClassName(secureContextString); env->SetProtoMethod(t, "init", SecureContext::Init); env->SetProtoMethod(t, "setKey", SecureContext::SetKey); @@ -359,8 +361,7 @@ void SecureContext::Initialize(Environment* env, Local target) { static_cast(ReadOnly | DontDelete), AccessorSignature::New(env->isolate(), t)); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"), - t->GetFunction()); + target->Set(secureContextString, t->GetFunction()); env->set_secure_context_constructor_template(t); } @@ -2728,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); @@ -6128,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 beaf581afca0ff..cafa17ab39b028 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1480,10 +1480,11 @@ void InitFs(Local target, Local fst = FunctionTemplate::New(env->isolate(), NewFSReqWrap); fst->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId); - fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"), - fst->GetFunction()); + AsyncWrap::AddWrapMethods(env, fst); + Local wrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"); + fst->SetClassName(wrapString); + target->Set(wrapString, fst->GetFunction()); } } // end namespace node diff --git a/src/node_http2.cc b/src/node_http2.cc index 9308e9e68e930e..a8eb0a37bf8062 100755 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1206,14 +1206,13 @@ void Initialize(Local target, env->SetMethod(target, "nghttp2ErrorString", HttpErrorString); Local http2SessionClassName = - String::NewFromUtf8(isolate, "Http2Session", - v8::NewStringType::kInternalized).ToLocalChecked(); + FIXED_ONE_BYTE_STRING(isolate, "Http2Session"); Local session = 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_serdes.cc b/src/node_serdes.cc index a077598dfe4cbf..ced9e4dd982f79 100644 --- a/src/node_serdes.cc +++ b/src/node_serdes.cc @@ -451,9 +451,11 @@ void InitializeSerdesBindings(Local target, "_setTreatArrayBufferViewsAsHostObjects", SerializerContext::SetTreatArrayBufferViewsAsHostObjects); - ser->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer")); + Local serializerString = + FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"); + ser->SetClassName(serializerString); target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"), + serializerString, ser->GetFunction(env->context()).ToLocalChecked()).FromJust(); Local des = @@ -474,9 +476,11 @@ void InitializeSerdesBindings(Local target, env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble); env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes); - des->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer")); + Local deserializerString = + FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"); + des->SetClassName(deserializerString); target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"), + deserializerString, des->GetFunction(env->context()).ToLocalChecked()).FromJust(); } diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 18bf2c54193d7b..4b2e4db7bc418c 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -39,6 +39,7 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; +using v8::String; using v8::Value; @@ -47,14 +48,15 @@ void StatWatcher::Initialize(Environment* env, Local target) { Local t = env->NewFunctionTemplate(StatWatcher::New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher")); + Local statWatcherString = + 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); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"), - t->GetFunction()); + target->Set(statWatcherString, t->GetFunction()); } diff --git a/src/node_zlib.cc b/src/node_zlib.cc index b66313f179cdaa..fdfd314222664c 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -50,6 +50,7 @@ using v8::Local; using v8::Number; using v8::Object; using v8::Persistent; +using v8::String; using v8::Uint32Array; using v8::Value; @@ -685,7 +686,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); @@ -693,8 +694,9 @@ void InitZlib(Local target, env->SetProtoMethod(z, "params", ZCtx::Params); env->SetProtoMethod(z, "reset", ZCtx::Reset); - z->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"), z->GetFunction()); + Local zlibString = FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"); + z->SetClassName(zlibString); + target->Set(zlibString, z->GetFunction()); target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"), FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)); diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 2185580b0662e8..e29cf6d70f1a74 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -44,6 +44,7 @@ using v8::FunctionTemplate; using v8::HandleScope; using v8::Local; using v8::Object; +using v8::String; using v8::Value; using AsyncHooks = Environment::AsyncHooks; @@ -67,10 +68,11 @@ void PipeWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe")); + Local pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"); + 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); @@ -92,7 +94,7 @@ void PipeWrap::Initialize(Local target, env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances); #endif - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction()); + target->Set(pipeString, t->GetFunction()); env->set_pipe_constructor_template(t); // Create FunctionTemplate for PipeConnectWrap. @@ -102,10 +104,11 @@ void PipeWrap::Initialize(Local target, }; auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId); - cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"), - cwt->GetFunction()); + AsyncWrap::AddWrapMethods(env, cwt); + Local wrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"); + cwt->SetClassName(wrapString); + target->Set(wrapString, cwt->GetFunction()); } diff --git a/src/process_wrap.cc b/src/process_wrap.cc index cae0788927bc89..7accc8c129ecbc 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -53,9 +53,11 @@ class ProcessWrap : public HandleWrap { Environment* env = Environment::GetCurrent(context); Local constructor = env->NewFunctionTemplate(New); constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process")); + Local processString = + 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); @@ -66,8 +68,7 @@ class ProcessWrap : public HandleWrap { env->SetProtoMethod(constructor, "unref", HandleWrap::Unref); env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"), - constructor->GetFunction()); + target->Set(processString, constructor->GetFunction()); } size_t self_size() const override { return sizeof(*this); } diff --git a/src/signal_wrap.cc b/src/signal_wrap.cc index 1af98a9311f102..048a3de4beaece 100644 --- a/src/signal_wrap.cc +++ b/src/signal_wrap.cc @@ -37,6 +37,7 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; +using v8::String; using v8::Value; namespace { @@ -49,9 +50,11 @@ class SignalWrap : public HandleWrap { Environment* env = Environment::GetCurrent(context); Local constructor = env->NewFunctionTemplate(New); constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal")); + Local signalString = + 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); @@ -59,8 +62,7 @@ class SignalWrap : public HandleWrap { env->SetProtoMethod(constructor, "start", Start); env->SetProtoMethod(constructor, "stop", Stop); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"), - constructor->GetFunction()); + target->Set(signalString, constructor->GetFunction()); } size_t self_size() const override { return sizeof(*this); } diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 3497146cb07983..12e8211fb7b20a 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -67,18 +67,20 @@ void StreamWrap::Initialize(Local target, Local sw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); sw->InstanceTemplate()->SetInternalFieldCount(1); - sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap")); - env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"), - sw->GetFunction()); + Local wrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"); + sw->SetClassName(wrapString); + AsyncWrap::AddWrapMethods(env, sw); + target->Set(wrapString, sw->GetFunction()); Local ww = FunctionTemplate::New(env->isolate(), is_construct_call_callback); ww->InstanceTemplate()->SetInternalFieldCount(1); - ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap")); - env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"), - ww->GetFunction()); + Local writeWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"); + ww->SetClassName(writeWrapString); + 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 4bee4b97097f40..2c2aeab6dd9356 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -71,18 +71,18 @@ void TCPWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP")); + Local tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"); + t->SetClassName(tcpString); t->InstanceTemplate()->SetInternalFieldCount(1); // Init properties - t->InstanceTemplate()->Set(String::NewFromUtf8(env->isolate(), "reading"), + t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"), Boolean::New(env->isolate(), false)); t->InstanceTemplate()->Set(env->owner_string(), Null(env->isolate())); 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); @@ -109,7 +109,7 @@ void TCPWrap::Initialize(Local target, env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts); #endif - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"), t->GetFunction()); + target->Set(tcpString, t->GetFunction()); env->set_tcp_constructor_template(t); // Create FunctionTemplate for TCPConnectWrap. @@ -119,10 +119,11 @@ void TCPWrap::Initialize(Local target, }; auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId); - cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"), - cwt->GetFunction()); + AsyncWrap::AddWrapMethods(env, cwt); + Local wrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"); + cwt->SetClassName(wrapString); + target->Set(wrapString, cwt->GetFunction()); } diff --git a/src/timer_wrap.cc b/src/timer_wrap.cc index 609d087e334f72..e398a471e74242 100644 --- a/src/timer_wrap.cc +++ b/src/timer_wrap.cc @@ -39,6 +39,7 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; +using v8::String; using v8::Value; const uint32_t kOnTimeout = 0; @@ -50,14 +51,15 @@ class TimerWrap : public HandleWrap { Local context) { Environment* env = Environment::GetCurrent(context); Local constructor = env->NewFunctionTemplate(New); + Local timerString = FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"); constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer")); + constructor->SetClassName(timerString); constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"), Integer::New(env->isolate(), kOnTimeout)); 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); @@ -67,8 +69,7 @@ class TimerWrap : public HandleWrap { env->SetProtoMethod(constructor, "start", Start); env->SetProtoMethod(constructor, "stop", Stop); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"), - constructor->GetFunction()); + target->Set(timerString, constructor->GetFunction()); } size_t self_size() const override { return sizeof(*this); } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index f03dbd7fd650d8..77d8ce7b5ea447 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -938,12 +938,15 @@ void TLSWrap::Initialize(Local target, CHECK(args.IsConstructCall()); args.This()->SetAlignedPointerInInternalField(0, nullptr); }; + + Local tlsWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap"); + auto t = env->NewFunctionTemplate(constructor); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap")); + 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); @@ -962,8 +965,7 @@ void TLSWrap::Initialize(Local target, env->set_tls_wrap_constructor_template(t); env->set_tls_wrap_constructor_function(t->GetFunction()); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap"), - t->GetFunction()); + target->Set(tlsWrapString, t->GetFunction()); } } // namespace node diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index f3f1edfe5d3248..ee793056c845d8 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -41,6 +41,7 @@ using v8::FunctionTemplate; using v8::Integer; using v8::Local; using v8::Object; +using v8::String; using v8::Value; @@ -49,11 +50,13 @@ void TTYWrap::Initialize(Local target, Local context) { Environment* env = Environment::GetCurrent(context); + Local ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"); + Local t = env->NewFunctionTemplate(New); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY")); + 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); @@ -68,7 +71,7 @@ void TTYWrap::Initialize(Local target, env->SetMethod(target, "isTTY", IsTTY); env->SetMethod(target, "guessHandleType", GuessHandleType); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"), t->GetFunction()); + target->Set(ttyString, t->GetFunction()); env->set_tty_constructor_template(t); } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index c192de6d628cec..37396602baac5b 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -106,7 +106,9 @@ void UDPWrap::Initialize(Local target, Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "UDP")); + Local udpString = + FIXED_ONE_BYTE_STRING(env->isolate(), "UDP"); + t->SetClassName(udpString); enum PropertyAttribute attributes = static_cast(v8::ReadOnly | v8::DontDelete); @@ -137,19 +139,20 @@ 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(FIXED_ONE_BYTE_STRING(env->isolate(), "UDP"), t->GetFunction()); + target->Set(udpString, t->GetFunction()); env->set_udp_constructor_function(t->GetFunction()); // Create FunctionTemplate for SendWrap Local swt = FunctionTemplate::New(env->isolate(), NewSendWrap); swt->InstanceTemplate()->SetInternalFieldCount(1); - env->SetProtoMethod(swt, "getAsyncId", AsyncWrap::GetAsyncId); - swt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap")); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"), - swt->GetFunction()); + AsyncWrap::AddWrapMethods(env, swt); + Local sendWrapString = + FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"); + swt->SetClassName(sendWrapString); + target->Set(sendWrapString, swt->GetFunction()); }