emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.92k stars 3.32k forks source link

executing es6 imported Module function for audioworklet in non-cross-origin-isolated environment results in js error #22191

Open patrick99e99 opened 5 months ago

patrick99e99 commented 5 months ago

Please include the following in your bug report:

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.61 (67fa4c16496b157a7fc3377afd69ee0445e8a6e3)

I am building with the following flags:

-sAUDIO_WORKLET=1 -sWASM_WORKERS=1 -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,callMain,addFunction,removeFunction -sEXPORT_ES6=1 -sMODULARIZE=1 -sALLOW_MEMORY_GROWTH=1 -sALLOW_TABLE_GROWTH=1

And when I attempt to call the Module() function, it is blowing up with:

ReferenceError: SharedArrayBuffer is not defined
    at emulator.js:345:38
    at (index):22:7

This comes from the code that is attempting to reference SharedArrayBuffer:

 345   if (!(wasmMemory.buffer instanceof SharedArrayBuffer)) {
 346     err("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has S
 347     if (ENVIRONMENT_IS_NODE) {
 348       err("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)");
 349     }
 350     throw Error("bad memory");
 351   }
 352 }

I don't know if this is considered desired behavior? Am I supposed to not attempt to instantiate that Module function if crossOriginIsolated === false ?

I also was wondering if there is any way to tell emscripten to not use the SharedArrayBuffer for web audio?

I found that google has a sample webaudio + webassembly project that uses an audioworklet without requiring crossOriginIsolated .. But they are not using any of the emscripten_create_wasm_audio_worklet_node or emscripten_create_wasm_audio_worklet_processor_async that I am, rather, they are manually building the audio context and using a .js class for their worklet class... So I was wondering if there's any way to still leverage emscripten to use c++ for setting up audio context + worklets but not be required to have those headers set?

sbc100 commented 4 months ago

I believe that the support we have for audio worklets (-sAUDIO_WORKLET) is fundamentally linked to use of SharedArrayBuffer (since its is based on WASM_WORKERS, which is based on SharedArrayBuffers).

@juj can confirm.