diff --git a/src/Canvas.cc b/src/Canvas.cc index a7318ca82..860d5bb46 100644 --- a/src/Canvas.cc +++ b/src/Canvas.cc @@ -35,6 +35,12 @@ "with at least a family (string) and optionally weight (string/number) " \ "and style (string)." +#define CHECK_RECEIVER(prop) \ + if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { \ + Nan::ThrowTypeError("Method " #prop " called on incompatible receiver"); \ + return; \ + } + using namespace v8; using namespace std; @@ -144,10 +150,7 @@ NAN_METHOD(Canvas::New) { */ NAN_GETTER(Canvas::GetType) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.GetType called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.GetType); Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(canvas->backend()->getName()).ToLocalChecked()); } @@ -156,10 +159,7 @@ NAN_GETTER(Canvas::GetType) { * Get stride. */ NAN_GETTER(Canvas::GetStride) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.GetStride called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.GetStride); Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(canvas->stride())); } @@ -169,10 +169,7 @@ NAN_GETTER(Canvas::GetStride) { */ NAN_GETTER(Canvas::GetWidth) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.GetWidth called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.GetWidth); Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(canvas->getWidth())); } @@ -182,10 +179,7 @@ NAN_GETTER(Canvas::GetWidth) { */ NAN_SETTER(Canvas::SetWidth) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.SetWidth called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.SetWidth); if (value->IsNumber()) { Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); canvas->backend()->setWidth(Nan::To(value).FromMaybe(0)); @@ -198,10 +192,7 @@ NAN_SETTER(Canvas::SetWidth) { */ NAN_GETTER(Canvas::GetHeight) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.GetHeight called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.GetHeight); Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(canvas->getHeight())); } @@ -211,10 +202,7 @@ NAN_GETTER(Canvas::GetHeight) { */ NAN_SETTER(Canvas::SetHeight) { - if (!Canvas::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Canvas.SetHeight called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Canvas.SetHeight); if (value->IsNumber()) { Canvas *canvas = Nan::ObjectWrap::Unwrap(info.This()); canvas->backend()->setHeight(Nan::To(value).FromMaybe(0)); @@ -973,3 +961,5 @@ Local Canvas::Error(cairo_status_t status) { return Exception::Error(Nan::New(cairo_status_to_string(status)).ToLocalChecked()); } + +#undef CHECK_RECEIVER diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 667e1cf93..699ec88fc 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -36,6 +36,12 @@ Nan::Persistent Context2d::constructor; double width = args[2]; \ double height = args[3]; +#define CHECK_RECEIVER(prop) \ + if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { \ + Nan::ThrowTypeError("Method " #prop " called on incompatible receiver"); \ + return; \ + } + constexpr double twoPi = M_PI * 2.; /* @@ -713,10 +719,7 @@ NAN_METHOD(Context2d::SaveExternalModules) { */ NAN_GETTER(Context2d::GetFormat) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetFormat called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetFormat); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); std::string pixelFormatString; switch (context->canvas()->backend()->getFormat()) { @@ -1396,10 +1399,7 @@ NAN_METHOD(Context2d::DrawImage) { */ NAN_GETTER(Context2d::GetGlobalAlpha) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetGlobalAlpha called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetGlobalAlpha); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(context->state->globalAlpha)); } @@ -1409,10 +1409,7 @@ NAN_GETTER(Context2d::GetGlobalAlpha) { */ NAN_SETTER(Context2d::SetGlobalAlpha) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetGlobalAlpha called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetGlobalAlpha); double n = Nan::To(value).FromMaybe(0); if (n >= 0 && n <= 1) { Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); @@ -1425,10 +1422,7 @@ NAN_SETTER(Context2d::SetGlobalAlpha) { */ NAN_GETTER(Context2d::GetGlobalCompositeOperation) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetGlobalCompositeOperation called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetGlobalCompositeOperation); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); @@ -1479,10 +1473,7 @@ NAN_GETTER(Context2d::GetGlobalCompositeOperation) { */ NAN_SETTER(Context2d::SetPatternQuality) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetPatternQuality called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetPatternQuality); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Nan::Utf8String quality(Nan::To(value).ToLocalChecked()); if (0 == strcmp("fast", *quality)) { @@ -1503,10 +1494,7 @@ NAN_SETTER(Context2d::SetPatternQuality) { */ NAN_GETTER(Context2d::GetPatternQuality) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetPatternQuality called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetPatternQuality); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *quality; switch (context->state->patternQuality) { @@ -1524,10 +1512,7 @@ NAN_GETTER(Context2d::GetPatternQuality) { */ NAN_SETTER(Context2d::SetImageSmoothingEnabled) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetImageSmoothingEnabled called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetImageSmoothingEnabled); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); context->state->imageSmoothingEnabled = Nan::To(value).FromMaybe(false); } @@ -1537,10 +1522,7 @@ NAN_SETTER(Context2d::SetImageSmoothingEnabled) { */ NAN_GETTER(Context2d::GetImageSmoothingEnabled) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetImageSmoothingEnabled called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetImageSmoothingEnabled); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(context->state->imageSmoothingEnabled)); } @@ -1550,10 +1532,7 @@ NAN_GETTER(Context2d::GetImageSmoothingEnabled) { */ NAN_SETTER(Context2d::SetGlobalCompositeOperation) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetGlobalCompositeOperation called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetGlobalCompositeOperation); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); Nan::Utf8String opStr(Nan::To(value).ToLocalChecked()); // Unlike CSS colors, this *is* case-sensitive @@ -1601,10 +1580,7 @@ NAN_SETTER(Context2d::SetGlobalCompositeOperation) { */ NAN_GETTER(Context2d::GetShadowOffsetX) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetShadowOffsetX called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetShadowOffsetX); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(context->state->shadowOffsetX)); } @@ -1614,10 +1590,7 @@ NAN_GETTER(Context2d::GetShadowOffsetX) { */ NAN_SETTER(Context2d::SetShadowOffsetX) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetShadowOffsetX called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetShadowOffsetX); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); context->state->shadowOffsetX = Nan::To(value).FromMaybe(0); } @@ -1627,10 +1600,7 @@ NAN_SETTER(Context2d::SetShadowOffsetX) { */ NAN_GETTER(Context2d::GetShadowOffsetY) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetShadowOffsetY called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetShadowOffsetY); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(context->state->shadowOffsetY)); } @@ -1640,10 +1610,7 @@ NAN_GETTER(Context2d::GetShadowOffsetY) { */ NAN_SETTER(Context2d::SetShadowOffsetY) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetShadowOffsetY called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetShadowOffsetY); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); context->state->shadowOffsetY = Nan::To(value).FromMaybe(0); } @@ -1653,10 +1620,7 @@ NAN_SETTER(Context2d::SetShadowOffsetY) { */ NAN_GETTER(Context2d::GetShadowBlur) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetShadowBlur called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetShadowBlur); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(context->state->shadowBlur)); } @@ -1666,10 +1630,7 @@ NAN_GETTER(Context2d::GetShadowBlur) { */ NAN_SETTER(Context2d::SetShadowBlur) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetShadowBlur called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetShadowBlur); int n = Nan::To(value).FromMaybe(0); if (n >= 0) { Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); @@ -1682,10 +1643,7 @@ NAN_SETTER(Context2d::SetShadowBlur) { */ NAN_GETTER(Context2d::GetAntiAlias) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetAntiAlias called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetAntiAlias); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *aa; switch (cairo_get_antialias(context->context())) { @@ -1702,10 +1660,7 @@ NAN_GETTER(Context2d::GetAntiAlias) { */ NAN_SETTER(Context2d::SetAntiAlias) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetAntiAlias called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetAntiAlias); Nan::Utf8String str(Nan::To(value).ToLocalChecked()); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); @@ -1729,10 +1684,7 @@ NAN_SETTER(Context2d::SetAntiAlias) { */ NAN_GETTER(Context2d::GetTextDrawingMode) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetTextDrawingMode called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetTextDrawingMode); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *mode; if (context->state->textDrawingMode == TEXT_DRAW_PATHS) { @@ -1750,10 +1702,7 @@ NAN_GETTER(Context2d::GetTextDrawingMode) { */ NAN_SETTER(Context2d::SetTextDrawingMode) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetTextDrawingMode called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetTextDrawingMode); Nan::Utf8String str(Nan::To(value).ToLocalChecked()); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); if (0 == strcmp("path", *str)) { @@ -1768,10 +1717,7 @@ NAN_SETTER(Context2d::SetTextDrawingMode) { */ NAN_GETTER(Context2d::GetQuality) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetQuality called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetQuality); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *filter; switch (cairo_pattern_get_filter(cairo_get_source(context->context()))) { @@ -1789,10 +1735,7 @@ NAN_GETTER(Context2d::GetQuality) { */ NAN_SETTER(Context2d::SetQuality) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetQuality called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetQuality); Nan::Utf8String str(Nan::To(value).ToLocalChecked()); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_filter_t filter; @@ -1855,10 +1798,7 @@ void parse_matrix_from_object(cairo_matrix_t &matrix, Local mat) { */ NAN_GETTER(Context2d::GetCurrentTransform) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetCurrentTransform called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetCurrentTransform); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Local instance = get_current_transform(context); @@ -1870,10 +1810,7 @@ NAN_GETTER(Context2d::GetCurrentTransform) { */ NAN_SETTER(Context2d::SetCurrentTransform) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetCurrentTransform called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetCurrentTransform); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Local ctx = Nan::GetCurrentContext(); Local mat = Nan::To(value).ToLocalChecked(); @@ -1895,10 +1832,7 @@ NAN_SETTER(Context2d::SetCurrentTransform) { */ NAN_GETTER(Context2d::GetFillStyle) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetFillStyle called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetFillStyle); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Isolate *iso = Isolate::GetCurrent(); Local style; @@ -1916,10 +1850,7 @@ NAN_GETTER(Context2d::GetFillStyle) { */ NAN_SETTER(Context2d::SetFillStyle) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetFillStyle called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetFillStyle); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); if (value->IsString()) { @@ -1947,10 +1878,7 @@ NAN_SETTER(Context2d::SetFillStyle) { */ NAN_GETTER(Context2d::GetStrokeStyle) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetStrokeStyle called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetStrokeStyle); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Local style; @@ -1967,10 +1895,7 @@ NAN_GETTER(Context2d::GetStrokeStyle) { */ NAN_SETTER(Context2d::SetStrokeStyle) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetStrokeStyle called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetStrokeStyle); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); if (value->IsString()) { @@ -1998,10 +1923,7 @@ NAN_SETTER(Context2d::SetStrokeStyle) { */ NAN_GETTER(Context2d::GetMiterLimit) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetMiterLimit called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetMiterLimit); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(cairo_get_miter_limit(context->context()))); } @@ -2011,10 +1933,7 @@ NAN_GETTER(Context2d::GetMiterLimit) { */ NAN_SETTER(Context2d::SetMiterLimit) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetMiterLimit called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetMiterLimit); double n = Nan::To(value).FromMaybe(0); if (n > 0) { Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); @@ -2027,10 +1946,7 @@ NAN_SETTER(Context2d::SetMiterLimit) { */ NAN_GETTER(Context2d::GetLineWidth) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetLineWidth called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetLineWidth); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); info.GetReturnValue().Set(Nan::New(cairo_get_line_width(context->context()))); } @@ -2040,10 +1956,7 @@ NAN_GETTER(Context2d::GetLineWidth) { */ NAN_SETTER(Context2d::SetLineWidth) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetLineWidth called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetLineWidth); double n = Nan::To(value).FromMaybe(0); if (n > 0 && n != std::numeric_limits::infinity()) { Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); @@ -2056,10 +1969,7 @@ NAN_SETTER(Context2d::SetLineWidth) { */ NAN_GETTER(Context2d::GetLineJoin) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetLineJoin called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetLineJoin); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *join; switch (cairo_get_line_join(context->context())) { @@ -2075,10 +1985,7 @@ NAN_GETTER(Context2d::GetLineJoin) { */ NAN_SETTER(Context2d::SetLineJoin) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetLineJoin called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetLineJoin); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); Nan::Utf8String type(Nan::To(value).ToLocalChecked()); @@ -2096,10 +2003,7 @@ NAN_SETTER(Context2d::SetLineJoin) { */ NAN_GETTER(Context2d::GetLineCap) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetLineCap called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetLineCap); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char *cap; switch (cairo_get_line_cap(context->context())) { @@ -2115,10 +2019,7 @@ NAN_GETTER(Context2d::GetLineCap) { */ NAN_SETTER(Context2d::SetLineCap) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetLineCap called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetLineCap); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); Nan::Utf8String type(Nan::To(value).ToLocalChecked()); @@ -2153,10 +2054,7 @@ NAN_METHOD(Context2d::IsPointInPath) { */ NAN_SETTER(Context2d::SetShadowColor) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetShadowColor called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetShadowColor); short ok; Nan::Utf8String str(Nan::To(value).ToLocalChecked()); uint32_t rgba = rgba_from_string(*str, &ok); @@ -2171,10 +2069,7 @@ NAN_SETTER(Context2d::SetShadowColor) { */ NAN_GETTER(Context2d::GetShadowColor) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetShadowColor called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetShadowColor); char buf[64]; Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); rgba_to_string(context->state->shadow, buf, sizeof(buf)); @@ -2649,10 +2544,7 @@ NAN_METHOD(Context2d::MoveTo) { */ NAN_GETTER(Context2d::GetFont) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetFont called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetFont); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); Isolate *iso = Isolate::GetCurrent(); Local font; @@ -2675,10 +2567,7 @@ NAN_GETTER(Context2d::GetFont) { */ NAN_SETTER(Context2d::SetFont) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetFont called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetFont); if (!value->IsString()) return; Isolate *iso = Isolate::GetCurrent(); @@ -2736,10 +2625,7 @@ NAN_SETTER(Context2d::SetFont) { */ NAN_GETTER(Context2d::GetTextBaseline) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetTextBaseline called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetTextBaseline); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char* baseline; switch (context->state->textBaseline) { @@ -2759,10 +2645,7 @@ NAN_GETTER(Context2d::GetTextBaseline) { */ NAN_SETTER(Context2d::SetTextBaseline) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetTextBaseline called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetTextBaseline); if (!value->IsString()) return; Nan::Utf8String opStr(Nan::To(value).ToLocalChecked()); @@ -2786,10 +2669,7 @@ NAN_SETTER(Context2d::SetTextBaseline) { */ NAN_GETTER(Context2d::GetTextAlign) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetTextAlign called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetTextAlign); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); const char* align; switch (context->state->textAlignment) { @@ -2809,10 +2689,7 @@ NAN_GETTER(Context2d::GetTextAlign) { */ NAN_SETTER(Context2d::SetTextAlign) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetTextAlign called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetTextAlign); if (!value->IsString()) return; Nan::Utf8String opStr(Nan::To(value).ToLocalChecked()); @@ -2975,10 +2852,7 @@ NAN_METHOD(Context2d::GetLineDash) { * ref: http://www.w3.org/TR/2dcontext/#dom-context-2d-setlinedash */ NAN_SETTER(Context2d::SetLineDashOffset) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.SetLineDashOffset called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.SetLineDashOffset); double offset = Nan::To(value).FromMaybe(0); if (!std::isfinite(offset)) return; @@ -2996,10 +2870,7 @@ NAN_SETTER(Context2d::SetLineDashOffset) { * ref: http://www.w3.org/TR/2dcontext/#dom-context-2d-setlinedash */ NAN_GETTER(Context2d::GetLineDashOffset) { - if (!Context2d::constructor.Get(info.GetIsolate())->HasInstance(info.This())) { - Nan::ThrowTypeError("Method Context2d.GetLineDashOffset called on incompatible receiver"); - return; - } + CHECK_RECEIVER(Context2d.GetLineDashOffset); Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->context(); double offset; @@ -3486,3 +3357,5 @@ NAN_METHOD(Context2d::Ellipse) { } cairo_set_matrix(ctx, &save_matrix); } + +#undef CHECK_RECEIVER