From 203fb38a69c339726a3daedacf35cec8663d0ff9 Mon Sep 17 00:00:00 2001 From: King Koopa Date: Wed, 21 May 2014 11:23:08 +0300 Subject: [PATCH] Ported to NAN 1.2.0 --- .travis.yml | 1 + binding.gyp | 5 +- package.json | 89 ++- src/ds_emitter.hpp | 49 +- src/js_grid_utils.hpp | 23 +- src/mapnik_.cpp | 15 +- src/mapnik_.hpp | 5 +- src/mapnik_cairo_surface.cpp | 85 +- src/mapnik_cairo_surface.hpp | 11 +- src/mapnik_color.cpp | 113 ++- src/mapnik_color.hpp | 18 +- src/mapnik_datasource.cpp | 166 ++-- src/mapnik_datasource.hpp | 15 +- src/mapnik_expression.cpp | 75 +- src/mapnik_expression.hpp | 9 +- src/mapnik_feature.cpp | 201 +++-- src/mapnik_feature.hpp | 25 +- src/mapnik_featureset.cpp | 52 +- src/mapnik_featureset.hpp | 7 +- src/mapnik_fonts.hpp | 61 +- src/mapnik_geometry.cpp | 69 +- src/mapnik_geometry.hpp | 11 +- src/mapnik_grid.cpp | 518 ++++++------ src/mapnik_grid.hpp | 32 +- src/mapnik_grid_view.cpp | 449 ++++++----- src/mapnik_grid_view.hpp | 20 +- src/mapnik_image.cpp | 819 ++++++++++--------- src/mapnik_image.hpp | 59 +- src/mapnik_image_view.cpp | 323 ++++---- src/mapnik_image_view.hpp | 26 +- src/mapnik_layer.cpp | 162 ++-- src/mapnik_layer.hpp | 15 +- src/mapnik_map.cpp | 1274 ++++++++++++++++-------------- src/mapnik_map.hpp | 61 +- src/mapnik_memory_datasource.cpp | 147 ++-- src/mapnik_memory_datasource.hpp | 15 +- src/mapnik_palette.cpp | 64 +- src/mapnik_palette.hpp | 9 +- src/mapnik_plugins.hpp | 41 +- src/mapnik_projection.cpp | 319 ++++---- src/mapnik_projection.hpp | 15 +- src/mapnik_vector_tile.cpp | 950 +++++++++++----------- src/mapnik_vector_tile.hpp | 52 +- src/node_mapnik.cpp | 110 ++- src/utils.hpp | 57 +- 45 files changed, 3415 insertions(+), 3227 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9531a502ef..dc8b42b1ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ compiler: env: matrix: + - NODE_NVM_VERSION="0.11" - NODE_NVM_VERSION="0.10" - NODE_NVM_VERSION="0.8" global: diff --git a/binding.gyp b/binding.gyp index f820a6173c..a1ca9f6511 100644 --- a/binding.gyp +++ b/binding.gyp @@ -80,7 +80,8 @@ 'include_dirs': [ './node_modules/mapnik-vector-tile/src/', '<(SHARED_INTERMEDIATE_DIR)/', - './src' + './src', + " (mapnik.org)", - "version" : "1.4.3", - "main" : "./lib/mapnik.js", + "name": "mapnik", + "description": "Tile rendering library for node", + "url": "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/mapnik/node-mapnik", + "homepage": "http://mapnik.org", + "author": "Dane Springmeyer (mapnik.org)", + "version": "1.4.3", + "main": "./lib/mapnik.js", "binary": { - "module_name": "mapnik", - "module_path": "./lib/binding/", - "remote_path": "v{version}", - "host": "https://node-mapnik.s3-us-west-1.amazonaws.com" + "module_name": "mapnik", + "module_path": "./lib/binding/", + "remote_path": "v{version}", + "host": "https://node-mapnik.s3-us-west-1.amazonaws.com" }, - "bugs" : { - "email" : "dane@dbsgeo.com", - "url" : "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/mapnik/node-mapnik/issues" + "bugs": { + "email": "dane@dbsgeo.com", + "url": "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/mapnik/node-mapnik/issues" }, - "keywords" : [ - "map", - "graphics", - "canvas", - "tile", - "mapnik", - "carto" + "keywords": [ + "map", + "graphics", + "canvas", + "tile", + "mapnik", + "carto" ], - "repository" : { - "type" : "git", - "url" : "git://github.com/mapnik/node-mapnik.git" + "repository": { + "type": "git", + "url": "git://github.com/mapnik/node-mapnik.git" }, - "contributors" : ["Konstantin Käfer"], - "licenses" : [ - { - "type": "BSD", - "url": "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/mapnik/node-mapnik/blob/master/LICENSE.txt" - } + "contributors": [ + "Konstantin Käfer" ], - "dependencies" : { - "mapnik-vector-tile": "0.4.2", - "node-pre-gyp":"~0.5.0" + "licenses": [ + { + "type": "BSD", + "url": "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/mapnik/node-mapnik/blob/master/LICENSE.txt" + } + ], + "dependencies": { + "mapnik-vector-tile": "0.4.2", + "nan": "~1.2.0", + "node-pre-gyp": "~0.5.0" }, "bundledDependencies": [ - "mapnik-vector-tile", - "node-pre-gyp" + "mapnik-vector-tile", + "node-pre-gyp" ], - "bin" : { - "mapnik-inspect.js" : "./bin/mapnik-inspect.js", - "mapnik-render.js" : "./bin/mapnik-render.js" + "bin": { + "mapnik-inspect.js": "./bin/mapnik-inspect.js", + "mapnik-render.js": "./bin/mapnik-render.js" }, - "engines" : { - "node": ">= 0.8.0 < 0.11.4" + "engines": { + "node": ">= 0.8.0 < 0.11.4" }, - "scripts" : { - "test" : "mocha", - "install": "node-pre-gyp install --fallback-to-build" + "scripts": { + "test": "mocha", + "install": "node-pre-gyp install --fallback-to-build" }, "devDependencies": { "sphericalmercator": "~1.0.2", diff --git a/src/ds_emitter.hpp b/src/ds_emitter.hpp index d96bdfa889..e6d188b683 100644 --- a/src/ds_emitter.hpp +++ b/src/ds_emitter.hpp @@ -1,9 +1,6 @@ #ifndef __NODE_MAPNIK_DS_EMITTER_H__ #define __NODE_MAPNIK_DS_EMITTER_H__ -// v8 -#include - #include "utils.hpp" // mapnik @@ -21,25 +18,26 @@ namespace node_mapnik { static void describe_datasource(Local description, mapnik::datasource_ptr ds) { + NanScope(); try { // type if (ds->type() == mapnik::datasource::Raster) { - description->Set(String::NewSymbol("type"), String::New("raster")); + description->Set(NanNew("type"), NanNew("raster")); } else { - description->Set(String::NewSymbol("type"), String::New("vector")); + description->Set(NanNew("type"), NanNew("vector")); } mapnik::layer_descriptor ld = ds->get_descriptor(); // encoding - description->Set(String::NewSymbol("encoding"), String::New(ld.get_encoding().c_str())); + description->Set(NanNew("encoding"), NanNew(ld.get_encoding().c_str())); // field names and types - Local fields = Object::New(); + Local fields = NanNew(); std::vector const& desc = ld.get_descriptors(); std::vector::const_iterator itr = desc.begin(); std::vector::const_iterator end = desc.end(); @@ -55,15 +53,16 @@ static void describe_datasource(Local description, mapnik::datasource_pt else if (field_type == mapnik::Geometry) type = "Geometry"; else if (field_type == mapnik::Object) type = "Object"; else type = "Unknown"; - fields->Set(String::NewSymbol(itr->get_name().c_str()),String::New(type.c_str())); + fields->Set(NanNew(itr->get_name().c_str()), NanNew(type.c_str())); + fields->Set(NanNew(itr->get_name().c_str()), NanNew(type.c_str())); ++itr; } - description->Set(String::NewSymbol("fields"), fields); + description->Set(NanNew("fields"), fields); - Local js_type = String::New("unknown"); + Local js_type = NanNew("unknown"); if (ds->type() == mapnik::datasource::Raster) { - js_type = String::New("raster"); + js_type = NanNew("raster"); } else { @@ -76,22 +75,22 @@ static void describe_datasource(Local description, mapnik::datasource_pt { case mapnik::datasource::Point: { - js_type = String::New("point"); + js_type = NanNew("point"); break; } case mapnik::datasource::LineString: { - js_type = String::New("linestring"); + js_type = NanNew("linestring"); break; } case mapnik::datasource::Polygon: { - js_type = String::New("polygon"); + js_type = NanNew("polygon"); break; } case mapnik::datasource::Collection: { - js_type = String::New("collection"); + js_type = NanNew("collection"); break; } default: @@ -102,24 +101,22 @@ static void describe_datasource(Local description, mapnik::datasource_pt } #endif } - description->Set(String::NewSymbol("geometry_type"), js_type); + description->Set(NanNew("geometry_type"), js_type); } catch (std::exception const& ex) { - ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); } catch (...) { - ThrowException(Exception::Error( - String::New("unknown exception happened when calling describe_datasource, please file bug"))); + NanThrowError("unknown exception happened when calling describe_datasource, please file bug"); } } static void datasource_features(Local a, mapnik::datasource_ptr ds, unsigned first, unsigned last) { - + NanScope(); try { mapnik::query q(ds->envelope()); @@ -141,7 +138,7 @@ static void datasource_features(Local a, mapnik::datasource_ptr ds, unsig while ((fp = fs->next())) { if ((idx >= first) && (idx <= last || last == 0)) { - Local feat = Object::New(); + Local feat = NanNew(); #if MAPNIK_VERSION >= 200100 mapnik::feature_impl::iterator f_itr = fp->begin(); mapnik::feature_impl::iterator f_end = fp->end(); @@ -165,7 +162,7 @@ static void datasource_features(Local a, mapnik::datasource_ptr ds, unsig } #endif // add feature id - feat->Set(String::NewSymbol("__id__"), Number::New(fp->id())); + feat->Set(NanNew("__id__"), NanNew(fp->id())); a->Set(idx, feat); } @@ -175,13 +172,11 @@ static void datasource_features(Local a, mapnik::datasource_ptr ds, unsig } catch (std::exception const& ex) { - ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); } catch (...) { - ThrowException(Exception::Error( - String::New("unknown exception happened when calling datasource_features, please file bug"))); + NanThrowError("unknown exception happened when calling datasource_features, please file bug"); } } diff --git a/src/js_grid_utils.hpp b/src/js_grid_utils.hpp index ab2a6f872a..806368ff7c 100644 --- a/src/js_grid_utils.hpp +++ b/src/js_grid_utils.hpp @@ -1,9 +1,6 @@ #ifndef __NODE_MAPNIK_GRID_UTILS_H__ #define __NODE_MAPNIK_GRID_UTILS_H__ -// v8 -#include - // mapnik #include // for feature_impl, etc #include // for grid @@ -155,6 +152,7 @@ static void write_features(T const& grid_type, Local& feature_data, std::vector const& key_order) { + NanScope(); typename T::feature_type const& g_features = grid_type.get_grid_features(); if (g_features.size() <= 0) { @@ -176,19 +174,19 @@ static void write_features(T const& grid_type, } bool found = false; - Local feat = Object::New(); + Local feat = NanNew(); mapnik::feature_ptr feature = feat_itr->second; BOOST_FOREACH ( std::string const& attr, attributes ) { if (attr == "__id__") { - feat->Set(String::NewSymbol(attr.c_str()),Integer::New(feature->id())); + feat->Set(NanNew(attr.c_str()), NanNew(feature->id())); } else if (feature->has_key(attr)) { found = true; mapnik::feature_impl::value_type const& attr_val = feature->get(attr); - feat->Set(String::NewSymbol(attr.c_str()), + feat->Set(NanNew(attr.c_str()), boost::apply_visitor(node_mapnik::value_converter(), attr_val.base())); } @@ -196,7 +194,7 @@ static void write_features(T const& grid_type, if (found) { - feature_data->Set(String::NewSymbol(feat_itr->first.c_str()), feat); + feature_data->Set(NanNew(feat_itr->first.c_str()), feat); } } } @@ -210,6 +208,7 @@ static void grid2utf(T const& grid_type, Local& l, std::vector& key_order) { + NanScope(); typedef std::map< typename T::lookup_type, typename T::value_type> keys_type; typedef typename keys_type::const_iterator keys_iterator; @@ -254,7 +253,7 @@ static void grid2utf(T const& grid_type, } // else, shouldn't get here... } - l->Set(row_idx, String::New(line.get(),array_size)); + l->Set(row_idx, NanNew(line.get(),array_size)); ++row_idx; } } @@ -267,6 +266,7 @@ static void grid2utf(T const& grid_type, std::vector& key_order, unsigned int resolution) { + NanScope(); typedef std::map< typename T::lookup_type, typename T::value_type> keys_type; typedef typename keys_type::const_iterator keys_iterator; @@ -311,7 +311,7 @@ static void grid2utf(T const& grid_type, } // else, shouldn't get here... } - l->Set(row_idx, String::New(line.get(),array_size)); + l->Set(row_idx, NanNew(line.get(),array_size)); ++row_idx; } } @@ -322,6 +322,7 @@ static void write_features(T const& grid_type, Local& feature_data, std::vector const& key_order) { + NanScope(); std::string const& key = grid_type.get_key(); std::set const& attributes = grid_type.property_names(); typename T::feature_type const& g_features = grid_type.get_grid_features(); @@ -340,7 +341,7 @@ static void write_features(T const& grid_type, // only serialize features visible in the grid if(std::find(key_order.begin(), key_order.end(), join_value) != key_order.end()) { - Local feat = Object::New(); + Local feat = NanNew(); std::map::const_iterator it = props.begin(); std::map::const_iterator end = props.end(); bool found = false; @@ -358,7 +359,7 @@ static void write_features(T const& grid_type, if (found) { - feature_data->Set(String::NewSymbol(feat_itr->first.c_str()), feat); + feature_data->Set(NanNew(feat_itr->first.c_str()), feat); } } } diff --git a/src/mapnik_.cpp b/src/mapnik_.cpp index a027fbbff5..6499dc6c21 100644 --- a/src/mapnik_.cpp +++ b/src/mapnik_.cpp @@ -7,13 +7,14 @@ Persistent •::constructor; void •::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(•::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("•")); + Local lcons = NanNew(•::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("•")); - target->Set(String::NewSymbol("•"),constructor->GetFunction()); + target->Set(NanNew("•"),constructor->GetFunction()); + NanAssignPersistent(constructor, lcons); } •::•(std::string const& name) : @@ -24,7 +25,7 @@ ObjectWrap(), { } -Handle •::New(const Arguments& args) +NAN_METHOD(•::New) { - HandleScope scope; + NanScope(); } diff --git a/src/mapnik_.hpp b/src/mapnik_.hpp index c0fa353920..127e3902ec 100644 --- a/src/mapnik_.hpp +++ b/src/mapnik_.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_•_H__ #define __NODE_MAPNIK_•_H__ -#include -#include +#include // mapnik #include "mapnik3x_compatibility.hpp" @@ -19,7 +18,7 @@ class •: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); •(std::string const& name); •(std::string const& name, std::string const& srs); diff --git a/src/mapnik_cairo_surface.cpp b/src/mapnik_cairo_surface.cpp index 2220250bc5..44e3fe7348 100644 --- a/src/mapnik_cairo_surface.cpp +++ b/src/mapnik_cairo_surface.cpp @@ -1,8 +1,3 @@ -// node -#include -#include -#include - #include "utils.hpp" #include "mapnik_cairo_surface.hpp" using namespace v8; @@ -10,15 +5,16 @@ using namespace v8; Persistent CairoSurface::constructor; void CairoSurface::Initialize(Handle target) { + NanScope(); - HandleScope scope; - constructor = Persistent::New(FunctionTemplate::New(CairoSurface::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("CairoSurface")); - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "getData", getData); - target->Set(String::NewSymbol("CairoSurface"),constructor->GetFunction()); + Local lcons = NanNew(CairoSurface::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("CairoSurface")); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "getData", getData); + target->Set(NanNew("CairoSurface"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } CairoSurface::CairoSurface(std::string const& format, unsigned int width, unsigned int height) : @@ -33,67 +29,70 @@ CairoSurface::~CairoSurface() { } -Handle CairoSurface::New(const Arguments& args) +NAN_METHOD(CairoSurface::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); CairoSurface* im = static_cast(ptr); im->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } if (args.Length() == 3) { if (!args[0]->IsString()) - return ThrowException(Exception::Error( - String::New("CairoSurface 'format' must be a string"))); + { + NanThrowTypeError("CairoSurface 'format' must be a string"); + NanReturnUndefined(); + } std::string format = TOSTR(args[0]); if (!args[1]->IsNumber() || !args[2]->IsNumber()) - return ThrowException(Exception::Error( - String::New("CairoSurface 'width' and 'height' must be a integers"))); - CairoSurface* im = new CairoSurface(format,args[1]->IntegerValue(),args[2]->IntegerValue()); + { + NanThrowTypeError("CairoSurface 'width' and 'height' must be a integers"); + NanReturnUndefined(); + } + CairoSurface* im = new CairoSurface(format, args[1]->IntegerValue(), args[2]->IntegerValue()); im->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("CairoSurface requires three arguments: format, width, and height"))); + NanThrowError("CairoSurface requires three arguments: format, width, and height"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle CairoSurface::width(const Arguments& args) +NAN_METHOD(CairoSurface::width) { - HandleScope scope; + NanScope(); - CairoSurface* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->width())); + CairoSurface* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->width())); } -Handle CairoSurface::height(const Arguments& args) +NAN_METHOD(CairoSurface::height) { - HandleScope scope; + NanScope(); - CairoSurface* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->height())); + CairoSurface* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->height())); } -Handle CairoSurface::getData(const Arguments& args) +NAN_METHOD(CairoSurface::getData) { - HandleScope scope; - CairoSurface* surface = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + CairoSurface* surface = node::ObjectWrap::Unwrap(args.Holder()); std::string s = surface->ss_.str(); - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New((char*)s.data(),s.size())); - #else - return scope.Close(node::Buffer::New((char*)s.data(),s.size())->handle_); - #endif + NanReturnValue(NanNewBufferHandle((char*)s.data(), s.size())); } diff --git a/src/mapnik_cairo_surface.hpp b/src/mapnik_cairo_surface.hpp index e565b48bdf..3e5ee99a70 100644 --- a/src/mapnik_cairo_surface.hpp +++ b/src/mapnik_cairo_surface.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_CAIRO_H__ #define __NODE_MAPNIK_CAIRO_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" #include #include @@ -21,10 +20,10 @@ class CairoSurface: public node::ObjectWrap { typedef std::stringstream i_stream; static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); - static Handle getData(const Arguments &args); - static Handle width(const Arguments &args); - static Handle height(const Arguments &args); + static NAN_METHOD(New); + static NAN_METHOD(getData); + static NAN_METHOD(width); + static NAN_METHOD(height); void _ref() { Ref(); } void _unref() { Unref(); } CairoSurface(std::string const& format, unsigned int width, unsigned int height); diff --git a/src/mapnik_color.cpp b/src/mapnik_color.cpp index 44504a6860..9942cade4a 100644 --- a/src/mapnik_color.cpp +++ b/src/mapnik_color.cpp @@ -1,9 +1,6 @@ #include "mapnik_color.hpp" -#include "node.h" // for NODE_SET_PROTOTYPE_METHOD -#include "node_object_wrap.h" // for ObjectWrap #include "utils.hpp" // for ATTR, TOSTR -#include "v8.h" // for Handle, String, Integer, etc // mapnik #include // for color @@ -19,24 +16,24 @@ Persistent Color::constructor; void Color::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Color::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Color")); + Local lcons = NanNew(Color::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Color")); // methods - NODE_SET_PROTOTYPE_METHOD(constructor, "hex", hex); - NODE_SET_PROTOTYPE_METHOD(constructor, "toString", toString); + NODE_SET_PROTOTYPE_METHOD(lcons, "hex", hex); + NODE_SET_PROTOTYPE_METHOD(lcons, "toString", toString); // properties - ATTR(constructor, "r", get_prop, set_prop); - ATTR(constructor, "g", get_prop, set_prop); - ATTR(constructor, "b", get_prop, set_prop); - ATTR(constructor, "a", get_prop, set_prop); + ATTR(lcons, "r", get_prop, set_prop); + ATTR(lcons, "g", get_prop, set_prop); + ATTR(lcons, "b", get_prop, set_prop); + ATTR(lcons, "a", get_prop, set_prop); - - target->Set(String::NewSymbol("Color"),constructor->GetFunction()); + target->Set(NanNew("Color"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Color::Color() : @@ -47,20 +44,23 @@ Color::~Color() { } -Handle Color::New(const Arguments& args) +NAN_METHOD(Color::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Color* c = static_cast(ptr); c->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } color_ptr c_p; @@ -86,18 +86,16 @@ Handle Color::New(const Arguments& args) int a = args[3]->IntegerValue(); c_p = MAPNIK_MAKE_SHARED(r,g,b,a); } else { - return ThrowException(Exception::Error( - String::New("invalid arguments: colors can be created from a string, integer r,g,b values, or integer r,g,b,a values"))); - - + NanThrowTypeError("invalid arguments: colors can be created from a string, integer r,g,b values, or integer r,g,b,a values"); + NanReturnUndefined(); } // todo allow int,int,int and int,int,int,int contructor } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } if (c_p) @@ -105,54 +103,53 @@ Handle Color::New(const Arguments& args) Color* c = new Color(); c->Wrap(args.This()); c->this_ = c_p; - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("unknown exception happened, please file bug"))); + NanThrowError("unknown exception happened, please file bug"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } Handle Color::New(mapnik::color const& color) { - HandleScope scope; + NanEscapableScope(); Color* c = new Color(); c->this_ = MAPNIK_MAKE_SHARED(color); - Handle ext = External::New(c); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(c); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle Color::get_prop(Local property, - const AccessorInfo& info) +NAN_GETTER(Color::get_prop) { - HandleScope scope; - Color* c = node::ObjectWrap::Unwrap(info.This()); + NanScope(); + Color* c = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "a") - return scope.Close(Integer::New(c->get()->alpha())); + NanReturnValue(NanNew(c->get()->alpha())); else if (a == "r") - return scope.Close(Integer::New(c->get()->red())); + NanReturnValue(NanNew(c->get()->red())); else if (a == "g") - return scope.Close(Integer::New(c->get()->green())); + NanReturnValue(NanNew(c->get()->green())); else if (a == "b") - return scope.Close(Integer::New(c->get()->blue())); - return Undefined(); + NanReturnValue(NanNew(c->get()->blue())); + NanReturnUndefined(); } -void Color::set_prop(Local property, - Local value, - const AccessorInfo& info) +NAN_SETTER(Color::set_prop) { - HandleScope scope; - Color* c = node::ObjectWrap::Unwrap(info.This()); + NanScope(); + Color* c = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (!value->IsNumber()) - ThrowException(Exception::TypeError( - String::New("color channel value must be an integer"))); + { + NanThrowTypeError("color channel value must be an integer"); + return; + } if (a == "a") { c->get()->set_alpha(value->IntegerValue()); } else if (a == "r") { @@ -164,20 +161,20 @@ void Color::set_prop(Local property, } } -Handle Color::toString(const Arguments& args) +NAN_METHOD(Color::toString) { - HandleScope scope; + NanScope(); - Color* c = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(String::New( c->get()->to_string().c_str() )); + Color* c = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(c->get()->to_string().c_str())); } -Handle Color::hex(const Arguments& args) +NAN_METHOD(Color::hex) { - HandleScope scope; + NanScope(); - Color* c = node::ObjectWrap::Unwrap(args.This()); + Color* c = node::ObjectWrap::Unwrap(args.Holder()); std::string hex = c->get()->to_hex_string(); - return scope.Close(String::New( hex.c_str() )); + NanReturnValue(NanNew(hex.c_str())); } diff --git a/src/mapnik_color.hpp b/src/mapnik_color.hpp index adf676b4cc..a012f8fd0d 100644 --- a/src/mapnik_color.hpp +++ b/src/mapnik_color.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_COLOR_H__ #define __NODE_MAPNIK_COLOR_H__ -#include // for ObjectWrap -#include // for Handle, AccessorInfo, etc +#include #include "mapnik3x_compatibility.hpp" // boost @@ -17,16 +16,13 @@ class Color: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(mapnik::color const& color); - static Handle toString(const Arguments &args); - static Handle hex(const Arguments &args); - - static Handle get_prop(Local property, - const AccessorInfo& info); - static void set_prop(Local property, - Local value, - const AccessorInfo& info); + static NAN_METHOD(toString); + static NAN_METHOD(hex); + + static NAN_GETTER(get_prop); + static NAN_SETTER(set_prop); Color(); inline color_ptr get() { return this_; } diff --git a/src/mapnik_datasource.cpp b/src/mapnik_datasource.cpp index 8a14e9158d..ae3d0640c8 100644 --- a/src/mapnik_datasource.cpp +++ b/src/mapnik_datasource.cpp @@ -3,11 +3,6 @@ #include "utils.hpp" #include "ds_emitter.hpp" -// node -#include "node.h" // for NODE_SET_PROTOTYPE_METHOD -#include "node_object_wrap.h" // for ObjectWrap -#include "v8.h" // for String, Handle, Object, etc - // mapnik #include // for attribute_descriptor #include // for box2d @@ -26,20 +21,21 @@ Persistent Datasource::constructor; void Datasource::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Datasource::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Datasource")); + Local lcons = NanNew(Datasource::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Datasource")); // methods - NODE_SET_PROTOTYPE_METHOD(constructor, "parameters", parameters); - NODE_SET_PROTOTYPE_METHOD(constructor, "describe", describe); - NODE_SET_PROTOTYPE_METHOD(constructor, "features", features); - NODE_SET_PROTOTYPE_METHOD(constructor, "featureset", featureset); - NODE_SET_PROTOTYPE_METHOD(constructor, "extent", extent); - - target->Set(String::NewSymbol("Datasource"),constructor->GetFunction()); + NODE_SET_PROTOTYPE_METHOD(lcons, "parameters", parameters); + NODE_SET_PROTOTYPE_METHOD(lcons, "describe", describe); + NODE_SET_PROTOTYPE_METHOD(lcons, "features", features); + NODE_SET_PROTOTYPE_METHOD(lcons, "featureset", featureset); + NODE_SET_PROTOTYPE_METHOD(lcons, "extent", extent); + + target->Set(NanNew("Datasource"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Datasource::Datasource() : @@ -50,44 +46,50 @@ Datasource::~Datasource() { } -Handle Datasource::New(const Arguments& args) +NAN_METHOD(Datasource::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Datasource* d = static_cast(ptr); if (d->datasource_->type() == mapnik::datasource::Raster) { - args.This()->Set(String::NewSymbol("type"), - String::NewSymbol("raster"), + args.This()->Set(NanNew("type"), + NanNew("raster"), static_cast(v8::ReadOnly|v8::DontDelete)); } else { - args.This()->Set(String::NewSymbol("type"), - String::NewSymbol("vector"), + args.This()->Set(NanNew("type"), + NanNew("vector"), static_cast(v8::ReadOnly|v8::DontDelete)); } d->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } - if (args.Length() != 1){ - return ThrowException(Exception::TypeError( - String::New("accepts only one argument, an object of key:value datasource options"))); + if (args.Length() != 1) + { + NanThrowTypeError("accepts only one argument, an object of key:value datasource options"); + NanReturnUndefined(); } if (!args[0]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("Must provide an object, eg {type: 'shape', file : 'world.shp'}"))); + { + NanThrowTypeError("Must provide an object, eg {type: 'shape', file : 'world.shp'}"); + NanReturnUndefined(); + } - Local options = args[0]->ToObject(); + Local options = args[0].As(); mapnik::parameters params; Local names = options->GetPropertyNames(); @@ -112,46 +114,46 @@ Handle Datasource::New(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } if (ds) { if (ds->type() == mapnik::datasource::Raster) { - args.This()->Set(String::NewSymbol("type"), - String::NewSymbol("raster"), + args.This()->Set(NanNew("type"), + NanNew("raster"), static_cast(v8::ReadOnly|v8::DontDelete)); } else { - args.This()->Set(String::NewSymbol("type"), - String::NewSymbol("vector"), + args.This()->Set(NanNew("type"), + NanNew("vector"), static_cast(v8::ReadOnly|v8::DontDelete)); } Datasource* d = new Datasource(); d->Wrap(args.This()); d->datasource_ = ds; - return args.This(); + NanReturnValue(args.This()); } - return Undefined(); + NanReturnUndefined(); } Handle Datasource::New(mapnik::datasource_ptr ds_ptr) { - HandleScope scope; + NanEscapableScope(); Datasource* d = new Datasource(); d->datasource_ = ds_ptr; - Handle ext = External::New(d); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(d); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle Datasource::parameters(const Arguments& args) +NAN_METHOD(Datasource::parameters) { - HandleScope scope; + NanScope(); Datasource* d = node::ObjectWrap::Unwrap(args.This()); - Local ds = Object::New(); + Local ds = NanNew(); mapnik::parameters::const_iterator it = d->datasource_->params().begin(); mapnik::parameters::const_iterator end = d->datasource_->params().end(); for (; it != end; ++it) @@ -159,13 +161,13 @@ Handle Datasource::parameters(const Arguments& args) node_mapnik::params_to_object serializer( ds , it->first); boost::apply_visitor( serializer, it->second ); } - return scope.Close(ds); + NanReturnValue(ds); } -Handle Datasource::extent(const Arguments& args) +NAN_METHOD(Datasource::extent) { - HandleScope scope; - Datasource* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Datasource* d = node::ObjectWrap::Unwrap(args.Holder()); mapnik::box2d e; try { @@ -173,41 +175,41 @@ Handle Datasource::extent(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - Local a = Array::New(4); - a->Set(0, Number::New(e.minx())); - a->Set(1, Number::New(e.miny())); - a->Set(2, Number::New(e.maxx())); - a->Set(3, Number::New(e.maxy())); - return scope.Close(a); + Local a = NanNew(4); + a->Set(0, NanNew(e.minx())); + a->Set(1, NanNew(e.miny())); + a->Set(2, NanNew(e.maxx())); + a->Set(3, NanNew(e.maxy())); + NanReturnValue(a); } -Handle Datasource::describe(const Arguments& args) +NAN_METHOD(Datasource::describe) { - HandleScope scope; - Datasource* d = node::ObjectWrap::Unwrap(args.This()); - Local description = Object::New(); + NanScope(); + Datasource* d = node::ObjectWrap::Unwrap(args.Holder()); + Local description = NanNew(); try { node_mapnik::describe_datasource(description,d->datasource_); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return scope.Close(description); + NanReturnValue(description); } -Handle Datasource::features(const Arguments& args) +NAN_METHOD(Datasource::features) { std::clog << "Datasource.features() is deprecated and will be removed at node-mapnik 1.3.x (please use Datasource.featureset instead)\n"; - HandleScope scope; + NanScope(); unsigned first = 0; unsigned last = 0; @@ -215,35 +217,37 @@ Handle Datasource::features(const Arguments& args) if (args.Length() == 2) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::Error( - String::New("Index of 'first' and 'last' feature must be an integer"))); + { + NanThrowTypeError("Index of 'first' and 'last' feature must be an integer"); + NanReturnUndefined(); + } first = args[0]->IntegerValue(); last = args[1]->IntegerValue(); } - Datasource* d = node::ObjectWrap::Unwrap(args.This()); + Datasource* d = node::ObjectWrap::Unwrap(args.Holder()); // TODO - we don't know features.length at this point - Local a = Array::New(0); + Local a = NanNew(0); try { node_mapnik::datasource_features(a,d->datasource_,first,last); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return scope.Close(a); + NanReturnValue(a); } -Handle Datasource::featureset(const Arguments& args) +NAN_METHOD(Datasource::featureset) { - HandleScope scope; + NanScope(); - Datasource* ds = node::ObjectWrap::Unwrap(args.This()); + Datasource* ds = node::ObjectWrap::Unwrap(args.Holder()); mapnik::featureset_ptr fs; try @@ -263,14 +267,14 @@ Handle Datasource::featureset(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } if (fs) { - return scope.Close(Featureset::New(fs)); + NanReturnValue(Featureset::New(fs)); } - return Undefined(); + NanReturnUndefined(); } diff --git a/src/mapnik_datasource.hpp b/src/mapnik_datasource.hpp index a1f735aca6..5bbb3ac2fb 100644 --- a/src/mapnik_datasource.hpp +++ b/src/mapnik_datasource.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_DATASOURCE_H__ #define __NODE_MAPNIK_DATASOURCE_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" #include MAPNIK_SHARED_INCLUDE @@ -17,14 +16,14 @@ class Datasource: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(datasource_ptr ds_ptr); - static Handle parameters(const Arguments &args); - static Handle describe(const Arguments &args); - static Handle features(const Arguments &args); - static Handle featureset(const Arguments &args); - static Handle extent(const Arguments &args); + static NAN_METHOD(parameters); + static NAN_METHOD(describe); + static NAN_METHOD(features); + static NAN_METHOD(featureset); + static NAN_METHOD(extent); Datasource(); inline datasource_ptr get() { return datasource_; } diff --git a/src/mapnik_expression.cpp b/src/mapnik_expression.cpp index 8718bad420..26a6064262 100644 --- a/src/mapnik_expression.cpp +++ b/src/mapnik_expression.cpp @@ -1,6 +1,5 @@ #ifdef NODE_MAPNIK_EXPRESSION -#include #include "utils.hpp" #include "mapnik_expression.hpp" #include "mapnik_feature.hpp" @@ -20,16 +19,17 @@ Persistent Expression::constructor; void Expression::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Expression::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Expression")); + Local lcons = NanNew(Expression::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lconst->SetClassName(NanNew("Expression")); - NODE_SET_PROTOTYPE_METHOD(constructor, "toString", toString); - NODE_SET_PROTOTYPE_METHOD(constructor, "evaluate", evaluate); + NODE_SET_PROTOTYPE_METHOD(lcons, "toString", toString); + NODE_SET_PROTOTYPE_METHOD(lcons, "evaluate", evaluate); - target->Set(String::NewSymbol("Expression"),constructor->GetFunction()); + target->Set(NanNew("Expression"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Expression::Expression() : @@ -40,20 +40,23 @@ Expression::~Expression() { } -Handle Expression::New(const Arguments& args) +NAN_METHOD(Expression::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Expression* e = static_cast(ptr); e->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } mapnik::expression_ptr e_ptr; @@ -63,14 +66,14 @@ Handle Expression::New(const Arguments& args) e_ptr = mapnik::parse_expression(TOSTR(args[0]),"utf8"); } else { - return ThrowException(Exception::Error( - String::New("invalid arguments: accepts a single argument of string type"))); + NanThrowTypeError("invalid arguments: accepts a single argument of string type"); + NanReturnUndefined(); } } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } if (e_ptr) @@ -78,48 +81,50 @@ Handle Expression::New(const Arguments& args) Expression* e = new Expression(); e->Wrap(args.This()); e->this_ = e_ptr; - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("unknown exception happened, please file bug"))); + NanThrowError("unknown exception happened, please file bug"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Expression::toString(const Arguments& args) +NAN_METHOD(Expression::toString) { - HandleScope scope; + NanScope(); - Expression* e = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(String::New( mapnik::to_expression_string(*e->get()).c_str() )); + Expression* e = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(mapnik::to_expression_string(*e->get()).c_str())); } -Handle Expression::evaluate(const Arguments& args) +NAN_METHOD(Expression::evaluate) { - HandleScope scope; + NanScope(); if (args.Length() < 1) { - return ThrowException(Exception::Error( - String::New("requires a mapnik.Feature as an argument"))); + NanThrowError("requires a mapnik.Feature as an argument"); + NanReturnUndefined(); } - Local obj = args[0]->ToObject(); + Local obj = args[0].As(); if (obj->IsNull() || obj->IsUndefined()) { - return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature not null/undefined"))); + NanThrowTypeError("first argument is invalid, must be a mapnik.Feature not null/undefined"); + NanReturnUndefined(); } - if (!Feature::constructor->HasInstance(obj)) { - return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature"))); + if (!NanNew(Feature::constructor)->HasInstance(obj)) { + NanThrowTypeError("first argument is invalid, must be a mapnik.Feature"); + NanReturnUndefined(); } Feature* f = node::ObjectWrap::Unwrap(obj); - Expression* e = node::ObjectWrap::Unwrap(args.This()); + Expression* e = node::ObjectWrap::Unwrap(args.Holder()); mapnik::value value_obj = boost::apply_visitor(mapnik::evaluate(*(f->get())),*(e->get())); - return scope.Close(boost::apply_visitor(node_mapnik::value_converter(),value_obj.base())); + NanReturnValue(boost::apply_visitor(node_mapnik::value_converter(),value_obj.base())); } #endif diff --git a/src/mapnik_expression.hpp b/src/mapnik_expression.hpp index 02e954c6ab..0759122b09 100644 --- a/src/mapnik_expression.hpp +++ b/src/mapnik_expression.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_EXPRESSION_H__ #define __NODE_MAPNIK_EXPRESSION_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" // mapnik @@ -22,9 +21,9 @@ class Expression: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); - static Handle toString(const Arguments &args); - static Handle evaluate(const Arguments &args); + static NAN_METHOD(New); + static NAN_METHOD(toString); + static NAN_METHOD(evaluate); Expression(); inline mapnik::expression_ptr get() { return this_; } diff --git a/src/mapnik_feature.cpp b/src/mapnik_feature.cpp index ab692d59b5..ef294309bb 100644 --- a/src/mapnik_feature.cpp +++ b/src/mapnik_feature.cpp @@ -2,11 +2,6 @@ #include "mapnik_feature.hpp" #include "mapnik_geometry.hpp" -// node -#include -#include -#include - // mapnik #include #include @@ -28,23 +23,24 @@ Persistent Feature::constructor; void Feature::Initialize(Handle target) { - HandleScope scope; - - constructor = Persistent::New(FunctionTemplate::New(Feature::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Feature")); - - NODE_SET_PROTOTYPE_METHOD(constructor, "id", id); - NODE_SET_PROTOTYPE_METHOD(constructor, "extent", extent); - NODE_SET_PROTOTYPE_METHOD(constructor, "attributes", attributes); - NODE_SET_PROTOTYPE_METHOD(constructor, "addGeometry", addGeometry); - NODE_SET_PROTOTYPE_METHOD(constructor, "addAttributes", addAttributes); - NODE_SET_PROTOTYPE_METHOD(constructor, "numGeometries", numGeometries); - NODE_SET_PROTOTYPE_METHOD(constructor, "toString", toString); - NODE_SET_PROTOTYPE_METHOD(constructor, "toJSON", toJSON); - NODE_SET_PROTOTYPE_METHOD(constructor, "toWKB", toWKB); - NODE_SET_PROTOTYPE_METHOD(constructor, "toWKT", toWKT); - target->Set(String::NewSymbol("Feature"),constructor->GetFunction()); + NanScope(); + + Local lcons = NanNew(Feature::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Feature")); + + NODE_SET_PROTOTYPE_METHOD(lcons, "id", id); + NODE_SET_PROTOTYPE_METHOD(lcons, "extent", extent); + NODE_SET_PROTOTYPE_METHOD(lcons, "attributes", attributes); + NODE_SET_PROTOTYPE_METHOD(lcons, "addGeometry", addGeometry); + NODE_SET_PROTOTYPE_METHOD(lcons, "addAttributes", addAttributes); + NODE_SET_PROTOTYPE_METHOD(lcons, "numGeometries", numGeometries); + NODE_SET_PROTOTYPE_METHOD(lcons, "toString", toString); + NODE_SET_PROTOTYPE_METHOD(lcons, "toJSON", toJSON); + NODE_SET_PROTOTYPE_METHOD(lcons, "toWKB", toWKB); + NODE_SET_PROTOTYPE_METHOD(lcons, "toWKT", toWKT); + target->Set(NanNew("Feature"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Feature::Feature(mapnik::feature_ptr f) : @@ -67,74 +63,77 @@ Feature::~Feature() { } -Handle Feature::New(const Arguments& args) +NAN_METHOD(Feature::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Feature* f = static_cast(ptr); f->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } // TODO - expose mapnik.Context if (args.Length() > 1 || args.Length() < 1 || !args[0]->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("requires one argument: an integer feature id"))); + NanThrowTypeError("requires one argument: an integer feature id"); + NanReturnUndefined(); } Feature* f = new Feature(args[0]->IntegerValue()); f->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } Handle Feature::New(mapnik::feature_ptr f_ptr) { - HandleScope scope; + NanEscapableScope(); Feature* f = new Feature(f_ptr); - Handle ext = External::New(f); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(f); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle Feature::id(const Arguments& args) +NAN_METHOD(Feature::id) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Number::New(fp->get()->id())); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(fp->get()->id())); } -Handle Feature::extent(const Arguments& args) +NAN_METHOD(Feature::extent) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); - Local a = Array::New(4); + Local a = NanNew(4); mapnik::box2d const& e = fp->get()->envelope(); - a->Set(0, Number::New(e.minx())); - a->Set(1, Number::New(e.miny())); - a->Set(2, Number::New(e.maxx())); - a->Set(3, Number::New(e.maxy())); + a->Set(0, NanNew(e.minx())); + a->Set(1, NanNew(e.miny())); + a->Set(2, NanNew(e.maxx())); + a->Set(3, NanNew(e.maxy())); - return scope.Close(a); + NanReturnValue(a); } -Handle Feature::attributes(const Arguments& args) +NAN_METHOD(Feature::attributes) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); - Local feat = Object::New(); + Local feat = NanNew(); #if MAPNIK_VERSION >= 200100 mapnik::feature_ptr feature = fp->get(); @@ -155,30 +154,31 @@ Handle Feature::attributes(const Arguments& args) boost::apply_visitor( serializer, it->second.base() ); } #endif - return scope.Close(feat); + NanReturnValue(feat); } -Handle Feature::numGeometries(const Arguments& args) +NAN_METHOD(Feature::numGeometries) { - HandleScope scope; - Feature* fp = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(fp->get()->num_geometries())); + NanScope(); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(fp->get()->num_geometries())); } // TODO void? -Handle Feature::addGeometry(const Arguments& args) +NAN_METHOD(Feature::addGeometry) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() >= 1 ) { Local value = args[0]; if (value->IsNull() || value->IsUndefined()) { - return ThrowException(Exception::TypeError(String::New("mapnik.Geometry instance expected"))); + NanThrowTypeError("mapnik.Geometry instance expected"); + NanReturnUndefined(); } else { Local obj = value->ToObject(); - if (Geometry::constructor->HasInstance(obj)) { + if (NanNew(Geometry::constructor)->HasInstance(obj)) { Geometry* g = node::ObjectWrap::Unwrap(obj); try @@ -187,28 +187,29 @@ Handle Feature::addGeometry(const Arguments& args) } catch (std::exception const& ex ) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } } } - return Undefined(); + NanReturnUndefined(); } -Handle Feature::addAttributes(const Arguments& args) +NAN_METHOD(Feature::addAttributes) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() > 0 ) { Local val = args[0]; if (val->IsNull() || val->IsUndefined()) { - return ThrowException(Exception::TypeError(String::New("object expected"))); + NanThrowTypeError("object expected"); + NanReturnUndefined(); } else { - Local attr = val->ToObject(); + Local attr = val.As(); try { Local names = attr->GetPropertyNames(); @@ -257,81 +258,77 @@ Handle Feature::addAttributes(const Arguments& args) } catch (std::exception const& ex ) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } } - return Undefined(); + NanReturnUndefined(); } -Handle Feature::toString(const Arguments& args) +NAN_METHOD(Feature::toString) { - HandleScope scope; + NanScope(); - Feature* fp = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(String::New(fp->get()->to_string().c_str())); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(fp->get()->to_string().c_str())); } -Handle Feature::toJSON(const Arguments& args) +NAN_METHOD(Feature::toJSON) { - HandleScope scope; + NanScope(); std::string json; #if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100 - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); // TODO - create once? if (!generator.generate(json,*(fp->get()))) { - return ThrowException(Exception::Error( - String::New("Failed to generate GeoJSON"))); + NanThrowError("Failed to generate GeoJSON"); + NanReturnUndefined(); } #else - return ThrowException(Exception::Error( - String::New("GeoJSON output requires at least boost 1.47 and mapnik 2.1.x"))); + NanThrowError("GeoJSON output requires at least boost 1.47 and mapnik 2.1.x"); + NanReturnUndefined(); #endif - return scope.Close(String::New(json.c_str())); + NanReturnValue(NanNew(json.c_str())); } -Handle Feature::toWKT(const Arguments& args) +NAN_METHOD(Feature::toWKT) { - HandleScope scope; + NanScope(); std::string wkt; #if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100 - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); if (!mapnik::util::to_wkt(wkt, fp->get()->paths())) { - return ThrowException(Exception::Error( - String::New("Failed to generate WKT"))); + NanThrowError("Failed to generate WKT"); + NanReturnUndefined(); } #else - return ThrowException(Exception::Error( - String::New("WKT output requires at least boost 1.47 and mapnik 2.1.x"))); + NanThrowError("WKT output requires at least boost 1.47 and mapnik 2.1.x"); + NanReturnUndefined(); #endif - return scope.Close(String::New(wkt.c_str())); + NanReturnValue(NanNew(wkt.c_str())); } -Handle Feature::toWKB(const Arguments& args) +NAN_METHOD(Feature::toWKB) { - HandleScope scope; + NanScope(); std::string wkt; #if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100 - Feature* fp = node::ObjectWrap::Unwrap(args.This()); + Feature* fp = node::ObjectWrap::Unwrap(args.Holder()); mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(fp->get()->paths(), mapnik::util::wkbNDR); - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New(wkb->buffer(), wkb->size())); - #else - return scope.Close(node::Buffer::New(wkb->buffer(), wkb->size())->handle_); - #endif + NanReturnValue(NanNewBufferHandle(wkb->buffer(), wkb->size())); #else - return ThrowException(Exception::Error( - String::New("WKB output requires at least boost 1.47 and mapnik 2.1.x"))); + NanThrowError("WKB output requires at least boost 1.47 and mapnik 2.1.x"); + NanReturnUndefined(); #endif } diff --git a/src/mapnik_feature.hpp b/src/mapnik_feature.hpp index c5f13c93af..c6ada1cd7e 100644 --- a/src/mapnik_feature.hpp +++ b/src/mapnik_feature.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_FEATURE_H__ #define __NODE_MAPNIK_FEATURE_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" // mapnik @@ -19,18 +18,18 @@ class Feature: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(mapnik::feature_ptr f_ptr); - static Handle id(const Arguments &args); - static Handle extent(const Arguments &args); - static Handle attributes(const Arguments &args); - static Handle addGeometry(const Arguments &args); - static Handle addAttributes(const Arguments &args); - static Handle numGeometries(const Arguments &args); - static Handle toString(const Arguments &args); - static Handle toJSON(const Arguments &args); - static Handle toWKB(const Arguments &args); - static Handle toWKT(const Arguments &args); + static NAN_METHOD(id); + static NAN_METHOD(extent); + static NAN_METHOD(attributes); + static NAN_METHOD(addGeometry); + static NAN_METHOD(addAttributes); + static NAN_METHOD(numGeometries); + static NAN_METHOD(toString); + static NAN_METHOD(toJSON); + static NAN_METHOD(toWKB); + static NAN_METHOD(toWKT); // todo // how to allow altering of attributes diff --git a/src/mapnik_featureset.cpp b/src/mapnik_featureset.cpp index f2300461f8..96c33aefed 100644 --- a/src/mapnik_featureset.cpp +++ b/src/mapnik_featureset.cpp @@ -6,15 +6,16 @@ Persistent Featureset::constructor; void Featureset::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Featureset::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Featureset")); + Local lcons = NanNew(Featureset::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Featureset")); - NODE_SET_PROTOTYPE_METHOD(constructor, "next", next); + NODE_SET_PROTOTYPE_METHOD(lcons, "next", next); - target->Set(String::NewSymbol("Featureset"),constructor->GetFunction()); + target->Set(NanNew("Featureset"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Featureset::Featureset() : @@ -25,31 +26,34 @@ Featureset::~Featureset() { } -Handle Featureset::New(const Arguments& args) +NAN_METHOD(Featureset::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Featureset* fs = static_cast(ptr); fs->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } - return ThrowException(Exception::TypeError( - String::New("Sorry a Featureset cannot currently be created, only accessed via an existing datasource"))); + NanThrowTypeError("Sorry a Featureset cannot currently be created, only accessed via an existing datasource"); + NanReturnUndefined(); } -Handle Featureset::next(const Arguments& args) +NAN_METHOD(Featureset::next) { - HandleScope scope; + NanScope(); - Featureset* fs = node::ObjectWrap::Unwrap(args.This()); + Featureset* fs = node::ObjectWrap::Unwrap(args.Holder()); if (fs->this_) { mapnik::feature_ptr fp; @@ -59,23 +63,23 @@ Handle Featureset::next(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } if (fp) { - return scope.Close(Feature::New(fp)); + NanReturnValue(Feature::New(fp)); } } - return Undefined(); + NanReturnUndefined(); } Handle Featureset::New(mapnik::featureset_ptr fs_ptr) { - HandleScope scope; + NanEscapableScope(); Featureset* fs = new Featureset(); fs->this_ = fs_ptr; - Handle ext = External::New(fs); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(fs); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } diff --git a/src/mapnik_featureset.hpp b/src/mapnik_featureset.hpp index ef55494b1a..78b2399c40 100644 --- a/src/mapnik_featureset.hpp +++ b/src/mapnik_featureset.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_FEATURESET_H__ #define __NODE_MAPNIK_FEATURESET_H__ -#include -#include +#include #include #include "mapnik3x_compatibility.hpp" @@ -14,9 +13,9 @@ class Featureset: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(mapnik::featureset_ptr fs_ptr); - static Handle next(const Arguments &args); + static NAN_METHOD(next); Featureset(); diff --git a/src/mapnik_fonts.hpp b/src/mapnik_fonts.hpp index 8fc88d3ba9..a90956de10 100644 --- a/src/mapnik_fonts.hpp +++ b/src/mapnik_fonts.hpp @@ -2,9 +2,6 @@ #define __NODE_MAPNIK_FONTS_H__ -// v8 -#include - // mapnik #include #include @@ -19,15 +16,17 @@ using namespace v8; namespace node_mapnik { -static inline Handle register_fonts(const Arguments& args) +static inline NAN_METHOD(register_fonts) { - HandleScope scope; + NanScope(); try { if (args.Length() == 0 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a directory of fonts"))); + { + NanThrowTypeError("first argument must be a path to a directory of fonts"); + NanReturnUndefined(); + } bool found = false; @@ -36,16 +35,20 @@ static inline Handle register_fonts(const Arguments& args) // option hash if (args.Length() == 2){ if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("second argument is optional, but if provided must be an object, eg. { recurse:Boolean }"))); + { + NanThrowTypeError("second argument is optional, but if provided must be an object, eg. { recurse:Boolean }"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); - if (options->Has(String::New("recurse"))) + Local options = args[1].As(); + if (options->Has(NanNew("recurse"))) { - Local recurse_opt = options->Get(String::New("recurse")); + Local recurse_opt = options->Get(NanNew("recurse")); if (!recurse_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'recurse' must be a Boolean"))); + { + NanThrowTypeError("'recurse' must be a Boolean"); + NanReturnUndefined(); + } bool recurse = recurse_opt->BooleanValue(); std::string path = TOSTR(args[0]); @@ -62,48 +65,48 @@ static inline Handle register_fonts(const Arguments& args) if (names_after.size() == names_before.size()) found = false; - return scope.Close(Boolean::New(found)); + NanReturnValue(NanNew(found)); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -static inline Handle available_font_faces(const Arguments& args) +static inline NAN_METHOD(available_font_faces) { - HandleScope scope; + NanScope(); std::vector const& names = mapnik::freetype_engine::face_names(); - Local a = Array::New(names.size()); + Local a = NanNew(names.size()); for (unsigned i = 0; i < names.size(); ++i) { - a->Set(i, String::New(names[i].c_str())); + a->Set(i, NanNew(names[i].c_str())); } - return scope.Close(a); + NanReturnValue(a); } -static inline Handle available_font_files(const Arguments& args) +static inline NAN_METHOD(available_font_files) { - HandleScope scope; + NanScope(); #if MAPNIK_VERSION >= 200100 std::map > const& mapping = mapnik::freetype_engine::get_mapping(); - Local obj = Object::New(); + Local obj = NanNew(); std::map >::const_iterator itr; for (itr = mapping.begin();itr!=mapping.end();++itr) { - obj->Set(String::NewSymbol(itr->first.c_str()),String::New(itr->second.second.c_str())); + obj->Set(NanNew(itr->first.c_str()), NanNew(itr->second.second.c_str())); } #else std::map const& mapping = mapnik::freetype_engine::get_mapping(); - Local obj = Object::New(); + Local obj = NanNew(); std::map::const_iterator itr; for (itr = mapping.begin();itr!=mapping.end();++itr) { - obj->Set(String::NewSymbol(itr->first.c_str()),String::New(itr->second.c_str())); + obj->Set(NanNew(itr->first.c_str()), NanNew(itr->second.c_str())); } #endif - return scope.Close(obj); + NanReturnValue(obj); } diff --git a/src/mapnik_geometry.cpp b/src/mapnik_geometry.cpp index 85ba2562e3..acf1a45a71 100644 --- a/src/mapnik_geometry.cpp +++ b/src/mapnik_geometry.cpp @@ -11,21 +11,22 @@ Persistent Geometry::constructor; void Geometry::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Geometry::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Geometry")); + Local lcons = NanNew(Geometry::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Geometry")); - NODE_SET_PROTOTYPE_METHOD(constructor, "extent", extent); - NODE_SET_PROTOTYPE_METHOD(constructor, "type", type); - NODE_MAPNIK_DEFINE_CONSTANT(constructor->GetFunction(), + NODE_SET_PROTOTYPE_METHOD(lcons, "extent", extent); + NODE_SET_PROTOTYPE_METHOD(lcons, "type", type); + NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(), "Point",MAPNIK_POINT) - NODE_MAPNIK_DEFINE_CONSTANT(constructor->GetFunction(), + NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(), "LineString",MAPNIK_LINESTRING) - NODE_MAPNIK_DEFINE_CONSTANT(constructor->GetFunction(), + NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(), "Polygon",MAPNIK_POLYGON) - target->Set(String::NewSymbol("Geometry"),constructor->GetFunction()); + target->Set(NanNew("Geometry"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Geometry::Geometry() : @@ -36,53 +37,55 @@ Geometry::~Geometry() { } -Handle Geometry::New(const Arguments& args) +NAN_METHOD(Geometry::New) { - HandleScope scope; - //if (!args.IsConstructCall()) - // return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + NanScope(); + //if (!args.IsConstructCall()) { + // NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + // NanReturnUndefined(); + //} if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Geometry* g = static_cast(ptr); g->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("a mapnik.Geometry cannot be created directly - rather you should create mapnik.Path objects which can contain one or more geometries"))); + NanThrowError("a mapnik.Geometry cannot be created directly - rather you should create mapnik.Path objects which can contain one or more geometries"); + NanReturnUndefined(); } - return args.This(); + NanReturnValue(args.This()); } -Handle Geometry::extent(const Arguments& args) +NAN_METHOD(Geometry::extent) { - HandleScope scope; + NanScope(); - Geometry* g = node::ObjectWrap::Unwrap(args.This()); + Geometry* g = node::ObjectWrap::Unwrap(args.Holder()); - Local a = Array::New(4); + Local a = NanNew(4); mapnik::box2d const& e = g->this_->envelope(); - a->Set(0, Number::New(e.minx())); - a->Set(1, Number::New(e.miny())); - a->Set(2, Number::New(e.maxx())); - a->Set(3, Number::New(e.maxy())); + a->Set(0, NanNew(e.minx())); + a->Set(1, NanNew(e.miny())); + a->Set(2, NanNew(e.maxx())); + a->Set(3, NanNew(e.maxy())); - return scope.Close(a); + NanReturnValue(a); } -Handle Geometry::type(const Arguments& args) +NAN_METHOD(Geometry::type) { - HandleScope scope; + NanScope(); - Geometry* g = node::ObjectWrap::Unwrap(args.This()); + Geometry* g = node::ObjectWrap::Unwrap(args.Holder()); MAPNIK_GEOM_TYPE type = g->this_->type(); // TODO - can we return the actual symbol? - //return scope.Close(constructor->GetFunction()->Get(String::NewSymbol("Point"))); - return scope.Close(Integer::New(static_cast(type))); + //NanReturnValue(constructor->GetFunction()->Get(NanNew("Point"))); + NanReturnValue(NanNew(static_cast(type))); } diff --git a/src/mapnik_geometry.hpp b/src/mapnik_geometry.hpp index 86d1a6987d..c2c92ec093 100644 --- a/src/mapnik_geometry.hpp +++ b/src/mapnik_geometry.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_GEOMETRY_H__ #define __NODE_MAPNIK_GEOMETRY_H__ -#include -#include +#include // mapnik #include @@ -19,10 +18,10 @@ class Geometry: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); - static Handle fromWKT(const Arguments &args); - static Handle extent(const Arguments &args); - static Handle type(const Arguments &args); + static NAN_METHOD(New); + static NAN_METHOD(fromWKT); + static NAN_METHOD(extent); + static NAN_METHOD(type); Geometry(); inline geometry_ptr get() { return this_; } diff --git a/src/mapnik_grid.cpp b/src/mapnik_grid.cpp index 8ec7592793..28caf8c0b9 100644 --- a/src/mapnik_grid.cpp +++ b/src/mapnik_grid.cpp @@ -1,10 +1,4 @@ -// node -#include // for NODE_SET_PROTOTYPE_METHOD, etc -#include // for ObjectWrap -#include -#include - // mapnik #include @@ -27,31 +21,33 @@ Persistent Grid::constructor; void Grid::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Grid::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Grid")); + Local lcons = NanNew(Grid::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Grid")); // methods - NODE_SET_PROTOTYPE_METHOD(constructor, "encodeSync", encodeSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode); - NODE_SET_PROTOTYPE_METHOD(constructor, "fields", fields); - NODE_SET_PROTOTYPE_METHOD(constructor, "view", view); - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "painted", painted); - NODE_SET_PROTOTYPE_METHOD(constructor, "clear", clear); - NODE_SET_PROTOTYPE_METHOD(constructor, "clearSync", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "encodeSync", encodeSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "encode", encode); + NODE_SET_PROTOTYPE_METHOD(lcons, "fields", fields); + NODE_SET_PROTOTYPE_METHOD(lcons, "view", view); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "painted", painted); + NODE_SET_PROTOTYPE_METHOD(lcons, "clear", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "clearSync", clear); // properties - ATTR(constructor, "key", get_prop, set_prop); + ATTR(lcons, "key", get_prop, set_prop); - target->Set(String::NewSymbol("Grid"),constructor->GetFunction()); + target->Set(NanNew("Grid"), lcons->GetFunction()); #if MAPNIK_VERSION < 200100 - NODE_MAPNIK_DEFINE_CONSTANT(constructor->GetFunction(), "base_mask", 0); + NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(), "base_mask", 0); #else - NODE_MAPNIK_DEFINE_64_BIT_CONSTANT(constructor->GetFunction(), "base_mask", mapnik::grid::base_mask); + NODE_MAPNIK_DEFINE_64_BIT_CONSTANT(lcons->GetFunction(), "base_mask", mapnik::grid::base_mask); #endif + + NanAssignPersistent(constructor, lcons); } Grid::Grid(unsigned int width, unsigned int height, std::string const& key, unsigned int resolution) : @@ -61,35 +57,40 @@ Grid::Grid(unsigned int width, unsigned int height, std::string const& key, unsi #if MAPNIK_VERSION <= 200100 this_->painted(false); #endif - V8::AdjustAmountOfExternalAllocatedMemory(estimated_size_); + NanAdjustExternalMemory(estimated_size_); } Grid::~Grid() { - V8::AdjustAmountOfExternalAllocatedMemory(-estimated_size_); + NanAdjustExternalMemory(-estimated_size_); } -Handle Grid::New(const Arguments& args) +NAN_METHOD(Grid::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Grid* g = static_cast(ptr); g->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } if (args.Length() >= 2) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("Grid 'width' and 'height' must be a integers"))); + { + NanThrowTypeError("Grid 'width' and 'height' must be a integers"); + NanReturnUndefined(); + } // defaults std::string key("__id__"); @@ -98,49 +99,61 @@ Handle Grid::New(const Arguments& args) if (args.Length() >= 3) { if (!args[2]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional third arg must be an options object"))); - Local options = args[2]->ToObject(); + { + NanThrowTypeError("optional third arg must be an options object"); + NanReturnUndefined(); + } + Local options = args[2].As(); - if (options->Has(String::New("key"))) { - Local bind_opt = options->Get(String::New("key")); + if (options->Has(NanNew("key"))) { + Local bind_opt = options->Get(NanNew("key")); if (!bind_opt->IsString()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'key' must be an string"))); + { + NanThrowTypeError("optional arg 'key' must be an string"); + NanReturnUndefined(); + } key = TOSTR(bind_opt); } // TODO - remove, deprecated - if (options->Has(String::New("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + if (options->Has(NanNew("resolution"))) { + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'resolution' must be an string"))); + { + NanThrowTypeError("optional arg 'resolution' must be an string"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } } - Grid* g = new Grid(args[0]->IntegerValue(),args[1]->IntegerValue(),key,resolution); + Grid* g = new Grid(args[0]->IntegerValue(), args[1]->IntegerValue(), key, resolution); g->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("please provide Grid width and height"))); + NanThrowError("please provide Grid width and height"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Grid::clearSync(const Arguments& args) +NAN_METHOD(Grid::clearSync) { - HandleScope scope; + NanScope(); + NanReturnValue(_clearSync(args)); +} + +Local Grid::_clearSync(_NAN_METHOD_ARGS) +{ + NanEscapableScope(); #if MAPNIK_VERSION >= 200200 - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); g->get()->clear(); #endif - return Undefined(); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -152,27 +165,29 @@ typedef struct { Persistent cb; } clear_grid_baton_t; -Handle Grid::clear(const Arguments& args) +NAN_METHOD(Grid::clear) { - HandleScope scope; - Grid* g = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { - return clearSync(args); + NanReturnValue(_clearSync(args)); } // ensure callback is a function Local callback = args[args.Length()-1]; if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } clear_grid_baton_t *closure = new clear_grid_baton_t(); closure->request.data = closure; closure->g = g; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Clear, (uv_after_work_cb)EIO_AfterClear); g->Ref(); - return Undefined(); + NanReturnUndefined(); } void Grid::EIO_Clear(uv_work_t* req) @@ -193,124 +208,122 @@ void Grid::EIO_Clear(uv_work_t* req) void Grid::EIO_AfterClear(uv_work_t* req) { - HandleScope scope; + NanScope(); clear_grid_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); - } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); + Local argv[2] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } closure->g->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Grid::painted(const Arguments& args) +NAN_METHOD(Grid::painted) { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Boolean::New(g->get()->painted())); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(g->get()->painted())); } -Handle Grid::width(const Arguments& args) +NAN_METHOD(Grid::width) { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(g->get()->width())); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(g->get()->width())); } -Handle Grid::height(const Arguments& args) +NAN_METHOD(Grid::height) { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(g->get()->height())); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(g->get()->height())); } -Handle Grid::get_prop(Local property, - const AccessorInfo& info) +NAN_GETTER(Grid::get_prop) { - HandleScope scope; - Grid* g = node::ObjectWrap::Unwrap(info.Holder()); + NanScope(); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "key") - return scope.Close(String::New(g->get()->get_key().c_str())); - return Undefined(); + { + NanReturnValue(NanNew(g->get()->get_key().c_str())); + } + NanReturnUndefined(); } -void Grid::set_prop(Local property, - Local value, - const AccessorInfo& info) +NAN_SETTER(Grid::set_prop) { - HandleScope scope; - Grid* g = node::ObjectWrap::Unwrap(info.Holder()); + NanScope(); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "key") { if (!value->IsNumber()) - ThrowException(Exception::TypeError( - String::New("width must be an integer"))); + { + NanThrowTypeError("width must be an integer"); + return; + } g->get()->set_key(TOSTR(value)); } } -Handle Grid::fields(const Arguments& args) +NAN_METHOD(Grid::fields) { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); std::set const& a = g->get()->property_names(); std::set::const_iterator itr = a.begin(); std::set::const_iterator end = a.end(); - Local l = Array::New(a.size()); + Local l = NanNew(a.size()); int idx = 0; for (; itr != end; ++itr) { std::string name = *itr; - l->Set(idx, String::New(name.c_str())); + l->Set(idx, NanNew(name.c_str())); ++idx; } - return scope.Close(l); + NanReturnValue(l); } -Handle Grid::view(const Arguments& args) +NAN_METHOD(Grid::view) { - HandleScope scope; + NanScope(); if ( (args.Length() != 4) || (!args[0]->IsNumber() && !args[1]->IsNumber() && !args[2]->IsNumber() && !args[3]->IsNumber() )) - return ThrowException(Exception::TypeError( - String::New("requires 4 integer arguments: x, y, width, height"))); + { + NanThrowTypeError("requires 4 integer arguments: x, y, width, height"); + NanReturnUndefined(); + } unsigned x = args[0]->IntegerValue(); unsigned y = args[1]->IntegerValue(); unsigned w = args[2]->IntegerValue(); unsigned h = args[3]->IntegerValue(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(GridView::New(g,x,y,w,h)); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(GridView::New(g,x,y,w,h)); } #if MAPNIK_VERSION >= 200100 -Handle Grid::encodeSync(const Arguments& args) // format, resolution +NAN_METHOD(Grid::encodeSync) // format, resolution { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -320,35 +333,43 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -361,18 +382,18 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution node_mapnik::grid2utf(*g->get(),lines,key_order,resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it < key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } mapnik::grid const& grid_type = *g->get(); // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(*g->get(), feature_data, @@ -380,23 +401,23 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution } // Create the return hash. - Local json = Object::New(); - Local grid_array = Array::New(); + Local json = NanNew(); + Local grid_array = NanNew(); unsigned array_size = std::ceil(grid_type.width()/static_cast(resolution)); for (unsigned j=0;jSet(j,String::New(&lines[j],array_size)); + grid_array->Set(j, NanNew(&lines[j],array_size)); } - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); - return json; + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); + NanReturnValue(json); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } @@ -413,11 +434,11 @@ typedef struct { std::vector key_order; } encode_grid_baton_t; -Handle Grid::encode(const Arguments& args) // format, resolution +NAN_METHOD(Grid::encode) // format, resolution { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -427,35 +448,43 @@ Handle Grid::encode(const Arguments& args) // format, resolution // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -463,8 +492,10 @@ Handle Grid::encode(const Arguments& args) // format, resolution // ensure callback is a function if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } Local callback = Local::Cast(args[args.Length()-1]); encode_grid_baton_t *closure = new encode_grid_baton_t(); @@ -474,11 +505,11 @@ Handle Grid::encode(const Arguments& args) // format, resolution closure->error = false; closure->resolution = resolution; closure->add_features = add_features; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); // todo - reserve lines size? uv_queue_work(uv_default_loop(), &closure->request, EIO_Encode, (uv_after_work_cb)EIO_AfterEncode); g->Ref(); - return Undefined(); + NanReturnUndefined(); } void Grid::EIO_Encode(uv_work_t* req) @@ -501,29 +532,28 @@ void Grid::EIO_Encode(uv_work_t* req) void Grid::EIO_AfterEncode(uv_work_t* req) { - HandleScope scope; + NanScope(); encode_grid_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { // convert key order to proper javascript array - Local keys_a = Array::New(closure->key_order.size()); + Local keys_a = NanNew(closure->key_order.size()); std::vector::iterator it; unsigned int i; for (it = closure->key_order.begin(), i = 0; it < closure->key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } mapnik::grid const& grid_type = *closure->g->get(); // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (closure->add_features) { node_mapnik::write_features(grid_type, feature_data, @@ -531,38 +561,34 @@ void Grid::EIO_AfterEncode(uv_work_t* req) } // Create the return hash. - Local json = Object::New(); - Local grid_array = Array::New(closure->lines.size()); + Local json = NanNew(); + Local grid_array = NanNew(closure->lines.size()); unsigned array_size = std::ceil(grid_type.width()/static_cast(closure->resolution)); for (unsigned j=0;jlines.size();++j) { - grid_array->Set(j,String::New(&closure->lines[j],array_size)); + grid_array->Set(j, NanNew(&closure->lines[j],array_size)); } - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); - Local argv[2] = { Local::New(Null()), Local::New(json) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanNew(json) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->g->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } #else -Handle Grid::encodeSync(const Arguments& args) // format, resolution +NAN_METHOD(Grid::encodeSync) // format, resolution { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -572,35 +598,43 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -608,21 +642,21 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution try { - Local grid_array = Array::New(); + Local grid_array = NanNew(); std::vector key_order; node_mapnik::grid2utf(*g->get(),grid_array,key_order,resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it < key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(*g->get(), feature_data, @@ -631,27 +665,27 @@ Handle Grid::encodeSync(const Arguments& args) // format, resolution } // Create the return hash. - Local json = Object::New(); - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); + Local json = NanNew(); + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); return json; } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } // @TODO: convert this to EIO. It's currently doing all the work in the main // thread, and just provides an async interface. -Handle Grid::encode(const Arguments& args) // format, resolution +NAN_METHOD(Grid::encode) // format, resolution { - HandleScope scope; + NanScope(); - Grid* g = node::ObjectWrap::Unwrap(args.This()); + Grid* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -661,35 +695,43 @@ Handle Grid::encode(const Arguments& args) // format, resolution // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } Local options = args[1]->ToObject(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -697,27 +739,29 @@ Handle Grid::encode(const Arguments& args) // format, resolution // ensure callback is a function if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); - Local callback = Local::Cast(args[args.Length()-1]); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } + Local callback = args[args.Length() - 1].As(); try { - Local grid_array = Array::New(); + Local grid_array = NanNew(); std::vector key_order; - node_mapnik::grid2utf(*g->get(),grid_array,key_order,resolution); + node_mapnik::grid2utf(*g->get(), grid_array, key_order, resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it < key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(*g->get(), feature_data, @@ -726,25 +770,21 @@ Handle Grid::encode(const Arguments& args) // format, resolution } // Create the return hash. - Local json = Object::New(); - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); - - TryCatch try_catch; - Local argv[2] = { Local::New(Null()), Local::New(json) }; - callback->Call(Context::GetCurrent()->Global(), 2, argv); - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } + Local json = NanNew(); + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); + + Local argv[2] = { NanNull(), NanNew(json) }; + NanMakeCallback(NanGetCurrentContext()->Global(), callback, 2, argv); } catch (std::exception const& ex) { - Local argv[1] = { Exception::Error(String::New(ex.what())) }; - callback->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(ex.what()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), callback, 1, argv); } - return scope.Close(Undefined()); + NanReturnUndefined(); } #endif diff --git a/src/mapnik_grid.hpp b/src/mapnik_grid.hpp index 02e671d419..05138f0975 100644 --- a/src/mapnik_grid.hpp +++ b/src/mapnik_grid.hpp @@ -1,9 +1,7 @@ #ifndef __NODE_MAPNIK_GRID_H__ #define __NODE_MAPNIK_GRID_H__ -#include -#include -#include +#include #include #include "mapnik3x_compatibility.hpp" @@ -17,28 +15,26 @@ class Grid: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle encodeSync(const Arguments &args); - static Handle encode(const Arguments &args); + static NAN_METHOD(encodeSync); + static NAN_METHOD(encode); static void EIO_Encode(uv_work_t* req); static void EIO_AfterEncode(uv_work_t* req); - static Handle fields(const Arguments &args); - static Handle view(const Arguments &args); - static Handle width(const Arguments &args); - static Handle height(const Arguments &args); - static Handle painted(const Arguments &args); - static Handle clearSync(const Arguments& args); - static Handle clear(const Arguments& args); + static NAN_METHOD(fields); + static NAN_METHOD(view); + static NAN_METHOD(width); + static NAN_METHOD(height); + static NAN_METHOD(painted); + static Local _clearSync(_NAN_METHOD_ARGS); + static NAN_METHOD(clearSync); + static NAN_METHOD(clear); static void EIO_Clear(uv_work_t* req); static void EIO_AfterClear(uv_work_t* req); - static Handle get_prop(Local property, - const AccessorInfo& info); - static void set_prop(Local property, - Local value, - const AccessorInfo& info); + static NAN_GETTER(get_prop); + static NAN_SETTER(set_prop); void _ref() { Ref(); } void _unref() { Unref(); } diff --git a/src/mapnik_grid_view.cpp b/src/mapnik_grid_view.cpp index 79d4552779..32d3b6f465 100644 --- a/src/mapnik_grid_view.cpp +++ b/src/mapnik_grid_view.cpp @@ -1,10 +1,3 @@ - -// node -#include // for NODE_SET_PROTOTYPE_METHOD, etc -#include // for ObjectWrap -#include -#include - // mapnik #include // for hit_grid<>::lookup_type, etc #include // for grid_view, hit_grid_view, etc @@ -28,21 +21,22 @@ Persistent GridView::constructor; void GridView::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(GridView::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("GridView")); + Local lcons = NanNew(GridView::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("GridView")); - NODE_SET_PROTOTYPE_METHOD(constructor, "encodeSync", encodeSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode); - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolid", isSolid); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolidSync", isSolidSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel); + NODE_SET_PROTOTYPE_METHOD(lcons, "encodeSync", encodeSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "encode", encode); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolid", isSolid); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolidSync", isSolidSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "getPixel", getPixel); - target->Set(String::NewSymbol("GridView"),constructor->GetFunction()); + target->Set(NanNew("GridView"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } @@ -58,24 +52,28 @@ GridView::~GridView() JSGrid_->_unref(); } -Handle GridView::New(const Arguments& args) +NAN_METHOD(GridView::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); GridView* g = static_cast(ptr); g->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(String::New("Cannot create this object from Javascript")); + NanThrowError("Cannot create this object from Javascript"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } Handle GridView::New(Grid * JSGrid, @@ -85,28 +83,28 @@ Handle GridView::New(Grid * JSGrid, unsigned h ) { - HandleScope scope; + NanEscapableScope(); GridView* gv = new GridView(JSGrid); gv->this_ = MAPNIK_MAKE_SHARED(JSGrid->get()->get_view(x,y,w,h)); - Handle ext = External::New(gv); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(gv); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle GridView::width(const Arguments& args) +NAN_METHOD(GridView::width) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(g->get()->width())); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(g->get()->width())); } -Handle GridView::height(const Arguments& args) +NAN_METHOD(GridView::height) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(g->get()->height())); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(g->get()->height())); } typedef struct { @@ -120,19 +118,21 @@ typedef struct { } is_solid_grid_view_baton_t; -Handle GridView::isSolid(const Arguments& args) +NAN_METHOD(GridView::isSolid) { - HandleScope scope; - GridView* g = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { return isSolidSync(args); } // ensure callback is a function - Local callback = args[args.Length()-1]; + Local callback = args[args.Length() - 1]; if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } is_solid_grid_view_baton_t *closure = new is_solid_grid_view_baton_t(); closure->request.data = closure; @@ -140,10 +140,10 @@ Handle GridView::isSolid(const Arguments& args) closure->result = true; closure->pixel = 0; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_IsSolid, (uv_after_work_cb)EIO_AfterIsSolid); g->Ref(); - return Undefined(); + NanReturnUndefined(); } void GridView::EIO_IsSolid(uv_work_t* req) { @@ -175,44 +175,39 @@ void GridView::EIO_IsSolid(uv_work_t* req) void GridView::EIO_AfterIsSolid(uv_work_t* req) { - HandleScope scope; + NanScope(); is_solid_grid_view_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { if (closure->result) { - Local argv[3] = { Local::New(Null()), - Local::New(Boolean::New(closure->result)), - Local::New(Number::New(closure->pixel)), + Local argv[3] = { NanNull(), + NanNew(closure->result), + NanNew(closure->pixel), }; - closure->cb->Call(Context::GetCurrent()->Global(), 3, argv); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 3, argv); } else { - Local argv[2] = { Local::New(Null()), - Local::New(Boolean::New(closure->result)) + Local argv[2] = { NanNull(), + NanNew(closure->result) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); - } closure->g->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle GridView::isSolidSync(const Arguments& args) +NAN_METHOD(GridView::isSolidSync) { - HandleScope scope; - GridView* g = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); grid_view_ptr view = g->get(); if (view->width() > 0 && view->height() > 0) { @@ -224,17 +219,17 @@ Handle GridView::isSolidSync(const Arguments& args) { if (first_pixel != row[x]) { - return scope.Close(False()); + NanReturnValue(NanFalse()); } } } } - return scope.Close(True()); + NanReturnValue(NanTrue()); } -Handle GridView::getPixel(const Arguments& args) +NAN_METHOD(GridView::getPixel) { - HandleScope scope; + NanScope(); unsigned x = 0; @@ -242,35 +237,39 @@ Handle GridView::getPixel(const Arguments& args) if (args.Length() >= 2) { if (!args[0]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'x' must be an integer"))); + { + NanThrowTypeError("first arg, 'x' must be an integer"); + NanReturnUndefined(); + } if (!args[1]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("second arg, 'y' must be an integer"))); + { + NanThrowTypeError("second arg, 'y' must be an integer"); + NanReturnUndefined(); + } x = args[0]->IntegerValue(); y = args[1]->IntegerValue(); } else { - return ThrowException(Exception::TypeError( - String::New("must supply x,y to query pixel color"))); + NanThrowTypeError("must supply x,y to query pixel color"); + NanReturnUndefined(); } - GridView* g = node::ObjectWrap::Unwrap(args.This()); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); grid_view_ptr view = g->get(); if (x < view->width() && y < view->height()) { mapnik::grid_view::value_type pixel = view->getRow(y)[x]; - return Number::New(pixel); + NanReturnValue(NanNew(pixel)); } - return Undefined(); + NanReturnUndefined(); } #if MAPNIK_VERSION >= 200100 -Handle GridView::encodeSync(const Arguments& args) +NAN_METHOD(GridView::encodeSync) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -280,35 +279,43 @@ Handle GridView::encodeSync(const Arguments& args) // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -321,18 +328,18 @@ Handle GridView::encodeSync(const Arguments& args) node_mapnik::grid2utf(*g->get(),lines,key_order,resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it != key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } mapnik::grid_view const& grid_type = *g->get(); // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(grid_type, feature_data, @@ -340,23 +347,23 @@ Handle GridView::encodeSync(const Arguments& args) } // Create the return hash. - Local json = Object::New(); - Local grid_array = Array::New(); + Local json = NanNew(); + Local grid_array = NanNew(); unsigned array_size = std::ceil(grid_type.width()/static_cast(resolution)); for (unsigned j=0;jSet(j,String::New(&lines[j],array_size)); + grid_array->Set(j, NanNew(&lines[j],array_size)); } - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); - return json; + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); + NanReturnValue(json); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } @@ -375,11 +382,11 @@ typedef struct { } encode_grid_view_baton_t; -Handle GridView::encode(const Arguments& args) +NAN_METHOD(GridView::encode) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -389,35 +396,43 @@ Handle GridView::encode(const Arguments& args) // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -425,9 +440,11 @@ Handle GridView::encode(const Arguments& args) // ensure callback is a function if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); - Local callback = Local::Cast(args[args.Length()-1]); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } + Local callback = args[args.Length() - 1].As(); encode_grid_view_baton_t *closure = new encode_grid_view_baton_t(); closure->request.data = closure; @@ -436,10 +453,10 @@ Handle GridView::encode(const Arguments& args) closure->error = false; closure->resolution = resolution; closure->add_features = add_features; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback); uv_queue_work(uv_default_loop(), &closure->request, EIO_Encode, (uv_after_work_cb)EIO_AfterEncode); g->Ref(); - return Undefined(); + NanReturnUndefined(); } void GridView::EIO_Encode(uv_work_t* req) @@ -463,69 +480,63 @@ void GridView::EIO_Encode(uv_work_t* req) void GridView::EIO_AfterEncode(uv_work_t* req) { - HandleScope scope; + NanScope(); encode_grid_view_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { // convert key order to proper javascript array - Local keys_a = Array::New(closure->key_order.size()); + Local keys_a = NanNew(closure->key_order.size()); std::vector::iterator it; unsigned int i; for (it = closure->key_order.begin(), i = 0; it != closure->key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } mapnik::grid_view const& grid_type = *(closure->g->get()); // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (closure->add_features) { node_mapnik::write_features(grid_type, feature_data, closure->key_order); } // Create the return hash. - Local json = Object::New(); - Local grid_array = Array::New(closure->lines.size()); + Local json = NanNew(); + Local grid_array = NanNew(closure->lines.size()); unsigned array_size = std::ceil(grid_type.width()/static_cast(closure->resolution)); for (unsigned j=0;jlines.size();++j) { - grid_array->Set(j,String::New(&closure->lines[j],array_size)); + grid_array->Set(j, NanNew(&closure->lines[j], array_size)); } - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); - - Local argv[2] = { Local::New(Null()), Local::New(json) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), json }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->g->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } #else -Handle GridView::encodeSync(const Arguments& args) +NAN_METHOD(GridView::encodeSync) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -535,35 +546,43 @@ Handle GridView::encodeSync(const Arguments& args) // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -571,21 +590,21 @@ Handle GridView::encodeSync(const Arguments& args) try { - Local grid_array = Array::New(); + Local grid_array = NanNew(); std::vector key_order; - node_mapnik::grid2utf(*g->get(),grid_array,key_order,resolution); + node_mapnik::grid2utf(*g->get(), grid_array, key_order, resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it != key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(*g->get(), feature_data, @@ -594,26 +613,26 @@ Handle GridView::encodeSync(const Arguments& args) } // Create the return hash. - Local json = Object::New(); - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); - return json; + Local json = NanNew(); + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); + NanReturnValue(json); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle GridView::encode(const Arguments& args) +NAN_METHOD(GridView::encode) { - HandleScope scope; + NanScope(); - GridView* g = node::ObjectWrap::Unwrap(args.This()); + GridView* g = node::ObjectWrap::Unwrap(args.Holder()); // defaults std::string format("utf"); @@ -623,35 +642,43 @@ Handle GridView::encode(const Arguments& args) // accept custom format if (args.Length() >= 1){ if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } Local options = args[1]->ToObject(); - if (options->Has(String::New("resolution"))) + if (options->Has(NanNew("resolution"))) { - Local bind_opt = options->Get(String::New("resolution")); + Local bind_opt = options->Get(NanNew("resolution")); if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("'resolution' must be an Integer"))); + { + NanThrowTypeError("'resolution' must be an Integer"); + NanReturnUndefined(); + } resolution = bind_opt->IntegerValue(); } - if (options->Has(String::New("features"))) + if (options->Has(NanNew("features"))) { - Local bind_opt = options->Get(String::New("features")); + Local bind_opt = options->Get(NanNew("features")); if (!bind_opt->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'features' must be an Boolean"))); + { + NanThrowTypeError("'features' must be an Boolean"); + NanReturnUndefined(); + } add_features = bind_opt->BooleanValue(); } @@ -659,27 +686,29 @@ Handle GridView::encode(const Arguments& args) // ensure callback is a function if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); - Local callback = Local::Cast(args[args.Length()-1]); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } + Local callback = args[args.Length() - 1].As(); try { - Local grid_array = Array::New(); + Local grid_array = NanNew(); std::vector key_order; - node_mapnik::grid2utf(*g->get(),grid_array,key_order,resolution); + node_mapnik::grid2utf(*g->get(), grid_array, key_order, resolution); // convert key order to proper javascript array - Local keys_a = Array::New(key_order.size()); + Local keys_a = NanNew(key_order.size()); std::vector::iterator it; unsigned int i; for (it = key_order.begin(), i = 0; it != key_order.end(); ++it, ++i) { - keys_a->Set(i, String::New((*it).c_str())); + keys_a->Set(i, NanNew((*it).c_str())); } // gather feature data - Local feature_data = Object::New(); + Local feature_data = NanNew(); if (add_features) { node_mapnik::write_features(*g->get(), feature_data, @@ -688,25 +717,21 @@ Handle GridView::encode(const Arguments& args) } // Create the return hash. - Local json = Object::New(); - json->Set(String::NewSymbol("grid"), grid_array); - json->Set(String::NewSymbol("keys"), keys_a); - json->Set(String::NewSymbol("data"), feature_data); + Local json = NanNew(); + json->Set(NanNew("grid"), grid_array); + json->Set(NanNew("keys"), keys_a); + json->Set(NanNew("data"), feature_data); - TryCatch try_catch; - Local argv[2] = { Local::New(Null()), Local::New(json) }; - callback->Call(Context::GetCurrent()->Global(), 2, argv); - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } + Local argv[2] = { NanNull(), json }; + NanMakeCallback(NanGetCurrentContext()->Global(), callback, 2, argv); } catch (std::exception const& ex) { - Local argv[1] = { Exception::Error(String::New(ex.what())) }; - callback->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(ex.what()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), callback, 1, argv); } - return scope.Close(Undefined()); + NanReturnUndefined(); } #endif diff --git a/src/mapnik_grid_view.hpp b/src/mapnik_grid_view.hpp index d1e43609bf..1667cc4e2a 100644 --- a/src/mapnik_grid_view.hpp +++ b/src/mapnik_grid_view.hpp @@ -1,9 +1,7 @@ #ifndef __NODE_MAPNIK_GRID_VIEW_H__ #define __NODE_MAPNIK_GRID_VIEW_H__ -#include -#include -#include +#include #include #include "mapnik3x_compatibility.hpp" #include MAPNIK_SHARED_INCLUDE @@ -17,22 +15,22 @@ class GridView: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(Grid * JSGrid, unsigned x,unsigned y, unsigned w, unsigned h); - static Handle encodeSync(const Arguments &args); - static Handle encode(const Arguments &args); + static NAN_METHOD(encodeSync); + static NAN_METHOD(encode); static void EIO_Encode(uv_work_t* req); static void EIO_AfterEncode(uv_work_t* req); - static Handle width(const Arguments &args); - static Handle height(const Arguments &args); - static Handle isSolid(const Arguments &args); + static NAN_METHOD(width); + static NAN_METHOD(height); + static NAN_METHOD(isSolid); static void EIO_IsSolid(uv_work_t* req); static void EIO_AfterIsSolid(uv_work_t* req); - static Handle isSolidSync(const Arguments &args); - static Handle getPixel(const Arguments &args); + static NAN_METHOD(isSolidSync); + static NAN_METHOD(getPixel); GridView(Grid * JSGrid); inline grid_view_ptr get() { return this_; } diff --git a/src/mapnik_image.cpp b/src/mapnik_image.cpp index 8b2029d57c..eaa8b064d9 100644 --- a/src/mapnik_image.cpp +++ b/src/mapnik_image.cpp @@ -1,8 +1,3 @@ -// node -#include -#include -#include - // mapnik #include // for color #include // for image_32 @@ -39,46 +34,47 @@ Persistent Image::constructor; void Image::Initialize(Handle target) { - HandleScope scope; - - constructor = Persistent::New(FunctionTemplate::New(Image::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Image")); - - NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel); - NODE_SET_PROTOTYPE_METHOD(constructor, "setPixel", setPixel); - NODE_SET_PROTOTYPE_METHOD(constructor, "encodeSync", encodeSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode); - NODE_SET_PROTOTYPE_METHOD(constructor, "view", view); - NODE_SET_PROTOTYPE_METHOD(constructor, "save", save); - NODE_SET_PROTOTYPE_METHOD(constructor, "setGrayScaleToAlpha", setGrayScaleToAlpha); - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "painted", painted); - NODE_SET_PROTOTYPE_METHOD(constructor, "composite", composite); - NODE_SET_PROTOTYPE_METHOD(constructor, "premultiplySync", premultiplySync); - NODE_SET_PROTOTYPE_METHOD(constructor, "premultiply", premultiply); - NODE_SET_PROTOTYPE_METHOD(constructor, "demultiplySync", demultiplySync); - NODE_SET_PROTOTYPE_METHOD(constructor, "demultiply", demultiply); - NODE_SET_PROTOTYPE_METHOD(constructor, "clear", clear); - NODE_SET_PROTOTYPE_METHOD(constructor, "clearSync", clear); - - ATTR(constructor, "background", get_prop, set_prop); + NanScope(); + + Local lcons = NanNew(Image::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Image")); + + NODE_SET_PROTOTYPE_METHOD(lcons, "getPixel", getPixel); + NODE_SET_PROTOTYPE_METHOD(lcons, "setPixel", setPixel); + NODE_SET_PROTOTYPE_METHOD(lcons, "encodeSync", encodeSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "encode", encode); + NODE_SET_PROTOTYPE_METHOD(lcons, "view", view); + NODE_SET_PROTOTYPE_METHOD(lcons, "save", save); + NODE_SET_PROTOTYPE_METHOD(lcons, "setGrayScaleToAlpha", setGrayScaleToAlpha); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "painted", painted); + NODE_SET_PROTOTYPE_METHOD(lcons, "composite", composite); + NODE_SET_PROTOTYPE_METHOD(lcons, "premultiplySync", premultiplySync); + NODE_SET_PROTOTYPE_METHOD(lcons, "premultiply", premultiply); + NODE_SET_PROTOTYPE_METHOD(lcons, "demultiplySync", demultiplySync); + NODE_SET_PROTOTYPE_METHOD(lcons, "demultiply", demultiply); + NODE_SET_PROTOTYPE_METHOD(lcons, "clear", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "clearSync", clear); + + ATTR(lcons, "background", get_prop, set_prop); // This *must* go after the ATTR setting - NODE_SET_METHOD(constructor->GetFunction(), + NODE_SET_METHOD(lcons->GetFunction(), "open", Image::open); - NODE_SET_METHOD(constructor->GetFunction(), + NODE_SET_METHOD(lcons->GetFunction(), "fromBytes", Image::fromBytes); - NODE_SET_METHOD(constructor->GetFunction(), + NODE_SET_METHOD(lcons->GetFunction(), "openSync", Image::openSync); - NODE_SET_METHOD(constructor->GetFunction(), + NODE_SET_METHOD(lcons->GetFunction(), "fromBytesSync", Image::fromBytesSync); - target->Set(String::NewSymbol("Image"),constructor->GetFunction()); + target->Set(NanNew("Image"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Image::Image(unsigned int width, unsigned int height) : @@ -86,7 +82,7 @@ Image::Image(unsigned int width, unsigned int height) : this_(MAPNIK_MAKE_SHARED(width,height)), estimated_size_(width * height * 4) { - V8::AdjustAmountOfExternalAllocatedMemory(estimated_size_); + NanAdjustExternalMemory(estimated_size_); } Image::Image(image_ptr _this) : @@ -94,28 +90,31 @@ Image::Image(image_ptr _this) : this_(_this), estimated_size_(this_->width() * this_->height() * 4) { - V8::AdjustAmountOfExternalAllocatedMemory(estimated_size_); + NanAdjustExternalMemory(estimated_size_); } Image::~Image() { - V8::AdjustAmountOfExternalAllocatedMemory(-estimated_size_); + NanAdjustExternalMemory(-estimated_size_); } -Handle Image::New(const Arguments& args) +NAN_METHOD(Image::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Image* im = static_cast(ptr); im->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } try @@ -123,81 +122,87 @@ Handle Image::New(const Arguments& args) if (args.Length() == 2) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::Error( - String::New("Image 'width' and 'height' must be a integers"))); + { + NanThrowTypeError("Image 'width' and 'height' must be a integers"); + NanReturnUndefined(); + } Image* im = new Image(args[0]->IntegerValue(),args[1]->IntegerValue()); im->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("please provide Image width and height"))); + NanThrowError("please provide Image width and height"); + NanReturnUndefined(); } } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Image::get_prop(Local property, - const AccessorInfo& info) +NAN_GETTER(Image::get_prop) { - HandleScope scope; - Image* im = node::ObjectWrap::Unwrap(info.Holder()); + NanScope(); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "background") { boost::optional c = im->get()->get_background(); - if (c) - return scope.Close(Color::New(*c)); - else - return Undefined(); + if (c) { + NanReturnValue(Color::New(*c)); + } else { + NanReturnUndefined(); + } } - return Undefined(); + NanReturnUndefined(); } -void Image::set_prop(Local property, - Local value, - const AccessorInfo& info) +NAN_SETTER(Image::set_prop) { - HandleScope scope; - Image* im = node::ObjectWrap::Unwrap(info.Holder()); + NanScope(); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "background") { if (!value->IsObject()) - ThrowException(Exception::TypeError( - String::New("mapnik.Color expected"))); + { + NanThrowTypeError("mapnik.Color expected"); + return; + } Local obj = value->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Color::constructor->HasInstance(obj)) - ThrowException(Exception::TypeError(String::New("mapnik.Color expected"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Color::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Color expected"); + return; + } Color *c = node::ObjectWrap::Unwrap(obj); im->get()->set_background(*c->get()); } } -Handle Image::getPixel(const Arguments& args) +NAN_METHOD(Image::getPixel) { - HandleScope scope; + NanScope(); int x = 0; int y = 0; if (args.Length() >= 2) { - if (!args[0]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'x' must be an integer"))); - if (!args[1]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("second arg, 'y' must be an integer"))); + if (!args[0]->IsNumber()) { + NanThrowTypeError("first arg, 'x' must be an integer"); + NanReturnUndefined(); + } + if (!args[1]->IsNumber()) { + NanThrowTypeError("second arg, 'y' must be an integer"); + NanReturnUndefined(); + } x = args[0]->IntegerValue(); y = args[1]->IntegerValue(); } else { - return ThrowException(Exception::TypeError( - String::New("must supply x,y to query pixel color"))); + NanThrowError("must supply x,y to query pixel color"); + NanReturnUndefined(); } - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); mapnik::image_data_32 const& data = im->this_->data(); if (x >= 0 && x < static_cast(data.width()) && y >= 0 && y < static_cast(data.height())) @@ -207,42 +212,50 @@ Handle Image::getPixel(const Arguments& args) unsigned g = (pixel >> 8) & 0xff; unsigned b = (pixel >> 16) & 0xff; unsigned a = (pixel >> 24) & 0xff; - return Color::New(mapnik::color(r,g,b,a)); + NanReturnValue(Color::New(mapnik::color(r,g,b,a))); } - return Undefined(); + NanReturnUndefined(); } -Handle Image::setPixel(const Arguments& args) +NAN_METHOD(Image::setPixel) { - HandleScope scope; + NanScope(); if (args.Length() < 3 || (!args[0]->IsNumber() && !args[1]->IsNumber())) { - return ThrowException(Exception::TypeError(String::New("expects three arguments: x, y, and pixel value"))); + NanThrowTypeError("expects three arguments: x, y, and pixel value"); + NanReturnUndefined(); } Local obj = args[2]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Color::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Color expected as third arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Color::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Color expected as third arg"); + NanReturnUndefined(); + } Color * color = node::ObjectWrap::Unwrap(obj); int x = args[0]->IntegerValue(); int y = args[1]->IntegerValue(); - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); mapnik::image_data_32 & data = im->this_->data(); if (x < static_cast(data.width()) && y < static_cast(data.height())) { data(x,y) = color->get()->rgba(); - return scope.Close(Undefined()); + NanReturnUndefined(); } - return ThrowException(Exception::TypeError(String::New("invalid pixel requested"))); - return scope.Close(Undefined()); + NanThrowTypeError("invalid pixel requested"); + NanReturnUndefined(); } -Handle Image::clearSync(const Arguments& args) +NAN_METHOD(Image::clearSync) { - HandleScope scope; + NanScope(); + NanReturnValue(_clearSync(args)); +} + +Local Image::_clearSync(_NAN_METHOD_ARGS) { + NanEscapableScope(); #if MAPNIK_VERSION >= 200200 - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); im->get()->clear(); #endif - return Undefined(); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -254,27 +267,28 @@ typedef struct { Persistent cb; } clear_image_baton_t; -Handle Image::clear(const Arguments& args) +NAN_METHOD(Image::clear) { - HandleScope scope; - Image* im = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { - return clearSync(args); + NanReturnValue(_clearSync(args)); } // ensure callback is a function Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } clear_image_baton_t *closure = new clear_image_baton_t(); closure->request.data = closure; closure->im = im; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Clear, (uv_after_work_cb)EIO_AfterClear); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Clear(uv_work_t* req) @@ -295,44 +309,43 @@ void Image::EIO_Clear(uv_work_t* req) void Image::EIO_AfterClear(uv_work_t* req) { - HandleScope scope; + NanScope(); clear_image_baton_t *closure = static_cast(req->data); TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[1] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); - } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); + Local argv[1] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } closure->im->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Image::setGrayScaleToAlpha(const Arguments& args) +NAN_METHOD(Image::setGrayScaleToAlpha) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { im->this_->set_grayscale_to_alpha(); } else { - if (!args[0]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be a mapnik.Color"))); + if (!args[0]->IsObject()) { + NanThrowTypeError("optional second arg must be a mapnik.Color"); + NanReturnUndefined(); + } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Color::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Color expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Color::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Color expected as second arg"); + NanReturnUndefined(); + } Color * color = node::ObjectWrap::Unwrap(obj); @@ -359,7 +372,7 @@ Handle Image::setGrayScaleToAlpha(const Arguments& args) } } - return Undefined(); + NanReturnUndefined(); } typedef struct { @@ -371,38 +384,44 @@ typedef struct { } image_op_baton_t; -Handle Image::premultiplySync(const Arguments& args) +NAN_METHOD(Image::premultiplySync) { - HandleScope scope; + NanScope(); + NanReturnValue(_premultiplySync(args)); +} + +Local Image::_premultiplySync(_NAN_METHOD_ARGS) { + NanEscapableScope(); #if MAPNIK_VERSION >= 200100 - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); im->get()->premultiply(); #endif - return Undefined(); + return NanEscapeScope(NanUndefined()); } -Handle Image::premultiply(const Arguments& args) +NAN_METHOD(Image::premultiply) { - HandleScope scope; + NanScope(); if (args.Length() == 0) { - return premultiplySync(args); + NanReturnValue(_premultiplySync(args)); } - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); // ensure callback is a function Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } image_op_baton_t *closure = new image_op_baton_t(); closure->request.data = closure; closure->im = im; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Premultiply, (uv_after_work_cb)EIO_AfterMultiply); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Premultiply(uv_work_t* req) @@ -422,59 +441,61 @@ void Image::EIO_Premultiply(uv_work_t* req) void Image::EIO_AfterMultiply(uv_work_t* req) { - HandleScope scope; + NanScope(); image_op_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->im->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->im) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->im->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Image::demultiplySync(const Arguments& args) +NAN_METHOD(Image::demultiplySync) { - HandleScope scope; + NanScope(); + NanReturnValue(_demultiplySync(args)); +} + +Local Image::_demultiplySync(_NAN_METHOD_ARGS) { + NanEscapableScope(); #if MAPNIK_VERSION >= 200100 - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); im->get()->demultiply(); #endif - return Undefined(); + return NanEscapeScope(NanUndefined()); } -Handle Image::demultiply(const Arguments& args) +NAN_METHOD(Image::demultiply) { - HandleScope scope; + NanScope(); if (args.Length() == 0) { - return demultiplySync(args); + NanReturnValue(_demultiplySync(args)); } - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); // ensure callback is a function Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } image_op_baton_t *closure = new image_op_baton_t(); closure->request.data = closure; closure->im = im; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Demultiply, (uv_after_work_cb)EIO_AfterMultiply); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Demultiply(uv_work_t* req) @@ -492,42 +513,48 @@ void Image::EIO_Demultiply(uv_work_t* req) } } -Handle Image::painted(const Arguments& args) +NAN_METHOD(Image::painted) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Boolean::New(im->get()->painted())); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->get()->painted())); } -Handle Image::width(const Arguments& args) +NAN_METHOD(Image::width) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->get()->width())); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->get()->width())); } -Handle Image::height(const Arguments& args) +NAN_METHOD(Image::height) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->get()->height())); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->get()->height())); } -Handle Image::openSync(const Arguments& args) +NAN_METHOD(Image::openSync) { - HandleScope scope; + NanScope(); + NanReturnValue(_openSync(args)); +} + +Local Image::_openSync(_NAN_METHOD_ARGS) +{ + NanEscapableScope(); if (args.Length() < 1) { - return ThrowException(Exception::TypeError( - String::New("must provide a string argument"))); + NanThrowError("must provide a string argument"); + return NanEscapeScope(NanUndefined()); } if (!args[0]->IsString()) { - return ThrowException(Exception::TypeError(String::New( - "Argument must be a string"))); + NanThrowTypeError("Argument must be a string"); + return NanEscapeScope(NanUndefined()); } try @@ -542,20 +569,20 @@ Handle Image::openSync(const Arguments& args) MAPNIK_SHARED_PTR image_ptr(new mapnik::image_32(reader->width(),reader->height())); reader->read(0,0,image_ptr->data()); Image* im = new Image(image_ptr); - Handle ext = External::New(im); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(im); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } - return ThrowException(Exception::TypeError(String::New( - ("Failed to load: " + filename).c_str()))); + NanThrowTypeError(("Failed to load: " + filename).c_str()); + return NanEscapeScope(NanUndefined()); } - return ThrowException(Exception::TypeError(String::New( - ("Unsupported image format:" + filename).c_str()))); + NanThrowTypeError(("Unsupported image format:" + filename).c_str()); + return NanEscapeScope(NanUndefined()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + return NanEscapeScope(NanUndefined()); } } @@ -578,37 +605,38 @@ typedef struct { Persistent cb; } image_file_ptr_baton_t; -Handle Image::open(const Arguments& args) +NAN_METHOD(Image::open) { - HandleScope scope; + NanScope(); if (args.Length() == 1) { - return openSync(args); + NanReturnValue(_openSync(args)); } if (args.Length() < 2) { - return ThrowException(Exception::TypeError( - String::New("must provide a string argument"))); + NanThrowError("must provide a string argument"); + NanReturnUndefined(); } if (!args[0]->IsString()) { - return ThrowException(Exception::TypeError(String::New( - "Argument must be a string"))); + NanThrowTypeError("Argument must be a string"); + NanReturnUndefined(); } // ensure callback is a function Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } image_file_ptr_baton_t *closure = new image_file_ptr_baton_t(); closure->request.data = closure; closure->filename = TOSTR(args[0]); closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Open, (uv_after_work_cb)EIO_AfterOpen); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Open(uv_work_t* req) @@ -647,44 +675,48 @@ void Image::EIO_Open(uv_work_t* req) void Image::EIO_AfterOpen(uv_work_t* req) { - HandleScope scope; + NanScope(); image_file_ptr_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error || !closure->im) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { Image* im = new Image(closure->im); - Handle ext = External::New(im); - Local image_obj = constructor->GetFunction()->NewInstance(1, &ext); - Local argv[2] = { Local::New(Null()), Local::New(ObjectWrap::Unwrap(image_obj)->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Handle ext = NanNew(im); + Local image_obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + Local argv[2] = { NanNull(), NanObjectWrapHandle(ObjectWrap::Unwrap(image_obj)) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Image::fromBytesSync(const Arguments& args) +NAN_METHOD(Image::fromBytesSync) +{ + NanScope(); + NanReturnValue(_fromBytesSync(args)); +} + +Local Image::_fromBytesSync(_NAN_METHOD_ARGS) { - HandleScope scope; + NanEscapableScope(); if (args.Length() < 1 || !args[0]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("must provide a buffer argument"))); + NanThrowTypeError("must provide a buffer argument"); + return NanEscapeScope(NanUndefined()); } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined()) - return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a Buffer"))); + if (obj->IsNull() || obj->IsUndefined()) { + NanThrowTypeError("first argument is invalid, must be a Buffer"); + return NanEscapeScope(NanUndefined()); + } if (!node::Buffer::HasInstance(obj)) { - return ThrowException(Exception::TypeError(String::New( - "first argument must be a buffer"))); + NanThrowTypeError("first argument must be a buffer"); + return NanEscapeScope(NanUndefined()); } try @@ -695,58 +727,61 @@ Handle Image::fromBytesSync(const Arguments& args) MAPNIK_SHARED_PTR image_ptr(new mapnik::image_32(reader->width(),reader->height())); reader->read(0,0,image_ptr->data()); Image* im = new Image(image_ptr); - Handle ext = External::New(im); - return scope.Close(constructor->GetFunction()->NewInstance(1, &ext)); + Handle ext = NanNew(im); + return NanEscapeScope(NanNew(constructor)->GetFunction()->NewInstance(1, &ext)); } - return ThrowException(Exception::TypeError(String::New( - "Failed to load from buffer"))); + NanThrowTypeError("Failed to load from buffer"); + return NanEscapeScope(NanUndefined()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + return NanEscapeScope(NanUndefined()); } } -Handle Image::fromBytes(const Arguments& args) +NAN_METHOD(Image::fromBytes) { - HandleScope scope; + NanScope(); if (args.Length() == 1) { - return fromBytesSync(args); + NanReturnValue(_fromBytesSync(args)); } if (args.Length() < 2) { - return ThrowException(Exception::TypeError( - String::New("must provide a buffer argument"))); + NanThrowError("must provide a buffer argument"); + NanReturnUndefined(); } if (!args[0]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("must provide a buffer argument"))); + NanThrowTypeError("must provide a buffer argument"); + NanReturnUndefined(); } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined()) - return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a Buffer"))); + if (obj->IsNull() || obj->IsUndefined()) { + NanThrowTypeError("first argument is invalid, must be a Buffer"); + NanReturnUndefined(); + } if (!node::Buffer::HasInstance(obj)) { - return ThrowException(Exception::TypeError(String::New( - "first argument must be a buffer"))); + NanThrowTypeError("first argument must be a buffer"); + NanReturnUndefined(); } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } image_mem_ptr_baton_t *closure = new image_mem_ptr_baton_t(); closure->request.data = closure; closure->data = node::Buffer::Data(obj); closure->dataLength = node::Buffer::Length(obj); closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_FromBytes, (uv_after_work_cb)EIO_AfterFromBytes); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_FromBytes(uv_work_t* req) @@ -776,63 +811,64 @@ void Image::EIO_FromBytes(uv_work_t* req) void Image::EIO_AfterFromBytes(uv_work_t* req) { - HandleScope scope; + NanScope(); image_mem_ptr_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error || !closure->im) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { Image* im = new Image(closure->im); - Handle ext = External::New(im); - Local image_obj = constructor->GetFunction()->NewInstance(1, &ext); - Local argv[2] = { Local::New(Null()), Local::New(ObjectWrap::Unwrap(image_obj)->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Handle ext = NanNew(im); + Local image_obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + Local argv[2] = { NanNull(), NanObjectWrapHandle(ObjectWrap::Unwrap(image_obj)) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Image::encodeSync(const Arguments& args) +NAN_METHOD(Image::encodeSync) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); std::string format = "png"; palette_ptr palette; // accept custom format if (args.Length() >= 1){ - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + if (!args[0]->IsString()) { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } Local options = args[1]->ToObject(); - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } } @@ -847,17 +883,12 @@ Handle Image::encodeSync(const Arguments& args) s = save_to_string(*(im->this_), format); } - // https://github.com/joyent/node/commit/3a2f273bd73bc94a6e93f342d629106a9f022f2d#src/node_buffer.h - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New((char*)s.data(),s.size())); - #else - return scope.Close(node::Buffer::New((char*)s.data(),s.size())->handle_); - #endif + NanReturnValue(NanNewBufferHandle((char*)s.data(), s.size())); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } @@ -872,51 +903,57 @@ typedef struct { std::string result; } encode_image_baton_t; -Handle Image::encode(const Arguments& args) +NAN_METHOD(Image::encode) { - HandleScope scope; + NanScope(); - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); std::string format = "png"; palette_ptr palette; // accept custom format if (args.Length() >= 1){ - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + if (!args[0]->IsString()) { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } - Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + Local obj = format_opt.As(); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } encode_image_baton_t *closure = new encode_image_baton_t(); closure->request.data = closure; @@ -924,11 +961,11 @@ Handle Image::encode(const Arguments& args) closure->format = format; closure->palette = palette; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Encode, (uv_after_work_cb)EIO_AfterEncode); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Encode(uv_work_t* req) @@ -954,42 +991,33 @@ void Image::EIO_Encode(uv_work_t* req) void Image::EIO_AfterEncode(uv_work_t* req) { - HandleScope scope; + NanScope(); encode_image_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - #if NODE_VERSION_AT_LEAST(0, 11, 0) - Local argv[2] = { Local::New(Null()), Local::New(node::Buffer::New((char*)closure->result.data(),closure->result.size())) }; - #else - Local argv[2] = { Local::New(Null()), Local::New(node::Buffer::New((char*)closure->result.data(),closure->result.size())->handle_) }; - #endif - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanNewBufferHandle((char*)closure->result.data(), closure->result.size()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->im->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Image::view(const Arguments& args) +NAN_METHOD(Image::view) { - HandleScope scope; + NanScope(); - if ( (args.Length() != 4) || (!args[0]->IsNumber() && !args[1]->IsNumber() && !args[2]->IsNumber() && !args[3]->IsNumber() )) - return ThrowException(Exception::TypeError( - String::New("requires 4 integer arguments: x, y, width, height"))); + if ( (args.Length() != 4) || (!args[0]->IsNumber() && !args[1]->IsNumber() && !args[2]->IsNumber() && !args[3]->IsNumber() )) { + NanThrowTypeError("requires 4 integer arguments: x, y, width, height"); + NanReturnUndefined(); + } // TODO parse args unsigned x = args[0]->IntegerValue(); @@ -997,17 +1025,17 @@ Handle Image::view(const Arguments& args) unsigned w = args[2]->IntegerValue(); unsigned h = args[3]->IntegerValue(); - Image* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(ImageView::New(im,x,y,w,h)); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(ImageView::New(im,x,y,w,h)); } -Handle Image::save(const Arguments& args) +NAN_METHOD(Image::save) { - HandleScope scope; + NanScope(); if (args.Length() == 0 || !args[0]->IsString()){ - return ThrowException(Exception::TypeError( - String::New("filename required"))); + NanThrowTypeError("filename required"); + NanReturnUndefined(); } std::string filename = TOSTR(args[0]); @@ -1015,9 +1043,10 @@ Handle Image::save(const Arguments& args) std::string format(""); if (args.Length() >= 2) { - if (!args[1]->IsString()) - return ThrowException(Exception::TypeError( - String::New("both 'filename' and 'format' arguments must be strings"))); + if (!args[1]->IsString()) { + NanThrowTypeError("both 'filename' and 'format' arguments must be strings"); + NanReturnUndefined(); + } format = TOSTR(args[1]); } else @@ -1026,22 +1055,22 @@ Handle Image::save(const Arguments& args) if (format == "") { std::ostringstream s(""); s << "unknown output extension for: " << filename << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } } - Image* im = node::ObjectWrap::Unwrap(args.This()); + Image* im = node::ObjectWrap::Unwrap(args.Holder()); try { mapnik::save_to_file(im->get()->data(),filename, format); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } #if MAPNIK_VERSION >= 200100 @@ -1060,29 +1089,34 @@ typedef struct { Persistent cb; } composite_image_baton_t; -Handle Image::composite(const Arguments& args) +NAN_METHOD(Image::composite) { - HandleScope scope; + NanScope(); if (args.Length() < 1){ - return ThrowException(Exception::TypeError( - String::New("requires at least one argument: an image mask"))); + NanThrowTypeError("requires at least one argument: an image mask"); + NanReturnUndefined(); } if (!args[0]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("first argument must be an image mask"))); + NanThrowTypeError("first argument must be an image mask"); + NanReturnUndefined(); } - Local im2 = args[0]->ToObject(); - if (im2->IsNull() || im2->IsUndefined() || !Image::constructor->HasInstance(im2)) - return ThrowException(Exception::TypeError(String::New("mapnik.Image expected as first arg"))); + Local im2 = args[0].As(); + if (im2->IsNull() || im2->IsUndefined() || !NanNew(Image::constructor)->HasInstance(im2)) + { + NanThrowTypeError("mapnik.Image expected as first arg"); + NanReturnUndefined(); + } // ensure callback is a function - Local callback = args[args.Length()-1]; + Local callback = args[args.Length() - 1]; if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } try { @@ -1093,71 +1127,73 @@ Handle Image::composite(const Arguments& args) int dy = 0; if (args.Length() >= 2) { if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("comp_op"))) + if (options->Has(NanNew("comp_op"))) { - Local opt = options->Get(String::New("comp_op")); + Local opt = options->Get(NanNew("comp_op")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("comp_op must be a mapnik.compositeOp value"))); + NanThrowTypeError("comp_op must be a mapnik.compositeOp value"); + NanReturnUndefined(); } mode = static_cast(opt->IntegerValue()); } - if (options->Has(String::New("opacity"))) + if (options->Has(NanNew("opacity"))) { - Local opt = options->Get(String::New("opacity")); + Local opt = options->Get(NanNew("opacity")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("opacity must be a floating point number"))); + NanThrowTypeError("opacity must be a floating point number"); + NanReturnUndefined(); } opacity = opt->NumberValue(); } - if (options->Has(String::New("dx"))) + if (options->Has(NanNew("dx"))) { - Local opt = options->Get(String::New("dx")); + Local opt = options->Get(NanNew("dx")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("dx must be an integer"))); + NanThrowTypeError("dx must be an integer"); + NanReturnUndefined(); } dx = opt->IntegerValue(); } - if (options->Has(String::New("dy"))) + if (options->Has(NanNew("dy"))) { - Local opt = options->Get(String::New("dy")); + Local opt = options->Get(NanNew("dy")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("dy must be an integer"))); + NanThrowTypeError("dy must be an integer"); + NanReturnUndefined(); } dy = opt->IntegerValue(); } - if (options->Has(String::New("image_filters"))) + if (options->Has(NanNew("image_filters"))) { - Local opt = options->Get(String::New("image_filters")); + Local opt = options->Get(NanNew("image_filters")); if (!opt->IsString()) { - return ThrowException(Exception::TypeError( - String::New("image_filters argument must string of filter names"))); + NanThrowTypeError("image_filters argument must string of filter names"); + NanReturnUndefined(); } std::string filter_str = TOSTR(opt); bool result = mapnik::filter::parse_image_filters(filter_str, filters); if (!result) { - return ThrowException(Exception::TypeError( - String::New("could not parse image_filters"))); + NanThrowTypeError("could not parse image_filters"); + NanReturnUndefined(); } } } composite_image_baton_t *closure = new composite_image_baton_t(); closure->request.data = closure; - closure->im1 = node::ObjectWrap::Unwrap(args.This()); + closure->im1 = node::ObjectWrap::Unwrap(args.Holder()); closure->im2 = node::ObjectWrap::Unwrap(im2); closure->mode = mode; closure->opacity = opacity; @@ -1165,16 +1201,17 @@ Handle Image::composite(const Arguments& args) closure->dx = dx; closure->dy = dy; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Composite, (uv_after_work_cb)EIO_AfterComposite); closure->im1->Ref(); closure->im2->Ref(); } catch (std::exception const& ex) { - return ThrowException(Exception::Error(String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } void Image::EIO_Composite(uv_work_t* req) @@ -1202,38 +1239,32 @@ void Image::EIO_Composite(uv_work_t* req) void Image::EIO_AfterComposite(uv_work_t* req) { - HandleScope scope; + NanScope(); composite_image_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->im1->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->im1) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->im1->Unref(); closure->im2->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } #else -Handle Image::composite(const Arguments& args) +NAN_METHOD(Image::composite) { - HandleScope scope; + NanScope(); - return ThrowException(Exception::TypeError( - String::New("compositing is only supported if node-mapnik is built against >= Mapnik 2.1.x"))); + NanThrowTypeError("compositing is only supported if node-mapnik is built against >= Mapnik 2.1.x"); + NanReturnUndefined(); } diff --git a/src/mapnik_image.hpp b/src/mapnik_image.hpp index 6da50877a5..6857617e9b 100644 --- a/src/mapnik_image.hpp +++ b/src/mapnik_image.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_IMAGE_H__ #define __NODE_MAPNIK_IMAGE_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" #include MAPNIK_SHARED_INCLUDE @@ -16,49 +15,51 @@ class Image: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle getPixel(const Arguments &args); - static Handle setPixel(const Arguments &args); - static Handle encodeSync(const Arguments &args); - static Handle encode(const Arguments &args); + static NAN_METHOD(getPixel); + static NAN_METHOD(setPixel); + static NAN_METHOD(encodeSync); + static NAN_METHOD(encode); static void EIO_Encode(uv_work_t* req); static void EIO_AfterEncode(uv_work_t* req); - static Handle setGrayScaleToAlpha(const Arguments &args); - static Handle width(const Arguments &args); - static Handle height(const Arguments &args); - static Handle view(const Arguments &args); - static Handle openSync(const Arguments &args); - static Handle open(const Arguments &args); + static NAN_METHOD(setGrayScaleToAlpha); + static NAN_METHOD(width); + static NAN_METHOD(height); + static NAN_METHOD(view); + static Local _openSync(_NAN_METHOD_ARGS); + static NAN_METHOD(openSync); + static NAN_METHOD(open); static void EIO_Open(uv_work_t* req); static void EIO_AfterOpen(uv_work_t* req); - static Handle fromBytesSync(const Arguments &args); - static Handle fromBytes(const Arguments &args); + static Local _fromBytesSync(_NAN_METHOD_ARGS); + static NAN_METHOD(fromBytesSync); + static NAN_METHOD(fromBytes); static void EIO_FromBytes(uv_work_t* req); static void EIO_AfterFromBytes(uv_work_t* req); - static Handle save(const Arguments &args); - static Handle painted(const Arguments &args); - static Handle composite(const Arguments &args); - static Handle premultiplySync(const Arguments& args); - static Handle premultiply(const Arguments& args); + static NAN_METHOD(save); + static NAN_METHOD(painted); + static NAN_METHOD(composite); + static Local _premultiplySync(_NAN_METHOD_ARGS); + static NAN_METHOD(premultiplySync); + static NAN_METHOD(premultiply); static void EIO_Premultiply(uv_work_t* req); - static Handle demultiplySync(const Arguments& args); - static Handle demultiply(const Arguments& args); + static Local _demultiplySync(_NAN_METHOD_ARGS); + static NAN_METHOD(demultiplySync); + static NAN_METHOD(demultiply); static void EIO_Demultiply(uv_work_t* req); static void EIO_AfterMultiply(uv_work_t* req); - static Handle clearSync(const Arguments& args); - static Handle clear(const Arguments& args); + static Local _clearSync(_NAN_METHOD_ARGS); + static NAN_METHOD(clearSync); + static NAN_METHOD(clear); static void EIO_Clear(uv_work_t* req); static void EIO_AfterClear(uv_work_t* req); static void EIO_Composite(uv_work_t* req); static void EIO_AfterComposite(uv_work_t* req); - static Handle get_prop(Local property, - const AccessorInfo& info); - static void set_prop(Local property, - Local value, - const AccessorInfo& info); + static NAN_GETTER(get_prop); + static NAN_SETTER(set_prop); void _ref() { Ref(); } void _unref() { Unref(); } diff --git a/src/mapnik_image_view.cpp b/src/mapnik_image_view.cpp index 05ee0f91a6..da5b0575dd 100644 --- a/src/mapnik_image_view.cpp +++ b/src/mapnik_image_view.cpp @@ -1,12 +1,4 @@ -// node -#include // for NODE_SET_PROTOTYPE_METHOD, etc -#include // for ObjectWrap -#include -#include -#include -#include - // mapnik #include // for color #include // for image_view, etc @@ -29,22 +21,23 @@ Persistent ImageView::constructor; void ImageView::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(ImageView::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("ImageView")); + Local lcons = NanNew(ImageView::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("ImageView")); - NODE_SET_PROTOTYPE_METHOD(constructor, "encodeSync", encodeSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode); - NODE_SET_PROTOTYPE_METHOD(constructor, "save", save); - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolid", isSolid); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolidSync", isSolidSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel); + NODE_SET_PROTOTYPE_METHOD(lcons, "encodeSync", encodeSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "encode", encode); + NODE_SET_PROTOTYPE_METHOD(lcons, "save", save); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolid", isSolid); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolidSync", isSolidSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "getPixel", getPixel); - target->Set(String::NewSymbol("ImageView"),constructor->GetFunction()); + target->Set(NanNew("ImageView"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } @@ -60,24 +53,28 @@ ImageView::~ImageView() JSImage_->_unref(); } -Handle ImageView::New(const Arguments& args) +NAN_METHOD(ImageView::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { //std::clog << "image view external!\n"; - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); ImageView* im = static_cast(ptr); im->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(String::New("Cannot create this object from Javascript")); + NanThrowError("Cannot create this object from Javascript"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } Handle ImageView::New(Image * JSImage , @@ -87,12 +84,12 @@ Handle ImageView::New(Image * JSImage , unsigned h ) { - HandleScope scope; + NanEscapableScope(); ImageView* imv = new ImageView(JSImage); imv->this_ = MAPNIK_MAKE_SHARED >(JSImage->get()->get_view(x,y,w,h)); - Handle ext = External::New(imv); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(imv); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } @@ -106,19 +103,20 @@ typedef struct { mapnik::image_view::pixel_type pixel; } is_solid_image_view_baton_t; -Handle ImageView::isSolid(const Arguments& args) +NAN_METHOD(ImageView::isSolid) { - HandleScope scope; - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { - return isSolidSync(args); + NanReturnValue(_isSolidSync(args)); } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } is_solid_image_view_baton_t *closure = new is_solid_image_view_baton_t(); closure->request.data = closure; @@ -126,10 +124,10 @@ Handle ImageView::isSolid(const Arguments& args) closure->result = true; closure->pixel = 0; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_IsSolid, (uv_after_work_cb)EIO_AfterIsSolid); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void ImageView::EIO_IsSolid(uv_work_t* req) @@ -163,45 +161,44 @@ void ImageView::EIO_IsSolid(uv_work_t* req) void ImageView::EIO_AfterIsSolid(uv_work_t* req) { - HandleScope scope; + NanScope(); is_solid_image_view_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { if (closure->result) { - Local argv[3] = { Local::New(Null()), - Local::New(Boolean::New(closure->result)), - Local::New(Number::New(closure->pixel)), + Local argv[3] = { NanNull(), + NanNew(closure->result), + NanNew(closure->pixel), }; - closure->cb->Call(Context::GetCurrent()->Global(), 3, argv); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 3, argv); } else { - Local argv[2] = { Local::New(Null()), - Local::New(Boolean::New(closure->result)) - }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), NanNew(closure->result) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); - } closure->im->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle ImageView::isSolidSync(const Arguments& args) +NAN_METHOD(ImageView::isSolidSync) { - HandleScope scope; - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + NanReturnValue(_isSolidSync(args)); +} + +Local ImageView::_isSolidSync(_NAN_METHOD_ARGS) +{ + NanEscapableScope(); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); image_view_ptr view = im->get(); if (view->width() > 0 && view->height() > 0) { @@ -214,37 +211,39 @@ Handle ImageView::isSolidSync(const Arguments& args) { if (first_pixel != row[x]) { - return scope.Close(False()); + return NanEscapeScope(NanFalse()); } } } } - return scope.Close(True()); + return NanEscapeScope(NanTrue()); } -Handle ImageView::getPixel(const Arguments& args) +NAN_METHOD(ImageView::getPixel) { - HandleScope scope; + NanScope(); int x = 0; int y = 0; if (args.Length() >= 2) { - if (!args[0]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'x' must be an integer"))); - if (!args[1]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("second arg, 'y' must be an integer"))); + if (!args[0]->IsNumber()) { + NanThrowTypeError("first arg, 'x' must be an integer"); + NanReturnUndefined(); + } + if (!args[1]->IsNumber()) { + NanThrowTypeError("second arg, 'y' must be an integer"); + NanReturnUndefined(); + } x = args[0]->IntegerValue(); y = args[1]->IntegerValue(); } else { - return ThrowException(Exception::TypeError( - String::New("must supply x,y to query pixel color"))); + NanThrowTypeError("must supply x,y to query pixel color"); + NanReturnUndefined(); } - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); image_view_ptr view = im->get(); if (x >= 0 && x < static_cast(view->width()) && y >=0 && y < static_cast(view->height())) @@ -255,64 +254,69 @@ Handle ImageView::getPixel(const Arguments& args) unsigned g = (pixel >> 8) & 0xff; unsigned b = (pixel >> 16) & 0xff; unsigned a = (pixel >> 24) & 0xff; - return Color::New(mapnik::color(r,g,b,a)); + NanReturnValue(Color::New(mapnik::color(r,g,b,a))); } - return Undefined(); + NanReturnUndefined(); } -Handle ImageView::width(const Arguments& args) +NAN_METHOD(ImageView::width) { - HandleScope scope; + NanScope(); - ImageView* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->get()->width())); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->get()->width())); } -Handle ImageView::height(const Arguments& args) +NAN_METHOD(ImageView::height) { - HandleScope scope; + NanScope(); - ImageView* im = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(im->get()->height())); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(im->get()->height())); } -Handle ImageView::encodeSync(const Arguments& args) +NAN_METHOD(ImageView::encodeSync) { - HandleScope scope; + NanScope(); - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); std::string format = "png"; palette_ptr palette; // accept custom format if (args.Length() >= 1) { - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + if (!args[0]->IsString()) { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } - Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + Local obj = format_opt.As(); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } @@ -329,16 +333,12 @@ Handle ImageView::encodeSync(const Arguments& args) s = save_to_string(image, format); } - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New((char*)s.data(),s.size())); - #else - return scope.Close(node::Buffer::New((char*)s.data(),s.size())->handle_); - #endif + NanReturnValue(NanNewBufferHandle((char*)s.data(),s.size())); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } @@ -354,51 +354,57 @@ typedef struct { } encode_image_view_baton_t; -Handle ImageView::encode(const Arguments& args) +NAN_METHOD(ImageView::encode) { - HandleScope scope; + NanScope(); - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); std::string format = "png"; palette_ptr palette; // accept custom format if (args.Length() > 1){ - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first arg, 'format' must be a string"))); + if (!args[0]->IsString()) { + NanThrowTypeError("first arg, 'format' must be a string"); + NanReturnUndefined(); + } format = TOSTR(args[0]); } // options hash if (args.Length() >= 2) { - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } - Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + Local obj = format_opt.As(); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } encode_image_view_baton_t *closure = new encode_image_view_baton_t(); closure->request.data = closure; @@ -406,10 +412,10 @@ Handle ImageView::encode(const Arguments& args) closure->format = format; closure->palette = palette; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Encode, (uv_after_work_cb)EIO_AfterEncode); im->Ref(); - return Undefined(); + NanReturnUndefined(); } void ImageView::EIO_Encode(uv_work_t* req) @@ -436,43 +442,33 @@ void ImageView::EIO_Encode(uv_work_t* req) void ImageView::EIO_AfterEncode(uv_work_t* req) { - HandleScope scope; + NanScope(); encode_image_view_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - #if NODE_VERSION_AT_LEAST(0, 11, 0) - Local argv[2] = { Local::New(Null()), Local::New(node::Buffer::New((char*)closure->result.data(),closure->result.size())) }; - #else - Local argv[2] = { Local::New(Null()), Local::New(node::Buffer::New((char*)closure->result.data(),closure->result.size())->handle_) }; - #endif - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanNewBufferHandle((char*)closure->result.data(), closure->result.size()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->im->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle ImageView::save(const Arguments& args) +NAN_METHOD(ImageView::save) { - HandleScope scope; + NanScope(); if (args.Length() == 0 || !args[0]->IsString()){ - return ThrowException(Exception::TypeError( - String::New("filename required"))); + NanThrowTypeError("filename required"); + NanReturnUndefined(); } std::string filename = TOSTR(args[0]); @@ -480,30 +476,31 @@ Handle ImageView::save(const Arguments& args) std::string format(""); if (args.Length() >= 2) { - if (!args[1]->IsString()) - return ThrowException(Exception::TypeError( - String::New("both 'filename' and 'format' arguments must be strings"))); + if (!args[1]->IsString()) { + NanThrowTypeError("both 'filename' and 'format' arguments must be strings"); + NanReturnUndefined(); + } format = mapnik::guess_type(TOSTR(args[1])); if (format == "") { std::ostringstream s(""); s << "unknown output extension for: " << filename << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } } - ImageView* im = node::ObjectWrap::Unwrap(args.This()); + ImageView* im = node::ObjectWrap::Unwrap(args.Holder()); try { save_to_file(*im->get(),filename); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } diff --git a/src/mapnik_image_view.hpp b/src/mapnik_image_view.hpp index c2e6d6087f..9d9fbdc7d6 100644 --- a/src/mapnik_image_view.hpp +++ b/src/mapnik_image_view.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_IMAGE_VIEW_H__ #define __NODE_MAPNIK_IMAGE_VIEW_H__ -#include -#include +#include #include #include // for image_data_32 #include "mapnik3x_compatibility.hpp" @@ -17,25 +16,26 @@ class ImageView: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(Image * JSImage, unsigned x,unsigned y, unsigned w, unsigned h); - static Handle encodeSync(const Arguments &args); - static Handle encode(const Arguments &args); + static NAN_METHOD(encodeSync); + static NAN_METHOD(encode); static void EIO_Encode(uv_work_t* req); static void EIO_AfterEncode(uv_work_t* req); - //static Handle view(const Arguments &args); - static Handle width(const Arguments &args); - static Handle height(const Arguments &args); - //static Handle open(const Arguments &args); - static Handle save(const Arguments &args); - static Handle isSolid(const Arguments &args); + //static NAN_METHOD(view); + static NAN_METHOD(width); + static NAN_METHOD(height); + //static NAN_METHOD(open); + static NAN_METHOD(save); + static NAN_METHOD(isSolid); static void EIO_IsSolid(uv_work_t* req); static void EIO_AfterIsSolid(uv_work_t* req); - static Handle isSolidSync(const Arguments &args); - static Handle getPixel(const Arguments &args); + static Local _isSolidSync(_NAN_METHOD_ARGS); + static NAN_METHOD(isSolidSync); + static NAN_METHOD(getPixel); ImageView(Image * JSImage); inline image_view_ptr get() { return this_; } diff --git a/src/mapnik_layer.cpp b/src/mapnik_layer.cpp index 364111671a..73279a9f68 100644 --- a/src/mapnik_layer.cpp +++ b/src/mapnik_layer.cpp @@ -1,9 +1,6 @@ #include "mapnik_layer.hpp" -#include "node.h" // for NODE_SET_PROTOTYPE_METHOD -#include "node_object_wrap.h" // for ObjectWrap #include "utils.hpp" // for TOSTR, ATTR, etc -#include "v8.h" // for String, Handle, Object, etc #include "mapnik_datasource.hpp" #include "mapnik_memory_datasource.hpp" @@ -33,22 +30,23 @@ Persistent Layer::constructor; void Layer::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Layer::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Layer")); + Local lcons = NanNew(Layer::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Layer")); // methods - NODE_SET_PROTOTYPE_METHOD(constructor, "describe", describe); + NODE_SET_PROTOTYPE_METHOD(lcons, "describe", describe); // properties - ATTR(constructor, "name", get_prop, set_prop); - ATTR(constructor, "srs", get_prop, set_prop); - ATTR(constructor, "styles", get_prop, set_prop); - ATTR(constructor, "datasource", get_prop, set_prop); + ATTR(lcons, "name", get_prop, set_prop); + ATTR(lcons, "srs", get_prop, set_prop); + ATTR(lcons, "styles", get_prop, set_prop); + ATTR(lcons, "datasource", get_prop, set_prop); - target->Set(String::NewSymbol("Layer"),constructor->GetFunction()); + target->Set(NanNew("Layer"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Layer::Layer(std::string const& name): @@ -66,101 +64,101 @@ Layer::Layer(): Layer::~Layer() {} -Handle Layer::New(const Arguments& args) +NAN_METHOD(Layer::New) { - HandleScope scope; + NanScope(); - if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + if (!args.IsConstructCall()) { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); + Local ext = args[0].As(); void* ptr = ext->Value(); Layer* l = static_cast(ptr); l->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } if (args.Length() == 1) { if (!args[0]->IsString()) { - return ThrowException(Exception::TypeError( - String::New("'name' must be a string"))); + NanThrowTypeError("'name' must be a string"); + NanReturnUndefined(); } Layer* l = new Layer(TOSTR(args[0])); l->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else if (args.Length() == 2) { - if (!args[0]->IsString() || !args[1]->IsString()) - return ThrowException(Exception::TypeError( - String::New("'name' and 'srs' must be a strings"))); + if (!args[0]->IsString() || !args[1]->IsString()) { + NanThrowTypeError("'name' and 'srs' must be a strings"); + NanReturnUndefined(); + } Layer* l = new Layer(TOSTR(args[0]),TOSTR(args[1])); l->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::TypeError( - String::New("please provide Layer name and optional srs"))); + NanThrowTypeError("please provide Layer name and optional srs"); + NanReturnUndefined(); } - return args.This(); + NanReturnValue(args.This()); } Handle Layer::New(mapnik::layer const& lay_ref) { - HandleScope scope; + NanEscapableScope(); Layer* l = new Layer(); // copy new mapnik::layer into the shared_ptr l->layer_ = MAPNIK_MAKE_SHARED(lay_ref); - Handle ext = External::New(l); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(l); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle Layer::get_prop(Local property, - const AccessorInfo& info) +NAN_GETTER(Layer::get_prop) { - HandleScope scope; - Layer* l = node::ObjectWrap::Unwrap(info.This()); + NanScope(); + Layer* l = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "name") - return scope.Close(String::New(l->layer_->name().c_str())); + NanReturnValue(NanNew(l->layer_->name().c_str())); else if (a == "srs") - return scope.Close(String::New(l->layer_->srs().c_str())); + NanReturnValue(NanNew(l->layer_->srs().c_str())); else if (a == "styles") { std::vector const& style_names = l->layer_->styles(); - Local s = Array::New(style_names.size()); + Local s = NanNew(style_names.size()); for (unsigned i = 0; i < style_names.size(); ++i) { - s->Set(i, String::New(style_names[i].c_str()) ); + s->Set(i, NanNew(style_names[i].c_str()) ); } - return scope.Close(s); + NanReturnValue(s); } else if (a == "datasource") { mapnik::datasource_ptr ds = l->layer_->datasource(); if (ds) { - return scope.Close(Datasource::New(ds)); + NanReturnValue(Datasource::New(ds)); } } - return Undefined(); + NanReturnUndefined(); } -void Layer::set_prop(Local property, - Local value, - const AccessorInfo& info) +NAN_SETTER(Layer::set_prop) { - HandleScope scope; - Layer* l = node::ObjectWrap::Unwrap(info.This()); + NanScope(); + Layer* l = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if (a == "name") { if (!value->IsString()) { - ThrowException(Exception::Error( - String::New("'name' must be a string"))); + NanThrowTypeError("'name' must be a string"); + return; } else { l->layer_->set_name(TOSTR(value)); } @@ -168,19 +166,19 @@ void Layer::set_prop(Local property, else if (a == "srs") { if (!value->IsString()) { - ThrowException(Exception::Error( - String::New("'srs' must be a string"))); + NanThrowTypeError("'srs' must be a string"); + return; } else { l->layer_->set_srs(TOSTR(value)); } } else if (a == "styles") { - if (!value->IsArray()) - ThrowException(Exception::Error( - String::New("Must provide an array of style names"))); - else { - Local arr = Local::Cast(value->ToObject()); + if (!value->IsArray()) { + NanThrowTypeError("Must provide an array of style names"); + return; + } else { + Local arr = value.As(); // todo - how to check if cast worked? unsigned int i = 0; unsigned int a_length = arr->Length(); @@ -192,87 +190,89 @@ void Layer::set_prop(Local property, } else if (a == "datasource") { - Local obj = value->ToObject(); + Local obj = value.As(); if (value->IsNull() || value->IsUndefined()) { - ThrowException(Exception::TypeError(String::New("mapnik.Datasource, or mapnik.MemoryDatasource instance expected"))); + NanThrowTypeError("mapnik.Datasource, or mapnik.MemoryDatasource instance expected"); + return; } else { - if (Datasource::constructor->HasInstance(obj)) { + if (NanNew(Datasource::constructor)->HasInstance(obj)) { Datasource *d = node::ObjectWrap::Unwrap(obj); l->layer_->set_datasource(d->get()); } - /*else if (JSDatasource::constructor->HasInstance(obj)) + /*else if (NanNew(JSDatasource::constructor)->HasInstance(obj)) { JSDatasource *d = node::ObjectWrap::Unwrap(obj); l->layer_->set_datasource(d->get()); }*/ - else if (MemoryDatasource::constructor->HasInstance(obj)) + else if (NanNew(MemoryDatasource::constructor)->HasInstance(obj)) { MemoryDatasource *d = node::ObjectWrap::Unwrap(obj); l->layer_->set_datasource(d->get()); } else { - ThrowException(Exception::TypeError(String::New("mapnik.Datasource, mapnik.JSDatasource, or mapnik.MemoryDatasource instance expected"))); + NanThrowTypeError("mapnik.Datasource, mapnik.JSDatasource, or mapnik.MemoryDatasource instance expected"); + return; } } } } -Handle Layer::describe(const Arguments& args) +NAN_METHOD(Layer::describe) { - HandleScope scope; + NanScope(); - Layer* l = node::ObjectWrap::Unwrap(args.This()); + Layer* l = node::ObjectWrap::Unwrap(args.Holder()); - Local description = Object::New(); + Local description = NanNew(); mapnik::layer const& layer = *l->layer_; if ( layer.name() != "" ) { - description->Set(String::NewSymbol("name"), String::New(layer.name().c_str())); + description->Set(NanNew("name"), NanNew(layer.name().c_str())); } if ( layer.srs() != "" ) { - description->Set(String::NewSymbol("srs"), String::New(layer.srs().c_str())); + description->Set(NanNew("srs"), NanNew(layer.srs().c_str())); } if ( !layer.active()) { - description->Set(String::NewSymbol("status"), Boolean::New(layer.active())); + description->Set(NanNew("status"), NanNew(layer.active())); } if ( layer.clear_label_cache()) { - description->Set(String::NewSymbol("clear_label_cache"), Boolean::New(layer.clear_label_cache())); + description->Set(NanNew("clear_label_cache"), NanNew(layer.clear_label_cache())); } if ( layer.min_zoom() > 0) { - description->Set(String::NewSymbol("minzoom"), Number::New(layer.min_zoom())); + description->Set(NanNew("minzoom"), NanNew(layer.min_zoom())); } if ( layer.max_zoom() != std::numeric_limits::max() ) { - description->Set(String::NewSymbol("maxzoom"), Number::New(layer.max_zoom())); + description->Set(NanNew("maxzoom"), NanNew(layer.max_zoom())); } if ( layer.queryable()) { - description->Set(String::NewSymbol("queryable"), Boolean::New(layer.queryable())); + description->Set(NanNew("queryable"), NanNew(layer.queryable())); } std::vector const& style_names = layer.styles(); - Local s = Array::New(style_names.size()); + Local s = NanNew(style_names.size()); for (unsigned i = 0; i < style_names.size(); ++i) { - s->Set(i, String::New(style_names[i].c_str()) ); + s->Set(i, NanNew(style_names[i].c_str()) ); } - description->Set(String::NewSymbol("styles"), s ); + description->Set(NanNew("styles"), s ); mapnik::datasource_ptr datasource = layer.datasource(); - Local ds = Object::New(); - description->Set(String::NewSymbol("datasource"), ds ); + Local ds = NanNew(); + description->Set(NanNew("datasource"), ds ); if ( datasource ) { mapnik::parameters::const_iterator it = datasource->params().begin(); @@ -284,7 +284,7 @@ Handle Layer::describe(const Arguments& args) } } - return scope.Close(description); + NanReturnValue(description); } #if MAPNIK_VERSION <= 200000 diff --git a/src/mapnik_layer.hpp b/src/mapnik_layer.hpp index e202b2d7ac..4af932d500 100644 --- a/src/mapnik_layer.hpp +++ b/src/mapnik_layer.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_LAYER_H__ #define __NODE_MAPNIK_LAYER_H__ -#include // for ObjectWrap -#include // for Handle, AccessorInfo, etc +#include #include "mapnik3x_compatibility.hpp" // stl #include @@ -19,17 +18,13 @@ class Layer: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(mapnik::layer const& lay_ref); - static Handle describe(const Arguments &args); - - static Handle get_prop(Local property, - const AccessorInfo& info); - static void set_prop(Local property, - Local value, - const AccessorInfo& info); + static NAN_METHOD(describe); + static NAN_GETTER(get_prop); + static NAN_SETTER(set_prop); Layer(std::string const& name); Layer(std::string const& name, std::string const& srs); diff --git a/src/mapnik_map.cpp b/src/mapnik_map.cpp index 1439801949..350a1991f0 100644 --- a/src/mapnik_map.cpp +++ b/src/mapnik_map.cpp @@ -1,4 +1,3 @@ - #include "mapnik_map.hpp" #include "utils.hpp" #include "mapnik_color.hpp" // for Color, Color::constructor @@ -11,11 +10,6 @@ #include "vector_tile_backend_pbf.hpp" #include "mapnik_vector_tile.hpp" -// node -#include -#include -#include - // mapnik #include // for agg_renderer #include // for box2d @@ -52,51 +46,52 @@ Persistent Map::constructor; void Map::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Map::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Map")); + Local lcons = NanNew(Map::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Map")); - NODE_SET_PROTOTYPE_METHOD(constructor, "load", load); - NODE_SET_PROTOTYPE_METHOD(constructor, "loadSync", loadSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "fromStringSync", fromStringSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "fromString", fromString); - NODE_SET_PROTOTYPE_METHOD(constructor, "save", save); - NODE_SET_PROTOTYPE_METHOD(constructor, "clear", clear); - NODE_SET_PROTOTYPE_METHOD(constructor, "toXML", to_string); - NODE_SET_PROTOTYPE_METHOD(constructor, "resize", resize); + NODE_SET_PROTOTYPE_METHOD(lcons, "load", load); + NODE_SET_PROTOTYPE_METHOD(lcons, "loadSync", loadSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "fromStringSync", fromStringSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "fromString", fromString); + NODE_SET_PROTOTYPE_METHOD(lcons, "save", save); + NODE_SET_PROTOTYPE_METHOD(lcons, "clear", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "toXML", to_string); + NODE_SET_PROTOTYPE_METHOD(lcons, "resize", resize); - NODE_SET_PROTOTYPE_METHOD(constructor, "render", render); - NODE_SET_PROTOTYPE_METHOD(constructor, "renderSync", renderSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "renderFile", renderFile); - NODE_SET_PROTOTYPE_METHOD(constructor, "renderFileSync", renderFileSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "render", render); + NODE_SET_PROTOTYPE_METHOD(lcons, "renderSync", renderSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "renderFile", renderFile); + NODE_SET_PROTOTYPE_METHOD(lcons, "renderFileSync", renderFileSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "zoomAll", zoomAll); - NODE_SET_PROTOTYPE_METHOD(constructor, "zoomToBox", zoomToBox); //setExtent - NODE_SET_PROTOTYPE_METHOD(constructor, "scale", scale); - NODE_SET_PROTOTYPE_METHOD(constructor, "scaleDenominator", scaleDenominator); - NODE_SET_PROTOTYPE_METHOD(constructor, "queryPoint", queryPoint); - NODE_SET_PROTOTYPE_METHOD(constructor, "queryMapPoint", queryMapPoint); + NODE_SET_PROTOTYPE_METHOD(lcons, "zoomAll", zoomAll); + NODE_SET_PROTOTYPE_METHOD(lcons, "zoomToBox", zoomToBox); //setExtent + NODE_SET_PROTOTYPE_METHOD(lcons, "scale", scale); + NODE_SET_PROTOTYPE_METHOD(lcons, "scaleDenominator", scaleDenominator); + NODE_SET_PROTOTYPE_METHOD(lcons, "queryPoint", queryPoint); + NODE_SET_PROTOTYPE_METHOD(lcons, "queryMapPoint", queryMapPoint); // layer access - NODE_SET_PROTOTYPE_METHOD(constructor, "add_layer", add_layer); - NODE_SET_PROTOTYPE_METHOD(constructor, "get_layer", get_layer); - NODE_SET_PROTOTYPE_METHOD(constructor, "layers", layers); + NODE_SET_PROTOTYPE_METHOD(lcons, "add_layer", add_layer); + NODE_SET_PROTOTYPE_METHOD(lcons, "get_layer", get_layer); + NODE_SET_PROTOTYPE_METHOD(lcons, "layers", layers); // properties - ATTR(constructor, "srs", get_prop, set_prop); - ATTR(constructor, "width", get_prop, set_prop); - ATTR(constructor, "height", get_prop, set_prop); - ATTR(constructor, "bufferSize", get_prop, set_prop); - ATTR(constructor, "extent", get_prop, set_prop); - ATTR(constructor, "bufferedExtent", get_prop, set_prop); - ATTR(constructor, "maximumExtent", get_prop, set_prop); - ATTR(constructor, "background", get_prop, set_prop); - ATTR(constructor, "parameters", get_prop, set_prop); - - target->Set(String::NewSymbol("Map"),constructor->GetFunction()); + ATTR(lcons, "srs", get_prop, set_prop); + ATTR(lcons, "width", get_prop, set_prop); + ATTR(lcons, "height", get_prop, set_prop); + ATTR(lcons, "bufferSize", get_prop, set_prop); + ATTR(lcons, "extent", get_prop, set_prop); + ATTR(lcons, "bufferedExtent", get_prop, set_prop); + ATTR(lcons, "maximumExtent", get_prop, set_prop); + ATTR(lcons, "background", get_prop, set_prop); + ATTR(lcons, "parameters", get_prop, set_prop); + + target->Set(NanNew("Map"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Map::Map(int width, int height) : @@ -123,102 +118,111 @@ int Map::active() const { return in_use_; } -Handle Map::New(const Arguments& args) +NAN_METHOD(Map::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } // accept a reference or v8:External? if (args[0]->IsExternal()) { - return ThrowException(String::New("No support yet for passing v8:External wrapper around C++ void*")); + NanThrowError("No support yet for passing v8:External wrapper around C++ void*"); + NanReturnUndefined(); } if (args.Length() == 2) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::Error( - String::New("'width' and 'height' must be a integers"))); + { + NanThrowTypeError("'width' and 'height' must be integers"); + NanReturnUndefined(); + } Map* m = new Map(args[0]->IntegerValue(),args[1]->IntegerValue()); m->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else if (args.Length() == 3) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::Error( - String::New("'width' and 'height' must be a integers"))); + { + NanThrowTypeError("'width' and 'height' must be integers"); + NanReturnUndefined(); + } if (!args[2]->IsString()) - return ThrowException(Exception::Error( - String::New("'srs' value must be a string"))); - Map* m = new Map(args[0]->IntegerValue(),args[1]->IntegerValue(),TOSTR(args[2])); + { + NanThrowError("'srs' value must be a string"); + NanReturnUndefined(); + } + Map* m = new Map(args[0]->IntegerValue(), args[1]->IntegerValue(), TOSTR(args[2])); m->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("please provide Map width and height and optional srs"))); + NanThrowError("please provide Map width and height and optional srs"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Map::get_prop(Local property, - const AccessorInfo& info) +NAN_GETTER(Map::get_prop) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(info.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if(a == "extent") { - Local arr = Array::New(4); + Local arr = NanNew(4); mapnik::box2d const& e = m->map_->get_current_extent(); - arr->Set(0, Number::New(e.minx())); - arr->Set(1, Number::New(e.miny())); - arr->Set(2, Number::New(e.maxx())); - arr->Set(3, Number::New(e.maxy())); - return scope.Close(arr); + arr->Set(0, NanNew(e.minx())); + arr->Set(1, NanNew(e.miny())); + arr->Set(2, NanNew(e.maxx())); + arr->Set(3, NanNew(e.maxy())); + NanReturnValue(arr); } else if(a == "bufferedExtent") { boost::optional > const& e = m->map_->get_buffered_extent(); if (!e) - return Undefined(); - Local arr = Array::New(4); - arr->Set(0, Number::New(e->minx())); - arr->Set(1, Number::New(e->miny())); - arr->Set(2, Number::New(e->maxx())); - arr->Set(3, Number::New(e->maxy())); - return scope.Close(arr); + NanReturnUndefined(); + Local arr = NanNew(4); + arr->Set(0, NanNew(e->minx())); + arr->Set(1, NanNew(e->miny())); + arr->Set(2, NanNew(e->maxx())); + arr->Set(3, NanNew(e->maxy())); + NanReturnValue(arr); } else if(a == "maximumExtent") { boost::optional > const& e = m->map_->maximum_extent(); if (!e) - return Undefined(); - Local arr = Array::New(4); - arr->Set(0, Number::New(e->minx())); - arr->Set(1, Number::New(e->miny())); - arr->Set(2, Number::New(e->maxx())); - arr->Set(3, Number::New(e->maxy())); - return scope.Close(arr); + NanReturnUndefined(); + Local arr = NanNew(4); + arr->Set(0, NanNew(e->minx())); + arr->Set(1, NanNew(e->miny())); + arr->Set(2, NanNew(e->maxx())); + arr->Set(3, NanNew(e->maxy())); + NanReturnValue(arr); } else if(a == "width") - return scope.Close(Integer::New(m->map_->width())); + NanReturnValue(NanNew(m->map_->width())); else if(a == "height") - return scope.Close(Integer::New(m->map_->height())); + NanReturnValue(NanNew(m->map_->height())); else if (a == "srs") - return scope.Close(String::New(m->map_->srs().c_str())); + NanReturnValue(NanNew(m->map_->srs().c_str())); else if(a == "bufferSize") - return scope.Close(Integer::New(m->map_->buffer_size())); + NanReturnValue(NanNew(m->map_->buffer_size())); else if (a == "background") { boost::optional c = m->map_->background(); if (c) - return scope.Close(Color::New(*c)); + NanReturnValue(Color::New(*c)); else - return Undefined(); + NanReturnUndefined(); } else if (a == "parameters") { - Local ds = Object::New(); + Local ds = NanNew(); #if MAPNIK_VERSION >= 200100 mapnik::parameters const& params = m->map_->get_extra_parameters(); mapnik::parameters::const_iterator it = params.begin(); @@ -229,27 +233,25 @@ Handle Map::get_prop(Local property, boost::apply_visitor( serializer, it->second ); } #endif - return scope.Close(ds); + NanReturnValue(ds); } - return Undefined(); + NanReturnUndefined(); } -void Map::set_prop(Local property, - Local value, - const AccessorInfo& info) +NAN_SETTER(Map::set_prop) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(info.Holder()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string a = TOSTR(property); if(a == "extent" || a == "maximumExtent") { if (!value->IsArray()) { - ThrowException(Exception::Error( - String::New("Must provide an array of: [minx,miny,maxx,maxy]"))); + NanThrowError("Must provide an array of: [minx,miny,maxx,maxy]"); + return; } else { - Local arr = Local::Cast(value); + Local arr = value.As(); if (arr->Length() != 4) { - ThrowException(Exception::Error( - String::New("Must provide an array of: [minx,miny,maxx,maxy]"))); + NanThrowError("Must provide an array of: [minx,miny,maxx,maxy]"); + return; } else { double minx = arr->Get(0)->NumberValue(); double miny = arr->Get(1)->NumberValue(); @@ -266,56 +268,62 @@ void Map::set_prop(Local property, else if (a == "srs") { if (!value->IsString()) { - ThrowException(Exception::Error( - String::New("'srs' must be a string"))); + NanThrowError("'srs' must be a string"); + return; } else { m->map_->set_srs(TOSTR(value)); } } else if (a == "bufferSize") { if (!value->IsNumber()) { - ThrowException(Exception::Error( - String::New("Must provide an integer bufferSize"))); + NanThrowTypeError("Must provide an integer bufferSize"); + return; } else { m->map_->set_buffer_size(value->IntegerValue()); } } else if (a == "width") { if (!value->IsNumber()) { - ThrowException(Exception::Error( - String::New("Must provide an integer width"))); + NanThrowTypeError("Must provide an integer width"); + return; } else { m->map_->set_width(value->IntegerValue()); } } else if (a == "height") { if (!value->IsNumber()) { - ThrowException(Exception::Error( - String::New("Must provide an integer height"))); + NanThrowTypeError("Must provide an integer height"); + return; } else { m->map_->set_height(value->IntegerValue()); } } else if (a == "background") { - if (!value->IsObject()) - ThrowException(Exception::TypeError( - String::New("mapnik.Color expected"))); + if (!value->IsObject()) { + NanThrowTypeError("mapnik.Color expected"); + return; + } - Local obj = value->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Color::constructor->HasInstance(obj)) - ThrowException(Exception::TypeError(String::New("mapnik.Color expected"))); + Local obj = value.As(); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Color::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Color expected"); + return; + } Color *c = node::ObjectWrap::Unwrap(obj); m->map_->set_background(*c->get()); } else if (a == "parameters") { #if MAPNIK_VERSION >= 200100 - if (!value->IsObject()) - ThrowException(Exception::TypeError( - String::New("object expected for map.parameters"))); + if (!value->IsObject()) { + NanThrowTypeError("object expected for map.parameters"); + return; + } Local obj = value->ToObject(); - if (obj->IsNull() || obj->IsUndefined()) - ThrowException(Exception::TypeError(String::New("object expected for map.parameters, cannot be null/undefined"))); + if (obj->IsNull() || obj->IsUndefined()) { + NanThrowTypeError("object expected for map.parameters, cannot be null/undefined"); + return; + } mapnik::parameters params; Local names = obj->GetPropertyNames(); @@ -345,18 +353,18 @@ void Map::set_prop(Local property, } } -Handle Map::scale(const Arguments& args) +NAN_METHOD(Map::scale) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Number::New(m->map_->scale())); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(m->map_->scale())); } -Handle Map::scaleDenominator(const Arguments& args) +NAN_METHOD(Map::scaleDenominator) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Number::New(m->map_->scale_denominator())); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(m->map_->scale_denominator())); } typedef struct { @@ -373,34 +381,34 @@ typedef struct { } query_map_baton_t; -Handle Map::queryMapPoint(const Arguments& args) +NAN_METHOD(Map::queryMapPoint) { - HandleScope scope; + NanScope(); abstractQueryPoint(args,false); - return Undefined(); + NanReturnUndefined(); } -Handle Map::queryPoint(const Arguments& args) +NAN_METHOD(Map::queryPoint) { - HandleScope scope; + NanScope(); abstractQueryPoint(args,true); - return Undefined(); + NanReturnUndefined(); } -Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) +Handle Map::abstractQueryPoint(_NAN_METHOD_ARGS, bool geo_coords) { - HandleScope scope; + NanScope(); if (args.Length() < 3) { - return ThrowException(Exception::TypeError( - String::New("requires at least three arguments, a x,y query and a callback"))); + NanThrowError("requires at least three arguments, a x,y query and a callback"); + return NanUndefined(); } double x,y; if (!args[0]->IsNumber() || !args[1]->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("x,y arguments must be numbers"))); + NanThrowTypeError("x,y arguments must be numbers"); + return NanUndefined(); } else { @@ -408,27 +416,29 @@ Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) y = args[1]->NumberValue(); } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); - Local options = Object::New(); + Local options = NanNew(); int layer_idx = -1; if (args.Length() > 3) { // options object - if (!args[2]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional third argument must be an options object"))); + if (!args[2]->IsObject()) { + NanThrowTypeError("optional third argument must be an options object"); + return NanUndefined(); + } options = args[2]->ToObject(); - if (options->Has(String::New("layer"))) + if (options->Has(NanNew("layer"))) { std::vector const& layers = m->map_->layers(); - Local layer_id = options->Get(String::New("layer")); - if (! (layer_id->IsString() || layer_id->IsNumber()) ) - return ThrowException(Exception::TypeError( - String::New("'layer' option required for map query and must be either a layer name(string) or layer index (integer)"))); + Local layer_id = options->Get(NanNew("layer")); + if (! (layer_id->IsString() || layer_id->IsNumber()) ) { + NanThrowTypeError("'layer' option required for map query and must be either a layer name(string) or layer index (integer)"); + return NanUndefined(); + } if (layer_id->IsString()) { bool found = false; @@ -448,7 +458,8 @@ Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) { std::ostringstream s; s << "Layer name '" << layer_name << "' not found"; - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + return NanUndefined(); } } else if (layer_id->IsNumber()) @@ -468,7 +479,8 @@ Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) { s << "no layers found in map"; } - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + return NanUndefined(); } else if (layer_idx >= static_cast(layer_num)) { std::ostringstream s; s << "Zero-based layer index '" << layer_idx << "' not valid, "; @@ -480,19 +492,22 @@ Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) { s << "no layers found in map"; } - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + return NanUndefined(); } } else { - return ThrowException(Exception::TypeError(String::New("layer id must be a string or index number"))); + NanThrowTypeError("layer id must be a string or index number"); + return NanUndefined(); } } } // ensure function callback - Local callback = args[args.Length()-1]; - if (!callback->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!callback->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + return NanUndefined(); + } query_map_baton_t *closure = new query_map_baton_t(); closure->request.data = closure; @@ -502,10 +517,10 @@ Handle Map::abstractQueryPoint(const Arguments& args, bool geo_coords) closure->layer_idx = static_cast(layer_idx); closure->geo_coords = geo_coords; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_QueryMap, (uv_after_work_cb)EIO_AfterQueryMap); m->Ref(); - return Undefined(); + return NanUndefined(); } void Map::EIO_QueryMap(uv_work_t* req) @@ -566,91 +581,88 @@ void Map::EIO_QueryMap(uv_work_t* req) void Map::EIO_AfterQueryMap(uv_work_t* req) { - HandleScope scope; + NanScope(); query_map_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { std::size_t num_result = closure->featuresets.size(); if (num_result >= 1) { - Local a = Array::New(num_result); + Local a = NanNew(num_result); typedef std::map fs_itr; fs_itr::const_iterator it = closure->featuresets.begin(); fs_itr::const_iterator end = closure->featuresets.end(); unsigned idx = 0; for (; it != end; ++it) { - Local obj = Object::New(); - obj->Set(String::NewSymbol("layer"), String::New(it->first.c_str())); - obj->Set(String::NewSymbol("featureset"), Featureset::New(it->second)); + Local obj = NanNew(); + obj->Set(NanNew("layer"), NanNew(it->first.c_str())); + obj->Set(NanNew("featureset"), Featureset::New(it->second)); a->Set(idx, obj); ++idx; } closure->featuresets.clear(); - Local argv[2] = { Local::New(Null()), a }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), a }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } else { - Local argv[2] = { Local::New(Null()), - Local::New(Undefined()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), NanUndefined() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - closure->m->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Map::layers(const Arguments& args) +NAN_METHOD(Map::layers) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::vector const& layers = m->map_->layers(); - Local a = Array::New(layers.size()); + Local a = NanNew(layers.size()); for (unsigned i = 0; i < layers.size(); ++i ) { a->Set(i, Layer::New(layers[i])); } - return scope.Close(a); + NanReturnValue(a); } -Handle Map::add_layer(const Arguments &args) { - HandleScope scope; +NAN_METHOD(Map::add_layer) { + NanScope(); - if (!args[0]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("mapnik.Layer expected"))); + if (!args[0]->IsObject()) { + NanThrowTypeError("mapnik.Layer expected"); + NanReturnUndefined(); + } - Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Layer::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Layer expected"))); + Local obj = args[0].As(); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Layer::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Layer expected"); + NanReturnUndefined(); + } Layer *l = node::ObjectWrap::Unwrap(obj); - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); m->map_->MAPNIK_ADD_LAYER(*l->get()); - return Undefined(); + NanReturnUndefined(); } -Handle Map::get_layer(const Arguments& args) +NAN_METHOD(Map::get_layer) { - HandleScope scope; + NanScope(); - if (args.Length() != 1) - return ThrowException(Exception::Error( - String::New("Please provide layer name or index"))); + if (args.Length() != 1) { + NanThrowError("Please provide layer name or index"); + NanReturnUndefined(); + } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::vector const& layers = m->map_->layers(); Local layer = args[0]; @@ -660,12 +672,12 @@ Handle Map::get_layer(const Arguments& args) if (index < layers.size()) { - return scope.Close(Layer::New(layers[index])); + NanReturnValue(Layer::New(layers[index])); } else { - return ThrowException(Exception::TypeError( - String::New("invalid layer index"))); + NanThrowTypeError("invalid layer index"); + NanReturnUndefined(); } } else if (layer->IsString()) @@ -678,7 +690,7 @@ Handle Map::get_layer(const Arguments& args) if (lyr.name() == layer_name) { found = true; - return scope.Close(Layer::New(layers[idx])); + NanReturnValue(Layer::New(layers[idx])); } ++idx; } @@ -686,43 +698,45 @@ Handle Map::get_layer(const Arguments& args) { std::ostringstream s; s << "Layer name '" << layer_name << "' not found"; - return ThrowException(Exception::TypeError( - String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else { - return ThrowException(Exception::TypeError( - String::New("first argument must be either a layer name(string) or layer index (integer)"))); + NanThrowTypeError("first argument must be either a layer name(string) or layer index (integer)"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Map::clear(const Arguments& args) +NAN_METHOD(Map::clear) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); m->map_->remove_all(); - return Undefined(); + NanReturnUndefined(); } -Handle Map::resize(const Arguments& args) +NAN_METHOD(Map::resize) { - HandleScope scope; + NanScope(); - if (args.Length() != 2) - return ThrowException(Exception::Error( - String::New("Please provide width and height"))); + if (args.Length() != 2) { + NanThrowError("Please provide width and height"); + NanReturnUndefined(); + } - if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("width and height must be integers"))); + if (!args[0]->IsNumber() || !args[1]->IsNumber()) { + NanThrowTypeError("width and height must be integers"); + NanReturnUndefined(); + } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); m->map_->resize(args[0]->IntegerValue(),args[1]->IntegerValue()); - return Undefined(); + NanReturnUndefined(); } @@ -738,56 +752,62 @@ typedef struct { } load_xml_baton_t; -Handle Map::load(const Arguments& args) +NAN_METHOD(Map::load) { - HandleScope scope; + NanScope(); - if (args.Length() < 2) - return ThrowException(Exception::Error( - String::New("please provide a stylesheet path, options, and callback"))); + if (args.Length() < 2) { + NanThrowError("please provide a stylesheet path, options, and callback"); + NanReturnUndefined(); + } // ensure stylesheet path is a string Local stylesheet = args[0]; - if (!stylesheet->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a mapnik stylesheet"))); + if (!stylesheet->IsString()) { + NanThrowTypeError("first argument must be a path to a mapnik stylesheet"); + NanReturnUndefined(); + } // ensure callback is a function Local callback = args[args.Length()-1]; - if (!callback->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!callback->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } // ensure options object - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("options must be an object, eg {strict: true}"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("options must be an object, eg {strict: true}"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); bool strict = false; - Local param = String::New("strict"); + Local param = NanNew("strict"); if (options->Has(param)) { Local param_val = options->Get(param); - if (!param_val->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'strict' must be a Boolean"))); + if (!param_val->IsBoolean()) { + NanThrowTypeError("'strict' must be a Boolean"); + NanReturnUndefined(); + } strict = param_val->BooleanValue(); } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); load_xml_baton_t *closure = new load_xml_baton_t(); closure->request.data = closure; - param = String::New("base"); + param = NanNew("base"); if (options->Has(param)) { Local param_val = options->Get(param); - if (!param_val->IsString()) - return ThrowException(Exception::TypeError( - String::New("'base' must be a string representing a filesystem path"))); + if (!param_val->IsString()) { + NanThrowTypeError("'base' must be a string representing a filesystem path"); + NanReturnUndefined(); + } closure->base_path = TOSTR(param_val); } @@ -795,10 +815,10 @@ Handle Map::load(const Arguments& args) closure->m = m; closure->strict = strict; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Load, (uv_after_work_cb)EIO_AfterLoad); m->Ref(); - return Undefined(); + NanReturnUndefined(); } void Map::EIO_Load(uv_work_t* req) @@ -823,38 +843,33 @@ void Map::EIO_Load(uv_work_t* req) void Map::EIO_AfterLoad(uv_work_t* req) { - HandleScope scope; + NanScope(); load_xml_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->m->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->m) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->m->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Map::loadSync(const Arguments& args) +NAN_METHOD(Map::loadSync) { - HandleScope scope; - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a mapnik stylesheet"))); + NanScope(); + if (!args[0]->IsString()) { + NanThrowTypeError("first argument must be a path to a mapnik stylesheet"); + NanReturnUndefined(); + } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string stylesheet = TOSTR(args[0]); bool strict = false; std::string base_path; @@ -862,35 +877,41 @@ Handle Map::loadSync(const Arguments& args) if (args.Length() > 2) { - return ThrowException(Exception::TypeError( - String::New("only accepts two arguments: a path to a mapnik stylesheet and an optional options object"))); + NanThrowError("only accepts two arguments: a path to a mapnik stylesheet and an optional options object"); + NanReturnUndefined(); } else if (args.Length() == 2) { // ensure options object if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("options must be an object, eg {strict: true}"))); + { + NanThrowTypeError("options must be an object, eg {strict: true}"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - Local param = String::New("strict"); + Local param = NanNew("strict"); if (options->Has(param)) { Local param_val = options->Get(param); if (!param_val->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'strict' must be a Boolean"))); + { + NanThrowTypeError("'strict' must be a Boolean"); + NanReturnUndefined(); + } strict = param_val->BooleanValue(); } - param = String::New("base"); + param = NanNew("base"); if (options->Has(param)) { Local param_val = options->Get(param); if (!param_val->IsString()) - return ThrowException(Exception::TypeError( - String::New("'base' must be a string representing a filesystem path"))); + { + NanThrowTypeError("'base' must be a string representing a filesystem path"); + NanReturnUndefined(); + } base_path = TOSTR(param_val); } } @@ -905,23 +926,24 @@ Handle Map::loadSync(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Map::fromStringSync(const Arguments& args) +NAN_METHOD(Map::fromStringSync) { - HandleScope scope; + NanScope(); if (args.Length() < 1) { - return ThrowException(Exception::TypeError( - String::New("Accepts 2 arguments: stylesheet string and an optional options"))); + NanThrowError("Accepts 2 arguments: stylesheet string and an optional options"); + NanReturnUndefined(); } - if (!args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a mapnik stylesheet string"))); + if (!args[0]->IsString()) { + NanThrowTypeError("first argument must be a mapnik stylesheet string"); + NanReturnUndefined(); + } // defaults @@ -930,34 +952,37 @@ Handle Map::fromStringSync(const Arguments& args) if (args.Length() >= 2) { // ensure options object - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("options must be an object, eg {strict: true, base: \".\"'}"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("options must be an object, eg {strict: true, base: \".\"'}"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - Local param = String::New("strict"); + Local param = NanNew("strict"); if (options->Has(param)) { Local param_val = options->Get(param); - if (!param_val->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'strict' must be a Boolean"))); + if (!param_val->IsBoolean()) { + NanThrowTypeError("'strict' must be a Boolean"); + NanReturnUndefined(); + } strict = param_val->BooleanValue(); } - param = String::New("base"); + param = NanNew("base"); if (options->Has(param)) { Local param_val = options->Get(param); - if (!param_val->IsString()) - return ThrowException(Exception::TypeError( - String::New("'base' must be a string representing a filesystem path"))); + if (!param_val->IsString()) { + NanThrowTypeError("'base' must be a string representing a filesystem path"); + NanReturnUndefined(); + } base_path = TOSTR(param_val); } } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string stylesheet = TOSTR(args[0]); @@ -967,62 +992,74 @@ Handle Map::fromStringSync(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Map::fromString(const Arguments& args) +NAN_METHOD(Map::fromString) { - HandleScope scope; + NanScope(); if (args.Length() < 2) - return ThrowException(Exception::Error( - String::New("please provide a stylesheet string, options, and callback"))); + { + NanThrowError("please provide a stylesheet string, options, and callback"); + NanReturnUndefined(); + } // ensure stylesheet path is a string Local stylesheet = args[0]; if (!stylesheet->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a mapnik stylesheet string"))); + { + NanThrowTypeError("first argument must be a path to a mapnik stylesheet string"); + NanReturnUndefined(); + } // ensure callback is a function Local callback = args[args.Length()-1]; if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } // ensure options object if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("options must be an object, eg {strict: true, base: \".\"'}"))); + { + NanThrowTypeError("options must be an object, eg {strict: true, base: \".\"'}"); + NanReturnUndefined(); + } Local options = args[1]->ToObject(); bool strict = false; - Local param = String::New("strict"); + Local param = NanNew("strict"); if (options->Has(param)) { Local param_val = options->Get(param); if (!param_val->IsBoolean()) - return ThrowException(Exception::TypeError( - String::New("'strict' must be a Boolean"))); + { + NanThrowTypeError("'strict' must be a Boolean"); + NanReturnUndefined(); + } strict = param_val->BooleanValue(); } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); load_xml_baton_t *closure = new load_xml_baton_t(); closure->request.data = closure; - param = String::New("base"); + param = NanNew("base"); if (options->Has(param)) { Local param_val = options->Get(param); if (!param_val->IsString()) - return ThrowException(Exception::TypeError( - String::New("'base' must be a string representing a filesystem path"))); + { + NanThrowTypeError("'base' must be a string representing a filesystem path"); + NanReturnUndefined(); + } closure->base_path = TOSTR(param_val); } @@ -1030,10 +1067,10 @@ Handle Map::fromString(const Arguments& args) closure->m = m; closure->strict = strict; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_FromString, (uv_after_work_cb)EIO_AfterFromString); m->Ref(); - return Undefined(); + NanReturnUndefined(); } void Map::EIO_FromString(uv_work_t* req) @@ -1053,73 +1090,71 @@ void Map::EIO_FromString(uv_work_t* req) void Map::EIO_AfterFromString(uv_work_t* req) { - HandleScope scope; + NanScope(); load_xml_baton_t *closure = static_cast(req->data); TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->m->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->m) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->m->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle Map::save(const Arguments& args) +NAN_METHOD(Map::save) { - HandleScope scope; + NanScope(); if (args.Length() != 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to map.xml to save"))); + { + NanThrowTypeError("first argument must be a path to map.xml to save"); + NanReturnUndefined(); + } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string filename = TOSTR(args[0]); bool explicit_defaults = false; mapnik::save_map(*m->map_,filename,explicit_defaults); - return Undefined(); + NanReturnUndefined(); } -Handle Map::to_string(const Arguments& args) +NAN_METHOD(Map::to_string) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); bool explicit_defaults = false; std::string map_string = mapnik::save_map_to_string(*m->map_,explicit_defaults); - return scope.Close(String::New(map_string.c_str())); + NanReturnValue(NanNew(map_string.c_str())); } -Handle Map::zoomAll(const Arguments& args) +NAN_METHOD(Map::zoomAll) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); try { m->map_->zoom_all(); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle Map::zoomToBox(const Arguments& args) +NAN_METHOD(Map::zoomToBox) { - HandleScope scope; - Map* m = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); double minx; double miny; @@ -1129,9 +1164,11 @@ Handle Map::zoomToBox(const Arguments& args) if (args.Length() == 1) { if (!args[0]->IsArray()) - return ThrowException(Exception::Error( - String::New("Must provide an array of: [minx,miny,maxx,maxy]"))); - Local a = Local::Cast(args[0]); + { + NanThrowError("Must provide an array of: [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } + Local a = args[0].As(); minx = a->Get(0)->NumberValue(); miny = a->Get(1)->NumberValue(); maxx = a->Get(2)->NumberValue(); @@ -1139,9 +1176,10 @@ Handle Map::zoomToBox(const Arguments& args) } else if (args.Length() != 4) - return ThrowException(Exception::Error( - String::New("Must provide 4 arguments: minx,miny,maxx,maxy"))); - else { + { + NanThrowError("Must provide 4 arguments: minx,miny,maxx,maxy"); + NanReturnUndefined(); + } else { minx = args[0]->NumberValue(); miny = args[1]->NumberValue(); maxx = args[2]->NumberValue(); @@ -1149,7 +1187,7 @@ Handle Map::zoomToBox(const Arguments& args) } mapnik::box2d box(minx,miny,maxx,maxy); m->map_->zoom_to_box(box); - return Undefined(); + NanReturnUndefined(); } struct image_baton_t { @@ -1222,28 +1260,29 @@ struct vector_tile_baton_t { error(false) {} }; -Handle Map::render(const Arguments& args) +NAN_METHOD(Map::render) { - HandleScope scope; + NanScope(); // ensure at least 2 args if (args.Length() < 2) { - return ThrowException(Exception::TypeError( - String::New("requires at least two arguments, a renderable mapnik object, and a callback"))); + NanThrowTypeError("requires at least two arguments, a renderable mapnik object, and a callback"); + NanReturnUndefined(); } // ensure renderable object if (!args[0]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("requires a renderable mapnik object to be passed as first argument"))); + NanThrowTypeError("requires a renderable mapnik object to be passed as first argument"); + NanReturnUndefined(); } // ensure function callback - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); if (m->active() != 0) { std::ostringstream s; @@ -1263,68 +1302,76 @@ Handle Map::render(const Arguments& args) unsigned offset_x = 0; unsigned offset_y = 0; - Local options = Object::New(); + Local options = NanNew(); if (args.Length() > 2) { // options object - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second argument must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second argument must be an options object"); + NanReturnUndefined(); + } options = args[1]->ToObject(); - if (options->Has(String::New("buffer_size"))) { - Local bind_opt = options->Get(String::New("buffer_size")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'buffer_size' must be a number"))); + if (options->Has(NanNew("buffer_size"))) { + Local bind_opt = options->Get(NanNew("buffer_size")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'buffer_size' must be a number"); + NanReturnUndefined(); + } buffer_size = bind_opt->IntegerValue(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); + } scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::New("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + if (options->Has(NanNew("scale_denominator"))) { + Local bind_opt = options->Get(NanNew("scale_denominator")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); + } scale_denominator = bind_opt->NumberValue(); } - if (options->Has(String::New("offset_x"))) { - Local bind_opt = options->Get(String::New("offset_x")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'offset_x' must be a number"))); + if (options->Has(NanNew("offset_x"))) { + Local bind_opt = options->Get(NanNew("offset_x")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'offset_x' must be a number"); + NanReturnUndefined(); + } offset_x = bind_opt->IntegerValue(); } - if (options->Has(String::New("offset_y"))) { - Local bind_opt = options->Get(String::New("offset_y")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'offset_y' must be a number"))); + if (options->Has(NanNew("offset_y"))) { + Local bind_opt = options->Get(NanNew("offset_y")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'offset_y' must be a number"); + NanReturnUndefined(); + } offset_y = bind_opt->IntegerValue(); } } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined()) - return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a renderable mapnik object, not null/undefined"))); + if (obj->IsNull() || obj->IsUndefined()) { + NanThrowTypeError("first argument is invalid, must be a renderable mapnik object, not null/undefined"); + NanReturnUndefined(); + } - if (Image::constructor->HasInstance(obj)) { + if (NanNew(Image::constructor)->HasInstance(obj)) { image_baton_t *closure = new image_baton_t(); closure->request.data = closure; @@ -1337,27 +1384,28 @@ Handle Map::render(const Arguments& args) closure->offset_x = offset_x; closure->offset_y = offset_y; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(args[args.Length()-1])); + NanAssignPersistent(closure->cb, args[args.Length() - 1].As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_RenderImage, (uv_after_work_cb)EIO_AfterRenderImage); - } else if (Grid::constructor->HasInstance(obj)) { + } else if (NanNew(Grid::constructor)->HasInstance(obj)) { Grid * g = node::ObjectWrap::Unwrap(obj); std::size_t layer_idx = 0; // grid requires special options for now - if (!options->Has(String::New("layer"))) { - return ThrowException(Exception::TypeError( - String::New("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"))); + if (!options->Has(NanNew("layer"))) { + NanThrowTypeError("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"); + NanReturnUndefined(); } else { std::vector const& layers = m->map_->layers(); - Local layer_id = options->Get(String::New("layer")); - if (! (layer_id->IsString() || layer_id->IsNumber()) ) - return ThrowException(Exception::TypeError( - String::New("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"))); + Local layer_id = options->Get(NanNew("layer")); + if (! (layer_id->IsString() || layer_id->IsNumber()) ) { + NanThrowTypeError("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"); + NanReturnUndefined(); + } if (layer_id->IsString()) { bool found = false; @@ -1377,7 +1425,8 @@ Handle Map::render(const Arguments& args) { std::ostringstream s; s << "Layer name '" << layer_name << "' not found"; - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else if (layer_id->IsNumber()) { layer_idx = layer_id->IntegerValue(); @@ -1394,19 +1443,22 @@ Handle Map::render(const Arguments& args) { s << "no layers found in map"; } - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else { - return ThrowException(Exception::TypeError(String::New("layer id must be a string or index number"))); + NanThrowTypeError("layer id must be a string or index number"); + NanReturnUndefined(); } } - if (options->Has(String::New("fields"))) { + if (options->Has(NanNew("fields"))) { - Local param_val = options->Get(String::New("fields")); - if (!param_val->IsArray()) - return ThrowException(Exception::TypeError( - String::New("option 'fields' must be an array of strings"))); + Local param_val = options->Get(NanNew("fields")); + if (!param_val->IsArray()) { + NanThrowTypeError("option 'fields' must be an array of strings"); + NanReturnUndefined(); + } Local a = Local::Cast(param_val); unsigned int i = 0; unsigned int num_fields = a->Length(); @@ -1431,30 +1483,30 @@ Handle Map::render(const Arguments& args) closure->offset_x = offset_x; closure->offset_y = offset_y; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(args[args.Length()-1])); + NanAssignPersistent(closure->cb, args[args.Length() - 1].As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_RenderGrid, (uv_after_work_cb)EIO_AfterRenderGrid); - } else if (VectorTile::constructor->HasInstance(obj)) { + } else if (NanNew(VectorTile::constructor)->HasInstance(obj)) { vector_tile_baton_t *closure = new vector_tile_baton_t(); VectorTile * vector_tile_obj = node::ObjectWrap::Unwrap(obj); - if (options->Has(String::New("tolerance"))) { + if (options->Has(NanNew("tolerance"))) { - Local param_val = options->Get(String::New("tolerance")); + Local param_val = options->Get(NanNew("tolerance")); if (!param_val->IsNumber()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("option 'tolerance' must be an unsigned integer"))); + NanThrowTypeError("option 'tolerance' must be an unsigned integer"); + NanReturnUndefined(); } closure->tolerance = param_val->IntegerValue(); } - if (options->Has(String::New("path_multiplier"))) { - Local param_val = options->Get(String::New("path_multiplier")); + if (options->Has(NanNew("path_multiplier"))) { + Local param_val = options->Get(NanNew("path_multiplier")); if (!param_val->IsNumber()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("option 'path_multiplier' must be an unsigned integer"))); + NanThrowTypeError("option 'path_multiplier' must be an unsigned integer"); + NanReturnUndefined(); } closure->path_multiplier = param_val->NumberValue(); } @@ -1469,15 +1521,16 @@ Handle Map::render(const Arguments& args) closure->offset_x = offset_x; closure->offset_y = offset_y; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(args[args.Length()-1])); + NanAssignPersistent(closure->cb, args[args.Length() - 1].As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_RenderVectorTile, (uv_after_work_cb)EIO_AfterRenderVectorTile); } else { - return ThrowException(Exception::TypeError(String::New("renderable mapnik object expected"))); + NanThrowTypeError("renderable mapnik object expected"); + NanReturnUndefined(); } m->acquire(); m->Ref(); - return Undefined(); + NanReturnUndefined(); } void Map::EIO_RenderVectorTile(uv_work_t* req) @@ -1512,28 +1565,22 @@ void Map::EIO_RenderVectorTile(uv_work_t* req) void Map::EIO_AfterRenderVectorTile(uv_work_t* req) { - HandleScope scope; + NanScope(); vector_tile_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->d->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->d) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->m->release(); closure->m->Unref(); closure->d->_unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } @@ -1582,30 +1629,24 @@ void Map::EIO_RenderGrid(uv_work_t* req) void Map::EIO_AfterRenderGrid(uv_work_t* req) { - HandleScope scope; + NanScope(); grid_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { // TODO - add more attributes // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->g->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->g) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->m->release(); closure->m->Unref(); closure->g->_unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } @@ -1631,28 +1672,22 @@ void Map::EIO_RenderImage(uv_work_t* req) void Map::EIO_AfterRenderImage(uv_work_t* req) { - HandleScope scope; + NanScope(); image_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()), Local::New(closure->im->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->im) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } closure->m->release(); closure->m->Unref(); closure->im->_unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } @@ -1670,13 +1705,14 @@ typedef struct { Persistent cb; } render_file_baton_t; -Handle Map::renderFile(const Arguments& args) +NAN_METHOD(Map::renderFile) { - HandleScope scope; + NanScope(); - if (args.Length() < 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a file to save"))); + if (args.Length() < 1 || !args[0]->IsString()) { + NanThrowTypeError("first argument must be a path to a file to save"); + NanReturnUndefined(); + } // defaults std::string format = "png"; @@ -1686,59 +1722,66 @@ Handle Map::renderFile(const Arguments& args) Local callback = args[args.Length()-1]; - if (!callback->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!callback->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } if (!args[1]->IsFunction() && args[1]->IsObject()) { Local options = args[1]->ToObject(); - if (options->Has(String::New("format"))) + if (options->Has(NanNew("format"))) { - Local format_opt = options->Get(String::New("format")); - if (!format_opt->IsString()) - return ThrowException(Exception::TypeError( - String::New("'format' must be a String"))); + Local format_opt = options->Get(NanNew("format")); + if (!format_opt->IsString()) { + NanThrowTypeError("'format' must be a String"); + NanReturnUndefined(); + } format = TOSTR(format_opt); } - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); + } scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::New("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + if (options->Has(NanNew("scale_denominator"))) { + Local bind_opt = options->Get(NanNew("scale_denominator")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); + } scale_denominator = bind_opt->NumberValue(); } } else if (!args[1]->IsFunction()) { - return ThrowException(Exception::TypeError( - String::New("optional argument must be an object"))); + NanThrowTypeError("optional argument must be an object"); + NanReturnUndefined(); } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string output = TOSTR(args[0]); //maybe do this in the async part? @@ -1747,8 +1790,8 @@ Handle Map::renderFile(const Arguments& args) if (format == "") { std::ostringstream s(""); s << "unknown output extension for: " << output << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } } @@ -1761,8 +1804,8 @@ Handle Map::renderFile(const Arguments& args) delete closure; std::ostringstream s(""); s << "Cairo backend is not available, cannot write to " << format << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); #endif } else { closure->use_cairo = false; @@ -1774,7 +1817,7 @@ Handle Map::renderFile(const Arguments& args) closure->scale_factor = scale_factor; closure->scale_denominator = scale_denominator; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); closure->format = format; closure->palette = palette; @@ -1783,7 +1826,7 @@ Handle Map::renderFile(const Arguments& args) uv_queue_work(uv_default_loop(), &closure->request, EIO_RenderFile, (uv_after_work_cb)EIO_AfterRenderFile); m->Ref(); - return Undefined(); + NanReturnUndefined(); } @@ -1831,39 +1874,34 @@ void Map::EIO_RenderFile(uv_work_t* req) void Map::EIO_AfterRenderFile(uv_work_t* req) { - HandleScope scope; + NanScope(); render_file_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(),1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[1] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(),1, argv); - } - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[1] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } closure->m->release(); closure->m->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } // TODO - add support for grids -Handle Map::renderSync(const Arguments& args) +NAN_METHOD(Map::renderSync) { - HandleScope scope; + NanScope(); - if (args.Length() < 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("argument must be a format string"))); + if (args.Length() < 1 || !args[0]->IsString()) { + NanThrowTypeError("argument must be a format string"); + NanReturnUndefined(); + } std::string format = TOSTR(args[0]); palette_ptr palette; @@ -1871,47 +1909,54 @@ Handle Map::renderSync(const Arguments& args) double scale_denominator = 0.0; if (args.Length() >= 2){ - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("second argument is optional, but if provided must be an object, eg. {format: 'pdf'}"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("second argument is optional, but if provided must be an object, eg. {format: 'pdf'}"); + NanReturnUndefined(); + } Local options = args[1]->ToObject(); - if (options->Has(String::New("format"))) + if (options->Has(NanNew("format"))) { - Local format_opt = options->Get(String::New("format")); - if (!format_opt->IsString()) - return ThrowException(Exception::TypeError( - String::New("'format' must be a String"))); + Local format_opt = options->Get(NanNew("format")); + if (!format_opt->IsString()) { + NanThrowTypeError("'format' must be a String"); + NanReturnUndefined(); + } format = TOSTR(format_opt); } - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); + } scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::New("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + if (options->Has(NanNew("scale_denominator"))) { + Local bind_opt = options->Get(NanNew("scale_denominator")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); + } scale_denominator = bind_opt->NumberValue(); } @@ -1919,28 +1964,32 @@ Handle Map::renderSync(const Arguments& args) // options hash if (args.Length() >= 2) { - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("optional second arg must be an options object"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("optional second arg must be an options object"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); + Local options = args[1].As(); - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local bind_opt = options->Get(String::New("palette")); - if (!bind_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("mapnik.Palette expected as second arg"))); + Local bind_opt = options->Get(NanNew("palette")); + if (!bind_opt->IsObject()) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } Local obj = bind_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string s; try { @@ -1958,26 +2007,24 @@ Handle Map::renderSync(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); - } - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New((char*)s.data(),s.size())); - #else - return scope.Close(node::Buffer::New((char*)s.data(),s.size())->handle_); - #endif + NanThrowError(ex.what()); + NanReturnUndefined(); + } + NanReturnValue(NanNewBufferHandle((char*)s.data(), s.size())); } -Handle Map::renderFileSync(const Arguments& args) +NAN_METHOD(Map::renderFileSync) { - HandleScope scope; - if (args.Length() < 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a file to save"))); + NanScope(); + if (args.Length() < 1 || !args[0]->IsString()) { + NanThrowTypeError("first argument must be a path to a file to save"); + NanReturnUndefined(); + } - if (args.Length() > 2) - return ThrowException(Exception::TypeError( - String::New("accepts two arguments, a required path to a file, an optional options object, eg. {format: 'pdf'}"))); + if (args.Length() > 2) { + NanThrowError("accepts two arguments, a required path to a file, an optional options object, eg. {format: 'pdf'}"); + NanReturnUndefined(); + } // defaults double scale_factor = 1.0; @@ -1986,53 +2033,60 @@ Handle Map::renderFileSync(const Arguments& args) palette_ptr palette; if (args.Length() >= 2){ - if (!args[1]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("second argument is optional, but if provided must be an object, eg. {format: 'pdf'}"))); + if (!args[1]->IsObject()) { + NanThrowTypeError("second argument is optional, but if provided must be an object, eg. {format: 'pdf'}"); + NanReturnUndefined(); + } - Local options = args[1]->ToObject(); - if (options->Has(String::New("format"))) + Local options = args[1].As(); + if (options->Has(NanNew("format"))) { - Local format_opt = options->Get(String::New("format")); - if (!format_opt->IsString()) - return ThrowException(Exception::TypeError( - String::New("'format' must be a String"))); + Local format_opt = options->Get(NanNew("format")); + if (!format_opt->IsString()) { + NanThrowTypeError("'format' must be a String"); + NanReturnUndefined(); + } format = TOSTR(format_opt); } - if (options->Has(String::New("palette"))) + if (options->Has(NanNew("palette"))) { - Local format_opt = options->Get(String::New("palette")); - if (!format_opt->IsObject()) - return ThrowException(Exception::TypeError( - String::New("'palette' must be an object"))); + Local format_opt = options->Get(NanNew("palette")); + if (!format_opt->IsObject()) { + NanThrowTypeError("'palette' must be an object"); + NanReturnUndefined(); + } Local obj = format_opt->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Palette::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Palette expected as second arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Palette::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Palette expected as second arg"); + NanReturnUndefined(); + } palette = node::ObjectWrap::Unwrap(obj)->palette(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); + } scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::New("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); - if (!bind_opt->IsNumber()) - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + if (options->Has(NanNew("scale_denominator"))) { + Local bind_opt = options->Get(NanNew("scale_denominator")); + if (!bind_opt->IsNumber()) { + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); + } scale_denominator = bind_opt->NumberValue(); } } - Map* m = node::ObjectWrap::Unwrap(args.This()); + Map* m = node::ObjectWrap::Unwrap(args.Holder()); std::string output = TOSTR(args[0]); if (format.empty()) { @@ -2040,8 +2094,8 @@ Handle Map::renderFileSync(const Arguments& args) if (format == "") { std::ostringstream s(""); s << "unknown output extension for: " << output << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } } @@ -2063,8 +2117,8 @@ Handle Map::renderFileSync(const Arguments& args) #else std::ostringstream s(""); s << "Cairo backend is not available, cannot write to " << format << "\n"; - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); #endif } else @@ -2084,8 +2138,8 @@ Handle Map::renderFileSync(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } diff --git a/src/mapnik_map.hpp b/src/mapnik_map.hpp index e1cc93b5c0..03daa0b6d4 100644 --- a/src/mapnik_map.hpp +++ b/src/mapnik_map.hpp @@ -1,9 +1,7 @@ #ifndef __NODE_MAPNIK_MAP_H__ #define __NODE_MAPNIK_MAP_H__ -#include -#include -#include +#include #include "mapnik3x_compatibility.hpp" // boost #include MAPNIK_SHARED_INCLUDE @@ -20,20 +18,20 @@ class Map: public node::ObjectWrap { static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle loadSync(const Arguments &args); - static Handle load(const Arguments &args); + static NAN_METHOD(loadSync); + static NAN_METHOD(load); static void EIO_Load(uv_work_t* req); static void EIO_AfterLoad(uv_work_t* req); - static Handle fromStringSync(const Arguments &args); - static Handle fromString(const Arguments &args); + static NAN_METHOD(fromStringSync); + static NAN_METHOD(fromString); static void EIO_FromString(uv_work_t* req); static void EIO_AfterFromString(uv_work_t* req); // async rendering - static Handle render(const Arguments &args); + static NAN_METHOD(render); static void EIO_RenderImage(uv_work_t* req); static void EIO_AfterRenderImage(uv_work_t* req); static void EIO_RenderGrid(uv_work_t* req); @@ -41,38 +39,35 @@ class Map: public node::ObjectWrap { static void EIO_RenderVectorTile(uv_work_t* req); static void EIO_AfterRenderVectorTile(uv_work_t* req); - static Handle renderFile(const Arguments &args); + static NAN_METHOD(renderFile); static void EIO_RenderFile(uv_work_t* req); static void EIO_AfterRenderFile(uv_work_t* req); // sync rendering - static Handle renderSync(const Arguments &args); - static Handle renderFileSync(const Arguments &args); - - static Handle save(const Arguments &args); - static Handle to_string(const Arguments &args); - - static Handle clear(const Arguments &args); - static Handle resize(const Arguments &args); - static Handle zoomAll(const Arguments &args); - static Handle zoomToBox(const Arguments &args); - static Handle layers(const Arguments &args); - static Handle scale(const Arguments &args); - static Handle scaleDenominator(const Arguments &args); - static Handle queryPoint(const Arguments &args); - static Handle queryMapPoint(const Arguments &args); - static Handle abstractQueryPoint(const Arguments &args, bool geo_coords); + static NAN_METHOD(renderSync); + static NAN_METHOD(renderFileSync); + + static NAN_METHOD(save); + static NAN_METHOD(to_string); + + static NAN_METHOD(clear); + static NAN_METHOD(resize); + static NAN_METHOD(zoomAll); + static NAN_METHOD(zoomToBox); + static NAN_METHOD(layers); + static NAN_METHOD(scale); + static NAN_METHOD(scaleDenominator); + static NAN_METHOD(queryPoint); + static NAN_METHOD(queryMapPoint); + static Handle abstractQueryPoint(_NAN_METHOD_ARGS, bool geo_coords); static void EIO_QueryMap(uv_work_t* req); static void EIO_AfterQueryMap(uv_work_t* req); - static Handle add_layer(const Arguments &args); - static Handle get_layer(const Arguments &args); + static NAN_METHOD(add_layer); + static NAN_METHOD(get_layer); - static Handle get_prop(Local property, - const AccessorInfo& info); - static void set_prop(Local property, - Local value, - const AccessorInfo& info); + static NAN_GETTER(get_prop); + static NAN_SETTER(set_prop); Map(int width, int height); Map(int width, int height, std::string const& srs); diff --git a/src/mapnik_memory_datasource.cpp b/src/mapnik_memory_datasource.cpp index 7ba21f047d..9c0e89ce1f 100644 --- a/src/mapnik_memory_datasource.cpp +++ b/src/mapnik_memory_datasource.cpp @@ -1,4 +1,3 @@ - // mapnik #include #include @@ -22,20 +21,21 @@ Persistent MemoryDatasource::constructor; void MemoryDatasource::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(MemoryDatasource::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("MemoryDatasource")); + Local lcons = NanNew(MemoryDatasource::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("MemoryDatasource")); // methods - NODE_SET_PROTOTYPE_METHOD(constructor, "parameters", parameters); - NODE_SET_PROTOTYPE_METHOD(constructor, "describe", describe); - NODE_SET_PROTOTYPE_METHOD(constructor, "features", features); - NODE_SET_PROTOTYPE_METHOD(constructor, "featureset", featureset); - NODE_SET_PROTOTYPE_METHOD(constructor, "add", add); - - target->Set(String::NewSymbol("MemoryDatasource"),constructor->GetFunction()); + NODE_SET_PROTOTYPE_METHOD(lcons, "parameters", parameters); + NODE_SET_PROTOTYPE_METHOD(lcons, "describe", describe); + NODE_SET_PROTOTYPE_METHOD(lcons, "features", features); + NODE_SET_PROTOTYPE_METHOD(lcons, "featureset", featureset); + NODE_SET_PROTOTYPE_METHOD(lcons, "add", add); + + target->Set(NanNew("MemoryDatasource"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } MemoryDatasource::MemoryDatasource() : @@ -48,12 +48,15 @@ MemoryDatasource::~MemoryDatasource() { } -Handle MemoryDatasource::New(const Arguments& args) +NAN_METHOD(MemoryDatasource::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args[0]->IsExternal()) { @@ -62,18 +65,20 @@ Handle MemoryDatasource::New(const Arguments& args) void* ptr = ext->Value(); MemoryDatasource* d = static_cast(ptr); d->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } if (args.Length() != 1){ - return ThrowException(Exception::TypeError( - String::New("accepts only one argument, an object of key:value datasource options"))); + NanThrowTypeError("accepts only one argument, an object of key:value datasource options"); + NanReturnUndefined(); } if (!args[0]->IsObject()) - return ThrowException(Exception::TypeError( - String::New("Must provide an object, eg {type: 'shape', file : 'world.shp'}"))); + { + NanThrowTypeError("Must provide an object, eg {type: 'shape', file : 'world.shp'}"); + NanReturnUndefined(); + } - Local options = args[0]->ToObject(); + Local options = args[0].As(); mapnik::parameters params; Local names = options->GetPropertyNames(); @@ -90,23 +95,23 @@ Handle MemoryDatasource::New(const Arguments& args) MemoryDatasource* d = new MemoryDatasource(); d->Wrap(args.This()); d->datasource_ = MAPNIK_MAKE_SHARED(); - return args.This(); + NanReturnValue(args.This()); } Handle MemoryDatasource::New(mapnik::datasource_ptr ds_ptr) { - HandleScope scope; + NanEscapableScope(); MemoryDatasource* d = new MemoryDatasource(); d->datasource_ = ds_ptr; - Handle ext = External::New(d); - Handle obj = constructor->GetFunction()->NewInstance(1, &ext); - return scope.Close(obj); + Handle ext = NanNew(d); + Handle obj = NanNew(constructor)->GetFunction()->NewInstance(1, &ext); + return NanEscapeScope(obj); } -Handle MemoryDatasource::parameters(const Arguments& args) +NAN_METHOD(MemoryDatasource::parameters) { - HandleScope scope; - MemoryDatasource* d = node::ObjectWrap::Unwrap(args.This()); - Local ds = Object::New(); + NanScope(); + MemoryDatasource* d = node::ObjectWrap::Unwrap(args.Holder()); + Local ds = NanNew(); if (d->datasource_) { mapnik::parameters::const_iterator it = d->datasource_->params().begin(); mapnik::parameters::const_iterator end = d->datasource_->params().end(); @@ -116,31 +121,31 @@ Handle MemoryDatasource::parameters(const Arguments& args) boost::apply_visitor( serializer, it->second ); } } - return scope.Close(ds); + NanReturnValue(ds); } -Handle MemoryDatasource::describe(const Arguments& args) +NAN_METHOD(MemoryDatasource::describe) { - HandleScope scope; - MemoryDatasource* d = node::ObjectWrap::Unwrap(args.This()); - Local description = Object::New(); + NanScope(); + MemoryDatasource* d = node::ObjectWrap::Unwrap(args.Holder()); + Local description = NanNew(); if (d->datasource_) { try { node_mapnik::describe_datasource(description,d->datasource_); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } - return scope.Close(description); + NanReturnValue(description); } -Handle MemoryDatasource::features(const Arguments& args) +NAN_METHOD(MemoryDatasource::features) { - HandleScope scope; + NanScope(); unsigned first = 0; unsigned last = 0; @@ -148,16 +153,18 @@ Handle MemoryDatasource::features(const Arguments& args) if (args.Length() == 2) { if (!args[0]->IsNumber() || !args[1]->IsNumber()) - return ThrowException(Exception::Error( - String::New("Index of 'first' and 'last' feature must be an integer"))); + { + NanThrowError("Index of 'first' and 'last' feature must be an integer"); + NanReturnUndefined(); + } first = args[0]->IntegerValue(); last = args[1]->IntegerValue(); } - MemoryDatasource* d = node::ObjectWrap::Unwrap(args.This()); + MemoryDatasource* d = node::ObjectWrap::Unwrap(args.Holder()); // TODO - we don't know features.length at this point - Local a = Array::New(0); + Local a = NanNew(0); if (d->datasource_) { try @@ -166,20 +173,20 @@ Handle MemoryDatasource::features(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } - return scope.Close(a); + NanReturnValue(a); } -Handle MemoryDatasource::featureset(const Arguments& args) +NAN_METHOD(MemoryDatasource::featureset) { - HandleScope scope; + NanScope(); - MemoryDatasource* d = node::ObjectWrap::Unwrap(args.This()); + MemoryDatasource* d = node::ObjectWrap::Unwrap(args.Holder()); try { @@ -197,42 +204,44 @@ Handle MemoryDatasource::featureset(const Arguments& args) mapnik::featureset_ptr fs = d->datasource_->features(q); if (fs) { - return scope.Close(Featureset::New(fs)); + NanReturnValue(Featureset::New(fs)); } } } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } -Handle MemoryDatasource::add(const Arguments& args) +NAN_METHOD(MemoryDatasource::add) { - HandleScope scope; + NanScope(); if ((args.Length() != 1) || !args[0]->IsObject()) { - return ThrowException(Exception::Error( - String::New("accepts one argument: an object including x and y (or wkt) and properties"))); + NanThrowError("accepts one argument: an object including x and y (or wkt) and properties"); + NanReturnUndefined(); } - MemoryDatasource* d = node::ObjectWrap::Unwrap(args.This()); + MemoryDatasource* d = node::ObjectWrap::Unwrap(args.Holder()); - Local obj = args[0]->ToObject(); + Local obj = args[0].As(); - if (obj->Has(String::New("wkt")) || (obj->Has(String::New("x")) && obj->Has(String::New("y")))) + if (obj->Has(NanNew("wkt")) || (obj->Has(NanNew("x")) && obj->Has(NanNew("y")))) { - if (obj->Has(String::New("wkt"))) - return ThrowException(Exception::Error( - String::New("wkt not yet supported"))); + if (obj->Has(NanNew("wkt"))) + { + NanThrowError("wkt not yet supported"); + NanReturnUndefined(); + } - Local x = obj->Get(String::New("x")); - Local y = obj->Get(String::New("y")); + Local x = obj->Get(NanNew("x")); + Local y = obj->Get(NanNew("y")); if (!x->IsUndefined() && x->IsNumber() && !y->IsUndefined() && y->IsNumber()) { mapnik::geometry_type * pt = new mapnik::geometry_type(MAPNIK_POINT); @@ -245,9 +254,9 @@ Handle MemoryDatasource::add(const Arguments& args) #endif ++(d->feature_id_); feature->add_geometry(pt); - if (obj->Has(String::New("properties"))) + if (obj->Has(NanNew("properties"))) { - Local props = obj->Get(String::New("properties")); + Local props = obj->Get(NanNew("properties")); if (props->IsObject()) { Local p_obj = props->ToObject(); @@ -300,5 +309,5 @@ Handle MemoryDatasource::add(const Arguments& args) cache->push(feature); } } - return scope.Close(False()); + NanReturnValue(NanFalse()); } diff --git a/src/mapnik_memory_datasource.hpp b/src/mapnik_memory_datasource.hpp index 7b11964157..bec4146eb5 100644 --- a/src/mapnik_memory_datasource.hpp +++ b/src/mapnik_memory_datasource.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_MEMORY_DATASOURCE_H__ #define __NODE_MAPNIK_MEMORY_DATASOURCE_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" #include #include @@ -13,13 +12,13 @@ class MemoryDatasource: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); static Handle New(mapnik::datasource_ptr ds_ptr); - static Handle parameters(const Arguments &args); - static Handle describe(const Arguments &args); - static Handle features(const Arguments &args); - static Handle featureset(const Arguments &args); - static Handle add(const Arguments &args); + static NAN_METHOD(parameters); + static NAN_METHOD(describe); + static NAN_METHOD(features); + static NAN_METHOD(featureset); + static NAN_METHOD(add); MemoryDatasource(); inline mapnik::datasource_ptr get() { return datasource_; } diff --git a/src/mapnik_palette.cpp b/src/mapnik_palette.cpp index ed3b523c2f..8056c9f8ab 100644 --- a/src/mapnik_palette.cpp +++ b/src/mapnik_palette.cpp @@ -1,12 +1,7 @@ - // node-mapnik #include "mapnik_palette.hpp" #include "utils.hpp" -// node -#include -#include - // stl #include #include @@ -19,16 +14,17 @@ Persistent Palette::constructor; void Palette::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Palette::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Palette")); + Local lcons = NanNew(Palette::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Palette")); - NODE_SET_PROTOTYPE_METHOD(constructor, "toString", ToString); - NODE_SET_PROTOTYPE_METHOD(constructor, "toBuffer", ToBuffer); + NODE_SET_PROTOTYPE_METHOD(lcons, "toString", ToString); + NODE_SET_PROTOTYPE_METHOD(lcons, "toBuffer", ToBuffer); - target->Set(String::NewSymbol("Palette"), constructor->GetFunction()); + target->Set(NanNew("Palette"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Palette::Palette(std::string const& palette, mapnik::rgba_palette::palette_type type) : @@ -38,22 +34,25 @@ Palette::Palette(std::string const& palette, mapnik::rgba_palette::palette_type Palette::~Palette() { } -Handle Palette::New(const Arguments& args) { - HandleScope scope; +NAN_METHOD(Palette::New) { + NanScope(); if (!args.IsConstructCall()) { - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); } std::string palette; mapnik::rgba_palette::palette_type type = mapnik::rgba_palette::PALETTE_RGBA; if (args.Length() >= 1) { if (args[0]->IsString()) { - String::AsciiValue obj(args[0]->ToString()); - palette = std::string(*obj, obj.length()); + size_t count; + char* decoded = static_cast(NanRawString(args[0], Nan::ASCII, &count, NULL, 0, String::NO_OPTIONS)); + palette = std::string(decoded, count); + delete[] decoded; } else if (node::Buffer::HasInstance(args[0])) { - Local obj = args[0]->ToObject(); + Local obj = args[0].As(); palette = std::string(node::Buffer::Data(obj), node::Buffer::Length(obj)); } } @@ -66,26 +65,27 @@ Handle Palette::New(const Arguments& args) { } if (!palette.length()) { - return ThrowException(Exception::TypeError( - String::New("First parameter must be a palette string"))); + NanThrowTypeError("First parameter must be a palette string"); + NanReturnUndefined(); } try { Palette* p = new Palette(palette, type); p->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error(String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle Palette::ToString(const Arguments& args) +NAN_METHOD(Palette::ToString) { - HandleScope scope; - palette_ptr p = node::ObjectWrap::Unwrap(args.This())->palette_; + NanScope(); + palette_ptr p = node::ObjectWrap::Unwrap(args.Holder())->palette_; const std::vector& colors = p->palette(); unsigned length = colors.size(); @@ -108,14 +108,14 @@ Handle Palette::ToString(const Arguments& args) } str << "]"; - return scope.Close(String::New(str.str().c_str())); + NanReturnValue(NanNew(str.str().c_str())); } -Handle Palette::ToBuffer(const Arguments& args) +NAN_METHOD(Palette::ToBuffer) { - HandleScope scope; + NanScope(); - palette_ptr p = node::ObjectWrap::Unwrap(args.This())->palette_; + palette_ptr p = node::ObjectWrap::Unwrap(args.Holder())->palette_; const std::vector& colors = p->palette(); unsigned length = colors.size(); @@ -129,9 +129,5 @@ Handle Palette::ToBuffer(const Arguments& args) palette[pos++] = colors[i].b; palette[pos++] = (i < alphaLength) ? alpha[i] : 0xFF; } - #if NODE_VERSION_AT_LEAST(0, 11, 0) - return scope.Close(node::Buffer::New(palette, length * 4)); - #else - return scope.Close(node::Buffer::New(palette, length * 4)->handle_); - #endif + NanReturnValue(NanNewBufferHandle(palette, length * 4)); } diff --git a/src/mapnik_palette.hpp b/src/mapnik_palette.hpp index 84726e3ff5..4b6a4e1420 100644 --- a/src/mapnik_palette.hpp +++ b/src/mapnik_palette.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_PALETTE_H__ #define __NODE_MAPNIK_PALETTE_H__ -#include -#include +#include #include "mapnik3x_compatibility.hpp" // boost #include MAPNIK_SHARED_INCLUDE @@ -19,10 +18,10 @@ class Palette: public node::ObjectWrap { explicit Palette(std::string const& palette, mapnik::rgba_palette::palette_type type); static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle ToString(const Arguments& args); - static Handle ToBuffer(const Arguments& args); + static NAN_METHOD(ToString); + static NAN_METHOD(ToBuffer); inline palette_ptr palette() { return palette_; } private: diff --git a/src/mapnik_plugins.hpp b/src/mapnik_plugins.hpp index f9dc383da6..e3ac50949b 100644 --- a/src/mapnik_plugins.hpp +++ b/src/mapnik_plugins.hpp @@ -2,9 +2,6 @@ #define __NODE_MAPNIK_PLUGINS_H__ -// v8 -#include - // mapnik #include #include @@ -19,28 +16,30 @@ using namespace v8; namespace node_mapnik { -static inline Handle available_input_plugins(const Arguments& args) +static inline NAN_METHOD(available_input_plugins) { - HandleScope scope; + NanScope(); #if MAPNIK_VERSION >= 200200 std::vector names = mapnik::datasource_cache::instance().plugin_names(); #else std::vector names = mapnik::datasource_cache::instance()->plugin_names(); #endif - Local a = Array::New(names.size()); + Local a = NanNew(names.size()); for (unsigned i = 0; i < names.size(); ++i) { - a->Set(i, String::New(names[i].c_str())); + a->Set(i, NanNew(names[i].c_str())); } - return scope.Close(a); + NanReturnValue(a); } -static inline Handle register_datasource(const Arguments& args) +static inline NAN_METHOD(register_datasource) { - HandleScope scope; + NanScope(); if (args.Length() != 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a mapnik input plugin (.input)"))); + { + NanThrowTypeError("first argument must be a path to a mapnik input plugin (.input)"); + NanReturnUndefined(); + } #if MAPNIK_VERSION >= 200200 std::vector names_before = mapnik::datasource_cache::instance().plugin_names(); #else @@ -55,16 +54,18 @@ static inline Handle register_datasource(const Arguments& args) std::vector names_after = mapnik::datasource_cache::instance()->plugin_names(); #endif if (names_after.size() > names_before.size()) - return scope.Close(True()); - return scope.Close(False()); + NanReturnValue(NanTrue()); + NanReturnValue(NanFalse()); } -static inline Handle register_datasources(const Arguments& args) +static inline NAN_METHOD(register_datasources) { - HandleScope scope; + NanScope(); if (args.Length() != 1 || !args[0]->IsString()) - return ThrowException(Exception::TypeError( - String::New("first argument must be a path to a directory of mapnik input plugins"))); + { + NanThrowTypeError("first argument must be a path to a directory of mapnik input plugins"); + NanReturnUndefined(); + } #if MAPNIK_VERSION >= 200200 std::vector names_before = mapnik::datasource_cache::instance().plugin_names(); #else @@ -79,8 +80,8 @@ static inline Handle register_datasources(const Arguments& args) std::vector names_after = mapnik::datasource_cache::instance()->plugin_names(); #endif if (names_after.size() > names_before.size()) - return scope.Close(True()); - return scope.Close(False()); + NanReturnValue(NanTrue()); + NanReturnValue(NanFalse()); } diff --git a/src/mapnik_projection.cpp b/src/mapnik_projection.cpp index 7d66a68088..30d3c88afe 100644 --- a/src/mapnik_projection.cpp +++ b/src/mapnik_projection.cpp @@ -1,4 +1,3 @@ - #include "mapnik_projection.hpp" #include "utils.hpp" @@ -9,16 +8,17 @@ Persistent Projection::constructor; void Projection::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(Projection::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("Projection")); + Local lcons = NanNew(Projection::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("Projection")); - NODE_SET_PROTOTYPE_METHOD(constructor, "forward", forward); - NODE_SET_PROTOTYPE_METHOD(constructor, "inverse", inverse); + NODE_SET_PROTOTYPE_METHOD(lcons, "forward", forward); + NODE_SET_PROTOTYPE_METHOD(lcons, "inverse", inverse); - target->Set(String::NewSymbol("Projection"),constructor->GetFunction()); + target->Set(NanNew("Projection"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } Projection::Projection(std::string const& name) : @@ -31,65 +31,69 @@ Projection::~Projection() bool Projection::HasInstance(Handle val) { + NanScope(); if (!val->IsObject()) return false; - Local obj = val->ToObject(); + Handle obj = val.As(); - if (constructor->HasInstance(obj)) + if (NanNew(constructor)->HasInstance(obj)) return true; return false; } -Handle Projection::New(const Arguments& args) +NAN_METHOD(Projection::New) { - HandleScope scope; + NanScope(); if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args.Length() <= 0 || !args[0]->IsString()) { - return ThrowException(Exception::TypeError( - String::New("please provide a proj4 intialization string"))); + NanThrowTypeError("please provide a proj4 intialization string"); + NanReturnUndefined(); } try { Projection* p = new Projection(TOSTR(args[0])); p->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle Projection::forward(const Arguments& args) +NAN_METHOD(Projection::forward) { - HandleScope scope; - Projection* p = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Projection* p = node::ObjectWrap::Unwrap(args.Holder()); try { - if (args.Length() != 1) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - else - { - if (!args[0]->IsArray()) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - Local a = Local::Cast(args[0]); + if (args.Length() != 1) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } else { + if (!args[0]->IsArray()) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } + Local a = args[0].As(); unsigned int array_length = a->Length(); if (array_length == 2) { double x = a->Get(0)->NumberValue(); double y = a->Get(1)->NumberValue(); p->projection_->forward(x,y); - Local arr = Array::New(2); - arr->Set(0, Number::New(x)); - arr->Set(1, Number::New(y)); - return scope.Close(arr); + Local arr = NanNew(2); + arr->Set(0, NanNew(x)); + arr->Set(1, NanNew(y)); + NanReturnValue(arr); } else if (array_length == 4) { @@ -99,49 +103,51 @@ Handle Projection::forward(const Arguments& args) double maxy = a->Get(3)->NumberValue(); p->projection_->forward(minx,miny); p->projection_->forward(maxx,maxy); - Local arr = Array::New(4); - arr->Set(0, Number::New(minx)); - arr->Set(1, Number::New(miny)); - arr->Set(2, Number::New(maxx)); - arr->Set(3, Number::New(maxy)); - return scope.Close(arr); + Local arr = NanNew(4); + arr->Set(0, NanNew(minx)); + arr->Set(1, NanNew(miny)); + arr->Set(2, NanNew(maxx)); + arr->Set(3, NanNew(maxy)); + NanReturnValue(arr); } else - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); + { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } } } catch (std::exception const & ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle Projection::inverse(const Arguments& args) +NAN_METHOD(Projection::inverse) { - HandleScope scope; - Projection* p = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + Projection* p = node::ObjectWrap::Unwrap(args.Holder()); try { - if (args.Length() != 1) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - else - { - if (!args[0]->IsArray()) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - Local a = Local::Cast(args[0]); + if (args.Length() != 1) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } else { + if (!args[0]->IsArray()) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } + Local a = args[0].As(); unsigned int array_length = a->Length(); if (array_length == 2) { double x = a->Get(0)->NumberValue(); double y = a->Get(1)->NumberValue(); p->projection_->inverse(x,y); - Local arr = Array::New(2); - arr->Set(0, Number::New(x)); - arr->Set(1, Number::New(y)); - return scope.Close(arr); + Local arr = NanNew(2); + arr->Set(0, NanNew(x)); + arr->Set(1, NanNew(y)); + NanReturnValue(arr); } else if (array_length == 4) { @@ -151,20 +157,22 @@ Handle Projection::inverse(const Arguments& args) double maxy = a->Get(3)->NumberValue(); p->projection_->inverse(minx,miny); p->projection_->inverse(maxx,maxy); - Local arr = Array::New(4); - arr->Set(0, Number::New(minx)); - arr->Set(1, Number::New(miny)); - arr->Set(2, Number::New(maxx)); - arr->Set(3, Number::New(maxy)); - return scope.Close(arr); + Local arr = NanNew(4); + arr->Set(0, NanNew(minx)); + arr->Set(1, NanNew(miny)); + arr->Set(2, NanNew(maxx)); + arr->Set(3, NanNew(maxy)); + NanReturnValue(arr); } else - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); + { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } } } catch (std::exception const & ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } @@ -172,16 +180,17 @@ Persistent ProjTransform::constructor; void ProjTransform::Initialize(Handle target) { - HandleScope scope; + NanScope(); - constructor = Persistent::New(FunctionTemplate::New(ProjTransform::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("ProjTransform")); + Local lcons = NanNew(ProjTransform::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("ProjTransform")); - NODE_SET_PROTOTYPE_METHOD(constructor, "forward", forward); - NODE_SET_PROTOTYPE_METHOD(constructor, "backward", backward); + NODE_SET_PROTOTYPE_METHOD(lcons, "forward", forward); + NODE_SET_PROTOTYPE_METHOD(lcons, "backward", backward); - target->Set(String::NewSymbol("ProjTransform"),constructor->GetFunction()); + target->Set(NanNew("ProjTransform"), lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } ProjTransform::ProjTransform(mapnik::projection const& src, @@ -193,24 +202,30 @@ ProjTransform::~ProjTransform() { } -Handle ProjTransform::New(const Arguments& args) +NAN_METHOD(ProjTransform::New) { - HandleScope scope; - if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + NanScope(); + if (!args.IsConstructCall()) { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("please provide two arguments: a pair of mapnik.Projection objects"))); + NanThrowTypeError("please provide two arguments: a pair of mapnik.Projection objects"); + NanReturnUndefined(); } - Local src_obj = args[0]->ToObject(); - if (src_obj->IsNull() || src_obj->IsUndefined() || !Projection::HasInstance(src_obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Projection expected for first argument"))); + Local src_obj = args[0].As(); + if (src_obj->IsNull() || src_obj->IsUndefined() || !Projection::HasInstance(src_obj)) { + NanThrowTypeError("mapnik.Projection expected for first argument"); + NanReturnUndefined(); + } - Local dest_obj = args[1]->ToObject(); - if (dest_obj->IsNull() || dest_obj->IsUndefined() || !Projection::HasInstance(dest_obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Projection expected for second argument"))); + Local dest_obj = args[1].As(); + if (dest_obj->IsNull() || dest_obj->IsUndefined() || !Projection::HasInstance(dest_obj)) { + NanThrowTypeError("mapnik.Projection expected for second argument"); + NanReturnUndefined(); + } Projection *p1 = node::ObjectWrap::Unwrap(src_obj); Projection *p2 = node::ObjectWrap::Unwrap(dest_obj); @@ -219,30 +234,30 @@ Handle ProjTransform::New(const Arguments& args) { ProjTransform* p = new ProjTransform(*p1->get(),*p2->get()); p->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle ProjTransform::forward(const Arguments& args) +NAN_METHOD(ProjTransform::forward) { - HandleScope scope; - ProjTransform* p = node::ObjectWrap::Unwrap(args.This()); - - if (args.Length() != 1) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - else - { - if (!args[0]->IsArray()) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); + NanScope(); + ProjTransform* p = node::ObjectWrap::Unwrap(args.Holder()); + + if (args.Length() != 1) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } else { + if (!args[0]->IsArray()) { + NanThrowTypeError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } - Local a = Local::Cast(args[0]); + Local a = args[0].As(); unsigned int array_length = a->Length(); if (array_length == 2) { @@ -254,14 +269,14 @@ Handle ProjTransform::forward(const Arguments& args) std::ostringstream s; s << "Failed to forward project " << a->Get(0)->NumberValue() << "," << a->Get(1)->NumberValue() << " from " << p->this_->source().params() << " to " << p->this_->dest().params(); - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } - Local arr = Array::New(2); - arr->Set(0, Number::New(x)); - arr->Set(1, Number::New(y)); - return scope.Close(arr); + Local arr = NanNew(2); + arr->Set(0, NanNew(x)); + arr->Set(1, NanNew(y)); + NanReturnValue(arr); } else if (array_length == 4) { @@ -274,41 +289,40 @@ Handle ProjTransform::forward(const Arguments& args) std::ostringstream s; s << "Failed to forward project " << box << " from " << p->this_->source().params() << " to " << p->this_->dest().params(); - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); - + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } - Local arr = Array::New(4); - arr->Set(0, Number::New(box.minx())); - arr->Set(1, Number::New(box.miny())); - arr->Set(2, Number::New(box.maxx())); - arr->Set(3, Number::New(box.maxy())); - return scope.Close(arr); + Local arr = NanNew(4); + arr->Set(0, NanNew(box.minx())); + arr->Set(1, NanNew(box.miny())); + arr->Set(2, NanNew(box.maxx())); + arr->Set(3, NanNew(box.maxy())); + NanReturnValue(arr); } else - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - + { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } } } -Handle ProjTransform::backward(const Arguments& args) +NAN_METHOD(ProjTransform::backward) { - HandleScope scope; - ProjTransform* p = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + ProjTransform* p = node::ObjectWrap::Unwrap(args.Holder()); - if (args.Length() != 1) - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - else - { + if (args.Length() != 1) { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } else { if (!args[0]->IsArray()) { - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); + NanThrowTypeError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); } - Local a = Local::Cast(args[0]); + Local a = args[0].As(); unsigned int array_length = a->Length(); if (array_length == 2) { @@ -320,14 +334,13 @@ Handle ProjTransform::backward(const Arguments& args) std::ostringstream s; s << "Failed to back project " << a->Get(0)->NumberValue() << "," << a->Get(1)->NumberValue() << " from " << p->this_->dest().params() << " to: " << p->this_->source().params(); - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); - + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } - Local arr = Array::New(2); - arr->Set(0, Number::New(x)); - arr->Set(1, Number::New(y)); - return scope.Close(arr); + Local arr = NanNew(2); + arr->Set(0, NanNew(x)); + arr->Set(1, NanNew(y)); + NanReturnValue(arr); } else if (array_length == 4) { @@ -340,20 +353,20 @@ Handle ProjTransform::backward(const Arguments& args) std::ostringstream s; s << "Failed to back project " << box << " from " << p->this_->source().params() << " to " << p->this_->dest().params(); - return ThrowException(Exception::Error( - String::New(s.str().c_str()))); - + NanThrowError(s.str().c_str()); + NanReturnUndefined(); } - Local arr = Array::New(4); - arr->Set(0, Number::New(box.minx())); - arr->Set(1, Number::New(box.miny())); - arr->Set(2, Number::New(box.maxx())); - arr->Set(3, Number::New(box.maxy())); - return scope.Close(arr); + Local arr = NanNew(4); + arr->Set(0, NanNew(box.minx())); + arr->Set(1, NanNew(box.miny())); + arr->Set(2, NanNew(box.maxx())); + arr->Set(3, NanNew(box.maxy())); + NanReturnValue(arr); } else - return ThrowException(Exception::Error( - String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"))); - + { + NanThrowError("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]"); + NanReturnUndefined(); + } } } diff --git a/src/mapnik_projection.hpp b/src/mapnik_projection.hpp index d76504479a..0ec2248782 100644 --- a/src/mapnik_projection.hpp +++ b/src/mapnik_projection.hpp @@ -1,8 +1,7 @@ #ifndef __NODE_MAPNIK_PROJECTION_H__ #define __NODE_MAPNIK_PROJECTION_H__ -#include -#include +#include // boost #include "mapnik3x_compatibility.hpp" @@ -19,10 +18,10 @@ class Projection: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle inverse(const Arguments& args); - static Handle forward(const Arguments& args); + static NAN_METHOD(inverse); + static NAN_METHOD(forward); static bool HasInstance(Handle val); explicit Projection(std::string const& name); @@ -40,10 +39,10 @@ class ProjTransform: public node::ObjectWrap { public: static Persistent constructor; static void Initialize(Handle target); - static Handle New(const Arguments &args); + static NAN_METHOD(New); - static Handle forward(const Arguments& args); - static Handle backward(const Arguments& args); + static NAN_METHOD(forward); + static NAN_METHOD(backward); ProjTransform(mapnik::projection const& src, mapnik::projection const& dest); diff --git a/src/mapnik_vector_tile.cpp b/src/mapnik_vector_tile.cpp index 11082d4462..049e2f85fa 100644 --- a/src/mapnik_vector_tile.cpp +++ b/src/mapnik_vector_tile.cpp @@ -1,7 +1,3 @@ -#include "node.h" // for NODE_SET_PROTOTYPE_METHOD, etc -#include -#include - #include "utils.hpp" #include "mapnik_map.hpp" #include "mapnik_image.hpp" @@ -141,36 +137,38 @@ bool _hit_test(PathType & path, double x, double y, double tol, double & distanc Persistent VectorTile::constructor; void VectorTile::Initialize(Handle target) { - HandleScope scope; - constructor = Persistent::New(FunctionTemplate::New(VectorTile::New)); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("VectorTile")); - NODE_SET_PROTOTYPE_METHOD(constructor, "render", render); - NODE_SET_PROTOTYPE_METHOD(constructor, "setData", setData); - NODE_SET_PROTOTYPE_METHOD(constructor, "setDataSync", setDataSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "getData", getData); - NODE_SET_PROTOTYPE_METHOD(constructor, "parse", parse); - NODE_SET_PROTOTYPE_METHOD(constructor, "parseSync", parseSync); - NODE_SET_PROTOTYPE_METHOD(constructor, "addData", addData); - NODE_SET_PROTOTYPE_METHOD(constructor, "composite", composite); - NODE_SET_PROTOTYPE_METHOD(constructor, "query", query); - NODE_SET_PROTOTYPE_METHOD(constructor, "names", names); - NODE_SET_PROTOTYPE_METHOD(constructor, "toJSON", toJSON); - NODE_SET_PROTOTYPE_METHOD(constructor, "toGeoJSON", toGeoJSON); - NODE_SET_PROTOTYPE_METHOD(constructor, "fromGeoJSON", fromGeoJSON); + NanScope(); + + Local lcons = NanNew(VectorTile::New); + lcons->InstanceTemplate()->SetInternalFieldCount(1); + lcons->SetClassName(NanNew("VectorTile")); + NODE_SET_PROTOTYPE_METHOD(lcons, "render", render); + NODE_SET_PROTOTYPE_METHOD(lcons, "setData", setData); + NODE_SET_PROTOTYPE_METHOD(lcons, "setDataSync", setDataSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "getData", getData); + NODE_SET_PROTOTYPE_METHOD(lcons, "parse", parse); + NODE_SET_PROTOTYPE_METHOD(lcons, "parseSync", parseSync); + NODE_SET_PROTOTYPE_METHOD(lcons, "addData", addData); + NODE_SET_PROTOTYPE_METHOD(lcons, "composite", composite); + NODE_SET_PROTOTYPE_METHOD(lcons, "query", query); + NODE_SET_PROTOTYPE_METHOD(lcons, "names", names); + NODE_SET_PROTOTYPE_METHOD(lcons, "toJSON", toJSON); + NODE_SET_PROTOTYPE_METHOD(lcons, "toGeoJSON", toGeoJSON); + NODE_SET_PROTOTYPE_METHOD(lcons, "fromGeoJSON", fromGeoJSON); #ifdef PROTOBUF_FULL - NODE_SET_PROTOTYPE_METHOD(constructor, "toString", toString); + NODE_SET_PROTOTYPE_METHOD(lcons, "toString", toString); #endif // common to mapnik.Image - NODE_SET_PROTOTYPE_METHOD(constructor, "width", width); - NODE_SET_PROTOTYPE_METHOD(constructor, "height", height); - NODE_SET_PROTOTYPE_METHOD(constructor, "painted", painted); - NODE_SET_PROTOTYPE_METHOD(constructor, "clear", clear); - NODE_SET_PROTOTYPE_METHOD(constructor, "clearSync", clear); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolid", isSolid); - NODE_SET_PROTOTYPE_METHOD(constructor, "isSolidSync", isSolidSync); - target->Set(String::NewSymbol("VectorTile"),constructor->GetFunction()); + NODE_SET_PROTOTYPE_METHOD(lcons, "width", width); + NODE_SET_PROTOTYPE_METHOD(lcons, "height", height); + NODE_SET_PROTOTYPE_METHOD(lcons, "painted", painted); + NODE_SET_PROTOTYPE_METHOD(lcons, "clear", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "clearSync", clear); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolid", isSolid); + NODE_SET_PROTOTYPE_METHOD(lcons, "isSolidSync", isSolidSync); + target->Set(NanNew("VectorTile"),lcons->GetFunction()); + NanAssignPersistent(constructor, lcons); } VectorTile::VectorTile(int z, int x, int y, unsigned w, unsigned h) : @@ -188,11 +186,13 @@ VectorTile::VectorTile(int z, int x, int y, unsigned w, unsigned h) : VectorTile::~VectorTile() { } -Handle VectorTile::New(const Arguments& args) +NAN_METHOD(VectorTile::New) { - HandleScope scope; - if (!args.IsConstructCall()) - return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword")); + NanScope(); + if (!args.IsConstructCall()) { + NanThrowError("Cannot call constructor as function, you need to use 'new' keyword"); + NanReturnUndefined(); + } if (args.Length() >= 3) { @@ -200,34 +200,34 @@ Handle VectorTile::New(const Arguments& args) !args[1]->IsNumber() || !args[2]->IsNumber()) { - return ThrowException(Exception::Error( - String::New("required args (z, x, and y) must be a integers"))); + NanThrowTypeError("required args (z, x, and y) must be a integers"); + NanReturnUndefined(); } unsigned width = 256; unsigned height = 256; - Local options = Object::New(); + Local options = NanNew(); if (args.Length() > 3) { if (!args[3]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("optional fourth argument must be an options object"))); + NanThrowTypeError("optional fourth argument must be an options object"); + NanReturnUndefined(); } options = args[3]->ToObject(); - if (options->Has(String::New("width"))) { - Local opt = options->Get(String::New("width")); + if (options->Has(NanNew("width"))) { + Local opt = options->Get(NanNew("width")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'width' must be a number"))); + NanThrowTypeError("optional arg 'width' must be a number"); + NanReturnUndefined(); } width = opt->IntegerValue(); } - if (options->Has(String::New("height"))) { - Local opt = options->Get(String::New("height")); + if (options->Has(NanNew("height"))) { + Local opt = options->Get(NanNew("height")); if (!opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'height' must be a number"))); + NanThrowTypeError("optional arg 'height' must be a number"); + NanReturnUndefined(); } height = opt->IntegerValue(); } @@ -239,14 +239,14 @@ Handle VectorTile::New(const Arguments& args) width,height); d->Wrap(args.This()); - return args.This(); + NanReturnValue(args.This()); } else { - return ThrowException(Exception::Error( - String::New("please provide a z, x, y"))); + NanThrowError("please provide a z, x, y"); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } std::vector VectorTile::lazy_names() @@ -325,18 +325,18 @@ void VectorTile::parse_proto() } } -Handle VectorTile::composite(const Arguments& args) +NAN_METHOD(VectorTile::composite) { - HandleScope scope; + NanScope(); if (args.Length() < 1 || !args[0]->IsArray()) { - return ThrowException(Exception::TypeError( - String::New("must provide an array of VectorTile objects and an optional options object"))); + NanThrowTypeError("must provide an array of VectorTile objects and an optional options object"); + NanReturnUndefined(); } - Local vtiles = Local::Cast(args[0]); + Local vtiles = args[0].As(); unsigned num_tiles = vtiles->Length(); if (num_tiles < 1) { - return ThrowException(Exception::TypeError( - String::New("must provide an array with at least one VectorTile object and an optional options object"))); + NanThrowTypeError("must provide an array with at least one VectorTile object and an optional options object"); + NanReturnUndefined(); } // options needed for re-rendering tiles @@ -353,94 +353,95 @@ Handle VectorTile::composite(const Arguments& args) mapnik::box2d max_extent(-20037508.34,-20037508.34,20037508.34,20037508.34); std::string merc_srs("+init=epsg:3857"); - Local options = Object::New(); + Local options = NanNew(); if (args.Length() > 1) { // options object if (!args[1]->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("optional second argument must be an options object"))); + NanThrowTypeError("optional second argument must be an options object"); + NanReturnUndefined(); } options = args[1]->ToObject(); - if (options->Has(String::New("path_multiplier"))) { + if (options->Has(NanNew("path_multiplier"))) { - Local param_val = options->Get(String::New("path_multiplier")); + Local param_val = options->Get(NanNew("path_multiplier")); if (!param_val->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("option 'path_multiplier' must be an unsigned integer"))); + NanThrowTypeError("option 'path_multiplier' must be an unsigned integer"); + NanReturnUndefined(); } path_multiplier = param_val->NumberValue(); } - if (options->Has(String::NewSymbol("tolerance"))) + if (options->Has(NanNew("tolerance"))) { - Local tol = options->Get(String::New("tolerance")); + Local tol = options->Get(NanNew("tolerance")); if (!tol->IsNumber()) { - return ThrowException(Exception::TypeError(String::New("tolerance value must be a number"))); + NanThrowTypeError("tolerance value must be a number"); + NanReturnUndefined(); } tolerance = tol->NumberValue(); } - if (options->Has(String::New("buffer_size"))) { - Local bind_opt = options->Get(String::New("buffer_size")); + if (options->Has(NanNew("buffer_size"))) { + Local bind_opt = options->Get(NanNew("buffer_size")); if (!bind_opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'buffer_size' must be a number"))); + NanThrowTypeError("optional arg 'buffer_size' must be a number"); + NanReturnUndefined(); } buffer_size = bind_opt->IntegerValue(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); if (!bind_opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); } scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::NewSymbol("scale_denominator"))) + if (options->Has(NanNew("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); + Local bind_opt = options->Get(NanNew("scale_denominator")); if (!bind_opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); } scale_denominator = bind_opt->NumberValue(); } - if (options->Has(String::New("offset_x"))) { - Local bind_opt = options->Get(String::New("offset_x")); + if (options->Has(NanNew("offset_x"))) { + Local bind_opt = options->Get(NanNew("offset_x")); if (!bind_opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'offset_x' must be a number"))); + NanThrowTypeError("optional arg 'offset_x' must be a number"); + NanReturnUndefined(); } offset_x = bind_opt->IntegerValue(); } - if (options->Has(String::New("offset_y"))) { - Local bind_opt = options->Get(String::New("offset_y")); + if (options->Has(NanNew("offset_y"))) { + Local bind_opt = options->Get(NanNew("offset_y")); if (!bind_opt->IsNumber()) { - return ThrowException(Exception::TypeError( - String::New("optional arg 'offset_y' must be a number"))); + NanThrowTypeError("optional arg 'offset_y' must be a number"); + NanReturnUndefined(); } offset_y = bind_opt->IntegerValue(); } } - VectorTile* target_vt = node::ObjectWrap::Unwrap(args.This()); + VectorTile* target_vt = node::ObjectWrap::Unwrap(args.Holder()); for (unsigned i=0;i < num_tiles;++i) { Local val = vtiles->Get(i); if (!val->IsObject()) { - return ThrowException(Exception::TypeError( - String::New("must provide an array of VectorTile objects"))); + NanThrowTypeError("must provide an array of VectorTile objects"); + NanReturnUndefined(); } Local tile_obj = val->ToObject(); - if (tile_obj->IsNull() || tile_obj->IsUndefined() || !VectorTile::constructor->HasInstance(tile_obj)) { - return ThrowException(Exception::TypeError( - String::New("must provide an array of VectorTile objects"))); + if (tile_obj->IsNull() || tile_obj->IsUndefined() || !NanNew(VectorTile::constructor)->HasInstance(tile_obj)) { + NanThrowTypeError("must provide an array of VectorTile objects"); + NanReturnUndefined(); } VectorTile* vt = node::ObjectWrap::Unwrap(tile_obj); // TODO - handle name clashes @@ -459,7 +460,7 @@ Handle VectorTile::composite(const Arguments& args) mapnik::vector::tile new_tiledata; backend_type backend(new_tiledata, path_multiplier); - + // get mercator extent of target tile mapnik::vector::spherical_mercator merc(target_vt->width()); double minx,miny,maxx,maxy; @@ -550,108 +551,111 @@ Handle VectorTile::composite(const Arguments& args) std::string new_message; if (!new_tiledata.SerializeToString(&new_message)) { - return ThrowException(Exception::Error( - String::New("could not serialize new data for vt"))); + NanThrowError("could not serialize new data for vt"); + NanReturnUndefined(); } target_vt->buffer_.append(new_message.data(),new_message.size()); target_vt->status_ = VectorTile::LAZY_MERGE; } } - return scope.Close(Undefined()); + NanReturnUndefined(); } #ifdef PROTOBUF_FULL -Handle VectorTile::toString(const Arguments& args) +NAN_METHOD(VectorTile::toString) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); mapnik::vector::tile const& tiledata = d->get_tile(); - return scope.Close(String::New(tiledata.DebugString().c_str())); + NanReturnValue(NanNew(tiledata.DebugString().c_str())); } #endif -Handle VectorTile::names(const Arguments& args) +NAN_METHOD(VectorTile::names) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); int raw_size = d->buffer_.size(); if (d->byte_size_ <= raw_size) { std::vector names = d->lazy_names(); - Local arr = Array::New(names.size()); + Local arr = NanNew(names.size()); unsigned idx = 0; BOOST_FOREACH ( std::string const& name, names ) { - arr->Set(idx++,String::New(name.c_str())); + arr->Set(idx++,NanNew(name.c_str())); } - return scope.Close(arr); + NanReturnValue(arr); } else { mapnik::vector::tile const& tiledata = d->get_tile(); - Local arr = Array::New(tiledata.layers_size()); + Local arr = NanNew(tiledata.layers_size()); for (int i=0; i < tiledata.layers_size(); ++i) { mapnik::vector::tile_layer const& layer = tiledata.layers(i); - arr->Set(i, String::New(layer.name().c_str())); + arr->Set(i, NanNew(layer.name().c_str())); } - return scope.Close(arr); + NanReturnValue(arr); } - return scope.Close(Undefined()); + NanReturnUndefined(); } -Handle VectorTile::width(const Arguments& args) +NAN_METHOD(VectorTile::width) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(d->width())); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(d->width())); } -Handle VectorTile::height(const Arguments& args) +NAN_METHOD(VectorTile::height) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Integer::New(d->height())); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(d->height())); } -Handle VectorTile::painted(const Arguments& args) +NAN_METHOD(VectorTile::painted) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); - return scope.Close(Boolean::New(d->painted())); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); + NanReturnValue(NanNew(d->painted())); } -Handle VectorTile::query(const Arguments& args) +NAN_METHOD(VectorTile::query) { - HandleScope scope; + NanScope(); if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsNumber()) { - return ThrowException(Exception::Error( - String::New("expects lon,lat args"))); + NanThrowError("expects lon,lat args"); + NanReturnUndefined(); } double tolerance = 0.0; // meters std::string layer_name(""); if (args.Length() > 2) { - Local options = Object::New(); + Local options = NanNew(); if (!args[2]->IsObject()) { - return ThrowException(Exception::TypeError(String::New("optional third argument must be an options object"))); + NanThrowTypeError("optional third argument must be an options object"); + NanReturnUndefined(); } options = args[2]->ToObject(); - if (options->Has(String::NewSymbol("tolerance"))) + if (options->Has(NanNew("tolerance"))) { - Local tol = options->Get(String::New("tolerance")); + Local tol = options->Get(NanNew("tolerance")); if (!tol->IsNumber()) { - return ThrowException(Exception::TypeError(String::New("tolerance value must be a number"))); + NanThrowTypeError("tolerance value must be a number"); + NanReturnUndefined(); } tolerance = tol->NumberValue(); } - if (options->Has(String::NewSymbol("layer"))) + if (options->Has(NanNew("layer"))) { - Local layer_id = options->Get(String::New("layer")); + Local layer_id = options->Get(NanNew("layer")); if (!layer_id->IsString()) { - return ThrowException(Exception::TypeError(String::New("layer value must be a string"))); + NanThrowTypeError("layer value must be a string"); + NanReturnUndefined(); } layer_name = TOSTR(layer_id); } @@ -659,7 +663,7 @@ Handle VectorTile::query(const Arguments& args) double lon = args[0]->NumberValue(); double lat = args[1]->NumberValue(); - Local arr = Array::New(); + Local arr = NanNew(); try { mapnik::projection wgs84("+init=epsg:4326"); mapnik::projection merc("+init=epsg:3857"); @@ -669,10 +673,10 @@ Handle VectorTile::query(const Arguments& args) double z = 0; if (!tr.forward(x,y,z)) { - return ThrowException(Exception::Error( - String::New("could not reproject lon/lat to mercator"))); + NanThrowError("could not reproject lon/lat to mercator"); + NanReturnUndefined(); } - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); mapnik::vector::tile const& tiledata = d->get_tile(); mapnik::coord2d pt(x,y); unsigned idx = 0; @@ -719,8 +723,8 @@ Handle VectorTile::query(const Arguments& args) { Handle feat = Feature::New(feature); Local feat_obj = feat->ToObject(); - feat_obj->Set(String::New("layer"),String::New(layer.name().c_str())); - feat_obj->Set(String::New("distance"),Number::New(distance)); + feat_obj->Set(NanNew("layer"),NanNew(layer.name().c_str())); + feat_obj->Set(NanNew("distance"),NanNew(distance)); arr->Set(idx++,feat); } } @@ -760,8 +764,8 @@ Handle VectorTile::query(const Arguments& args) { Handle feat = Feature::New(feature); Local feat_obj = feat->ToObject(); - feat_obj->Set(String::New("layer"),String::New(layer.name().c_str())); - feat_obj->Set(String::New("distance"),Number::New(distance)); + feat_obj->Set(NanNew("layer"),NanNew(layer.name().c_str())); + feat_obj->Set(NanNew("distance"),NanNew(distance)); arr->Set(idx++,feat); } } @@ -771,40 +775,40 @@ Handle VectorTile::query(const Arguments& args) } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return scope.Close(arr); + NanReturnValue(arr); } -Handle VectorTile::toJSON(const Arguments& args) +NAN_METHOD(VectorTile::toJSON) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); mapnik::vector::tile const& tiledata = d->get_tile(); - Local arr = Array::New(tiledata.layers_size()); + Local arr = NanNew(tiledata.layers_size()); for (int i=0; i < tiledata.layers_size(); ++i) { mapnik::vector::tile_layer const& layer = tiledata.layers(i); - Local layer_obj = Object::New(); - layer_obj->Set(String::NewSymbol("name"), String::New(layer.name().c_str())); - layer_obj->Set(String::NewSymbol("extent"), Integer::New(layer.extent())); - layer_obj->Set(String::NewSymbol("version"), Integer::New(layer.version())); + Local layer_obj = NanNew(); + layer_obj->Set(NanNew("name"), NanNew(layer.name().c_str())); + layer_obj->Set(NanNew("extent"), NanNew(layer.extent())); + layer_obj->Set(NanNew("version"), NanNew(layer.version())); - Local f_arr = Array::New(layer.features_size()); + Local f_arr = NanNew(layer.features_size()); for (int j=0; j < layer.features_size(); ++j) { - Local feature_obj = Object::New(); + Local feature_obj = NanNew(); mapnik::vector::tile_feature const& f = layer.features(j); - feature_obj->Set(String::NewSymbol("id"),Number::New(f.id())); - feature_obj->Set(String::NewSymbol("type"),Integer::New(f.type())); - Local g_arr = Array::New(); + feature_obj->Set(NanNew("id"),NanNew(f.id())); + feature_obj->Set(NanNew("type"),NanNew(f.type())); + Local g_arr = NanNew(); for (int k = 0; k < f.geometry_size();++k) { - g_arr->Set(k,Number::New(f.geometry(k))); + g_arr->Set(k,NanNew(f.geometry(k))); } - feature_obj->Set(String::NewSymbol("geometry"),g_arr); - Local att_obj = Object::New(); + feature_obj->Set(NanNew("geometry"),g_arr); + Local att_obj = NanNew(); for (int m = 0; m < f.tags_size(); m += 2) { std::size_t key_name = f.tags(m); @@ -816,46 +820,46 @@ Handle VectorTile::toJSON(const Arguments& args) mapnik::vector::tile_value const& value = layer.values(key_value); if (value.has_string_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), String::New(value.string_value().c_str())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.string_value().c_str())); } else if (value.has_int_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.int_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.int_value())); } else if (value.has_double_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.double_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.double_value())); } else if (value.has_float_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.float_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.float_value())); } else if (value.has_bool_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Boolean::New(value.bool_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.bool_value())); } else if (value.has_sint_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.sint_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.sint_value())); } else if (value.has_uint_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.uint_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.uint_value())); } else { - att_obj->Set(String::NewSymbol(name.c_str()), Undefined()); + att_obj->Set(NanNew(name.c_str()), NanUndefined()); } } - feature_obj->Set(String::NewSymbol("properties"),att_obj); + feature_obj->Set(NanNew("properties"),att_obj); } f_arr->Set(j,feature_obj); } - layer_obj->Set(String::NewSymbol("features"), f_arr); + layer_obj->Set(NanNew("features"), f_arr); arr->Set(i, layer_obj); } - return scope.Close(arr); + NanReturnValue(arr); } static void layer_to_geojson(mapnik::vector::tile_layer const& layer, @@ -876,27 +880,27 @@ static void layer_to_geojson(mapnik::vector::tile_layer const& layer, for (int j=0; j < layer.features_size(); ++j) { double scale_ = (static_cast(layer.extent()) / width) * static_cast(width)/resolution; - Local feature_obj = Object::New(); - feature_obj->Set(String::NewSymbol("type"),String::New("Feature")); - Local geometry = Object::New(); + Local feature_obj = NanNew(); + feature_obj->Set(NanNew("type"),NanNew("Feature")); + Local geometry = NanNew(); mapnik::vector::tile_feature const& f = layer.features(j); unsigned int g_type = f.type(); - Local js_type = String::New("Unknown"); + Local js_type = NanNew("Unknown"); switch (g_type) { case MAPNIK_POINT: { - js_type = String::New("Point"); + js_type = NanNew("Point"); break; } case MAPNIK_LINESTRING: { - js_type = String::New("LineString"); + js_type = NanNew("LineString"); break; } case MAPNIK_POLYGON: { - js_type = String::New("Polygon"); + js_type = NanNew("Polygon"); break; } default: @@ -904,17 +908,17 @@ static void layer_to_geojson(mapnik::vector::tile_layer const& layer, break; } } - geometry->Set(String::NewSymbol("type"),js_type); - Local g_arr = Array::New(); + geometry->Set(NanNew("type"), js_type); + Local g_arr = NanNew(); if (g_type == MAPNIK_POLYGON) { - Local enclosing_array = Array::New(1); + Local enclosing_array = NanNew(1); enclosing_array->Set(0,g_arr); - geometry->Set(String::NewSymbol("coordinates"),enclosing_array); + geometry->Set(NanNew("coordinates"),enclosing_array); } else { - geometry->Set(String::NewSymbol("coordinates"),g_arr); + geometry->Set(NanNew("coordinates"),g_arr); } int cmd = -1; const int cmd_bits = 3; @@ -945,14 +949,14 @@ static void layer_to_geojson(mapnik::vector::tile_layer const& layer, { if (g_type == MAPNIK_POINT) { - g_arr->Set(0,Number::New(x2)); - g_arr->Set(1,Number::New(y2)); + g_arr->Set(0,NanNew(x2)); + g_arr->Set(1,NanNew(y2)); } else { - Local v_arr = Array::New(2); - v_arr->Set(0,Number::New(x2)); - v_arr->Set(1,Number::New(y2)); + Local v_arr = NanNew(2); + v_arr->Set(0,NanNew(x2)); + v_arr->Set(1,NanNew(y2)); g_arr->Set(idx++,v_arr); } } @@ -974,13 +978,13 @@ static void layer_to_geojson(mapnik::vector::tile_layer const& layer, } } } - feature_obj->Set(String::NewSymbol("geometry"),geometry); - Local att_obj = Object::New(); + feature_obj->Set(NanNew("geometry"),geometry); + Local att_obj = NanNew(); for (int m = 0; m < f.tags_size(); m += 2) { std::size_t key_name = f.tags(m); std::size_t key_value = f.tags(m + 1); - + if (key_name < static_cast(layer.keys_size()) && key_value < static_cast(layer.values_size())) { @@ -988,55 +992,57 @@ static void layer_to_geojson(mapnik::vector::tile_layer const& layer, mapnik::vector::tile_value const& value = layer.values(key_value); if (value.has_string_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), String::New(value.string_value().c_str())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.string_value().c_str())); } else if (value.has_int_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.int_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.int_value())); } else if (value.has_double_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.double_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.double_value())); } else if (value.has_float_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.float_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.float_value())); } else if (value.has_bool_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Boolean::New(value.bool_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.bool_value())); } else if (value.has_sint_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.sint_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.sint_value())); } else if (value.has_uint_value()) { - att_obj->Set(String::NewSymbol(name.c_str()), Number::New(value.uint_value())); + att_obj->Set(NanNew(name.c_str()), NanNew(value.uint_value())); } else { - att_obj->Set(String::NewSymbol(name.c_str()), Undefined()); + att_obj->Set(NanNew(name.c_str()), NanUndefined()); } } } - feature_obj->Set(String::NewSymbol("properties"),att_obj); + feature_obj->Set(NanNew("properties"),att_obj); f_arr->Set(j+idx0,feature_obj); } } -Handle VectorTile::toGeoJSON(const Arguments& args) +NAN_METHOD(VectorTile::toGeoJSON) { - HandleScope scope; - if (args.Length() < 1) - return ThrowException(Exception::Error( - String::New("first argument must be either a layer name (string) or layer index (integer)"))); + NanScope(); + if (args.Length() < 1) { + NanThrowError("first argument must be either a layer name (string) or layer index (integer)"); + NanReturnUndefined(); + } Local layer_id = args[0]; - if (! (layer_id->IsString() || layer_id->IsNumber()) ) - return ThrowException(Exception::TypeError( - String::New("'layer' argument must be either a layer name (string) or layer index (integer)"))); + if (! (layer_id->IsString() || layer_id->IsNumber()) ) { + NanThrowTypeError("'layer' argument must be either a layer name (string) or layer index (integer)"); + NanReturnUndefined(); + } - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); mapnik::vector::tile const& tiledata = d->get_tile(); std::size_t layer_num = tiledata.layers_size(); int layer_idx = -1; @@ -1072,7 +1078,8 @@ Handle VectorTile::toGeoJSON(const Arguments& args) { std::ostringstream s; s << "Layer name '" << layer_name << "' not found"; - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } } @@ -1091,7 +1098,8 @@ Handle VectorTile::toGeoJSON(const Arguments& args) { s << "no layers found in map"; } - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } else if (layer_idx >= static_cast(layer_num)) { std::ostringstream s; s << "Zero-based layer index '" << layer_idx << "' not valid, "; @@ -1103,36 +1111,38 @@ Handle VectorTile::toGeoJSON(const Arguments& args) { s << "no layers found in map"; } - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else { - return ThrowException(Exception::TypeError(String::New("layer id must be a string or index number"))); + NanThrowTypeError("layer id must be a string or index number"); + NanReturnUndefined(); } try { if (all_array) { - Local layer_arr = Array::New(layer_num); + Local layer_arr = NanNew(layer_num); for (unsigned i=0;i layer_obj = Object::New(); - layer_obj->Set(String::NewSymbol("type"), String::New("FeatureCollection")); - Local f_arr = Array::New(); - layer_obj->Set(String::NewSymbol("features"), f_arr); + Local layer_obj = NanNew(); + layer_obj->Set(NanNew("type"), NanNew("FeatureCollection")); + Local f_arr = NanNew(); + layer_obj->Set(NanNew("features"), f_arr); mapnik::vector::tile_layer const& layer = tiledata.layers(i); - layer_obj->Set(String::NewSymbol("name"), String::New(layer.name().c_str())); + layer_obj->Set(NanNew("name"), NanNew(layer.name().c_str())); layer_to_geojson(layer,f_arr,d->x_,d->y_,d->z_,d->width_,0); layer_arr->Set(i,layer_obj); } - return scope.Close(layer_arr); + NanReturnValue(layer_arr); } else { - Local layer_obj = Object::New(); - layer_obj->Set(String::NewSymbol("type"), String::New("FeatureCollection")); - Local f_arr = Array::New(); - layer_obj->Set(String::NewSymbol("features"), f_arr); + Local layer_obj = NanNew(); + layer_obj->Set(NanNew("type"), NanNew("FeatureCollection")); + Local f_arr = NanNew(); + layer_obj->Set(NanNew("features"), f_arr); if (all_flattened) { for (unsigned i=0;i VectorTile::toGeoJSON(const Arguments& args) mapnik::vector::tile_layer const& layer = tiledata.layers(i); layer_to_geojson(layer,f_arr,d->x_,d->y_,d->z_,d->width_,f_arr->Length()); } - return scope.Close(layer_obj); + NanReturnValue(layer_obj); } else { mapnik::vector::tile_layer const& layer = tiledata.layers(layer_idx); - layer_obj->Set(String::NewSymbol("name"), String::New(layer.name().c_str())); + layer_obj->Set(NanNew("name"), NanNew(layer.name().c_str())); layer_to_geojson(layer,f_arr,d->x_,d->y_,d->z_,d->width_,0); - return scope.Close(layer_obj); + NanReturnValue(layer_obj); } } } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle VectorTile::parseSync(const Arguments& args) +NAN_METHOD(VectorTile::parseSync) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + NanReturnValue(_parseSync(args)); +} + +Local VectorTile::_parseSync(_NAN_METHOD_ARGS) +{ + NanEscapableScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); try { d->parse_proto(); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + return NanEscapeScope(NanUndefined()); } - return Undefined(); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -1182,28 +1198,29 @@ typedef struct { Persistent cb; } vector_tile_parse_baton_t; -Handle VectorTile::parse(const Arguments& args) +NAN_METHOD(VectorTile::parse) { - HandleScope scope; + NanScope(); if (args.Length() == 0) { - return parseSync(args); + NanReturnValue(_parseSync(args)); } // ensure callback is a function Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + if (!args[args.Length()-1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); vector_tile_parse_baton_t *closure = new vector_tile_parse_baton_t(); closure->request.data = closure; closure->d = d; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Parse, (uv_after_work_cb)EIO_AfterParse); d->Ref(); - return Undefined(); + NanReturnUndefined(); } void VectorTile::EIO_Parse(uv_work_t* req) @@ -1222,36 +1239,35 @@ void VectorTile::EIO_Parse(uv_work_t* req) void VectorTile::EIO_AfterParse(uv_work_t* req) { - HandleScope scope; + NanScope(); vector_tile_parse_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[1] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); - } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); + Local argv[1] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } + closure->d->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle VectorTile::fromGeoJSON(const Arguments& args) +NAN_METHOD(VectorTile::fromGeoJSON) { - HandleScope scope; - VectorTile* d = ObjectWrap::Unwrap(args.This()); - if (args.Length() < 1 || !args[0]->IsString()) - return ThrowException(Exception::Error( - String::New("first argument must be a GeoJSON string"))); - if (args.Length() < 2 || !args[1]->IsString()) - return ThrowException(Exception::Error( - String::New("second argument must be a layer name (string)"))); + NanScope(); + VectorTile* d = ObjectWrap::Unwrap(args.Holder()); + if (args.Length() < 1 || !args[0]->IsString()) { + NanThrowError("first argument must be a GeoJSON string"); + NanReturnUndefined(); + } + if (args.Length() < 2 || !args[1]->IsString()) { + NanThrowError("second argument must be a layer name (string)"); + NanReturnUndefined(); + } std::string geojson_string = TOSTR(args[0]); std::string geojson_name = TOSTR(args[1]); try @@ -1279,57 +1295,69 @@ Handle VectorTile::fromGeoJSON(const Arguments& args) m_req); ren.apply(); d->painted(ren.painted()); - return True(); + NanReturnValue(NanTrue()); } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } } -Handle VectorTile::addData(const Arguments& args) +NAN_METHOD(VectorTile::addData) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 1 || !args[0]->IsObject()) - return ThrowException(Exception::Error( - String::New("first argument must be a buffer object"))); - Local obj = args[0]->ToObject(); + { + NanThrowError("first argument must be a buffer object"); + NanReturnUndefined(); + } + Local obj = args[0].As(); if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) - return ThrowException(Exception::Error( - String::New("first arg must be a buffer object"))); + { + NanThrowTypeError("first arg must be a buffer object"); + NanReturnUndefined(); + } std::size_t buffer_size = node::Buffer::Length(obj); if (buffer_size <= 0) { - return ThrowException(Exception::Error( - String::New("cannot accept empty buffer as protobuf"))); + NanThrowError("cannot accept empty buffer as protobuf"); + NanReturnUndefined(); } d->buffer_.append(node::Buffer::Data(obj),buffer_size); d->status_ = VectorTile::LAZY_MERGE; - return Undefined(); + NanReturnUndefined(); } -Handle VectorTile::setDataSync(const Arguments& args) +NAN_METHOD(VectorTile::setDataSync) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); - if (args.Length() < 1 || !args[0]->IsObject()) - return ThrowException(Exception::Error( - String::New("first argument must be a buffer object"))); + NanScope(); + NanReturnValue(_setDataSync(args)); +} + +Local VectorTile::_setDataSync(_NAN_METHOD_ARGS) +{ + NanEscapableScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); + if (args.Length() < 1 || !args[0]->IsObject()) { + NanThrowTypeError("first argument must be a buffer object"); + return NanEscapeScope(NanUndefined()); + } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) - return ThrowException(Exception::Error( - String::New("first arg must be a buffer object"))); + if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) { + NanThrowTypeError("first arg must be a buffer object"); + return NanEscapeScope(NanUndefined()); + } std::size_t buffer_size = node::Buffer::Length(obj); if (buffer_size <= 0) { - return ThrowException(Exception::Error( - String::New("cannot accept empty buffer as protobuf"))); + NanThrowError("cannot accept empty buffer as protobuf"); + return NanEscapeScope(NanUndefined()); } d->buffer_ = std::string(node::Buffer::Data(obj),buffer_size); d->status_ = VectorTile::LAZY_SET; - return Undefined(); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -1342,29 +1370,32 @@ typedef struct { Persistent cb; } vector_tile_setdata_baton_t; -Handle VectorTile::setData(const Arguments& args) +NAN_METHOD(VectorTile::setData) { - HandleScope scope; + NanScope(); if (args.Length() == 1) { - return setDataSync(args); + NanReturnValue(_setDataSync(args)); } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!args[args.Length() - 1]->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } - if (args.Length() < 1 || !args[0]->IsObject()) - return ThrowException(Exception::Error( - String::New("first argument must be a buffer object"))); + if (args.Length() < 1 || !args[0]->IsObject()) { + NanThrowTypeError("first argument must be a buffer object"); + NanReturnUndefined(); + } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) - return ThrowException(Exception::Error( - String::New("first arg must be a buffer object"))); + if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) { + NanThrowTypeError("first arg must be a buffer object"); + NanReturnUndefined(); + } - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); vector_tile_setdata_baton_t *closure = new vector_tile_setdata_baton_t(); closure->request.data = closure; @@ -1372,10 +1403,10 @@ Handle VectorTile::setData(const Arguments& args) closure->data = node::Buffer::Data(obj); closure->dataLength = node::Buffer::Length(obj); closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_SetData, (uv_after_work_cb)EIO_AfterSetData); d->Ref(); - return Undefined(); + NanReturnUndefined(); } void VectorTile::EIO_SetData(uv_work_t* req) @@ -1396,61 +1427,56 @@ void VectorTile::EIO_SetData(uv_work_t* req) void VectorTile::EIO_AfterSetData(uv_work_t* req) { - HandleScope scope; + NanScope(); vector_tile_setdata_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[1] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } closure->d->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle VectorTile::getData(const Arguments& args) +NAN_METHOD(VectorTile::getData) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); try { // shortcut: return raw data and avoid trip through proto object // TODO - safe for null string? int raw_size = d->buffer_.size(); if (d->byte_size_ <= raw_size) { - return scope.Close(node::Buffer::New((char*)d->buffer_.data(),raw_size)->handle_); + NanReturnValue(NanNewBufferHandle((char*)d->buffer_.data(),raw_size)); } else { // NOTE: tiledata.ByteSize() must be called // after each modification of tiledata otherwise the // SerializeWithCachedSizesToArray will throw mapnik::vector::tile const& tiledata = d->get_tile(); - node::Buffer *retbuf = node::Buffer::New(d->byte_size_); + Local retbuf = NanNewBufferHandle(d->byte_size_); // TODO - consider wrapping in fastbuffer: https://gist.github.com/drewish/2732711 // http://www.samcday.com.au/blog/2011/03/03/creating-a-proper-buffer-in-a-node-c-addon/ google::protobuf::uint8* start = reinterpret_cast(node::Buffer::Data(retbuf)); google::protobuf::uint8* end = tiledata.SerializeWithCachedSizesToArray(start); if (end - start != d->byte_size_) { - return ThrowException(Exception::Error( - String::New("serialization failed, possible race condition"))); + NanThrowError("serialization failed, possible race condition"); + NanReturnUndefined(); } - return scope.Close(retbuf->handle_); + NanReturnValue(retbuf); } } catch (std::exception const& ex) { - return ThrowException(Exception::Error( - String::New(ex.what()))); + NanThrowError(ex.what()); + NanReturnUndefined(); } - return Undefined(); + NanReturnUndefined(); } struct vector_tile_render_baton_t { @@ -1496,94 +1522,101 @@ struct vector_tile_render_baton_t { use_cairo(true) {} }; -Handle VectorTile::render(const Arguments& args) +NAN_METHOD(VectorTile::render) { - HandleScope scope; + NanScope(); - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 1 || !args[0]->IsObject()) { - return ThrowException(Exception::TypeError(String::New("mapnik.Map expected as first arg"))); + NanThrowTypeError("mapnik.Map expected as first arg"); + NanReturnUndefined(); } Local obj = args[0]->ToObject(); - if (obj->IsNull() || obj->IsUndefined() || !Map::constructor->HasInstance(obj)) - return ThrowException(Exception::TypeError(String::New("mapnik.Map expected as first arg"))); + if (obj->IsNull() || obj->IsUndefined() || !NanNew(Map::constructor)->HasInstance(obj)) { + NanThrowTypeError("mapnik.Map expected as first arg"); + NanReturnUndefined(); + } Map *m = node::ObjectWrap::Unwrap(obj); if (args.Length() < 2 || !args[1]->IsObject()) { - return ThrowException(Exception::TypeError(String::New("a renderable mapnik object is expected as second arg"))); + NanThrowTypeError("a renderable mapnik object is expected as second arg"); + NanReturnUndefined(); } Local im_obj = args[1]->ToObject(); - if (im_obj->IsNull() || im_obj->IsUndefined()) - return ThrowException(Exception::TypeError(String::New("a renderable mapnik object is expected as second arg"))); + if (im_obj->IsNull() || im_obj->IsUndefined()) { + NanThrowTypeError("a renderable mapnik object is expected as second arg"); + NanReturnUndefined(); + } // ensure callback is a function Local callback = args[args.Length()-1]; if (!args[args.Length()-1]->IsFunction()) { - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); } vector_tile_render_baton_t *closure = new vector_tile_render_baton_t(); - Local options = Object::New(); + Local options = NanNew(); if (args.Length() > 2) { if (!args[2]->IsObject()) { delete closure; - return ThrowException(Exception::TypeError(String::New("optional third argument must be an options object"))); + NanThrowTypeError("optional third argument must be an options object"); + NanReturnUndefined(); } options = args[2]->ToObject(); - if (options->Has(String::NewSymbol("z"))) + if (options->Has(NanNew("z"))) { closure->zxy_override = true; - closure->z = options->Get(String::New("z"))->IntegerValue(); + closure->z = options->Get(NanNew("z"))->IntegerValue(); } - if (options->Has(String::NewSymbol("x"))) + if (options->Has(NanNew("x"))) { closure->zxy_override = true; - closure->x = options->Get(String::New("x"))->IntegerValue(); + closure->x = options->Get(NanNew("x"))->IntegerValue(); } - if (options->Has(String::NewSymbol("y"))) + if (options->Has(NanNew("y"))) { closure->zxy_override = true; - closure->y = options->Get(String::New("y"))->IntegerValue(); + closure->y = options->Get(NanNew("y"))->IntegerValue(); } - if (options->Has(String::New("buffer_size"))) { - Local bind_opt = options->Get(String::New("buffer_size")); + if (options->Has(NanNew("buffer_size"))) { + Local bind_opt = options->Get(NanNew("buffer_size")); if (!bind_opt->IsNumber()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("optional arg 'buffer_size' must be a number"))); + NanThrowTypeError("optional arg 'buffer_size' must be a number"); + NanReturnUndefined(); } closure->buffer_size = bind_opt->IntegerValue(); } - if (options->Has(String::New("scale"))) { - Local bind_opt = options->Get(String::New("scale")); + if (options->Has(NanNew("scale"))) { + Local bind_opt = options->Get(NanNew("scale")); if (!bind_opt->IsNumber()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale' must be a number"))); + NanThrowTypeError("optional arg 'scale' must be a number"); + NanReturnUndefined(); } closure->scale_factor = bind_opt->NumberValue(); } - if (options->Has(String::NewSymbol("scale_denominator"))) + if (options->Has(NanNew("scale_denominator"))) { - Local bind_opt = options->Get(String::New("scale_denominator")); + Local bind_opt = options->Get(NanNew("scale_denominator")); if (!bind_opt->IsNumber()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("optional arg 'scale_denominator' must be a number"))); + NanThrowTypeError("optional arg 'scale_denominator' must be a number"); + NanReturnUndefined(); } closure->scale_denominator = bind_opt->NumberValue(); } } closure->layer_idx = 0; - if (Image::constructor->HasInstance(im_obj)) + if (NanNew(Image::constructor)->HasInstance(im_obj)) { Image *im = node::ObjectWrap::Unwrap(im_obj); closure->im = im; @@ -1591,20 +1624,21 @@ Handle VectorTile::render(const Arguments& args) closure->height = im->get()->height(); closure->im->_ref(); } - else if (CairoSurface::constructor->HasInstance(im_obj)) + else if (NanNew(CairoSurface::constructor)->HasInstance(im_obj)) { CairoSurface *c = node::ObjectWrap::Unwrap(im_obj); closure->c = c; closure->width = c->width(); closure->height = c->height(); closure->c->_ref(); - if (options->Has(String::New("renderer"))) + if (options->Has(NanNew("renderer"))) { - Local renderer = options->Get(String::New("renderer")); + Local renderer = options->Get(NanNew("renderer")); if (!renderer->IsString() ) { delete closure; - return ThrowException(String::New("'renderer' option must be a string of either 'svg' or 'cairo'")); + NanThrowError("'renderer' option must be a string of either 'svg' or 'cairo'"); + NanReturnUndefined(); } std::string renderer_name = TOSTR(renderer); if (renderer_name == "cairo") @@ -1618,11 +1652,12 @@ Handle VectorTile::render(const Arguments& args) else { delete closure; - return ThrowException(String::New("'renderer' option must be a string of either 'svg' or 'cairo'")); + NanThrowError("'renderer' option must be a string of either 'svg' or 'cairo'"); + NanReturnUndefined(); } } } - else if (Grid::constructor->HasInstance(im_obj)) + else if (NanNew(Grid::constructor)->HasInstance(im_obj)) { Grid *g = node::ObjectWrap::Unwrap(im_obj); closure->g = g; @@ -1633,20 +1668,20 @@ Handle VectorTile::render(const Arguments& args) std::size_t layer_idx = 0; // grid requires special options for now - if (!options->Has(String::New("layer"))) + if (!options->Has(NanNew("layer"))) { delete closure; - return ThrowException(Exception::TypeError( - String::New("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"))); + NanThrowTypeError("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"); + NanReturnUndefined(); } else { std::vector const& layers = m->get()->layers(); - Local layer_id = options->Get(String::New("layer")); + Local layer_id = options->Get(NanNew("layer")); if (! (layer_id->IsString() || layer_id->IsNumber()) ) { delete closure; - return ThrowException(Exception::TypeError( - String::New("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"))); + NanThrowTypeError("'layer' option required for grid rendering and must be either a layer name(string) or layer index (integer)"); + NanReturnUndefined(); } if (layer_id->IsString()) { @@ -1668,7 +1703,8 @@ Handle VectorTile::render(const Arguments& args) std::ostringstream s; s << "Layer name '" << layer_name << "' not found"; delete closure; - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else if (layer_id->IsNumber()) { layer_idx = layer_id->IntegerValue(); @@ -1686,21 +1722,23 @@ Handle VectorTile::render(const Arguments& args) s << "no layers found in map"; } delete closure; - return ThrowException(Exception::TypeError(String::New(s.str().c_str()))); + NanThrowTypeError(s.str().c_str()); + NanReturnUndefined(); } } else { delete closure; - return ThrowException(Exception::TypeError(String::New("layer id must be a string or index number"))); + NanThrowTypeError("layer id must be a string or index number"); + NanReturnUndefined(); } } - if (options->Has(String::New("fields"))) { + if (options->Has(NanNew("fields"))) { - Local param_val = options->Get(String::New("fields")); + Local param_val = options->Get(NanNew("fields")); if (!param_val->IsArray()) { delete closure; - return ThrowException(Exception::TypeError( - String::New("option 'fields' must be an array of strings"))); + NanThrowTypeError("option 'fields' must be an array of strings"); + NanReturnUndefined(); } Local a = Local::Cast(param_val); unsigned int i = 0; @@ -1718,17 +1756,18 @@ Handle VectorTile::render(const Arguments& args) else { delete closure; - return ThrowException(Exception::TypeError(String::New("renderable mapnik object expected as second arg"))); + NanThrowTypeError("renderable mapnik object expected as second arg"); + NanReturnUndefined(); } closure->request.data = closure; closure->d = d; closure->m = m; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_RenderTile, (uv_after_work_cb)EIO_AfterRenderTile); m->_ref(); d->Ref(); - return Undefined(); + NanReturnUndefined(); } template void process_layers(Renderer & ren, @@ -1936,55 +1975,54 @@ void VectorTile::EIO_RenderTile(uv_work_t* req) void VectorTile::EIO_AfterRenderTile(uv_work_t* req) { - HandleScope scope; + NanScope(); vector_tile_render_baton_t *closure = static_cast(req->data); - TryCatch try_catch; - if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { if (closure->im) { - Local argv[2] = { Local::New(Null()), Local::New(closure->im->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->im) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } else if (closure->g) { - Local argv[2] = { Local::New(Null()), Local::New(closure->g->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->g) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } else if (closure->c) { - Local argv[2] = { Local::New(Null()), Local::New(closure->c->handle_) }; - closure->cb->Call(Context::GetCurrent()->Global(), 2, argv); + Local argv[2] = { NanNull(), NanObjectWrapHandle(closure->c) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 2, argv); } } - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - closure->m->_unref(); if (closure->im) closure->im->_unref(); if (closure->g) closure->g->_unref(); closure->d->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } +NAN_METHOD(VectorTile::clearSync) +{ + NanScope(); + NanReturnValue(_clearSync(args)); +} -Handle VectorTile::clearSync(const Arguments& args) +Local VectorTile::_clearSync(_NAN_METHOD_ARGS) { - HandleScope scope; + NanEscapableScope(); #if MAPNIK_VERSION >= 200200 - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); d->clear(); #endif - return Undefined(); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -1996,27 +2034,28 @@ typedef struct { Persistent cb; } clear_vector_tile_baton_t; -Handle VectorTile::clear(const Arguments& args) +NAN_METHOD(VectorTile::clear) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { - return clearSync(args); + NanReturnValue(_clearSync(args)); } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!callback->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } clear_vector_tile_baton_t *closure = new clear_vector_tile_baton_t(); closure->request.data = closure; closure->d = d; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_Clear, (uv_after_work_cb)EIO_AfterClear); d->Ref(); - return Undefined(); + NanReturnUndefined(); } void VectorTile::EIO_Clear(uv_work_t* req) @@ -2037,51 +2076,52 @@ void VectorTile::EIO_Clear(uv_work_t* req) void VectorTile::EIO_AfterClear(uv_work_t* req) { - HandleScope scope; + NanScope(); clear_vector_tile_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[2] = { Local::New(Null()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); - } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); + Local argv[1] = { NanNull() }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } closure->d->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } -Handle VectorTile::isSolidSync(const Arguments& args) +NAN_METHOD(VectorTile::isSolidSync) +{ + NanScope(); + NanReturnValue(_isSolidSync(args)); +} + +Local VectorTile::_isSolidSync(_NAN_METHOD_ARGS) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanEscapableScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); try { std::string key; - bool is_solid = mapnik::vector::is_solid_extent(d->get_tile(),key); + bool is_solid = mapnik::vector::is_solid_extent(d->get_tile(), key); if (is_solid) { - return scope.Close(String::New(key.c_str())); + return NanEscapeScope(NanNew(key.c_str())); } else { - return scope.Close(False()); + return NanEscapeScope(NanFalse()); } } catch (std::exception const& ex) { - return ThrowException(Exception::TypeError( - String::New(ex.what()))); + NanThrowError(ex.what()); + return NanEscapeScope(NanUndefined()); } - return scope.Close(Undefined()); + return NanEscapeScope(NanUndefined()); } typedef struct { @@ -2094,29 +2134,30 @@ typedef struct { bool result; } is_solid_vector_tile_baton_t; -Handle VectorTile::isSolid(const Arguments& args) +NAN_METHOD(VectorTile::isSolid) { - HandleScope scope; - VectorTile* d = node::ObjectWrap::Unwrap(args.This()); + NanScope(); + VectorTile* d = node::ObjectWrap::Unwrap(args.Holder()); if (args.Length() == 0) { - return isSolidSync(args); + NanReturnValue(_isSolidSync(args)); } // ensure callback is a function - Local callback = args[args.Length()-1]; - if (!args[args.Length()-1]->IsFunction()) - return ThrowException(Exception::TypeError( - String::New("last argument must be a callback function"))); + Local callback = args[args.Length() - 1]; + if (!callback->IsFunction()) { + NanThrowTypeError("last argument must be a callback function"); + NanReturnUndefined(); + } is_solid_vector_tile_baton_t *closure = new is_solid_vector_tile_baton_t(); closure->request.data = closure; closure->d = d; closure->result = true; closure->error = false; - closure->cb = Persistent::New(Handle::Cast(callback)); + NanAssignPersistent(closure->cb, callback.As()); uv_queue_work(uv_default_loop(), &closure->request, EIO_IsSolid, (uv_after_work_cb)EIO_AfterIsSolid); d->Ref(); - return Undefined(); + NanReturnUndefined(); } void VectorTile::EIO_IsSolid(uv_work_t* req) @@ -2134,26 +2175,21 @@ void VectorTile::EIO_IsSolid(uv_work_t* req) void VectorTile::EIO_AfterIsSolid(uv_work_t* req) { - HandleScope scope; + NanScope(); is_solid_vector_tile_baton_t *closure = static_cast(req->data); - TryCatch try_catch; if (closure->error) { - Local argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) }; - closure->cb->Call(Context::GetCurrent()->Global(), 1, argv); + Local argv[1] = { NanError(closure->error_name.c_str()) }; + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 1, argv); } else { - Local argv[3] = { Local::New(Null()), - Local::New(Boolean::New(closure->result)), - Local::New(String::New(closure->key.c_str())) + Local argv[3] = { NanNull(), + NanNew(closure->result), + NanNew(closure->key.c_str()) }; - closure->cb->Call(Context::GetCurrent()->Global(), 3, argv); - } - if (try_catch.HasCaught()) - { - node::FatalException(try_catch); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(closure->cb), 3, argv); } closure->d->Unref(); - closure->cb.Dispose(); + NanDisposePersistent(closure->cb); delete closure; } diff --git a/src/mapnik_vector_tile.hpp b/src/mapnik_vector_tile.hpp index 3d32c7f6ca..56b87c7e86 100644 --- a/src/mapnik_vector_tile.hpp +++ b/src/mapnik_vector_tile.hpp @@ -1,9 +1,7 @@ #ifndef __NODE_MAPNIK_VECTOR_TILE_H__ #define __NODE_MAPNIK_VECTOR_TILE_H__ -#include -#include -#include "uv.h" +#include #include "vector_tile.pb.h" #include #include @@ -22,41 +20,45 @@ class VectorTile: public node::ObjectWrap { }; static Persistent constructor; static void Initialize(Handle target); - static Handle New(Arguments const&args); - static Handle getData(Arguments const& args); - static Handle render(Arguments const& args); - static Handle toJSON(Arguments const& args); - static Handle query(Arguments const& args); - static Handle names(Arguments const& args); - static Handle toGeoJSON(Arguments const& args); - static Handle fromGeoJSON(Arguments const& args); + static NAN_METHOD(New); + static NAN_METHOD(getData); + static NAN_METHOD(render); + static NAN_METHOD(toJSON); + static NAN_METHOD(query); + static NAN_METHOD(names); + static NAN_METHOD(toGeoJSON); + static NAN_METHOD(fromGeoJSON); #ifdef PROTOBUF_FULL - static Handle toString(Arguments const& args); + static NAN_METHOD(toString); #endif static void EIO_RenderTile(uv_work_t* req); static void EIO_AfterRenderTile(uv_work_t* req); - static Handle setData(Arguments const& args); + static NAN_METHOD(setData); static void EIO_SetData(uv_work_t* req); static void EIO_AfterSetData(uv_work_t* req); - static Handle setDataSync(Arguments const& args); - static Handle parse(Arguments const& args); + static Local _setDataSync(_NAN_METHOD_ARGS); + static NAN_METHOD(setDataSync); + static NAN_METHOD(parse); static void EIO_Parse(uv_work_t* req); static void EIO_AfterParse(uv_work_t* req); - static Handle parseSync(Arguments const& args); - static Handle addData(Arguments const& args); - static Handle composite(Arguments const& args); + static NAN_METHOD(parseSync); + static Local _parseSync(_NAN_METHOD_ARGS); + static NAN_METHOD(addData); + static NAN_METHOD(composite); // methods common to mapnik.Image - static Handle width(Arguments const& args); - static Handle height(Arguments const& args); - static Handle painted(Arguments const& args); - static Handle clearSync(const Arguments& args); - static Handle clear(const Arguments& args); + static NAN_METHOD(width); + static NAN_METHOD(height); + static NAN_METHOD(painted); + static NAN_METHOD(clearSync); + static Local _clearSync(_NAN_METHOD_ARGS); + static NAN_METHOD(clear); static void EIO_Clear(uv_work_t* req); static void EIO_AfterClear(uv_work_t* req); - static Handle isSolid(Arguments const& args); + static NAN_METHOD(isSolid); static void EIO_IsSolid(uv_work_t* req); static void EIO_AfterIsSolid(uv_work_t* req); - static Handle isSolidSync(Arguments const& args); + static NAN_METHOD(isSolidSync); + static Local _isSolidSync(_NAN_METHOD_ARGS); VectorTile(int z, int x, int y, unsigned w, unsigned h); diff --git a/src/node_mapnik.cpp b/src/node_mapnik.cpp index 07103e461a..43e9108cc7 100644 --- a/src/node_mapnik.cpp +++ b/src/node_mapnik.cpp @@ -1,9 +1,3 @@ -// v8 -#include - -// node -#include -#include #include "mapnik3x_compatibility.hpp" // node-mapnik @@ -63,10 +57,10 @@ using namespace v8; * until real work has been done. This indicates that V8 has done * as much cleanup as it will be able to do. */ -static Handle gc(const Arguments& args) +static NAN_METHOD(gc) { - HandleScope scope; - return scope.Close(Boolean::New(V8::IdleNotification())); + NanScope(); + NanReturnValue(NanNew(V8::IdleNotification())); } static std::string format_version(int version) @@ -76,9 +70,9 @@ static std::string format_version(int version) return s.str(); } -static Handle clearCache(const Arguments& args) +static NAN_METHOD(clearCache) { - HandleScope scope; + NanScope(); #if MAPNIK_VERSION >= 200300 #if defined(SHAPE_MEMORY_MAPPED_FILE) mapnik::marker_cache::instance().clear(); @@ -95,23 +89,23 @@ static Handle clearCache(const Arguments& args) #endif #endif #endif - return scope.Close(Undefined()); + NanReturnUndefined(); } -static Handle shutdown(const Arguments& args) +static NAN_METHOD(shutdown) { - HandleScope scope; + NanScope(); google::protobuf::ShutdownProtobufLibrary(); // http://lists.fedoraproject.org/pipermail/devel/2010-January/129117.html xmlCleanupParser(); - return scope.Close(Undefined()); + NanReturnUndefined(); } extern "C" { static void InitMapnik (Handle target) { - HandleScope scope; + NanScope(); GOOGLE_PROTOBUF_VERIFY_VERSION; // https://mail.gnome.org/archives/xml/2007-October/msg00004.html // calls http://xmlsoft.org/html/libxml-xmlversion.html#xmlCheckVersion @@ -155,92 +149,92 @@ extern "C" { CairoSurface::Initialize(target); // versions of deps - Local versions = Object::New(); - versions->Set(String::NewSymbol("node"), String::New(NODE_VERSION+1)); // NOTE: +1 strips the v in v0.10.26 - versions->Set(String::NewSymbol("v8"), String::New(V8::GetVersion())); - versions->Set(String::NewSymbol("boost"), String::New(format_version(BOOST_VERSION).c_str())); - versions->Set(String::NewSymbol("boost_number"), Integer::New(BOOST_VERSION)); - versions->Set(String::NewSymbol("mapnik"), String::New(format_version(MAPNIK_VERSION).c_str())); - versions->Set(String::NewSymbol("mapnik_number"), Integer::New(MAPNIK_VERSION)); - versions->Set(String::NewSymbol("libxml"), String::New(LIBXML_DOTTED_VERSION)); + Local versions = NanNew(); + versions->Set(NanNew("node"), NanNew(NODE_VERSION+1)); // NOTE: +1 strips the v in v0.10.26 + versions->Set(NanNew("v8"), NanNew(V8::GetVersion())); + versions->Set(NanNew("boost"), NanNew(format_version(BOOST_VERSION).c_str())); + versions->Set(NanNew("boost_number"), NanNew(BOOST_VERSION)); + versions->Set(NanNew("mapnik"), NanNew(format_version(MAPNIK_VERSION).c_str())); + versions->Set(NanNew("mapnik_number"), NanNew(MAPNIK_VERSION)); + versions->Set(NanNew("libxml"), NanNew(LIBXML_DOTTED_VERSION)); #if defined(HAVE_CAIRO) - versions->Set(String::NewSymbol("cairo"), String::New(CAIRO_VERSION_STRING)); + versions->Set(NanNew("cairo"), NanNew(CAIRO_VERSION_STRING)); #endif - target->Set(String::NewSymbol("versions"), versions); + target->Set(NanNew("versions"), versions); - Local supports = Object::New(); + Local supports = NanNew(); #if MAPNIK_VERSION >= 200300 #ifdef GRID_RENDERER - supports->Set(String::NewSymbol("grid"), True()); + supports->Set(NanNew("grid"), NanTrue()); #else - supports->Set(String::NewSymbol("grid"), False()); + supports->Set(NanNew("grid"), NanFalse()); #endif #else - supports->Set(String::NewSymbol("grid"), True()); + supports->Set(NanNew("grid"), NanTrue()); #endif #ifdef SVG_RENDERER - supports->Set(String::NewSymbol("svg"), True()); + supports->Set(NanNew("svg"), NanTrue()); #else - supports->Set(String::NewSymbol("svg"), False()); + supports->Set(NanNew("svg"), NanFalse()); #endif #if defined(HAVE_CAIRO) - supports->Set(String::NewSymbol("cairo"), True()); + supports->Set(NanNew("cairo"), NanTrue()); #ifdef CAIRO_HAS_PDF_SURFACE - supports->Set(String::NewSymbol("cairo_pdf"), True()); + supports->Set(NanNew("cairo_pdf"), NanTrue()); #else - supports->Set(String::NewSymbol("cairo_pdf"), False()); + supports->Set(NanNew("cairo_pdf"), NanFalse()); #endif #ifdef CAIRO_HAS_SVG_SURFACE - supports->Set(String::NewSymbol("cairo_svg"), True()); + supports->Set(NanNew("cairo_svg"), NanTrue()); #else - supports->Set(String::NewSymbol("cairo_svg"), False()); + supports->Set(NanNew("cairo_svg"), NanFalse()); #endif #else - supports->Set(String::NewSymbol("cairo"), False()); + supports->Set(NanNew("cairo"), NanFalse()); #endif #if defined(HAVE_PNG) - supports->Set(String::NewSymbol("png"), True()); + supports->Set(NanNew("png"), NanTrue()); #else - supports->Set(String::NewSymbol("png"), False()); + supports->Set(NanNew("png"), NanFalse()); #endif #if defined(HAVE_JPEG) - supports->Set(String::NewSymbol("jpeg"), True()); + supports->Set(NanNew("jpeg"), NanTrue()); #else - supports->Set(String::NewSymbol("jpeg"), False()); + supports->Set(NanNew("jpeg"), NanFalse()); #endif #if defined(HAVE_TIFF) - supports->Set(String::NewSymbol("tiff"), True()); + supports->Set(NanNew("tiff"), NanTrue()); #else - supports->Set(String::NewSymbol("tiff"), False()); + supports->Set(NanNew("tiff"), NanFalse()); #endif #if defined(HAVE_WEBP) - supports->Set(String::NewSymbol("webp"), True()); + supports->Set(NanNew("webp"), NanTrue()); #else - supports->Set(String::NewSymbol("webp"), False()); + supports->Set(NanNew("webp"), NanFalse()); #endif #if defined(MAPNIK_USE_PROJ4) - supports->Set(String::NewSymbol("proj4"), True()); + supports->Set(NanNew("proj4"), NanTrue()); #else - supports->Set(String::NewSymbol("proj4"), False()); + supports->Set(NanNew("proj4"), NanFalse()); #endif #if defined(MAPNIK_THREADSAFE) - supports->Set(String::NewSymbol("threadsafe"), True()); + supports->Set(NanNew("threadsafe"), NanTrue()); #else - supports->Set(String::NewSymbol("threadsafe"), False()); + supports->Set(NanNew("threadsafe"), NanFalse()); #endif - target->Set(String::NewSymbol("supports"), supports); + target->Set(NanNew("supports"), supports); #if MAPNIK_VERSION >= 200100 - Local composite_ops = Object::New(); + Local composite_ops = NanNew(); NODE_MAPNIK_DEFINE_CONSTANT(composite_ops, "clear", mapnik::clear) NODE_MAPNIK_DEFINE_CONSTANT(composite_ops, "src", mapnik::src) NODE_MAPNIK_DEFINE_CONSTANT(composite_ops, "dst", mapnik::dst) @@ -275,7 +269,7 @@ extern "C" { NODE_MAPNIK_DEFINE_CONSTANT(composite_ops, "color", mapnik::_color) NODE_MAPNIK_DEFINE_CONSTANT(composite_ops, "value", mapnik::_value) - target->Set(String::NewSymbol("compositeOp"), composite_ops); + target->Set(NanNew("compositeOp"), composite_ops); #endif } @@ -289,7 +283,7 @@ extern "C" { #define NODE_MAPNIK_MODULE(modname, regfunc) \ extern "C" { \ - MAPNIK_EXP node::node_module_struct modname ## _module = \ + MAPNIK_EXP node::node_module_struct modname ## _module = \ { \ NODE_STANDARD_MODULE_STUFF, \ (node::addon_register_func)regfunc, \ @@ -301,14 +295,18 @@ extern "C" { #define NODE_MAPNIK_MODULE(modname, regfunc) \ extern "C" { \ - MAPNIK_EXP node::node_module_struct modname ## _module = \ + MAPNIK_EXP node::node_module_struct modname ## _module = \ { \ NODE_STANDARD_MODULE_STUFF, \ - regfunc, \ + regfunc, \ NODE_STRINGIFY(modname) \ }; \ } #endif +#if NODE_MODULE_VERSION > 0x000B +NODE_MODULE(mapnik, node_mapnik::InitMapnik) +#else NODE_MAPNIK_MODULE(mapnik, node_mapnik::InitMapnik) +#endif diff --git a/src/utils.hpp b/src/utils.hpp index e102e9d51b..5b99685f0a 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,8 +1,8 @@ #ifndef __NODE_MAPNIK_UTILS_H__ #define __NODE_MAPNIK_UTILS_H__ -// v8 -#include +// nan +#include // stl #include @@ -19,31 +19,32 @@ #define TOSTR(obj) (*String::Utf8Value((obj)->ToString())) #define FUNCTION_ARG(I, VAR) \ - if (args.Length() <= (I) || !args[I]->IsFunction()) \ - return ThrowException(Exception::TypeError( \ - String::New("Argument " #I " must be a function"))); \ - Local VAR = Local::Cast(args[I]); + if (args.Length() <= (I) || !args[I]->IsFunction()) { \ + NanThrowTypeError("Argument " #I " must be a function"); \ + NanReturnUndefined(); \ + } \ + Local VAR = args[I].As(); #define ATTR(t, name, get, set) \ - t->InstanceTemplate()->SetAccessor(String::NewSymbol(name), get, set); + t->InstanceTemplate()->SetAccessor(NanNew(name), get, set); -#define NODE_MAPNIK_DEFINE_CONSTANT(target, name, constant) \ - (target)->Set(v8::String::NewSymbol(name), \ - v8::Integer::New(constant), \ - static_cast( \ +#define NODE_MAPNIK_DEFINE_CONSTANT(target, name, constant) \ + (target)->Set(NanNew(name), \ + NanNew(constant), \ + static_cast( \ v8::ReadOnly|v8::DontDelete)); -#define NODE_MAPNIK_DEFINE_64_BIT_CONSTANT(target, name, constant) \ - (target)->Set(v8::String::NewSymbol(name), \ - v8::Number::New(constant), \ - static_cast( \ +#define NODE_MAPNIK_DEFINE_64_BIT_CONSTANT(target, name, constant) \ + (target)->Set(NanNew(name), \ + NanNew(constant), \ + static_cast( \ v8::ReadOnly|v8::DontDelete)); using namespace v8; namespace node_mapnik { - + #if MAPNIK_VERSION >= 200200 typedef mapnik::value_integer value_integer; #else @@ -60,35 +61,35 @@ struct params_to_object : public boost::static_visitor<> void operator () ( value_integer val ) { - ds_->Set(String::NewSymbol(key_.c_str()), Number::New(val) ); + ds_->Set(NanNew(key_.c_str()), NanNew(val) ); } void operator () ( bool val ) { - ds_->Set(String::NewSymbol(key_.c_str()), Boolean::New(val) ); + ds_->Set(NanNew(key_.c_str()), NanNew(val) ); } void operator () ( double val ) { - ds_->Set(String::NewSymbol(key_.c_str()), Number::New(val) ); + ds_->Set(NanNew(key_.c_str()), NanNew(val) ); } void operator () ( std::string const& val ) { - ds_->Set(String::NewSymbol(key_.c_str()), String::New(val.c_str()) ); + ds_->Set(NanNew(key_.c_str()), NanNew(val.c_str()) ); } void operator () ( mapnik::value_unicode_string const& val) { std::string buffer; mapnik::to_utf8(val,buffer); - ds_->Set(String::NewSymbol(key_.c_str()), String::New(buffer.c_str()) ); + ds_->Set(NanNew(key_.c_str()), NanNew(buffer.c_str()) ); } void operator () ( mapnik::value_null const& val ) { - ds_->Set(String::NewSymbol(key_.c_str()), Null() ); + ds_->Set(NanNew(key_.c_str()), NanNull() ); } private: @@ -100,34 +101,34 @@ struct value_converter: public boost::static_visitor > { Handle operator () ( value_integer val ) const { - return Number::New(val); + return NanNew(val); } Handle operator () ( bool val ) const { - return Boolean::New(val); + return NanNew(val); } Handle operator () ( double val ) const { - return Number::New(val); + return NanNew(val); } Handle operator () ( std::string const& val ) const { - return String::New(val.c_str()); + return NanNew(val.c_str()); } Handle operator () ( mapnik::value_unicode_string const& val) const { std::string buffer; mapnik::to_utf8(val,buffer); - return String::New(buffer.c_str()); + return NanNew(buffer.c_str()); } Handle operator () ( mapnik::value_null const& val ) const { - return Undefined(); + return NanUndefined(); } };