emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.73k stars 3.3k forks source link

In docs, mention CORS with respect to Audio Worklets #20454

Open ad8e opened 1 year ago

ad8e commented 1 year ago

https://github.com/emscripten-core/emscripten/issues/20349#issuecomment-1762155539 encountered problems with SharedArrayBuffer. Neither of the tutorials mentions CORS, so this was an inevitable error: https://emscripten.org/docs/api_reference/wasm_audio_worklets.html, https://github.com/emscripten-core/emscripten/blob/main/test/webaudio/audioworklet.c.

Proposed solution: Add docs. At https://emscripten.org/docs/api_reference/wasm_audio_worklets.html, right after "Compile the code with the linker flags -sAUDIO_WORKLET=1 -sWASM_WORKERS=1 to enable targeting AudioWorklets.", this text is added:

"Web browsers prohibit sharing data between threads unless CORS headers are set. If you try to open the html file you just compiled, you will receive the error SharedArrayBuffer is not defined. To fix this, host the html file with a web server that sets CORS headers. For example, you can run python3 \[this python script\] to serve all files in the same directory, then navigate to http://127.0.0.1:8000/your_file_here.html. See MDN for more information."

The python script is this one. The extension should be .py; github doesn't let me upload .py files.

kripken commented 1 year ago

This might also make sense in an FAQ entry, as it is not specific to wasm workers - it also happens with pthreads. An FAQ entry about what to do with the SharedArrayBuffer is not defined error might be best.

Also there might be good docs for that elsewhere on the web, that show up when you search that question, which we could link to from our docs.

sbc100 commented 11 months ago

Better still we could show a message in debug builds when SharedArrayBuffer is not available. We already do this in certain place(s). e.g.: https://github.com/emscripten-core/emscripten/blob/00eae746d233099faccc299dd3abc86ae834f837/src/runtime_init_memory.js#L51-L59

We should probably add a generic/central test up front and report consistently on all builds that depend on shared memory