### Install wit-bindgen-go Tool Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/go.html Install the `wit-bindgen-go` tool using `go get -tool`. This tool is essential for working with WebAssembly Component Model WIT files in Go. ```bash go get -tool go.bytecodealliance.org/cmd/wit-bindgen-go ``` -------------------------------- ### Install WIT Bindgen and Wasm Tools Source: https://component-model.bytecodealliance.org/print.html Install necessary tools for generating MoonBit bindings from WIT and for WebAssembly component conversion. Rust toolchain is required for these installations. ```bash cargo install wit-bindgen-cli cargo install wasm-tools cargo install wasmtime-cli ``` -------------------------------- ### Run Rust Example Host with Adder Component Source: https://component-model.bytecodealliance.org/print.html Execute the Rust example host to run an adder WebAssembly component. Ensure the WASI adapter version is compatible with your wasmtime version. Arguments are passed after the double dashes. ```bash git clone https://github.com/bytecodealliance/component-docs.git cd component-docs/component-model/examples/example-host cargo run --release -- 1 2 /adder.wasm ``` ```bash cargo run --release -- 1 2 adder.wasm Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host) Finished `release` profile [optimized] target(s) in 7.85s Running `target/debug/example-host 1 2 /path/to/adder.wasm` 1 + 2 = 3 ``` ```bash cargo run --release -- 1 2 adder.wasm Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host) Finished `release` profile [optimized] target(s) in 7.85s Running `target/release/example-host 1 2 /path/to/adder.component.wasm` Error: Failed to instantiate the example world Caused by: 0: component imports instance `wasi:io/error@0.2.2`, but a matching implementation was not found in the linker 1: instance export `error` has the wrong type 2: resource implementation is missing ``` -------------------------------- ### Test component with example-host Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Clones the example host repository and runs the component with provided arguments. ```bash git clone https://github.com/bytecodealliance/component-docs.git cd component-docs/component-model/examples/example-host cp /path/to/adder.component.wasm . cargo run --release -- 5 3 adder.component.wasm ``` -------------------------------- ### Create Go Project Directory and Initialize Module Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/go.html Set up a new directory for your Go project and initialize it with a Go module. This is the starting point for your component. ```bash mkdir add && cd add go mod init example.com ``` -------------------------------- ### Initialize .NET Component Project Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Commands to install the necessary templates and create a new component project. ```bash dotnet new install BytecodeAlliance.Componentize.DotNet.Templates dotnet new componentize.wasi.cli -o adder cd adder ``` -------------------------------- ### Install componentize-py Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/python.html Install the tool required to convert Python applications into WebAssembly components. ```bash pip install componentize-py ``` -------------------------------- ### Install Required Tools Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Install necessary CLI tools using Cargo. ```bash cargo install wit-bindgen-cli cargo install wasm-tools cargo install wasmtime-cli ``` -------------------------------- ### View Build Error Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Example of a build failure due to a missing implementation. ```text ➜ dotnet build Restore complete (8.6s) You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy adder failed with 1 error(s) (25.6s) /path/to/adder/obj/Debug/net10.0/wasi-wasm/wit_bindgen/AdderWorld.wit.exports.docs.adder.v0_1_0.AddInterop.cs(15,19): error CS0103: The name 'AddImpl' does not exist in the current context Build failed with 1 error(s) in 34.6s ``` -------------------------------- ### Install wasm-tools Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/rust.html Install the wasm-tools CLI for WebAssembly binary introspection and manipulation. ```bash cargo install --locked wasm-tools ``` -------------------------------- ### Install Wasmtime CLI Source: https://component-model.bytecodealliance.org/language-support/using-http-in-components/rust.html Install the Wasmtime CLI, which includes a built-in HTTP server for serving WebAssembly HTTP components. ```bash curl https://wasmtime.dev/install.sh -sSf | bash ``` -------------------------------- ### Example WIT Output Source: https://component-model.bytecodealliance.org/language-support/importing-and-reusing-components/javascript.html The expected WIT structure showing unfulfilled imports in an incomplete component. ```WIT package root:component; world root { import example:string-reverse/reverse@0.1.0; export example:string-reverse-upper/reversed-upper@0.1.0; } ``` -------------------------------- ### Run component with Rust host Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/python.html Execute the generated component using the provided Rust example host. ```bash cargo run --release -- 1 2 adder.wasm ``` -------------------------------- ### Verify Tool Installation Source: https://component-model.bytecodealliance.org/print.html Commands to check the installed versions of TinyGo and wasm-tools. ```bash $ tinygo version tinygo version 0.34.0 ... $ wasm-tools -V wasm-tools 1.255.0 ... ``` -------------------------------- ### Install wasmtime Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/python.html Install the wasmtime package to invoke components from Python. ```bash $ pip install wasmtime ``` -------------------------------- ### Install componentize-py Source: https://component-model.bytecodealliance.org/print.html Install the `componentize-py` package using pip. Ensure you have Python 3.10 or later. ```bash pip install componentize-py ``` -------------------------------- ### Usage example for generated code Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Demonstrates how to call the generated add function and handle errors. ```moonbit #![allow(unused)] fn main() { // Generated by `wit-bindgen` 0.45.0. ///| fn init { let _ = @add.add(1, 2).unwrap_or_error() catch { Overflow => ... } } } ``` -------------------------------- ### Verify Tool Installations Source: https://component-model.bytecodealliance.org/print.html Confirm that wit-bindgen, wasm-tools, and wasmtime have been installed correctly by checking their versions. This ensures the tools are ready for use. ```bash $ wit-bindgen --version wit-bindgen-cli 0.45.0 $ wasm-tools --version wasm-tools 1.238.0 $ wasmtime --version wasmtime 36.0.2 ``` -------------------------------- ### Verify MoonBit Installation Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Check the installed versions of the MoonBit toolchain components. ```bash $ moon version --all moon 0.1.20250826 (8ab6c9e 2025-08-26) ~/.moon/bin/moon moonc v0.6.25+d6913262c (2025-08-27) ~/.moon/bin/moonc moonrun 0.1.20250826 (8ab6c9e 2025-08-26) ~/.moon/bin/moonrun moon-pilot 0.0.1-95f12db ~/.moon/bin/moon-pilot ``` -------------------------------- ### View transpiled file output Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/javascript.html Example of the file structure generated after running the transpilation command. ```text Transpiled JS Component Files: - dist/transpiled/adder.core.wasm 10.6 MiB - dist/transpiled/adder.d.ts 0.11 KiB - dist/transpiled/adder.js 21.1 KiB - dist/transpiled/interfaces/docs-adder-add.d.ts 0 ``` -------------------------------- ### Verify TinyGo and wasm-tools Installation Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/go.html Run these commands to confirm that TinyGo and wasm-tools are installed correctly and to check their versions. ```bash tinygo version wasm-tools -V ``` -------------------------------- ### WIT Documentation Comment Examples Source: https://component-model.bytecodealliance.org/print.html Illustrates WIT's documentation comment syntax using both single-line (`///`) and multi-line (`/** ... */`) formats for documenting a function declaration. ```wit /// Prints "hello". print-hello: func(); ``` ```wit /** Prints "hello". */ print-hello: func(); ``` -------------------------------- ### Install jco CLI Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/javascript.html Installs the `jco` command-line tool globally using npm. This tool is used for componentizing JavaScript and running components. ```bash npm install -g @bytecodealliance/jco ``` -------------------------------- ### WebAssembly Text Format (WAT) Module Example Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/wat.html A basic module expressed in WAT, defining an add function and exporting it. This format is useful for testing and experimentation. ```wat (module (func $add (param $lhs i32) (param $rhs i32) (result i32) local.get $lhs local.get $rhs i32.add) (export "docs:adder/add@0.1.0#add" (func $add)) ) ``` -------------------------------- ### Build Transpiled JavaScript with jco Source: https://component-model.bytecodealliance.org/print.html Run this command in the jco example project to build the existing JavaScript code. ```bash npm run transpiled-js ``` -------------------------------- ### Example Output of String Reversal Source: https://component-model.bytecodealliance.org/language-support/importing-and-reusing-components/javascript.html The expected output when running the 'run.js' script, demonstrating the successful execution of the transpiled WebAssembly module. ```text reverseString('!dlrow olleh') = hello world! ``` -------------------------------- ### Example WIT interface definition Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html The expected WIT structure for the adder component. ```wit package root:component; world root { export docs:adder/add@0.1.0; } package docs:adder@0.1.0 { interface add { add: func(x: u32, y: u32) -> u32; } } ``` -------------------------------- ### Example error output Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/python.html Error output when the host fails to satisfy WASI imports. ```text cargo run --release -- 1 2 adder.wasm Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host) Finished `release` profile [optimized] target(s) in 7.85s Running `target/release/example-host 1 2 /path/to/adder.component.wasm` Error: Failed to instantiate the example world Caused by: 0: component imports instance `wasi:io/error@0.2.2`, but a matching implementation was not found in the linker 1: instance export `error` has the wrong type 2: resource implementation is missing ``` -------------------------------- ### Define the Adder WIT World Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/go.html Create a WIT file to define the component's interface and world. This example defines an `adder` world with an `add` function and includes WASI CLI imports. ```wit package docs:adder@0.1.0; interface add { add: func(x: u32, y: u32) -> u32; } world adder { include wasi:cli/imports@0.2.0; export add; } ``` -------------------------------- ### Initialize Project Directory Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Create the project structure and WIT directory. ```bash mkdir moonbit-adder && cd moonbit-adder mkdir wit ``` -------------------------------- ### Test Component with example-host Source: https://component-model.bytecodealliance.org/print.html Use the provided example-host to run a component with specific arguments. ```bash git clone https://github.com/bytecodealliance/component-docs.git cd component-docs/component-model/examples/example-host cp /path/to/adder.component.wasm . cargo run --release -- 5 3 adder.component.wasm ``` ```text 5 + 3 = 8 ``` -------------------------------- ### Verify Tool Installations Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Check versions of the installed CLI tools. ```bash $ wit-bindgen --version wit-bindgen-cli 0.45.0 $ wasm-tools --version wasm-tools 1.238.0 $ wasmtime --version wasmtime 36.0.2 ``` -------------------------------- ### Create a new command component project Source: https://component-model.bytecodealliance.org/language-support/creating-runnable-components/rust.html Initializes a new Rust project configured as a binary application. ```bash cargo new runnable-example ``` -------------------------------- ### WIT Resource Method: open-at() Source: https://component-model.bytecodealliance.org/design/wit-example.html Declares the 'open-at' method, which acts as a constructor for a resource. It takes a path string and returns a result containing either a descriptor or an error code. Use this to open or create a resource at a specific path. ```wit open-at: func( path: string, ) -> result; ``` -------------------------------- ### Create New Rust Project Source: https://component-model.bytecodealliance.org/language-support/using-http-in-components/rust.html Create a new Rust project and navigate into its directory. ```bash cargo new wasm-http-hello-world cd wasm-http-hello-world ``` -------------------------------- ### Install wasm-tools via Cargo Source: https://component-model.bytecodealliance.org/print.html Commands to install the wasm-tools CLI using the Rust toolchain. ```bash cargo install --locked wasm-tools@1.235.0 --force ``` ```bash cargo binstall wasm-tools@1.235.0 ``` -------------------------------- ### Create MoonBit Project Directory Source: https://component-model.bytecodealliance.org/print.html Set up the project structure by creating a new directory for the MoonBit project and a subdirectory for WIT definitions. Navigate into the newly created project directory. ```bash mkdir moonbit-adder && cd moonbit-adder mkdir wit ``` -------------------------------- ### Configure Project WIT Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Add the WIT file reference to the .csproj file. ```xml ``` -------------------------------- ### Implement Host App Logic Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Imports the 'add' interface from the 'hostapp' world and calls the 'Add' function. Ensure the package name in the `using` statement matches the WIT definition. ```csharp // Pull in all imports of the `hostapp` world, namely the `add` interface. // example.component refers to the package name defined in the WIT file. using HostappWorld.wit.imports.docs.adder.v0_1_0; uint left = 1; uint right = 2; var result = AddInterop.Add(left, right); Console.WriteLine($"{left} + {right} = {result}"); ``` -------------------------------- ### open-at constructor Source: https://component-model.bytecodealliance.org/print.html Opens a file path and returns a descriptor resource. ```APIDOC ## open-at ### Description Constructor method that opens a file at the specified path and returns a descriptor. ### Parameters #### Parameters - **path** (string) - Required - The file path to open. ### Response #### Success Response (200) - **result** (result) - Returns a new descriptor on success, or an error-code. ``` -------------------------------- ### Build Component Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Command to compile the component. ```bash dotnet build ``` -------------------------------- ### Create a new library component project Source: https://component-model.bytecodealliance.org/language-support/creating-runnable-components/rust.html Initializes a new Rust project as a library crate. ```bash cargo new --lib runnable-example ``` -------------------------------- ### Run Component via Cargo Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/javascript.html Execute the component using the example-host project. Replace with the actual directory containing the compiled adder.wasm file. ```bash cargo run --release -- 1 2 /adder.wasm ``` -------------------------------- ### HTTP response output Source: https://component-model.bytecodealliance.org/print.html Example output from a successful curl request to the running component. ```http HTTP/1.1 200 OK transfer-encoding: chunked date: Mon, 13 Apr 2026 23:22:20 GMT Hello, world! ``` -------------------------------- ### Define WIT Service World Source: https://component-model.bytecodealliance.org/composing-and-distributing/distributing.html Example WIT world definition that includes a dependency. ```wit package foo:wasi-http-service; world target-world { include wasi:http/proxy@0.2.3; } ``` -------------------------------- ### Run WebAssembly Component with Wasmtime Source: https://component-model.bytecodealliance.org/language-support/using-http-in-components/rust.html Serve the compiled WebAssembly component using the `wasmtime serve` command. This spins up an HTTP server that uses the component to handle requests. A new instance of the component is created for each request. ```bash wasmtime serve -Scli -Shttp target/wasm32-wasip2/release/wasm-http-hello-world.wasm ``` -------------------------------- ### WIT interface with error handling Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Example WIT definition featuring a variant for error handling. ```wit package docs:adder@0.1.0; interface add { variant computation-error { overflow } add: func(x: u32, y: u32) -> result; } world adder { import add; } ``` -------------------------------- ### Create New .NET Component Project Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/csharp.html Scaffolds a new .NET project for a WebAssembly component that implements a WASI CLI. ```bash cd .. dotnet new componentize.wasi.cli -o host-app cd host-app ``` -------------------------------- ### Fetch WIT Package Source: https://component-model.bytecodealliance.org/composing-and-distributing/distributing.html Command to fetch a published WIT package. ```bash wkg get --format wit docs:adder@0.1.0 --output adder.wit ``` -------------------------------- ### Successful Component Execution Output Source: https://component-model.bytecodealliance.org/print.html This is the expected output when the WebAssembly component runs successfully with the `example-host`. ```text __ cargo run --release -- 1 2 adder.wasm Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host) Finished `release` profile [optimized] target(s) in 7.85s Running `target/debug/example-host 1 2 /path/to/adder.wasm` 1 + 2 = 3 ``` -------------------------------- ### Inspect a WebAssembly component file Source: https://component-model.bytecodealliance.org/design/why-component-model.html Uses the file command to verify the binary format of a component. ```bash $ file add.component.wasm add.component.wasm: WebAssembly (wasm) binary module version 0x1000d ``` -------------------------------- ### Create New Rust Library Project Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/rust.html Initialize a new Rust library project named 'adder' using Cargo. ```bash $ cargo new --lib adder Creating library `adder` package note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html ``` -------------------------------- ### Define functions in WIT Source: https://component-model.bytecodealliance.org/design/wit.html Examples of function declarations including those with no return values, return values, and multiple return values. ```wit do-nothing: func(); ``` ```wit // This function does not return a value print: func(message: string); // These functions return values add: func(a: u64, b: u64) -> u64; lookup: func(store: kv-store, key: string) -> option; ``` ```wit get-customers-paged: func(cont: continuation-token) -> tuple, continuation-token>; ``` -------------------------------- ### Build WIT Directory as Wasm Component with `wkg` Source: https://component-model.bytecodealliance.org/print.html Packages the contents of a WIT directory (e.g., `tutorial/wit/adder`) into a Wasm component using the `wkg wit build` command. This is the first step in publishing WIT as a Wasm artifact. ```bash # Package the contents of add WIT directory as Wasm wkg wit build --wit-dir tutorial/wit/adder ``` -------------------------------- ### C code referencing WASI printf Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/c.html Example C code that uses printf, which relies on WASI Preview 1 APIs when compiled with wasi-libc. ```c #include "adder.h" #include uint32_t exports_docs_adder_add_add(uint32_t x, uint32_t y) { uint32_t result = x + y; // On traditional platforms, printf() prints to stdout, but on Wasm platforms, // stdout and the idea of printing to an output stream is // introduced and managed by WASI. // // When building this code with wasi-libc (as a part of wasi-sdk), the printf call // below is implemented with code that uses `wasi:cli/stdout` and `wasi:io/streams`. printf("%d", result); return result; } ``` -------------------------------- ### Compile WASI Preview 1 Module with clang Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/c.html This command compiles a WebAssembly Preview 1 module using `clang`. It assumes `WASI_SDK_PATH` is defined. The output is a core WebAssembly module, not a component. ```bash $WASI_SDK_PATH/bin/clang \ -o adder.wasm \ -mexec-model=reactor \ component.c \ adder.c \ adder_component_type.o ``` -------------------------------- ### Component Interface Definition Source: https://component-model.bytecodealliance.org/language-support/importing-and-reusing-components/javascript.html The output of `npx jco wit` displays the component's world definition, including its exports. This example shows a component that exports a 'reversed-upper' function. ```wit package root:component; world root { export example:string-reverse-upper/reversed-upper@0.1.0; } ``` -------------------------------- ### Configure WASI Namespace Registry in `wkg` Source: https://component-model.bytecodealliance.org/print.html Configures `wkg` to fetch WASI packages from the WebAssembly OCI GitHub Container Registry. This setup is useful for accessing the latest WASI interfaces. ```toml # $XDG_CONFIG_HOME/wasm-pkg/config.toml default_registry = "ghcr.io" [namespace_registries] # Tell wkg that packages with the `wasi` namespace are in an OCI registry # under ghcr.io/webassembly wasi = { registry = "wasi", metadata = { preferredProtocol = "oci", "oci" = {registry = "ghcr.io", namespacePrefix = "webassembly/" } } } ``` -------------------------------- ### Fetch WIT Dependencies Source: https://component-model.bytecodealliance.org/composing-and-distributing/distributing.html Command to fetch all dependencies for a WIT project. ```bash wkg wit fetch ``` -------------------------------- ### Run Transpiled Component in JavaScript Source: https://component-model.bytecodealliance.org/print.html Import and execute a transpiled WASM component in JavaScript. Ensure the build, compose, and transpile steps are completed before running. The example demonstrates calling a `reverseAndUppercase` function. ```javascript /** * If this import listed below is missing, please run * * ``` * npm run build && npm run compose && npm run transpile` * ``` */ import { reversedUpper } from "./dist/transpiled/string-reverse-upper.js"; const result = reversedUpper.reverseAndUppercase("!dlroW olleH"); console.log(`reverseAndUppercase('!dlroW olleH') = ${result}`); ``` -------------------------------- ### Execute the Component Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/rust.html Run the compiled component using a host application and Wasmtime. ```bash $ cd examples/example-host $ cargo run --release -- 1 2 ../add/target/wasm32-wasip2/release/adder.wasm ``` -------------------------------- ### WIT Result Type Examples Source: https://component-model.bytecodealliance.org/print.html Shows the `result` type in WIT for representing operations that can either succeed with a value of type `T` or fail with an error of type `E`. This is similar to `Result` in Rust. ```wit result ``` -------------------------------- ### Implement Hostapp World Logic in C# Source: https://component-model.bytecodealliance.org/print.html Modify the `Program.cs` file to import the necessary interfaces from the `hostapp` world and call the imported functions. Ensure the namespace matches the package name defined in the WIT file. ```csharp // Pull in all imports of the `hostapp` world, namely the `add` interface. // example.component refers to the package name defined in the WIT file. using HostappWorld.wit.imports.docs.adder.v0_1_0; uint left = 1; uint right = 2; var result = AddInterop.Add(left, right); Console.WriteLine($"{left} + {right} = {result}"); ``` -------------------------------- ### Configure MoonBit Package for Wasm Target Source: https://component-model.bytecodealliance.org/print.html Configure the `gen/moon.pkg.json` file to specify the WebAssembly target and export the necessary functions, including the 'add' function and 'cabi_realloc'. This setup is crucial for the Wasm backend. ```json { // link configuration for Wasm backend "link": { "wasm": { "exports": [ // Export for cabi_realloc "cabi_realloc:cabi_realloc", // Export per the interface definition "wasmExportAdd:docs:adder/add@0.1.0#add" ], "export-memory-name": "memory", "heap-start-address": 16 } }, "import": [ { "path": "docs/adder/ffi", "alias": "ffi" }, { "path": "docs/adder/gen/interface/docs/adder/add", "alias": "add" } ] } ``` -------------------------------- ### Initialize a new Rust library project Source: https://component-model.bytecodealliance.org/print.html Use cargo to create a new library crate for the WebAssembly component. ```bash $ cargo new --lib adder ``` -------------------------------- ### Generate Bindings and Download Component (Python) Source: https://component-model.bytecodealliance.org/print.html Generate Python bindings for a WebAssembly component and download a pre-built adder component. Ensure any previous add.wasm file is removed. ```bash # Get an add component that does not import the WASI CLI world $ wget https://github.com/bytecodealliance/component-docs/raw/main/component-model/examples/example-host/add.wasm $ python3 -m wasmtime.bindgen add.wasm --out-dir add ``` -------------------------------- ### Run wasi-sdk inside a Docker container Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/c.html This command runs the `wasi-sdk` container, mounting the local source directory. This is useful to avoid installing the SDK locally. Replace `path/to/app/src` with your actual source directory. ```bash docker run --rm -it \ --mount type=bind,src=path/to/app/src,dst=/app \ ghcr.io/webassembly/wasi-sdk:wasi-sdk-27 ``` -------------------------------- ### Run Adder Component with Host Application Source: https://component-model.bytecodealliance.org/print.html Execute the compiled WebAssembly component using a Rust host application. This involves navigating to the host example directory and running `cargo run` with the component's path. ```Shell $ cd examples/example-host $ cargo run --release -- 1 2 ../add/target/wasm32-wasip2/release/adder.wasm ``` -------------------------------- ### Generate Go Bindings for WIT Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/go.html Use this command to generate Go bindings for your WebAssembly component based on its WIT definitions. The generated code will be placed in the specified output directory. ```bash go tool wit-bindgen-go generate --world adder --out internal ./docs:adder@0.1.0.wasm ``` -------------------------------- ### Run Transpiled Component in JavaScript Source: https://component-model.bytecodealliance.org/language-support/importing-and-reusing-components/javascript.html Import and execute functions from a transpiled WebAssembly component in a JavaScript environment. Ensure the component is built, composed, and transpiled before running. The example imports `reversedUpper` and calls its `reverseAndUppercase` method. ```javascript /** * If this import listed below is missing, please run * * ``` * npm run build && npm run compose && npm run transpile` * ``` */ import { reversedUpper } from "./dist/transpiled/string-reverse-upper.js"; const result = reversedUpper.reverseAndUppercase("!dlroW olleH"); console.log(`reverseAndUppercase('!dlroW olleH') = ${result}`); ``` -------------------------------- ### Build and run HTTP component Source: https://component-model.bytecodealliance.org/print.html Commands to compile the Rust project to WebAssembly and serve it using Wasmtime. ```bash cargo build --release --target wasm32-wasip2 ``` ```bash wasmtime serve -Scli -Shttp target/wasm32-wasip2/release/wasm-http-hello-world.wasm ``` -------------------------------- ### Project structure Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/python.html The expected directory structure after generating bindings. ```text ├── wit │   └── component.wit └── wit_world ├── exports │   ├── add.py │   └── __init__.py ├── __init__.py └── types.py ``` -------------------------------- ### Adapt P1 to P2 component Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/c.html Command to create a P2 component using an adapter module to map WASI Preview 1 APIs to Preview 2. ```bash wasm-tools component new \ adder.wasm \ --adapt wasi_snapshot_preview1.wasm \ -o adder.component.wasm ``` -------------------------------- ### Rust Code: Load, Instantiate, and Call Component Source: https://component-model.bytecodealliance.org/print.html This Rust code snippet demonstrates loading a WebAssembly component from a file, instantiating it with a given store and linker, and then calling an exported function (e.g., `add`). Ensure the `engine`, `path`, `store`, `linker`, `x`, and `y` variables are correctly defined and available in scope. ```rust #![allow(unused)] fn main() { let component = Component::from_file(&engine, path).context("Component file not found")?; let (instance, _) = Example::instantiate_async(&mut store, &component, &linker) .await .context("Failed to instantiate the example world")?; instance .call_add(&mut store, x, y) .await .context("Failed to call add function") } ``` -------------------------------- ### Serve an HTTP component Source: https://component-model.bytecodealliance.org/running-components/wasmtime.html Executes a component implementing the wasi:http/proxy world using the serve subcommand. ```bash wasmtime serve ``` -------------------------------- ### Scaffold Command Component Source: https://component-model.bytecodealliance.org/tutorial.html Initializes a new Rust library project for the command component. ```bash cargo new --lib example cd example ``` -------------------------------- ### Build and Publish WIT Source: https://component-model.bytecodealliance.org/composing-and-distributing/distributing.html Commands to build a WIT directory into a Wasm component and publish it. ```bash # Package the contents of add WIT directory as Wasm wkg wit build --wit-dir tutorial/wit/adder # Publish the produced component wkg publish docs:adder@0.1.0.wasm ``` -------------------------------- ### Generate Bindings with componentize-py Source: https://component-model.bytecodealliance.org/print.html Generate language bindings for a WIT world using `componentize-py`. This step is optional as `componentize` can generate bindings on-the-fly. ```bash componentize-py --wit-path wit --world adder bindings . ``` -------------------------------- ### Verify component interface Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/moonbit.html Displays the WIT interface definition of the generated component. ```bash wasm-tools component wit adder.component.wasm ``` -------------------------------- ### Inspect WebAssembly binary file information Source: https://component-model.bytecodealliance.org/design/why-component-model.html Use the file command to verify the format and version of a compiled .wasm binary. ```bash $ file adder.wasm adder.wasm: WebAssembly (wasm) binary module version 0x1 (MVP) ``` -------------------------------- ### Execute the JavaScript module Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/javascript.html Run the Node.js script that utilizes the transpiled WebAssembly component. ```bash node run.js ``` -------------------------------- ### Run a WebAssembly component with jco Source: https://component-model.bytecodealliance.org/running-components/jco.html Executes a WebAssembly component using the jco runtime, granting it full access to system resources. ```bash jco run ``` -------------------------------- ### Verify WebAssembly component output Source: https://component-model.bytecodealliance.org/print.html Confirms the successful creation of the WebAssembly component file. ```bash OK Successfully written adder.wasm. ``` ```bash $ wasm-tools print adder.wasm | head -1 (component ``` -------------------------------- ### Generate C bindings with wit-bindgen Source: https://component-model.bytecodealliance.org/language-support/building-a-simple-component/c.html Use the wit-bindgen CLI to generate C header and source files from a WIT definition. ```bash $ wit-bindgen c path/to/adder/world.wit Generating "adder.c" Generating "adder.h" Generating "adder_component_type.o" ```