Skip to content

Commit

Permalink
src: add HandleWrap::AddWrapMethods
Browse files Browse the repository at this point in the history
Extracts common setters to a single location

PR-URL: #21769
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
maclover7 authored and targos committed Jul 14, 2018
1 parent 51d613d commit 4ed5d1a
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 47 deletions.
10 changes: 10 additions & 0 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace node {

using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Local;
using v8::Object;
Expand Down Expand Up @@ -130,4 +131,13 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
}


void HandleWrap::AddWrapMethods(Environment* env,
Local<FunctionTemplate> t) {
env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethodNoSideEffect(t, "hasRef", HandleWrap::HasRef);
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
}


} // namespace node
3 changes: 3 additions & 0 deletions src/handle_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class HandleWrap : public AsyncWrap {
virtual void Close(
v8::Local<v8::Value> close_callback = v8::Local<v8::Value>());

static void AddWrapMethods(Environment* env,
v8::Local<v8::FunctionTemplate> constructor);

protected:
HandleWrap(Environment* env,
v8::Local<v8::Object> object,
Expand Down
5 changes: 1 addition & 4 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,12 @@ MaybeLocal<Function> GetMessagePortConstructor(
m->InstanceTemplate()->SetInternalFieldCount(1);

AsyncWrap::AddWrapMethods(env, m);
HandleWrap::AddWrapMethods(env, m);

env->SetProtoMethod(m, "postMessage", MessagePort::PostMessage);
env->SetProtoMethod(m, "start", MessagePort::Start);
env->SetProtoMethod(m, "stop", MessagePort::Stop);
env->SetProtoMethod(m, "drain", MessagePort::Drain);
env->SetProtoMethod(m, "close", HandleWrap::Close);
env->SetProtoMethod(m, "unref", HandleWrap::Unref);
env->SetProtoMethod(m, "ref", HandleWrap::Ref);
env->SetProtoMethod(m, "hasRef", HandleWrap::HasRef);

env->set_message_port_constructor_template(m);
}
Expand Down
6 changes: 2 additions & 4 deletions src/node_stat_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
t->SetClassName(statWatcherString);

AsyncWrap::AddWrapMethods(env, t);
HandleWrap::AddWrapMethods(env, t);

env->SetProtoMethod(t, "start", StatWatcher::Start);
env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);

target->Set(statWatcherString, t->GetFunction());
}
Expand Down
7 changes: 1 addition & 6 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ void PipeWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->SetInternalFieldCount(1);

AsyncWrap::AddWrapMethods(env, t);

env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);

HandleWrap::AddWrapMethods(env, t);
LibuvStreamWrap::AddMethods(env, t);

env->SetProtoMethod(t, "bind", Bind);
Expand Down
7 changes: 1 addition & 6 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@ class ProcessWrap : public HandleWrap {
constructor->SetClassName(processString);

AsyncWrap::AddWrapMethods(env, constructor);

env->SetProtoMethod(constructor, "close", HandleWrap::Close);
HandleWrap::AddWrapMethods(env, constructor);

env->SetProtoMethod(constructor, "spawn", Spawn);
env->SetProtoMethod(constructor, "kill", Kill);

env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);

target->Set(processString, constructor->GetFunction());
}

Expand Down
6 changes: 2 additions & 4 deletions src/signal_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ class SignalWrap : public HandleWrap {
constructor->SetClassName(signalString);

AsyncWrap::AddWrapMethods(env, constructor);
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
HandleWrap::AddWrapMethods(env, constructor);

env->SetProtoMethod(constructor, "start", Start);
env->SetProtoMethod(constructor, "stop", Stop);

Expand Down
8 changes: 1 addition & 7 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,7 @@ void TCPWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));

AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);

env->SetProtoMethod(t, "close", HandleWrap::Close);

env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);

HandleWrap::AddWrapMethods(env, t);
LibuvStreamWrap::AddMethods(env, t);

env->SetProtoMethod(t, "open", Open);
Expand Down
6 changes: 1 addition & 5 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ class TimerWrap : public HandleWrap {
env->SetTemplateMethod(constructor, "now", Now);

AsyncWrap::AddWrapMethods(env, constructor);

env->SetProtoMethod(constructor, "close", HandleWrap::Close);
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
HandleWrap::AddWrapMethods(env, constructor);

env->SetProtoMethod(constructor, "start", Start);
env->SetProtoMethod(constructor, "stop", Stop);
Expand Down
7 changes: 1 addition & 6 deletions src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ void TTYWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->SetInternalFieldCount(1);

AsyncWrap::AddWrapMethods(env, t);

env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethodNoSideEffect(t, "hasRef", HandleWrap::HasRef);

HandleWrap::AddWrapMethods(env, t);
LibuvStreamWrap::AddMethods(env, t);

env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
Expand Down
6 changes: 1 addition & 5 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ void UDPWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "send", Send);
env->SetProtoMethod(t, "bind6", Bind6);
env->SetProtoMethod(t, "send6", Send6);
env->SetProtoMethod(t, "close", Close);
env->SetProtoMethod(t, "recvStart", RecvStart);
env->SetProtoMethod(t, "recvStop", RecvStop);
env->SetProtoMethod(t, "getsockname",
Expand All @@ -133,11 +132,8 @@ void UDPWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "setTTL", SetTTL);
env->SetProtoMethod(t, "bufferSize", BufferSize);

env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);

AsyncWrap::AddWrapMethods(env, t);
HandleWrap::AddWrapMethods(env, t);

target->Set(udpString, t->GetFunction());
env->set_udp_constructor_function(t->GetFunction());
Expand Down

0 comments on commit 4ed5d1a

Please sign in to comment.