Skip to content

Commit fb60ca1

Browse files
committed
Fix accessing prototypes of Canvas, Image, Context2d
Fixes #803
1 parent f1361eb commit fb60ca1

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

History.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
===================
3+
4+
* Fix accessing prototypes of Canvas, Image, Context2d (#808)
5+
16
1.6.0 / 2016-10-16
27
==================
38

@@ -13,7 +18,7 @@
1318
* Add issue template (#791)
1419

1520
1.4.0 / 2016-06-03
16-
==================
21+
===================
1722

1823
* Add support for evenodd fill rule (#762)
1924

src/Canvas.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Canvas::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
4646

4747
// Prototype
4848
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
49+
proto->SetInternalFieldCount(1);
4950
Nan::SetPrototypeMethod(ctor, "toBuffer", ToBuffer);
5051
Nan::SetPrototypeMethod(ctor, "streamPNGSync", StreamPNGSync);
5152
Nan::SetPrototypeMethod(ctor, "streamPDFSync", StreamPDFSync);

src/CanvasRenderingContext2d.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Context2d::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
8484

8585
// Prototype
8686
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
87+
proto->SetInternalFieldCount(1);
8788
Nan::SetPrototypeMethod(ctor, "drawImage", DrawImage);
8889
Nan::SetPrototypeMethod(ctor, "putImageData", PutImageData);
8990
Nan::SetPrototypeMethod(ctor, "getImageData", GetImageData);

src/Image.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Image::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
4545

4646
// Prototype
4747
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
48+
proto->SetInternalFieldCount(1);
4849
Nan::SetAccessor(proto, Nan::New("source").ToLocalChecked(), GetSource, SetSource);
4950
Nan::SetAccessor(proto, Nan::New("complete").ToLocalChecked(), GetComplete);
5051
Nan::SetAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth);

src/ImageData.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ImageData::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
2525

2626
// Prototype
2727
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
28+
proto->SetInternalFieldCount(1);
2829
Nan::SetAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth);
2930
Nan::SetAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight);
3031
Nan::Set(target, Nan::New("ImageData").ToLocalChecked(), ctor->GetFunction());

test/canvas.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ describe('Canvas', function () {
2525
assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.cairoVersion));
2626
});
2727

28+
it('Allows accessing the prototype on the constructor (GH-803)', function () {
29+
// Fails for 0.8 and 0.10.
30+
if (/v0.(8|10)/.test(process.version)) return;
31+
var c = new Canvas(10, 10);
32+
Canvas.prototype.width;
33+
assert(!c.hasOwnProperty('width'));
34+
assert('width' in c);
35+
assert(Canvas.prototype.hasOwnProperty('width'));
36+
});
37+
2838
it('.parseFont()', function () {
2939
var tests = [
3040
'20px Arial'
@@ -607,6 +617,11 @@ describe('Canvas', function () {
607617
});
608618
});
609619

620+
xit('Context2d allows accessing the prototype on the constructor (GH-803)', function () {
621+
// Seems like a bug in libcairo2 causes a crash if this is evaluated.
622+
Canvas.Context2d.prototype.patternQuality;
623+
});
624+
610625
it('Context2d#createImageData(width, height)', function () {
611626
var canvas = new Canvas(20, 20)
612627
, ctx = canvas.getContext('2d');
@@ -622,6 +637,12 @@ describe('Canvas', function () {
622637
assert.equal(0, imageData.data[3]);
623638
});
624639

640+
it('ImageData allows accessing the prototype on the constructor (GH-803)', function () {
641+
// Fails for 0.8 and 0.10.
642+
if (/v0.(8|10)/.test(process.version)) return;
643+
Canvas.ImageData.prototype.width;
644+
});
645+
625646
it('Context2d#measureText().width', function () {
626647
var canvas = new Canvas(20, 20)
627648
, ctx = canvas.getContext('2d');

test/image.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ describe('Image', function () {
1919
assert.ok(Image instanceof Function);
2020
});
2121

22+
it('Allows accessing the prototype on the constructor (GH-803)', function () {
23+
// Fails for 0.8 and 0.10.
24+
if (/v0.(8|10)/.test(process.version)) return;
25+
var img = new Image();
26+
Image.prototype.width;
27+
assert(!img.hasOwnProperty('width'));
28+
assert('width' in img);
29+
assert(Image.prototype.hasOwnProperty('width'));
30+
});
31+
2232
it('Image#onload', function () {
2333
var img = new Image
2434
, onloadCalled = 0;

0 commit comments

Comments
 (0)