Re: [w3c/ServiceWorker] Size limits with synchronous WebAssembly functions (#1499)

Also, I want to make it really clear what our situation is:

1. wasm-bindgen is a compiler which allows you to compile Rust code into `.wasm` and run that `.wasm` in the browser.

2. In order to accomplish this, wasm-bindgen generates a `.wasm` file and also a `.js` file which contains glue code.

3. The user then simply has to load that `.js` file and wasm-bindgen automatically handles everything else (including loading + running the `.wasm` file).

Right now, this works great... except in service workers. Service workers require you to *synchronously* define the event listeners, but `.wasm` files are always loaded asynchronously (with `instantiateStreaming`).

Therefore, you cannot define the event listeners in your Rust code. Instead, the user of wasm-bindgen must create a separate `.js` file which defines the event listeners and loads + caches the `.wasm` file, as I showed [above](https://github.com/w3c/ServiceWorker/issues/1499#issuecomment-578730536).

Even though wasm-bindgen is already generating `.js` glue code, we cannot put [that code](https://github.com/w3c/ServiceWorker/issues/1499#issuecomment-578730536) into the `.js` glue code, because we don't know what events + caching the user wants. Different users will want different events and caching.

So every user of wasm-bindgen must create their own `.js` file which [does all of this](https://github.com/w3c/ServiceWorker/issues/1499#issuecomment-578730536). And there isn't really anything wasm-bindgen can do to make that easier on our users.

So from wasm-bindgen's perspective, there are three possible solutions to this:

1. The browser can have built-in support for `.wasm` files in service workers (ala [esm-integration](https://github.com/WebAssembly/esm-integration)). This is ideal for us.

2. wasm-bindgen can generate some glue code which runs the Rust code synchronously, thus allowing for the Rust code to define the event listeners.

   Right now that means using the sync `new WebAssembly.Module` / `new WebAssembly.Instance` functions. But that doesn't work if there is a 4 KB limit for those functions.

3. The `load` function I defined [above](https://github.com/w3c/ServiceWorker/issues/1499#issuecomment-578730536) (or similar) would be added to `Cache`.

   This still requires the user to create a separate `.js` file, but at least that `.js` file would be really small (since it would only be defining the event listeners and nothing else).

   Right now it's too tricky to load the `.wasm` file in a way that works offline.

Option 1 is ideal for us, option 2 isn't as good but it's okay, and option 3 is the worst but is acceptable.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1499#issuecomment-579153296

Received on Tuesday, 28 January 2020 09:23:09 UTC