Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created October 8, 2012 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooTallNate/1f48f89f8250f3f05839 to your computer and use it in GitHub Desktop.
Save TooTallNate/1f48f89f8250f3f05839 to your computer and use it in GitHub Desktop.
damn C++ templates... (revised to use a macro instead...)
#include "v8.h"
#include "node.h"
using namespace v8;
using namespace node;
namespace {
#define PASTE2(a, b) a##b
#define PASTE(a, b) PASTE2(a, b)
#define FN(type, v8type, fn) \
Handle<Value> PASTE(node_, fn) (const Arguments& args) { \
HandleScope scope; \
type input = (type)args[0]->PASTE(v8type, Value)(); \
type output = fn(input); \
return scope.Close(Number::New(output)); \
}
/* functions we want to expose to JavaScript */
int foo (int a) {
return -a;
}
float bar (float f) {
return f * f;
}
/* define the V8 functions */
FN(int, Int32, foo);
FN(float, Number, bar);
void Initialize(Handle<Object> target) {
HandleScope scope;
NODE_SET_METHOD(target, "foo", node_foo);
NODE_SET_METHOD(target, "bar", node_bar);
}
}
NODE_MODULE(binding, Initialize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment