Browse Source

src: reduce code duplication

Adds `AsyncWrap::AddWrapMethods()` to add common methods
to a `Local<FunctionTemplate>`. Follows same pattern as
stream base.

PR-URL: https://github.com/nodejs/node/pull/14937
Reviewed-By: Anna Henningsen <anna@addaleax.net>
canary-base
James M Snell 8 years ago
parent
commit
5e7c69716e
  1. 7
      src/async-wrap.cc
  2. 9
      src/async-wrap.h
  3. 8
      src/cares_wrap.cc
  4. 2
      src/fs_event_wrap.cc
  5. 2
      src/js_stream.cc
  6. 6
      src/node_crypto.cc
  7. 2
      src/node_file.cc
  8. 2
      src/node_http2.cc
  9. 2
      src/node_http_parser.cc
  10. 2
      src/node_stat_watcher.cc
  11. 2
      src/node_zlib.cc
  12. 4
      src/pipe_wrap.cc
  13. 2
      src/process_wrap.cc
  14. 2
      src/signal_wrap.cc
  15. 4
      src/stream_wrap.cc
  16. 5
      src/tcp_wrap.cc
  17. 2
      src/timer_wrap.cc
  18. 3
      src/tls_wrap.cc
  19. 2
      src/tty_wrap.cc
  20. 4
      src/udp_wrap.cc

7
src/async-wrap.cc

@ -468,6 +468,13 @@ void AsyncWrap::QueueDestroyId(const FunctionCallbackInfo<Value>& args) {
PushBackDestroyId(Environment::GetCurrent(args), args[0]->NumberValue());
}
void AsyncWrap::AddWrapMethods(Environment* env,
Local<FunctionTemplate> constructor,
int flag) {
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
if (flag & kFlagHasReset)
env->SetProtoMethod(constructor, "asyncReset", AsyncWrap::AsyncReset);
}
void AsyncWrap::Initialize(Local<Object> target,
Local<Value> unused,

9
src/async-wrap.h

@ -87,6 +87,11 @@ class AsyncWrap : public BaseObject {
PROVIDERS_LENGTH,
};
enum Flags {
kFlagNone = 0x0,
kFlagHasReset = 0x1
};
AsyncWrap(Environment* env,
v8::Local<v8::Object> object,
ProviderType provider,
@ -94,6 +99,10 @@ class AsyncWrap : public BaseObject {
virtual ~AsyncWrap();
static void AddWrapMethods(Environment* env,
v8::Local<v8::FunctionTemplate> constructor,
int flags = kFlagNone);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context);

8
src/cares_wrap.cc

@ -2189,7 +2189,7 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> aiw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
aiw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(aiw, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, aiw);
Local<String> addrInfoWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
aiw->SetClassName(addrInfoWrapString);
@ -2198,7 +2198,7 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> niw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
niw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(niw, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, niw);
Local<String> nameInfoWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
niw->SetClassName(nameInfoWrapString);
@ -2207,7 +2207,7 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> qrw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
qrw->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(qrw, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, qrw);
Local<String> queryWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
qrw->SetClassName(queryWrapString);
@ -2216,7 +2216,7 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> channel_wrap =
env->NewFunctionTemplate(ChannelWrap::New);
channel_wrap->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(channel_wrap, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, channel_wrap);
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);

2
src/fs_event_wrap.cc

@ -94,7 +94,7 @@ void FSEventWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(fsevent_string);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "start", Start);
env->SetProtoMethod(t, "close", Close);

2
src/js_stream.cc

@ -218,7 +218,7 @@ void JSStream::Initialize(Local<Object> target,
t->SetClassName(jsStreamString);
t->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "doAlloc", DoAlloc);
env->SetProtoMethod(t, "doRead", DoRead);

6
src/node_crypto.cc

@ -2729,7 +2729,7 @@ void Connection::Initialize(Environment* env, Local<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"));
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "encIn", Connection::EncIn);
env->SetProtoMethod(t, "clearOut", Connection::ClearOut);
env->SetProtoMethod(t, "clearIn", Connection::ClearIn);
@ -6137,14 +6137,14 @@ void InitCrypto(Local<Object> target,
Local<FunctionTemplate> pb = FunctionTemplate::New(env->isolate());
pb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PBKDF2"));
env->SetProtoMethod(pb, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, pb);
Local<ObjectTemplate> pbt = pb->InstanceTemplate();
pbt->SetInternalFieldCount(1);
env->set_pbkdf2_constructor_template(pbt);
Local<FunctionTemplate> rb = FunctionTemplate::New(env->isolate());
rb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "RandomBytes"));
env->SetProtoMethod(rb, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, rb);
Local<ObjectTemplate> rbt = rb->InstanceTemplate();
rbt->SetInternalFieldCount(1);
env->set_randombytes_constructor_template(rbt);

2
src/node_file.cc

@ -1483,7 +1483,7 @@ void InitFs(Local<Object> target,
Local<FunctionTemplate> fst =
FunctionTemplate::New(env->isolate(), NewFSReqWrap);
fst->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, fst);
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap");
fst->SetClassName(wrapString);

2
src/node_http2.cc

@ -1212,7 +1212,7 @@ void Initialize(Local<Object> target,
env->NewFunctionTemplate(Http2Session::New);
session->SetClassName(http2SessionClassName);
session->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(session, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, session);
env->SetProtoMethod(session, "consume",
Http2Session::Consume);
env->SetProtoMethod(session, "destroy",

2
src/node_http_parser.cc

@ -794,7 +794,7 @@ void InitHttpParser(Local<Object> target,
#undef V
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "methods"), methods);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "close", Parser::Close);
env->SetProtoMethod(t, "execute", Parser::Execute);
env->SetProtoMethod(t, "finish", Parser::Finish);

2
src/node_stat_watcher.cc

@ -52,7 +52,7 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
t->SetClassName(statWatcherString);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "start", StatWatcher::Start);
env->SetProtoMethod(t, "stop", StatWatcher::Stop);

2
src/node_zlib.cc

@ -686,7 +686,7 @@ void InitZlib(Local<Object> target,
z->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(z, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, z);
env->SetProtoMethod(z, "write", ZCtx::Write<true>);
env->SetProtoMethod(z, "writeSync", ZCtx::Write<false>);
env->SetProtoMethod(z, "init", ZCtx::Init);

4
src/pipe_wrap.cc

@ -72,7 +72,7 @@ void PipeWrap::Initialize(Local<Object> target,
t->SetClassName(pipeString);
t->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
@ -104,7 +104,7 @@ void PipeWrap::Initialize(Local<Object> target,
};
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
cwt->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, cwt);
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
cwt->SetClassName(wrapString);

2
src/process_wrap.cc

@ -57,7 +57,7 @@ class ProcessWrap : public HandleWrap {
FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
constructor->SetClassName(processString);
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, constructor);
env->SetProtoMethod(constructor, "close", HandleWrap::Close);

2
src/signal_wrap.cc

@ -54,7 +54,7 @@ class SignalWrap : public HandleWrap {
FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
constructor->SetClassName(signalString);
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, constructor);
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);

4
src/stream_wrap.cc

@ -70,7 +70,7 @@ void StreamWrap::Initialize(Local<Object> target,
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
sw->SetClassName(wrapString);
env->SetProtoMethod(sw, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, sw);
target->Set(wrapString, sw->GetFunction());
Local<FunctionTemplate> ww =
@ -79,7 +79,7 @@ void StreamWrap::Initialize(Local<Object> target,
Local<String> writeWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
ww->SetClassName(writeWrapString);
env->SetProtoMethod(ww, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, ww);
target->Set(writeWrapString, ww->GetFunction());
env->set_write_wrap_constructor_function(ww->GetFunction());
}

5
src/tcp_wrap.cc

@ -82,8 +82,7 @@ void TCPWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset);
AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);
env->SetProtoMethod(t, "close", HandleWrap::Close);
@ -120,7 +119,7 @@ void TCPWrap::Initialize(Local<Object> target,
};
auto cwt = FunctionTemplate::New(env->isolate(), constructor);
cwt->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, cwt);
Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
cwt->SetClassName(wrapString);

2
src/timer_wrap.cc

@ -59,7 +59,7 @@ class TimerWrap : public HandleWrap {
env->SetTemplateMethod(constructor, "now", Now);
env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, constructor);
env->SetProtoMethod(constructor, "close", HandleWrap::Close);
env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);

3
src/tls_wrap.cc

@ -946,8 +946,7 @@ void TLSWrap::Initialize(Local<Object> target,
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(tlsWrapString);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
env->SetProtoMethod(t, "asyncReset", AsyncWrap::AsyncReset);
AsyncWrap::AddWrapMethods(env, t, AsyncWrap::kFlagHasReset);
env->SetProtoMethod(t, "receive", Receive);
env->SetProtoMethod(t, "start", Start);
env->SetProtoMethod(t, "setVerifyMode", SetVerifyMode);

2
src/tty_wrap.cc

@ -56,7 +56,7 @@ void TTYWrap::Initialize(Local<Object> target,
t->SetClassName(ttyString);
t->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);

4
src/udp_wrap.cc

@ -139,7 +139,7 @@ void UDPWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, t);
target->Set(udpString, t->GetFunction());
env->set_udp_constructor_function(t->GetFunction());
@ -148,7 +148,7 @@ void UDPWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> swt =
FunctionTemplate::New(env->isolate(), NewSendWrap);
swt->InstanceTemplate()->SetInternalFieldCount(1);
env->SetProtoMethod(swt, "getAsyncId", AsyncWrap::GetAsyncId);
AsyncWrap::AddWrapMethods(env, swt);
Local<String> sendWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap");
swt->SetClassName(sendWrapString);

Loading…
Cancel
Save