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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
maclover7 committed Jul 13, 2018
1 parent d2ee7d6 commit 45732c7
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 @@ -718,15 +718,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);

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 @@ -115,7 +115,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 @@ -129,11 +128,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 45732c7

Please sign in to comment.