Skip to content

Commit d73181f

Browse files
jasnellMylesBorins
authored andcommitted
src: reduce duplicated boilerplate with new env utility fn
Signed-off-by: James M Snell <[email protected]> PR-URL: #36536 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 9aba288 commit d73181f

33 files changed

+82
-258
lines changed

src/README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,7 @@ void Initialize(Local<Object> target,
405405

406406
env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers);
407407

408-
Local<String> channel_wrap_string =
409-
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
410-
channel_wrap->SetClassName(channel_wrap_string);
411-
target->Set(env->context(), channel_wrap_string,
412-
channel_wrap->GetFunction(context).ToLocalChecked()).Check();
408+
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
413409
}
414410

415411
// Run the `Initialize` function when loading this module through

src/cares_wrap.cc

+4-23
Original file line numberDiff line numberDiff line change
@@ -1898,32 +1898,17 @@ void Initialize(Local<Object> target,
18981898
Local<FunctionTemplate> aiw =
18991899
BaseObject::MakeLazilyInitializedJSTemplate(env);
19001900
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
1901-
Local<String> addrInfoWrapString =
1902-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
1903-
aiw->SetClassName(addrInfoWrapString);
1904-
target->Set(env->context(),
1905-
addrInfoWrapString,
1906-
aiw->GetFunction(context).ToLocalChecked()).Check();
1901+
env->SetConstructorFunction(target, "GetAddrInfoReqWrap", aiw);
19071902

19081903
Local<FunctionTemplate> niw =
19091904
BaseObject::MakeLazilyInitializedJSTemplate(env);
19101905
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
1911-
Local<String> nameInfoWrapString =
1912-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
1913-
niw->SetClassName(nameInfoWrapString);
1914-
target->Set(env->context(),
1915-
nameInfoWrapString,
1916-
niw->GetFunction(context).ToLocalChecked()).Check();
1906+
env->SetConstructorFunction(target, "GetNameInfoReqWrap", niw);
19171907

19181908
Local<FunctionTemplate> qrw =
19191909
BaseObject::MakeLazilyInitializedJSTemplate(env);
19201910
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
1921-
Local<String> queryWrapString =
1922-
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
1923-
qrw->SetClassName(queryWrapString);
1924-
target->Set(env->context(),
1925-
queryWrapString,
1926-
qrw->GetFunction(context).ToLocalChecked()).Check();
1911+
env->SetConstructorFunction(target, "QueryReqWrap", qrw);
19271912

19281913
Local<FunctionTemplate> channel_wrap =
19291914
env->NewFunctionTemplate(ChannelWrap::New);
@@ -1950,11 +1935,7 @@ void Initialize(Local<Object> target,
19501935
env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress);
19511936
env->SetProtoMethod(channel_wrap, "cancel", Cancel);
19521937

1953-
Local<String> channelWrapString =
1954-
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
1955-
channel_wrap->SetClassName(channelWrapString);
1956-
target->Set(env->context(), channelWrapString,
1957-
channel_wrap->GetFunction(context).ToLocalChecked()).Check();
1938+
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
19581939
}
19591940

19601941
} // namespace cares_wrap

src/env-inl.h

+18
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,24 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
11071107
t->SetClassName(name_string);
11081108
}
11091109

1110+
inline void Environment::SetConstructorFunction(
1111+
v8::Local<v8::Object> that,
1112+
const char* name,
1113+
v8::Local<v8::FunctionTemplate> tmpl) {
1114+
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
1115+
}
1116+
1117+
inline void Environment::SetConstructorFunction(
1118+
v8::Local<v8::Object> that,
1119+
v8::Local<v8::String> name,
1120+
v8::Local<v8::FunctionTemplate> tmpl) {
1121+
tmpl->SetClassName(name);
1122+
that->Set(
1123+
context(),
1124+
name,
1125+
tmpl->GetFunction(context()).ToLocalChecked()).Check();
1126+
}
1127+
11101128
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
11111129
auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
11121130
fn, arg, cleanup_hook_counter_++

src/env.h

+8
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,14 @@ class Environment : public MemoryRetainer {
11111111
const char* name,
11121112
v8::FunctionCallback callback);
11131113

1114+
inline void SetConstructorFunction(v8::Local<v8::Object> that,
1115+
const char* name,
1116+
v8::Local<v8::FunctionTemplate> tmpl);
1117+
1118+
inline void SetConstructorFunction(v8::Local<v8::Object> that,
1119+
v8::Local<v8::String> name,
1120+
v8::Local<v8::FunctionTemplate> tmpl);
1121+
11141122
void AtExit(void (*cb)(void* arg), void* arg);
11151123
void RunAtExitCallbacks();
11161124

src/fs_event_wrap.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ void FSEventWrap::Initialize(Local<Object> target,
9595
void* priv) {
9696
Environment* env = Environment::GetCurrent(context);
9797

98-
auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent");
9998
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
10099
t->InstanceTemplate()->SetInternalFieldCount(
101100
FSEventWrap::kInternalFieldCount);
102-
t->SetClassName(fsevent_string);
103101

104102
t->Inherit(HandleWrap::GetConstructorTemplate(env));
105103
env->SetProtoMethod(t, "start", Start);
@@ -116,9 +114,7 @@ void FSEventWrap::Initialize(Local<Object> target,
116114
Local<FunctionTemplate>(),
117115
static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum));
118116

119-
target->Set(env->context(),
120-
fsevent_string,
121-
t->GetFunction(context).ToLocalChecked()).Check();
117+
env->SetConstructorFunction(target, "FSEvent", t);
122118
}
123119

124120

src/inspector_js_api.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,17 @@ class JSBindingsConnection : public AsyncWrap {
101101
}
102102

103103
static void Bind(Environment* env, Local<Object> target) {
104-
Local<String> class_name = ConnectionType::GetClassName(env);
105104
Local<FunctionTemplate> tmpl =
106105
env->NewFunctionTemplate(JSBindingsConnection::New);
107106
tmpl->InstanceTemplate()->SetInternalFieldCount(
108107
JSBindingsConnection::kInternalFieldCount);
109-
tmpl->SetClassName(class_name);
110108
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
111109
env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
112110
env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
113-
target->Set(env->context(),
114-
class_name,
115-
tmpl->GetFunction(env->context()).ToLocalChecked())
116-
.ToChecked();
111+
env->SetConstructorFunction(
112+
target,
113+
ConnectionType::GetClassName(env),
114+
tmpl);
117115
}
118116

119117
static void New(const FunctionCallbackInfo<Value>& info) {

src/js_stream.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ using v8::HandleScope;
1919
using v8::Int32;
2020
using v8::Local;
2121
using v8::Object;
22-
using v8::String;
2322
using v8::Value;
2423

2524

@@ -200,9 +199,6 @@ void JSStream::Initialize(Local<Object> target,
200199
Environment* env = Environment::GetCurrent(context);
201200

202201
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
203-
Local<String> jsStreamString =
204-
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
205-
t->SetClassName(jsStreamString);
206202
t->InstanceTemplate()
207203
->SetInternalFieldCount(StreamBase::kInternalFieldCount);
208204
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
@@ -213,9 +209,7 @@ void JSStream::Initialize(Local<Object> target,
213209
env->SetProtoMethod(t, "emitEOF", EmitEOF);
214210

215211
StreamBase::AddMethods(env, t);
216-
target->Set(env->context(),
217-
jsStreamString,
218-
t->GetFunction(context).ToLocalChecked()).Check();
212+
env->SetConstructorFunction(target, "JSStream", t);
219213
}
220214

221215
} // namespace node

src/js_udp_wrap.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ using v8::HandleScope;
1616
using v8::Int32;
1717
using v8::Local;
1818
using v8::Object;
19-
using v8::String;
2019
using v8::Value;
2120

2221
// JSUDPWrap is a testing utility used by test/common/udppair.js
@@ -195,9 +194,6 @@ void JSUDPWrap::Initialize(Local<Object> target,
195194
Environment* env = Environment::GetCurrent(context);
196195

197196
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
198-
Local<String> js_udp_wrap_string =
199-
FIXED_ONE_BYTE_STRING(env->isolate(), "JSUDPWrap");
200-
t->SetClassName(js_udp_wrap_string);
201197
t->InstanceTemplate()
202198
->SetInternalFieldCount(UDPWrapBase::kUDPWrapBaseField + 1);
203199
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
@@ -207,9 +203,7 @@ void JSUDPWrap::Initialize(Local<Object> target,
207203
env->SetProtoMethod(t, "onSendDone", OnSendDone);
208204
env->SetProtoMethod(t, "onAfterBind", OnAfterBind);
209205

210-
target->Set(env->context(),
211-
js_udp_wrap_string,
212-
t->GetFunction(context).ToLocalChecked()).Check();
206+
env->SetConstructorFunction(target, "JSUDPWrap", t);
213207
}
214208

215209

src/module_wrap.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,8 @@ void ModuleWrap::Initialize(Local<Object> target,
733733
Local<Context> context,
734734
void* priv) {
735735
Environment* env = Environment::GetCurrent(context);
736-
Isolate* isolate = env->isolate();
737736

738737
Local<FunctionTemplate> tpl = env->NewFunctionTemplate(New);
739-
tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"));
740738
tpl->InstanceTemplate()->SetInternalFieldCount(
741739
ModuleWrap::kInternalFieldCount);
742740
tpl->Inherit(BaseObject::GetConstructorTemplate(env));
@@ -752,8 +750,8 @@ void ModuleWrap::Initialize(Local<Object> target,
752750
env->SetProtoMethodNoSideEffect(tpl, "getStaticDependencySpecifiers",
753751
GetStaticDependencySpecifiers);
754752

755-
target->Set(env->context(), FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"),
756-
tpl->GetFunction(context).ToLocalChecked()).Check();
753+
env->SetConstructorFunction(target, "ModuleWrap", tpl);
754+
757755
env->SetMethod(target,
758756
"setImportModuleDynamicallyCallback",
759757
SetImportModuleDynamicallyCallback);

src/node_contextify.cc

+1-11
Original file line numberDiff line numberDiff line change
@@ -1292,21 +1292,11 @@ void MicrotaskQueueWrap::New(const FunctionCallbackInfo<Value>& args) {
12921292

12931293
void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) {
12941294
HandleScope scope(env->isolate());
1295-
Local<String> class_name =
1296-
FIXED_ONE_BYTE_STRING(env->isolate(), "MicrotaskQueue");
1297-
12981295
Local<FunctionTemplate> tmpl = env->NewFunctionTemplate(New);
12991296
tmpl->InstanceTemplate()->SetInternalFieldCount(
13001297
ContextifyScript::kInternalFieldCount);
1301-
tmpl->SetClassName(class_name);
1302-
1303-
if (target->Set(env->context(),
1304-
class_name,
1305-
tmpl->GetFunction(env->context()).ToLocalChecked())
1306-
.IsNothing()) {
1307-
return;
1308-
}
13091298
env->set_microtask_queue_ctor_template(tmpl);
1299+
env->SetConstructorFunction(target, "MicrotaskQueue", tmpl);
13101300
}
13111301

13121302

src/node_dir.cc

+1-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ using v8::Null;
3939
using v8::Number;
4040
using v8::Object;
4141
using v8::ObjectTemplate;
42-
using v8::String;
4342
using v8::Value;
4443

4544
#define TRACE_NAME(name) "fs_dir.sync." #name
@@ -349,7 +348,6 @@ void Initialize(Local<Object> target,
349348
Local<Context> context,
350349
void* priv) {
351350
Environment* env = Environment::GetCurrent(context);
352-
Isolate* isolate = env->isolate();
353351

354352
env->SetMethod(target, "opendir", OpenDir);
355353

@@ -360,13 +358,7 @@ void Initialize(Local<Object> target,
360358
env->SetProtoMethod(dir, "close", DirHandle::Close);
361359
Local<ObjectTemplate> dirt = dir->InstanceTemplate();
362360
dirt->SetInternalFieldCount(DirHandle::kInternalFieldCount);
363-
Local<String> handleString =
364-
FIXED_ONE_BYTE_STRING(isolate, "DirHandle");
365-
dir->SetClassName(handleString);
366-
target
367-
->Set(context, handleString,
368-
dir->GetFunction(env->context()).ToLocalChecked())
369-
.FromJust();
361+
env->SetConstructorFunction(target, "DirHandle", dir);
370362
env->set_dir_instance_template(dirt);
371363
}
372364

src/node_file.cc

+2-14
Original file line numberDiff line numberDiff line change
@@ -2472,13 +2472,7 @@ void Initialize(Local<Object> target,
24722472
fst->InstanceTemplate()->SetInternalFieldCount(
24732473
FSReqBase::kInternalFieldCount);
24742474
fst->Inherit(AsyncWrap::GetConstructorTemplate(env));
2475-
Local<String> wrapString =
2476-
FIXED_ONE_BYTE_STRING(isolate, "FSReqCallback");
2477-
fst->SetClassName(wrapString);
2478-
target
2479-
->Set(context, wrapString,
2480-
fst->GetFunction(env->context()).ToLocalChecked())
2481-
.Check();
2475+
env->SetConstructorFunction(target, "FSReqCallback", fst);
24822476

24832477
// Create FunctionTemplate for FileHandleReadWrap. There’s no need
24842478
// to do anything in the constructor, so we only store the instance template.
@@ -2509,14 +2503,8 @@ void Initialize(Local<Object> target,
25092503
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
25102504
Local<ObjectTemplate> fdt = fd->InstanceTemplate();
25112505
fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
2512-
Local<String> handleString =
2513-
FIXED_ONE_BYTE_STRING(isolate, "FileHandle");
2514-
fd->SetClassName(handleString);
25152506
StreamBase::AddMethods(env, fd);
2516-
target
2517-
->Set(context, handleString,
2518-
fd->GetFunction(env->context()).ToLocalChecked())
2519-
.Check();
2507+
env->SetConstructorFunction(target, "FileHandle", fd);
25202508
env->set_fd_constructor_template(fdt);
25212509

25222510
// Create FunctionTemplate for FileHandle::CloseReq

src/node_http2.cc

+2-12
Original file line numberDiff line numberDiff line change
@@ -3070,9 +3070,6 @@ void Initialize(Local<Object> target,
30703070
env->SetMethod(target, "packSettings", PackSettings);
30713071
env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions);
30723072

3073-
Local<String> http2SessionClassName =
3074-
FIXED_ONE_BYTE_STRING(isolate, "Http2Session");
3075-
30763073
Local<FunctionTemplate> ping = FunctionTemplate::New(env->isolate());
30773074
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping"));
30783075
ping->Inherit(AsyncWrap::GetConstructorTemplate(env));
@@ -3081,14 +3078,12 @@ void Initialize(Local<Object> target,
30813078
env->set_http2ping_constructor_template(pingt);
30823079

30833080
Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate());
3084-
setting->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Setting"));
30853081
setting->Inherit(AsyncWrap::GetConstructorTemplate(env));
30863082
Local<ObjectTemplate> settingt = setting->InstanceTemplate();
30873083
settingt->SetInternalFieldCount(AsyncWrap::kInternalFieldCount);
30883084
env->set_http2settings_constructor_template(settingt);
30893085

30903086
Local<FunctionTemplate> stream = FunctionTemplate::New(env->isolate());
3091-
stream->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"));
30923087
env->SetProtoMethod(stream, "id", Http2Stream::GetID);
30933088
env->SetProtoMethod(stream, "destroy", Http2Stream::Destroy);
30943089
env->SetProtoMethod(stream, "priority", Http2Stream::Priority);
@@ -3103,13 +3098,10 @@ void Initialize(Local<Object> target,
31033098
Local<ObjectTemplate> streamt = stream->InstanceTemplate();
31043099
streamt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
31053100
env->set_http2stream_constructor_template(streamt);
3106-
target->Set(context,
3107-
FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"),
3108-
stream->GetFunction(env->context()).ToLocalChecked()).Check();
3101+
env->SetConstructorFunction(target, "Http2Stream", stream);
31093102

31103103
Local<FunctionTemplate> session =
31113104
env->NewFunctionTemplate(Http2Session::New);
3112-
session->SetClassName(http2SessionClassName);
31133105
session->InstanceTemplate()->SetInternalFieldCount(
31143106
Http2Session::kInternalFieldCount);
31153107
session->Inherit(AsyncWrap::GetConstructorTemplate(env));
@@ -3135,9 +3127,7 @@ void Initialize(Local<Object> target,
31353127
env->SetProtoMethod(
31363128
session, "remoteSettings",
31373129
Http2Session::RefreshSettings<nghttp2_session_get_remote_settings>);
3138-
target->Set(context,
3139-
http2SessionClassName,
3140-
session->GetFunction(env->context()).ToLocalChecked()).Check();
3130+
env->SetConstructorFunction(target, "Http2Session", session);
31413131

31423132
Local<Object> constants = Object::New(isolate);
31433133

src/node_http_parser.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,6 @@ void InitializeHttpParser(Local<Object> target,
956956

957957
Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New);
958958
t->InstanceTemplate()->SetInternalFieldCount(Parser::kInternalFieldCount);
959-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"));
960959

961960
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "REQUEST"),
962961
Integer::New(env->isolate(), HTTP_REQUEST));
@@ -999,9 +998,7 @@ void InitializeHttpParser(Local<Object> target,
999998
env->SetProtoMethod(t, "unconsume", Parser::Unconsume);
1000999
env->SetProtoMethod(t, "getCurrentBuffer", Parser::GetCurrentBuffer);
10011000

1002-
target->Set(env->context(),
1003-
FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"),
1004-
t->GetFunction(env->context()).ToLocalChecked()).Check();
1001+
env->SetConstructorFunction(target, "HTTPParser", t);
10051002
}
10061003

10071004
} // anonymous namespace

0 commit comments

Comments
 (0)