-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddon.cc
More file actions
151 lines (121 loc) · 2.7 KB
/
Copy pathaddon.cc
File metadata and controls
151 lines (121 loc) · 2.7 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <napi.h>
class ErrorAsyncWorker : public Napi::AsyncWorker
{
public:
ErrorAsyncWorker(
const Napi::Function& callback,
Napi::Error error
) : Napi::AsyncWorker(callback), error(error)
{
}
protected:
void Execute() override
{
// Do nothing...?
}
void OnOK() override
{
Napi::Env env = Env();
Callback().MakeCallback(
Receiver().Value(),
{
error.Value(),
env.Undefined()
}
);
}
void OnError(const Napi::Error& e) override
{
Napi::Env env = Env();
Callback().MakeCallback(
Receiver().Value(),
{
e.Value(),
env.Undefined()
}
);
}
private:
Napi::Error error;
};
class SumAsyncWorker : public Napi::AsyncWorker
{
public:
SumAsyncWorker(
const Napi::Function& callback,
const double arg0,
const double arg1
) : Napi::AsyncWorker(callback), arg0(arg0), arg1(arg1), sum(0)
{
}
protected:
void Execute() override
{
sum = arg0 + arg1;
}
void OnOK() override
{
Napi::Env env = Env();
Callback().MakeCallback(
Receiver().Value(),
{
env.Null(),
Napi::Number::New(env, sum)
}
);
}
void OnError(const Napi::Error& e) override
{
Napi::Env env = Env();
Callback().MakeCallback(
Receiver().Value(),
{
e.Value(),
env.Undefined()
}
);
}
private:
double arg0;
double arg1;
double sum;
};
void SumAsyncCallback(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
//
// Account for known potential issues that MUST be handled by
// synchronously throwing an `Error`
//
if (info.Length() < 3) {
Napi::TypeError::New(env, "Invalid argument count").ThrowAsJavaScriptException();
return;
}
if (!info[2].IsFunction()) {
Napi::TypeError::New(env, "Invalid argument types").ThrowAsJavaScriptException();
return;
}
//
// Handle all other potential issues asynchronously via the provided callback
//
Napi::Function cb = info[2].As<Napi::Function>();
if (info.Length() != 3) {
(new ErrorAsyncWorker(cb, Napi::TypeError::New(env, "Invalid argument count")))->Queue();
}
else if (!info[0].IsNumber() || !info[1].IsNumber()) {
(new ErrorAsyncWorker(cb, Napi::TypeError::New(env, "Invalid argument types")))->Queue();
}
else {
double arg0 = info[0].As<Napi::Number>().DoubleValue();
double arg1 = info[1].As<Napi::Number>().DoubleValue();
(new SumAsyncWorker(cb, arg0, arg1))->Queue();
}
return;
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(
Napi::String::New(env, "add"),
Napi::Function::New(env, SumAsyncCallback)
);
return exports;
}
NODE_API_MODULE(addon, Init)