WebAssembly Quick Note

### Overview

- [WASM / WebAssembly](https://webassembly.org/)

<img src="https://kodekloud.com/blog/content/images/size/w1000/2023/03/Screenshot-2023-03-16-at-23.43.00.png" alt="WebAssembly Overview" width="300"/>

  WebAssembly(WASM) is the **binary format**, can be compiled with C/C++/Rust and so on.
  2 different file formats, a binary WebAssembly file (*.wasm) as well as the text format for WebAssembly (*.wat)
  a *.wast file which is the test format for the official WebAssembly spec test suite.
  Ahead-Of-Time (AOT) compile a WebAssembly module to produce a "compiled wasm" (.cwasm) file.

- [WASMTIME](https://docs.wasmtime.dev/introduction.html)

  A standalone runtime for WebAssembly, WASI, and the Component Model by the Bytecode Alliance.
  Wasmtime can be used as a library to embed WebAssembly execution support within applications.
  Using wasmtime API allow user application like Rust/C to instantiate wasm module to be called from the application.

- [WASI](https://wasi.dev/)

  WebAssembly System Interface, defines interfaces that provide a secure and portable way to access several operating-system-like features such as filesystems, networking, clocks, and random numbers.
  Components can use WASI 0.2, but modules can use APIs from WASI 0.1, an earlier stage of WASI's development.
  
  According to the below, if you want to access the standard interfaces to the platform, WASI 0.2 is mandatory.

  | API          | Repository                                    | Version |
  |--------------|-----------------------------------------------|---------|
  | I/O          | [wasi-io](https://github.com/WebAssembly/wasi-io)          | 0.2.0   |
  | Clocks       | [wasi-clocks](https://github.com/WebAssembly/wasi-clocks)  | 0.2.0   |
  | Random       | [wasi-random](https://github.com/WebAssembly/wasi-random)  | 0.2.0   |
  | Filesystem   | [wasi-filesystem](https://github.com/WebAssembly/wasi-filesystem) | 0.2.0   |
  | Sockets      | [wasi-sockets](https://github.com/WebAssembly/wasi-sockets)    | 0.2.0   |
  | CLI          | [wasi-cli](https://github.com/WebAssembly/wasi-cli)          | 0.2.0   |
  | HTTP         | [wasi-http](https://github.com/WebAssembly/wasi-http)        | 0.2.0   |

- [Component Model](https://component-model.bytecodealliance.org/)

  WebAssembly Component Model is a broad-reaching architecture for building interoperable Wasm libraries, applications, and environments.

  `.wasm` core modules are defined by [WebAssembly Core Specification](https://webassembly.github.io/spec/core/), those `modules` can run in the browser, or runtime such as wasmtime or WAMR. But core modules are limited in how they expose their functionality to the outside world to functions that take and return only a small number of core WebAssembly types (essentially only integers and floating-point numbers).
  Against this, in the component model, those type interfaces are described in a language called [WIT Wasm Interface Type](https://component-model.bytecodealliance.org/design/wit.html), so component is wrapping core module to import and export the wit interfaces to interoperate.
  This increases the wasm portability significantly. Not only for architectures and operating system, but also programming languages.
  Combined with Wasm's strong sandboxing, this opens the door to yet further benefits.

  A WIT world is a higher-level contract that describes a component's capabilities and needs.
  For example, WASI (the WebAssembly System Interface) defines a "command line" world which imports interfaces that command line programs typically expect to have available to them such as file I/O, random number generation, clocks and so on. This world has a single export for running the command line tool.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/112819/8baa707f-a923-dd77-4b1a-cef4c9bce334.png)

### Examples

- [wasmtime examples](https://github.com/bytecodealliance/wasmtime/tree/main/examples)
  - Provides most of the major examples with C and Rust, with aligned [rust tutorials](https://docs.wasmtime.dev/lang-rust.html) and [c tutorials](https://docs.wasmtime.dev/lang-c.html).
- [component tutorial](https://component-model.bytecodealliance.org/tutorial.html)

### Runtime

Not to mention [wasmtime](https://wasmtime.dev/) as standard, there are many runtime supported for WASM.

| Runtime   | Features                                                         | Performance                                | Trade-offs                                                                                      |
|-----------|------------------------------------------------------------------|--------------------------------------------|------------------------------------------------------------------------------------------------|
| [WAMR](https://bytecodealliance.github.io/wamr.dev/)      | Designed for resource-constrained environments, supports interpreter and AOT compilation, compatible with WASI | Optimized for low memory and low power devices | Sacrifices some performance for reduced resource consumption                                    |
| [Wasmedge](https://wasmedge.org/)  | Supports JIT compilation, AOT compilation, and interpreter modes, focuses on performance and platform compatibility | Emphasizes speed and efficiency           | May require more memory and processing power compared to lightweight runtimes                   |
| [Wazero](https://wazero.io/)    | Lightweight and minimalistic runtime, optimized for simplicity and efficiency | Prioritizes minimal resource consumption | Limited feature set compared to more comprehensive runtimes                                      |
| [Wasmer](https://wasmer.io/)    | Provides support for JIT compilation, AOT compilation, and interpreter modes, focuses on performance, security, and flexibility | Offers competitive performance with a balance between speed and resource consumption | May require more memory and processing power compared to lightweight runtimes                   |
| [Wasmi](https://github.com/wasmi-labs/wasmi)     | Pure Rust WebAssembly interpreter, designed for simplicity, portability, and ease of integration into Rust projects | Generally slower compared to JIT-based runtimes | Sacrifices some performance for simplicity and ease of use                                      |
| [Wasm3](https://github.com/wasm3/wasm3)     | High-performance WebAssembly interpreter, designed for efficiency and portability across various platforms | Emphasizes speed and efficiency           | May not offer the same level of optimization as JIT-based runtimes                              |

### Tools

- [WABT: The WebAssembly Binary Toolkit](https://github.com/WebAssembly/wabt)

  WABT (we pronounce it "wabbit") is a suite of tools for WebAssembly, debugging and profiling utilities are supported.

  e.g: converting WASM text format into WASM binary format.
  ```shell
  wat2wasm hello.wat -o hello.wasm
  ```

### Documents

- https://docs.wasmtime.dev/
- https://wasi.dev/