Wasm Language Support

WebAssembly can be targeted by the majority of top programming languages; however, the level of support varies. This document details the subset of languages that target WASI and support components. This is a living document, so if you are aware of advancements in a toolchain, please do not hesitate to contribute documentation. You can find more information about the development of support for specific languages here. Each section covers how to build and run components for a given toolchain.

One of the benefits of components is their portability across host runtimes. The runtime only needs to know what world the component is targeting in order to import or execute the component. This language guide hopes to demonstrate that with a prevailing example world defined in examples/example-host/add.wit. Furthermore, an example host that understands the example world has been provided in examples/example-host for running components. Each toolchain section walks through creating a component of this world, which can be run either in the example host or from an application of that toolchain. This aims to provide a full story for using components within and among toolchains.

Language Agnostic Tooling

Building a Component with wasm-tools

wasm-tools provides a suite of subcommands for working with WebAssembly modules and components.

wasm-tools can be used to create a component from WebAssembly Text (WAT). This walks through creating a component from WAT that implements the example world and simply adds two numbers.

  1. Install wasm-tools, a tool for low-level manipulation of Wasm modules and components.

  2. The add function is defined inside the following example world:

    package example:component
    world example {
        export add: func(x: s32, y: s32) -> s32
  3. Define an add core module in WAT that exports an add function that adds two parameters:

      (func $add (param $lhs i32) (param $rhs i32) (result i32)
          local.get $lhs
          local.get $rhs
      (export "add" (func $add))
  4. Use wasm-tools to create a component from the core module, first embedding component metadata inside the core module and then encoding the WAT to a Wasm binary.

    $ wasm-tools component embed add.wit add.wat -o add.wasm
    $ wasm-tools component new add.wasm -o add.component.wasm

Running a Component with Wasmtime

You can "run" a component by calling one of its exports. Hosts and runtimes often only support running components with certain exports. The wasmtime CLI can only run "command" components, so in order to run the add function above, it first must be composed with a primary "command" component that calls it. See documentation on running components for more details.