let thread_id = null; let buf_offset = null; let exports = null; let flags = null; function done() { postMessage('done'); } async function init_wasm(tid, memory, offset, notify_flags, stack_base) { thread_id = tid; buf_offset = offset; const result = await WebAssembly.instantiateStreaming(fetch('wasm/multi.wasm'), { env: { 'memory': memory } }); exports = result.instance.exports; //console.log(tid, 'init'); exports.set_sp(stack_base - thread_id * 4096); flags = notify_flags; done(); } function do_work(num, callback) { //console.log(thread_id, 'work'); exports.write_a_number(buf_offset, thread_id, thread_id * num); done(); } onmessage = (e) => { if (e.data.type === 0) { init_wasm(e.data.thread_id, e.data.memory, e.data.buffer_offset, e.data.flags, e.data.stack_base); } else if (e.data.type === 1) { do_work(e.data.num); } }