Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: reduce duplicated boilerplate with new env utility fn #36536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,7 @@ void Initialize(Local<Object> target,

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

Local<String> 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
Expand Down
27 changes: 4 additions & 23 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2345,32 +2345,17 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> aiw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> 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<FunctionTemplate> niw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> 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<FunctionTemplate> qrw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> 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<FunctionTemplate> channel_wrap =
env->NewFunctionTemplate(ChannelWrap::New);
Expand All @@ -2397,11 +2382,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress);
env->SetProtoMethod(channel_wrap, "cancel", Cancel);

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

} // anonymous namespace
Expand Down
5 changes: 1 addition & 4 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethodNoSideEffect(t, "getAuthTag", GetAuthTag);
env->SetProtoMethod(t, "setAuthTag", SetAuthTag);
env->SetProtoMethod(t, "setAAD", SetAAD);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "CipherBase", t);

env->SetMethodNoSideEffect(target, "getSSLCiphers", GetSSLCiphers);
env->SetMethodNoSideEffect(target, "getCiphers", GetCiphers);
Expand Down
7 changes: 2 additions & 5 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(
SecureContext::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));
Local<String> secureContextString =
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
t->SetClassName(secureContextString);

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

target->Set(env->context(), secureContextString,
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "SecureContext", t);

env->set_secure_context_constructor_template(t);

env->SetMethodNoSideEffect(target, "getRootCertificates",
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate>(),
attributes);

target->Set(env->context(),
name,
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, name, t);
};

make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New);
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "ECDH", t);

env->SetMethodNoSideEffect(target, "ECDHConvertKey", ECDH::ConvertKey);
env->SetMethodNoSideEffect(target, "getCurves", ECDH::GetCurves);
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "update", HashUpdate);
env->SetProtoMethod(t, "digest", HashDigest);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "Hash", t);

env->SetMethodNoSideEffect(target, "getHashes", GetHashes);

Expand Down
4 changes: 1 addition & 3 deletions src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "update", HmacUpdate);
env->SetProtoMethod(t, "digest", HmacDigest);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "Hmac", t);

HmacJob::Initialize(env, target);
}
Expand Down
8 changes: 2 additions & 6 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "update", SignUpdate);
env->SetProtoMethod(t, "sign", SignFinal);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "Sign", t);

env->SetMethod(target, "signOneShot", Sign::SignSync);

Expand Down Expand Up @@ -396,9 +394,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "update", VerifyUpdate);
env->SetProtoMethod(t, "verify", VerifyFinal);

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"),
t->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "Verify", t);

env->SetMethod(target, "verifyOneShot", Verify::VerifySync);
}
Expand Down
8 changes: 1 addition & 7 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,11 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
Environment* env,
v8::Local<v8::Object> target) {
v8::Local<v8::FunctionTemplate> job = env->NewFunctionTemplate(new_fn);
v8::Local<v8::String> class_name =
OneByteString(env->isolate(), CryptoJobTraits::JobName);
job->SetClassName(class_name);
job->Inherit(AsyncWrap::GetConstructorTemplate(env));
job->InstanceTemplate()->SetInternalFieldCount(
AsyncWrap::kInternalFieldCount);
env->SetProtoMethod(job, "run", Run);
target->Set(
env->context(),
class_name,
job->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, CryptoJobTraits::JobName, job);
}

private:
Expand Down
18 changes: 18 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,24 @@ inline void Environment::SetInstanceMethod(v8::Local<v8::FunctionTemplate> that,
t->SetClassName(name_string);
}

inline void Environment::SetConstructorFunction(
v8::Local<v8::Object> that,
const char* name,
v8::Local<v8::FunctionTemplate> tmpl) {
SetConstructorFunction(that, OneByteString(isolate(), name), tmpl);
}

inline void Environment::SetConstructorFunction(
v8::Local<v8::Object> that,
v8::Local<v8::String> name,
v8::Local<v8::FunctionTemplate> 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_++
Expand Down
8 changes: 8 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,14 @@ class Environment : public MemoryRetainer {
const char* name,
v8::FunctionCallback callback);

inline void SetConstructorFunction(v8::Local<v8::Object> that,
const char* name,
v8::Local<v8::FunctionTemplate> tmpl);

inline void SetConstructorFunction(v8::Local<v8::Object> that,
v8::Local<v8::String> name,
v8::Local<v8::FunctionTemplate> tmpl);

void AtExit(void (*cb)(void* arg), void* arg);
void RunAtExitCallbacks();

Expand Down
6 changes: 1 addition & 5 deletions src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ void FSEventWrap::Initialize(Local<Object> target,
void* priv) {
Environment* env = Environment::GetCurrent(context);

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

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

target->Set(env->context(),
fsevent_string,
t->GetFunction(context).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "FSEvent", t);
}


Expand Down
10 changes: 4 additions & 6 deletions src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,17 @@ class JSBindingsConnection : public AsyncWrap {
}

static void Bind(Environment* env, Local<Object> target) {
Local<String> class_name = ConnectionType::GetClassName(env);
Local<FunctionTemplate> 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<Value>& info) {
Expand Down
8 changes: 1 addition & 7 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ using v8::HandleScope;
using v8::Int32;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;


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

Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Local<String> jsStreamString =
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
t->SetClassName(jsStreamString);
t->InstanceTemplate()
->SetInternalFieldCount(StreamBase::kInternalFieldCount);
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
Expand All @@ -213,9 +209,7 @@ void JSStream::Initialize(Local<Object> 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
Expand Down
8 changes: 1 addition & 7 deletions src/js_udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -195,9 +194,6 @@ void JSUDPWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);

Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Local<String> 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));
Expand All @@ -207,9 +203,7 @@ void JSUDPWrap::Initialize(Local<Object> 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);
}


Expand Down
6 changes: 2 additions & 4 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,8 @@ void ModuleWrap::Initialize(Local<Object> target,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();

Local<FunctionTemplate> tpl = env->NewFunctionTemplate(New);
tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"));
tpl->InstanceTemplate()->SetInternalFieldCount(
ModuleWrap::kInternalFieldCount);
tpl->Inherit(BaseObject::GetConstructorTemplate(env));
Expand All @@ -741,8 +739,8 @@ void ModuleWrap::Initialize(Local<Object> 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);
Expand Down
12 changes: 1 addition & 11 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1282,21 +1282,11 @@ void MicrotaskQueueWrap::New(const FunctionCallbackInfo<Value>& args) {

void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) {
HandleScope scope(env->isolate());
Local<String> class_name =
FIXED_ONE_BYTE_STRING(env->isolate(), "MicrotaskQueue");

Local<FunctionTemplate> 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);
}


Expand Down
Loading