wasm-bindgen
wasm-bindgen copied to clipboard
Support multithreading in wasm-bindgen-test
Motivation
Current implementation of wasm-bindgen-test does not support multithreading WASM well.
As a common feature of WASM, there is need to put it on the agenda.
Proposed Solution
There are a lot of things we need to do for supporting WASM...
1、Multithreading WASM uses SharedArrayBuffer on the Web, so we need to add these two headers in our response server.
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
More details can be found there. And this can be done with a little bit vector operations in here. 2、The initialization of threadpool would block the current thread. Normally browser would prevent user from blocking the main thread. If we force to initialize threadpool in main worker, we would see error like this:
Uncaught (in promise) RuntimeError: Atomics.wait cannot be called in this context
...emit stack trace
So the only way is to move the initialization part into another worker. That's to say, we cannot run the multithreading WASM in main thread. However, there are no such things like Document object in these workers , while the wasm-bindgen-test is using document to display tests results in its Context. So it's IMPOSSIBLE for current implementation to test multithreading WASM.
Alternatives
Previously I use HTTP interceptor like caddy to fix the missing headers. The core configuration is here:
header +Cross-Origin-Embedder-Policy "require-corp"
header +Cross-Origin-Opener-Policy "same-origin"
But I finally give up for the second reason.