-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.cpp
50 lines (40 loc) · 1.02 KB
/
library.cpp
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
#include <nan.h>
#include <iostream>
#include <chrono>
#include <thread>
#ifdef _WIN32
#include <processthreadsapi.h>
#else
#include <unistd.h>
#endif
using Nan::New;
using Nan::Set;
using Nan::GetFunction;
using v8::FunctionTemplate;
using v8::String;
using v8::Integer;
using namespace std;
NAN_METHOD(ExampleMethod)
{
#ifdef _WIN32
DWORD pid = GetProcessId(GetCurrentProcess());
#else
pid_t pid = getpid();
#endif
cout << "ExampleMethod is called from pid " << pid << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
int randomNumber = 1;
for (int i = 1; i <= 100; i++)
randomNumber += i;
info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(randomNumber)));
}
NAN_MODULE_INIT(Init)
{
Set(target, New<String>("exampleMethod").ToLocalChecked(), GetFunction(New<FunctionTemplate>(ExampleMethod)).ToLocalChecked());
}
// Register native add on
#if NODE_MODULE_VERSION >= 80
NAN_MODULE_WORKER_ENABLED(SimpleNativeAddon, Init)
#else
NODE_MODULE(SimpleNativeAddon, Init)
#endif