Opened 8 months ago

Last modified 6 weeks ago

#7058 new change

Write WebAssembly module loader — at Version 2

Reported by: mjethani Assignee:
Priority: Unknown Milestone:
Module: Core Keywords:
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)

Background

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:

<script>
  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(0, 2)); // prints 3
  });
</script>

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

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

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.

Compatibility

(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 (2)

comment:1 Changed 8 months ago by mjethani

  • Description modified (diff)

comment:2 Changed 8 months ago by mjethani

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