Skip to content

Commit 4ed5d1a

Browse files
maclover7targos
authored andcommitted
src: add HandleWrap::AddWrapMethods
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]>
1 parent 51d613d commit 4ed5d1a

11 files changed

+24
-47
lines changed

src/handle_wrap.cc

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace node {
2929

3030
using v8::Context;
3131
using v8::FunctionCallbackInfo;
32+
using v8::FunctionTemplate;
3233
using v8::HandleScope;
3334
using v8::Local;
3435
using v8::Object;
@@ -130,4 +131,13 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
130131
}
131132

132133

134+
void HandleWrap::AddWrapMethods(Environment* env,
135+
Local<FunctionTemplate> t) {
136+
env->SetProtoMethod(t, "close", HandleWrap::Close);
137+
env->SetProtoMethodNoSideEffect(t, "hasRef", HandleWrap::HasRef);
138+
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
139+
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
140+
}
141+
142+
133143
} // namespace node

src/handle_wrap.h

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class HandleWrap : public AsyncWrap {
7373
virtual void Close(
7474
v8::Local<v8::Value> close_callback = v8::Local<v8::Value>());
7575

76+
static void AddWrapMethods(Environment* env,
77+
v8::Local<v8::FunctionTemplate> constructor);
78+
7679
protected:
7780
HandleWrap(Environment* env,
7881
v8::Local<v8::Object> object,

src/node_messaging.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,12 @@ MaybeLocal<Function> GetMessagePortConstructor(
724724
m->InstanceTemplate()->SetInternalFieldCount(1);
725725

726726
AsyncWrap::AddWrapMethods(env, m);
727+
HandleWrap::AddWrapMethods(env, m);
727728

728729
env->SetProtoMethod(m, "postMessage", MessagePort::PostMessage);
729730
env->SetProtoMethod(m, "start", MessagePort::Start);
730731
env->SetProtoMethod(m, "stop", MessagePort::Stop);
731732
env->SetProtoMethod(m, "drain", MessagePort::Drain);
732-
env->SetProtoMethod(m, "close", HandleWrap::Close);
733-
env->SetProtoMethod(m, "unref", HandleWrap::Unref);
734-
env->SetProtoMethod(m, "ref", HandleWrap::Ref);
735-
env->SetProtoMethod(m, "hasRef", HandleWrap::HasRef);
736733

737734
env->set_message_port_constructor_template(m);
738735
}

src/node_stat_watcher.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
5252
t->SetClassName(statWatcherString);
5353

5454
AsyncWrap::AddWrapMethods(env, t);
55+
HandleWrap::AddWrapMethods(env, t);
56+
5557
env->SetProtoMethod(t, "start", StatWatcher::Start);
56-
env->SetProtoMethod(t, "close", HandleWrap::Close);
57-
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
58-
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
59-
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
6058

6159
target->Set(statWatcherString, t->GetFunction());
6260
}

src/pipe_wrap.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,7 @@ void PipeWrap::Initialize(Local<Object> target,
7777
t->InstanceTemplate()->SetInternalFieldCount(1);
7878

7979
AsyncWrap::AddWrapMethods(env, t);
80-
81-
env->SetProtoMethod(t, "close", HandleWrap::Close);
82-
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
83-
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
84-
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
85-
80+
HandleWrap::AddWrapMethods(env, t);
8681
LibuvStreamWrap::AddMethods(env, t);
8782

8883
env->SetProtoMethod(t, "bind", Bind);

src/process_wrap.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,11 @@ class ProcessWrap : public HandleWrap {
5858
constructor->SetClassName(processString);
5959

6060
AsyncWrap::AddWrapMethods(env, constructor);
61-
62-
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
61+
HandleWrap::AddWrapMethods(env, constructor);
6362

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

67-
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
68-
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
69-
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
70-
7166
target->Set(processString, constructor->GetFunction());
7267
}
7368

src/signal_wrap.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ class SignalWrap : public HandleWrap {
5252
constructor->SetClassName(signalString);
5353

5454
AsyncWrap::AddWrapMethods(env, constructor);
55-
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
56-
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
57-
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
58-
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
55+
HandleWrap::AddWrapMethods(env, constructor);
56+
5957
env->SetProtoMethod(constructor, "start", Start);
6058
env->SetProtoMethod(constructor, "stop", Stop);
6159

src/tcp_wrap.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,7 @@ void TCPWrap::Initialize(Local<Object> target,
8686
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
8787

8888
AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);
89-
90-
env->SetProtoMethod(t, "close", HandleWrap::Close);
91-
92-
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
93-
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
94-
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
95-
89+
HandleWrap::AddWrapMethods(env, t);
9690
LibuvStreamWrap::AddMethods(env, t);
9791

9892
env->SetProtoMethod(t, "open", Open);

src/timer_wrap.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ class TimerWrap : public HandleWrap {
5555
env->SetTemplateMethod(constructor, "now", Now);
5656

5757
AsyncWrap::AddWrapMethods(env, constructor);
58-
59-
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
60-
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
61-
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
62-
env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
58+
HandleWrap::AddWrapMethods(env, constructor);
6359

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

src/tty_wrap.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ void TTYWrap::Initialize(Local<Object> target,
5454
t->InstanceTemplate()->SetInternalFieldCount(1);
5555

5656
AsyncWrap::AddWrapMethods(env, t);
57-
58-
env->SetProtoMethod(t, "close", HandleWrap::Close);
59-
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
60-
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
61-
env->SetProtoMethodNoSideEffect(t, "hasRef", HandleWrap::HasRef);
62-
57+
HandleWrap::AddWrapMethods(env, t);
6358
LibuvStreamWrap::AddMethods(env, t);
6459

6560
env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);

src/udp_wrap.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ void UDPWrap::Initialize(Local<Object> target,
119119
env->SetProtoMethod(t, "send", Send);
120120
env->SetProtoMethod(t, "bind6", Bind6);
121121
env->SetProtoMethod(t, "send6", Send6);
122-
env->SetProtoMethod(t, "close", Close);
123122
env->SetProtoMethod(t, "recvStart", RecvStart);
124123
env->SetProtoMethod(t, "recvStop", RecvStop);
125124
env->SetProtoMethod(t, "getsockname",
@@ -133,11 +132,8 @@ void UDPWrap::Initialize(Local<Object> target,
133132
env->SetProtoMethod(t, "setTTL", SetTTL);
134133
env->SetProtoMethod(t, "bufferSize", BufferSize);
135134

136-
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
137-
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
138-
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
139-
140135
AsyncWrap::AddWrapMethods(env, t);
136+
HandleWrap::AddWrapMethods(env, t);
141137

142138
target->Set(udpString, t->GetFunction());
143139
env->set_udp_constructor_function(t->GetFunction());

0 commit comments

Comments
 (0)