Hello folks. Today I'm excited to share with you about some work I've been hacking on in Firefox's WebAssembly (AKA Wasm) engine recently.
(see Lin Clark's excellent Cartoon Intro to WebAssembly if you're not familiar with WebAssembly)
by exchanging values through mechanisms such as function calls between the languages,
or shared linear memory.
The initial MVP release of Wasm came with four built-in types:
f64 (32-bit and 64-bit
integers and 32-bit and 64-bit floats, respectively).
Concretely, a simple example program (with a single function that returns an
like this one:
(func (export "f") (result i64)
would produce an error like this when you would try to run it from JS:
then(obj => console.log(obj.instance.exports.f())).catch(console.error);
[error]: TypeError: cannot pass i64 to or from JS
In Firefox 78, you won't get an error. Instead, the JS caller will receive a BigInt
42n and can continue its computation.
One of the practical ramifications of this limitation in previous releases
was that tools that produce Wasm
code would have to legalize it by transforming the code to accept or produce
multiple 32-bit integers instead. For example, a function with a signature
(param i64) would be translated to one with
(param i32 i32). This can
increase compiled code size and complicate the JS logic in your program.
The reason that the limitation existed was that until relatively recently, there was no good way to represent a 64-bit integer value in JS; the Number type in JS doesn't allow you to represent the full range of a 64-bit integer.
That's all changed since BigInt (arbitrary-length integers) became part of the JS standard and started shipping in browsers. In Firefox, it's been available since 2019 after my colleagues at Igalia shipped it (see Andy Wingo's blog post about that).
Support for this proposal has also landed in tools like
Emscripten already as well (you have to pass a
WASM_BIGINT flag to use it).
It was possible to fill in this gap in the JS API thanks to a long line of work on BigInt in JS standards and engines. Some of my colleagues at Igalia such as Dan Ehrenberg and Caio Lima have been pushing forward a lot of the work on BigInt, which we talked about a little bit in a recent blog post.
Much of the BigInt/i64 interoperation work in Firefox was originally done by Sven Sauleau, who championed the spec proposal along with Dan Ehrenberg. Ms2ger from Igalia also worked on the spec text and tests. Many thanks to the engineers at Mozilla (Lars Hansen, Benjamin Bouvier, and André Bargull) who helped review the work and provided bug fixes.
Finally, our work on WebAssembly at Igalia is sponsored by Tech at Bloomberg. Thanks to them for making this work on the web platform possible! They have been a great partner in Igalia's mission to help build an expressive and open web platform for everyone.