wasm: use shared, imported memory

This switches our wasm build to use "shared" memory. Shared memory can
be shared across multiple web workers, which is something we'll want to
support for our multi-threaded behaviors later.

Shared memory has a number of different restrictions so this updates
zig-js to support it as well as updates some of our functions that need
to be aware of it.
This commit is contained in:
Mitchell Hashimoto
2022-12-24 16:20:59 -08:00
parent 61450fce25
commit 241bfee7d4
5 changed files with 52 additions and 9 deletions

View File

@@ -4,9 +4,11 @@ const zjs = new ZigJS();
const importObject = {
module: {},
env: {
memory: new WebAssembly.Memory({ initial: 25, maximum: 65536, shared: true }),
log: (ptr: number, len: number) => {
const view = new DataView(zjs.memory.buffer, ptr, Number(len));
const str = new TextDecoder('utf-8').decode(view);
const arr = new Uint8ClampedArray(zjs.memory.buffer, ptr, len);
const data = arr.slice();
const str = new TextDecoder('utf-8').decode(data);
console.log(str);
},
},
@@ -20,8 +22,8 @@ fetch(url.href).then(response =>
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results => {
const memory = importObject.env.memory;
const {
memory,
malloc,
free,
face_new,