Opened 22 months ago

Last modified 10 months ago

#7058 closed change

Write WebAssembly module loader — at Version 4

Reported by: mjethani Assignee:
Priority: Unknown Milestone:
Module: Core Keywords: closed-in-favor-of-gitlab
Cc: sergz, sebastian, kzar, hfiguiere, greiner Blocked By:
Blocking: Platform: Unknown / Cross platform
Ready: no Confidential: no
Tester: Unknown Verified working: no
Review URL(s):

Description (last modified by mjethani)


We would like to start moving some of our code to WebAssembly if and where possible as we run into our limits of what we can do with pure JavaScript (#7000). This would be somewhat in parallel to the Emscripten effort, but could also be seen as a way to gradually migrate to that implementation over time. In any case, the lessons learnt from the Emscripten effort would come in handy in this piecemeal migration.

The first step for doing this is to have a loader that can load a WebAssembly version of a module on platforms that support it and automatically fall back to a pure JavaScript implementation otherwise, based on a mapping somewhere. This mapping could be in the build script, for example, or it could be hardcoded into the source of the module.

These days it's easy to load a WebAssembly module using a few lines of JavaScript:

  function loadModule(path)
    return fetch(`${path}.wasm`).then(response => response.arrayBuffer())
           .then(bytes => WebAssembly.instantiate(bytes))
           .then(object => object.instance.exports);

  loadModule("simple").then(({add}) =>
    console.log(add(1, 2)); // prints 3

It would be easy to modify the above implementation to fall back to require'ing the pure JavaScript version of the module if the .wasm file is not found or if WebAssembly is not supported on the platform, while keeping it transparent to client code.

It is also trivial these days to write WebAssembly, even by hand:

;; simple.wat

  (func (export "add") (param i32) (param i32) (result i32)
    get_local 0
    get_local 1

This can be compiled to a WebAssembly binary using WABT:

wat2wasm -o simple.wasm simple.wat

There are higher-level languages and frameworks being built around this (e.g. Walt). The web is no longer so tightly coupled with JavaScript.

As a first step, we need a WebAssembly module loader that falls back to alternative pure JavaScript implementations based on certain criteria.


(What versions of Chrome, Firefox, and Edge is WebAssembly supported on? What are the details we need to know before we start work? To be filled out.)

What to change

To be determined.

Change History (4)

comment:1 Changed 22 months ago by mjethani

  • Description modified (diff)

comment:2 Changed 22 months ago by mjethani

  • Description modified (diff)

comment:3 Changed 22 months ago by mjethani

  • Cc sergz sebastian kzar hfiguiere added

comment:4 Changed 22 months ago by mjethani

  • Description modified (diff)
Note: See TracTickets for help on using tickets.