Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit e97b961815b8804e17d6793101960db53e4c32a2 1 parent 836344c
@defunctzombie defunctzombie authored bnoordhuis committed
Showing with 19 additions and 13 deletions.
  1. +19 −13 src/node.h
View
32 src/node.h
@@ -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,
Please sign in to comment.
Something went wrong with that request. Please try again.