Skip to content

Commit

Permalink
Bugfix/Node.js 18 -> Assertion failed: (object->InternalFieldCount() …
Browse files Browse the repository at this point in the history
…> 0) (#2133)

* add node 16 & 18 to build

* bump version

* fix Assertion failed: (object->InternalFieldCount() > 0), function Unwrap, file nan_object_wrap.h, line 32.

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* reimplement accessors on prototype

* Update CHANGELOG.md

* add review comments

* remove deprecated constructor overload
  • Loading branch information
TheDadi committed Oct 30, 2022
1 parent 0e6504a commit 2876b6e
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14, 16]
node: [10, 12, 14, 16, 18]
steps:
- uses: actions/setup-node@v3
with:
Expand All @@ -33,7 +33,7 @@ jobs:
runs-on: windows-2019
strategy:
matrix:
node: [10, 12, 14, 16]
node: [10, 12, 14, 16, 18]
steps:
- uses: actions/setup-node@v3
with:
Expand All @@ -57,7 +57,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
node: [10, 12, 14, 16]
node: [10, 12, 14, 16, 18]
steps:
- uses: actions/setup-node@v3
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/prebuild.yaml
Expand Up @@ -24,7 +24,7 @@ jobs:
Linux:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, Linux
runs-on: ubuntu-latest
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
macOS:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, macOS
runs-on: macos-latest
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
Win:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, Windows
runs-on: windows-latest
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,14 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
* `textBaseline` and `textAlign` were not saved/restored by `save()`/`restore()`. ([#1936](https://github.com/Automattic/node-canvas/issues/2029))

2.10.2
==================
### Fixed
* Fix `Assertion failed: (object->InternalFieldCount() > 0), function Unwrap, file nan_object_wrap.h, line 32.` ([#2025](https://github.com/Automattic/node-canvas/issues/2025))
### Changed
* Update nan to v2.17.0 to ensure Node.js v18+ support.
* Implement valid `this` checks in all `SetAccessor` methods.

2.10.1
==================
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,7 +51,7 @@
"types": "types/index.d.ts",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.0",
"nan": "^2.15.0",
"nan": "^2.17.0",
"simple-get": "^3.0.3"
},
"devDependencies": {
Expand Down
36 changes: 30 additions & 6 deletions src/Canvas.cc
Expand Up @@ -64,10 +64,10 @@ Canvas::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
#ifdef HAVE_JPEG
Nan::SetPrototypeMethod(ctor, "streamJPEGSync", StreamJPEGSync);
#endif
SetProtoAccessor(proto, Nan::New("type").ToLocalChecked(), GetType, NULL, ctor);
SetProtoAccessor(proto, Nan::New("stride").ToLocalChecked(), GetStride, NULL, ctor);
SetProtoAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth, SetWidth, ctor);
SetProtoAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight, SetHeight, ctor);
Nan::SetAccessor(proto, Nan::New("type").ToLocalChecked(), GetType);
Nan::SetAccessor(proto, Nan::New("stride").ToLocalChecked(), GetStride);
Nan::SetAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth, SetWidth);
Nan::SetAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight, SetHeight);

Nan::SetTemplate(proto, "PNG_NO_FILTERS", Nan::New<Uint32>(PNG_NO_FILTERS));
Nan::SetTemplate(proto, "PNG_FILTER_NONE", Nan::New<Uint32>(PNG_FILTER_NONE));
Expand Down Expand Up @@ -144,6 +144,10 @@ 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;
}
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
info.GetReturnValue().Set(Nan::New<String>(canvas->backend()->getName()).ToLocalChecked());
}
Expand All @@ -152,6 +156,10 @@ 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;
}
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
info.GetReturnValue().Set(Nan::New<Number>(canvas->stride()));
}
Expand All @@ -161,6 +169,10 @@ 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;
}
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
info.GetReturnValue().Set(Nan::New<Number>(canvas->getWidth()));
}
Expand All @@ -170,6 +182,10 @@ 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;
}
if (value->IsNumber()) {
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
canvas->backend()->setWidth(Nan::To<uint32_t>(value).FromMaybe(0));
Expand All @@ -182,6 +198,10 @@ 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;
}
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
info.GetReturnValue().Set(Nan::New<Number>(canvas->getHeight()));
}
Expand All @@ -191,6 +211,10 @@ 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;
}
if (value->IsNumber()) {
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
canvas->backend()->setHeight(Nan::To<uint32_t>(value).FromMaybe(0));
Expand Down Expand Up @@ -773,13 +797,13 @@ NAN_METHOD(Canvas::RegisterFont) {
NAN_METHOD(Canvas::DeregisterAllFonts) {
// Unload all fonts from pango to free up memory
bool success = true;

std::for_each(font_face_list.begin(), font_face_list.end(), [&](FontFace& f) {
if (!deregister_font( (unsigned char *)f.file_path )) success = false;
pango_font_description_free(f.user_desc);
pango_font_description_free(f.sys_desc);
});

font_face_list.clear();
if (!success) Nan::ThrowError("Could not deregister one or more fonts");
}
Expand Down

0 comments on commit 2876b6e

Please sign in to comment.