From d73181f243350ce803cc1fb9976e71b368480c6a 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/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/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 ++++----- 33 files changed, 82 insertions(+), 258 deletions(-) diff --git a/src/README.md b/src/README.md index dd9a9df449f147..b278c0287366cf 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 83308dbfafb2d1..c11eb3880d8256 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1898,32 +1898,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); @@ -1950,11 +1935,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); } } // namespace cares_wrap diff --git a/src/env-inl.h b/src/env-inl.h index ae5d231afb4776..c78ebd2a542733 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -1107,6 +1107,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 ebc0002bdafd9d..f4c0e7d90cca96 100644 --- a/src/env.h +++ b/src/env.h @@ -1111,6 +1111,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 f1eb5fe7fd7448..c2d59effe0eebd 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -101,19 +101,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 1da35f9f2fdaa3..b80e23327e818e 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -733,10 +733,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)); @@ -752,8 +750,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 2a2ee49494ff53..c17018be6e32bf 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1292,21 +1292,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 a6800a8350d766..9275aac17d6b33 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 2a7b6dfcec7779..118796a48df7c8 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2472,13 +2472,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. @@ -2509,14 +2503,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 d9e3a179f60a4c..4ac84d275d8ec5 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -3070,9 +3070,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)); @@ -3081,14 +3078,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); @@ -3103,13 +3098,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)); @@ -3135,9 +3127,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 cb44320c380e02..d3184bb1a14c92 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 24b0c48bd76a02..b743ec72b45ee2 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1316,32 +1316,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 546cbfc911485e..aa6db069fa0c26 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -660,8 +660,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 0cd3e005953b61..835179d511417a 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 58d03eefa6e6e6..0921f30bfe710e 100644 --- a/src/node_sockaddr.cc +++ b/src/node_sockaddr.cc @@ -17,7 +17,6 @@ using v8::FunctionTemplate; using v8::Local; using v8::MaybeLocal; using v8::Object; -using v8::String; using v8::Value; namespace { @@ -674,11 +673,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); @@ -687,8 +684,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 58813a9083a560..b36a5fc0bf774a 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -135,10 +135,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 eac09f8d44fcbd..7ea8cf8fe6bfc8 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -333,19 +333,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 52e85c38be1660..ffcc37c5274c98 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1678,9 +1678,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); @@ -1733,9 +1731,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 8058c4e9caf3da..1f9531e930fca4 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -843,12 +843,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 bbb00153f7a8a8..45920c2603b179 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/signal_wrap.cc b/src/signal_wrap.cc index ac7be821ea4fb3..e8a1500d2e9961 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 a2cb185551800b..203eb1e9415926 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 dbec7e505e2d71..009136cef6bac5 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -108,11 +108,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)`.