Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

require new in constructor #717

Merged
merged 1 commit into from Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Canvas.cc
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/CanvasGradient.cc
Expand Up @@ -35,6 +35,10 @@ Gradient::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
*/

NAN_METHOD(Gradient::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

// Linear
if (4 == info.Length()) {
Gradient *grad = new Gradient(
Expand Down
4 changes: 4 additions & 0 deletions src/CanvasPattern.cc
Expand Up @@ -37,6 +37,10 @@ Pattern::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
*/

NAN_METHOD(Pattern::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

int w = 0
, h = 0;
cairo_surface_t *surface;
Expand Down
4 changes: 4 additions & 0 deletions src/CanvasRenderingContext2d.cc
Expand Up @@ -536,6 +536,10 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
*/

NAN_METHOD(Context2d::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

Local<Object> obj = info[0]->ToObject();
if (!Nan::New(Canvas::constructor)->HasInstance(obj))
return Nan::ThrowTypeError("Canvas expected");
Expand Down
5 changes: 4 additions & 1 deletion src/FontFace.cc
Expand Up @@ -53,6 +53,10 @@ static cairo_user_data_key_t key;
*/

NAN_METHOD(FontFace::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

if (!info[0]->IsString()
|| !info[1]->IsNumber()) {
return Nan::ThrowError("Wrong argument types passed to FontFace constructor");
Expand Down Expand Up @@ -107,4 +111,3 @@ NAN_METHOD(FontFace::New) {
face->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}

4 changes: 4 additions & 0 deletions src/Image.cc
Expand Up @@ -67,6 +67,10 @@ Image::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
*/

NAN_METHOD(Image::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

Image *img = new Image;
img->data_mode = DATA_IMAGE;
img->Wrap(info.This());
Expand Down
4 changes: 4 additions & 0 deletions src/ImageData.cc
Expand Up @@ -35,6 +35,10 @@ ImageData::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
*/

NAN_METHOD(ImageData::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
}

#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Local<v8::Object> clampedArray;
Local<Object> global = Context::GetCurrent()->Global();
Expand Down
4 changes: 4 additions & 0 deletions test/canvas.test.js
Expand Up @@ -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));
});
Expand Down
4 changes: 4 additions & 0 deletions test/image.test.js
Expand Up @@ -11,6 +11,10 @@ var png_checkers = __dirname + '/fixtures/checkers.png';
var png_clock = __dirname + '/fixtures/clock.png';

describe('Image', function () {
it('should require new', function () {
assert.throws(function () { Image(); }, TypeError);
});

it('Image', function () {
assert.ok(Image instanceof Function);
});
Expand Down
4 changes: 4 additions & 0 deletions test/imageData.test.js
Expand Up @@ -5,6 +5,10 @@ var Canvas = require('../')
, assert = require('assert');

describe('ImageData', function () {
it('should require new', function () {
assert.throws(function () { ImageData(); }, TypeError);
});

it('should throw with invalid numeric arguments', function () {
assert.throws(function () {
new ImageData(0, 0);
Expand Down