From 89e133a1d8a3bfd655d5ae4f6b7071a1bbbcdc71 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 25 Feb 2015 20:43:14 +0300 Subject: [PATCH] stream_base: remove static JSMethod declarations Move JS methods to the stream_base-inl.h and thus define them on each use of `StreamBase::AddMethods`. Inline `AddMethods` itself, so that there won't be any need in a static declaration in stream_base.cc. NOTE: This basically allows using this API in user-land, though, some polishing is required before releasing it. PR-URL: https://github.com/iojs/io.js/pull/957 Reviewed-By: Chris Dickinson --- node.gyp | 1 + src/js_stream.cc | 1 + src/stream_base-inl.h | 83 +++++++++++++++++++++++++++++++++++++++++++ src/stream_base.cc | 76 +++++---------------------------------- src/stream_base.h | 4 +-- src/stream_wrap.cc | 3 ++ src/tls_wrap.cc | 2 ++ 7 files changed, 100 insertions(+), 70 deletions(-) create mode 100644 src/stream_base-inl.h diff --git a/node.gyp b/node.gyp index 4af27d8de093c2..34c30337582fe3 100644 --- a/node.gyp +++ b/node.gyp @@ -156,6 +156,7 @@ 'src/req-wrap-inl.h', 'src/string_bytes.h', 'src/stream_base.h', + 'src/stream_base-inl.h', 'src/stream_wrap.h', 'src/tree.h', 'src/util.h', diff --git a/src/js_stream.cc b/src/js_stream.cc index 38ab847954d056..02f32510c87464 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -5,6 +5,7 @@ #include "env-inl.h" #include "node_buffer.h" #include "stream_base.h" +#include "stream_base-inl.h" #include "v8.h" namespace node { diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h new file mode 100644 index 00000000000000..2167bdeb130b69 --- /dev/null +++ b/src/stream_base-inl.h @@ -0,0 +1,83 @@ +#ifndef SRC_STREAM_BASE_INL_H_ +#define SRC_STREAM_BASE_INL_H_ + +#include "stream_base.h" + +#include "node.h" +#include "env.h" +#include "env-inl.h" +#include "v8.h" + +namespace node { + +using v8::FunctionCallbackInfo; +using v8::FunctionTemplate; +using v8::Handle; +using v8::HandleScope; +using v8::Local; +using v8::PropertyAttribute; +using v8::PropertyCallbackInfo; +using v8::String; +using v8::Value; + +template +void StreamBase::AddMethods(Environment* env, Handle t) { + HandleScope scope(env->isolate()); + + enum PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + t->InstanceTemplate()->SetAccessor(env->fd_string(), + GetFD, + nullptr, + Handle(), + v8::DEFAULT, + attributes); + + env->SetProtoMethod(t, "readStart", JSMethod); + env->SetProtoMethod(t, "readStop", JSMethod); + env->SetProtoMethod(t, "shutdown", JSMethod); + env->SetProtoMethod(t, "writev", JSMethod); + env->SetProtoMethod(t, + "writeBuffer", + JSMethod); + env->SetProtoMethod(t, + "writeAsciiString", + JSMethod >); + env->SetProtoMethod(t, + "writeUtf8String", + JSMethod >); + env->SetProtoMethod(t, + "writeUcs2String", + JSMethod >); + env->SetProtoMethod(t, + "writeBinaryString", + JSMethod >); +} + + +template +void StreamBase::GetFD(Local key, + const PropertyCallbackInfo& args) { + StreamBase* wrap = Unwrap(args.Holder()); + + if (!wrap->IsAlive()) + return args.GetReturnValue().Set(UV_EINVAL); + + args.GetReturnValue().Set(wrap->GetFD()); +} + + +template & args)> +void StreamBase::JSMethod(const FunctionCallbackInfo& args) { + StreamBase* wrap = Unwrap(args.Holder()); + + if (!wrap->IsAlive()) + return args.GetReturnValue().Set(UV_EINVAL); + + args.GetReturnValue().Set((wrap->*Method)(args)); +} + +} // namespace node + +#endif // SRC_STREAM_BASE_INL_H_ diff --git a/src/stream_base.cc b/src/stream_base.cc index 82b1d65396c352..fb6f5322f6dafc 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -19,83 +19,23 @@ namespace node { using v8::Array; using v8::Context; using v8::FunctionCallbackInfo; -using v8::FunctionTemplate; using v8::Handle; using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Number; using v8::Object; -using v8::PropertyAttribute; -using v8::PropertyCallbackInfo; using v8::String; using v8::Value; -template void StreamBase::AddMethods(Environment* env, - Handle t); -template void StreamBase::AddMethods(Environment* env, - Handle t); -template void StreamBase::AddMethods(Environment* env, - Handle t); - - -template -void StreamBase::AddMethods(Environment* env, Handle t) { - HandleScope scope(env->isolate()); - - enum PropertyAttribute attributes = - static_cast(v8::ReadOnly | v8::DontDelete); - t->InstanceTemplate()->SetAccessor(env->fd_string(), - GetFD, - nullptr, - Handle(), - v8::DEFAULT, - attributes); - - env->SetProtoMethod(t, "readStart", JSMethod); - env->SetProtoMethod(t, "readStop", JSMethod); - env->SetProtoMethod(t, "shutdown", JSMethod); - env->SetProtoMethod(t, "writev", JSMethod); - env->SetProtoMethod(t, - "writeBuffer", - JSMethod); - env->SetProtoMethod(t, - "writeAsciiString", - JSMethod >); - env->SetProtoMethod(t, - "writeUtf8String", - JSMethod >); - env->SetProtoMethod(t, - "writeUcs2String", - JSMethod >); - env->SetProtoMethod(t, - "writeBinaryString", - JSMethod >); -} - - -template -void StreamBase::GetFD(Local key, - const PropertyCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); - - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - args.GetReturnValue().Set(wrap->GetFD()); -} - - -template & args)> -void StreamBase::JSMethod(const FunctionCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); - - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - args.GetReturnValue().Set((wrap->*Method)(args)); -} +template int StreamBase::WriteString( + const FunctionCallbackInfo& args); +template int StreamBase::WriteString( + const FunctionCallbackInfo& args); +template int StreamBase::WriteString( + const FunctionCallbackInfo& args); +template int StreamBase::WriteString( + const FunctionCallbackInfo& args); int StreamBase::ReadStart(const FunctionCallbackInfo& args) { diff --git a/src/stream_base.h b/src/stream_base.h index 87aae0597376fd..0864eb51cac1ed 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -159,8 +159,8 @@ class StreamResource { class StreamBase : public StreamResource { public: template - static void AddMethods(Environment* env, - v8::Handle target); + static inline void AddMethods(Environment* env, + v8::Handle target); virtual void* Cast() = 0; virtual bool IsAlive() = 0; diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index c8ea8d228fe081..824a880ebd6341 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -1,4 +1,7 @@ #include "stream_wrap.h" +#include "stream_base.h" +#include "stream_base-inl.h" + #include "env-inl.h" #include "env.h" #include "handle_wrap.h" diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 86056723df18ff..1647ab669b29a3 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -9,6 +9,8 @@ #include "node_wrap.h" // WithGenericStream #include "node_counters.h" #include "node_internals.h" +#include "stream_base.h" +#include "stream_base-inl.h" #include "util.h" #include "util-inl.h"