Skip to content

Commit e97b961

Browse files
defunctzombiebnoordhuis
authored andcommitted
add node::SetMethod and node::SetPrototypeMethod
defines cannot be used if the callback is a templated and has multiple template arguments. The comma separating the arguments breaks the preprocessor argument handling. Using a templated function is clearer and more idiomatic in c++.
1 parent 836344c commit e97b961

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/node.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,25 @@ uv_loop_t* Loop();
101101
static_cast<v8::PropertyAttribute>( \
102102
v8::ReadOnly|v8::DontDelete))
103103

104-
#define NODE_SET_METHOD(obj, name, callback) \
105-
obj->Set(v8::String::NewSymbol(name), \
106-
v8::FunctionTemplate::New(callback)->GetFunction())
107-
108-
#define NODE_SET_PROTOTYPE_METHOD(templ, name, callback) \
109-
do { \
110-
v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \
111-
v8::Local<v8::FunctionTemplate> __callback##_TEM = \
112-
v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
113-
__callback##_SIG); \
114-
templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \
115-
__callback##_TEM); \
116-
} while (0)
104+
template <typename target_t>
105+
void SetMethod(target_t obj, const char* name,
106+
v8::InvocationCallback callback)
107+
{
108+
obj->Set(v8::String::NewSymbol(name),
109+
v8::FunctionTemplate::New(callback)->GetFunction());
110+
}
111+
112+
template <typename target_t>
113+
void SetPrototypeMethod(target_t target,
114+
const char* name, v8::InvocationCallback callback)
115+
{
116+
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback);
117+
target->PrototypeTemplate()->Set(v8::String::NewSymbol(name), templ);
118+
}
119+
120+
// for backwards compatibility
121+
#define NODE_SET_METHOD node::SetMethod
122+
#define NODE_SET_PROTOTYPE_METHOD node::SetPrototypeMethod
117123

118124
enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX};
119125
enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,

0 commit comments

Comments
 (0)