Skip to content

Commit

Permalink
feat: add preventAutoDelete method to ClassHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed Jul 20, 2024
1 parent 5e3e0f7 commit 1eaa6ab
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build/preamble_vips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ declare module Vips {
* @return `true` if this handle is deleted.
*/
isDeleted(): boolean;

/**
* Prevents the C++ object from being auto deleted
* @return `this`
*/
preventAutoDelete(): T;
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"src/closure-externs/wasm-vips.js",
"src/fixed-threadpool-web.js",
"src/modules-pre.js",
"src/modules-post.js",
"src/workaround-cors-pre.js"
],
"env": [
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(MAIN_LINK_OPTIONS
$<$<BOOL:${ENABLE_WASMFS}>:-sWASMFS>
$<$<BOOL:${ENABLE_MODULES}>:-sMAIN_MODULE=2>
$<$<BOOL:${ENABLE_MODULES}>:--pre-js=${CMAKE_CURRENT_SOURCE_DIR}/modules-pre.js>
--post-js=${CMAKE_CURRENT_SOURCE_DIR}/modules-post.js
-sAUTOLOAD_DYLIBS=0
-sABORTING_MALLOC=0
-sMALLOC=mimalloc
Expand Down
10 changes: 10 additions & 0 deletions src/modules-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Object.assign(ClassHandle.prototype, {
'preventAutoDelete' () {
const index = deletionQueue.indexOf(this);
if (index > -1) {
deletionQueue.splice(index, 1);
}
this.$$.deleteScheduled = false;
return this;
}
});
72 changes: 72 additions & 0 deletions test/unit/test_auto_delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* global vips, expect, cleanup */
'use strict';

describe('auto delete', () => {
after(function () {
cleanup();
expect(vips.deletionQueue.length).to.equal(0);
});

it('auto delete', function () {
const im = vips.Image.black(100, 100);
expect(vips.deletionQueue.length).to.equal(1);
expect(() => im.clone()).to.throw(/Object already scheduled for deletion/);
im.gaussblur(0.3); // creates new handle
expect(vips.deletionQueue.length).to.equal(2);
cleanup();
expect(vips.deletionQueue.length).to.equal(0);
});

it('preventAutoDelete', function () {
const im = new vips.Image();
const source = new vips.SourceCustom();
expect(vips.deletionQueue.length).to.equal(2);
im.preventAutoDelete();
expect(vips.deletionQueue.length).to.equal(1);
source.preventAutoDelete();
expect(vips.deletionQueue.length).to.equal(0);

// cloned objects do not retain prevent auto delete status
const cloned = im.clone();
expect(vips.deletionQueue.length).to.equal(1);
cleanup();
expect(vips.deletionQueue.length).to.equal(0);

im.delete(); // should not fail
source.delete();
expect(() => im.delete()).to.throw(/Image instance already deleted/);
expect(() => source.delete()).to.throw(/SourceCustom instance already deleted/);
// already deleted by cleanup, should fail
expect(() => cloned.delete()).to.throw(/Image instance already deleted/);
});

it('preventAutoDelete on all handles', function () {
const handles = Object.entries(vips).filter(
([key, Handle]) =>
key !== 'Object' && !!Handle?.prototype?.preventAutoDelete
);
expect(handles.length).to.equal(12);

for (const [name] of handles) {
const h = new vips[name]();
expect(vips.deletionQueue.length).to.equal(1);
h.preventAutoDelete();
expect(vips.deletionQueue.length).to.equal(0);
h.delete();
expect(() => h.delete()).to.throw(new RegExp(`${name} instance already deleted`));
}
});

it('preventAutoDelete on cloned handle', function () {
const im = new vips.Image();
expect(vips.deletionQueue.length).to.equal(1);
im.preventAutoDelete();
expect(vips.deletionQueue.length).to.equal(0);
const cloned = im.clone();
expect(vips.deletionQueue.length).to.equal(1);
cloned.preventAutoDelete();
expect(vips.deletionQueue.length).to.equal(0);
cloned.delete();
im.delete();
});
});

0 comments on commit 1eaa6ab

Please sign in to comment.