(module
(memory (export "memory") 1)
(data (i32.const 0) "Hello World!")
)
fetch('hello.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes))
.then(result => {
// Access the memory
const memory = result.instance.exports.memory;
const buffer = new Uint8Array(memory.buffer);
// Use the memory
const offset = 0; // Start offset
const length = 13; // Length of the string to read
const message = new TextDecoder().decode(buffer.slice(offset, offset + length));
console.log(message); // This should print "Hello, World!"
// Check if the memory is shared
if (memory.buffer instanceof SharedArrayBuffer) {
console.log("WebAssembly memory is shared.");
} else {
console.log("WebAssembly memory is not shared.");
}
})
.catch(error => console.error(error));
Data type of "memory.buffer" is ArrayBuffer and therefore cannot be used as shared data in a worker. Is there a way to make the exported memory usable as shared?
You have to declare it as shared: