diff --git a/binding.gyp b/binding.gyp index bfb223a..299cf66 100644 --- a/binding.gyp +++ b/binding.gyp @@ -30,7 +30,8 @@ ], "include_dirs": [ "../libosmium/include/", - "./src/" + "./src/", + "", - "contributors": [ - "Jochen Topf " - ], - "main": "./lib/osmium.js", - "bugs": { - "email": "dane@mapbox.com", - "url": "https://github.com/osmcode/node-osmium/issues" - }, - "keywords": [ - "osmium", - "osm", - "pbf", - "changeset" - ], - "repository": { - "type": "git", - "url": "git://github.com/osmcode/node-osmium.git" - }, - "binary": { - "module_name" : "osmium", - "module_path" : "./lib/binding/", - "host" : "https://mapbox-node-binary.s3.amazonaws.com", - "remote_path" : "./{module_name}/v{version}", - "package_name": "{node_abi}-{platform}-{arch}.tar.gz" - }, - "dependencies": { - "node-pre-gyp": "~0.6.6" - }, - "bundledDependencies":["node-pre-gyp"], - "devDependencies": { - "aws-sdk": "~2.1.26", - "geojson-coords": "0.0.0", - "mocha": "1.x", - "osm-testdata": "1.0.0", - "turf-difference": "^2.0.0", - "wellknown": "^0.4.0" - }, - "licenses": [ { "type": "Boost" } ], - "engines": { - "node": ">= 0.10.0 < 0.11.0" - }, - "scripts": { - "prepublish": "npm ls", - "install": "node-pre-gyp install --fallback-to-build", - "test": "mocha -R spec --timeout 100000" + "name": "osmium", + "version": "0.4.5", + "description": "Node.js bindings to Osmium", + "url": "https://github.com/osmcode/node-osmium", + "homepage": "http://osmcode.org/node-osmium", + "author": "Dane Springmeyer ", + "contributors": [ + "Jochen Topf " + ], + "main": "./lib/osmium.js", + "bugs": { + "email": "dane@mapbox.com", + "url": "https://github.com/osmcode/node-osmium/issues" + }, + "keywords": [ + "osmium", + "osm", + "pbf", + "changeset" + ], + "repository": { + "type": "git", + "url": "git://github.com/osmcode/node-osmium.git" + }, + "binary": { + "module_name": "osmium", + "module_path": "./lib/binding/", + "host": "https://mapbox-node-binary.s3.amazonaws.com", + "remote_path": "./{module_name}/v{version}", + "package_name": "{node_abi}-{platform}-{arch}.tar.gz" + }, + "dependencies": { + "nan": "2.2.1", + "node-pre-gyp": "~0.6.24" + }, + "bundledDependencies": [ + "node-pre-gyp" + ], + "devDependencies": { + "aws-sdk": "~2.1.26", + "geojson-coords": "0.0.0", + "mocha": "1.x", + "osm-testdata": "1.0.0", + "turf-difference": "^2.0.0", + "wellknown": "^0.4.0" + }, + "licenses": [ + { + "type": "Boost" } + ], + "engines": { + "node": ">= 0.10.0 < 0.11.0" + }, + "scripts": { + "prepublish": "npm ls", + "install": "node-pre-gyp install --fallback-to-build", + "test": "mocha -R spec --timeout 100000" + } } diff --git a/src/apply.cpp b/src/apply.cpp index baab8c1..74e5308 100644 --- a/src/apply.cpp +++ b/src/apply.cpp @@ -2,9 +2,8 @@ // boost #include -// node -#include -#include +// nan +#include // osmium #include @@ -110,11 +109,11 @@ namespace node_osmium { typedef boost::variant::HandlerPass2&> some_handler_type; template - v8::Handle apply_iterator(TIter it, TIter end, std::vector& handlers) { + v8::Local apply_iterator(TIter it, TIter end, std::vector& handlers) { struct javascript_error {}; - v8::HandleScope scope; - v8::TryCatch trycatch; + Nan::EscapableHandleScope scope; + Nan::TryCatch trycatch; try { osmium::item_type last_type = osmium::item_type::undefined; @@ -149,66 +148,74 @@ namespace node_osmium { } } catch (const javascript_error&) { trycatch.ReThrow(); - } catch (const std::exception& e) { - std::string msg("osmium error: "); - msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); } - return scope.Close(v8::Undefined()); + return scope.Escape(Nan::Undefined()); } - v8::Handle apply(const v8::Arguments& args) { - v8::HandleScope scope; - - if (args.Length() > 0 && args[0]->IsObject()) { + NAN_METHOD(apply) { + if (info.Length() > 0 && info[0]->IsObject()) { std::vector handlers; - for (int i=1; i != args.Length(); ++i) { - if (!args[i]->IsObject()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide handler objects as second and further parameters to apply()"))); + for (int i=1; i != info.Length(); ++i) { + if (!info[i]->IsObject()) { + Nan::ThrowTypeError(Nan::New("please provide handler objects as second and further parameters to apply()").ToLocalChecked()); + return; } - auto obj = args[i]->ToObject(); - if (JSHandler::constructor->HasInstance(obj)) { + auto obj = info[i]->ToObject(); + if (Nan::New(JSHandler::constructor)->HasInstance(obj)) { handlers.push_back(unwrap(obj)); - } else if (LocationHandlerWrap::constructor->HasInstance(obj)) { + } else if (Nan::New(LocationHandlerWrap::constructor)->HasInstance(obj)) { handlers.push_back(unwrap(obj)); - } else if (MultipolygonHandlerWrap::constructor->HasInstance(obj)) { + } else if (Nan::New(MultipolygonHandlerWrap::constructor)->HasInstance(obj)) { handlers.push_back(unwrap(obj)); } } - auto source = args[0]->ToObject(); - if (BasicReaderWrap::constructor->HasInstance(source)) { - osmium::io::Reader& reader = unwrap(source); + try { + auto source = info[0]->ToObject(); + if (Nan::New(BasicReaderWrap::constructor)->HasInstance(source)) { + osmium::io::Reader& reader = unwrap(source); - if (reader.eof()) { - return ThrowException(v8::Exception::Error(v8::String::New("apply() called on a reader that has reached EOF"))); - } + if (reader.eof()) { + Nan::ThrowError(Nan::New("apply() called on a reader that has reached EOF").ToLocalChecked()); + return; + } - typedef osmium::io::InputIterator input_iterator; + typedef osmium::io::InputIterator input_iterator; - return scope.Close(apply_iterator(input_iterator{reader}, input_iterator{}, handlers)); - } else if (FlexReaderWrap::constructor->HasInstance(source)) { - flex_reader_type& reader = unwrap(source); + info.GetReturnValue().Set(apply_iterator(input_iterator{reader}, input_iterator{}, handlers)); + return; + } else if (Nan::New(FlexReaderWrap::constructor)->HasInstance(source)) { + flex_reader_type& reader = unwrap(source); - if (reader.eof()) { - return ThrowException(v8::Exception::Error(v8::String::New("apply() called on a reader that has reached EOF"))); - } + if (reader.eof()) { + Nan::ThrowError(Nan::New("apply() called on a reader that has reached EOF").ToLocalChecked()); + return; + } - typedef osmium::io::InputIterator input_iterator; + typedef osmium::io::InputIterator input_iterator; - return scope.Close(apply_iterator(input_iterator{reader}, input_iterator{}, handlers)); - } else if (BufferWrap::constructor->HasInstance(source)) { - osmium::memory::Buffer& buffer = unwrap(source); - return scope.Close(apply_iterator(buffer.begin(), buffer.end(), handlers)); - } else if (node::Buffer::HasInstance(source)) { - osmium::memory::Buffer buffer(reinterpret_cast(node::Buffer::Data(source)), node::Buffer::Length(source)); + info.GetReturnValue().Set(apply_iterator(input_iterator{reader}, input_iterator{}, handlers)); + return; + } else if (Nan::New(BufferWrap::constructor)->HasInstance(source)) { + osmium::memory::Buffer& buffer = unwrap(source); + info.GetReturnValue().Set(apply_iterator(buffer.begin(), buffer.end(), handlers)); + return; + } else if (node::Buffer::HasInstance(source)) { + osmium::memory::Buffer buffer(reinterpret_cast(node::Buffer::Data(source)), node::Buffer::Length(source)); - return scope.Close(apply_iterator(buffer.begin(), buffer.end(), handlers)); + info.GetReturnValue().Set(apply_iterator(buffer.begin(), buffer.end(), handlers)); + return; + } + } catch (const std::exception& e) { + std::string msg("osmium error: "); + msg += e.what(); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } } - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a BasicReader, FlexReader or Buffer object as first parameter"))); + Nan::ThrowTypeError(Nan::New("please provide a BasicReader, FlexReader or Buffer object as first parameter").ToLocalChecked()); } } // namespace node_osmium diff --git a/src/apply.hpp b/src/apply.hpp index e0a5115..be8dbcd 100644 --- a/src/apply.hpp +++ b/src/apply.hpp @@ -1,12 +1,11 @@ #ifndef APPLY_HPP #define APPLY_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" namespace node_osmium { - v8::Handle apply(const v8::Arguments& args); + NAN_METHOD(apply); } // namespace node_osmium diff --git a/src/basic_reader_wrap.cpp b/src/basic_reader_wrap.cpp index 0346305..f80b858 100644 --- a/src/basic_reader_wrap.cpp +++ b/src/basic_reader_wrap.cpp @@ -19,124 +19,137 @@ namespace node_osmium { - extern v8::Persistent module; - - v8::Persistent BasicReaderWrap::constructor; - - void BasicReaderWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(BasicReaderWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_BasicReader); - node::SetPrototypeMethod(constructor, "header", header); - node::SetPrototypeMethod(constructor, "close", close); - node::SetPrototypeMethod(constructor, "read", read); - node::SetPrototypeMethod(constructor, "read_all", read_all); - target->Set(symbol_BasicReader, constructor->GetFunction()); + extern Nan::Persistent module; + + Nan::Persistent BasicReaderWrap::constructor; + + void BasicReaderWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(BasicReaderWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_BasicReader)); + Nan::SetPrototypeMethod(lcons, "header", header); + Nan::SetPrototypeMethod(lcons, "close", close); + Nan::SetPrototypeMethod(lcons, "read", read); + Nan::SetPrototypeMethod(lcons, "read_all", read_all); + target->Set(Nan::New(symbol_BasicReader), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle BasicReaderWrap::New(const v8::Arguments& args) { - v8::HandleScope scope; - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(BasicReaderWrap::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } - if (args.Length() < 1 || args.Length() > 2) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a File object or string for the first argument and optional options v8::Object when creating a BasicReader"))); + if (info.Length() < 1 || info.Length() > 2) { + Nan::ThrowTypeError(Nan::New("please provide a File object or string for the first argument and optional options v8::Object when creating a BasicReader").ToLocalChecked()); + return; } try { osmium::osm_entity_bits::type read_which_entities = osmium::osm_entity_bits::all; - if (args.Length() == 2) { - if (!args[1]->IsObject()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("Second argument to BasicReader constructor must be object"))); + if (info.Length() == 2) { + if (!info[1]->IsObject()) { + Nan::ThrowTypeError(Nan::New("Second argument to BasicReader constructor must be object").ToLocalChecked()); + return; } - read_which_entities = object_to_entity_bits(args[1]->ToObject()); + read_which_entities = object_to_entity_bits(info[1]->ToObject()); } - if (args[0]->IsString()) { - v8::String::Utf8Value filename { args[0] }; + if (info[0]->IsString()) { + v8::String::Utf8Value filename { info[0] }; osmium::io::File file(*filename); BasicReaderWrap* reader_wrap = new BasicReaderWrap(file, read_which_entities); - reader_wrap->Wrap(args.This()); - return args.This(); - } else if (args[0]->IsObject() && FileWrap::constructor->HasInstance(args[0]->ToObject())) { - v8::Local file_obj = args[0]->ToObject(); + reader_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; + } else if (info[0]->IsObject() && Nan::New(FileWrap::constructor)->HasInstance(info[0]->ToObject())) { + v8::Local file_obj = info[0]->ToObject(); BasicReaderWrap* reader_wrap = new BasicReaderWrap(unwrap(file_obj), read_which_entities); - reader_wrap->Wrap(args.This()); - return args.This(); + reader_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a File object or string for the first argument when creating a BasicReader"))); + Nan::ThrowTypeError(Nan::New("please provide a File object or string for the first argument when creating a BasicReader").ToLocalChecked()); + return; } } catch (const std::exception& ex) { - return ThrowException(v8::Exception::TypeError(v8::String::New(ex.what()))); + Nan::ThrowTypeError(Nan::New(ex.what()).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle BasicReaderWrap::header(const v8::Arguments& args) { + NAN_METHOD(BasicReaderWrap::header) { INSTANCE_CHECK(BasicReaderWrap, "BasicReader", "header"); - v8::HandleScope scope; - v8::Local obj = v8::Object::New(); - const osmium::io::Header& header = unwrap(args.This()).header(); - obj->Set(symbol_generator, v8::String::New(header.get("generator").c_str())); + v8::Local obj = Nan::New(); + const osmium::io::Header& header = unwrap(info.This()).header(); + obj->Set(Nan::New(symbol_generator), Nan::New(header.get("generator")).ToLocalChecked()); - auto bounds_array = v8::Array::New(header.boxes().size()); + auto bounds_array = Nan::New(header.boxes().size()); int i=0; for (const osmium::Box& box : header.boxes()) { bounds_array->Set(i++, create_js_box(box)); } - obj->Set(symbol_bounds, bounds_array); - return scope.Close(obj); + obj->Set(Nan::New(symbol_bounds), bounds_array); + info.GetReturnValue().Set(obj); + return; } - v8::Handle BasicReaderWrap::close(const v8::Arguments& args) { + NAN_METHOD(BasicReaderWrap::close) { INSTANCE_CHECK(BasicReaderWrap, "BasicReader", "close"); - v8::HandleScope scope; try { - unwrap(args.This()).close(); + unwrap(info.This()).close(); } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle BasicReaderWrap::read(const v8::Arguments& args) { + NAN_METHOD(BasicReaderWrap::read) { INSTANCE_CHECK(BasicReaderWrap, "BasicReader", "read"); - v8::HandleScope scope; try { - osmium::memory::Buffer buffer = unwrap(args.This()).read(); + osmium::memory::Buffer buffer = unwrap(info.This()).read(); if (buffer) { - return scope.Close(new_external(std::move(buffer))); + info.GetReturnValue().Set(new_external(std::move(buffer))); + return; } } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle BasicReaderWrap::read_all(const v8::Arguments& args) { + NAN_METHOD(BasicReaderWrap::read_all) { INSTANCE_CHECK(BasicReaderWrap, "BasicReader", "read_all"); osmium::memory::Buffer buffer(1024*1024, osmium::memory::Buffer::auto_grow::yes); - v8::HandleScope scope; try { - osmium::io::Reader& reader = unwrap(args.This()); + osmium::io::Reader& reader = unwrap(info.This()); while (osmium::memory::Buffer read_buffer = reader.read()) { buffer.add_buffer(read_buffer); buffer.commit(); } if (buffer) { - return scope.Close(new_external(std::move(buffer))); + info.GetReturnValue().Set(new_external(std::move(buffer))); + return; } } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/basic_reader_wrap.hpp b/src/basic_reader_wrap.hpp index 5566bb5..4f25600 100644 --- a/src/basic_reader_wrap.hpp +++ b/src/basic_reader_wrap.hpp @@ -1,9 +1,7 @@ #ifndef BASIC_READER_WRAP_HPP #define BASIC_READER_WRAP_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -14,23 +12,23 @@ namespace node_osmium { - class BasicReaderWrap : public node::ObjectWrap { + class BasicReaderWrap : public Nan::ObjectWrap { - static v8::Handle header(const v8::Arguments& args); - static v8::Handle close(const v8::Arguments& args); - static v8::Handle read(const v8::Arguments& args); - static v8::Handle read_all(const v8::Arguments& args); + static NAN_METHOD(header); + static NAN_METHOD(close); + static NAN_METHOD(read); + static NAN_METHOD(read_all); osmium::io::Reader m_this; public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); BasicReaderWrap(const osmium::io::File& file, osmium::osm_entity_bits::type entities) : - ObjectWrap(), + Nan::ObjectWrap(), m_this(file, entities) { } diff --git a/src/buffer_wrap.cpp b/src/buffer_wrap.cpp index 78803e1..61912b8 100644 --- a/src/buffer_wrap.cpp +++ b/src/buffer_wrap.cpp @@ -1,9 +1,6 @@ #include -// node -#include - // osmium #include @@ -20,64 +17,66 @@ namespace node_osmium { - v8::Persistent BufferWrap::constructor; - - void BufferWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(BufferWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Buffer); - node::SetPrototypeMethod(constructor, "clear", clear); - node::SetPrototypeMethod(constructor, "next", next); - node::SetPrototypeMethod(constructor, "filter_point_in_time", filter_point_in_time); - node::SetPrototypeMethod(constructor, "create_node_buffer", create_node_buffer); - target->Set(symbol_Buffer, constructor->GetFunction()); + Nan::Persistent BufferWrap::constructor; + + void BufferWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(BufferWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Buffer)); + Nan::SetPrototypeMethod(lcons, "clear", clear); + Nan::SetPrototypeMethod(lcons, "next", next); + Nan::SetPrototypeMethod(lcons, "filter_point_in_time", filter_point_in_time); + Nan::SetPrototypeMethod(lcons, "create_node_buffer", create_node_buffer); + target->Set(Nan::New(symbol_Buffer), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle BufferWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); - } else if (args.Length() == 1 && args[0]->IsObject()) { - auto obj = args[0]->ToObject(); + NAN_METHOD(BufferWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; + } else if (info.Length() == 1 && info[0]->IsObject()) { + auto obj = info[0]->ToObject(); if (node::Buffer::HasInstance(obj)) { try { osmium::memory::Buffer buffer(reinterpret_cast(node::Buffer::Data(obj)), node::Buffer::Length(obj)); BufferWrap* buffer_wrap = new BufferWrap(std::move(buffer)); - buffer_wrap->Wrap(args.This()); + buffer_wrap->Wrap(info.This()); // Store the node::Buffer in the new osmium::Buffer object // so the node::Buffer doesn't go away if it goes out of scope // outside this function. - args.This()->Set(NODE_PSYMBOL("_data"), obj); - - return args.This(); + info.This()->Set(Nan::New("_data").ToLocalChecked(), obj); + info.GetReturnValue().Set(info.This()); + return; } catch (std::exception const& ex) { - return ThrowException(v8::Exception::TypeError(v8::String::New(ex.what()))); + Nan::ThrowTypeError(Nan::New(ex.what()).ToLocalChecked()); + return; } } } - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Buffer takes a single argument, a node::Buffer"))); + Nan::ThrowTypeError(Nan::New("osmium.Buffer takes a single argument, a node::Buffer").ToLocalChecked()); + return; } - v8::Handle BufferWrap::clear(const v8::Arguments& args) { + NAN_METHOD(BufferWrap::clear) { INSTANCE_CHECK(BufferWrap, "Buffer", "clear"); - BufferWrap* buffer_wrap = node::ObjectWrap::Unwrap(args.This()); + BufferWrap* buffer_wrap = Nan::ObjectWrap::Unwrap(info.This()); buffer_wrap->m_this = std::move(osmium::memory::Buffer()); buffer_wrap->m_iterator = buffer_wrap->m_this.end(); - return v8::Undefined(); + info.GetReturnValue().Set(Nan::Undefined()); } - v8::Handle BufferWrap::next(const v8::Arguments& args) { + NAN_METHOD(BufferWrap::next) { INSTANCE_CHECK(BufferWrap, "Buffer", "next"); - BufferWrap* buffer_wrap = node::ObjectWrap::Unwrap(args.This()); - - v8::HandleScope scope; + BufferWrap* buffer_wrap = Nan::ObjectWrap::Unwrap(info.This()); int filter_id = 0; - if (args.Length() == 1 && args[0]->IsInt32()) { - filter_id = args[0]->ToInt32()->Value(); + if (info.Length() == 1 && info[0]->IsInt32()) { + filter_id = info[0]->ToInt32()->Value(); } while (buffer_wrap->m_iterator != buffer_wrap->m_this.end()) { @@ -87,24 +86,29 @@ namespace node_osmium { if (Filter::get_filter(filter_id).match(entity)) { switch (entity.type()) { case osmium::item_type::node: { - node::ObjectWrap::Unwrap(the_Node)->set(entity); - return scope.Close(the_Node); + Nan::ObjectWrap::Unwrap(Nan::New(the_Node))->set(entity); + info.GetReturnValue().Set(Nan::New(the_Node)); + return; } case osmium::item_type::way: { - node::ObjectWrap::Unwrap(the_Way)->set(entity); - return scope.Close(the_Way); + Nan::ObjectWrap::Unwrap(Nan::New(the_Way))->set(entity); + info.GetReturnValue().Set(Nan::New(the_Way)); + return; } case osmium::item_type::relation: { - node::ObjectWrap::Unwrap(the_Relation)->set(entity); - return scope.Close(the_Relation); + Nan::ObjectWrap::Unwrap(Nan::New(the_Relation))->set(entity); + info.GetReturnValue().Set(Nan::New(the_Relation)); + return; } case osmium::item_type::area: { - node::ObjectWrap::Unwrap(the_Area)->set(entity); - return scope.Close(the_Area); + Nan::ObjectWrap::Unwrap(Nan::New(the_Area))->set(entity); + info.GetReturnValue().Set(Nan::New(the_Area)); + return; } case osmium::item_type::changeset: { - node::ObjectWrap::Unwrap(the_Changeset)->set(entity); - return scope.Close(the_Changeset); + Nan::ObjectWrap::Unwrap(Nan::New(the_Changeset))->set(entity); + info.GetReturnValue().Set(Nan::New(the_Changeset)); + return; } default: assert(false); @@ -113,28 +117,29 @@ namespace node_osmium { } } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle BufferWrap::filter_point_in_time(const v8::Arguments& args) { + NAN_METHOD(BufferWrap::filter_point_in_time) { INSTANCE_CHECK(BufferWrap, "Buffer", "filter_point_in_time"); - v8::HandleScope scope; - if (args.Length() != 1) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a point in time as first and only argument"))); + if (info.Length() != 1) { + Nan::ThrowTypeError(Nan::New("please provide a point in time as first and only argument").ToLocalChecked()); + return; } osmium::Timestamp point_in_time; - if (args[0]->IsInt32()) { - point_in_time = args[0]->Int32Value(); - } else if (args[0]->IsString()) { - v8::String::Utf8Value time_string { args[0] }; + if (info[0]->IsInt32()) { + point_in_time = info[0]->Int32Value(); + } else if (info[0]->IsString()) { + v8::String::Utf8Value time_string { info[0] }; point_in_time = osmium::Timestamp(*time_string); - } else if (args[0]->IsDate()) { - point_in_time = osmium::Timestamp(static_cast(v8::Date::Cast(*args[0])->NumberValue() / 1000)); + } else if (info[0]->IsDate()) { + point_in_time = osmium::Timestamp(static_cast(v8::Date::Cast(*info[0])->NumberValue() / 1000)); } typedef osmium::DiffIterator> diff_iterator; - osmium::memory::Buffer& buffer = unwrap(args.This()); + osmium::memory::Buffer& buffer = unwrap(info.This()); osmium::memory::Buffer fbuffer(buffer.committed(), osmium::memory::Buffer::auto_grow::yes); { auto dbegin = diff_iterator(buffer.begin(), buffer.end()); @@ -148,35 +153,36 @@ namespace node_osmium { }); } - return scope.Close(new_external(std::move(fbuffer))); + info.GetReturnValue().Set(new_external(std::move(fbuffer))); + return; } - v8::Handle BufferWrap::create_node_buffer(const v8::Arguments& args) { + NAN_METHOD(BufferWrap::create_node_buffer) { INSTANCE_CHECK(BufferWrap, "Buffer", "create_node_buffer"); - osmium::memory::Buffer& buffer = unwrap(args.This()); + osmium::memory::Buffer& buffer = unwrap(info.This()); if (!buffer) { - return v8::Undefined(); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::HandleScope scope; - int length = buffer.committed(); - node::Buffer* slow_buffer = node::Buffer::New(length); + v8::Local slow_buffer = Nan::NewBuffer(length).ToLocalChecked(); std::copy_n(buffer.data(), length, node::Buffer::Data(slow_buffer)); - v8::Local global = v8::Context::GetCurrent()->Global(); + v8::Local global = Nan::GetCurrentContext()->Global(); v8::Local buffer_constructor = - v8::Local::Cast(global->Get(v8::String::New("Buffer"))); + v8::Local::Cast(global->Get(Nan::New("Buffer").ToLocalChecked())); - v8::Handle constructor_args[3] = { - slow_buffer->handle_, - v8::Integer::New(length), - v8::Integer::New(0) + v8::Local constructor_info[3] = { + slow_buffer, + Nan::New(length), + Nan::New(0) }; - return scope.Close(buffer_constructor->NewInstance(3, constructor_args)); + info.GetReturnValue().Set(buffer_constructor->NewInstance(3, constructor_info)); + return; } diff --git a/src/buffer_wrap.hpp b/src/buffer_wrap.hpp index 7a808ac..1e0f424 100644 --- a/src/buffer_wrap.hpp +++ b/src/buffer_wrap.hpp @@ -4,33 +4,31 @@ // c++ #include -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include namespace node_osmium { - class BufferWrap : public node::ObjectWrap { + class BufferWrap : public Nan::ObjectWrap { osmium::memory::Buffer m_this; osmium::memory::Buffer::iterator m_iterator; - static v8::Handle clear(const v8::Arguments& args); - static v8::Handle next(const v8::Arguments& args); - static v8::Handle filter_point_in_time(const v8::Arguments& args); - static v8::Handle create_node_buffer(const v8::Arguments& args); + static NAN_METHOD(clear); + static NAN_METHOD(next); + static NAN_METHOD(filter_point_in_time); + static NAN_METHOD(create_node_buffer); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); BufferWrap(osmium::memory::Buffer&& buffer) : - ObjectWrap(), + Nan::ObjectWrap(), m_this(std::move(buffer)), m_iterator() { if (m_this) { diff --git a/src/file_wrap.cpp b/src/file_wrap.cpp index 63cf415..08eba94 100644 --- a/src/file_wrap.cpp +++ b/src/file_wrap.cpp @@ -2,67 +2,70 @@ // c++ #include -// node -#include - // node-osmium #include "node_osmium.hpp" #include "file_wrap.hpp" namespace node_osmium { - v8::Persistent FileWrap::constructor; + Nan::Persistent FileWrap::constructor; - void FileWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(FileWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_File); - target->Set(symbol_File, constructor->GetFunction()); + void FileWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(FileWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_File)); + target->Set(Nan::New(symbol_File), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle FileWrap::New(const v8::Arguments& args) { - v8::HandleScope scope; - - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(FileWrap::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } - if (args.Length() == 0 || args.Length() > 2) { - return ThrowException(v8::Exception::TypeError(v8::String::New("File is constructed with one or two arguments: filename or node.Buffer and optional format"))); + if (info.Length() == 0 || info.Length() > 2) { + Nan::ThrowTypeError(Nan::New("File is constructed with one or two arguments: filename or node.Buffer and optional format").ToLocalChecked()); + return; } std::string format; - if (args.Length() == 2) { - if (!args[1]->IsString()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("second argument to File constructor (format) must be a string"))); + if (info.Length() == 2) { + if (!info[1]->IsString()) { + Nan::ThrowTypeError(Nan::New("second argument to File constructor (format) must be a string").ToLocalChecked()); + return; } - v8::String::Utf8Value format_string { args[1] }; + v8::String::Utf8Value format_string { info[1] }; format = *format_string; } try { osmium::io::File file; - if (args[0]->IsString()) { - v8::String::Utf8Value filename { args[0] }; + if (info[0]->IsString()) { + v8::String::Utf8Value filename { info[0] }; file = osmium::io::File(*filename, format); - } else if (args[0]->IsObject() && node::Buffer::HasInstance(args[0]->ToObject())) { - auto source = args[0]->ToObject(); + } else if (info[0]->IsObject() && node::Buffer::HasInstance(info[0]->ToObject())) { + auto source = info[0]->ToObject(); file = osmium::io::File(node::Buffer::Data(source), node::Buffer::Length(source), format); } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("first argument to File constructor must be a string (filename) or node.Buffer"))); + Nan::ThrowTypeError(Nan::New("first argument to File constructor must be a string (filename) or node.Buffer").ToLocalChecked()); + return; } file.check(); FileWrap* file_wrap = new FileWrap(std::move(file)); - file_wrap->Wrap(args.This()); - return args.This(); + file_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } catch (const std::exception& e) { - return ThrowException(v8::Exception::TypeError(v8::String::New(e.what()))); + Nan::ThrowTypeError(Nan::New(e.what()).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/file_wrap.hpp b/src/file_wrap.hpp index 195c3f5..c7454db 100644 --- a/src/file_wrap.hpp +++ b/src/file_wrap.hpp @@ -5,27 +5,25 @@ #include #include -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include namespace node_osmium { - class FileWrap : public node::ObjectWrap { + class FileWrap : public Nan::ObjectWrap { osmium::io::File m_this; public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); FileWrap(osmium::io::File&& file) : - node::ObjectWrap(), + Nan::ObjectWrap(), m_this(std::move(file)) { } diff --git a/src/filter.cpp b/src/filter.cpp index 5c8ded0..66d4278 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -18,18 +18,18 @@ namespace node_osmium { all_filters.emplace_back(new Filter()); } - v8::Handle Filter::register_filter(const v8::Arguments& args) { - v8::HandleScope scope; - - if (args.Length() == 1 && args[0]->IsObject()) { - auto object = args[0]->ToObject(); + NAN_METHOD(Filter::register_filter) { + if (info.Length() == 1 && info[0]->IsObject()) { + auto object = info[0]->ToObject(); // XXX check that object is of class Filter Filter::all_filters.emplace_back(new Filter(object)); assert(Filter::all_filters.size() < std::numeric_limits::max()); - return scope.Close(v8::Integer::New(static_cast(Filter::all_filters.size() - 1))); + info.GetReturnValue().Set(Nan::New(static_cast(Filter::all_filters.size() - 1))); + return; } - return ThrowException(v8::Exception::Error(v8::String::New("registering filter failed"))); + Nan::ThrowError(Nan::New("registering filter failed").ToLocalChecked()); + return; } const Filter& Filter::get_filter(size_t id) { @@ -71,14 +71,14 @@ namespace node_osmium { m_tagged_entity_bits(osmium::osm_entity_bits::nothing) { } - Filter::Filter(v8::Handle object) : + Filter::Filter(v8::Local object) : m_entity_bits(osmium::osm_entity_bits::nothing), m_tagged_entity_bits(osmium::osm_entity_bits::nothing) { - setup_filter(v8::Array::Cast(*object->Get(NODE_PSYMBOL("_node"))), osmium::item_type::node); - setup_filter(v8::Array::Cast(*object->Get(NODE_PSYMBOL("_way"))), osmium::item_type::way); - setup_filter(v8::Array::Cast(*object->Get(NODE_PSYMBOL("_relation"))), osmium::item_type::relation); - setup_filter(v8::Array::Cast(*object->Get(NODE_PSYMBOL("_changeset"))), osmium::item_type::changeset); - setup_filter(v8::Array::Cast(*object->Get(NODE_PSYMBOL("_area"))), osmium::item_type::area); + setup_filter(v8::Array::Cast(*object->Get(Nan::New("_node").ToLocalChecked())), osmium::item_type::node); + setup_filter(v8::Array::Cast(*object->Get(Nan::New("_way").ToLocalChecked())), osmium::item_type::way); + setup_filter(v8::Array::Cast(*object->Get(Nan::New("_relation").ToLocalChecked())), osmium::item_type::relation); + setup_filter(v8::Array::Cast(*object->Get(Nan::New("_changeset").ToLocalChecked())), osmium::item_type::changeset); + setup_filter(v8::Array::Cast(*object->Get(Nan::New("_area").ToLocalChecked())), osmium::item_type::area); } bool Filter::match(const osmium::OSMEntity& entity) const { diff --git a/src/filter.hpp b/src/filter.hpp index 9695613..55cf846 100644 --- a/src/filter.hpp +++ b/src/filter.hpp @@ -2,8 +2,7 @@ // c++ #include -// nodejs -#include +#include "include_nan.hpp" // osmium #include @@ -28,11 +27,11 @@ namespace node_osmium { public: static void init_filters(); - static v8::Handle register_filter(const v8::Arguments& args); + static NAN_METHOD(register_filter); static const Filter& get_filter(size_t id); Filter(); - Filter(v8::Handle object); + Filter(v8::Local object); bool match(const osmium::OSMEntity& entity) const; diff --git a/src/flex_reader_wrap.cpp b/src/flex_reader_wrap.cpp index efd63a7..8b19fe9 100644 --- a/src/flex_reader_wrap.cpp +++ b/src/flex_reader_wrap.cpp @@ -4,9 +4,6 @@ #include #include -// node -#include - // node-osmium #include "node_osmium.hpp" #include "buffer_wrap.hpp" @@ -19,131 +16,145 @@ namespace node_osmium { - extern v8::Persistent module; - - v8::Persistent FlexReaderWrap::constructor; - - void FlexReaderWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(FlexReaderWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_FlexReader); - node::SetPrototypeMethod(constructor, "header", header); - node::SetPrototypeMethod(constructor, "close", close); - node::SetPrototypeMethod(constructor, "read", read); - node::SetPrototypeMethod(constructor, "read_all", read_all); - target->Set(symbol_FlexReader, constructor->GetFunction()); + extern Nan::Persistent module; + + Nan::Persistent FlexReaderWrap::constructor; + + void FlexReaderWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(FlexReaderWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_FlexReader)); + Nan::SetPrototypeMethod(lcons, "header", header); + Nan::SetPrototypeMethod(lcons, "close", close); + Nan::SetPrototypeMethod(lcons, "read", read); + Nan::SetPrototypeMethod(lcons, "read_all", read_all); + target->Set(Nan::New(symbol_FlexReader), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle FlexReaderWrap::New(const v8::Arguments& args) { - v8::HandleScope scope; - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(FlexReaderWrap::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } - if (args.Length() < 2 || args.Length() > 3) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a File object or string for the first argument, a LocationHandler as second argument, and optional options v8::Object when creating a FlexReader"))); + if (info.Length() < 2 || info.Length() > 3) { + Nan::ThrowTypeError(Nan::New("please provide a File object or string for the first argument, a LocationHandler as second argument, and optional options v8::Object when creating a FlexReader").ToLocalChecked()); + return; } try { osmium::osm_entity_bits::type read_which_entities = osmium::osm_entity_bits::nwra; - if (args.Length() == 3 && !args[2]->IsUndefined()) { - if (!args[2]->IsObject()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("Third argument to FlexReader constructor must be object"))); + if (info.Length() == 3 && !info[2]->IsUndefined()) { + if (!info[2]->IsObject()) { + Nan::ThrowTypeError(Nan::New("Third argument to FlexReader constructor must be object").ToLocalChecked()); + return; } - read_which_entities = object_to_entity_bits(args[2]->ToObject()); + read_which_entities = object_to_entity_bits(info[2]->ToObject()); } - if (args[1]->IsObject() && LocationHandlerWrap::constructor->HasInstance(args[1]->ToObject())) { - v8::Local location_handler_obj = args[1]->ToObject(); + if (info[1]->IsObject() && Nan::New(LocationHandlerWrap::constructor)->HasInstance(info[1]->ToObject())) { + v8::Local location_handler_obj = info[1]->ToObject(); location_handler_type& location_handler = unwrap(location_handler_obj); - if (args[0]->IsString()) { - v8::String::Utf8Value filename { args[0] }; + if (info[0]->IsString()) { + v8::String::Utf8Value filename { info[0] }; osmium::io::File file(*filename); FlexReaderWrap* reader_wrap = new FlexReaderWrap(file, location_handler, read_which_entities); - reader_wrap->Wrap(args.This()); - return args.This(); - } else if (args[0]->IsObject() && FileWrap::constructor->HasInstance(args[0]->ToObject())) { - v8::Local file_obj = args[0]->ToObject(); + reader_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; + } else if (info[0]->IsObject() && Nan::New(FileWrap::constructor)->HasInstance(info[0]->ToObject())) { + v8::Local file_obj = info[0]->ToObject(); FlexReaderWrap* reader_wrap = new FlexReaderWrap(unwrap(file_obj), location_handler, read_which_entities); - reader_wrap->Wrap(args.This()); - return args.This(); + reader_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a File object or string for the first argument when creating a FlexReader"))); + Nan::ThrowTypeError(Nan::New("please provide a File object or string for the first argument when creating a FlexReader").ToLocalChecked()); + return; } } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a LocationHandler object for the second argument when creating a FlexReader"))); + Nan::ThrowTypeError(Nan::New("please provide a LocationHandler object for the second argument when creating a FlexReader").ToLocalChecked()); + return; } } catch (const std::exception& ex) { - return ThrowException(v8::Exception::TypeError(v8::String::New(ex.what()))); + Nan::ThrowTypeError(Nan::New(ex.what()).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle FlexReaderWrap::header(const v8::Arguments& args) { + NAN_METHOD(FlexReaderWrap::header) { INSTANCE_CHECK(FlexReaderWrap, "FlexReader", "header"); - v8::HandleScope scope; - v8::Local obj = v8::Object::New(); - const osmium::io::Header& header = unwrap(args.This()).header(); - obj->Set(symbol_generator, v8::String::New(header.get("generator").c_str())); + v8::Local obj = Nan::New(); + const osmium::io::Header& header = unwrap(info.This()).header(); + obj->Set(Nan::New(symbol_generator), Nan::New(header.get("generator")).ToLocalChecked()); - auto bounds_array = v8::Array::New(header.boxes().size()); + auto bounds_array = Nan::New(static_cast(header.boxes().size())); int i=0; for (const osmium::Box& box : header.boxes()) { bounds_array->Set(i++, create_js_box(box)); } - obj->Set(symbol_bounds, bounds_array); - return scope.Close(obj); + obj->Set(Nan::New(symbol_bounds), bounds_array); + info.GetReturnValue().Set(obj); + return; } - v8::Handle FlexReaderWrap::close(const v8::Arguments& args) { + NAN_METHOD(FlexReaderWrap::close) { INSTANCE_CHECK(FlexReaderWrap, "FlexReader", "close"); - v8::HandleScope scope; try { - unwrap(args.This()).close(); + unwrap(info.This()).close(); } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle FlexReaderWrap::read(const v8::Arguments& args) { + NAN_METHOD(FlexReaderWrap::read) { INSTANCE_CHECK(FlexReaderWrap, "FlexReader", "read"); - v8::HandleScope scope; try { - osmium::memory::Buffer buffer = unwrap(args.This()).read(); + osmium::memory::Buffer buffer = unwrap(info.This()).read(); if (buffer) { - return scope.Close(new_external(std::move(buffer))); + info.GetReturnValue().Set(new_external(std::move(buffer))); + return; } } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle FlexReaderWrap::read_all(const v8::Arguments& args) { + NAN_METHOD(FlexReaderWrap::read_all) { INSTANCE_CHECK(FlexReaderWrap, "FlexReader", "read_all"); osmium::memory::Buffer buffer(1024*1024, osmium::memory::Buffer::auto_grow::yes); - v8::HandleScope scope; try { - flex_reader_type& reader = unwrap(args.This()); + flex_reader_type& reader = unwrap(info.This()); while (osmium::memory::Buffer read_buffer = reader.read()) { buffer.add_buffer(read_buffer); buffer.commit(); } if (buffer) { - return scope.Close(new_external(std::move(buffer))); + info.GetReturnValue().Set(new_external(std::move(buffer))); + return; } } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/flex_reader_wrap.hpp b/src/flex_reader_wrap.hpp index 2aa4b40..064b0fe 100644 --- a/src/flex_reader_wrap.hpp +++ b/src/flex_reader_wrap.hpp @@ -1,9 +1,7 @@ #ifndef FLEX_READER_WRAP_HPP #define FLEX_READER_WRAP_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -19,23 +17,23 @@ namespace node_osmium { typedef osmium::experimental::FlexReader flex_reader_type; - class FlexReaderWrap : public node::ObjectWrap { + class FlexReaderWrap : public Nan::ObjectWrap { - static v8::Handle header(const v8::Arguments& args); - static v8::Handle close(const v8::Arguments& args); - static v8::Handle read(const v8::Arguments& args); - static v8::Handle read_all(const v8::Arguments& args); + static NAN_METHOD(header); + static NAN_METHOD(close); + static NAN_METHOD(read); + static NAN_METHOD(read_all); flex_reader_type m_this; public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); FlexReaderWrap(const osmium::io::File& file, location_handler_type& location_handler, osmium::osm_entity_bits::type entities) : - ObjectWrap(), + Nan::ObjectWrap(), m_this(file, location_handler, entities) { } diff --git a/src/handler.cpp b/src/handler.cpp index 0f1e8b6..54c5b9a 100644 --- a/src/handler.cpp +++ b/src/handler.cpp @@ -2,10 +2,6 @@ // c++ #include -// node -#include -#include - // osmium #include @@ -21,168 +17,156 @@ namespace node_osmium { - v8::Persistent JSHandler::constructor; - v8::Persistent JSHandler::symbol_tagged_nodes_only; + Nan::Persistent JSHandler::constructor; - void JSHandler::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(JSHandler::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Handler); - node::SetPrototypeMethod(constructor, "on", on); - node::SetPrototypeMethod(constructor, "options", options); - node::SetPrototypeMethod(constructor, "end", stream_end); - target->Set(symbol_Handler, constructor->GetFunction()); + void JSHandler::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(JSHandler::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Handler)); + Nan::SetPrototypeMethod(lcons, "on", on); + Nan::SetPrototypeMethod(lcons, "options", options); + Nan::SetPrototypeMethod(lcons, "end", stream_end); + target->Set(Nan::New(symbol_Handler), lcons->GetFunction()); + constructor.Reset(lcons); - symbol_tagged_nodes_only = NODE_PSYMBOL("tagged_nodes_only"); } JSHandler::JSHandler() : - ObjectWrap(), + Nan::ObjectWrap(), node_callback_for_tagged_only(false) { } JSHandler::~JSHandler() { - init_cb.Dispose(); + init_cb.Reset(); - before_nodes_cb.Dispose(); - node_cb.Dispose(); - after_nodes_cb.Dispose(); + before_nodes_cb.Reset(); + node_cb.Reset(); + after_nodes_cb.Reset(); - before_ways_cb.Dispose(); - way_cb.Dispose(); - after_ways_cb.Dispose(); + before_ways_cb.Reset(); + way_cb.Reset(); + after_ways_cb.Reset(); - before_relations_cb.Dispose(); - relation_cb.Dispose(); - after_relations_cb.Dispose(); + before_relations_cb.Reset(); + relation_cb.Reset(); + after_relations_cb.Reset(); - area_cb.Dispose(); + area_cb.Reset(); - before_changesets_cb.Dispose(); - changeset_cb.Dispose(); - after_changesets_cb.Dispose(); + before_changesets_cb.Reset(); + changeset_cb.Reset(); + after_changesets_cb.Reset(); - done_cb.Dispose(); + done_cb.Reset(); } - v8::Handle JSHandler::New(const v8::Arguments& args) { - v8::HandleScope scope; - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(JSHandler::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } - if (args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); + if (info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); void* ptr = ext->Value(); JSHandler* b = static_cast(ptr); - b->Wrap(args.This()); - return args.This(); + b->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { JSHandler* jshandler = new JSHandler(); - jshandler->Wrap(args.This()); - return args.This(); + jshandler->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle JSHandler::options(const v8::Arguments& args) { + NAN_METHOD(JSHandler::options) { INSTANCE_CHECK(JSHandler, "handler", "options"); - v8::HandleScope scope; - if (args.Length() != 1 || !args[0]->IsObject()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a single object as parameter"))); + if (info.Length() != 1 || !info[0]->IsObject()) { + Nan::ThrowTypeError(Nan::New("please provide a single object as parameter").ToLocalChecked()); + return; } - v8::Local tagged_nodes_only = args[0]->ToObject()->Get(symbol_tagged_nodes_only); + v8::Local tagged_nodes_only = info[0]->ToObject()->Get(Nan::New("tagged_nodes_only").ToLocalChecked()); if (tagged_nodes_only->IsBoolean()) { - unwrap(args.This()).node_callback_for_tagged_only = tagged_nodes_only->BooleanValue(); + unwrap(info.This()).node_callback_for_tagged_only = tagged_nodes_only->BooleanValue(); } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle JSHandler::on(const v8::Arguments& args) { + NAN_METHOD(JSHandler::on) { INSTANCE_CHECK(JSHandler, "handler", "on"); - v8::HandleScope scope; - if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsFunction()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide an event name and callback function"))); + if (info.Length() != 2 || !info[0]->IsString() || !info[1]->IsFunction()) { + Nan::ThrowTypeError(Nan::New("please provide an event name and callback function").ToLocalChecked()); + return; } - v8::String::Utf8Value callback_name_string { args[0] }; + v8::String::Utf8Value callback_name_string { info[0] }; std::string callback_name = *callback_name_string; - v8::Local callback = v8::Local::Cast(args[1]); + v8::Local callback = v8::Local::Cast(info[1]); if (callback->IsNull() || callback->IsUndefined()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a valid callback function for second arg"))); + Nan::ThrowTypeError(Nan::New("please provide a valid callback function for second arg").ToLocalChecked()); + return; } - JSHandler& handler = unwrap(args.This()); + JSHandler& handler = unwrap(info.This()); if (callback_name == "node") { - handler.node_cb.Dispose(); - handler.node_cb = v8::Persistent::New(callback); + handler.node_cb.Reset(callback.As()); } else if (callback_name == "way") { - handler.way_cb.Dispose(); - handler.way_cb = v8::Persistent::New(callback); + handler.way_cb.Reset(callback.As()); } else if (callback_name == "relation") { - handler.relation_cb.Dispose(); - handler.relation_cb = v8::Persistent::New(callback); + handler.relation_cb.Reset(callback.As()); } else if (callback_name == "area") { - handler.area_cb.Dispose(); - handler.area_cb = v8::Persistent::New(callback); + handler.area_cb.Reset(callback.As()); } else if (callback_name == "changeset") { - handler.changeset_cb.Dispose(); - handler.changeset_cb = v8::Persistent::New(callback); + handler.changeset_cb.Reset(callback.As()); } else if (callback_name == "init") { - handler.init_cb.Dispose(); - handler.init_cb = v8::Persistent::New(callback); + handler.init_cb.Reset(callback.As()); } else if (callback_name == "before_nodes") { - handler.before_nodes_cb.Dispose(); - handler.before_nodes_cb = v8::Persistent::New(callback); + handler.before_nodes_cb.Reset(callback.As()); } else if (callback_name == "after_nodes") { - handler.after_nodes_cb.Dispose(); - handler.after_nodes_cb = v8::Persistent::New(callback); + handler.after_nodes_cb.Reset(callback.As()); } else if (callback_name == "before_ways") { - handler.before_ways_cb.Dispose(); - handler.before_ways_cb = v8::Persistent::New(callback); + handler.before_ways_cb.Reset(callback.As()); } else if (callback_name == "after_ways") { - handler.after_ways_cb.Dispose(); - handler.after_ways_cb = v8::Persistent::New(callback); + handler.after_ways_cb.Reset(callback.As()); } else if (callback_name == "before_relations") { - handler.before_relations_cb.Dispose(); - handler.before_relations_cb = v8::Persistent::New(callback); + handler.before_relations_cb.Reset(callback.As()); } else if (callback_name == "after_relations") { - handler.after_relations_cb.Dispose(); - handler.after_relations_cb = v8::Persistent::New(callback); + handler.after_relations_cb.Reset(callback.As()); } else if (callback_name == "before_changesets") { - handler.before_changesets_cb.Dispose(); - handler.before_changesets_cb = v8::Persistent::New(callback); + handler.before_changesets_cb.Reset(callback.As()); } else if (callback_name == "after_changesets") { - handler.after_changesets_cb.Dispose(); - handler.after_changesets_cb = v8::Persistent::New(callback); + handler.after_changesets_cb.Reset(callback.As()); } else if (callback_name == "done") { - handler.done_cb.Dispose(); - handler.done_cb = v8::Persistent::New(callback); + handler.done_cb.Reset(callback.As()); } else { - return ThrowException(v8::Exception::RangeError(v8::String::New("unknown callback name as first argument"))); + Nan::ThrowRangeError(Nan::New("unknown callback name as first argument").ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } template - void call_callback_with_entity(const v8::Persistent& function, const osmium::OSMEntity& entity) { + void call_callback_with_entity(const Nan::Persistent& function, const osmium::OSMEntity& entity) { if (function.IsEmpty()) { return; } - - v8::HandleScope scope; v8::Local argv[1] = { new_external(entity) }; - function->Call(v8::Context::GetCurrent()->Global(), 1, argv); + Nan::New(function)->Call(Nan::GetCurrentContext()->Global(), 1, argv); } - void call_callback(const v8::Persistent& function) { + void call_callback(const Nan::Persistent& function) { if (function.IsEmpty()) { return; } - - function->Call(v8::Context::GetCurrent()->Global(), 0, nullptr); + Nan::New(function)->Call(Nan::GetCurrentContext()->Global(), 0, nullptr); } void JSHandler::dispatch_entity(const osmium::OSMEntity& entity) const { @@ -257,17 +241,18 @@ namespace node_osmium { call_callback(done_cb); } - v8::Handle JSHandler::stream_end(const v8::Arguments& args) { + NAN_METHOD(JSHandler::stream_end) { INSTANCE_CHECK(JSHandler, "handler", "end"); - v8::HandleScope scope; - if (args.Length() != 0) { - return ThrowException(v8::Exception::TypeError(v8::String::New("end() doesn't take any parameters"))); + if (info.Length() != 0) { + Nan::ThrowTypeError(Nan::New("end() doesn't take any parameters").ToLocalChecked()); + return; } - JSHandler& handler = unwrap(args.This()); + JSHandler& handler = unwrap(info.This()); call_callback(handler.done_cb); - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/handler.hpp b/src/handler.hpp index 8229f64..a0a8162 100644 --- a/src/handler.hpp +++ b/src/handler.hpp @@ -1,9 +1,7 @@ #ifndef HANDLER_HPP #define HANDLER_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium namespace osmium { @@ -12,43 +10,43 @@ namespace osmium { namespace node_osmium { - class JSHandler : public node::ObjectWrap { + class JSHandler : public Nan::ObjectWrap { - static v8::Persistent symbol_tagged_nodes_only; + static Nan::Persistent symbol_tagged_nodes_only; bool node_callback_for_tagged_only; - v8::Persistent init_cb; + Nan::Persistent init_cb; - v8::Persistent before_nodes_cb; - v8::Persistent node_cb; - v8::Persistent after_nodes_cb; + Nan::Persistent before_nodes_cb; + Nan::Persistent node_cb; + Nan::Persistent after_nodes_cb; - v8::Persistent before_ways_cb; - v8::Persistent way_cb; - v8::Persistent after_ways_cb; + Nan::Persistent before_ways_cb; + Nan::Persistent way_cb; + Nan::Persistent after_ways_cb; - v8::Persistent before_relations_cb; - v8::Persistent relation_cb; - v8::Persistent after_relations_cb; + Nan::Persistent before_relations_cb; + Nan::Persistent relation_cb; + Nan::Persistent after_relations_cb; - v8::Persistent area_cb; + Nan::Persistent area_cb; - v8::Persistent before_changesets_cb; - v8::Persistent changeset_cb; - v8::Persistent after_changesets_cb; + Nan::Persistent before_changesets_cb; + Nan::Persistent changeset_cb; + Nan::Persistent after_changesets_cb; - v8::Persistent done_cb; + Nan::Persistent done_cb; - static v8::Handle on(const v8::Arguments& args); - static v8::Handle options(const v8::Arguments& args); - static v8::Handle stream_end(const v8::Arguments& args); + static NAN_METHOD(on); + static NAN_METHOD(options); + static NAN_METHOD(stream_end); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); JSHandler(); diff --git a/src/include_nan.hpp b/src/include_nan.hpp new file mode 100644 index 0000000..5ce3b7c --- /dev/null +++ b/src/include_nan.hpp @@ -0,0 +1,11 @@ +#ifndef INCLUDE_NAN_HPP +#define INCLUDE_NAN_HPP + +// nan +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop + +#endif // INCLUDE_NAN_HPP diff --git a/src/include_v8.hpp b/src/include_v8.hpp deleted file mode 100644 index 5b754a9..0000000 --- a/src/include_v8.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef INCLUDE_V8_HPP -#define INCLUDE_V8_HPP - -#include - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wctor-dtor-privacy" -#pragma GCC diagnostic ignored "-Wunused-parameter" -#include -#pragma GCC diagnostic pop - -#endif // INCLUDE_V8_HPP diff --git a/src/location_handler_wrap.cpp b/src/location_handler_wrap.cpp index df41744..0cab8d0 100644 --- a/src/location_handler_wrap.cpp +++ b/src/location_handler_wrap.cpp @@ -6,62 +6,68 @@ namespace node_osmium { - v8::Persistent LocationHandlerWrap::constructor; + Nan::Persistent LocationHandlerWrap::constructor; - void LocationHandlerWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(LocationHandlerWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_LocationHandler); - node::SetPrototypeMethod(constructor, "clear", clear); - node::SetPrototypeMethod(constructor, "ignoreErrors", ignoreErrors); - node::SetPrototypeMethod(constructor, "end", stream_end); - target->Set(symbol_LocationHandler, constructor->GetFunction()); + void LocationHandlerWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(LocationHandlerWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_LocationHandler)); + Nan::SetPrototypeMethod(lcons, "clear", clear); + Nan::SetPrototypeMethod(lcons, "ignoreErrors", ignoreErrors); + Nan::SetPrototypeMethod(lcons, "end", stream_end); + target->Set(Nan::New(symbol_LocationHandler), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle LocationHandlerWrap::New(const v8::Arguments& args) { - v8::HandleScope scope; - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(LocationHandlerWrap::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } try { LocationHandlerWrap* location_handler_wrap; - if (args.Length() == 0) { + if (info.Length() == 0) { location_handler_wrap = new LocationHandlerWrap("sparse_mem_array"); } else { - if (args.Length() != 1) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a node cache type as string when creating a LocationHandler"))); + if (info.Length() != 1) { + Nan::ThrowTypeError(Nan::New("please provide a node cache type as string when creating a LocationHandler").ToLocalChecked()); + return; } - if (!args[0]->IsString()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("please provide a node cache type as string when creating a LocationHandler"))); + if (!info[0]->IsString()) { + Nan::ThrowTypeError(Nan::New("please provide a node cache type as string when creating a LocationHandler").ToLocalChecked()); + return; } - v8::String::Utf8Value index_map_type { args[0] }; + v8::String::Utf8Value index_map_type { info[0] }; location_handler_wrap = new LocationHandlerWrap(*index_map_type); } - location_handler_wrap->Wrap(args.This()); - return args.This(); + location_handler_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } catch (const std::exception& ex) { - return ThrowException(v8::Exception::TypeError(v8::String::New(ex.what()))); + Nan::ThrowTypeError(Nan::New(ex.what()).ToLocalChecked()); + return; } } - v8::Handle LocationHandlerWrap::ignoreErrors(const v8::Arguments& args) { + NAN_METHOD(LocationHandlerWrap::ignoreErrors) { INSTANCE_CHECK(LocationHandlerWrap, "location_handler", "ignoreErrors"); - v8::HandleScope scope; - unwrap(args.This()).ignore_errors(); - return scope.Close(v8::Undefined()); + unwrap(info.This()).ignore_errors(); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle LocationHandlerWrap::clear(const v8::Arguments& args) { + NAN_METHOD(LocationHandlerWrap::clear) { INSTANCE_CHECK(LocationHandlerWrap, "clear", "clear"); - v8::HandleScope scope; - unwrap(args.This()).clear(); - return scope.Close(v8::Undefined()); + unwrap(info.This()).clear(); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle LocationHandlerWrap::stream_end(const v8::Arguments& args) { - return v8::Undefined(); + NAN_METHOD(LocationHandlerWrap::stream_end) { + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/location_handler_wrap.hpp b/src/location_handler_wrap.hpp index 65f67d3..1b508b0 100644 --- a/src/location_handler_wrap.hpp +++ b/src/location_handler_wrap.hpp @@ -5,9 +5,7 @@ #include #include -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -18,24 +16,24 @@ namespace node_osmium { typedef osmium::index::map::Map index_type; typedef osmium::handler::NodeLocationsForWays location_handler_type; - class LocationHandlerWrap : public node::ObjectWrap { + class LocationHandlerWrap : public Nan::ObjectWrap { std::unique_ptr m_index; std::unique_ptr m_this; - static v8::Handle clear(const v8::Arguments& args); - static v8::Handle ignoreErrors(const v8::Arguments& args); - static v8::Handle stream_end(const v8::Arguments& args); + static NAN_METHOD(clear); + static NAN_METHOD(ignoreErrors); + static NAN_METHOD(stream_end); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); LocationHandlerWrap(const std::string& cache_type) : - ObjectWrap(), + Nan::ObjectWrap(), m_index(osmium::index::MapFactory::instance().create_map(cache_type)), m_this(new location_handler_type(*m_index)) { } diff --git a/src/multipolygon_collector_wrap.cpp b/src/multipolygon_collector_wrap.cpp index 4e725e3..f2860c1 100644 --- a/src/multipolygon_collector_wrap.cpp +++ b/src/multipolygon_collector_wrap.cpp @@ -1,7 +1,4 @@ -// node -#include - // node-osmium #include "node_osmium.hpp" #include "multipolygon_collector_wrap.hpp" @@ -14,85 +11,95 @@ namespace node_osmium { - v8::Persistent MultipolygonCollectorWrap::constructor; + Nan::Persistent MultipolygonCollectorWrap::constructor; - void MultipolygonCollectorWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(MultipolygonCollectorWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_MultipolygonCollector); - node::SetPrototypeMethod(constructor, "read_relations", read_relations); - node::SetPrototypeMethod(constructor, "handler", handler); - target->Set(symbol_MultipolygonCollector, constructor->GetFunction()); + void MultipolygonCollectorWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(MultipolygonCollectorWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_MultipolygonCollector)); + Nan::SetPrototypeMethod(lcons, "read_relations", read_relations); + Nan::SetPrototypeMethod(lcons, "handler", handler); + target->Set(Nan::New(symbol_MultipolygonCollector), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle MultipolygonCollectorWrap::New(const v8::Arguments& args) { - v8::HandleScope scope; - - if (!args.IsConstructCall()) { - return ThrowException(v8::Exception::Error(v8::String::New("Cannot call constructor as function, you need to use 'new' keyword"))); + NAN_METHOD(MultipolygonCollectorWrap::New) { + if (!info.IsConstructCall()) { + Nan::ThrowError(Nan::New("Cannot call constructor as function, you need to use 'new' keyword").ToLocalChecked()); + return; } - if (args.Length() != 0) { - return ThrowException(v8::Exception::TypeError(v8::String::New("MultipolygonCollector is constructed without arguments"))); + if (info.Length() != 0) { + Nan::ThrowTypeError(Nan::New("MultipolygonCollector is constructed without arguments").ToLocalChecked()); + return; } try { MultipolygonCollectorWrap* multipolygon_collector_wrap = new MultipolygonCollectorWrap(); - multipolygon_collector_wrap->Wrap(args.This()); - return args.This(); + multipolygon_collector_wrap->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } catch (const std::exception& e) { - return ThrowException(v8::Exception::TypeError(v8::String::New(e.what()))); + Nan::ThrowTypeError(Nan::New(e.what()).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle MultipolygonCollectorWrap::read_relations(const v8::Arguments& args) { + NAN_METHOD(MultipolygonCollectorWrap::read_relations) { INSTANCE_CHECK(MultipolygonCollectorWrap, "MultipolygonCollector", "read_relations"); - v8::HandleScope scope; - if (args.Length() != 1 || !args[0]->IsObject()) { - return ThrowException(v8::Exception::Error(v8::String::New("call MultipolygonCollector.read_relation() with BasicReader or Buffer object"))); + if (info.Length() != 1 || !info[0]->IsObject()) { + Nan::ThrowError(Nan::New("call MultipolygonCollector.read_relation() with BasicReader or Buffer object").ToLocalChecked()); + return; } try { - if (BasicReaderWrap::constructor->HasInstance(args[0]->ToObject())) { - osmium::io::Reader& reader = unwrap(args[0]->ToObject()); - unwrap(args.This()).read_relations(reader); - } else if (BufferWrap::constructor->HasInstance(args[0]->ToObject())) { - osmium::memory::Buffer& buffer = unwrap(args[0]->ToObject()); - unwrap(args.This()).read_relations(buffer.begin(), buffer.end()); + if (Nan::New(BasicReaderWrap::constructor)->HasInstance(info[0]->ToObject())) { + osmium::io::Reader& reader = unwrap(info[0]->ToObject()); + unwrap(info.This()).read_relations(reader); + } else if (Nan::New(BufferWrap::constructor)->HasInstance(info[0]->ToObject())) { + osmium::memory::Buffer& buffer = unwrap(info[0]->ToObject()); + unwrap(info.This()).read_relations(buffer.begin(), buffer.end()); } else { - return ThrowException(v8::Exception::Error(v8::String::New("call MultipolygonCollector.read_relation() with BasicReader or Buffer object"))); + Nan::ThrowError(Nan::New("call MultipolygonCollector.read_relation() with BasicReader or Buffer object").ToLocalChecked()); + return; } } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } - v8::Handle MultipolygonCollectorWrap::handler(const v8::Arguments& args) { + NAN_METHOD(MultipolygonCollectorWrap::handler) { INSTANCE_CHECK(MultipolygonCollectorWrap, "MultipolygonCollector", "handler"); - v8::HandleScope scope; - if (args.Length() != 1 || !args[0]->IsObject() || !JSHandler::constructor->HasInstance(args[0]->ToObject())) { - return ThrowException(v8::Exception::Error(v8::String::New("call MultipolygonCollector.handler() with Handler object"))); + if (info.Length() != 1 || !info[0]->IsObject() || !Nan::New(JSHandler::constructor)->HasInstance(info[0]->ToObject())) { + Nan::ThrowError(Nan::New("call MultipolygonCollector.handler() with Handler object").ToLocalChecked()); + return; } try { - JSHandler& handler = unwrap(args[0]->ToObject()); - auto& mc_handler = unwrap(args.This()).handler([&handler](const osmium::memory::Buffer& area_buffer) { + JSHandler& handler = unwrap(info[0]->ToObject()); + auto& mc_handler = unwrap(info.This()).handler([&handler](const osmium::memory::Buffer& area_buffer) { for (const osmium::OSMEntity& entity : area_buffer) { handler.dispatch_entity(entity); } }); - return scope.Close(new_external(mc_handler)); + info.GetReturnValue().Set(new_external(mc_handler)); + return; } catch (const std::exception& e) { std::string msg("osmium error: "); msg += e.what(); - return ThrowException(v8::Exception::Error(v8::String::New(msg.c_str()))); + Nan::ThrowError(Nan::New(msg).ToLocalChecked()); + return; } - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/multipolygon_collector_wrap.hpp b/src/multipolygon_collector_wrap.hpp index b59c5fb..154b448 100644 --- a/src/multipolygon_collector_wrap.hpp +++ b/src/multipolygon_collector_wrap.hpp @@ -1,9 +1,7 @@ #ifndef MULTIPOLYGON_COLLECTOR_WRAP_HPP #define MULTIPOLYGON_COLLECTOR_WRAP_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -11,22 +9,22 @@ namespace node_osmium { - class MultipolygonCollectorWrap : public node::ObjectWrap { + class MultipolygonCollectorWrap : public Nan::ObjectWrap { - static v8::Handle read_relations(const v8::Arguments& args); - static v8::Handle handler(const v8::Arguments& args); + static NAN_METHOD(read_relations); + static NAN_METHOD(handler); osmium::area::Assembler::config_type m_assembler_config; osmium::area::MultipolygonCollector m_collector; public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); MultipolygonCollectorWrap() : - node::ObjectWrap(), + Nan::ObjectWrap(), m_assembler_config(), m_collector(m_assembler_config) { } diff --git a/src/multipolygon_handler_wrap.cpp b/src/multipolygon_handler_wrap.cpp index 756f5fd..9e6ff50 100644 --- a/src/multipolygon_handler_wrap.cpp +++ b/src/multipolygon_handler_wrap.cpp @@ -1,7 +1,4 @@ -// node -#include - // node-osmium #include "node_osmium.hpp" #include "multipolygon_handler_wrap.hpp" @@ -9,38 +6,42 @@ namespace node_osmium { - v8::Persistent MultipolygonHandlerWrap::constructor; + Nan::Persistent MultipolygonHandlerWrap::constructor; - void MultipolygonHandlerWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(MultipolygonHandlerWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_MultipolygonHandler); - node::SetPrototypeMethod(constructor, "end", stream_end); - target->Set(symbol_MultipolygonHandler, constructor->GetFunction()); + void MultipolygonHandlerWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(MultipolygonHandlerWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_MultipolygonHandler)); + Nan::SetPrototypeMethod(lcons, "end", stream_end); + target->Set(Nan::New(symbol_MultipolygonHandler), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle MultipolygonHandlerWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(MultipolygonHandlerWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.MultipolygonHandler cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.MultipolygonHandler cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle MultipolygonHandlerWrap::stream_end(const v8::Arguments& args) { + NAN_METHOD(MultipolygonHandlerWrap::stream_end) { INSTANCE_CHECK(MultipolygonHandlerWrap, "MultipolygonHandler", "end"); - v8::HandleScope scope; - if (args.Length() != 0) { - return ThrowException(v8::Exception::TypeError(v8::String::New("end() doesn't take any parameters"))); + if (info.Length() != 0) { + Nan::ThrowTypeError(Nan::New("end() doesn't take any parameters").ToLocalChecked()); + return; } - auto& handler = unwrap(args.This()); + auto& handler = unwrap(info.This()); handler.flush(); - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } // namespace node_osmium diff --git a/src/multipolygon_handler_wrap.hpp b/src/multipolygon_handler_wrap.hpp index a1ac967..9430e68 100644 --- a/src/multipolygon_handler_wrap.hpp +++ b/src/multipolygon_handler_wrap.hpp @@ -1,9 +1,7 @@ #ifndef MULTIPOLYGON_HANDLER_WRAP_HPP #define MULTIPOLYGON_HANDLER_WRAP_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -11,21 +9,21 @@ namespace node_osmium { - class MultipolygonHandlerWrap : public node::ObjectWrap { + class MultipolygonHandlerWrap : public Nan::ObjectWrap { typedef osmium::area::MultipolygonCollector::HandlerPass2 handler_type; handler_type m_handler; - static v8::Handle stream_end(const v8::Arguments& args); + static NAN_METHOD(stream_end); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); MultipolygonHandlerWrap(handler_type& handler) : - node::ObjectWrap(), + Nan::ObjectWrap(), m_handler(handler) { } diff --git a/src/node_osmium.cpp b/src/node_osmium.cpp index 217646a..71af897 100644 --- a/src/node_osmium.cpp +++ b/src/node_osmium.cpp @@ -1,7 +1,4 @@ -// node -#include - // osmium #include #include @@ -28,91 +25,90 @@ namespace node_osmium { - v8::Persistent module; + Nan::Persistent module; osmium::geom::WKBFactory<> wkb_factory; osmium::geom::WKTFactory<> wkt_factory; - v8::Persistent symbol_OSMEntity; - v8::Persistent symbol_OSMObject; + Nan::Persistent symbol_OSMEntity; + Nan::Persistent symbol_OSMObject; - v8::Persistent symbol_Node; - v8::Persistent symbol_node; + Nan::Persistent symbol_Node; + Nan::Persistent symbol_node; - v8::Persistent symbol_Way; - v8::Persistent symbol_way; + Nan::Persistent symbol_Way; + Nan::Persistent symbol_way; - v8::Persistent symbol_Relation; - v8::Persistent symbol_relation; - v8::Persistent symbol_type; - v8::Persistent symbol_ref; - v8::Persistent symbol_role; + Nan::Persistent symbol_Relation; + Nan::Persistent symbol_relation; + Nan::Persistent symbol_type; + Nan::Persistent symbol_ref; + Nan::Persistent symbol_role; - v8::Persistent symbol_Area; - v8::Persistent symbol_area; + Nan::Persistent symbol_Area; + Nan::Persistent symbol_area; - v8::Persistent symbol_Changeset; - v8::Persistent symbol_changeset; + Nan::Persistent symbol_Changeset; + Nan::Persistent symbol_changeset; - v8::Persistent symbol_Coordinates; - v8::Persistent symbol_Box; + Nan::Persistent symbol_Coordinates; + Nan::Persistent symbol_Box; - v8::Persistent symbol_generator; - v8::Persistent symbol_bounds; + Nan::Persistent symbol_generator; + Nan::Persistent symbol_bounds; - v8::Persistent symbol_Buffer; - v8::Persistent symbol_File; - v8::Persistent symbol_Handler; - v8::Persistent symbol_LocationHandler; - v8::Persistent symbol_MultipolygonCollector; - v8::Persistent symbol_MultipolygonHandler; - v8::Persistent symbol_BasicReader; - v8::Persistent symbol_FlexReader; + Nan::Persistent symbol_Buffer; + Nan::Persistent symbol_File; + Nan::Persistent symbol_Handler; + Nan::Persistent symbol_LocationHandler; + Nan::Persistent symbol_MultipolygonCollector; + Nan::Persistent symbol_MultipolygonHandler; + Nan::Persistent symbol_BasicReader; + Nan::Persistent symbol_FlexReader; - v8::Persistent the_Node; - v8::Persistent the_Way; - v8::Persistent the_Relation; - v8::Persistent the_Area; - v8::Persistent the_Changeset; + Nan::Persistent the_Node; + Nan::Persistent the_Way; + Nan::Persistent the_Relation; + Nan::Persistent the_Area; + Nan::Persistent the_Changeset; extern "C" { - static void start(v8::Handle target) { - v8::HandleScope scope; - module = v8::Persistent::New(target); - - node::SetMethod(target, "apply_", node_osmium::apply); - node::SetMethod(target, "register_filter", node_osmium::Filter::register_filter); - - symbol_OSMEntity = NODE_PSYMBOL("OSMEntity"); - symbol_OSMObject = NODE_PSYMBOL("OSMObject"); - symbol_Node = NODE_PSYMBOL("Node"); - symbol_node = NODE_PSYMBOL("node"); - symbol_Way = NODE_PSYMBOL("Way"); - symbol_way = NODE_PSYMBOL("way"); - symbol_Relation = NODE_PSYMBOL("Relation"); - symbol_relation = NODE_PSYMBOL("relation"); - symbol_type = NODE_PSYMBOL("type"); - symbol_ref = NODE_PSYMBOL("ref"); - symbol_role = NODE_PSYMBOL("role"); - symbol_Area = NODE_PSYMBOL("Area"); - symbol_area = NODE_PSYMBOL("area"); - symbol_Changeset = NODE_PSYMBOL("Changeset"); - symbol_changeset = NODE_PSYMBOL("changeset"); - symbol_Coordinates = NODE_PSYMBOL("Coordinates"); - symbol_Box = NODE_PSYMBOL("Box"); - symbol_generator = NODE_PSYMBOL("generator"); - symbol_bounds = NODE_PSYMBOL("bounds"); - - symbol_Buffer = NODE_PSYMBOL("Buffer"); - symbol_File = NODE_PSYMBOL("File"); - symbol_Handler = NODE_PSYMBOL("Handler"); - symbol_LocationHandler = NODE_PSYMBOL("LocationHandler"); - symbol_MultipolygonCollector = NODE_PSYMBOL("MultipolygonCollector"); - symbol_MultipolygonHandler = NODE_PSYMBOL("MultipolygonHandler"); - symbol_BasicReader = NODE_PSYMBOL("BasicReader"); - symbol_FlexReader = NODE_PSYMBOL("FlexReader"); + static void start(v8::Local target) { + Nan::HandleScope scope; + module.Reset(target.As()); + + Nan::SetMethod(target, "apply_", node_osmium::apply); + Nan::SetMethod(target, "register_filter", node_osmium::Filter::register_filter); + + symbol_OSMEntity.Reset(Nan::New("OSMEntity").ToLocalChecked()); + symbol_OSMObject.Reset(Nan::New("OSMObject").ToLocalChecked()); + symbol_Node.Reset(Nan::New("Node").ToLocalChecked()); + symbol_node.Reset(Nan::New("node").ToLocalChecked()); + symbol_Way.Reset(Nan::New("Way").ToLocalChecked()); + symbol_way.Reset(Nan::New("way").ToLocalChecked()); + symbol_Relation.Reset(Nan::New("Relation").ToLocalChecked()); + symbol_relation.Reset(Nan::New("relation").ToLocalChecked()); + symbol_type.Reset(Nan::New("type").ToLocalChecked()); + symbol_ref.Reset(Nan::New("ref").ToLocalChecked()); + symbol_role.Reset(Nan::New("role").ToLocalChecked()); + symbol_Area.Reset(Nan::New("Area").ToLocalChecked()); + symbol_area.Reset(Nan::New("area").ToLocalChecked()); + symbol_Changeset.Reset(Nan::New("Changeset").ToLocalChecked()); + symbol_changeset.Reset(Nan::New("changeset").ToLocalChecked()); + symbol_Coordinates.Reset(Nan::New("Coordinates").ToLocalChecked()); + symbol_Box.Reset(Nan::New("Box").ToLocalChecked()); + symbol_generator.Reset(Nan::New("generator").ToLocalChecked()); + symbol_bounds.Reset(Nan::New("bounds").ToLocalChecked()); + symbol_Buffer.Reset(Nan::New("Buffer").ToLocalChecked()); + symbol_File.Reset(Nan::New("File").ToLocalChecked()); + symbol_Handler.Reset(Nan::New("Handler").ToLocalChecked()); + symbol_LocationHandler.Reset(Nan::New("LocationHandler").ToLocalChecked()); + symbol_MultipolygonCollector.Reset(Nan::New("MultipolygonCollector").ToLocalChecked()); + symbol_MultipolygonHandler.Reset(Nan::New("MultipolygonHandler").ToLocalChecked()); + symbol_BasicReader.Reset(Nan::New("BasicReader").ToLocalChecked()); + symbol_FlexReader.Reset(Nan::New("FlexReader").ToLocalChecked()); node_osmium::OSMEntityWrap::Initialize(target); - node_osmium::OSMObjectWrap::Initialize(target); + node_osmium::OSMWrappedObject::Initialize(target); node_osmium::OSMNodeWrap::Initialize(target); node_osmium::OSMWayWrap::Initialize(target); node_osmium::OSMRelationWrap::Initialize(target); @@ -129,11 +125,11 @@ namespace node_osmium { Filter::init_filters(); - the_Node = v8::Persistent::New(new_external()); - the_Way = v8::Persistent::New(new_external()); - the_Relation = v8::Persistent::New(new_external()); - the_Area = v8::Persistent::New(new_external()); - the_Changeset = v8::Persistent::New(new_external()); + the_Node.Reset(new_external()); + the_Way.Reset(new_external()); + the_Relation.Reset(new_external()); + the_Area.Reset(new_external()); + the_Changeset.Reset(new_external()); } } diff --git a/src/node_osmium.hpp b/src/node_osmium.hpp index 95cd247..a4c3380 100644 --- a/src/node_osmium.hpp +++ b/src/node_osmium.hpp @@ -1,51 +1,51 @@ #ifndef NODE_OSMIUM #define NODE_OSMIUM -#include "include_v8.hpp" +#include "include_nan.hpp" namespace node_osmium { - extern v8::Persistent symbol_OSMEntity; - extern v8::Persistent symbol_OSMObject; + extern Nan::Persistent symbol_OSMEntity; + extern Nan::Persistent symbol_OSMObject; - extern v8::Persistent symbol_Node; - extern v8::Persistent symbol_node; + extern Nan::Persistent symbol_Node; + extern Nan::Persistent symbol_node; - extern v8::Persistent symbol_Way; - extern v8::Persistent symbol_way; + extern Nan::Persistent symbol_Way; + extern Nan::Persistent symbol_way; - extern v8::Persistent symbol_Relation; - extern v8::Persistent symbol_relation; - extern v8::Persistent symbol_type; - extern v8::Persistent symbol_ref; - extern v8::Persistent symbol_role; + extern Nan::Persistent symbol_Relation; + extern Nan::Persistent symbol_relation; + extern Nan::Persistent symbol_type; + extern Nan::Persistent symbol_ref; + extern Nan::Persistent symbol_role; - extern v8::Persistent symbol_Area; - extern v8::Persistent symbol_area; + extern Nan::Persistent symbol_Area; + extern Nan::Persistent symbol_area; - extern v8::Persistent symbol_Changeset; - extern v8::Persistent symbol_changeset; + extern Nan::Persistent symbol_Changeset; + extern Nan::Persistent symbol_changeset; - extern v8::Persistent symbol_Coordinates; - extern v8::Persistent symbol_Box; + extern Nan::Persistent symbol_Coordinates; + extern Nan::Persistent symbol_Box; - extern v8::Persistent symbol_generator; - extern v8::Persistent symbol_bounds; + extern Nan::Persistent symbol_generator; + extern Nan::Persistent symbol_bounds; - extern v8::Persistent symbol_Buffer; - extern v8::Persistent symbol_File; - extern v8::Persistent symbol_Handler; - extern v8::Persistent symbol_LocationHandler; - extern v8::Persistent symbol_MultipolygonCollector; - extern v8::Persistent symbol_MultipolygonHandler; - extern v8::Persistent symbol_BasicReader; - extern v8::Persistent symbol_FlexReader; + extern Nan::Persistent symbol_Buffer; + extern Nan::Persistent symbol_File; + extern Nan::Persistent symbol_Handler; + extern Nan::Persistent symbol_LocationHandler; + extern Nan::Persistent symbol_MultipolygonCollector; + extern Nan::Persistent symbol_MultipolygonHandler; + extern Nan::Persistent symbol_BasicReader; + extern Nan::Persistent symbol_FlexReader; - extern v8::Persistent the_Node; - extern v8::Persistent the_Way; - extern v8::Persistent the_Relation; - extern v8::Persistent the_Area; - extern v8::Persistent the_Changeset; + extern Nan::Persistent the_Node; + extern Nan::Persistent the_Way; + extern Nan::Persistent the_Relation; + extern Nan::Persistent the_Area; + extern Nan::Persistent the_Changeset; } // namespace node_osmium diff --git a/src/osm_area_wrap.cpp b/src/osm_area_wrap.cpp index 3a6d60a..080f86e 100644 --- a/src/osm_area_wrap.cpp +++ b/src/osm_area_wrap.cpp @@ -1,8 +1,4 @@ -// node -#include -#include - // osmium #include #include @@ -14,108 +10,108 @@ namespace node_osmium { extern osmium::geom::WKBFactory<> wkb_factory; extern osmium::geom::WKTFactory<> wkt_factory; - extern v8::Persistent module; - - v8::Persistent OSMAreaWrap::constructor; - - void OSMAreaWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMAreaWrap::New)); - constructor->Inherit(OSMObjectWrap::constructor); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Area); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "type", get_type, attributes); - set_accessor(constructor, "orig_id", get_orig_id, attributes); - set_accessor(constructor, "from_way", from_way, attributes); - node::SetPrototypeMethod(constructor, "wkb", wkb); - node::SetPrototypeMethod(constructor, "wkt", wkt); - node::SetPrototypeMethod(constructor, "coordinates", coordinates); - target->Set(symbol_Area, constructor->GetFunction()); + extern Nan::Persistent module; + + Nan::Persistent OSMAreaWrap::constructor; + + void OSMAreaWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMAreaWrap::New); + lcons->Inherit(Nan::New(OSMWrappedObject::constructor)); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Area)); + ATTR(lcons, "type", get_type); + ATTR(lcons, "orig_id", get_orig_id); + ATTR(lcons, "from_way", get_from_way); + Nan::SetPrototypeMethod(lcons, "wkb", wkb); + Nan::SetPrototypeMethod(lcons, "wkt", wkt); + Nan::SetPrototypeMethod(lcons, "coordinates", coordinates); + target->Set(Nan::New(symbol_Area), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMAreaWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMAreaWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Area cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.Area cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMAreaWrap::get_orig_id(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Number::New(wrapped(info.This()).orig_id())); + NAN_GETTER(OSMAreaWrap::get_orig_id) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).orig_id())); + return; } - v8::Handle OSMAreaWrap::from_way(v8::Local /* property */, const v8::AccessorInfo& info) { - return v8::Boolean::New(wrapped(info.This()).from_way()); + NAN_GETTER(OSMAreaWrap::get_from_way) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).from_way())); + return; } - v8::Handle OSMAreaWrap::wkb(const v8::Arguments& args) { + NAN_METHOD(OSMAreaWrap::wkb) { INSTANCE_CHECK(OSMAreaWrap, "Area", "wkb"); - v8::HandleScope scope; try { - std::string wkb { wkb_factory.create_multipolygon(wrapped(args.This())) }; -#if NODE_VERSION_AT_LEAST(0, 10, 0) - return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); -#else - return scope.Close(node::Buffer::New(const_cast(wkb.data()), wkb.size())->handle_); -#endif + std::string wkb { wkb_factory.create_multipolygon(wrapped(info.This())) }; + info.GetReturnValue().Set(Nan::CopyBuffer(wkb.data(), wkb.size()).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } - v8::Handle OSMAreaWrap::wkt(const v8::Arguments& args) { + NAN_METHOD(OSMAreaWrap::wkt) { INSTANCE_CHECK(OSMAreaWrap, "Area", "wkt"); - v8::HandleScope scope; try { - std::string wkt { wkt_factory.create_multipolygon(wrapped(args.This())) }; - return scope.Close(v8::String::New(wkt.c_str())); + std::string wkt { wkt_factory.create_multipolygon(wrapped(info.This())) }; + info.GetReturnValue().Set(Nan::New(wkt).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } - v8::Handle get_coordinates(const osmium::NodeRefList& node_ref_list) { - v8::Local locations = v8::Array::New(node_ref_list.size()); + v8::Local get_coordinates(const osmium::NodeRefList& node_ref_list) { + v8::Local locations = Nan::New(node_ref_list.size()); int i = 0; for (const auto& node_ref : node_ref_list) { const osmium::Location location = node_ref.location(); - v8::Local coordinates = v8::Array::New(2); - coordinates->Set(0, v8::Number::New(location.lon())); - coordinates->Set(1, v8::Number::New(location.lat())); + v8::Local coordinates = Nan::New(2); + coordinates->Set(0, Nan::New(location.lon())); + coordinates->Set(1, Nan::New(location.lat())); locations->Set(i, coordinates); ++i; } return locations; } - v8::Handle OSMAreaWrap::coordinates(const v8::Arguments& args) { + NAN_METHOD(OSMAreaWrap::coordinates) { INSTANCE_CHECK(OSMAreaWrap, "Area", "coordinates"); - v8::HandleScope scope; - v8::Local cf = module->Get(symbol_Coordinates); + v8::Local cf = Nan::New(module)->Get(Nan::New(symbol_Coordinates)); assert(cf->IsFunction()); - const osmium::Area& area = wrapped(args.This()); + const osmium::Area& area = wrapped(info.This()); auto num_rings = area.num_rings(); if (num_rings.first == 0) { - return ThrowException(v8::Exception::Error(v8::String::New("Area has no geometry"))); + Nan::ThrowError(Nan::New("Area has no geometry").ToLocalChecked()); + return; } - v8::Local rings = v8::Array::New(num_rings.first); + v8::Local rings = Nan::New(num_rings.first); int n = 0; for (auto oit = area.cbegin(); oit != area.cend(); ++oit, ++n) { - v8::Local ring = v8::Array::New( - 1 + std::distance(area.inner_ring_cbegin(oit), area.inner_ring_cend(oit)) - ); + unsigned array_size = 1u + std::distance(area.inner_ring_cbegin(oit), area.inner_ring_cend(oit)); + v8::Local ring = Nan::New(array_size); int m = 0; ring->Set(m++, get_coordinates(*oit)); for (auto iit = area.inner_ring_cbegin(oit); iit != area.inner_ring_cend(oit); ++iit) { @@ -124,7 +120,8 @@ namespace node_osmium { rings->Set(n, ring); } - return scope.Close(rings); + info.GetReturnValue().Set(rings); + return; } } // namespace node_osmium diff --git a/src/osm_area_wrap.hpp b/src/osm_area_wrap.hpp index 0b23009..11c0b83 100644 --- a/src/osm_area_wrap.hpp +++ b/src/osm_area_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_AREA_WRAP_HPP #define OSM_AREA_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -18,35 +17,35 @@ namespace osmium { namespace node_osmium { - class OSMAreaWrap : public OSMObjectWrap { + class OSMAreaWrap : public OSMWrappedObject { - static v8::Handle get_type(v8::Local property, const v8::AccessorInfo& info) { - return symbol_area; + static NAN_GETTER(get_type) { + info.GetReturnValue().Set(Nan::New(symbol_area)); } - static v8::Handle get_orig_id(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle from_way(v8::Local property, const v8::AccessorInfo& info); + static NAN_GETTER(get_orig_id); + static NAN_GETTER(get_from_way); - static v8::Handle wkb(const v8::Arguments& args); - static v8::Handle wkt(const v8::Arguments& args); - static v8::Handle coordinates(const v8::Arguments& args); + static NAN_METHOD(wkb); + static NAN_METHOD(wkt); + static NAN_METHOD(coordinates); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::Area& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); } OSMAreaWrap() : - OSMObjectWrap() { + OSMWrappedObject() { } OSMAreaWrap(const osmium::OSMEntity& entity) : - OSMObjectWrap(entity) { + OSMWrappedObject(entity) { } private: diff --git a/src/osm_changeset_wrap.cpp b/src/osm_changeset_wrap.cpp index 0179a4e..e16376d 100644 --- a/src/osm_changeset_wrap.cpp +++ b/src/osm_changeset_wrap.cpp @@ -5,89 +5,92 @@ namespace node_osmium { - extern v8::Persistent module; - - v8::Persistent OSMChangesetWrap::constructor; - - void OSMChangesetWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMChangesetWrap::New)); - constructor->Inherit(OSMEntityWrap::constructor); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Changeset); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "type", get_type, attributes); - set_accessor(constructor, "id", get_id, attributes); - set_accessor(constructor, "uid", get_uid, attributes); - set_accessor(constructor, "user", get_user, attributes); - set_accessor(constructor, "num_changes", get_num_changes, attributes); - set_accessor(constructor, "created_at_seconds_since_epoch", get_created_at, attributes); - set_accessor(constructor, "closed_at_seconds_since_epoch", get_closed_at, attributes); - set_accessor(constructor, "open", get_open, attributes); - set_accessor(constructor, "closed", get_closed, attributes); - set_accessor(constructor, "bounds", get_bounds, attributes); - node::SetPrototypeMethod(constructor, "tags", tags); - target->Set(symbol_Changeset, constructor->GetFunction()); + extern Nan::Persistent module; + + Nan::Persistent OSMChangesetWrap::constructor; + + void OSMChangesetWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + + v8::Local lcons = Nan::New(OSMChangesetWrap::New); + lcons->Inherit(Nan::New(OSMEntityWrap::constructor)); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Changeset)); + ATTR(lcons, "type", get_type); + ATTR(lcons, "id", get_id); + ATTR(lcons, "uid", get_uid); + ATTR(lcons, "user", get_user); + ATTR(lcons, "num_changes", get_num_changes); + ATTR(lcons, "created_at_seconds_since_epoch", get_created_at); + ATTR(lcons, "closed_at_seconds_since_epoch", get_closed_at); + ATTR(lcons, "open", get_open); + ATTR(lcons, "closed", get_closed); + ATTR(lcons, "bounds", get_bounds); + Nan::SetPrototypeMethod(lcons, "tags", tags); + target->Set(Nan::New(symbol_Changeset), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMChangesetWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMChangesetWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Changeset cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.Changeset cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMChangesetWrap::tags(const v8::Arguments& args) { + NAN_METHOD(OSMChangesetWrap::tags) { INSTANCE_CHECK(OSMChangesetWrap, "Changeset", "tags"); - return OSMEntityWrap::tags_impl(args); + return OSMEntityWrap::tags_impl(info); } - v8::Handle OSMChangesetWrap::get_id(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Number::New(wrapped(info.This()).id())); + NAN_GETTER(OSMChangesetWrap::get_id) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).id())); + return; } - v8::Handle OSMChangesetWrap::get_uid(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).uid())); + NAN_GETTER(OSMChangesetWrap::get_uid) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).uid())); + return; } - v8::Handle OSMChangesetWrap::get_user(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::String::New(wrapped(info.This()).user())); + NAN_GETTER(OSMChangesetWrap::get_user) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).user()).ToLocalChecked()); + return; } - v8::Handle OSMChangesetWrap::get_num_changes(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).num_changes())); + NAN_GETTER(OSMChangesetWrap::get_num_changes) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).num_changes())); + return; } - v8::Handle OSMChangesetWrap::get_created_at(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(uint32_t(wrapped(info.This()).created_at()))); + NAN_GETTER(OSMChangesetWrap::get_created_at) { + info.GetReturnValue().Set(Nan::New(uint32_t(wrapped(info.This()).created_at()))); + return; } - v8::Handle OSMChangesetWrap::get_closed_at(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(uint32_t(wrapped(info.This()).closed_at()))); + NAN_GETTER(OSMChangesetWrap::get_closed_at) { + info.GetReturnValue().Set(Nan::New(uint32_t(wrapped(info.This()).closed_at()))); + return; } - v8::Handle OSMChangesetWrap::get_open(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Boolean::New(wrapped(info.This()).open())); + NAN_GETTER(OSMChangesetWrap::get_open) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).open())); + return; } - v8::Handle OSMChangesetWrap::get_closed(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Boolean::New(wrapped(info.This()).closed())); + NAN_GETTER(OSMChangesetWrap::get_closed) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).closed())); + return; } - v8::Handle OSMChangesetWrap::get_bounds(v8::Local /* property */, const v8::AccessorInfo& info) { - return create_js_box(wrapped(info.This()).bounds()); + NAN_GETTER(OSMChangesetWrap::get_bounds) { + info.GetReturnValue().Set(create_js_box(wrapped(info.This()).bounds())); + return; } } // namespace node_osmium diff --git a/src/osm_changeset_wrap.hpp b/src/osm_changeset_wrap.hpp index a9be56f..4b8cf19 100644 --- a/src/osm_changeset_wrap.hpp +++ b/src/osm_changeset_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_CHANGESET_WRAP_HPP #define OSM_CHANGESET_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -19,26 +18,26 @@ namespace node_osmium { class OSMChangesetWrap : public OSMEntityWrap { - static v8::Handle get_type(v8::Local property, const v8::AccessorInfo& info) { - return symbol_changeset; + static NAN_GETTER(get_type) { + info.GetReturnValue().Set(Nan::New(symbol_changeset)); } - static v8::Handle get_id(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_uid(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_user(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_num_changes(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_created_at(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_closed_at(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_open(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_closed(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_bounds(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle tags(const v8::Arguments& args); + static NAN_GETTER(get_id); + static NAN_GETTER(get_uid); + static NAN_GETTER(get_user); + static NAN_GETTER(get_num_changes); + static NAN_GETTER(get_created_at); + static NAN_GETTER(get_closed_at); + static NAN_GETTER(get_open); + static NAN_GETTER(get_closed); + static NAN_GETTER(get_bounds); + static NAN_METHOD(tags); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::Changeset& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); diff --git a/src/osm_entity_wrap.cpp b/src/osm_entity_wrap.cpp index 51122bd..1fe465f 100644 --- a/src/osm_entity_wrap.cpp +++ b/src/osm_entity_wrap.cpp @@ -5,23 +5,26 @@ namespace node_osmium { - v8::Persistent OSMEntityWrap::constructor; + Nan::Persistent OSMEntityWrap::constructor; - void OSMEntityWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMEntityWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_OSMEntity); - target->Set(symbol_OSMEntity, constructor->GetFunction()); + void OSMEntityWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMEntityWrap::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_OSMEntity)); + target->Set(Nan::New(symbol_OSMEntity), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMEntityWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMEntityWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.OSMEntity cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.OSMEntity cannot be created in Javascript").ToLocalChecked()); + return; } } diff --git a/src/osm_entity_wrap.hpp b/src/osm_entity_wrap.hpp index a1b237d..3f725ad 100644 --- a/src/osm_entity_wrap.hpp +++ b/src/osm_entity_wrap.hpp @@ -1,9 +1,7 @@ #ifndef OSM_ENTITY_WRAP_HPP #define OSM_ENTITY_WRAP_HPP -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium namespace osmium { @@ -12,51 +10,55 @@ namespace osmium { #include "utils.hpp" +#define ATTR(t, name, get) \ + Nan::SetAccessor(t->InstanceTemplate(), Nan::New(name).ToLocalChecked(), get, nullptr); + namespace node_osmium { - class OSMEntityWrap : public node::ObjectWrap { + class OSMEntityWrap : public Nan::ObjectWrap { const osmium::OSMEntity* m_entity; protected: - typedef v8::Handle accessor_type(v8::Local property, const v8::AccessorInfo& info); - - static void set_accessor(v8::Persistent t, const char* name, accessor_type getter, v8::PropertyAttribute attributes) { - t->InstanceTemplate()->SetAccessor(v8::String::NewSymbol(name), getter, nullptr, v8::Handle(), v8::DEFAULT, attributes); - } - template - static v8::Handle tags_impl(const v8::Arguments& args) { - v8::HandleScope scope; + static NAN_METHOD(tags_impl) { + Nan::HandleScope scope; - const T& object = static_cast(unwrap(args.This())); - switch (args.Length()) { + const T& object = static_cast(unwrap(info.This())); + switch (info.Length()) { case 0: { - v8::Local tags = v8::Object::New(); + v8::Local tags = Nan::New(); for (const auto& tag : object.tags()) { - tags->Set(v8::String::New(tag.key()), v8::String::New(tag.value())); + tags->Set(Nan::New(tag.key()).ToLocalChecked(), Nan::New(tag.value()).ToLocalChecked()); } - return scope.Close(tags); + info.GetReturnValue().Set(tags); + return; } case 1: { - if (!args[0]->IsString()) { + if (!info[0]->IsString()) { break; } - v8::String::Utf8Value key { args[0] }; + v8::String::Utf8Value key { info[0] }; const char* value = object.tags().get_value_by_key(*key); - return scope.Close(value ? v8::String::New(value) : v8::Undefined()); + if (value) { + info.GetReturnValue().Set(Nan::New(value).ToLocalChecked()); + return; + } + info.GetReturnValue().Set(Nan::Undefined()); + return; } } - return ThrowException(v8::Exception::TypeError(v8::String::New("call tags() without parameters or with a string (the key)"))); + Nan::ThrowTypeError(Nan::New("call tags() without parameters or with a string (the key)").ToLocalChecked()); + return; } public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); OSMEntityWrap() : m_entity(nullptr) { diff --git a/src/osm_node_wrap.cpp b/src/osm_node_wrap.cpp index 101fc3b..821ebd7 100644 --- a/src/osm_node_wrap.cpp +++ b/src/osm_node_wrap.cpp @@ -1,8 +1,4 @@ -// node -#include -#include - // osmium #include #include @@ -12,99 +8,99 @@ namespace node_osmium { - extern v8::Persistent module; + extern Nan::Persistent module; extern osmium::geom::WKBFactory<> wkb_factory; extern osmium::geom::WKTFactory<> wkt_factory; - v8::Persistent OSMNodeWrap::constructor; - - void OSMNodeWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMNodeWrap::New)); - constructor->Inherit(OSMObjectWrap::constructor); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Node); - node::SetPrototypeMethod(constructor, "wkb", wkb); - node::SetPrototypeMethod(constructor, "wkt", wkt); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "type", get_type, attributes); - set_accessor(constructor, "location", get_coordinates, attributes); - set_accessor(constructor, "coordinates", get_coordinates, attributes); - set_accessor(constructor, "lon", get_lon, attributes); - set_accessor(constructor, "lat", get_lat, attributes); - target->Set(symbol_Node, constructor->GetFunction()); + Nan::Persistent OSMNodeWrap::constructor; + + void OSMNodeWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMNodeWrap::New); + lcons->Inherit(Nan::New(OSMWrappedObject::constructor)); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Node)); + Nan::SetPrototypeMethod(lcons, "wkb", wkb); + Nan::SetPrototypeMethod(lcons, "wkt", wkt); + ATTR(lcons, "type", get_type); + ATTR(lcons, "location", get_coordinates); + ATTR(lcons, "coordinates", get_coordinates); + ATTR(lcons, "lon", get_lon); + ATTR(lcons, "lat", get_lat); + target->Set(Nan::New(symbol_Node), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMNodeWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMNodeWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Node cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.Node cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMNodeWrap::get_coordinates(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - - auto cf = module->Get(symbol_Coordinates); + NAN_GETTER(OSMNodeWrap::get_coordinates) { + auto cf = Nan::New(module)->Get(Nan::New(symbol_Coordinates)); assert(cf->IsFunction()); const osmium::Location& location = wrapped(info.This()).location(); if (!location) { - return scope.Close(v8::Local::Cast(cf)->NewInstance()); + info.GetReturnValue().Set(v8::Local::Cast(cf)->NewInstance()); + return; } - v8::Local lon = v8::Number::New(location.lon_without_check()); - v8::Local lat = v8::Number::New(location.lat_without_check()); + v8::Local lon = Nan::New(location.lon_without_check()); + v8::Local lat = Nan::New(location.lat_without_check()); v8::Local argv[2] = { lon, lat }; - return scope.Close(v8::Local::Cast(cf)->NewInstance(2, argv)); + info.GetReturnValue().Set(v8::Local::Cast(cf)->NewInstance(2, argv)); + return; } - v8::Handle OSMNodeWrap::get_lon(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; + NAN_GETTER(OSMNodeWrap::get_lon) { try { - return scope.Close(v8::Number::New(wrapped(info.This()).location().lon())); + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).location().lon())); + return; } catch (osmium::invalid_location&) { - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } - v8::Handle OSMNodeWrap::get_lat(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; + NAN_GETTER(OSMNodeWrap::get_lat) { try { - return scope.Close(v8::Number::New(wrapped(info.This()).location().lat())); + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).location().lat())); + return; } catch (osmium::invalid_location&) { - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } - v8::Handle OSMNodeWrap::wkb(const v8::Arguments& args) { + NAN_METHOD(OSMNodeWrap::wkb) { INSTANCE_CHECK(OSMNodeWrap, "Node", "wkb"); - v8::HandleScope scope; - try { - std::string wkb { wkb_factory.create_point(wrapped(args.This())) }; -#if NODE_VERSION_AT_LEAST(0, 10, 0) - return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); -#else - return scope.Close(node::Buffer::New(const_cast(wkb.data()), wkb.size())->handle_); -#endif + std::string wkb { wkb_factory.create_point(wrapped(info.This())) }; + info.GetReturnValue().Set(Nan::CopyBuffer(wkb.data(), wkb.size()).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } - v8::Handle OSMNodeWrap::wkt(const v8::Arguments& args) { + NAN_METHOD(OSMNodeWrap::wkt) { INSTANCE_CHECK(OSMNodeWrap, "Node", "wkt"); - v8::HandleScope scope; - try { - std::string wkt { wkt_factory.create_point(wrapped(args.This())) }; - return scope.Close(v8::String::New(wkt.c_str())); + std::string wkt { wkt_factory.create_point(wrapped(info.This())) }; + info.GetReturnValue().Set(Nan::New(wkt).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } diff --git a/src/osm_node_wrap.hpp b/src/osm_node_wrap.hpp index 3d8e08f..e9cc928 100644 --- a/src/osm_node_wrap.hpp +++ b/src/osm_node_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_NODE_WRAP_HPP #define OSM_NODE_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -18,34 +17,34 @@ namespace osmium { namespace node_osmium { - class OSMNodeWrap : public OSMObjectWrap { + class OSMNodeWrap : public OSMWrappedObject { - static v8::Handle get_type(v8::Local property, const v8::AccessorInfo& info) { - return symbol_node; + static NAN_GETTER(get_type) { + info.GetReturnValue().Set(Nan::New(symbol_node)); } - static v8::Handle get_coordinates(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_lon(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_lat(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle wkb(const v8::Arguments& args); - static v8::Handle wkt(const v8::Arguments& args); + static NAN_GETTER(get_coordinates); + static NAN_GETTER(get_lon); + static NAN_GETTER(get_lat); + static NAN_METHOD(wkb); + static NAN_METHOD(wkt); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::Node& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); } OSMNodeWrap() : - OSMObjectWrap() { + OSMWrappedObject() { } OSMNodeWrap(const osmium::OSMEntity& entity) : - OSMObjectWrap(entity) { + OSMWrappedObject(entity) { } private: diff --git a/src/osm_object_wrap.cpp b/src/osm_object_wrap.cpp index ec0be6f..ce45eec 100644 --- a/src/osm_object_wrap.cpp +++ b/src/osm_object_wrap.cpp @@ -5,73 +5,75 @@ namespace node_osmium { - v8::Persistent OSMObjectWrap::constructor; + Nan::Persistent OSMWrappedObject::constructor; - void OSMObjectWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMObjectWrap::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_OSMObject); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "id", get_id, attributes); - set_accessor(constructor, "version", get_version, attributes); - set_accessor(constructor, "changeset", get_changeset, attributes); - set_accessor(constructor, "visible", get_visible, attributes); - set_accessor(constructor, "timestamp_seconds_since_epoch", get_timestamp, attributes); - set_accessor(constructor, "uid", get_uid, attributes); - set_accessor(constructor, "user", get_user, attributes); - node::SetPrototypeMethod(constructor, "tags", tags); - target->Set(symbol_OSMObject, constructor->GetFunction()); + void OSMWrappedObject::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMWrappedObject::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_OSMObject)); + ATTR(lcons, "id", get_id); + ATTR(lcons, "version", get_version); + ATTR(lcons, "changeset", get_changeset); + ATTR(lcons, "visible", get_visible); + ATTR(lcons, "timestamp_seconds_since_epoch", get_timestamp); + ATTR(lcons, "uid", get_uid); + ATTR(lcons, "user", get_user); + Nan::SetPrototypeMethod(lcons, "tags", tags); + target->Set(Nan::New(symbol_OSMObject), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMObjectWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMWrappedObject::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.OSMObject cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.OSMObject cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMObjectWrap::tags(const v8::Arguments& args) { - INSTANCE_CHECK(OSMObjectWrap, "Object", "tags"); - return OSMEntityWrap::tags_impl(args); + NAN_METHOD(OSMWrappedObject::tags) { + INSTANCE_CHECK(OSMWrappedObject, "Object", "tags"); + return OSMEntityWrap::tags_impl(info); } - v8::Handle OSMObjectWrap::get_id(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Number::New(wrapped(info.This()).id())); + NAN_GETTER(OSMWrappedObject::get_id) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).id())); + return; } - v8::Handle OSMObjectWrap::get_version(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).version())); + NAN_GETTER(OSMWrappedObject::get_version) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).version())); + return; } - v8::Handle OSMObjectWrap::get_changeset(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).changeset())); + NAN_GETTER(OSMWrappedObject::get_changeset) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).changeset())); + return; } - v8::Handle OSMObjectWrap::get_visible(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Boolean::New(wrapped(info.This()).visible())); + NAN_GETTER(OSMWrappedObject::get_visible) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).visible())); + return; } - v8::Handle OSMObjectWrap::get_timestamp(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(uint32_t(wrapped(info.This()).timestamp()))); + NAN_GETTER(OSMWrappedObject::get_timestamp) { + info.GetReturnValue().Set(Nan::New(uint32_t(wrapped(info.This()).timestamp()))); + return; } - v8::Handle OSMObjectWrap::get_uid(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).uid())); + NAN_GETTER(OSMWrappedObject::get_uid) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).uid())); + return; } - v8::Handle OSMObjectWrap::get_user(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::String::New(wrapped(info.This()).user())); + NAN_GETTER(OSMWrappedObject::get_user) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).user()).ToLocalChecked()); + return; } } // namespace node_osmium diff --git a/src/osm_object_wrap.hpp b/src/osm_object_wrap.hpp index 91beaaa..455ef47 100644 --- a/src/osm_object_wrap.hpp +++ b/src/osm_object_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_OBJECT_WRAP_HPP #define OSM_OBJECT_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -16,40 +15,40 @@ namespace osmium { namespace node_osmium { - class OSMObjectWrap : public OSMEntityWrap { + class OSMWrappedObject : public OSMEntityWrap { - static v8::Handle get_id(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_version(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_changeset(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_visible(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_timestamp(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_uid(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle get_user(v8::Local property, const v8::AccessorInfo& info); - static v8::Handle tags(const v8::Arguments& args); + static NAN_GETTER(get_id); + static NAN_GETTER(get_version); + static NAN_GETTER(get_changeset); + static NAN_GETTER(get_visible); + static NAN_GETTER(get_timestamp); + static NAN_GETTER(get_uid); + static NAN_GETTER(get_user); + static NAN_METHOD(tags); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::OSMObject& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); } - OSMObjectWrap() : + OSMWrappedObject() : OSMEntityWrap() { } - OSMObjectWrap(const osmium::OSMEntity& entity) : + OSMWrappedObject(const osmium::OSMEntity& entity) : OSMEntityWrap(entity) { } protected: - virtual ~OSMObjectWrap() = default; + virtual ~OSMWrappedObject() = default; - }; // class OSMObjectWrap + }; // class OSMWrappedObject } // namespace node_osmium diff --git a/src/osm_relation_wrap.cpp b/src/osm_relation_wrap.cpp index db30e22..d2febb6 100644 --- a/src/osm_relation_wrap.cpp +++ b/src/osm_relation_wrap.cpp @@ -1,86 +1,88 @@ -// node -#include - // node-osmium #include "osm_relation_wrap.hpp" namespace node_osmium { - v8::Persistent OSMRelationWrap::constructor; + Nan::Persistent OSMRelationWrap::constructor; - void OSMRelationWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMRelationWrap::New)); - constructor->Inherit(OSMObjectWrap::constructor); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Relation); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "type", get_type, attributes); - set_accessor(constructor, "members_count", get_members_count, attributes); - node::SetPrototypeMethod(constructor, "members", members); - target->Set(symbol_Relation, constructor->GetFunction()); + void OSMRelationWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMRelationWrap::New); + lcons->Inherit(Nan::New(OSMWrappedObject::constructor)); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Relation)); + ATTR(lcons, "type", get_type); + ATTR(lcons, "members_count", get_members_count); + Nan::SetPrototypeMethod(lcons, "members", members); + target->Set(Nan::New(symbol_Relation), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMRelationWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMRelationWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Relation cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.Relation cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMRelationWrap::get_members_count(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).members().size())); + NAN_GETTER(OSMRelationWrap::get_members_count) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).members().size())); + return; } - v8::Handle OSMRelationWrap::members(const v8::Arguments& args) { - v8::HandleScope scope; - - const osmium::Relation& relation = wrapped(args.This()); + NAN_METHOD(OSMRelationWrap::members) { + const osmium::Relation& relation = wrapped(info.This()); - switch (args.Length()) { + switch (info.Length()) { case 0: { - v8::Local members = v8::Array::New(); + v8::Local members = Nan::New(); int i = 0; char typec[2] = " "; for (const auto& member : relation.members()) { - v8::Local jsmember = v8::Object::New(); + v8::Local jsmember = Nan::New(); typec[0] = osmium::item_type_to_char(member.type()); - jsmember->Set(symbol_type, v8::String::New(typec)); - jsmember->Set(symbol_ref, v8::Number::New(member.ref())); - jsmember->Set(symbol_role, v8::String::New(member.role())); + jsmember->Set(Nan::New(symbol_type), Nan::New(typec).ToLocalChecked()); + jsmember->Set(Nan::New(symbol_ref), Nan::New(member.ref())); + jsmember->Set(Nan::New(symbol_role), Nan::New(member.role()).ToLocalChecked()); members->Set(i, jsmember); ++i; } - return scope.Close(members); + info.GetReturnValue().Set(members); + return; } case 1: { - if (!args[0]->IsUint32()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("call members() without parameters or the index of the member you want"))); + if (!info[0]->IsUint32()) { + Nan::ThrowTypeError(Nan::New("call members() without parameters or the index of the member you want").ToLocalChecked()); + return; } - uint32_t n = args[0]->ToUint32()->Value(); + uint32_t n = info[0]->ToUint32()->Value(); if (n < relation.members().size()) { auto it = relation.members().begin(); std::advance(it, n); const osmium::RelationMember& member = *it; - v8::Local jsmember = v8::Object::New(); + v8::Local jsmember = Nan::New(); char typec[2] = " "; typec[0] = osmium::item_type_to_char(member.type()); - jsmember->Set(symbol_type, v8::String::New(typec)); - jsmember->Set(symbol_ref, v8::Number::New(member.ref())); - jsmember->Set(symbol_role, v8::String::New(member.role())); - return scope.Close(jsmember); + jsmember->Set(Nan::New(symbol_type), Nan::New(typec).ToLocalChecked()); + jsmember->Set(Nan::New(symbol_ref), Nan::New(member.ref())); + jsmember->Set(Nan::New(symbol_role), Nan::New(member.role()).ToLocalChecked()); + info.GetReturnValue().Set(jsmember); + return; } else { - return ThrowException(v8::Exception::RangeError(v8::String::New("argument to members() out of range"))); + Nan::ThrowRangeError(Nan::New("argument to members() out of range").ToLocalChecked()); + return; } } } - return ThrowException(v8::Exception::TypeError(v8::String::New("call members() without parameters or the index of the member you want"))); + Nan::ThrowTypeError(Nan::New("call members() without parameters or the index of the member you want").ToLocalChecked()); + return; } } // namespace node_osmium diff --git a/src/osm_relation_wrap.hpp b/src/osm_relation_wrap.hpp index 934615d..baf47d1 100644 --- a/src/osm_relation_wrap.hpp +++ b/src/osm_relation_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_RELATION_WRAP_HPP #define OSM_RELATION_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -18,31 +17,31 @@ namespace osmium { namespace node_osmium { - class OSMRelationWrap : public OSMObjectWrap { + class OSMRelationWrap : public OSMWrappedObject { - static v8::Handle get_type(v8::Local property, const v8::AccessorInfo& info) { - return symbol_relation; + static NAN_GETTER(get_type) { + info.GetReturnValue().Set(Nan::New(symbol_relation)); } - static v8::Handle get_members_count(v8::Local /* property */, const v8::AccessorInfo& info); - static v8::Handle members(const v8::Arguments& args); + static NAN_GETTER(get_members_count); + static NAN_METHOD(members); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::Relation& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); } OSMRelationWrap() : - OSMObjectWrap() { + OSMWrappedObject() { } OSMRelationWrap(const osmium::OSMEntity& entity) : - OSMObjectWrap(entity) { + OSMWrappedObject(entity) { } private: diff --git a/src/osm_way_wrap.cpp b/src/osm_way_wrap.cpp index 50e4a71..a217444 100644 --- a/src/osm_way_wrap.cpp +++ b/src/osm_way_wrap.cpp @@ -1,8 +1,4 @@ -// node -#include -#include - // osmium #include #include @@ -14,153 +10,165 @@ namespace node_osmium { extern osmium::geom::WKBFactory<> wkb_factory; extern osmium::geom::WKTFactory<> wkt_factory; - extern v8::Persistent module; - - v8::Persistent OSMWayWrap::constructor; - - void OSMWayWrap::Initialize(v8::Handle target) { - v8::HandleScope scope; - constructor = v8::Persistent::New(v8::FunctionTemplate::New(OSMWayWrap::New)); - constructor->Inherit(OSMObjectWrap::constructor); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(symbol_Way); - auto attributes = static_cast(v8::ReadOnly | v8::DontDelete); - set_accessor(constructor, "type", get_type, attributes); - set_accessor(constructor, "nodes_count", get_nodes_count, attributes); - node::SetPrototypeMethod(constructor, "node_refs", node_refs); - node::SetPrototypeMethod(constructor, "node_coordinates", node_coordinates); - node::SetPrototypeMethod(constructor, "wkb", wkb); - node::SetPrototypeMethod(constructor, "wkt", wkt); - target->Set(symbol_Way, constructor->GetFunction()); + extern Nan::Persistent module; + + Nan::Persistent OSMWayWrap::constructor; + + void OSMWayWrap::Initialize(v8::Local target) { + Nan::HandleScope scope; + v8::Local lcons = Nan::New(OSMWayWrap::New); + lcons->Inherit(Nan::New(OSMWrappedObject::constructor)); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(Nan::New(symbol_Way)); + ATTR(lcons, "type", get_type); + ATTR(lcons, "nodes_count", get_nodes_count); + Nan::SetPrototypeMethod(lcons, "node_refs", node_refs); + Nan::SetPrototypeMethod(lcons, "node_coordinates", node_coordinates); + Nan::SetPrototypeMethod(lcons, "wkb", wkb); + Nan::SetPrototypeMethod(lcons, "wkt", wkt); + target->Set(Nan::New(symbol_Way), lcons->GetFunction()); + constructor.Reset(lcons); } - v8::Handle OSMWayWrap::New(const v8::Arguments& args) { - if (args.Length() == 1 && args[0]->IsExternal()) { - v8::Local ext = v8::Local::Cast(args[0]); - static_cast(ext->Value())->Wrap(args.This()); - return args.This(); + NAN_METHOD(OSMWayWrap::New) { + if (info.Length() == 1 && info[0]->IsExternal()) { + v8::Local ext = v8::Local::Cast(info[0]); + static_cast(ext->Value())->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { - return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Way cannot be created in Javascript"))); + Nan::ThrowTypeError(Nan::New("osmium.Way cannot be created in Javascript").ToLocalChecked()); + return; } } - v8::Handle OSMWayWrap::wkb(const v8::Arguments& args) { - v8::HandleScope scope; + NAN_METHOD(OSMWayWrap::wkb) { try { - std::string wkb { wkb_factory.create_linestring(wrapped(args.This()), osmium::geom::use_nodes::unique) }; -#if NODE_VERSION_AT_LEAST(0, 10, 0) - return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); -#else - return scope.Close(node::Buffer::New(const_cast(wkb.data()), wkb.size())->handle_); -#endif + std::string wkb { wkb_factory.create_linestring(wrapped(info.This()), osmium::geom::use_nodes::unique) }; + info.GetReturnValue().Set(Nan::CopyBuffer(wkb.data(), wkb.size()).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } - v8::Handle OSMWayWrap::wkt(const v8::Arguments& args) { - v8::HandleScope scope; + NAN_METHOD(OSMWayWrap::wkt) { try { - std::string wkt { wkt_factory.create_linestring(wrapped(args.This()), osmium::geom::use_nodes::unique) }; - return scope.Close(v8::String::New(wkt.c_str())); + std::string wkt { wkt_factory.create_linestring(wrapped(info.This()), osmium::geom::use_nodes::unique) }; + info.GetReturnValue().Set(Nan::New(wkt).ToLocalChecked()); + return; } catch (std::runtime_error& e) { - return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); + Nan::ThrowError(Nan::New(e.what()).ToLocalChecked()); + return; } } - v8::Handle OSMWayWrap::get_nodes_count(v8::Local /* property */, const v8::AccessorInfo& info) { - v8::HandleScope scope; - return scope.Close(v8::Uint32::New(wrapped(info.This()).nodes().size())); + NAN_GETTER(OSMWayWrap::get_nodes_count) { + info.GetReturnValue().Set(Nan::New(wrapped(info.This()).nodes().size())); + return; } - v8::Handle OSMWayWrap::node_refs(const v8::Arguments& args) { - v8::HandleScope scope; + NAN_METHOD(OSMWayWrap::node_refs) { - const osmium::Way& way = wrapped(args.This()); + const osmium::Way& way = wrapped(info.This()); - switch (args.Length()) { + switch (info.Length()) { case 0: { - v8::Local nodes = v8::Array::New(way.nodes().size()); + v8::Local nodes = Nan::New(way.nodes().size()); int i = 0; for (const auto& node_ref : way.nodes()) { - nodes->Set(i, v8::Number::New(node_ref.ref())); + nodes->Set(i, Nan::New(node_ref.ref())); ++i; } - return scope.Close(nodes); + info.GetReturnValue().Set(nodes); + return; } case 1: { - if (!args[0]->IsUint32()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("call node_refs() without parameters or the index of the node you want"))); + if (!info[0]->IsUint32()) { + Nan::ThrowTypeError(Nan::New("call node_refs() without parameters or the index of the node you want").ToLocalChecked()); + return; } - uint32_t n = args[0]->ToUint32()->Value(); + uint32_t n = info[0]->ToUint32()->Value(); if (n < way.nodes().size()) { - return scope.Close(v8::Number::New(way.nodes()[n].ref())); + info.GetReturnValue().Set(Nan::New(way.nodes()[n].ref())); + return; } else { - return ThrowException(v8::Exception::RangeError(v8::String::New("argument to node_refs() out of range"))); + Nan::ThrowRangeError(Nan::New("argument to node_refs() out of range").ToLocalChecked()); + return; } } } - return ThrowException(v8::Exception::TypeError(v8::String::New("call node_refs() without parameters or the index of the node you want"))); + Nan::ThrowTypeError(Nan::New("call node_refs() without parameters or the index of the node you want").ToLocalChecked()); + return; } - v8::Handle OSMWayWrap::node_coordinates(const v8::Arguments& args) { - v8::HandleScope scope; + NAN_METHOD(OSMWayWrap::node_coordinates) { - auto cf = module->Get(symbol_Coordinates); + auto cf = Nan::New(module)->Get(Nan::New(symbol_Coordinates)); assert(cf->IsFunction()); - const osmium::Way& way = wrapped(args.This()); + const osmium::Way& way = wrapped(info.This()); if (way.nodes().size() < 2) { - return ThrowException(v8::Exception::Error(v8::String::New("Way has no geometry"))); + Nan::ThrowError(Nan::New("Way has no geometry").ToLocalChecked()); + return; } - switch (args.Length()) { + switch (info.Length()) { case 0: { try { - v8::Local nodes = v8::Array::New(0); + v8::Local nodes = Nan::New(0); int i = 0; osmium::Location last_location; for (const auto& node_ref : way.nodes()) { const osmium::Location location = node_ref.location(); if (location != last_location) { - v8::Local argv[2] = { v8::Number::New(location.lon()), v8::Number::New(location.lat()) }; + v8::Local argv[2] = { Nan::New(location.lon()), Nan::New(location.lat()) }; nodes->Set(i, v8::Local::Cast(cf)->NewInstance(2, argv)); ++i; last_location = location; } } if (i < 2) { - return ThrowException(v8::Exception::Error(v8::String::New("Way has no geometry"))); + Nan::ThrowError(Nan::New("Way has no geometry").ToLocalChecked()); + return; } - return scope.Close(nodes); + info.GetReturnValue().Set(nodes); + return; } catch (osmium::invalid_location&) { - return ThrowException(v8::Exception::TypeError(v8::String::New("location of at least one of the nodes in this way not set"))); + Nan::ThrowTypeError(Nan::New("location of at least one of the nodes in this way not set").ToLocalChecked()); + return; } } case 1: { - if (!args[0]->IsUint32()) { - return ThrowException(v8::Exception::TypeError(v8::String::New("call node_coordinates() without parameters or the index of the node you want"))); + if (!info[0]->IsUint32()) { + Nan::ThrowTypeError(Nan::New("call node_coordinates() without parameters or the index of the node you want").ToLocalChecked()); + return; } - uint32_t n = args[0]->ToUint32()->Value(); + uint32_t n = info[0]->ToUint32()->Value(); if (n < way.nodes().size()) { const osmium::Location location = way.nodes()[n].location(); if (location.valid()) { - v8::Local argv[2] = { v8::Number::New(location.lon()), v8::Number::New(location.lat()) }; - return scope.Close(v8::Local::Cast(cf)->NewInstance(2, argv)); + v8::Local argv[2] = { Nan::New(location.lon()), Nan::New(location.lat()) }; + info.GetReturnValue().Set(v8::Local::Cast(cf)->NewInstance(2, argv)); + return; } else { - return scope.Close(v8::Undefined()); + info.GetReturnValue().Set(Nan::Undefined()); + return; } } else { - return ThrowException(v8::Exception::RangeError(v8::String::New("argument to node_coordinates() out of range"))); + Nan::ThrowRangeError(Nan::New("argument to node_coordinates() out of range").ToLocalChecked()); + return; } } } - return ThrowException(v8::Exception::TypeError(v8::String::New("call node_coordinates() without parameters or the index of the node you want"))); + Nan::ThrowTypeError(Nan::New("call node_coordinates() without parameters or the index of the node you want").ToLocalChecked()); + return; } } // namespace node_osmium diff --git a/src/osm_way_wrap.hpp b/src/osm_way_wrap.hpp index aa9435b..f9ebf50 100644 --- a/src/osm_way_wrap.hpp +++ b/src/osm_way_wrap.hpp @@ -1,8 +1,7 @@ #ifndef OSM_WAY_WRAP_HPP #define OSM_WAY_WRAP_HPP -// v8/node -#include "include_v8.hpp" +#include "include_nan.hpp" // osmium #include @@ -18,34 +17,34 @@ namespace osmium { namespace node_osmium { - class OSMWayWrap : public OSMObjectWrap { + class OSMWayWrap : public OSMWrappedObject { - static v8::Handle get_type(v8::Local property, const v8::AccessorInfo& info) { - return symbol_way; + static NAN_GETTER(get_type) { + info.GetReturnValue().Set(Nan::New(symbol_way)); } - static v8::Handle get_nodes_count(v8::Local /* property */, const v8::AccessorInfo& info); - static v8::Handle node_refs(const v8::Arguments& args); - static v8::Handle node_coordinates(const v8::Arguments& args); - static v8::Handle wkb(const v8::Arguments& args); - static v8::Handle wkt(const v8::Arguments& args); + static NAN_GETTER(get_nodes_count); + static NAN_METHOD(node_refs); + static NAN_METHOD(node_coordinates); + static NAN_METHOD(wkb); + static NAN_METHOD(wkt); public: - static v8::Persistent constructor; - static void Initialize(v8::Handle target); - static v8::Handle New(const v8::Arguments& args); + static Nan::Persistent constructor; + static void Initialize(v8::Local target); + static NAN_METHOD(New); static const osmium::Way& wrapped(const v8::Local& object) { return static_cast(unwrap(object)); } OSMWayWrap() : - OSMObjectWrap() { + OSMWrappedObject() { } OSMWayWrap(const osmium::OSMEntity& entity) : - OSMObjectWrap(entity) { + OSMWrappedObject(entity) { } private: diff --git a/src/utils.cpp b/src/utils.cpp index 82a9096..42be5a1 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -7,49 +7,49 @@ namespace node_osmium { - extern v8::Persistent module; + extern Nan::Persistent module; - v8::Handle create_js_box(const osmium::Box& box) { - v8::HandleScope scope; + v8::Local create_js_box(const osmium::Box& box) { + Nan::EscapableHandleScope scope; if (!box.valid()) { - return scope.Close(v8::Undefined()); + return scope.Escape(Nan::Undefined()); } - auto cf = module->Get(symbol_Coordinates); + auto cf = Nan::New(module)->Get(Nan::New(symbol_Coordinates)); assert(cf->IsFunction()); - auto bf = module->Get(symbol_Box); + auto bf = Nan::New(module)->Get(Nan::New(symbol_Box)); assert(bf->IsFunction()); - v8::Local argv_bl[2] = { v8::Number::New(box.bottom_left().lon()), v8::Number::New(box.bottom_left().lat()) }; + v8::Local argv_bl[2] = { Nan::New(box.bottom_left().lon()), Nan::New(box.bottom_left().lat()) }; auto bottom_left = v8::Local::Cast(cf)->NewInstance(2, argv_bl); - v8::Local argv_tr[2] = { v8::Number::New(box.top_right().lon()), v8::Number::New(box.top_right().lat()) }; + v8::Local argv_tr[2] = { Nan::New(box.top_right().lon()), Nan::New(box.top_right().lat()) }; auto top_right = v8::Local::Cast(cf)->NewInstance(2, argv_tr); v8::Local argv_box[2] = { bottom_left, top_right }; - return scope.Close(v8::Local::Cast(bf)->NewInstance(2, argv_box)); + return scope.Escape(v8::Local::Cast(bf)->NewInstance(2, argv_box)); } osmium::osm_entity_bits::type object_to_entity_bits(v8::Local options) { osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nothing; - v8::Local want_nodes = options->Get(symbol_node); + v8::Local want_nodes = options->Get(Nan::New(symbol_node)); if (want_nodes->IsBoolean() && want_nodes->BooleanValue()) { entities |= osmium::osm_entity_bits::node; } - v8::Local want_ways = options->Get(symbol_way); + v8::Local want_ways = options->Get(Nan::New(symbol_way)); if (want_ways->IsBoolean() && want_ways->BooleanValue()) { entities |= osmium::osm_entity_bits::way; } - v8::Local want_relations = options->Get(symbol_relation); + v8::Local want_relations = options->Get(Nan::New(symbol_relation)); if (want_relations->IsBoolean() && want_relations->BooleanValue()) { entities |= osmium::osm_entity_bits::relation; } - v8::Local want_changesets = options->Get(symbol_changeset); + v8::Local want_changesets = options->Get(Nan::New(symbol_changeset)); if (want_changesets->IsBoolean() && want_changesets->BooleanValue()) { entities |= osmium::osm_entity_bits::changeset; } diff --git a/src/utils.hpp b/src/utils.hpp index fb179b3..4d5df21 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -4,9 +4,7 @@ // c++ #include -// v8/node -#include "include_v8.hpp" -#include +#include "include_nan.hpp" // osmium #include @@ -22,25 +20,26 @@ namespace osmium { // This will crash any C++ function that expects a certain type of object. With // this macro we can check whether we have the right kind of object. #define INSTANCE_CHECK(cpp_class, js_class, method) \ -if (!cpp_class::constructor->HasInstance(args.This())) { \ - return ThrowException(v8::Exception::TypeError(v8::String::New("You can only call " method "() on an osmium." js_class ))); \ +if (!Nan::New(cpp_class::constructor)->HasInstance(info.This())) { \ + Nan::ThrowTypeError(Nan::New("You can only call " method "() on an osmium." js_class ).ToLocalChecked()); \ + return ; \ } namespace node_osmium { template - auto unwrap(const v8::Local& object) -> decltype(node::ObjectWrap::Unwrap(object)->get()) { - return node::ObjectWrap::Unwrap(object)->get(); + auto unwrap(const v8::Local& object) -> decltype(Nan::ObjectWrap::Unwrap(object)->get()) { + return Nan::ObjectWrap::Unwrap(object)->get(); } template v8::Local new_external(Args&&... args) { - v8::HandleScope scope; - v8::Handle ext = v8::External::New(new T(std::forward(args)...)); - return scope.Close(T::constructor->GetFunction()->NewInstance(1, &ext)); + Nan::EscapableHandleScope scope; + v8::Handle ext = Nan::New(new T(std::forward(args)...)); + return scope.Escape(Nan::New(T::constructor)->GetFunction()->NewInstance(1, &ext)); } - v8::Handle create_js_box(const osmium::Box& box); + v8::Local create_js_box(const osmium::Box& box); osmium::osm_entity_bits::type object_to_entity_bits(v8::Local options);