From 1037e8a9a0547e7f78f47812e6627b56315b47e9 Mon Sep 17 00:00:00 2001 From: bjcscat <87087344+bjcscat@users.noreply.github.com> Date: Sat, 3 Dec 2022 13:49:28 -0600 Subject: [PATCH] use new typing and functions for more "safety" (#332) --- natives/blur.cc | 4 ++-- natives/blur.h | 2 +- natives/caption.cc | 8 ++++---- natives/caption.h | 2 +- natives/caption2.cc | 10 +++++----- natives/caption2.h | 2 +- natives/circle.cc | 2 +- natives/circle.h | 2 +- natives/colors.cc | 4 ++-- natives/colors.h | 2 +- natives/common.h | 26 ++++++++++++++++++++++++++ natives/crop.cc | 2 +- natives/crop.h | 2 +- natives/deepfry.cc | 2 +- natives/deepfry.h | 2 +- natives/explode.cc | 6 +++--- natives/explode.h | 2 +- natives/flag.cc | 6 +++--- natives/flag.h | 2 +- natives/flip.cc | 4 ++-- natives/flip.h | 2 +- natives/image.cc | 7 +++++-- natives/uncaption.cc | 4 ++-- natives/uncaption.h | 2 +- natives/watermark.cc | 20 ++++++++++---------- natives/watermark.h | 2 +- 26 files changed, 79 insertions(+), 50 deletions(-) diff --git a/natives/blur.cc b/natives/blur.cc index 0f27b3c..0324b7f 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -8,8 +8,8 @@ using namespace std; using namespace vips; char *Blur(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { - bool sharp = MAP_GET(Arguments, "sharp", bool); + ArgumentMap Arguments, size_t *DataSize) { + bool sharp = GetArgument(Arguments, "sharp"); VOption *options = VImage::option()->set("access", "sequential"); VImage in = diff --git a/natives/blur.h b/natives/blur.h index b0c14ea..af72efb 100644 --- a/natives/blur.h +++ b/natives/blur.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Blur(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Blur(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption.cc b/natives/caption.cc index c1f0599..c5968da 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -8,11 +8,11 @@ using namespace std; using namespace vips; char *Caption(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - string caption = MAP_GET(Arguments, "caption", string); - string font = MAP_GET(Arguments, "font", string); - string basePath = MAP_GET(Arguments, "basePath", string); + string caption = GetArgument(Arguments, "caption"); + string font = GetArgument(Arguments, "font"); + string basePath = GetArgument(Arguments, "basePath"); VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/caption.h b/natives/caption.h index b1266ec..a0a822f 100644 --- a/natives/caption.h +++ b/natives/caption.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Caption(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Caption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/caption2.cc b/natives/caption2.cc index b517ceb..d4c1dcc 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -8,12 +8,12 @@ using namespace std; using namespace vips; char *CaptionTwo(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - bool top = MAP_GET(Arguments, "top", bool); - string caption = MAP_GET(Arguments, "caption", string); - string font = MAP_GET(Arguments, "font", string); - string basePath = MAP_GET(Arguments, "basePath", string); + bool top = GetArgument(Arguments, "top"); + string caption = GetArgument(Arguments, "caption"); + string font = GetArgument(Arguments, "font"); + string basePath = GetArgument(Arguments, "basePath"); VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/caption2.h b/natives/caption2.h index 7fd17bc..2c9149e 100644 --- a/natives/caption2.h +++ b/natives/caption2.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* CaptionTwo(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/circle.cc b/natives/circle.cc index 4772393..aca74ae 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -11,7 +11,7 @@ using namespace std; using namespace Magick; char *Circle(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { Blob blob; diff --git a/natives/circle.h b/natives/circle.h index be378da..5cbce38 100644 --- a/natives/circle.h +++ b/natives/circle.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Circle(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Circle(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/colors.cc b/natives/colors.cc index 2ffc002..1156bb7 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -11,9 +11,9 @@ VImage sepia = VImage::new_matrixv(3, 3, 0.3588, 0.7044, 0.1368, 0.2990, 0.5870, 0.1140, 0.2392, 0.4696, 0.0912); char *Colors(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - string color = MAP_GET(Arguments, "color", string); + string color = GetArgument(Arguments, "color"); VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/colors.h b/natives/colors.h index ea11984..435cafd 100644 --- a/natives/colors.h +++ b/natives/colors.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Colors(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Colors(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/common.h b/natives/common.h index ebca0fe..68b6f8e 100644 --- a/natives/common.h +++ b/natives/common.h @@ -2,8 +2,34 @@ #include #include +#include #include +using std::string; +using std::variant; +using std::map; + +typedef variant ArgumentVariant; +typedef map ArgumentMap; + +template +T GetArgument(ArgumentMap map, string key) { + try { + return std::get(map.at(key)); + } catch (std::bad_variant_access&) { + throw "Invalid requested type from variant."; + } +} + +template +T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) { + try { + return std::get(map.at(key)); + } catch (...) { // this is, not great... + return fallback; + } +} + #define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0) #define MAP_GET(ARRAY, KEY, TYPE) (MAP_HAS(ARRAY, KEY) ? get(ARRAY.at(KEY)) : NULL) // C++ has forced my hand #define MAP_GET_FALLBACK(ARRAY, KEY, TYPE, FALLBACK) (MAP_HAS(ARRAY, KEY) ? get(ARRAY.at(KEY)) : FALLBACK) diff --git a/natives/crop.cc b/natives/crop.cc index d74badb..16b21a2 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -7,7 +7,7 @@ using namespace std; using namespace vips; char *Crop(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/crop.h b/natives/crop.h index a3ab89e..e497d94 100644 --- a/natives/crop.h +++ b/natives/crop.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Crop(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Crop(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/deepfry.cc b/natives/deepfry.cc index 5aa0941..0ed23b2 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -6,7 +6,7 @@ using namespace std; using namespace vips; char *Deepfry(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/deepfry.h b/natives/deepfry.h index fd8104e..fbfffce 100644 --- a/natives/deepfry.h +++ b/natives/deepfry.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Deepfry(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Deepfry(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/explode.cc b/natives/explode.cc index 9ab1a5d..c1675ff 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -12,10 +12,10 @@ using namespace std; using namespace Magick; char *Explode(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - int amount = MAP_GET(Arguments, "amount", int); - int delay = MAP_GET_FALLBACK(Arguments, "delay", int, 0); + int amount = GetArgument(Arguments, "amount"); + int delay = GetArgumentWithFallback(Arguments, "delay", 0); Blob blob; diff --git a/natives/explode.h b/natives/explode.h index 74427b6..d734d13 100644 --- a/natives/explode.h +++ b/natives/explode.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Explode(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Explode(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc index 75cf91c..c2d81dc 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -8,10 +8,10 @@ using namespace std; using namespace vips; char *Flag(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - string overlay = MAP_GET(Arguments, "overlay", string); - string basePath = MAP_GET(Arguments, "basePath", string); + string overlay = GetArgument(Arguments, "overlay"); + string basePath = GetArgument(Arguments, "basePath"); VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/flag.h b/natives/flag.h index da5fc46..c1f17e1 100644 --- a/natives/flag.h +++ b/natives/flag.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Flag(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Flag(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/flip.cc b/natives/flip.cc index c2ad6ea..2fd34ff 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -8,9 +8,9 @@ using namespace std; using namespace vips; char *Flip(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - bool flop = MAP_GET(Arguments, "flop", bool); + bool flop = GetArgument(Arguments, "flop"); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", type == "gif" diff --git a/natives/flip.h b/natives/flip.h index c910833..1654adc 100644 --- a/natives/flip.h +++ b/natives/flip.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Flip(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Flip(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index 7a717bb..54d04e5 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -51,7 +51,7 @@ using namespace std; -std::map Arguments, size_t* DataSize)> FunctionMap = { +std::map FunctionMap = { {"blur", &Blur}, {"caption", &Caption}, {"captionTwo", &CaptionTwo}, @@ -117,7 +117,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) { Napi::Array properties = obj.GetPropertyNames(); - std::map Arguments; + ArgumentMap Arguments; for (unsigned int i = 0; i < properties.Length(); i++) { string property = properties.Get(uint32_t(i)).As().Utf8Value(); @@ -138,6 +138,9 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) { } else { Arguments[property] = num.FloatValue(); } + } else { + throw "Unimplemented value type passed to image native."; + //Arguments[property] = val; } } diff --git a/natives/uncaption.cc b/natives/uncaption.cc index 11b0f93..30785c2 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -7,9 +7,9 @@ using namespace std; using namespace vips; char *Uncaption(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - float tolerance = MAP_GET_FALLBACK(Arguments, "tolerance", float, 0.5); + float tolerance = GetArgumentWithFallback(Arguments, "tolerance", 0.5); VOption *options = VImage::option(); diff --git a/natives/uncaption.h b/natives/uncaption.h index 6798490..b4bc5f3 100644 --- a/natives/uncaption.h +++ b/natives/uncaption.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Uncaption(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Uncaption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file diff --git a/natives/watermark.cc b/natives/watermark.cc index f1bb970..9df1822 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -7,23 +7,23 @@ using namespace std; using namespace vips; char *Watermark(string type, char *BufferData, size_t BufferLength, - map Arguments, size_t *DataSize) { + ArgumentMap Arguments, size_t *DataSize) { - string water = MAP_GET(Arguments, "water", string); - int gravity = MAP_GET(Arguments, "gravity", int); + string water = GetArgument(Arguments, "water"); + int gravity = GetArgument(Arguments, "gravity"); - bool resize = MAP_GET_FALLBACK(Arguments, "resize", bool, false); - ; - float yscale = MAP_GET_FALLBACK(Arguments, "yscale", float, false); + bool resize = GetArgumentWithFallback(Arguments, "resize", false); - bool append = MAP_GET_FALLBACK(Arguments, "append", bool, false); + float yscale = GetArgumentWithFallback(Arguments, "yscale", false); - bool alpha = MAP_GET_FALLBACK(Arguments, "alpha", bool, false); - bool flip = MAP_GET_FALLBACK(Arguments, "flip", bool, false); + bool append = GetArgumentWithFallback(Arguments, "append", false); + + bool alpha = GetArgumentWithFallback(Arguments, "alpha", false); + bool flip = GetArgumentWithFallback(Arguments, "flip", false); bool mc = MAP_HAS(Arguments, "mc"); - string basePath = MAP_GET(Arguments, "basePath", string); + string basePath = GetArgument(Arguments, "basePath"); VOption *options = VImage::option()->set("access", "sequential"); diff --git a/natives/watermark.h b/natives/watermark.h index 338a61a..0fa82e8 100644 --- a/natives/watermark.h +++ b/natives/watermark.h @@ -6,4 +6,4 @@ using std::map; using std::string; -char* Watermark(string type, char* BufferData, size_t BufferLength, map Arguments, size_t* DataSize); \ No newline at end of file +char* Watermark(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize); \ No newline at end of file