Skip to content

Commit c806a9c

Browse files
committed
Use FinalizationRegistry for automatic WASM memory cleanup
Replace manual destroy() methods with FinalizationRegistry so objects are automatically cleaned up by the garbage collector.
1 parent 280714f commit c806a9c

12 files changed

Lines changed: 298 additions & 336 deletions

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ require("harfbuzzjs").then(function (hb) {
5858
yCursor += yAdvance;
5959
}
6060

61-
// Release memory
62-
buffer.destroy();
63-
font.destroy();
64-
face.destroy();
65-
blob.destroy();
6661
})
6762
})
6863
```

examples/harfbuzz.example.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,5 @@
2727

2828
document.body.innerText = JSON.stringify({ shape: result, glyphs: glyphs }, undefined, 2);
2929
document.body.style.whiteSpace = 'pre';
30-
31-
buffer.destroy();
32-
font.destroy();
33-
face.destroy();
34-
blob.destroy();
3530
});
3631
</script>

examples/harfbuzz.example.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ function example(hb, fontBlob, text) {
2626

2727
var unicodes = face.collectUnicodes()
2828

29-
buffer.destroy();
30-
font.destroy();
31-
face.destroy();
32-
blob.destroy();
3329
return { shape: result, glyphs: glyphs, unicodes: unicodes };
3430
}
3531

examples/harfbuzz.example.node.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ function example(fontPath, text) {
1717
hb.shape(font, buffer);
1818
var result = buffer.json(font);
1919

20-
buffer.destroy();
21-
font.destroy();
22-
face.destroy();
23-
blob.destroy();
2420
return result;
2521
}
2622

src/blob.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module, exports, freeFuncPtr } from "./helpers";
1+
import { Module, exports, freeFuncPtr, registry } from "./helpers";
22

33
/**
44
* An object representing a {@link https://harfbuzz.github.io/harfbuzz-hb-blob.html | HarfBuzz blob}.
@@ -20,10 +20,7 @@ export class Blob {
2020
blobPtr,
2121
freeFuncPtr,
2222
);
23-
}
24-
25-
/** Free the object. */
26-
destroy() {
27-
exports.hb_blob_destroy(this.ptr);
23+
const ptr = this.ptr;
24+
registry.register(this, () => { exports.hb_blob_destroy(ptr); }, this);
2825
}
2926
}

src/buffer.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Module,
33
exports,
4+
registry,
45
hb_tag,
56
utf8_ptr_to_string,
67
string_to_ascii_ptr,
@@ -67,6 +68,8 @@ export class Buffer {
6768
} else {
6869
this.ptr = exports.hb_buffer_create();
6970
}
71+
const ptr = this.ptr;
72+
registry.register(this, () => { exports.hb_buffer_destroy(ptr); }, this);
7073
}
7174

7275
/**
@@ -213,8 +216,6 @@ export class Buffer {
213216
const buffer = new Buffer(bufferPtr);
214217
const font = new Font(fontPtr);
215218
const result = func(buffer, font, message);
216-
buffer.destroy();
217-
font.destroy();
218219
return result ? 1 : 0;
219220
};
220221
const traceFuncPtr = Module.addFunction(traceFunc, "iiiii");
@@ -292,10 +293,7 @@ export class Buffer {
292293
const positionsPtr32 =
293294
exports.hb_buffer_get_glyph_positions(this.ptr, 0) / 4;
294295
const positionsArray = positionsPtr32
295-
? Module.HEAP32.subarray(
296-
positionsPtr32,
297-
positionsPtr32 + this.getLength() * 5,
298-
)
296+
? Module.HEAP32.subarray(positionsPtr32, positionsPtr32 + this.getLength() * 5)
299297
: null;
300298

301299
const out: (GlyphInfo & Partial<GlyphPosition>)[] = [];
@@ -419,9 +417,4 @@ export class Buffer {
419417
);
420418
return JSON.parse(buf);
421419
}
422-
423-
/** Free the object. */
424-
destroy(): void {
425-
exports.hb_buffer_destroy(this.ptr);
426-
}
427420
}

src/face.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Module,
33
exports,
4+
registry,
45
STATIC_ARRAY_SIZE,
56
hb_tag,
67
hb_untag,
@@ -39,6 +40,8 @@ export class Face {
3940
constructor(blob: Blob, index: number = 0) {
4041
this.ptr = exports.hb_face_create(blob.ptr, index);
4142
this.upem = exports.hb_face_get_upem(this.ptr);
43+
const ptr = this.ptr;
44+
registry.register(this, () => { exports.hb_face_destroy(ptr); }, this);
4245
}
4346

4447
/**
@@ -355,8 +358,4 @@ export class Face {
355358
return names;
356359
}
357360

358-
/** Free the object. */
359-
destroy() {
360-
exports.hb_face_destroy(this.ptr);
361-
}
362361
}

src/font-funcs.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module, exports, utf8_ptr_to_string } from "./helpers";
1+
import { Module, exports, registry, utf8_ptr_to_string } from "./helpers";
22
import type { FontExtents, GlyphExtents } from "./types";
33
import { Font } from "./font";
44

@@ -13,6 +13,8 @@ export class FontFuncs {
1313

1414
constructor() {
1515
this.ptr = exports.hb_font_funcs_create();
16+
const ptr = this.ptr;
17+
registry.register(this, () => { exports.hb_font_funcs_destroy(ptr); }, this);
1618
}
1719

1820
/**
@@ -33,7 +35,6 @@ export class FontFuncs {
3335
) => {
3436
const font = new Font(fontPtr);
3537
const extents = func(font, glyph);
36-
font.destroy();
3738
if (extents) {
3839
Module.HEAP32[extentsPtr / 4] = extents.xBearing;
3940
Module.HEAP32[extentsPtr / 4 + 1] = extents.yBearing;
@@ -68,7 +69,6 @@ export class FontFuncs {
6869
const font = new Font(fontPtr);
6970
const name = utf8_ptr_to_string(namePtr, len);
7071
const glyph = func(font, name);
71-
font.destroy();
7272
if (glyph) {
7373
Module.HEAPU32[glyphPtr / 4] = glyph;
7474
return 1;
@@ -95,7 +95,6 @@ export class FontFuncs {
9595
) => {
9696
const font = new Font(fontPtr);
9797
const advance = func(font, glyph);
98-
font.destroy();
9998
return advance;
10099
},
101100
"ippip",
@@ -118,7 +117,6 @@ export class FontFuncs {
118117
) => {
119118
const font = new Font(fontPtr);
120119
const advance = func(font, glyph);
121-
font.destroy();
122120
return advance;
123121
},
124122
"ippip",
@@ -145,7 +143,6 @@ export class FontFuncs {
145143
) => {
146144
const font = new Font(fontPtr);
147145
const origin = func(font, glyph);
148-
font.destroy();
149146
if (origin) {
150147
Module.HEAP32[xPtr / 4] = origin[0];
151148
Module.HEAP32[yPtr / 4] = origin[1];
@@ -177,7 +174,6 @@ export class FontFuncs {
177174
) => {
178175
const font = new Font(fontPtr);
179176
const origin = func(font, glyph);
180-
font.destroy();
181177
if (origin) {
182178
Module.HEAP32[xPtr / 4] = origin[0];
183179
Module.HEAP32[yPtr / 4] = origin[1];
@@ -208,7 +204,6 @@ export class FontFuncs {
208204
) => {
209205
const font = new Font(fontPtr);
210206
const kerning = func(font, firstGlyph, secondGlyph);
211-
font.destroy();
212207
return kerning;
213208
},
214209
"ippiip",
@@ -234,7 +229,6 @@ export class FontFuncs {
234229
) => {
235230
const font = new Font(fontPtr);
236231
const name = func(font, glyph);
237-
font.destroy();
238232
if (name) {
239233
utf8Encoder.encodeInto(
240234
name,
@@ -267,7 +261,6 @@ export class FontFuncs {
267261
) => {
268262
const font = new Font(fontPtr);
269263
const glyph = func(font, unicode);
270-
font.destroy();
271264
if (glyph) {
272265
Module.HEAPU32[glyphPtr / 4] = glyph;
273266
return 1;
@@ -302,7 +295,6 @@ export class FontFuncs {
302295
) => {
303296
const font = new Font(fontPtr);
304297
const glyph = func(font, unicode, variationSelector);
305-
font.destroy();
306298
if (glyph) {
307299
Module.HEAPU32[glyphPtr / 4] = glyph;
308300
return 1;
@@ -329,7 +321,6 @@ export class FontFuncs {
329321
) => {
330322
const font = new Font(fontPtr);
331323
const extents = func(font);
332-
font.destroy();
333324
if (extents) {
334325
Module.HEAP32[extentsPtr / 4] = extents.ascender;
335326
Module.HEAP32[extentsPtr / 4 + 1] = extents.descender;
@@ -358,7 +349,6 @@ export class FontFuncs {
358349
) => {
359350
const font = new Font(fontPtr);
360351
const extents = func(font);
361-
font.destroy();
362352
if (extents) {
363353
Module.HEAP32[extentsPtr / 4] = extents.ascender;
364354
Module.HEAP32[extentsPtr / 4 + 1] = extents.descender;
@@ -371,9 +361,4 @@ export class FontFuncs {
371361
);
372362
exports.hb_font_funcs_set_font_v_extents_func(this.ptr, funcPtr, 0, 0);
373363
}
374-
375-
/** Free the object. */
376-
destroy(): void {
377-
exports.hb_font_funcs_destroy(this.ptr);
378-
}
379364
}

0 commit comments

Comments
 (0)