require new in constructor
@ -63,6 +63,10 @@ Canvas::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
*/
NAN_METHOD(Canvas::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}
int width = 0, height = 0;
canvas_type_t type = CANVAS_TYPE_IMAGE;
if (info[0]->IsNumber()) width = info[0]->Uint32Value();
@ -35,6 +35,10 @@ Gradient::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
NAN_METHOD(Gradient::New) {
// Linear
if (4 == info.Length()) {
Gradient *grad = new Gradient(
@ -37,6 +37,10 @@ Pattern::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
NAN_METHOD(Pattern::New) {
int w = 0
, h = 0;
cairo_surface_t *surface;
@ -536,6 +536,10 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
NAN_METHOD(Context2d::New) {
Local<Object> obj = info[0]->ToObject();
if (!Nan::New(Canvas::constructor)->HasInstance(obj))
return Nan::ThrowTypeError("Canvas expected");
@ -53,6 +53,10 @@ static cairo_user_data_key_t key;
NAN_METHOD(FontFace::New) {
if (!info[0]->IsString()
|| !info[1]->IsNumber()) {
return Nan::ThrowError("Wrong argument types passed to FontFace constructor");
@ -107,4 +111,3 @@ NAN_METHOD(FontFace::New) {
face->Wrap(info.This());
info.GetReturnValue().Set(info.This());
@ -67,6 +67,10 @@ Image::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
NAN_METHOD(Image::New) {
Image *img = new Image;
img->data_mode = DATA_IMAGE;
img->Wrap(info.This());
@ -35,6 +35,10 @@ ImageData::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
NAN_METHOD(ImageData::New) {
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Local<v8::Object> clampedArray;
Local<Object> global = Context::GetCurrent()->Global();
@ -12,6 +12,10 @@ console.log(' canvas: %s', Canvas.version);
console.log(' cairo: %s', Canvas.cairoVersion);
describe('Canvas', function () {
it('should require new', function () {
assert.throws(function () { Canvas(); }, TypeError);
});
it('.version', function () {
assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.version));
@ -11,6 +11,10 @@ var png_checkers = __dirname + '/fixtures/checkers.png';
var png_clock = __dirname + '/fixtures/clock.png';
describe('Image', function () {
assert.throws(function () { Image(); }, TypeError);
it('Image', function () {
assert.ok(Image instanceof Function);
@ -5,6 +5,10 @@ var Canvas = require('../')
, assert = require('assert');
describe('ImageData', function () {
assert.throws(function () { ImageData(); }, TypeError);
it('should throw with invalid numeric arguments', function () {
assert.throws(function () {
new ImageData(0, 0);