Skip to content

Commit

Permalink
src: reduce duplicated boilerplate with new env utility fn
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #36536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
jasnell authored and MylesBorins committed Aug 31, 2021
1 parent 9aba288 commit d73181f
Show file tree
Hide file tree
Showing 33 changed files with 82 additions and 258 deletions.
6 changes: 1 addition & 5 deletions src/README.md
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
Expand Up @@ -1898,32 +1898,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 @@ -1950,11 +1935,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);
}

} // namespace cares_wrap
Expand Down
18 changes: 18 additions & 0 deletions src/env-inl.h
Expand Up @@ -1107,6 +1107,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
Expand Up @@ -1111,6 +1111,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
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
Expand Up @@ -101,19 +101,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
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
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
Expand Up @@ -733,10 +733,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 @@ -752,8 +750,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
Expand Up @@ -1292,21 +1292,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
10 changes: 1 addition & 9 deletions src/node_dir.cc
Expand Up @@ -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
Expand Down Expand Up @@ -349,7 +348,6 @@ void Initialize(Local<Object> target,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();

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

Expand All @@ -360,13 +358,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(dir, "close", DirHandle::Close);
Local<ObjectTemplate> dirt = dir->InstanceTemplate();
dirt->SetInternalFieldCount(DirHandle::kInternalFieldCount);
Local<String> 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);
}

Expand Down
16 changes: 2 additions & 14 deletions src/node_file.cc
Expand Up @@ -2472,13 +2472,7 @@ void Initialize(Local<Object> target,
fst->InstanceTemplate()->SetInternalFieldCount(
FSReqBase::kInternalFieldCount);
fst->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> 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.
Expand Down Expand Up @@ -2509,14 +2503,8 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
Local<ObjectTemplate> fdt = fd->InstanceTemplate();
fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
Local<String> 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
Expand Down
14 changes: 2 additions & 12 deletions src/node_http2.cc
Expand Up @@ -3070,9 +3070,6 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "packSettings", PackSettings);
env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions);

Local<String> http2SessionClassName =
FIXED_ONE_BYTE_STRING(isolate, "Http2Session");

Local<FunctionTemplate> ping = FunctionTemplate::New(env->isolate());
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping"));
ping->Inherit(AsyncWrap::GetConstructorTemplate(env));
Expand All @@ -3081,14 +3078,12 @@ void Initialize(Local<Object> target,
env->set_http2ping_constructor_template(pingt);

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

Local<FunctionTemplate> 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);
Expand All @@ -3103,13 +3098,10 @@ void Initialize(Local<Object> target,
Local<ObjectTemplate> 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<FunctionTemplate> session =
env->NewFunctionTemplate(Http2Session::New);
session->SetClassName(http2SessionClassName);
session->InstanceTemplate()->SetInternalFieldCount(
Http2Session::kInternalFieldCount);
session->Inherit(AsyncWrap::GetConstructorTemplate(env));
Expand All @@ -3135,9 +3127,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(
session, "remoteSettings",
Http2Session::RefreshSettings<nghttp2_session_get_remote_settings>);
target->Set(context,
http2SessionClassName,
session->GetFunction(env->context()).ToLocalChecked()).Check();
env->SetConstructorFunction(target, "Http2Session", session);

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

Expand Down
5 changes: 1 addition & 4 deletions src/node_http_parser.cc
Expand Up @@ -956,7 +956,6 @@ void InitializeHttpParser(Local<Object> target,

Local<FunctionTemplate> 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));
Expand Down Expand Up @@ -999,9 +998,7 @@ void InitializeHttpParser(Local<Object> 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
Expand Down

0 comments on commit d73181f

Please sign in to comment.