forked from XadillaX/nyaa-nodejs-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.cc
More file actions
80 lines (66 loc) · 1.46 KB
/
addon.cc
File metadata and controls
80 lines (66 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <nan.h>
#include "nbody.h"
namespace __nbody__ {
using v8::Function;
using v8::Local;
using v8::Value;
class NBodyWorker : public Nan::AsyncWorker {
public:
NBodyWorker(unsigned int times, Nan::Callback* callback) :
times(times),
energy(0),
Nan::AsyncWorker(callback)
{
}
~NBodyWorker() {}
void Execute()
{
NBodySystem bodies;
for(unsigned int i = 0; i < times; i++) bodies.advance(0.01);
energy = bodies.energy();
}
void HandleOKCallback()
{
Nan::HandleScope scope;
Local<Value> argv[2] = {
Nan::Undefined(),
Nan::New(energy)
};
callback->Call(2, argv);
}
private:
unsigned int times;
double energy;
};
NAN_METHOD(Calc)
{
Nan::Callback* callback;
unsigned int times = 50000000;
if(info.Length() > 1)
{
double _times = Nan::To<double>(info[0]).FromJust();
if(_times < 0)
{
Nan::ThrowRangeError("Wrong times.");
return;
}
times = _times;
callback = new Nan::Callback(info[1].As<Function>());
}
else
if(info.Length() > 0)
{
callback = new Nan::Callback(info[0].As<Function>());
}
else
{
Nan::ThrowTypeError("Callback needed.");
}
Nan::AsyncQueueWorker(new NBodyWorker(times, callback));
}
NAN_MODULE_INIT(Init)
{
Nan::Export(target, "calc", Calc);
}
NODE_MODULE(reverse, Init)
}