Skip to content

Instantly share code, notes, and snippets.

@RReverser
Last active February 21, 2020 12:22
Show Gist options
  • Save RReverser/1c9f9b252ab7b5e85e404dcbef261cd9 to your computer and use it in GitHub Desktop.
Save RReverser/1c9f9b252ab7b5e85e404dcbef261cd9 to your computer and use it in GitHub Desktop.
// Create a Worker we want to share memory with:
let w = new Worker(`data:text/javascript,
onmessage = ({data: memory}) => {
// Got WebAssembly.Memory once, log same instance forever with no further postMessages:
setInterval(() => console.log('Current buffer in worker:', memory.buffer), 5_000);
}
`);
// Create a shared growable memory:
let m = new WebAssembly.Memory({ initial:1, maximum: 65536, shared: true });
// Send memory to the worker:
w.postMessage(m);
// ... within 5 seconds you should see SharedArrayBuffer(65536) in the console, coming from the Worker
// Now, let's grow memory by one page from the main thread:
m.grow(1);
// ... within 5 seconds you should see SharedArrayBuffer(131072) in the console
// This demonstrates that the previously shared memory object auto-updates on growth.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment