Skip to content

Commit

Permalink
Update Image implementation as per nodejs/node-addon-api#754
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed Aug 14, 2020
1 parent 43605c8 commit 95e3d96
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
'./mason_packages/.link/include/cairo',
'./mason_packages/.link/include/mapnik',
'./src',
"<!@(node -p \"require('node-addon-api').include.slice(1,-1)\")",
"<!@(node -p \"require('node-addon-api').include\")",
# TODO: move these to mason packages once we have a minimal windows client for mason (@springmeyer)
# https://github.com/mapbox/mason/issues/396
"./deps/geometry/include/",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"license": "BSD-3-Clause",
"dependencies": {
"mapnik-vector-tile": "3.0.1",
"node-addon-api": "~3.0.0",
"node-addon-api": "~3.0.1",
"node-pre-gyp": "~0.13.0"
},
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions src/blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ Napi::Value blend(Napi::CallbackInfo const& info)
else if (buffer.IsObject())
{
Napi::Object obj = buffer.As<Napi::Object>();
if (obj.InstanceOf(Image::constructor.Value()))
if (obj.InstanceOf(Image::Constructor(env)))
{
Image * im = Napi::ObjectWrap<Image>::Unwrap(obj);

Expand All @@ -876,7 +876,7 @@ Napi::Value blend(Napi::CallbackInfo const& info)
else if (buffer.IsObject())
{
Napi::Object possible_im = buffer.As<Napi::Object>();
if (possible_im.InstanceOf(Image::constructor.Value()))
if (possible_im.InstanceOf(Image::Constructor(env)))
{
Image * im = Napi::ObjectWrap<Image>::Unwrap(possible_im);
if (im->impl()->get_dtype() == mapnik::image_dtype_rgba8)
Expand Down
15 changes: 10 additions & 5 deletions src/mapnik_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include <sstream> // for basic_ostringstream, etc
#include <cstdlib>

Napi::FunctionReference Image::constructor;

Napi::Object Image::Initialize(Napi::Env env, Napi::Object exports, napi_property_attributes prop_attr)
{
Napi::HandleScope scope(env);
Expand Down Expand Up @@ -66,12 +64,19 @@ Napi::Object Image::Initialize(Napi::Env env, Napi::Object exports, napi_propert
StaticMethod<&Image::fromSVGBytesSync>("fromSVGBytesSync", prop_attr),
StaticMethod<&Image::fromSVGBytes>("fromSVGBytes", prop_attr)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
exports.Set("Image", func);
env.SetInstanceData<Napi::FunctionReference>(constructor);
return exports;
}

Napi::Object Image::NewInstance(Napi::Env env, Napi::Value arg)
{
Napi::Object obj = env.GetInstanceData<Napi::FunctionReference>()->New({arg});
return obj;
}

/**
* **`mapnik.Image`**
*
Expand Down Expand Up @@ -426,7 +431,7 @@ Napi::Value Image::compare(Napi::CallbackInfo const& info)
return env.Undefined();
}
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
if (!obj.InstanceOf(Image::Constructor(env)))
{
Napi::TypeError::New(env, "mapnik.Image expected as first arg").ThrowAsJavaScriptException();
return env.Undefined();
Expand Down
3 changes: 2 additions & 1 deletion src/mapnik_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Image : public Napi::ObjectWrap<Image>
public:
// initializer
static Napi::Object Initialize(Napi::Env env, Napi::Object exports, napi_property_attributes attr);
static Napi::Object NewInstance(Napi::Env env, Napi::Value arg);
static Napi::Function Constructor(Napi::Env env) { return env.GetInstanceData<Napi::FunctionReference>()->Value();}
// ctor
explicit Image(Napi::CallbackInfo const& info);
// methods
Expand Down Expand Up @@ -70,7 +72,6 @@ class Image : public Napi::ObjectWrap<Image>
Napi::Value offset(Napi::CallbackInfo const& info);
void offset(Napi::CallbackInfo const& info, Napi::Value const& value);
inline image_ptr impl() const { return image_; }
static Napi::FunctionReference constructor;
private:
static void encode_common_args_(Napi::CallbackInfo const& info, std::string& format, palette_ptr& palette);
static Napi::Value from_svg_sync_impl(Napi::CallbackInfo const& info, bool from_file);
Expand Down
2 changes: 1 addition & 1 deletion src/mapnik_image_compositing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Napi::Value Image::composite(Napi::CallbackInfo const& info)
}

Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
if (!obj.InstanceOf(Image::Constructor(env)))
{
Napi::TypeError::New(env, "mapnik.Image expected as first arg").ThrowAsJavaScriptException();
return env.Undefined();
Expand Down
4 changes: 2 additions & 2 deletions src/mapnik_image_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct AsyncCopy : Napi::AsyncWorker
if (image_out_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_out_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Undefined(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -265,7 +265,7 @@ Napi::Value Image::copySync(Napi::CallbackInfo const& info)
mapnik::image_copy(*image_, type, offset, scaling)
);
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_out);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return scope.Escape(obj);
}
catch (std::exception const& ex)
Expand Down
2 changes: 1 addition & 1 deletion src/mapnik_image_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct AsyncFilter : Napi::AsyncWorker
if (image_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down
7 changes: 4 additions & 3 deletions src/mapnik_image_from_bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct AsyncFromBytes : Napi::AsyncWorker
if (image_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -111,7 +111,7 @@ Napi::Value Image::fromBytesSync(Napi::CallbackInfo const& info)
mapnik::set_premultiplied_alpha(*imagep, true);
}
Napi::Value arg = Napi::External<image_ptr>::New(env, &imagep);
Napi::Object obj = constructor.New({arg});
Napi::Object obj = NewInstance(env, arg);
return scope.Escape(napi_value(obj)).ToObject();
}
// The only way this is ever reached is if the reader factory in
Expand Down Expand Up @@ -337,7 +337,8 @@ Napi::Value Image::fromBufferSync(Napi::CallbackInfo const& info)
mapnik::image_rgba8 im_wrapper(width, height, reinterpret_cast<unsigned char*>(obj.As<Napi::Buffer<char>>().Data()), premultiplied, painted);
image_ptr imagep = std::make_shared<mapnik::image_any>(im_wrapper);
Napi::Value arg = Napi::External<image_ptr>::New(env, &imagep);
Napi::Object image_obj = constructor.New({arg});
Napi::FunctionReference* constructor = env.GetInstanceData<Napi::FunctionReference>();
Napi::Object image_obj = constructor->New({arg});
image_obj.Set("_buffer", obj);
return scope.Escape(napi_value(image_obj)).ToObject();
}
Expand Down
6 changes: 3 additions & 3 deletions src/mapnik_image_from_svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct AsyncFromSVG : Napi::AsyncWorker
if (image_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -245,7 +245,7 @@ struct AsyncFromSVGBytes : Napi::AsyncWorker
if (image_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -432,7 +432,7 @@ Napi::Value Image::from_svg_sync_impl(Napi::CallbackInfo const& info, bool from_

image_ptr imagep = std::make_shared<mapnik::image_any>(im);
Napi::Value arg = Napi::External<image_ptr>::New(env, &imagep);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return scope.Escape(napi_value(obj)).ToObject();
}
catch (std::exception const& ex)
Expand Down
4 changes: 2 additions & 2 deletions src/mapnik_image_open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct AsyncOpen : Napi::AsyncWorker
if (image_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -99,7 +99,7 @@ Napi::Value Image::openSync(Napi::CallbackInfo const& info)
mapnik::set_premultiplied_alpha(*imagep, true);
}
Napi::Value arg = Napi::External<image_ptr>::New(env, &imagep);
Napi::Object obj = constructor.New({arg});
Napi::Object obj = NewInstance(env, arg);
return scope.Escape(napi_value(obj)).ToObject();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapnik_image_resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct AsyncResize : Napi::AsyncWorker
if (image_out_)
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_out_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}
return Base::GetResult(env);
Expand Down Expand Up @@ -594,7 +594,7 @@ Napi::Value Image::resizeSync(Napi::CallbackInfo const& info)
corrected_offset_y);
mapnik::util::apply_visitor(visit, *image_out);
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_out);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return scope.Escape(obj);
}
catch (std::exception const& ex)
Expand Down
4 changes: 2 additions & 2 deletions src/mapnik_map_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct AsyncRenderImage : AsyncRender
std::vector<napi_value> GetResult(Napi::Env env) override
{
Napi::Value arg = Napi::External<image_ptr>::New(env, &image_);
Napi::Object obj = Image::constructor.New({arg});
Napi::Object obj = Image::NewInstance(env, arg);
return {env.Null(), napi_value(obj)};
}

Expand Down Expand Up @@ -550,7 +550,7 @@ Napi::Value Map::render(Napi::CallbackInfo const& info)

Napi::Object obj = info[0].As<Napi::Object>();

if (obj.InstanceOf(Image::constructor.Value()))
if (obj.InstanceOf(Image::Constructor(env)))
{
image_ptr image = Napi::ObjectWrap<Image>::Unwrap(obj)->impl();
mapnik::attributes variables;
Expand Down
4 changes: 2 additions & 2 deletions src/mapnik_vector_tile_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Napi::Value VectorTile::addImageSync(Napi::CallbackInfo const& info)

std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
if (!obj.InstanceOf(Image::Constructor(env)))
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return scope.Escape(env.Undefined());
Expand Down Expand Up @@ -253,7 +253,7 @@ Napi::Value VectorTile::addImage(Napi::CallbackInfo const& info)
}
std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
if (!obj.InstanceOf(Image::Constructor(env)))
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return env.Undefined();
Expand Down
2 changes: 1 addition & 1 deletion src/mapnik_vector_tile_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ Napi::Value VectorTile::render(Napi::CallbackInfo const& info)
unsigned height = 0;
surface_type surface;
bool use_cairo = false;
if (im_obj.InstanceOf(Image::constructor.Value()))
if (im_obj.InstanceOf(Image::Constructor(env)))
{
Image* im = Napi::ObjectWrap<Image>::Unwrap(im_obj);
width = im->impl()->width();
Expand Down

0 comments on commit 95e3d96

Please sign in to comment.