GoogleChromeLabs / wasm-bindgen-rayon

An adapter for enabling Rayon-based concurrency on the Web with WebAssembly.
https://github.com/RReverser/wasm-bindgen-rayon
Apache License 2.0
404 stars 35 forks source link

SyntaxError when attempting to load in Firefox #8

Closed mycognosist closed 3 years ago

mycognosist commented 3 years ago

Thank you very much for your efforts with this library. I am using it for Scuttlebutt message validation in https://github.com/ssb-ngi-pointer/ssb-validate2-rsjs and have been successful when loading and calling the resulting WASM module in Chrome.

The issue I have is with Firefox. The following error is printed to the console in Firefox (88.0.1 (64-bit)) on Manjaro Linux:

SyntaxError: export declarations may only appear at top level of a module

The error may be repeated several times, once for each core on the machine (due to await initThreadPool(navigator.hardwareConcurrency)).

The underlying source of the error appears to be pkg/snippets/wasm-bindgen-rayon-7afa899f36665473/src/workerHelpers.no-bundler.js, line 43:

export async function startWorkers(module, memory, builder) {
  const workerInit = {
    type: 'wasm_bindgen_worker_init',
    module,
    memory,
    receiver: builder.receiver(),
    mainJS: builder.mainJS()
  };
// code continues ...

I wonder if you have encountered this error before and if you have any suggestions? Thanks for your time.

RReverser commented 3 years ago

Right, that's unfortunate side effect of Firefox not supporting module Workers yet. If you use a bundler, that issue is automatically gone, since they bundle code in a way where Worker is no longer ESM and is compatible with all browsers, but if not, including module-workers-polyfill should help.

RReverser commented 3 years ago

This probably needs to be documented in the "Usage without bundlers" section... Although it does say:

Note that, in addition to the earlier mentioned restrictions, this will work only in browsers with support for Module Workers (when using bundlers, those are bundled to regular Workers automatically).

RReverser commented 3 years ago

Added mention of the polyfill to the relevant docs section. I'll close this issue but let me know if it still doesn't work with polyfill, and I'll reopen.

RReverser commented 3 years ago

Also note from a quick look at your code: make sure to check the Caveats section.

Looks like you're currently using Wasm directly on the main thread (by including JS in HTML), but, unfortunately, it works in Chrome only due to a bug that will be fixed soon. To keep it working cross-browser, you'll have to move the main Wasm+JS to a Worker too. (Firefox will most likely throw an error here already once you get past the export issue.)

mycognosist commented 3 years ago

Thanks very much for your detailed feedback. I'll report back if I run into any issues when implementing the suggested changes.