From 4ccaea7c917c7beec139099c969faf78b7792337 Mon Sep 17 00:00:00 2001 From: Essem Date: Mon, 27 Jun 2022 22:50:53 -0500 Subject: [PATCH] Properly clear vips error data --- natives/blur.cc | 13 +++++++------ natives/caption.cc | 20 ++++++++++++-------- natives/caption2.cc | 19 +++++++++++-------- natives/circle.cc | 9 +++++---- natives/colors.cc | 13 +++++++------ natives/crop.cc | 16 +++++++++------- natives/deepfry.cc | 42 +++++++++++++++++++++--------------------- natives/flag.cc | 11 +++++++---- natives/flip.cc | 13 +++++++------ natives/freeze.cc | 14 +++++++------- natives/gamexplain.cc | 13 +++++++------ natives/globe.cc | 11 +++++++---- natives/homebrew.cc | 13 +++++++------ natives/invert.cc | 13 +++++++------ natives/jpeg.cc | 17 +++++++---------- natives/magik.cc | 9 +++++---- natives/meme.cc | 13 +++++++------ natives/mirror.cc | 13 +++++++------ natives/motivate.cc | 11 +++++++---- natives/reddit.cc | 14 ++++++++------ natives/resize.cc | 13 +++++++------ natives/reverse.cc | 13 +++++++------ natives/scott.cc | 9 +++++---- natives/snapchat.cc | 13 +++++++------ natives/sonic.cc | 13 +++++++------ natives/speed.cc | 14 +++++++------- natives/tile.cc | 9 +++++---- natives/togif.cc | 39 +++++++++++++++++++-------------------- natives/uncaption.cc | 13 +++++++------ natives/wall.cc | 9 +++++---- natives/watermark.cc | 13 +++++++------ natives/whisper.cc | 13 +++++++------ natives/zamn.cc | 13 +++++++------ 33 files changed, 259 insertions(+), 222 deletions(-) diff --git a/natives/blur.cc b/natives/blur.cc index 0052a89..fd281c8 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Blur(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -31,15 +32,15 @@ Napi::Value Blur(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/caption.cc b/natives/caption.cc index bd35bd8..34ac990 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -7,6 +7,8 @@ using namespace vips; Napi::Value Caption(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); + try { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); @@ -20,7 +22,8 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { VImage::new_from_buffer(data.Data(), data.Length(), "", type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); - if (!in.has_alpha()) in = in.bandjoin(255); + if (!in.has_alpha()) + in = in.bandjoin(255); int width = in.width(); int size = width / 10; @@ -62,17 +65,18 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { size_t length; final.write_to_buffer( ("." + type).c_str(), &buf, &length, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); + type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::New(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/caption2.cc b/natives/caption2.cc index 36cf43c..b9d1cd0 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -22,7 +23,8 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { VImage::new_from_buffer(data.Data(), data.Length(), "", type == "gif" ? options->set("n", -1) : options) .colourspace(VIPS_INTERPRETATION_sRGB); - if (!in.has_alpha()) in = in.bandjoin(255); + if (!in.has_alpha()) + in = in.bandjoin(255); int width = in.width(); int size = width / 13; @@ -66,17 +68,18 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { size_t length; final.write_to_buffer( ("." + type).c_str(), &buf, &length, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); + type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/circle.cc b/natives/circle.cc index 02e2c43..f02c372 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -9,6 +9,7 @@ using namespace Magick; Napi::Value Circle(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -46,14 +47,14 @@ Napi::Value Circle(const Napi::CallbackInfo &info) { writeImages(blurred.begin(), blurred.end(), &blob); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + return result; } \ No newline at end of file diff --git a/natives/colors.cc b/natives/colors.cc index f22c4ba..dcd4e50 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -10,6 +10,7 @@ VImage sepia = VImage::new_matrixv(3, 3, 0.3588, 0.7044, 0.1368, 0.2990, 0.5870, Napi::Value Colors(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -36,15 +37,15 @@ Napi::Value Colors(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/crop.cc b/natives/crop.cc index 782af93..548a39c 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Crop(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -48,17 +49,18 @@ Napi::Value Crop(const Napi::CallbackInfo &info) { size_t length; final.write_to_buffer( ("." + type).c_str(), &buf, &length, - type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); + type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) + : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/deepfry.cc b/natives/deepfry.cc index 240f33b..c04a3c0 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -7,14 +7,13 @@ using namespace vips; Napi::Value Deepfry(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - Napi::Object result = Napi::Object::New(env); - VOption *options = VImage::option()->set("access", "sequential"); VImage in = @@ -34,20 +33,20 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) { VImage final; if (totalHeight > 65500 && type == "gif") { vector img; - for (int i = 0; i < nPages; i++) { - VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); - void *jpgBuf; - size_t jpgLength; - img_frame.write_to_buffer( - ".jpg", &jpgBuf, &jpgLength, - VImage::option()->set("Q", 1)->set("strip", true)); - VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, ""); - jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight); - jpeged.set("delay", in.get_array_int("delay")); - img.push_back(jpeged); - } - final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); - final.set(VIPS_META_PAGE_HEIGHT, pageHeight); + for (int i = 0; i < nPages; i++) { + VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); + void *jpgBuf; + size_t jpgLength; + img_frame.write_to_buffer( + ".jpg", &jpgBuf, &jpgLength, + VImage::option()->set("Q", 1)->set("strip", true)); + VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, ""); + jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight); + jpeged.set("delay", in.get_array_int("delay")); + img.push_back(jpeged); + } + final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); + final.set(VIPS_META_PAGE_HEIGHT, pageHeight); } else { void *jpgBuf; size_t jpgLength; @@ -65,14 +64,15 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) { type == "gif" ? VImage::option()->set("dither", 0) : 0); - vips_thread_shutdown(); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc index b26be47..990a672 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Flag(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -58,13 +59,15 @@ Napi::Value Flag(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/flip.cc b/natives/flip.cc index 2426459..a73195a 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Flip(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -46,15 +47,15 @@ Napi::Value Flip(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/freeze.cc b/natives/freeze.cc index 68e5cf4..0fc7abd 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Freeze(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -18,8 +19,6 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { ? obj.Get("frame").As().Int32Value() : -1; - Napi::Object result = Napi::Object::New(env); - char *fileData = data.Data(); char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01"; char *descriptor = (char *)"\x2C\x00\x00\x00\x00"; @@ -69,8 +68,6 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); } else { lastPos = (char *)memchr(fileData, '\x21', data.Length()); @@ -93,10 +90,13 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { } result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index bb3aabf..1799319 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Gamexplain(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -51,15 +52,15 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/globe.cc b/natives/globe.cc index 69ab03b..fc53dda 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Globe(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -77,13 +78,15 @@ Napi::Value Globe(const Napi::CallbackInfo &info) { size_t length; final.write_to_buffer(".gif", &buf, &length); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", "gif"); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/homebrew.cc b/natives/homebrew.cc index 7fbf7e6..a34ca63 100644 --- a/natives/homebrew.cc +++ b/natives/homebrew.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Homebrew(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -34,15 +35,15 @@ Napi::Value Homebrew(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(".png", &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", "png"); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/invert.cc b/natives/invert.cc index 708116f..21b678a 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Invert(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -30,15 +31,15 @@ Napi::Value Invert(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/jpeg.cc b/natives/jpeg.cc index aa72034..858d2b3 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Jpeg(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -16,8 +17,6 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { : 0; string type = obj.Get("type").As().Utf8Value(); - Napi::Object result = Napi::Object::New(env); - if (type == "gif") { VImage in = VImage::new_from_buffer( @@ -67,8 +66,6 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { type == "gif" ? VImage::option()->set("dither", 0) : 0); - vips_thread_shutdown(); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); } else { @@ -79,16 +76,16 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { ".jpg", &buf, &length, VImage::option()->set("Q", quality)->set("strip", true)); - vips_thread_shutdown(); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", "jpg"); } - - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/magik.cc b/natives/magik.cc index fc65bb8..da36307 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -9,6 +9,7 @@ using namespace Magick; Napi::Value Magik(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -48,14 +49,14 @@ Napi::Value Magik(const Napi::CallbackInfo &info) { writeImages(blurred.begin(), blurred.end(), &blob); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + return result; } \ No newline at end of file diff --git a/natives/meme.cc b/natives/meme.cc index a8443a0..cc2d6e7 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Meme(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -130,15 +131,15 @@ Napi::Value Meme(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/mirror.cc b/natives/mirror.cc index d2cd0be..8d6b3b2 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Mirror(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -69,15 +70,15 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/motivate.cc b/natives/motivate.cc index af506fc..9700bb3 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Motivate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -108,13 +109,15 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 1) : 0); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/reddit.cc b/natives/reddit.cc index 41bc4fb..45aaafb 100644 --- a/natives/reddit.cc +++ b/natives/reddit.cc @@ -7,6 +7,8 @@ using namespace vips; Napi::Value Reddit(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); + try { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); @@ -61,15 +63,15 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/resize.cc b/natives/resize.cc index 7904db0..157d32f 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Resize(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -59,15 +60,15 @@ Napi::Value Resize(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/reverse.cc b/natives/reverse.cc index ad41db2..90789a3 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Reverse(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -57,15 +58,15 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) { final.write_to_buffer(".gif", &buf, &length, VImage::option()->set("dither", 0)); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", "gif"); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/scott.cc b/natives/scott.cc index 94ba1c6..523c341 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -9,6 +9,7 @@ using namespace Magick; Napi::Value Scott(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -59,14 +60,14 @@ Napi::Value Scott(const Napi::CallbackInfo &info) { writeImages(mid.begin(), mid.end(), &blob); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + return result; } \ No newline at end of file diff --git a/natives/snapchat.cc b/natives/snapchat.cc index 18bb5d2..fda09ca 100644 --- a/natives/snapchat.cc +++ b/natives/snapchat.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Snapchat(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -69,15 +70,15 @@ Napi::Value Snapchat(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/sonic.cc b/natives/sonic.cc index d86848e..b432ce2 100644 --- a/natives/sonic.cc +++ b/natives/sonic.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Sonic(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -33,15 +34,15 @@ Napi::Value Sonic(const Napi::CallbackInfo &info) { size_t length; out.write_to_buffer(".png", &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", "png"); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/speed.cc b/natives/speed.cc index 9aded07..3c0b008 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -37,13 +37,12 @@ void vipsRemove(Napi::Env *env, Napi::Object *result, Napi::Buffer data, size_t length; out.write_to_buffer(".gif", &buf, &length); - vips_thread_shutdown(); - result->Set("data", Napi::Buffer::Copy(*env, (char *)buf, length)); } Napi::Value Speed(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -54,8 +53,6 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { int speed = obj.Has("speed") ? obj.Get("speed").As().Int32Value() : 2; - Napi::Object result = Napi::Object::New(env); - char *fileData = data.Data(); char *match = (char *)"\x00\x21\xF9\x04"; @@ -106,10 +103,13 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { if (removeFrames) vipsRemove(&env, &result, data, speed); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/tile.cc b/natives/tile.cc index 0932b0e..03f238f 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -9,6 +9,7 @@ using namespace Magick; Napi::Value Tile(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -61,14 +62,14 @@ Napi::Value Tile(const Napi::CallbackInfo &info) { writeImages(mid.begin(), mid.end(), &blob); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + return result; } \ No newline at end of file diff --git a/natives/togif.cc b/natives/togif.cc index 9999809..5095410 100644 --- a/natives/togif.cc +++ b/natives/togif.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value ToGif(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -14,30 +15,28 @@ Napi::Value ToGif(const Napi::CallbackInfo &info) { string type = obj.Get("type").As().Utf8Value(); if (type == "gif") { - Napi::Object result = Napi::Object::New(env); result.Set("data", data); result.Set("type", "gif"); - return result; + } else { + VOption *options = VImage::option()->set("access", "sequential"); + + VImage in = + VImage::new_from_buffer(data.Data(), data.Length(), "", options); + + void *buf; + size_t length; + in.write_to_buffer(".gif", &buf, &length); + + result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); + result.Set("type", "gif"); } - - VOption *options = VImage::option()->set("access", "sequential"); - - VImage in = - VImage::new_from_buffer(data.Data(), data.Length(), "", options); - - void *buf; - size_t length; - in.write_to_buffer(".gif", &buf, &length); - - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); - result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); - result.Set("type", "gif"); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/uncaption.cc b/natives/uncaption.cc index 95cdb39..bb4aa68 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Uncaption(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -54,15 +55,15 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/wall.cc b/natives/wall.cc index 2686fbb..b6fab85 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -9,6 +9,7 @@ using namespace Magick; Napi::Value Wall(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -54,14 +55,14 @@ Napi::Value Wall(const Napi::CallbackInfo &info) { writeImages(mid.begin(), mid.end(), &blob); - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + return result; } \ No newline at end of file diff --git a/natives/watermark.cc b/natives/watermark.cc index cdac5cb..3588b52 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Watermark(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -156,15 +157,15 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } diff --git a/natives/whisper.cc b/natives/whisper.cc index 12c9dff..5c24626 100644 --- a/natives/whisper.cc +++ b/natives/whisper.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Whisper(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -83,15 +84,15 @@ Napi::Value Whisper(const Napi::CallbackInfo &info) { ("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file diff --git a/natives/zamn.cc b/natives/zamn.cc index 9b05eb7..d4e51ab 100644 --- a/natives/zamn.cc +++ b/natives/zamn.cc @@ -7,6 +7,7 @@ using namespace vips; Napi::Value Zamn(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); + Napi::Object result = Napi::Object::New(env); try { Napi::Object obj = info[0].As(); @@ -47,15 +48,15 @@ Napi::Value Zamn(const Napi::CallbackInfo &info) { size_t length; final.write_to_buffer(("." + type).c_str(), &buf, &length); - vips_thread_shutdown(); - - Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)buf, length)); result.Set("type", type); - return result; } catch (std::exception const &err) { - throw Napi::Error::New(env, err.what()); + Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); } catch (...) { - throw Napi::Error::New(env, "Unknown error"); + Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); } + + vips_error_clear(); + vips_thread_shutdown(); + return result; } \ No newline at end of file