ports/webassembly: Add support for browser WebWorkers #12118
+18
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds MicroPython WebAssembly support to WebWorkers on browsers. Changes are similar and adhere to existing code style. A small section is added to README.md file about the new support.
WASM port works great so far but it is always tied to main thread on browsers. This blocks us from loading/disposing multiple instances of the module or put the work outside of main thread to not block it. It looks like WASM side has no problems with running on a WebWorker, being loaded to a worker, and disposal when worker is disposed. This opens a door for having clean instances of the module whenever needed in a WebWorker. Getting it up and running on WebWorker's without modifying source code meant emulating global
window
anddocument
objects, addingaddEventListener
andremoveEventListener
functionality to each webWorker file. Having this change merged removes all this hack and boilterplate code and allows a very simple single API for receiving messages from WASM module.As WebWorkers don't have direct access to DOM, using a callback that has been previously set to the Module works. This means WASM module can have a dedicated communication line to push buffers to webWorker. When
webWorkerEventCallback
is not set on Module, webWorker environment is ignored.'myWebWorkerFile.js':
Please let me know if I need to take further action, I'd like to contribute this change to the repository. Thanks