Skip to content

Latest commit

 

History

History
384 lines (258 loc) · 10 KB

README.md

File metadata and controls

384 lines (258 loc) · 10 KB

Wasmer Examples

This directory contains a collection of examples. This isn't an exhaustive collection though, if one example is missing, please ask, we will be happy to fulfill your needs!

Handy Diagrams

As a quick introduction to Wasmer's main workflows, here are three diagrams. We hope it provides an overview of how the crates assemble together.

  1. Module compilation, illustrates how WebAssembly bytes are validated, parsed, and compiled, with the help of the wasmer::Module, wasmer_engine::Engine, and wasmer_compiler::Compiler API.

    Module compilation

  2. Module serialization, illustrates how a module can be serialized and deserialized, with the help of wasmer::Module::serialize and wasmer::Module::deserialize. The important part is that the engine can changed between those two steps, and thus how a headless engine can be used for the deserialization.

    Module serialization

  3. Module instantiation, illustrates what happens when wasmer::Instance::new is called.

    Module instantiation

Examples

The examples are written in a difficulty/discovery order. Concepts that are explained in an example is not necessarily re-explained in a next example.

Basics

  1. [Hello World][hello-world], explains the core concepts of the Wasmer API for compiling and executing WebAssembly.

    Keywords: introduction, instance, module.

    Execute the example
    $ cargo run --example hello-world --release --features "cranelift"
  2. Instantiating a module, explains the basics of using Wasmer and how to create an instance out of a Wasm module.

    Keywords: instance, module.

    Execute the example
    $ cargo run --example instance --release --features "cranelift"
  3. [Handling errors][errors], explains the basics of interacting with Wasm module memory.

    Keywords: memory, module.

    Execute the example
    $ cargo run --example memory --release --features "cranelift"
  4. Interacting with memory, explains the basics of interacting with Wasm module memory.

    Keywords: memory, module.

    Execute the example
    $ cargo run --example memory --release --features "cranelift"

Exports

  1. Exported global, explains how to work with exported globals: get/set their value, have information about their type.

    Keywords: export, global.

    Execute the example
    $ cargo run --example exported-globals --release --features "cranelift"
  2. Exported function, explains how to get and how to call an exported function. They come in 2 flavors: dynamic, and “static”/native. The pros and cons are discussed briefly.

    Keywords: export, function, dynamic, static, native.

    Execute the example
    $ cargo run --example exported-function --release --features "cranelift"
  3. Exported memory, explains how to read from and write to exported memory.

    Keywords: export, memory.

    Execute the example
    $ cargo run --example exported-memory --release --features "cranelift"

Imports

  1. Imported global, explains how to work with imported globals: create globals, import them, get/set their value.

    Keywords: import, global.

    Execute the example
    $ cargo run --example imported-globals --release --features "cranelift"
  2. Imported function, explains how to define an imported function. They come in 2 flavors: dynamic, and “static”/native.

    Keywords: import, function, dynamic, static, native.

    Execute the example
    $ cargo run --example imported-function --release --features "cranelift"

Externs

  1. Table, explains how to use Wasm Tables from the Wasmer API.

    Keywords: basic, table, call_indirect

    Execute the example
    $ cargo run --example table --release --features "cranelift"
  2. Memory, explains how to use Wasm Memories from the Wasmer API. Memory example is a work in progress.

    Keywords: basic, memory

    Execute the example
    $ cargo run --example memory --release --features "cranelift"

Tunables

  1. Limit memory, explains how to use Tunables to limit the size of an exported Wasm memory

    Keywords: basic, tunables, memory

    Execute the example
    $ cargo run --example tunables-limit-memory --release --features "cranelift"

Engines

  1. JIT engine, explains what an engine is, what the JIT engine is, and how to set it up. The example completes itself with the compilation of the Wasm module, its instantiation, and finally, by calling an exported function.

    Keywords: JIT, engine, in-memory, executable code.

    Execute the example
    $ cargo run --example engine-jit --release --features "cranelift"
  2. Native engine, explains what a native engine is, and how to set it up. The example completes itself with the compilation of the Wasm module, its instantiation, and finally, by calling an exported function.

    Keywords: native, engine, shared library, dynamic library, executable code.

    Execute the example
    $ cargo run --example engine-native --release --features "cranelift"
  3. Headless engines, explains what a headless engine is, what problem it does solve, and what are the benefits of it. The example completes itself with the instantiation of a pre-compiled Wasm module, and finally, by calling an exported function.

    Keywords: native, engine, constrained environment, ahead-of-time compilation, cross-compilation, executable code, serialization.

    Execute the example
    $ cargo run --example engine-headless --release --features "cranelift"
  4. Cross-compilation, illustrates the power of the abstraction over the engines and the compilers, such as it is possible to cross-compile a Wasm module for a custom target.

    Keywords: engine, compiler, cross-compilation.

    Execute the example
    $ cargo run --example cross-compilation --release --features "cranelift"

Compilers

  1. Singlepass compiler, explains how to use the wasmer-compiler-singlepass compiler.

    Keywords: compiler, singlepass.

    Execute the example
    $ cargo run --example compiler-singlepass --release --features "singlepass"
  2. Cranelift compiler, explains how to use the wasmer-compiler-cranelift compiler.

    Keywords: compiler, cranelift.

    Execute the example
    $ cargo run --example compiler-cranelift --release --features "cranelift"
  3. LLVM compiler, explains how to use the wasmer-compiler-llvm compiler.

    Keywords: compiler, llvm.

    Execute the example
    $ cargo run --example compiler-llvm --release --features "llvm"

Integrations

  1. WASI, explains how to use the WebAssembly System Interface (WASI), i.e. the wasmer-wasi crate.

    Keywords: wasi, system, interface

    Execute the example
    $ cargo run --example wasi --release --features "cranelift,wasi"
  2. WASI Pipes, builds on the WASI example to show off stdio piping in Wasmer.

    Keywords: wasi, system, interface

    Execute the example
    $ cargo run --example wasi-pipes --release --features "cranelift,wasi"