-
-
Save TooTallNate/1f48f89f8250f3f05839 to your computer and use it in GitHub Desktop.
damn C++ templates... (revised to use a macro instead...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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