Google Summer of Code 2021: Implementing a WebAssembly Loader for MetaCall

MetaCall
3 min readAug 20, 2021

--

WebAssembly + MetaCall

WebAssembly is an emerging standard for a binary instruction format designed to be run on a virtual machine. It is designed to be portable, stable and fast.

It is already possible to compile to WebAssembly from many different
programming languages, including ones that are currently unsupported by
MetaCall (e.g. Go, Zig, Kotlin and Swift).

As a part of Google Summer of Code 2021, I set out to change this by
implementing a WebAssembly loader for MetaCall.
In this blog post, I will be showcasing the final product, along with
describing its design and implementation.

Showcase: Calling WebAssembly from Python

Consider the following very simple WebAssembly module written in the text format:

This module exports a single function, add, that takes two 32-bit integers as
parameters and returns a 32-bit integer as its result. Normally, one would
convert the text file above into the binary format, but the WebAssembly loader also supports loading the text format directly. By using the Python port for MetaCall, we can now import WebAssembly functions directly from Python:

This script can then be called using the MetaCall command line tool:

Design and implementation

The WebAssembly loader is implemented using Wasmtime, a small and efficient WebAssembly runtime. This runtime was chosen for two
reasons in particular:

  1. Wasmtime supports the WebAssembly System Interface (WASI), an API for
    interacting with several important features of an operating system, such as the filesystem and networking. It is designed to be independent of the
    browser and is in the process of standardization.
  2. Wasmtime implements the standards-track Wasm C API for embedding. Using this API will make the implementation almost completely runtime-agnostic, making it easy to change the runtime in the future if necessary.

The loader is implemented almost exclusively through the use of the Wasm C API, with any Wasmtime-specific functionality being compiled conditionally. In particular, converting from the WebAssembly text format to the binary format is not supported by the standard API and requires Wasmtime. In the future, other run-times implementing the C API may be supported.

When loading a module, imports are resolved by looking for suitable exports
within previously loaded modules in the same handle, and functions are
discovered by iterating through all functions exported by the module. It is
thus possible to hide functions from MetaCall by not exporting them.

For more detailed information on the development process, refer to the source code in the MetaCall Core repository and the full list of commits related to the project.

Conclusion

The addition of a WebAssembly loader to MetaCall allows it to support a large — and expanding — range of programming languages through the implementation of a single loader. While WebAssembly — and consequently the loader — still have a long way to go, this project provides a good base for future development, such as support for WASI when it is more mature.

Jeppe Blomgren.
Google Summer of Code 2021 Student from MetaCall.

--

--

MetaCall

From DevOps to NoOps automatically, switch now to Serverless. Drag and Drop your code to generate APIs and deploy Cloud Web Services. https://metacall.io