Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion algovivo/mmgrten/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Engine {
}
this.wasmInstance = args.wasmInstance;
const arr = args.wasmInstance.exports.memory.buffer;
const mgr = new mmgr.MemoryManager(arr, args.wasmInstance.exports.__heap_base);
const mgr = new mmgr.MemoryManager(arr, Number(args.wasmInstance.exports.__heap_base));
this.mgr = mgr;

this.functional = this.F = new Functional({
Expand Down
5 changes: 5 additions & 0 deletions algovivo/mmgrten/mmgr/MemoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MemoryManager {
this.array = array;

if (heapBase == null) heapBase = 0;
else heapBase = Number(heapBase);

this.ptrToSlot = new Map();

Expand Down Expand Up @@ -121,6 +122,10 @@ class MemoryManager {

free(ptr) {
const slot = this.ptrToSlot.get(ptr);
if (slot == null) {
throw new Error(`no slot found for ptr ${ptr}`);
}
this.ptrToSlot.delete(ptr);
slot.free();
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/mmgr/memoryManager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const algovivo = require("algovivo");

test("malloc free with non-numeric heapBase", () => {
// simulate WebAssembly.Global which has valueOf() but is an object
const heapBase = { valueOf: () => 16 };
const buffer = new ArrayBuffer(1024);
const mgr = new algovivo.mmgrten.mmgr.MemoryManager(buffer, heapBase);

const ptr = mgr.malloc(32);
const numericPtr = Number(ptr);
mgr.free(numericPtr);
});
Loading