From 28fe1ef63512acd40cfd1dd15918db494c92f45f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 15 Dec 2020 11:52:17 -0800 Subject: [PATCH] src: reduce duplicated boilerplate with new env utility fn Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/36536 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Joyee Cheung --- src/README.md | 6 +----- src/cares_wrap.cc | 27 ++++----------------------- src/crypto/crypto_cipher.cc | 5 +---- src/crypto/crypto_context.cc | 7 ++----- src/crypto/crypto_dh.cc | 4 +--- src/crypto/crypto_ecdh.cc | 4 +--- src/crypto/crypto_hash.cc | 4 +--- src/crypto/crypto_hmac.cc | 4 +--- src/crypto/crypto_sig.cc | 8 ++------ src/crypto/crypto_util.h | 8 +------- src/env-inl.h | 18 ++++++++++++++++++ src/env.h | 8 ++++++++ src/fs_event_wrap.cc | 6 +----- src/inspector_js_api.cc | 10 ++++------ src/js_stream.cc | 8 +------- src/js_udp_wrap.cc | 8 +------- src/module_wrap.cc | 6 ++---- src/node_contextify.cc | 12 +----------- src/node_dir.cc | 10 +--------- src/node_file.cc | 16 ++-------------- src/node_http2.cc | 14 ++------------ src/node_http_parser.cc | 5 +---- src/node_messaging.cc | 26 +++++++++----------------- src/node_perf.cc | 3 +-- src/node_serdes.cc | 14 ++------------ src/node_sockaddr.cc | 6 +----- src/node_stat_watcher.cc | 7 +------ src/node_trace_events.cc | 5 +---- src/node_util.cc | 6 +----- src/node_wasi.cc | 6 +----- src/node_watchdog.cc | 9 +-------- src/node_worker.cc | 7 +------ src/node_zlib.cc | 7 +------ src/pipe_wrap.cc | 14 ++------------ src/process_wrap.cc | 7 +------ src/quic/node_quic_socket.cc | 14 ++------------ src/quic/node_quic_stream.cc | 9 +-------- src/signal_wrap.cc | 8 +------- src/stream_pipe.cc | 9 +-------- src/stream_wrap.cc | 15 ++------------- src/tcp_wrap.cc | 13 ++----------- src/udp_wrap.cc | 15 ++------------- src/uv.cc | 9 ++++----- 43 files changed, 95 insertions(+), 312 deletions(-) diff --git a/src/README.md b/src/README.md index 2be97e28b8fbea..aec56e7a7ba05a 100644 --- a/src/README.md +++ b/src/README.md @@ -405,11 +405,7 @@ void Initialize(Local target, env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers); - Local channel_wrap_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"); - channel_wrap->SetClassName(channel_wrap_string); - target->Set(env->context(), channel_wrap_string, - channel_wrap->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "ChannelWrap", channel_wrap); } // Run the `Initialize` function when loading this module through diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 71766fa39171af..2bf4211b118602 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -2345,32 +2345,17 @@ void Initialize(Local target, Local aiw = BaseObject::MakeLazilyInitializedJSTemplate(env); aiw->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local addrInfoWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"); - aiw->SetClassName(addrInfoWrapString); - target->Set(env->context(), - addrInfoWrapString, - aiw->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "GetAddrInfoReqWrap", aiw); Local niw = BaseObject::MakeLazilyInitializedJSTemplate(env); niw->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local nameInfoWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"); - niw->SetClassName(nameInfoWrapString); - target->Set(env->context(), - nameInfoWrapString, - niw->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "GetNameInfoReqWrap", niw); Local qrw = BaseObject::MakeLazilyInitializedJSTemplate(env); qrw->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local queryWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"); - qrw->SetClassName(queryWrapString); - target->Set(env->context(), - queryWrapString, - qrw->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "QueryReqWrap", qrw); Local channel_wrap = env->NewFunctionTemplate(ChannelWrap::New); @@ -2397,11 +2382,7 @@ void Initialize(Local target, env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress); env->SetProtoMethod(channel_wrap, "cancel", Cancel); - Local channelWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"); - channel_wrap->SetClassName(channelWrapString); - target->Set(env->context(), channelWrapString, - channel_wrap->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "ChannelWrap", channel_wrap); } } // anonymous namespace diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc index f3939d3477c6ca..ddbf7114b673cd 100644 --- a/src/crypto/crypto_cipher.cc +++ b/src/crypto/crypto_cipher.cc @@ -265,10 +265,7 @@ void CipherBase::Initialize(Environment* env, Local target) { env->SetProtoMethodNoSideEffect(t, "getAuthTag", GetAuthTag); env->SetProtoMethod(t, "setAuthTag", SetAuthTag); env->SetProtoMethod(t, "setAAD", SetAAD); - - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "CipherBase", t); env->SetMethodNoSideEffect(target, "getSSLCiphers", GetSSLCiphers); env->SetMethodNoSideEffect(target, "getCiphers", GetCiphers); diff --git a/src/crypto/crypto_context.cc b/src/crypto/crypto_context.cc index 612d21948495d4..874ae1c27c83ed 100644 --- a/src/crypto/crypto_context.cc +++ b/src/crypto/crypto_context.cc @@ -252,9 +252,6 @@ void SecureContext::Initialize(Environment* env, Local target) { t->InstanceTemplate()->SetInternalFieldCount( SecureContext::kInternalFieldCount); t->Inherit(BaseObject::GetConstructorTemplate(env)); - Local secureContextString = - FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"); - t->SetClassName(secureContextString); env->SetProtoMethod(t, "init", Init); env->SetProtoMethod(t, "setKey", SetKey); @@ -313,8 +310,8 @@ void SecureContext::Initialize(Environment* env, Local target) { Local(), static_cast(ReadOnly | DontDelete)); - target->Set(env->context(), secureContextString, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "SecureContext", t); + env->set_secure_context_constructor_template(t); env->SetMethodNoSideEffect(target, "getRootCertificates", diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index a2720301cab356..b40f06f4500cd8 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -93,9 +93,7 @@ void DiffieHellman::Initialize(Environment* env, Local target) { Local(), attributes); - target->Set(env->context(), - name, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, name, t); }; make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New); diff --git a/src/crypto/crypto_ecdh.cc b/src/crypto/crypto_ecdh.cc index 277a5a731d37ae..efeb08b908e427 100644 --- a/src/crypto/crypto_ecdh.cc +++ b/src/crypto/crypto_ecdh.cc @@ -52,9 +52,7 @@ void ECDH::Initialize(Environment* env, Local target) { env->SetProtoMethod(t, "setPublicKey", SetPublicKey); env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "ECDH", t); env->SetMethodNoSideEffect(target, "ECDHConvertKey", ECDH::ConvertKey); env->SetMethodNoSideEffect(target, "getCurves", ECDH::GetCurves); diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index d49a7c5d022f0b..664ffb847215af 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -50,9 +50,7 @@ void Hash::Initialize(Environment* env, Local target) { env->SetProtoMethod(t, "update", HashUpdate); env->SetProtoMethod(t, "digest", HashDigest); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Hash", t); env->SetMethodNoSideEffect(target, "getHashes", GetHashes); diff --git a/src/crypto/crypto_hmac.cc b/src/crypto/crypto_hmac.cc index ff7c1603020b50..055541196d26e1 100644 --- a/src/crypto/crypto_hmac.cc +++ b/src/crypto/crypto_hmac.cc @@ -48,9 +48,7 @@ void Hmac::Initialize(Environment* env, Local target) { env->SetProtoMethod(t, "update", HmacUpdate); env->SetProtoMethod(t, "digest", HmacDigest); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Hmac", t); HmacJob::Initialize(env, target); } diff --git a/src/crypto/crypto_sig.cc b/src/crypto/crypto_sig.cc index 59a9569ce8143c..a5a95878a9eeec 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -276,9 +276,7 @@ void Sign::Initialize(Environment* env, Local target) { env->SetProtoMethod(t, "update", SignUpdate); env->SetProtoMethod(t, "sign", SignFinal); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Sign", t); env->SetMethod(target, "signOneShot", Sign::SignSync); @@ -396,9 +394,7 @@ void Verify::Initialize(Environment* env, Local target) { env->SetProtoMethod(t, "update", VerifyUpdate); env->SetProtoMethod(t, "verify", VerifyFinal); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Verify", t); env->SetMethod(target, "verifyOneShot", Verify::VerifySync); } diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index a8aa4a707f423a..3f245910ed83d1 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -349,17 +349,11 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork { Environment* env, v8::Local target) { v8::Local job = env->NewFunctionTemplate(new_fn); - v8::Local class_name = - OneByteString(env->isolate(), CryptoJobTraits::JobName); - job->SetClassName(class_name); job->Inherit(AsyncWrap::GetConstructorTemplate(env)); job->InstanceTemplate()->SetInternalFieldCount( AsyncWrap::kInternalFieldCount); env->SetProtoMethod(job, "run", Run); - target->Set( - env->context(), - class_name, - job->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, CryptoJobTraits::JobName, job); } private: diff --git a/src/env-inl.h b/src/env-inl.h index cd9f6daaaff758..4cb68e1c5dea8f 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -1024,6 +1024,24 @@ inline void Environment::SetInstanceMethod(v8::Local that, t->SetClassName(name_string); } +inline void Environment::SetConstructorFunction( + v8::Local that, + const char* name, + v8::Local tmpl) { + SetConstructorFunction(that, OneByteString(isolate(), name), tmpl); +} + +inline void Environment::SetConstructorFunction( + v8::Local that, + v8::Local name, + v8::Local tmpl) { + tmpl->SetClassName(name); + that->Set( + context(), + name, + tmpl->GetFunction(context()).ToLocalChecked()).Check(); +} + void Environment::AddCleanupHook(CleanupCallback fn, void* arg) { auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback { fn, arg, cleanup_hook_counter_++ diff --git a/src/env.h b/src/env.h index a0d59ff8728deb..9ac090e07cc450 100644 --- a/src/env.h +++ b/src/env.h @@ -1233,6 +1233,14 @@ class Environment : public MemoryRetainer { const char* name, v8::FunctionCallback callback); + inline void SetConstructorFunction(v8::Local that, + const char* name, + v8::Local tmpl); + + inline void SetConstructorFunction(v8::Local that, + v8::Local name, + v8::Local tmpl); + void AtExit(void (*cb)(void* arg), void* arg); void RunAtExitCallbacks(); diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index faa650b7a10cf9..b79da7e83622c9 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -95,11 +95,9 @@ void FSEventWrap::Initialize(Local target, void* priv) { Environment* env = Environment::GetCurrent(context); - auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent"); Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( FSEventWrap::kInternalFieldCount); - t->SetClassName(fsevent_string); t->Inherit(HandleWrap::GetConstructorTemplate(env)); env->SetProtoMethod(t, "start", Start); @@ -116,9 +114,7 @@ void FSEventWrap::Initialize(Local target, Local(), static_cast(ReadOnly | DontDelete | DontEnum)); - target->Set(env->context(), - fsevent_string, - t->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "FSEvent", t); } diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index c0791ce3194ca4..8de1f8e7b0a88d 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -102,19 +102,17 @@ class JSBindingsConnection : public AsyncWrap { } static void Bind(Environment* env, Local target) { - Local class_name = ConnectionType::GetClassName(env); Local tmpl = env->NewFunctionTemplate(JSBindingsConnection::New); tmpl->InstanceTemplate()->SetInternalFieldCount( JSBindingsConnection::kInternalFieldCount); - tmpl->SetClassName(class_name); tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch); env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect); - target->Set(env->context(), - class_name, - tmpl->GetFunction(env->context()).ToLocalChecked()) - .ToChecked(); + env->SetConstructorFunction( + target, + ConnectionType::GetClassName(env), + tmpl); } static void New(const FunctionCallbackInfo& info) { diff --git a/src/js_stream.cc b/src/js_stream.cc index e4da0ce747e3a0..399e073efba697 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -19,7 +19,6 @@ using v8::HandleScope; using v8::Int32; using v8::Local; using v8::Object; -using v8::String; using v8::Value; @@ -200,9 +199,6 @@ void JSStream::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - Local jsStreamString = - FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"); - t->SetClassName(jsStreamString); t->InstanceTemplate() ->SetInternalFieldCount(StreamBase::kInternalFieldCount); t->Inherit(AsyncWrap::GetConstructorTemplate(env)); @@ -213,9 +209,7 @@ void JSStream::Initialize(Local target, env->SetProtoMethod(t, "emitEOF", EmitEOF); StreamBase::AddMethods(env, t); - target->Set(env->context(), - jsStreamString, - t->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "JSStream", t); } } // namespace node diff --git a/src/js_udp_wrap.cc b/src/js_udp_wrap.cc index c51683141186f0..6a9bda5cad1ecb 100644 --- a/src/js_udp_wrap.cc +++ b/src/js_udp_wrap.cc @@ -16,7 +16,6 @@ using v8::HandleScope; using v8::Int32; using v8::Local; using v8::Object; -using v8::String; using v8::Value; // JSUDPWrap is a testing utility used by test/common/udppair.js @@ -195,9 +194,6 @@ void JSUDPWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - Local js_udp_wrap_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "JSUDPWrap"); - t->SetClassName(js_udp_wrap_string); t->InstanceTemplate() ->SetInternalFieldCount(UDPWrapBase::kUDPWrapBaseField + 1); t->Inherit(AsyncWrap::GetConstructorTemplate(env)); @@ -207,9 +203,7 @@ void JSUDPWrap::Initialize(Local target, env->SetProtoMethod(t, "onSendDone", OnSendDone); env->SetProtoMethod(t, "onAfterBind", OnAfterBind); - target->Set(env->context(), - js_udp_wrap_string, - t->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "JSUDPWrap", t); } diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 9302fa6f68d837..0ac36d4aa6373f 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -722,10 +722,8 @@ void ModuleWrap::Initialize(Local target, Local context, void* priv) { Environment* env = Environment::GetCurrent(context); - Isolate* isolate = env->isolate(); Local tpl = env->NewFunctionTemplate(New); - tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap")); tpl->InstanceTemplate()->SetInternalFieldCount( ModuleWrap::kInternalFieldCount); tpl->Inherit(BaseObject::GetConstructorTemplate(env)); @@ -741,8 +739,8 @@ void ModuleWrap::Initialize(Local target, env->SetProtoMethodNoSideEffect(tpl, "getStaticDependencySpecifiers", GetStaticDependencySpecifiers); - target->Set(env->context(), FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), - tpl->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "ModuleWrap", tpl); + env->SetMethod(target, "setImportModuleDynamicallyCallback", SetImportModuleDynamicallyCallback); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 0accd99c0c4c5e..a0acdb75eede98 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1282,21 +1282,11 @@ void MicrotaskQueueWrap::New(const FunctionCallbackInfo& args) { void MicrotaskQueueWrap::Init(Environment* env, Local target) { HandleScope scope(env->isolate()); - Local class_name = - FIXED_ONE_BYTE_STRING(env->isolate(), "MicrotaskQueue"); - Local tmpl = env->NewFunctionTemplate(New); tmpl->InstanceTemplate()->SetInternalFieldCount( ContextifyScript::kInternalFieldCount); - tmpl->SetClassName(class_name); - - if (target->Set(env->context(), - class_name, - tmpl->GetFunction(env->context()).ToLocalChecked()) - .IsNothing()) { - return; - } env->set_microtask_queue_ctor_template(tmpl); + env->SetConstructorFunction(target, "MicrotaskQueue", tmpl); } diff --git a/src/node_dir.cc b/src/node_dir.cc index ac5739b99325c5..a8bb2a7083c4fc 100644 --- a/src/node_dir.cc +++ b/src/node_dir.cc @@ -39,7 +39,6 @@ using v8::Null; using v8::Number; using v8::Object; using v8::ObjectTemplate; -using v8::String; using v8::Value; #define TRACE_NAME(name) "fs_dir.sync." #name @@ -349,7 +348,6 @@ void Initialize(Local target, Local context, void* priv) { Environment* env = Environment::GetCurrent(context); - Isolate* isolate = env->isolate(); env->SetMethod(target, "opendir", OpenDir); @@ -360,13 +358,7 @@ void Initialize(Local target, env->SetProtoMethod(dir, "close", DirHandle::Close); Local dirt = dir->InstanceTemplate(); dirt->SetInternalFieldCount(DirHandle::kInternalFieldCount); - Local handleString = - FIXED_ONE_BYTE_STRING(isolate, "DirHandle"); - dir->SetClassName(handleString); - target - ->Set(context, handleString, - dir->GetFunction(env->context()).ToLocalChecked()) - .FromJust(); + env->SetConstructorFunction(target, "DirHandle", dir); env->set_dir_instance_template(dirt); } diff --git a/src/node_file.cc b/src/node_file.cc index 077727e87b533a..3e9a3076fd8780 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2471,13 +2471,7 @@ void Initialize(Local target, fst->InstanceTemplate()->SetInternalFieldCount( FSReqBase::kInternalFieldCount); fst->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local wrapString = - FIXED_ONE_BYTE_STRING(isolate, "FSReqCallback"); - fst->SetClassName(wrapString); - target - ->Set(context, wrapString, - fst->GetFunction(env->context()).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "FSReqCallback", fst); // Create FunctionTemplate for FileHandleReadWrap. There’s no need // to do anything in the constructor, so we only store the instance template. @@ -2508,14 +2502,8 @@ void Initialize(Local target, env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD); Local fdt = fd->InstanceTemplate(); fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount); - Local handleString = - FIXED_ONE_BYTE_STRING(isolate, "FileHandle"); - fd->SetClassName(handleString); StreamBase::AddMethods(env, fd); - target - ->Set(context, handleString, - fd->GetFunction(env->context()).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "FileHandle", fd); env->set_fd_constructor_template(fdt); // Create FunctionTemplate for FileHandle::CloseReq diff --git a/src/node_http2.cc b/src/node_http2.cc index 1e2da918bf5b69..930167418e18db 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -3054,9 +3054,6 @@ void Initialize(Local target, env->SetMethod(target, "packSettings", PackSettings); env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions); - Local http2SessionClassName = - FIXED_ONE_BYTE_STRING(isolate, "Http2Session"); - Local ping = FunctionTemplate::New(env->isolate()); ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping")); ping->Inherit(AsyncWrap::GetConstructorTemplate(env)); @@ -3065,14 +3062,12 @@ void Initialize(Local target, env->set_http2ping_constructor_template(pingt); Local setting = FunctionTemplate::New(env->isolate()); - setting->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Setting")); setting->Inherit(AsyncWrap::GetConstructorTemplate(env)); Local settingt = setting->InstanceTemplate(); settingt->SetInternalFieldCount(AsyncWrap::kInternalFieldCount); env->set_http2settings_constructor_template(settingt); Local stream = FunctionTemplate::New(env->isolate()); - stream->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream")); env->SetProtoMethod(stream, "id", Http2Stream::GetID); env->SetProtoMethod(stream, "destroy", Http2Stream::Destroy); env->SetProtoMethod(stream, "priority", Http2Stream::Priority); @@ -3087,13 +3082,10 @@ void Initialize(Local target, Local streamt = stream->InstanceTemplate(); streamt->SetInternalFieldCount(StreamBase::kInternalFieldCount); env->set_http2stream_constructor_template(streamt); - target->Set(context, - FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"), - stream->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Http2Stream", stream); Local session = env->NewFunctionTemplate(Http2Session::New); - session->SetClassName(http2SessionClassName); session->InstanceTemplate()->SetInternalFieldCount( Http2Session::kInternalFieldCount); session->Inherit(AsyncWrap::GetConstructorTemplate(env)); @@ -3119,9 +3111,7 @@ void Initialize(Local target, env->SetProtoMethod( session, "remoteSettings", Http2Session::RefreshSettings); - target->Set(context, - http2SessionClassName, - session->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Http2Session", session); Local constants = Object::New(isolate); diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 706e6132db6212..affc66585ed89a 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -956,7 +956,6 @@ void InitializeHttpParser(Local target, Local t = env->NewFunctionTemplate(Parser::New); t->InstanceTemplate()->SetInternalFieldCount(Parser::kInternalFieldCount); - t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser")); t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "REQUEST"), Integer::New(env->isolate(), HTTP_REQUEST)); @@ -999,9 +998,7 @@ void InitializeHttpParser(Local target, env->SetProtoMethod(t, "unconsume", Parser::Unconsume); env->SetProtoMethod(t, "getCurrentBuffer", Parser::GetCurrentBuffer); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"), - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "HTTPParser", t); } } // anonymous namespace diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 5f054c41c8eb04..78fb46ab6f2803 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1415,32 +1415,24 @@ static void InitMessaging(Local target, Environment* env = Environment::GetCurrent(context); { - Local message_channel_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "MessageChannel"); - Local templ = env->NewFunctionTemplate(MessageChannel); - templ->SetClassName(message_channel_string); - target->Set(context, - message_channel_string, - templ->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction( + target, + "MessageChannel", + env->NewFunctionTemplate(MessageChannel)); } { - Local js_transferable_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "JSTransferable"); Local t = env->NewFunctionTemplate(JSTransferable::New); t->Inherit(BaseObject::GetConstructorTemplate(env)); - t->SetClassName(js_transferable_string); t->InstanceTemplate()->SetInternalFieldCount( JSTransferable::kInternalFieldCount); - target->Set(context, - js_transferable_string, - t->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "JSTransferable", t); } - target->Set(context, - env->message_port_constructor_string(), - GetMessagePortConstructorTemplate(env) - ->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction( + target, + env->message_port_constructor_string(), + GetMessagePortConstructorTemplate(env)); // These are not methods on the MessagePort prototype, because // the browser equivalents do not provide them. diff --git a/src/node_perf.cc b/src/node_perf.cc index 1eddb00f48a6d2..4d977d4ba61789 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -705,8 +705,7 @@ void Initialize(Local target, env->SetProtoMethod(eldh, "enable", ELDHistogramEnable); env->SetProtoMethod(eldh, "disable", ELDHistogramDisable); env->SetProtoMethod(eldh, "reset", ELDHistogramReset); - target->Set(context, eldh_classname, - eldh->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, eldh_classname, eldh); } } // namespace performance diff --git a/src/node_serdes.cc b/src/node_serdes.cc index b51d315989ce71..879253f9bc2ebc 100644 --- a/src/node_serdes.cc +++ b/src/node_serdes.cc @@ -475,13 +475,8 @@ void Initialize(Local target, "_setTreatArrayBufferViewsAsHostObjects", SerializerContext::SetTreatArrayBufferViewsAsHostObjects); - Local serializerString = - FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"); - ser->SetClassName(serializerString); ser->ReadOnlyPrototype(); - target->Set(env->context(), - serializerString, - ser->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Serializer", ser); Local des = env->NewFunctionTemplate(DeserializerContext::New); @@ -503,14 +498,9 @@ void Initialize(Local target, env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble); env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes); - Local deserializerString = - FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"); des->SetLength(1); des->ReadOnlyPrototype(); - des->SetClassName(deserializerString); - target->Set(env->context(), - deserializerString, - des->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Deserializer", des); } } // anonymous namespace diff --git a/src/node_sockaddr.cc b/src/node_sockaddr.cc index 8d7c93255b0d81..3734453314a087 100644 --- a/src/node_sockaddr.cc +++ b/src/node_sockaddr.cc @@ -18,7 +18,6 @@ using v8::FunctionTemplate; using v8::Local; using v8::MaybeLocal; using v8::Object; -using v8::String; using v8::Value; namespace { @@ -675,11 +674,9 @@ void SocketAddressBlockListWrap::Initialize( void* priv) { Environment* env = Environment::GetCurrent(context); - Local name = FIXED_ONE_BYTE_STRING(env->isolate(), "BlockList"); Local t = env->NewFunctionTemplate(SocketAddressBlockListWrap::New); t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount); - t->SetClassName(name); env->SetProtoMethod(t, "addAddress", SocketAddressBlockListWrap::AddAddress); env->SetProtoMethod(t, "addRange", SocketAddressBlockListWrap::AddRange); @@ -688,8 +685,7 @@ void SocketAddressBlockListWrap::Initialize( env->SetProtoMethod(t, "getRules", SocketAddressBlockListWrap::GetRules); env->set_blocklist_instance_template(t->InstanceTemplate()); - target->Set(env->context(), name, - t->GetFunction(env->context()).ToLocalChecked()).FromJust(); + env->SetConstructorFunction(target, "BlockList", t); NODE_DEFINE_CONSTANT(target, AF_INET); NODE_DEFINE_CONSTANT(target, AF_INET6); diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 70903525baa735..344ea6bb7ea2e6 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -38,7 +38,6 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; -using v8::String; using v8::Uint32; using v8::Value; @@ -49,15 +48,11 @@ void StatWatcher::Initialize(Environment* env, Local target) { Local t = env->NewFunctionTemplate(StatWatcher::New); t->InstanceTemplate()->SetInternalFieldCount( StatWatcher::kInternalFieldCount); - Local statWatcherString = - FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"); - t->SetClassName(statWatcherString); t->Inherit(HandleWrap::GetConstructorTemplate(env)); env->SetProtoMethod(t, "start", StatWatcher::Start); - target->Set(env->context(), statWatcherString, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "StatWatcher", t); } diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index 9cefaa9227031d..af60aff4ab7bbe 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -138,10 +138,7 @@ void NodeCategorySet::Initialize(Local target, env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable); env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "CategorySet"), - category_set->GetFunction(env->context()).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "CategorySet", category_set); Local isTraceCategoryEnabled = FIXED_ONE_BYTE_STRING(env->isolate(), "isTraceCategoryEnabled"); diff --git a/src/node_util.cc b/src/node_util.cc index 3eefa73739aa3c..3f829081cb37ff 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -352,19 +352,15 @@ void Initialize(Local target, env->should_abort_on_uncaught_toggle().GetJSArray()) .FromJust()); - Local weak_ref_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference"); Local weak_ref = env->NewFunctionTemplate(WeakReference::New); weak_ref->InstanceTemplate()->SetInternalFieldCount( WeakReference::kInternalFieldCount); - weak_ref->SetClassName(weak_ref_string); weak_ref->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(weak_ref, "get", WeakReference::Get); env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef); env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef); - target->Set(context, weak_ref_string, - weak_ref->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "WeakReference", weak_ref); env->SetMethod(target, "guessHandleType", GuessHandleType); } diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 4dd534af4167ee..67d3966e2013a1 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1676,9 +1676,7 @@ static void Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local tmpl = env->NewFunctionTemplate(WASI::New); - auto wasi_wrap_string = FIXED_ONE_BYTE_STRING(env->isolate(), "WASI"); tmpl->InstanceTemplate()->SetInternalFieldCount(WASI::kInternalFieldCount); - tmpl->SetClassName(wasi_wrap_string); tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(tmpl, "args_get", WASI::ArgsGet); @@ -1731,9 +1729,7 @@ static void Initialize(Local target, env->SetInstanceMethod(tmpl, "_setMemory", WASI::_SetMemory); - target->Set(env->context(), - wasi_wrap_string, - tmpl->GetFunction(context).ToLocalChecked()).ToChecked(); + env->SetConstructorFunction(target, "WASI", tmpl); } diff --git a/src/node_watchdog.cc b/src/node_watchdog.cc index 8bd3b283b5329d..ff2a0229087138 100644 --- a/src/node_watchdog.cc +++ b/src/node_watchdog.cc @@ -124,19 +124,12 @@ void TraceSigintWatchdog::Init(Environment* env, Local target) { Local constructor = env->NewFunctionTemplate(New); constructor->InstanceTemplate()->SetInternalFieldCount( TraceSigintWatchdog::kInternalFieldCount); - Local js_sigint_watch_dog = - FIXED_ONE_BYTE_STRING(env->isolate(), "TraceSigintWatchdog"); - constructor->SetClassName(js_sigint_watch_dog); constructor->Inherit(HandleWrap::GetConstructorTemplate(env)); env->SetProtoMethod(constructor, "start", Start); env->SetProtoMethod(constructor, "stop", Stop); - target - ->Set(env->context(), - js_sigint_watch_dog, - constructor->GetFunction(env->context()).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "TraceSigintWatchdog", constructor); } void TraceSigintWatchdog::New(const FunctionCallbackInfo& args) { diff --git a/src/node_worker.cc b/src/node_worker.cc index 7369e13768e2d9..d163ec2461da07 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -815,12 +815,7 @@ void InitWorker(Local target, env->SetProtoMethod(w, "loopIdleTime", Worker::LoopIdleTime); env->SetProtoMethod(w, "loopStartTime", Worker::LoopStartTime); - Local workerString = - FIXED_ONE_BYTE_STRING(env->isolate(), "Worker"); - w->SetClassName(workerString); - target->Set(env->context(), - workerString, - w->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Worker", w); } { diff --git a/src/node_zlib.cc b/src/node_zlib.cc index efb11debf8f40d..2a2466052c92a5 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -54,7 +54,6 @@ using v8::Int32; using v8::Integer; using v8::Local; using v8::Object; -using v8::String; using v8::Uint32Array; using v8::Value; @@ -1262,11 +1261,7 @@ struct MakeClass { env->SetProtoMethod(z, "params", Stream::Params); env->SetProtoMethod(z, "reset", Stream::Reset); - Local zlibString = OneByteString(env->isolate(), name); - z->SetClassName(zlibString); - target->Set(env->context(), - zlibString, - z->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, name, z); } }; diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 1396395463dfed..7ec3c66a78bb95 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -43,7 +43,6 @@ using v8::Int32; using v8::Local; using v8::MaybeLocal; using v8::Object; -using v8::String; using v8::Value; MaybeLocal PipeWrap::Instantiate(Environment* env, @@ -69,8 +68,6 @@ void PipeWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - Local pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"); - t->SetClassName(pipeString); t->InstanceTemplate() ->SetInternalFieldCount(StreamBase::kInternalFieldCount); @@ -87,20 +84,13 @@ void PipeWrap::Initialize(Local target, env->SetProtoMethod(t, "fchmod", Fchmod); - target->Set(env->context(), - pipeString, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Pipe", t); env->set_pipe_constructor_template(t); // Create FunctionTemplate for PipeConnectWrap. auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env); cwt->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local wrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"); - cwt->SetClassName(wrapString); - target->Set(env->context(), - wrapString, - cwt->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "PipeConnectWrap", cwt); // Define constants Local constants = Object::New(env->isolate()); diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 3d1065c9922183..7628a1264c57f4 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -54,18 +54,13 @@ class ProcessWrap : public HandleWrap { Local constructor = env->NewFunctionTemplate(New); constructor->InstanceTemplate()->SetInternalFieldCount( ProcessWrap::kInternalFieldCount); - Local processString = - FIXED_ONE_BYTE_STRING(env->isolate(), "Process"); - constructor->SetClassName(processString); constructor->Inherit(HandleWrap::GetConstructorTemplate(env)); env->SetProtoMethod(constructor, "spawn", Spawn); env->SetProtoMethod(constructor, "kill", Kill); - target->Set(env->context(), - processString, - constructor->GetFunction(context).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "Process", constructor); } SET_NO_MEMORY_INFO() diff --git a/src/quic/node_quic_socket.cc b/src/quic/node_quic_socket.cc index abbdd50e47040b..810705014ca94d 100644 --- a/src/quic/node_quic_socket.cc +++ b/src/quic/node_quic_socket.cc @@ -39,7 +39,6 @@ using v8::Number; using v8::Object; using v8::ObjectTemplate; using v8::PropertyAttribute; -using v8::String; using v8::Value; namespace quic { @@ -1154,10 +1153,8 @@ void QuicEndpoint::Initialize( Local target, Local context) { Isolate* isolate = env->isolate(); - Local class_name = FIXED_ONE_BYTE_STRING(isolate, "QuicEndpoint"); Local endpoint = env->NewFunctionTemplate(NewQuicEndpoint); endpoint->Inherit(BaseObject::GetConstructorTemplate(env)); - endpoint->SetClassName(class_name); endpoint->InstanceTemplate()->SetInternalFieldCount( QuicEndpoint::kInternalFieldCount); env->SetProtoMethod(endpoint, @@ -1165,11 +1162,7 @@ void QuicEndpoint::Initialize( QuicEndpointWaitForPendingCallbacks); endpoint->InstanceTemplate()->Set(env->owner_symbol(), Null(isolate)); - target->Set( - context, - class_name, - endpoint->GetFunction(context).ToLocalChecked()) - .FromJust(); + env->SetConstructorFunction(target, "QuicEndpoint", endpoint); } void QuicSocket::Initialize( @@ -1177,10 +1170,8 @@ void QuicSocket::Initialize( Local target, Local context) { Isolate* isolate = env->isolate(); - Local class_name = FIXED_ONE_BYTE_STRING(isolate, "QuicSocket"); Local socket = env->NewFunctionTemplate(NewQuicSocket); socket->Inherit(AsyncWrap::GetConstructorTemplate(env)); - socket->SetClassName(class_name); socket->InstanceTemplate()->SetInternalFieldCount( QuicSocket::kInternalFieldCount); socket->InstanceTemplate()->Set(env->owner_symbol(), Null(isolate)); @@ -1197,8 +1188,7 @@ void QuicSocket::Initialize( "setDiagnosticPacketLoss", QuicSocketSetDiagnosticPacketLoss); socket->Inherit(HandleWrap::GetConstructorTemplate(env)); - target->Set(context, class_name, - socket->GetFunction(env->context()).ToLocalChecked()).FromJust(); + env->SetConstructorFunction(target, "QuicSocket", socket); Local sendwrap_ctor = FunctionTemplate::New(isolate); sendwrap_ctor->Inherit(AsyncWrap::GetConstructorTemplate(env)); diff --git a/src/quic/node_quic_stream.cc b/src/quic/node_quic_stream.cc index d63e66988ac2ac..57976ae50d9898 100644 --- a/src/quic/node_quic_stream.cc +++ b/src/quic/node_quic_stream.cc @@ -26,12 +26,10 @@ using v8::Array; using v8::Context; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; -using v8::Isolate; using v8::Local; using v8::Object; using v8::ObjectTemplate; using v8::PropertyAttribute; -using v8::String; using v8::Value; namespace quic { @@ -525,10 +523,7 @@ void QuicStream::Initialize( Environment* env, Local target, Local context) { - Isolate* isolate = env->isolate(); - Local class_name = FIXED_ONE_BYTE_STRING(isolate, "QuicStream"); Local stream = FunctionTemplate::New(env->isolate()); - stream->SetClassName(class_name); stream->Inherit(AsyncWrap::GetConstructorTemplate(env)); StreamBase::AddMethods(env, stream); Local streamt = stream->InstanceTemplate(); @@ -543,9 +538,7 @@ void QuicStream::Initialize( env->SetProtoMethod(stream, "submitTrailers", QuicStreamSubmitTrailers); env->SetProtoMethod(stream, "submitPush", QuicStreamSubmitPush); env->set_quicserverstream_instance_template(streamt); - target->Set(env->context(), - class_name, - stream->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "QuicStream", stream); env->SetMethod(target, "openBidirectionalStream", OpenBidirectionalStream); env->SetMethod(target, "openUnidirectionalStream", OpenUnidirectionalStream); diff --git a/src/signal_wrap.cc b/src/signal_wrap.cc index 2be7ac9834100b..49ea849f637cf7 100644 --- a/src/signal_wrap.cc +++ b/src/signal_wrap.cc @@ -35,7 +35,6 @@ using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; -using v8::String; using v8::Value; void DecreaseSignalHandlerCount(int signum); @@ -55,17 +54,12 @@ class SignalWrap : public HandleWrap { Local constructor = env->NewFunctionTemplate(New); constructor->InstanceTemplate()->SetInternalFieldCount( SignalWrap::kInternalFieldCount); - Local signalString = - FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"); - constructor->SetClassName(signalString); constructor->Inherit(HandleWrap::GetConstructorTemplate(env)); env->SetProtoMethod(constructor, "start", Start); env->SetProtoMethod(constructor, "stop", Stop); - target->Set(env->context(), signalString, - constructor->GetFunction(env->context()).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "Signal", constructor); } SET_NO_MEMORY_INFO() diff --git a/src/stream_pipe.cc b/src/stream_pipe.cc index 1422f9e0ea548e..afd7ec36eef294 100644 --- a/src/stream_pipe.cc +++ b/src/stream_pipe.cc @@ -13,7 +13,6 @@ using v8::FunctionTemplate; using v8::HandleScope; using v8::Local; using v8::Object; -using v8::String; using v8::Value; StreamPipe::StreamPipe(StreamBase* source, @@ -293,20 +292,14 @@ void InitializeStreamPipe(Local target, // Create FunctionTemplate for FileHandle::CloseReq Local pipe = env->NewFunctionTemplate(StreamPipe::New); - Local stream_pipe_string = - FIXED_ONE_BYTE_STRING(env->isolate(), "StreamPipe"); env->SetProtoMethod(pipe, "unpipe", StreamPipe::Unpipe); env->SetProtoMethod(pipe, "start", StreamPipe::Start); env->SetProtoMethod(pipe, "isClosed", StreamPipe::IsClosed); env->SetProtoMethod(pipe, "pendingWrites", StreamPipe::PendingWrites); pipe->Inherit(AsyncWrap::GetConstructorTemplate(env)); - pipe->SetClassName(stream_pipe_string); pipe->InstanceTemplate()->SetInternalFieldCount( StreamPipe::kInternalFieldCount); - target - ->Set(context, stream_pipe_string, - pipe->GetFunction(context).ToLocalChecked()) - .Check(); + env->SetConstructorFunction(target, "StreamPipe", pipe); } } // anonymous namespace diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index bd396110fccade..a1fa5e94b73711 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -49,7 +49,6 @@ using v8::Object; using v8::PropertyAttribute; using v8::ReadOnly; using v8::Signature; -using v8::String; using v8::Value; @@ -67,9 +66,6 @@ void LibuvStreamWrap::Initialize(Local target, Local sw = FunctionTemplate::New(env->isolate(), is_construct_call_callback); sw->InstanceTemplate()->SetInternalFieldCount(StreamReq::kInternalFieldCount); - Local wrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"); - sw->SetClassName(wrapString); // we need to set handle and callback to null, // so that those fields are created and functions @@ -88,22 +84,15 @@ void LibuvStreamWrap::Initialize(Local target, sw->Inherit(AsyncWrap::GetConstructorTemplate(env)); - target->Set(env->context(), - wrapString, - sw->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "ShutdownWrap", sw); env->set_shutdown_wrap_template(sw->InstanceTemplate()); Local ww = FunctionTemplate::New(env->isolate(), is_construct_call_callback); ww->InstanceTemplate()->SetInternalFieldCount( StreamReq::kInternalFieldCount); - Local writeWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"); - ww->SetClassName(writeWrapString); ww->Inherit(AsyncWrap::GetConstructorTemplate(env)); - target->Set(env->context(), - writeWrapString, - ww->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "WriteWrap", ww); env->set_write_wrap_template(ww->InstanceTemplate()); NODE_DEFINE_CONSTANT(target, kReadBytesOrError); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index fa45bd118d4724..cd7174984e2e36 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -74,8 +74,6 @@ void TCPWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Local t = env->NewFunctionTemplate(New); - Local tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"); - t->SetClassName(tcpString); t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount); // Init properties @@ -103,21 +101,14 @@ void TCPWrap::Initialize(Local target, env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts); #endif - target->Set(env->context(), - tcpString, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "TCP", t); env->set_tcp_constructor_template(t); // Create FunctionTemplate for TCPConnectWrap. Local cwt = BaseObject::MakeLazilyInitializedJSTemplate(env); cwt->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local wrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"); - cwt->SetClassName(wrapString); - target->Set(env->context(), - wrapString, - cwt->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "TCPConnectWrap", cwt); // Define constants Local constants = Object::New(env->isolate()); diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 26bafebcb22c14..e746f62c5edd81 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -43,7 +43,6 @@ using v8::Object; using v8::PropertyAttribute; using v8::ReadOnly; using v8::Signature; -using v8::String; using v8::Uint32; using v8::Undefined; using v8::Value; @@ -134,9 +133,6 @@ void UDPWrap::Initialize(Local target, Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( UDPWrapBase::kInternalFieldCount); - Local udpString = - FIXED_ONE_BYTE_STRING(env->isolate(), "UDP"); - t->SetClassName(udpString); enum PropertyAttribute attributes = static_cast(ReadOnly | DontDelete); @@ -182,9 +178,7 @@ void UDPWrap::Initialize(Local target, t->Inherit(HandleWrap::GetConstructorTemplate(env)); - target->Set(env->context(), - udpString, - t->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "UDP", t); env->set_udp_constructor_function( t->GetFunction(env->context()).ToLocalChecked()); @@ -192,12 +186,7 @@ void UDPWrap::Initialize(Local target, Local swt = BaseObject::MakeLazilyInitializedJSTemplate(env); swt->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local sendWrapString = - FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"); - swt->SetClassName(sendWrapString); - target->Set(env->context(), - sendWrapString, - swt->GetFunction(env->context()).ToLocalChecked()).Check(); + env->SetConstructorFunction(target, "SendWrap", swt); Local constants = Object::New(env->isolate()); NODE_DEFINE_CONSTANT(constants, UV_UDP_IPV6ONLY); diff --git a/src/uv.cc b/src/uv.cc index bf50c88111fa90..e4b428f339b578 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -106,11 +106,10 @@ void Initialize(Local target, void* priv) { Environment* env = Environment::GetCurrent(context); Isolate* isolate = env->isolate(); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(isolate, "errname"), - env->NewFunctionTemplate(ErrName) - ->GetFunction(env->context()) - .ToLocalChecked()).Check(); + env->SetConstructorFunction( + target, + "errname", + env->NewFunctionTemplate(ErrName)); // TODO(joyeecheung): This should be deprecated in user land in favor of // `util.getSystemErrorName(err)`.