Skip to content

Commit cc9bc09

Browse files
committed
Move to node-add-on-api
1 parent 1742697 commit cc9bc09

File tree

4 files changed

+83
-104
lines changed

4 files changed

+83
-104
lines changed

binding.gyp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"sources": ["src/addon.cc", "src/minkowski.cc"],
99
"cflags!": ["-fno-exceptions"],
1010
"cflags_cc!": ["-fno-exceptions"],
11+
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
1112
"conditions": [
1213
[
1314
'OS=="win"', {
@@ -47,8 +48,11 @@
4748
]
4849
],
4950
"include_dirs": [
50-
"<!(node -e \"require('nan')\")",
51+
"<!@(node -p \"require('node-addon-api').include\")",
5152
"./src/polygon/include"
53+
],
54+
"dependencies": [
55+
"<!(node -p \"require('node-addon-api').gyp\")"
5256
]
5357
}
5458
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
}
3030
],
3131
"dependencies": {
32-
"nan": "2.22.2",
32+
"node-addon-api": "^8.3.1",
3333
"node-gyp-build": "4.8.4"
3434
},
3535
"devDependencies": {

src/addon.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
#include <nan.h>
2-
// #include "minkowski.h"
1+
#include <napi.h>
32

4-
using Nan::GetFunction;
5-
using Nan::New;
6-
using Nan::Set;
7-
using v8::FunctionTemplate;
8-
using v8::Local;
9-
using v8::Object;
10-
using v8::String;
3+
Napi::Value CalculateNFP(const Napi::CallbackInfo& info);
114

12-
NAN_METHOD(calculateNFP);
13-
14-
NAN_MODULE_INIT(Init)
15-
{
16-
Nan::SetMethod(target, "calculateNFP", calculateNFP);
5+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
6+
exports.Set("calculateNFP", Napi::Function::New(env, CalculateNFP));
7+
return exports;
178
}
189

19-
NAN_MODULE_WORKER_ENABLED(addon, Init)
10+
NODE_API_MODULE(addon, Init)

src/minkowski.cc

Lines changed: 71 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1515

1616
#include <iostream>
1717
#include <string>
18-
#include <iostream>
1918
#include <sstream>
2019
#include <limits>
2120

22-
#include <nan.h>
21+
#include <napi.h>
2322
#include <boost/polygon/polygon.hpp>
2423

2524
#undef min
@@ -107,56 +106,42 @@ void convolve_two_polygon_sets(polygon_set& result, const polygon_set& a, const
107106

108107
double inputscale;
109108

110-
using v8::Local;
111-
using v8::Array;
112-
using v8::Isolate;
113-
using v8::String;
114-
using v8::Local;
115-
using v8::Object;
116-
using v8::Value;
117-
118-
using namespace boost::polygon;
119-
120-
NAN_METHOD(calculateNFP) {
121-
//std::stringstream buffer;
122-
//std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
123-
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
124-
125-
Isolate* isolate = info.GetIsolate();
126-
127-
Local<Object> group = Local<Object>::Cast(info[0]);
128-
Local<Array> A = Local<Array>::Cast(group->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"A",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
129-
Local<Array> B = Local<Array>::Cast(group->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"B",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
109+
Napi::Value CalculateNFP(const Napi::CallbackInfo& info) {
110+
Napi::Env env = info.Env();
111+
112+
Napi::Object group = info[0].As<Napi::Object>();
113+
Napi::Array A = group.Get("A").As<Napi::Array>();
114+
Napi::Array B = group.Get("B").As<Napi::Array>();
130115

131116
polygon_set a, b, c;
132117
std::vector<polygon> polys;
133118
std::vector<point> pts;
134119

135120
// get maximum bounds for scaling factor
136-
unsigned int len = A->Length();
121+
unsigned int len = A.Length();
137122
double Amaxx = 0;
138123
double Aminx = 0;
139124
double Amaxy = 0;
140125
double Aminy = 0;
141126
for (unsigned int i = 0; i < len; i++) {
142-
Local<Object> obj = Local<Object>::Cast(A->Get(isolate->GetCurrentContext(), i).ToLocalChecked());
143-
Amaxx = (std::max)(Amaxx, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
144-
Aminx = (std::min)(Aminx, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
145-
Amaxy = (std::max)(Amaxy, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
146-
Aminy = (std::min)(Aminy, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
127+
Napi::Object obj = A.Get(i).As<Napi::Object>();
128+
Amaxx = (std::max)(Amaxx, obj.Get("x").As<Napi::Number>().DoubleValue());
129+
Aminx = (std::min)(Aminx, obj.Get("x").As<Napi::Number>().DoubleValue());
130+
Amaxy = (std::max)(Amaxy, obj.Get("y").As<Napi::Number>().DoubleValue());
131+
Aminy = (std::min)(Aminy, obj.Get("y").As<Napi::Number>().DoubleValue());
147132
}
148133

149-
len = B->Length();
134+
len = B.Length();
150135
double Bmaxx = 0;
151136
double Bminx = 0;
152137
double Bmaxy = 0;
153138
double Bminy = 0;
154139
for (unsigned int i = 0; i < len; i++) {
155-
Local<Object> obj = Local<Object>::Cast(B->Get(isolate->GetCurrentContext(), i).ToLocalChecked());
156-
Bmaxx = (std::max)(Bmaxx, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
157-
Bminx = (std::min)(Bminx, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
158-
Bmaxy = (std::max)(Bmaxy, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
159-
Bminy = (std::min)(Bminy, (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
140+
Napi::Object obj = B.Get(i).As<Napi::Object>();
141+
Bmaxx = (std::max)(Bmaxx, obj.Get("x").As<Napi::Number>().DoubleValue());
142+
Bminx = (std::min)(Bminx, obj.Get("x").As<Napi::Number>().DoubleValue());
143+
Bmaxy = (std::max)(Bmaxy, obj.Get("y").As<Napi::Number>().DoubleValue());
144+
Bminy = (std::min)(Bminy, obj.Get("y").As<Napi::Number>().DoubleValue());
160145
}
161146

162147
double Cmaxx = Amaxx + Bmaxx;
@@ -171,19 +156,19 @@ NAN_METHOD(calculateNFP) {
171156
int maxi = std::numeric_limits<int>::max();
172157

173158
if(maxda < 1){
174-
maxda = 1;
159+
maxda = 1;
175160
}
176161

177162
// why 0.1? dunno. it doesn't screw up with 0.1
178163
inputscale = (0.1f * (double)(maxi)) / maxda;
179164

180165
//double scale = 1000;
181-
len = A->Length();
166+
len = A.Length();
182167

183168
for (unsigned int i = 0; i < len; i++) {
184-
Local<Object> obj = Local<Object>::Cast(A->Get(isolate->GetCurrentContext(), i).ToLocalChecked());
185-
int x = (int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
186-
int y = (int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
169+
Napi::Object obj = A.Get(i).As<Napi::Object>();
170+
int x = (int)(inputscale * obj.Get("x").As<Napi::Number>().DoubleValue());
171+
int y = (int)(inputscale * obj.Get("y").As<Napi::Number>().DoubleValue());
187172

188173
pts.push_back(point(x, y));
189174
}
@@ -193,40 +178,42 @@ NAN_METHOD(calculateNFP) {
193178
a+=poly;
194179

195180
// subtract holes from a here...
196-
Local<Array> holes = Local<Array>::Cast(A->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"children",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
197-
len = holes->Length();
198-
199-
for(unsigned int i=0; i<len; i++){
200-
Local<Array> hole = Local<Array>::Cast(holes->Get(isolate->GetCurrentContext(), i).ToLocalChecked());
201-
pts.clear();
202-
unsigned int hlen = hole->Length();
203-
for(unsigned int j=0; j<hlen; j++){
204-
Local<Object> obj = Local<Object>::Cast(hole->Get(isolate->GetCurrentContext(), j).ToLocalChecked());
205-
int x = (int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
206-
int y = (int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
207-
pts.push_back(point(x, y));
181+
if (A.Has("children")) {
182+
Napi::Array holes = A.Get("children").As<Napi::Array>();
183+
len = holes.Length();
184+
185+
for(unsigned int i=0; i<len; i++){
186+
Napi::Array hole = holes.Get(i).As<Napi::Array>();
187+
pts.clear();
188+
unsigned int hlen = hole.Length();
189+
for(unsigned int j=0; j<hlen; j++){
190+
Napi::Object obj = hole.Get(j).As<Napi::Object>();
191+
int x = (int)(inputscale * obj.Get("x").As<Napi::Number>().DoubleValue());
192+
int y = (int)(inputscale * obj.Get("y").As<Napi::Number>().DoubleValue());
193+
pts.push_back(point(x, y));
194+
}
195+
boost::polygon::set_points(poly, pts.begin(), pts.end());
196+
a -= poly;
208197
}
209-
boost::polygon::set_points(poly, pts.begin(), pts.end());
210-
a -= poly;
211198
}
212199

213200
//and then load points B
214201
pts.clear();
215-
len = B->Length();
202+
len = B.Length();
216203

217204
//javascript nfps are referenced with respect to the first point
218205
double xshift = 0;
219206
double yshift = 0;
220207

221208
for (unsigned int i = 0; i < len; i++) {
222-
Local<Object> obj = Local<Object>::Cast(B->Get(isolate->GetCurrentContext(), i).ToLocalChecked());
223-
int x = -(int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
224-
int y = -(int)(inputscale * (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust());
209+
Napi::Object obj = B.Get(i).As<Napi::Object>();
210+
int x = -(int)(inputscale * obj.Get("x").As<Napi::Number>().DoubleValue());
211+
int y = -(int)(inputscale * obj.Get("y").As<Napi::Number>().DoubleValue());
225212
pts.push_back(point(x, y));
226213

227214
if(i==0){
228-
xshift = (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust();
229-
yshift = (double)obj->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue(context).FromJust();
215+
xshift = obj.Get("x").As<Napi::Number>().DoubleValue();
216+
yshift = obj.Get("y").As<Napi::Number>().DoubleValue();
230217
}
231218
}
232219

@@ -238,47 +225,44 @@ NAN_METHOD(calculateNFP) {
238225
convolve_two_polygon_sets(c, a, b);
239226
c.get(polys);
240227

241-
Local<Array> result_list = Array::New(isolate);
228+
Napi::Array result_list = Napi::Array::New(env);
242229

243230
for(unsigned int i = 0; i < polys.size(); ++i ){
244231

245-
Local<Array> pointlist = Array::New(isolate);
246-
int j = 0;
247-
248-
for(polygon_traits<polygon>::iterator_type itr = polys[i].begin(); itr != polys[i].end(); ++itr) {
249-
Local<Object> p = Object::New(isolate);
250-
// std::cout << (double)(*itr).get(boost::polygon::HORIZONTAL) / inputscale << std::endl;
251-
p->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"x",v8::NewStringType::kNormal).ToLocalChecked(), v8::Number::New(isolate, ((double)(*itr).get(boost::polygon::HORIZONTAL)) / inputscale + xshift));
252-
p->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate,"y",v8::NewStringType::kNormal).ToLocalChecked(), v8::Number::New(isolate, ((double)(*itr).get(boost::polygon::VERTICAL)) / inputscale + yshift));
232+
Napi::Array pointlist = Napi::Array::New(env);
233+
int j = 0;
234+
235+
for(polygon_traits<polygon>::iterator_type itr = polys[i].begin(); itr != polys[i].end(); ++itr) {
236+
Napi::Object p = Napi::Object::New(env);
237+
p.Set("x", ((double)(*itr).get(boost::polygon::HORIZONTAL)) / inputscale + xshift);
238+
p.Set("y", ((double)(*itr).get(boost::polygon::VERTICAL)) / inputscale + yshift);
253239

254-
pointlist->Set(isolate->GetCurrentContext(), j, p);
240+
pointlist[j] = p;
255241
j++;
256242
}
257243

258244
// holes
259-
Local<Array> children = Array::New(isolate);
245+
Napi::Array children = Napi::Array::New(env);
260246
int k = 0;
261247
for(polygon_with_holes_traits<polygon>::iterator_holes_type itrh = begin_holes(polys[i]); itrh != end_holes(polys[i]); ++itrh){
262-
Local<Array> child = Array::New(isolate);
263-
int z = 0;
264-
for(polygon_traits<polygon>::iterator_type itr2 = (*itrh).begin(); itr2 != (*itrh).end(); ++itr2) {
265-
Local<Object> c = Object::New(isolate);
266-
c->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "x",v8::NewStringType::kNormal).ToLocalChecked(), v8::Number::New(isolate, ((double)(*itr2).get(boost::polygon::HORIZONTAL)) / inputscale + xshift));
267-
c->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "y",v8::NewStringType::kNormal).ToLocalChecked(), v8::Number::New(isolate, ((double)(*itr2).get(boost::polygon::VERTICAL)) / inputscale + yshift));
268-
269-
child->Set(isolate->GetCurrentContext(), z, c);
270-
z++;
271-
}
272-
children->Set(isolate->GetCurrentContext(), k, child);
273-
k++;
248+
Napi::Array child = Napi::Array::New(env);
249+
int z = 0;
250+
for(polygon_traits<polygon>::iterator_type itr2 = (*itrh).begin(); itr2 != (*itrh).end(); ++itr2) {
251+
Napi::Object c = Napi::Object::New(env);
252+
c.Set("x", ((double)(*itr2).get(boost::polygon::HORIZONTAL)) / inputscale + xshift);
253+
c.Set("y", ((double)(*itr2).get(boost::polygon::VERTICAL)) / inputscale + yshift);
254+
255+
child[z] = c;
256+
z++;
257+
}
258+
children[k] = child;
259+
k++;
274260
}
275261

276-
pointlist->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "children",v8::NewStringType::kNormal).ToLocalChecked(), children);
262+
pointlist.Set("children", children);
277263

278-
result_list->Set(isolate->GetCurrentContext(), i, pointlist);
264+
result_list[i] = pointlist;
279265
}
280266

281-
//std::string text = buffer.str();
282-
283-
info.GetReturnValue().Set(result_list);
267+
return result_list;
284268
}

0 commit comments

Comments
 (0)