Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
add node::SetMethod and node::SetPrototypeMethod
Browse files Browse the repository at this point in the history
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 e97b961
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/node.h
Expand Up @@ -101,19 +101,25 @@ uv_loop_t* Loop();
static_cast<v8::PropertyAttribute>( \
v8::ReadOnly|v8::DontDelete))

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

#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)
{
obj->Set(v8::String::NewSymbol(name),
v8::FunctionTemplate::New(callback)->GetFunction());
}

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,
Expand Down

0 comments on commit e97b961

Please sign in to comment.