MaulingMonkey / cargo-html

create self-contained HTML programs
Other
10 stars 0 forks source link

doesn't work in firefox #9

Closed joseluis closed 3 years ago

joseluis commented 3 years ago

I've tried the examples, and they work fine in Chrome but not in Firefox 85 on Linux.

This is the error in the inspector console in Firefox.

Uncaught ReferenceError: SharedArrayBuffer is not defined
    SharedCircularBuffer SharedCircularBuffer.ts:64
    main_dom guess.html:350
    <anonymous> guess.html:600
SharedCircularBuffer.ts:64:17
MaulingMonkey commented 3 years ago

Hmm. Reproduced on Firefox 85.0.2 on Windows.

SABs are supposedly supported in this version based on https://caniuse.com/?search=shared , but it looks like the entire type might be gated behind CORS headers even if you make no cross-origin requests per https://hacks.mozilla.org/2020/07/safely-reviving-shared-memory/ . How fustrating.

MaulingMonkey commented 3 years ago

SharedArrayBuffers are currently used to:

  1. Implement sleep requests for the worker thread executing the WASM. This could be replaced with a busy wait as a fallback.
  2. Block on reading stdin. This is significantly more complicated to replace, but there are a few options:
    • Busy loop in the worker thread and use another I/O communication system. While I believe the DOM thread can't send data to the worker thread via postMessage while it's busy looping (onmessage won't be called?), there are probably other higher latency options that can be polled (localStorage?)
    • Ship a pausable WASM interpreter and run it instead. I could possibly compile wasmtime to wasm itself, and then run wasm within wasm.
    • Kill the process when it blocks on stdin. When a new line is entered, rerun the process up to the point it had previously been killed, replaying the results of previous syscalls exactly. Since threads are not yet supported, this should be deterministic and guarantee identical results.
MaulingMonkey commented 3 years ago

I have not yet published a new version to crates.io nor updated the prebuilt examples.

  • Busy loop in the worker thread and use another I/O communication system. While I believe the DOM thread can't send data to the worker thread via postMessage while it's busy looping (onmessage won't be called?), there are probably other higher latency options that can be polled (localStorage?)

Exhausted this avenue - localStorage isn't available, and my attempts to smuggle data syncronously through IndexedDB etc. have failed.

I've discovered a fourth option though: rewrite the WASM to be asyncronous. Some prior art:

MaulingMonkey commented 3 years ago

FireFox 85+ should now work as of a61e95d91e430b5d861e010c076aa0ec91a1653d / 0.1.4 / published examples, although I've only tested the Windows version. SharedCircularBuffer is no longer required as cargo html will use wasm-pack as a crate to invoke asyncify via wasm-opt.

It's all fairly kludged together and still in need of cleanup, but it should at least work now ;)