Skip to content

Commit 03a6510

Browse files
authored
Coerce heapBase to number in MemoryManager to ensure consistent Map key types (#318)
1 parent a876aa4 commit 03a6510

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

algovivo/mmgrten/Engine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Engine {
1616
}
1717
this.wasmInstance = args.wasmInstance;
1818
const arr = args.wasmInstance.exports.memory.buffer;
19-
const mgr = new mmgr.MemoryManager(arr, args.wasmInstance.exports.__heap_base);
19+
const mgr = new mmgr.MemoryManager(arr, Number(args.wasmInstance.exports.__heap_base));
2020
this.mgr = mgr;
2121

2222
this.functional = this.F = new Functional({

algovivo/mmgrten/mmgr/MemoryManager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class MemoryManager {
66
this.array = array;
77

88
if (heapBase == null) heapBase = 0;
9+
else heapBase = Number(heapBase);
910

1011
this.ptrToSlot = new Map();
1112

@@ -121,6 +122,10 @@ class MemoryManager {
121122

122123
free(ptr) {
123124
const slot = this.ptrToSlot.get(ptr);
125+
if (slot == null) {
126+
throw new Error(`no slot found for ptr ${ptr}`);
127+
}
128+
this.ptrToSlot.delete(ptr);
124129
slot.free();
125130
}
126131
}

test/mmgr/memoryManager.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const algovivo = require("algovivo");
2+
3+
test("malloc free with non-numeric heapBase", () => {
4+
// simulate WebAssembly.Global which has valueOf() but is an object
5+
const heapBase = { valueOf: () => 16 };
6+
const buffer = new ArrayBuffer(1024);
7+
const mgr = new algovivo.mmgrten.mmgr.MemoryManager(buffer, heapBase);
8+
9+
const ptr = mgr.malloc(32);
10+
const numericPtr = Number(ptr);
11+
mgr.free(numericPtr);
12+
});

0 commit comments

Comments
 (0)