Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files

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++.
  • Loading branch information...
defunctzombie authored and bnoordhuis committed Jan 30, 2012
1 parent 836344c commit e97b961815b8804e17d6793101960db53e4c32a2
Showing with 19 additions and 13 deletions.
  1. +19 −13 src/node.h
@@ -101,19 +101,25 @@ uv_loop_t* Loop();
static_cast<v8::PropertyAttribute>( \

#define NODE_SET_METHOD(obj, name, callback) \
obj->Set(v8::String::NewSymbol(name), \

#define NODE_SET_PROTOTYPE_METHOD(templ, name, callback) \
do { \
v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \
v8::Local<v8::FunctionTemplate> __callback##_TEM = \
v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
__callback##_SIG); \
templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \
__callback##_TEM); \
} while (0)
template <typename target_t>
void SetMethod(target_t obj, const char* name,
v8::InvocationCallback callback)

template <typename target_t>
void SetPrototypeMethod(target_t target,
const char* name, v8::InvocationCallback callback)
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback);
target->PrototypeTemplate()->Set(v8::String::NewSymbol(name), templ);

// for backwards compatibility
#define NODE_SET_METHOD node::SetMethod
#define NODE_SET_PROTOTYPE_METHOD node::SetPrototypeMethod

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

0 comments on commit e97b961

Please sign in to comment.
You can’t perform that action at this time.