Skip to content

Commit 89e133a

Browse files
indutnyrvagg
authored andcommitted
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: #957 Reviewed-By: Chris Dickinson <[email protected]>
1 parent a558cd0 commit 89e133a

7 files changed

+100
-70
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
'src/req-wrap-inl.h',
157157
'src/string_bytes.h',
158158
'src/stream_base.h',
159+
'src/stream_base-inl.h',
159160
'src/stream_wrap.h',
160161
'src/tree.h',
161162
'src/util.h',

src/js_stream.cc

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "env-inl.h"
66
#include "node_buffer.h"
77
#include "stream_base.h"
8+
#include "stream_base-inl.h"
89
#include "v8.h"
910

1011
namespace node {

src/stream_base-inl.h

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef SRC_STREAM_BASE_INL_H_
2+
#define SRC_STREAM_BASE_INL_H_
3+
4+
#include "stream_base.h"
5+
6+
#include "node.h"
7+
#include "env.h"
8+
#include "env-inl.h"
9+
#include "v8.h"
10+
11+
namespace node {
12+
13+
using v8::FunctionCallbackInfo;
14+
using v8::FunctionTemplate;
15+
using v8::Handle;
16+
using v8::HandleScope;
17+
using v8::Local;
18+
using v8::PropertyAttribute;
19+
using v8::PropertyCallbackInfo;
20+
using v8::String;
21+
using v8::Value;
22+
23+
template <class Base>
24+
void StreamBase::AddMethods(Environment* env, Handle<FunctionTemplate> t) {
25+
HandleScope scope(env->isolate());
26+
27+
enum PropertyAttribute attributes =
28+
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
29+
t->InstanceTemplate()->SetAccessor(env->fd_string(),
30+
GetFD<Base>,
31+
nullptr,
32+
Handle<Value>(),
33+
v8::DEFAULT,
34+
attributes);
35+
36+
env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
37+
env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
38+
env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
39+
env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
40+
env->SetProtoMethod(t,
41+
"writeBuffer",
42+
JSMethod<Base, &StreamBase::WriteBuffer>);
43+
env->SetProtoMethod(t,
44+
"writeAsciiString",
45+
JSMethod<Base, &StreamBase::WriteString<ASCII> >);
46+
env->SetProtoMethod(t,
47+
"writeUtf8String",
48+
JSMethod<Base, &StreamBase::WriteString<UTF8> >);
49+
env->SetProtoMethod(t,
50+
"writeUcs2String",
51+
JSMethod<Base, &StreamBase::WriteString<UCS2> >);
52+
env->SetProtoMethod(t,
53+
"writeBinaryString",
54+
JSMethod<Base, &StreamBase::WriteString<BINARY> >);
55+
}
56+
57+
58+
template <class Base>
59+
void StreamBase::GetFD(Local<String> key,
60+
const PropertyCallbackInfo<Value>& args) {
61+
StreamBase* wrap = Unwrap<Base>(args.Holder());
62+
63+
if (!wrap->IsAlive())
64+
return args.GetReturnValue().Set(UV_EINVAL);
65+
66+
args.GetReturnValue().Set(wrap->GetFD());
67+
}
68+
69+
70+
template <class Base,
71+
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
72+
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
73+
StreamBase* wrap = Unwrap<Base>(args.Holder());
74+
75+
if (!wrap->IsAlive())
76+
return args.GetReturnValue().Set(UV_EINVAL);
77+
78+
args.GetReturnValue().Set((wrap->*Method)(args));
79+
}
80+
81+
} // namespace node
82+
83+
#endif // SRC_STREAM_BASE_INL_H_

src/stream_base.cc

+8-68
Original file line numberDiff line numberDiff line change
@@ -19,83 +19,23 @@ namespace node {
1919
using v8::Array;
2020
using v8::Context;
2121
using v8::FunctionCallbackInfo;
22-
using v8::FunctionTemplate;
2322
using v8::Handle;
2423
using v8::HandleScope;
2524
using v8::Integer;
2625
using v8::Local;
2726
using v8::Number;
2827
using v8::Object;
29-
using v8::PropertyAttribute;
30-
using v8::PropertyCallbackInfo;
3128
using v8::String;
3229
using v8::Value;
3330

34-
template void StreamBase::AddMethods<StreamWrap>(Environment* env,
35-
Handle<FunctionTemplate> t);
36-
template void StreamBase::AddMethods<TLSWrap>(Environment* env,
37-
Handle<FunctionTemplate> t);
38-
template void StreamBase::AddMethods<JSStream>(Environment* env,
39-
Handle<FunctionTemplate> t);
40-
41-
42-
template <class Base>
43-
void StreamBase::AddMethods(Environment* env, Handle<FunctionTemplate> t) {
44-
HandleScope scope(env->isolate());
45-
46-
enum PropertyAttribute attributes =
47-
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
48-
t->InstanceTemplate()->SetAccessor(env->fd_string(),
49-
GetFD<Base>,
50-
nullptr,
51-
Handle<Value>(),
52-
v8::DEFAULT,
53-
attributes);
54-
55-
env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
56-
env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
57-
env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
58-
env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
59-
env->SetProtoMethod(t,
60-
"writeBuffer",
61-
JSMethod<Base, &StreamBase::WriteBuffer>);
62-
env->SetProtoMethod(t,
63-
"writeAsciiString",
64-
JSMethod<Base, &StreamBase::WriteString<ASCII> >);
65-
env->SetProtoMethod(t,
66-
"writeUtf8String",
67-
JSMethod<Base, &StreamBase::WriteString<UTF8> >);
68-
env->SetProtoMethod(t,
69-
"writeUcs2String",
70-
JSMethod<Base, &StreamBase::WriteString<UCS2> >);
71-
env->SetProtoMethod(t,
72-
"writeBinaryString",
73-
JSMethod<Base, &StreamBase::WriteString<BINARY> >);
74-
}
75-
76-
77-
template <class Base>
78-
void StreamBase::GetFD(Local<String> key,
79-
const PropertyCallbackInfo<Value>& args) {
80-
StreamBase* wrap = Unwrap<Base>(args.Holder());
81-
82-
if (!wrap->IsAlive())
83-
return args.GetReturnValue().Set(UV_EINVAL);
84-
85-
args.GetReturnValue().Set(wrap->GetFD());
86-
}
87-
88-
89-
template <class Base,
90-
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
91-
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
92-
StreamBase* wrap = Unwrap<Base>(args.Holder());
93-
94-
if (!wrap->IsAlive())
95-
return args.GetReturnValue().Set(UV_EINVAL);
96-
97-
args.GetReturnValue().Set((wrap->*Method)(args));
98-
}
31+
template int StreamBase::WriteString<ASCII>(
32+
const FunctionCallbackInfo<Value>& args);
33+
template int StreamBase::WriteString<UTF8>(
34+
const FunctionCallbackInfo<Value>& args);
35+
template int StreamBase::WriteString<UCS2>(
36+
const FunctionCallbackInfo<Value>& args);
37+
template int StreamBase::WriteString<BINARY>(
38+
const FunctionCallbackInfo<Value>& args);
9939

10040

10141
int StreamBase::ReadStart(const FunctionCallbackInfo<Value>& args) {

src/stream_base.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class StreamResource {
159159
class StreamBase : public StreamResource {
160160
public:
161161
template <class Base>
162-
static void AddMethods(Environment* env,
163-
v8::Handle<v8::FunctionTemplate> target);
162+
static inline void AddMethods(Environment* env,
163+
v8::Handle<v8::FunctionTemplate> target);
164164

165165
virtual void* Cast() = 0;
166166
virtual bool IsAlive() = 0;

src/stream_wrap.cc

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "stream_wrap.h"
2+
#include "stream_base.h"
3+
#include "stream_base-inl.h"
4+
25
#include "env-inl.h"
36
#include "env.h"
47
#include "handle_wrap.h"

src/tls_wrap.cc

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "node_wrap.h" // WithGenericStream
1010
#include "node_counters.h"
1111
#include "node_internals.h"
12+
#include "stream_base.h"
13+
#include "stream_base-inl.h"
1214
#include "util.h"
1315
#include "util-inl.h"
1416

0 commit comments

Comments
 (0)