Last active
April 7, 2016 14:37
-
-
Save mateogianolio/497938cc5c7354497a81dde853dad80d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| 'targets': [ | |
| { | |
| 'target_name': 'math', | |
| 'sources': [ | |
| 'factorial.cc' | |
| ] | |
| } | |
| ] | |
| } |
This file contains hidden or 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 <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) |
This file contains hidden or 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
| (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