From 57c1e3ae26b40a495f8c3651cc927984c7c70e90 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sun, 31 Jan 2016 11:18:31 +0000 Subject: [PATCH] Slightly simplify marshalling of data from V8 Objects --- src/pipeline.cc | 115 ++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/pipeline.cc b/src/pipeline.cc index b5340d12..e37fad7b 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -964,6 +964,14 @@ class PipelineWorker : public AsyncWorker { } }; +// Convenience methods to access the attributes of a V8::Object +template T attrAs(Handle obj, std::string attr) { + return To(Get(obj, New(attr).ToLocalChecked()).ToLocalChecked()).FromJust(); +} +static std::string attrAsStr(Handle obj, std::string attr) { + return *Utf8String(Get(obj, New(attr).ToLocalChecked()).ToLocalChecked()); +} + /* pipeline(options, output, callback) */ @@ -975,9 +983,8 @@ NAN_METHOD(pipeline) { Local options = info[0].As(); // Input filename - baton->fileIn = *Utf8String(Get(options, New("fileIn").ToLocalChecked()).ToLocalChecked()); - baton->accessMethod = - To(Get(options, New("sequentialRead").ToLocalChecked()).ToLocalChecked()).FromJust() ? + baton->fileIn = attrAsStr(options, "fileIn"); + baton->accessMethod = attrAs(options, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM; // Input Buffer object Local bufferIn; @@ -987,32 +994,32 @@ NAN_METHOD(pipeline) { baton->bufferIn = node::Buffer::Data(bufferIn); } // ICC profile to use when input CMYK image has no embedded profile - baton->iccProfilePath = *Utf8String(Get(options, New("iccProfilePath").ToLocalChecked()).ToLocalChecked()); + baton->iccProfilePath = attrAsStr(options, "iccProfilePath"); // Limit input images to a given number of pixels, where pixels = width * height - baton->limitInputPixels = To(Get(options, New("limitInputPixels").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->limitInputPixels = attrAs(options, "limitInputPixels"); // Extract image options - baton->topOffsetPre = To(Get(options, New("topOffsetPre").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->leftOffsetPre = To(Get(options, New("leftOffsetPre").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->widthPre = To(Get(options, New("widthPre").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->heightPre = To(Get(options, New("heightPre").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->topOffsetPost = To(Get(options, New("topOffsetPost").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->leftOffsetPost = To(Get(options, New("leftOffsetPost").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->widthPost = To(Get(options, New("widthPost").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->heightPost = To(Get(options, New("heightPost").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->topOffsetPre = attrAs(options, "topOffsetPre"); + baton->leftOffsetPre = attrAs(options, "leftOffsetPre"); + baton->widthPre = attrAs(options, "widthPre"); + baton->heightPre = attrAs(options, "heightPre"); + baton->topOffsetPost = attrAs(options, "topOffsetPost"); + baton->leftOffsetPost = attrAs(options, "leftOffsetPost"); + baton->widthPost = attrAs(options, "widthPost"); + baton->heightPost = attrAs(options, "heightPost"); // Output image dimensions - baton->width = To(Get(options, New("width").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->height = To(Get(options, New("height").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->width = attrAs(options, "width"); + baton->height = attrAs(options, "height"); // Canvas option - Local canvas = To(Get(options, New("canvas").ToLocalChecked()).ToLocalChecked()).ToLocalChecked(); - if (Equals(canvas, New("crop").ToLocalChecked()).FromJust()) { + std::string canvas = attrAsStr(options, "canvas"); + if (canvas == "crop") { baton->canvas = Canvas::CROP; - } else if (Equals(canvas, New("embed").ToLocalChecked()).FromJust()) { + } else if (canvas == "embed") { baton->canvas = Canvas::EMBED; - } else if (Equals(canvas, New("max").ToLocalChecked()).FromJust()) { + } else if (canvas == "max") { baton->canvas = Canvas::MAX; - } else if (Equals(canvas, New("min").ToLocalChecked()).FromJust()) { + } else if (canvas == "min") { baton->canvas = Canvas::MIN; - } else if (Equals(canvas, New("ignore_aspect").ToLocalChecked()).FromJust()) { + } else if (canvas == "ignore_aspect") { baton->canvas = Canvas::IGNORE_ASPECT; } // Background colour @@ -1021,43 +1028,45 @@ NAN_METHOD(pipeline) { baton->background[i] = To(Get(background, i).ToLocalChecked()).FromJust(); } // Overlay options - baton->overlayPath = *Utf8String(Get(options, New("overlayPath").ToLocalChecked()).ToLocalChecked()); + baton->overlayPath = attrAsStr(options, "overlayPath"); // Resize options - baton->withoutEnlargement = To(Get(options, New("withoutEnlargement").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->gravity = To(Get(options, New("gravity").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->interpolator = *Utf8String(Get(options, New("interpolator").ToLocalChecked()).ToLocalChecked()); + baton->withoutEnlargement = attrAs(options, "withoutEnlargement"); + baton->gravity = attrAs(options, "gravity"); + baton->interpolator = attrAsStr(options, "interpolator"); // Operators - baton->flatten = To(Get(options, New("flatten").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->negate = To(Get(options, New("negate").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->blurSigma = To(Get(options, New("blurSigma").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->sharpenRadius = To(Get(options, New("sharpenRadius").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->sharpenFlat = To(Get(options, New("sharpenFlat").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->sharpenJagged = To(Get(options, New("sharpenJagged").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->threshold = To(Get(options, New("threshold").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->gamma = To(Get(options, New("gamma").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->greyscale = To(Get(options, New("greyscale").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->normalize = To(Get(options, New("normalize").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->angle = To(Get(options, New("angle").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->rotateBeforePreExtract = To(Get(options, New("rotateBeforePreExtract").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->flip = To(Get(options, New("flip").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->flop = To(Get(options, New("flop").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->flatten = attrAs(options, "flatten"); + baton->negate = attrAs(options, "negate"); + baton->blurSigma = attrAs(options, "blurSigma"); + baton->sharpenRadius = attrAs(options, "sharpenRadius"); + baton->sharpenFlat = attrAs(options, "sharpenFlat"); + baton->sharpenJagged = attrAs(options, "sharpenJagged"); + baton->threshold = attrAs(options, "threshold"); + baton->gamma = attrAs(options, "gamma"); + baton->greyscale = attrAs(options, "greyscale"); + baton->normalize = attrAs(options, "normalize"); + baton->angle = attrAs(options, "angle"); + baton->rotateBeforePreExtract = attrAs(options, "rotateBeforePreExtract"); + baton->flip = attrAs(options, "flip"); + baton->flop = attrAs(options, "flop"); // Output options - baton->progressive = To(Get(options, New("progressive").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->quality = To(Get(options, New("quality").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->compressionLevel = To(Get(options, New("compressionLevel").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->withoutAdaptiveFiltering = To(Get(options, New("withoutAdaptiveFiltering").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->withoutChromaSubsampling = To(Get(options, New("withoutChromaSubsampling").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->trellisQuantisation = To(Get(options, New("trellisQuantisation").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->overshootDeringing = To(Get(options, New("overshootDeringing").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->optimiseScans = To(Get(options, New("optimiseScans").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->withMetadata = To(Get(options, New("withMetadata").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->withMetadataOrientation = To(Get(options, New("withMetadataOrientation").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->progressive = attrAs(options, "progressive"); + baton->quality = attrAs(options, "quality"); + baton->compressionLevel = attrAs(options, "compressionLevel"); + baton->withoutAdaptiveFiltering = attrAs(options, "withoutAdaptiveFiltering"); + baton->withoutChromaSubsampling = attrAs(options, "withoutChromaSubsampling"); + baton->trellisQuantisation = attrAs(options, "trellisQuantisation"); + baton->overshootDeringing = attrAs(options, "overshootDeringing"); + baton->optimiseScans = attrAs(options, "optimiseScans"); + baton->withMetadata = attrAs(options, "withMetadata"); + baton->withMetadataOrientation = attrAs(options, "withMetadataOrientation"); // Output filename or __format for Buffer - baton->output = *Utf8String(Get(options, New("output").ToLocalChecked()).ToLocalChecked()); - baton->tileSize = To(Get(options, New("tileSize").ToLocalChecked()).ToLocalChecked()).FromJust(); - baton->tileOverlap = To(Get(options, New("tileOverlap").ToLocalChecked()).ToLocalChecked()).FromJust(); + baton->output = attrAsStr(options, "output"); + baton->tileSize = attrAs(options, "tileSize"); + baton->tileOverlap = attrAs(options, "tileOverlap"); // Function to notify of queue length changes - Callback *queueListener = new Callback(Get(options, New("queueListener").ToLocalChecked()).ToLocalChecked().As()); + Callback *queueListener = new Callback( + Get(options, New("queueListener").ToLocalChecked()).ToLocalChecked().As() + ); // Join queue for worker thread Callback *callback = new Callback(info[1].As());