Skip to content

Instantly share code, notes, and snippets.

@mateogianolio
Last active April 7, 2016 14:37
Show Gist options
  • Select an option

  • Save mateogianolio/497938cc5c7354497a81dde853dad80d to your computer and use it in GitHub Desktop.

Select an option

Save mateogianolio/497938cc5c7354497a81dde853dad80d to your computer and use it in GitHub Desktop.
{
'targets': [
{
'target_name': 'math',
'sources': [
'factorial.cc'
]
}
]
}
#include <node.h>
double factorial(double n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
void factorial(const v8::FunctionCallbackInfo<v8::Value>& info) {
info
.GetReturnValue()
.Set(v8::Number::New(
info.GetIsolate(),
factorial(info[0]->NumberValue())
));
}
void Init(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "factorial", factorial);
}
NODE_MODULE(addon, Init)
(function () {
'use strict';
var addon = require('./build/Release/factorial.node');
function factorial(n) {
if (n === 0)
return 1;
return n * factorial(n - 1);
}
console.log(addon.factorial(10), 'should equal', factorial(10));
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment