correctly load/store uint&int in the runtime.js for wasm64p32

This commit is contained in:
Laytan Laats
2024-05-17 16:32:34 +02:00
parent f9fd8f0c25
commit e08b51ed73
+41 -9
View File
@@ -13,7 +13,8 @@ function stripNewline(str) {
return str.replace(/\n/, ' ')
}
const STRING_SIZE = 2*4;
const INT_SIZE = 4; // NOTE: set to `8` if the target has 64 bit ints (`wasm64p32` for example).
const STRING_SIZE = 2*INT_SIZE;
class WasmMemoryInterface {
constructor() {
@@ -71,17 +72,32 @@ class WasmMemoryInterface {
};
loadF32(addr) { return this.mem.getFloat32(addr, true); }
loadF64(addr) { return this.mem.getFloat64(addr, true); }
loadInt(addr) { return this.mem.getInt32 (addr, true); }
loadUint(addr) { return this.mem.getUint32 (addr, true); }
loadPtr(addr) { return this.loadUint(addr); }
loadInt(addr) {
if (INT_SIZE == 8) {
return this.loadI64(addr);
} else if (INT_SIZE == 4) {
return this.loadI32(addr);
} else {
throw new Error('Unhandled `INT_SIZE`, expected `4` or `8`');
}
};
loadUint(addr) {
if (INT_SIZE == 8) {
return this.loadU64(addr);
} else if (INT_SIZE == 4) {
return this.loadU32(addr);
} else {
throw new Error('Unhandled `INT_SIZE`, expected `4` or `8`');
}
};
loadPtr(addr) { return this.loadU32(addr); }
loadBytes(ptr, len) {
return new Uint8Array(this.memory.buffer, ptr, len);
return new Uint8Array(this.memory.buffer, ptr, Number(len));
}
loadString(ptr, len) {
const bytes = this.loadBytes(ptr, len);
const bytes = this.loadBytes(ptr, Number(len));
return new TextDecoder().decode(bytes);
}
@@ -101,8 +117,24 @@ class WasmMemoryInterface {
}
storeF32(addr, value) { this.mem.setFloat32(addr, value, true); }
storeF64(addr, value) { this.mem.setFloat64(addr, value, true); }
storeInt(addr, value) { this.mem.setInt32 (addr, value, true); }
storeUint(addr, value) { this.mem.setUint32 (addr, value, true); }
storeInt(addr, value) {
if (INT_SIZE == 8) {
this.storeI64(addr, value);
} else if (INT_SIZE == 4) {
this.storeI32(addr, value);
} else {
throw new Error('Unhandled `INT_SIZE`, expected `4` or `8`');
}
}
storeUint(addr, value) {
if (INT_SIZE == 8) {
this.storeU64(addr, value);
} else if (INT_SIZE == 4) {
this.storeU32(addr, value);
} else {
throw new Error('Unhandled `INT_SIZE`, expected `4` or `8`');
}
}
// Returned length might not be the same as `value.length` if non-ascii strings are given.
storeString(addr, value) {