|
2 | 2 | // boost
|
3 | 3 | #include <boost/variant.hpp>
|
4 | 4 |
|
5 |
| -// node |
6 |
| -#include <node.h> |
7 |
| -#include <node_buffer.h> |
| 5 | +// nan |
| 6 | +#include <nan.h> |
8 | 7 |
|
9 | 8 | // osmium
|
10 | 9 | #include <osmium/io/input_iterator.hpp>
|
@@ -110,11 +109,11 @@ namespace node_osmium {
|
110 | 109 | typedef boost::variant<location_handler_type&, JSHandler&, osmium::area::MultipolygonCollector<osmium::area::Assembler>::HandlerPass2&> some_handler_type;
|
111 | 110 |
|
112 | 111 | 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) { |
114 | 113 | struct javascript_error {};
|
115 | 114 |
|
116 |
| - v8::HandleScope scope; |
117 |
| - v8::TryCatch trycatch; |
| 115 | + Nan::EscapableHandleScope scope; |
| 116 | + Nan::TryCatch trycatch; |
118 | 117 | try {
|
119 | 118 | osmium::item_type last_type = osmium::item_type::undefined;
|
120 | 119 |
|
@@ -149,66 +148,74 @@ namespace node_osmium {
|
149 | 148 | }
|
150 | 149 | } catch (const javascript_error&) {
|
151 | 150 | 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()))); |
156 | 151 | }
|
157 |
| - return scope.Close(v8::Undefined()); |
| 152 | + return scope.Escape(Nan::Undefined()); |
158 | 153 | }
|
159 | 154 |
|
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()) { |
164 | 157 | std::vector<some_handler_type> handlers;
|
165 | 158 |
|
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; |
169 | 163 | }
|
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)) { |
172 | 166 | handlers.push_back(unwrap<JSHandler>(obj));
|
173 |
| - } else if (LocationHandlerWrap::constructor->HasInstance(obj)) { |
| 167 | + } else if (Nan::New(LocationHandlerWrap::constructor)->HasInstance(obj)) { |
174 | 168 | handlers.push_back(unwrap<LocationHandlerWrap>(obj));
|
175 |
| - } else if (MultipolygonHandlerWrap::constructor->HasInstance(obj)) { |
| 169 | + } else if (Nan::New(MultipolygonHandlerWrap::constructor)->HasInstance(obj)) { |
176 | 170 | handlers.push_back(unwrap<MultipolygonHandlerWrap>(obj));
|
177 | 171 | }
|
178 | 172 | }
|
179 | 173 |
|
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); |
183 | 178 |
|
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 | + } |
187 | 183 |
|
188 |
| - typedef osmium::io::InputIterator<osmium::io::Reader, osmium::OSMEntity> input_iterator; |
| 184 | + typedef osmium::io::InputIterator<osmium::io::Reader, osmium::OSMEntity> input_iterator; |
189 | 185 |
|
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); |
193 | 190 |
|
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 | + } |
197 | 195 |
|
198 |
| - typedef osmium::io::InputIterator<flex_reader_type, osmium::OSMEntity> input_iterator; |
| 196 | + typedef osmium::io::InputIterator<flex_reader_type, osmium::OSMEntity> input_iterator; |
199 | 197 |
|
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)); |
206 | 206 |
|
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; |
208 | 215 | }
|
209 | 216 | }
|
210 | 217 |
|
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()); |
212 | 219 | }
|
213 | 220 |
|
214 | 221 | } // namespace node_osmium
|
0 commit comments