Skip to content

Commit

Permalink
src: fixup strings, reduce duplication
Browse files Browse the repository at this point in the history
PR-URL: #14937
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell authored and MylesBorins committed Sep 11, 2017
1 parent e40bde6 commit 9b9932a
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 73 deletions.
32 changes: 16 additions & 16 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2167,28 +2167,28 @@ void Initialize(Local<Object> target,
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
aiw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId);
aiw->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"),
aiw->GetFunction());
Local<String> addrInfoWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
aiw->SetClassName(addrInfoWrapString);
target->Set(addrInfoWrapString, aiw->GetFunction());

Local<FunctionTemplate> niw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
niw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId);
niw->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"),
niw->GetFunction());
Local<String> nameInfoWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
niw->SetClassName(nameInfoWrapString);
target->Set(nameInfoWrapString, niw->GetFunction());

Local<FunctionTemplate> qrw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
qrw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId);
qrw->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"),
qrw->GetFunction());
Local<String> queryWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
qrw->SetClassName(queryWrapString);
target->Set(queryWrapString, qrw->GetFunction());

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

channel_wrap->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"),
channel_wrap->GetFunction());
Local<String> channelWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
channel_wrap->SetClassName(channelWrapString);
target->Set(channelWrapString, channel_wrap->GetFunction());
}

} // anonymous namespace
Expand Down
8 changes: 5 additions & 3 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;


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

Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"));
Local<String> jsStreamString =
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
t->SetClassName(jsStreamString);
t->InstanceTemplate()->SetInternalFieldCount(1);

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

StreamBase::AddMethods<JSStream>(env, t, StreamBase::kFlagHasWritev);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"),
t->GetFunction());
target->Set(jsStreamString, t->GetFunction());
env->set_jsstream_constructor_template(t);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static void InitConfig(Local<Object> target,

debugOptions->DefineOwnProperty(
context,
FIXED_ONE_BYTE_STRING(isolate, "port"),
env->port_string(),
Integer::New(isolate, debug_options.port()),
ReadOnly).FromJust();

Expand Down
7 changes: 4 additions & 3 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ bool EntropySource(unsigned char* buffer, size_t length) {
void SecureContext::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(SecureContext::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));
Local<String> secureContextString =
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
t->SetClassName(secureContextString);

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"),
t->GetFunction());
target->Set(secureContextString, t->GetFunction());
env->set_secure_context_constructor_template(t);
}

Expand Down
7 changes: 4 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1484,9 +1484,10 @@ void InitFs(Local<Object> target,
FunctionTemplate::New(env->isolate(), NewFSReqWrap);
fst->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId);
fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"),
fst->GetFunction());
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap");
fst->SetClassName(wrapString);
target->Set(wrapString, fst->GetFunction());
}

} // end namespace node
Expand Down
3 changes: 1 addition & 2 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,7 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "nghttp2ErrorString", HttpErrorString);

Local<String> http2SessionClassName =
String::NewFromUtf8(isolate, "Http2Session",
v8::NewStringType::kInternalized).ToLocalChecked();
FIXED_ONE_BYTE_STRING(isolate, "Http2Session");

Local<FunctionTemplate> session =
env->NewFunctionTemplate(Http2Session::New);
Expand Down
12 changes: 8 additions & 4 deletions src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,11 @@ void InitializeSerdesBindings(Local<Object> target,
"_setTreatArrayBufferViewsAsHostObjects",
SerializerContext::SetTreatArrayBufferViewsAsHostObjects);

ser->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"));
Local<String> serializerString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
ser->SetClassName(serializerString);
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer"),
serializerString,
ser->GetFunction(env->context()).ToLocalChecked()).FromJust();

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

des->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"));
Local<String> deserializerString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
des->SetClassName(deserializerString);
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer"),
deserializerString,
des->GetFunction(env->context()).ToLocalChecked()).FromJust();
}

Expand Down
8 changes: 5 additions & 3 deletions src/node_stat_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;


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

Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"));
Local<String> statWatcherString =
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
t->SetClassName(statWatcherString);

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"),
t->GetFunction());
target->Set(statWatcherString, t->GetFunction());
}


Expand Down
6 changes: 4 additions & 2 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using v8::Integer;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

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

z->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib"), z->GetFunction());
Local<String> zlibString = FIXED_ONE_BYTE_STRING(env->isolate(), "Zlib");
z->SetClassName(zlibString);
target->Set(zlibString, z->GetFunction());

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION));
Expand Down
13 changes: 8 additions & 5 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

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

Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"));
Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
t->SetClassName(pipeString);
t->InstanceTemplate()->SetInternalFieldCount(1);

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction());
target->Set(pipeString, t->GetFunction());
env->set_pipe_constructor_template(t);

// Create FunctionTemplate for PipeConnectWrap.
Expand All @@ -103,9 +105,10 @@ void PipeWrap::Initialize(Local<Object> target,
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
cwt->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"),
cwt->GetFunction());
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
cwt->SetClassName(wrapString);
target->Set(wrapString, cwt->GetFunction());
}


Expand Down
7 changes: 4 additions & 3 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class ProcessWrap : public HandleWrap {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"));
Local<String> processString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
constructor->SetClassName(processString);

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

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"),
constructor->GetFunction());
target->Set(processString, constructor->GetFunction());
}

size_t self_size() const override { return sizeof(*this); }
Expand Down
8 changes: 5 additions & 3 deletions src/signal_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

namespace {
Expand All @@ -49,7 +50,9 @@ class SignalWrap : public HandleWrap {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
Local<String> signalString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
constructor->SetClassName(signalString);

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
constructor->GetFunction());
target->Set(signalString, constructor->GetFunction());
}

size_t self_size() const override { return sizeof(*this); }
Expand Down
14 changes: 8 additions & 6 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ void StreamWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> sw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
sw->InstanceTemplate()->SetInternalFieldCount(1);
sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
sw->SetClassName(wrapString);
env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
sw->GetFunction());
target->Set(wrapString, sw->GetFunction());

Local<FunctionTemplate> ww =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
ww->InstanceTemplate()->SetInternalFieldCount(1);
ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
Local<String> writeWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
ww->SetClassName(writeWrapString);
env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
ww->GetFunction());
target->Set(writeWrapString, ww->GetFunction());
env->set_write_wrap_constructor_function(ww->GetFunction());
}

Expand Down
14 changes: 8 additions & 6 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ void TCPWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);

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

// Init properties
t->InstanceTemplate()->Set(String::NewFromUtf8(env->isolate(), "reading"),
t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
Boolean::New(env->isolate(), false));
t->InstanceTemplate()->Set(env->owner_string(), Null(env->isolate()));
t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
Expand Down Expand Up @@ -109,7 +110,7 @@ void TCPWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
#endif

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"), t->GetFunction());
target->Set(tcpString, t->GetFunction());
env->set_tcp_constructor_template(t);

// Create FunctionTemplate for TCPConnectWrap.
Expand All @@ -120,9 +121,10 @@ void TCPWrap::Initialize(Local<Object> target,
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
cwt->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"),
cwt->GetFunction());
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
cwt->SetClassName(wrapString);
target->Set(wrapString, cwt->GetFunction());
}


Expand Down
7 changes: 4 additions & 3 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

const uint32_t kOnTimeout = 0;
Expand All @@ -50,8 +51,9 @@ class TimerWrap : public HandleWrap {
Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
Local<String> timerString = FIXED_ONE_BYTE_STRING(env->isolate(), "Timer");
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
constructor->SetClassName(timerString);
constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
Integer::New(env->isolate(), kOnTimeout));

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

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
constructor->GetFunction());
target->Set(timerString, constructor->GetFunction());
}

size_t self_size() const override { return sizeof(*this); }
Expand Down
Loading

0 comments on commit 9b9932a

Please sign in to comment.