forked from freezer333/cppwebify-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.cpp
More file actions
38 lines (27 loc) · 822 Bytes
/
addon.cpp
File metadata and controls
38 lines (27 loc) · 822 Bytes
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
#include <nan.h>
#include <functional>
#include <iostream>
#include "exchange.h"
#include "prime_sieve.h"
using namespace Nan;
using namespace std;
using namespace v8;
// Synchronous access to the `getPrimes()` function
NAN_METHOD(CalculatePrimes) {
Nan:: HandleScope scope;
int under = To<int>(info[0]).FromJust();
v8::Local<v8::Array> results = New<v8::Array>(under);
int i = 0;
Exchange x(
[&](void * data) {
Nan::Set(results, i, New<v8::Number>(*((int *) data)));
i++;
});
generate_primes(under, (void*)&x);
info.GetReturnValue().Set(results);
}
NAN_MODULE_INIT(Init) {
Nan::Set(target, New<String>("getPrimes").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(CalculatePrimes)).ToLocalChecked());
}
NODE_MODULE(addon, Init)