Skip to content

Commit 5a05dfe

Browse files
jasnellMylesBorins
authored andcommitted
src: fixup strings, reduce duplication
PR-URL: #14937 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1c3cb49 commit 5a05dfe

18 files changed

+104
-73
lines changed

src/cares_wrap.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -2167,28 +2167,28 @@ void Initialize(Local<Object> target,
21672167
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21682168
aiw->InstanceTemplate()->SetInternalFieldCount(1);
21692169
env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId);
2170-
aiw->SetClassName(
2171-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"));
2172-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"),
2173-
aiw->GetFunction());
2170+
Local<String> addrInfoWrapString =
2171+
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
2172+
aiw->SetClassName(addrInfoWrapString);
2173+
target->Set(addrInfoWrapString, aiw->GetFunction());
21742174

21752175
Local<FunctionTemplate> niw =
21762176
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21772177
niw->InstanceTemplate()->SetInternalFieldCount(1);
21782178
env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId);
2179-
niw->SetClassName(
2180-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"));
2181-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"),
2182-
niw->GetFunction());
2179+
Local<String> nameInfoWrapString =
2180+
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
2181+
niw->SetClassName(nameInfoWrapString);
2182+
target->Set(nameInfoWrapString, niw->GetFunction());
21832183

21842184
Local<FunctionTemplate> qrw =
21852185
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
21862186
qrw->InstanceTemplate()->SetInternalFieldCount(1);
21872187
env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId);
2188-
qrw->SetClassName(
2189-
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"));
2190-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"),
2191-
qrw->GetFunction());
2188+
Local<String> queryWrapString =
2189+
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
2190+
qrw->SetClassName(queryWrapString);
2191+
target->Set(queryWrapString, qrw->GetFunction());
21922192

21932193
Local<FunctionTemplate> channel_wrap =
21942194
env->NewFunctionTemplate(ChannelWrap::New);
@@ -2212,10 +2212,10 @@ void Initialize(Local<Object> target,
22122212
env->SetProtoMethod(channel_wrap, "setServers", SetServers);
22132213
env->SetProtoMethod(channel_wrap, "cancel", Cancel);
22142214

2215-
channel_wrap->SetClassName(
2216-
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"));
2217-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"),
2218-
channel_wrap->GetFunction());
2215+
Local<String> channelWrapString =
2216+
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
2217+
channel_wrap->SetClassName(channelWrapString);
2218+
target->Set(channelWrapString, channel_wrap->GetFunction());
22192219
}
22202220

22212221
} // anonymous namespace

src/js_stream.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using v8::HandleScope;
1818
using v8::Local;
1919
using v8::MaybeLocal;
2020
using v8::Object;
21+
using v8::String;
2122
using v8::Value;
2223

2324

@@ -212,7 +213,9 @@ void JSStream::Initialize(Local<Object> target,
212213
Environment* env = Environment::GetCurrent(context);
213214

214215
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
215-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"));
216+
Local<String> jsStreamString =
217+
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
218+
t->SetClassName(jsStreamString);
216219
t->InstanceTemplate()->SetInternalFieldCount(1);
217220

218221
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
@@ -226,8 +229,7 @@ void JSStream::Initialize(Local<Object> target,
226229
env->SetProtoMethod(t, "emitEOF", EmitEOF);
227230

228231
StreamBase::AddMethods<JSStream>(env, t, StreamBase::kFlagHasWritev);
229-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"),
230-
t->GetFunction());
232+
target->Set(jsStreamString, t->GetFunction());
231233
env->set_jsstream_constructor_template(t);
232234
}
233235

src/node_config.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static void InitConfig(Local<Object> target,
105105

106106
debugOptions->DefineOwnProperty(
107107
context,
108-
FIXED_ONE_BYTE_STRING(isolate, "port"),
108+
env->port_string(),
109109
Integer::New(isolate, debug_options.port()),
110110
ReadOnly).FromJust();
111111

src/node_crypto.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ bool EntropySource(unsigned char* buffer, size_t length) {
312312
void SecureContext::Initialize(Environment* env, Local<Object> target) {
313313
Local<FunctionTemplate> t = env->NewFunctionTemplate(SecureContext::New);
314314
t->InstanceTemplate()->SetInternalFieldCount(1);
315-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));
315+
Local<String> secureContextString =
316+
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
317+
t->SetClassName(secureContextString);
316318

317319
env->SetProtoMethod(t, "init", SecureContext::Init);
318320
env->SetProtoMethod(t, "setKey", SecureContext::SetKey);
@@ -359,8 +361,7 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
359361
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
360362
AccessorSignature::New(env->isolate(), t));
361363

362-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"),
363-
t->GetFunction());
364+
target->Set(secureContextString, t->GetFunction());
364365
env->set_secure_context_constructor_template(t);
365366
}
366367

src/node_file.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -1484,9 +1484,10 @@ void InitFs(Local<Object> target,
14841484
FunctionTemplate::New(env->isolate(), NewFSReqWrap);
14851485
fst->InstanceTemplate()->SetInternalFieldCount(1);
14861486
env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId);
1487-
fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"));
1488-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"),
1489-
fst->GetFunction());
1487+
Local<String> wrapString =
1488+
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap");
1489+
fst->SetClassName(wrapString);
1490+
target->Set(wrapString, fst->GetFunction());
14901491
}
14911492

14921493
} // end namespace node

src/node_http2.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,7 @@ void Initialize(Local<Object> target,
12061206
env->SetMethod(target, "nghttp2ErrorString", HttpErrorString);
12071207

12081208
Local<String> http2SessionClassName =
1209-
String::NewFromUtf8(isolate, "Http2Session",
1210-
v8::NewStringType::kInternalized).ToLocalChecked();
1209+
FIXED_ONE_BYTE_STRING(isolate, "Http2Session");
12111210

12121211
Local<FunctionTemplate> session =
12131212
env->NewFunctionTemplate(Http2Session::New);

src/node_serdes.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,11 @@ void InitializeSerdesBindings(Local<Object> target,
451451
"_setTreatArrayBufferViewsAsHostObjects",
452452
SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
453453

454-
ser->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"));
454+
Local<String> serializerString =
455+
FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
456+
ser->SetClassName(serializerString);
455457
target->Set(env->context(),
456-
FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"),
458+
serializerString,
457459
ser->GetFunction(env->context()).ToLocalChecked()).FromJust();
458460

459461
Local<FunctionTemplate> des =
@@ -474,9 +476,11 @@ void InitializeSerdesBindings(Local<Object> target,
474476
env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble);
475477
env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes);
476478

477-
des->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"));
479+
Local<String> deserializerString =
480+
FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
481+
des->SetClassName(deserializerString);
478482
target->Set(env->context(),
479-
FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"),
483+
deserializerString,
480484
des->GetFunction(env->context()).ToLocalChecked()).FromJust();
481485
}
482486

src/node_stat_watcher.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using v8::HandleScope;
3939
using v8::Integer;
4040
using v8::Local;
4141
using v8::Object;
42+
using v8::String;
4243
using v8::Value;
4344

4445

@@ -47,14 +48,15 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
4748

4849
Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
4950
t->InstanceTemplate()->SetInternalFieldCount(1);
50-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"));
51+
Local<String> statWatcherString =
52+
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
53+
t->SetClassName(statWatcherString);
5154

5255
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
5356
env->SetProtoMethod(t, "start", StatWatcher::Start);
5457
env->SetProtoMethod(t, "stop", StatWatcher::Stop);
5558

56-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"),
57-
t->GetFunction());
59+
target->Set(statWatcherString, t->GetFunction());
5860
}
5961

6062

src/node_zlib.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ using v8::Integer;
4848
using v8::Local;
4949
using v8::Number;
5050
using v8::Object;
51+
using v8::String;
5152
using v8::Value;
5253

5354
namespace {
@@ -691,8 +692,9 @@ void InitZlib(Local<Object> target,
691692
env->SetProtoMethod(z, "params", ZCtx::Params);
692693
env->SetProtoMethod(z, "reset", ZCtx::Reset);
693694

694-
z->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"));
695-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"), z->GetFunction());
695+
Local<String> zlibString = FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib");
696+
z->SetClassName(zlibString);
697+
target->Set(zlibString, z->GetFunction());
696698

697699
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
698700
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION));

src/pipe_wrap.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ using v8::FunctionTemplate;
4444
using v8::HandleScope;
4545
using v8::Local;
4646
using v8::Object;
47+
using v8::String;
4748
using v8::Value;
4849

4950
using AsyncHooks = Environment::AsyncHooks;
@@ -67,7 +68,8 @@ void PipeWrap::Initialize(Local<Object> target,
6768
Environment* env = Environment::GetCurrent(context);
6869

6970
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
70-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"));
71+
Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
72+
t->SetClassName(pipeString);
7173
t->InstanceTemplate()->SetInternalFieldCount(1);
7274

7375
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
@@ -92,7 +94,7 @@ void PipeWrap::Initialize(Local<Object> target,
9294
env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
9395
#endif
9496

95-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction());
97+
target->Set(pipeString, t->GetFunction());
9698
env->set_pipe_constructor_template(t);
9799

98100
// Create FunctionTemplate for PipeConnectWrap.
@@ -103,9 +105,10 @@ void PipeWrap::Initialize(Local<Object> target,
103105
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
104106
cwt->InstanceTemplate()->SetInternalFieldCount(1);
105107
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
106-
cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"));
107-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"),
108-
cwt->GetFunction());
108+
Local<String> wrapString =
109+
FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
110+
cwt->SetClassName(wrapString);
111+
target->Set(wrapString, cwt->GetFunction());
109112
}
110113

111114

src/process_wrap.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class ProcessWrap : public HandleWrap {
5353
Environment* env = Environment::GetCurrent(context);
5454
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
5555
constructor->InstanceTemplate()->SetInternalFieldCount(1);
56-
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"));
56+
Local<String> processString =
57+
FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
58+
constructor->SetClassName(processString);
5759

5860
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
5961

@@ -66,8 +68,7 @@ class ProcessWrap : public HandleWrap {
6668
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
6769
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
6870

69-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"),
70-
constructor->GetFunction());
71+
target->Set(processString, constructor->GetFunction());
7172
}
7273

7374
size_t self_size() const override { return sizeof(*this); }

src/signal_wrap.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ using v8::HandleScope;
3737
using v8::Integer;
3838
using v8::Local;
3939
using v8::Object;
40+
using v8::String;
4041
using v8::Value;
4142

4243
namespace {
@@ -49,7 +50,9 @@ class SignalWrap : public HandleWrap {
4950
Environment* env = Environment::GetCurrent(context);
5051
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
5152
constructor->InstanceTemplate()->SetInternalFieldCount(1);
52-
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
53+
Local<String> signalString =
54+
FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
55+
constructor->SetClassName(signalString);
5356

5457
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
5558
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
@@ -59,8 +62,7 @@ class SignalWrap : public HandleWrap {
5962
env->SetProtoMethod(constructor, "start", Start);
6063
env->SetProtoMethod(constructor, "stop", Stop);
6164

62-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
63-
constructor->GetFunction());
65+
target->Set(signalString, constructor->GetFunction());
6466
}
6567

6668
size_t self_size() const override { return sizeof(*this); }

src/stream_wrap.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,20 @@ void StreamWrap::Initialize(Local<Object> target,
6767
Local<FunctionTemplate> sw =
6868
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
6969
sw->InstanceTemplate()->SetInternalFieldCount(1);
70-
sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
70+
Local<String> wrapString =
71+
FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
72+
sw->SetClassName(wrapString);
7173
env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId);
72-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
73-
sw->GetFunction());
74+
target->Set(wrapString, sw->GetFunction());
7475

7576
Local<FunctionTemplate> ww =
7677
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
7778
ww->InstanceTemplate()->SetInternalFieldCount(1);
78-
ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
79+
Local<String> writeWrapString =
80+
FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
81+
ww->SetClassName(writeWrapString);
7982
env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId);
80-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
81-
ww->GetFunction());
83+
target->Set(writeWrapString, ww->GetFunction());
8284
env->set_write_wrap_constructor_function(ww->GetFunction());
8385
}
8486

src/tcp_wrap.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ void TCPWrap::Initialize(Local<Object> target,
7171
Environment* env = Environment::GetCurrent(context);
7272

7373
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
74-
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"));
74+
Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
75+
t->SetClassName(tcpString);
7576
t->InstanceTemplate()->SetInternalFieldCount(1);
7677

7778
// Init properties
78-
t->InstanceTemplate()->Set(String::NewFromUtf8(env->isolate(), "reading"),
79+
t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
7980
Boolean::New(env->isolate(), false));
8081
t->InstanceTemplate()->Set(env->owner_string(), Null(env->isolate()));
8182
t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
@@ -109,7 +110,7 @@ void TCPWrap::Initialize(Local<Object> target,
109110
env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
110111
#endif
111112

112-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"), t->GetFunction());
113+
target->Set(tcpString, t->GetFunction());
113114
env->set_tcp_constructor_template(t);
114115

115116
// Create FunctionTemplate for TCPConnectWrap.
@@ -120,9 +121,10 @@ void TCPWrap::Initialize(Local<Object> target,
120121
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
121122
cwt->InstanceTemplate()->SetInternalFieldCount(1);
122123
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
123-
cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"));
124-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"),
125-
cwt->GetFunction());
124+
Local<String> wrapString =
125+
FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
126+
cwt->SetClassName(wrapString);
127+
target->Set(wrapString, cwt->GetFunction());
126128
}
127129

128130

src/timer_wrap.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using v8::HandleScope;
3939
using v8::Integer;
4040
using v8::Local;
4141
using v8::Object;
42+
using v8::String;
4243
using v8::Value;
4344

4445
const uint32_t kOnTimeout = 0;
@@ -50,8 +51,9 @@ class TimerWrap : public HandleWrap {
5051
Local<Context> context) {
5152
Environment* env = Environment::GetCurrent(context);
5253
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
54+
Local<String> timerString = FIXED_ONE_BYTE_STRING(env->isolate(), "Timer");
5355
constructor->InstanceTemplate()->SetInternalFieldCount(1);
54-
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
56+
constructor->SetClassName(timerString);
5557
constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
5658
Integer::New(env->isolate(), kOnTimeout));
5759

@@ -67,8 +69,7 @@ class TimerWrap : public HandleWrap {
6769
env->SetProtoMethod(constructor, "start", Start);
6870
env->SetProtoMethod(constructor, "stop", Stop);
6971

70-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
71-
constructor->GetFunction());
72+
target->Set(timerString, constructor->GetFunction());
7273
}
7374

7475
size_t self_size() const override { return sizeof(*this); }

0 commit comments

Comments
 (0)