Skip to content

Commit 8fe0391

Browse files
ldemaillydeadprogram
authored andcommitted
fix: Avoid total failure on wasm finalizer call
1 parent 2d17dec commit 8fe0391

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

targets/wasm_exec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,22 @@
303303

304304
// func finalizeRef(v ref)
305305
"syscall/js.finalizeRef": (v_ref) => {
306-
// Note: TinyGo does not support finalizers so this should never be
307-
// called.
308-
console.error('syscall/js.finalizeRef not implemented');
306+
// Note: TinyGo does not support finalizers so this is only called
307+
// for one specific case, by js.go:jsString. and can/might leak memory.
308+
const id = mem().getUint32(unboxValue(v_ref), true);
309+
// Note that this if is so far seemingly never true. Someone should investigate why.
310+
if (this._goRefCounts?.[id] !== undefined) {
311+
this._goRefCounts[id]--;
312+
if (this._goRefCounts[id] === 0) {
313+
const v = this._values[id];
314+
this._values[id] = null;
315+
this._ids.delete(v);
316+
this._idPool.push(id);
317+
}
318+
} else {
319+
// Log as a hint and reminder that something is probably off.
320+
console.log("syscall/js.finalizeRef: unknown id", id);
321+
}
309322
},
310323

311324
// func stringVal(value string) ref

0 commit comments

Comments
 (0)