Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit cf788cf

Browse files
author
Dane Springmeyer
committed
Merge pull request #73 from osmcode/nan
[WIP] Port to Nan (Support node v4/5)
2 parents b3f4af0 + 4850ddb commit cf788cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1302
-1272
lines changed

binding.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
],
3131
"include_dirs": [
3232
"../libosmium/include/",
33-
"./src/"
33+
"./src/",
34+
"<!(node -e \"require('nan')\")"
3435
],
3536
"defines": [
3637
"_LARGEFILE_SOURCE",

package.json

+58-51
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
11
{
2-
"name": "osmium",
3-
"version": "0.4.5",
4-
"description": "Node.js bindings to Osmium",
5-
"url": "https://github.com/osmcode/node-osmium",
6-
"homepage": "http://osmcode.org/node-osmium",
7-
"author": "Dane Springmeyer <springmeyer>",
8-
"contributors": [
9-
"Jochen Topf <joto>"
10-
],
11-
"main": "./lib/osmium.js",
12-
"bugs": {
13-
"email": "[email protected]",
14-
"url": "https://github.com/osmcode/node-osmium/issues"
15-
},
16-
"keywords": [
17-
"osmium",
18-
"osm",
19-
"pbf",
20-
"changeset"
21-
],
22-
"repository": {
23-
"type": "git",
24-
"url": "git://github.com/osmcode/node-osmium.git"
25-
},
26-
"binary": {
27-
"module_name" : "osmium",
28-
"module_path" : "./lib/binding/",
29-
"host" : "https://mapbox-node-binary.s3.amazonaws.com",
30-
"remote_path" : "./{module_name}/v{version}",
31-
"package_name": "{node_abi}-{platform}-{arch}.tar.gz"
32-
},
33-
"dependencies": {
34-
"node-pre-gyp": "~0.6.6"
35-
},
36-
"bundledDependencies":["node-pre-gyp"],
37-
"devDependencies": {
38-
"aws-sdk": "~2.1.26",
39-
"geojson-coords": "0.0.0",
40-
"mocha": "1.x",
41-
"osm-testdata": "1.0.0",
42-
"turf-difference": "^2.0.0",
43-
"wellknown": "^0.4.0"
44-
},
45-
"licenses": [ { "type": "Boost" } ],
46-
"engines": {
47-
"node": ">= 0.10.0 < 0.11.0"
48-
},
49-
"scripts": {
50-
"prepublish": "npm ls",
51-
"install": "node-pre-gyp install --fallback-to-build",
52-
"test": "mocha -R spec --timeout 100000"
2+
"name": "osmium",
3+
"version": "0.4.5",
4+
"description": "Node.js bindings to Osmium",
5+
"url": "https://github.com/osmcode/node-osmium",
6+
"homepage": "http://osmcode.org/node-osmium",
7+
"author": "Dane Springmeyer <springmeyer>",
8+
"contributors": [
9+
"Jochen Topf <joto>"
10+
],
11+
"main": "./lib/osmium.js",
12+
"bugs": {
13+
"email": "[email protected]",
14+
"url": "https://github.com/osmcode/node-osmium/issues"
15+
},
16+
"keywords": [
17+
"osmium",
18+
"osm",
19+
"pbf",
20+
"changeset"
21+
],
22+
"repository": {
23+
"type": "git",
24+
"url": "git://github.com/osmcode/node-osmium.git"
25+
},
26+
"binary": {
27+
"module_name": "osmium",
28+
"module_path": "./lib/binding/",
29+
"host": "https://mapbox-node-binary.s3.amazonaws.com",
30+
"remote_path": "./{module_name}/v{version}",
31+
"package_name": "{node_abi}-{platform}-{arch}.tar.gz"
32+
},
33+
"dependencies": {
34+
"nan": "2.2.1",
35+
"node-pre-gyp": "~0.6.24"
36+
},
37+
"bundledDependencies": [
38+
"node-pre-gyp"
39+
],
40+
"devDependencies": {
41+
"aws-sdk": "~2.1.26",
42+
"geojson-coords": "0.0.0",
43+
"mocha": "1.x",
44+
"osm-testdata": "1.0.0",
45+
"turf-difference": "^2.0.0",
46+
"wellknown": "^0.4.0"
47+
},
48+
"licenses": [
49+
{
50+
"type": "Boost"
5351
}
52+
],
53+
"engines": {
54+
"node": ">= 0.10.0 < 0.11.0"
55+
},
56+
"scripts": {
57+
"prepublish": "npm ls",
58+
"install": "node-pre-gyp install --fallback-to-build",
59+
"test": "mocha -R spec --timeout 100000"
60+
}
5461
}

src/apply.cpp

+51-44
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// boost
33
#include <boost/variant.hpp>
44

5-
// node
6-
#include <node.h>
7-
#include <node_buffer.h>
5+
// nan
6+
#include <nan.h>
87

98
// osmium
109
#include <osmium/io/input_iterator.hpp>
@@ -110,11 +109,11 @@ namespace node_osmium {
110109
typedef boost::variant<location_handler_type&, JSHandler&, osmium::area::MultipolygonCollector<osmium::area::Assembler>::HandlerPass2&> some_handler_type;
111110

112111
template <class TIter>
113-
v8::Handle<v8::Value> apply_iterator(TIter it, TIter end, std::vector<some_handler_type>& handlers) {
112+
v8::Local<v8::Value> apply_iterator(TIter it, TIter end, std::vector<some_handler_type>& handlers) {
114113
struct javascript_error {};
115114

116-
v8::HandleScope scope;
117-
v8::TryCatch trycatch;
115+
Nan::EscapableHandleScope scope;
116+
Nan::TryCatch trycatch;
118117
try {
119118
osmium::item_type last_type = osmium::item_type::undefined;
120119

@@ -149,66 +148,74 @@ namespace node_osmium {
149148
}
150149
} catch (const javascript_error&) {
151150
trycatch.ReThrow();
152-
} catch (const std::exception& e) {
153-
std::string msg("osmium error: ");
154-
msg += e.what();
155-
return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str())));
156151
}
157-
return scope.Close(v8::Undefined());
152+
return scope.Escape(Nan::Undefined());
158153
}
159154

160-
v8::Handle<v8::Value> apply(const v8::Arguments& args) {
161-
v8::HandleScope scope;
162-
163-
if (args.Length() > 0 && args[0]->IsObject()) {
155+
NAN_METHOD(apply) {
156+
if (info.Length() > 0 && info[0]->IsObject()) {
164157
std::vector<some_handler_type> handlers;
165158

166-
for (int i=1; i != args.Length(); ++i) {
167-
if (!args[i]->IsObject()) {
168-
return ThrowException(v8::Exception::TypeError(v8::String::New("please provide handler objects as second and further parameters to apply()")));
159+
for (int i=1; i != info.Length(); ++i) {
160+
if (!info[i]->IsObject()) {
161+
Nan::ThrowTypeError(Nan::New("please provide handler objects as second and further parameters to apply()").ToLocalChecked());
162+
return;
169163
}
170-
auto obj = args[i]->ToObject();
171-
if (JSHandler::constructor->HasInstance(obj)) {
164+
auto obj = info[i]->ToObject();
165+
if (Nan::New(JSHandler::constructor)->HasInstance(obj)) {
172166
handlers.push_back(unwrap<JSHandler>(obj));
173-
} else if (LocationHandlerWrap::constructor->HasInstance(obj)) {
167+
} else if (Nan::New(LocationHandlerWrap::constructor)->HasInstance(obj)) {
174168
handlers.push_back(unwrap<LocationHandlerWrap>(obj));
175-
} else if (MultipolygonHandlerWrap::constructor->HasInstance(obj)) {
169+
} else if (Nan::New(MultipolygonHandlerWrap::constructor)->HasInstance(obj)) {
176170
handlers.push_back(unwrap<MultipolygonHandlerWrap>(obj));
177171
}
178172
}
179173

180-
auto source = args[0]->ToObject();
181-
if (BasicReaderWrap::constructor->HasInstance(source)) {
182-
osmium::io::Reader& reader = unwrap<BasicReaderWrap>(source);
174+
try {
175+
auto source = info[0]->ToObject();
176+
if (Nan::New(BasicReaderWrap::constructor)->HasInstance(source)) {
177+
osmium::io::Reader& reader = unwrap<BasicReaderWrap>(source);
183178

184-
if (reader.eof()) {
185-
return ThrowException(v8::Exception::Error(v8::String::New("apply() called on a reader that has reached EOF")));
186-
}
179+
if (reader.eof()) {
180+
Nan::ThrowError(Nan::New("apply() called on a reader that has reached EOF").ToLocalChecked());
181+
return;
182+
}
187183

188-
typedef osmium::io::InputIterator<osmium::io::Reader, osmium::OSMEntity> input_iterator;
184+
typedef osmium::io::InputIterator<osmium::io::Reader, osmium::OSMEntity> input_iterator;
189185

190-
return scope.Close(apply_iterator(input_iterator{reader}, input_iterator{}, handlers));
191-
} else if (FlexReaderWrap::constructor->HasInstance(source)) {
192-
flex_reader_type& reader = unwrap<FlexReaderWrap>(source);
186+
info.GetReturnValue().Set(apply_iterator(input_iterator{reader}, input_iterator{}, handlers));
187+
return;
188+
} else if (Nan::New(FlexReaderWrap::constructor)->HasInstance(source)) {
189+
flex_reader_type& reader = unwrap<FlexReaderWrap>(source);
193190

194-
if (reader.eof()) {
195-
return ThrowException(v8::Exception::Error(v8::String::New("apply() called on a reader that has reached EOF")));
196-
}
191+
if (reader.eof()) {
192+
Nan::ThrowError(Nan::New("apply() called on a reader that has reached EOF").ToLocalChecked());
193+
return;
194+
}
197195

198-
typedef osmium::io::InputIterator<flex_reader_type, osmium::OSMEntity> input_iterator;
196+
typedef osmium::io::InputIterator<flex_reader_type, osmium::OSMEntity> input_iterator;
199197

200-
return scope.Close(apply_iterator(input_iterator{reader}, input_iterator{}, handlers));
201-
} else if (BufferWrap::constructor->HasInstance(source)) {
202-
osmium::memory::Buffer& buffer = unwrap<BufferWrap>(source);
203-
return scope.Close(apply_iterator(buffer.begin(), buffer.end(), handlers));
204-
} else if (node::Buffer::HasInstance(source)) {
205-
osmium::memory::Buffer buffer(reinterpret_cast<unsigned char*>(node::Buffer::Data(source)), node::Buffer::Length(source));
198+
info.GetReturnValue().Set(apply_iterator(input_iterator{reader}, input_iterator{}, handlers));
199+
return;
200+
} else if (Nan::New(BufferWrap::constructor)->HasInstance(source)) {
201+
osmium::memory::Buffer& buffer = unwrap<BufferWrap>(source);
202+
info.GetReturnValue().Set(apply_iterator(buffer.begin(), buffer.end(), handlers));
203+
return;
204+
} else if (node::Buffer::HasInstance(source)) {
205+
osmium::memory::Buffer buffer(reinterpret_cast<unsigned char*>(node::Buffer::Data(source)), node::Buffer::Length(source));
206206

207-
return scope.Close(apply_iterator(buffer.begin<osmium::OSMEntity>(), buffer.end<osmium::OSMEntity>(), handlers));
207+
info.GetReturnValue().Set(apply_iterator(buffer.begin<osmium::OSMEntity>(), buffer.end<osmium::OSMEntity>(), handlers));
208+
return;
209+
}
210+
} catch (const std::exception& e) {
211+
std::string msg("osmium error: ");
212+
msg += e.what();
213+
Nan::ThrowError(Nan::New(msg).ToLocalChecked());
214+
return;
208215
}
209216
}
210217

211-
return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a BasicReader, FlexReader or Buffer object as first parameter")));
218+
Nan::ThrowTypeError(Nan::New("please provide a BasicReader, FlexReader or Buffer object as first parameter").ToLocalChecked());
212219
}
213220

214221
} // namespace node_osmium

src/apply.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#ifndef APPLY_HPP
22
#define APPLY_HPP
33

4-
// v8/node
5-
#include "include_v8.hpp"
4+
#include "include_nan.hpp"
65

76
namespace node_osmium {
87

9-
v8::Handle<v8::Value> apply(const v8::Arguments& args);
8+
NAN_METHOD(apply);
109

1110
} // namespace node_osmium
1211

0 commit comments

Comments
 (0)