Start here

View Source

This page helps you decide whether erlang_wasm fits what you want to do, and if it does, which module you use and which page to read. Read it first. It ends with the list of modules you may call and rely on; everything else is internal.

erlang_wasm runs WebAssembly inside your Erlang application. It is written in Erlang itself, so you have no native engine to build or ship.

Is it for you

usewhat it looks like
portable pluginsadd behaviour to a running system without loading native code into the VM
language runtimesrun JavaScript, Python or Lua code that arrives with a request, each request in its own sandbox
rule evaluationgive rules explicit inputs, explicit permissions and a deadline
shared validatorsrun the same checks in several services and languages
small domain languagescompile a small business-specific language to WebAssembly and run it here
runtime researchstudy the whole machine, written in a high-level functional language

Choose erlang_wasm when these points matter to your application:

  • how long a request lives and what it may access are part of your product;
  • you want to inspect or change how the runtime behaves;
  • a release that is mostly Erlang is easier for you to operate;
  • the measured speed is already enough for you.

If you need the fastest possible guest execution above all, a native engine such as Wasmtime, embedded through a NIF (native code loaded into the Erlang VM), is a better fit.

What do you have

Find the row for what you have. It names the module you call and the page that shows you how.

you haveyou useread
a core .wasm modulewasm, and wasi_preview1 if it imports WASIGetting started
a module in the text format, or one built in Erlang, that you want as a .wasmwasm:encode/1, and wasm:load/1 with {wat, Source}Turn text into a .wasm file
a program with a main (a WASI command)wasiWASI preview 1
a WASI 0.2 or 0.3 componentwasm_component, wasi_preview2, wasi_preview3Components, WASI 0.2, WASI 0.3
a component whose exports and imports you want listed with their types, without running itwasm_component:describe/1Describe a component
untrusted code, one instance per requestwasm_instance_workerUsing workers
a component that keeps state and handles messages from other actorswasm_actorActors
a script (JavaScript, Python, Lua) sent with each requestwasm_script_worker and an adapterHosting scripting languages
an HTTP handler componentwasi_http_listenerHTTP
a model, and a component that should call itwasi_nn, and a backend for your enginewasi-nn
a change to make in the runtimeCONTRIBUTING.mdWhere things are, Architecture

The library gives you the modules. You bring the guest: the .wasm file, or for a script the language runtime file and the source code. You also bring the values for the limits, and the supervision. You do not need anything in examples/: it holds three demonstrations for you to read and copy, plugin_worker, qjs_worker and nn_upper.

Read in this order

Pick the path for what you do. Stop when your question is answered.

You use it to run WebAssembly from your code:

  1. Concepts: the words every other page uses.
  2. Design principles: the rules that explain the API, such as why errors are values and why you load a module once.
  3. Getting started: an empty project to a running module.
  4. Producing a guest module: building a .wasm this runtime can run.
  5. The examples, one task per page.
  6. Hold modules and instances, Host functions and Pass data in and out.
  7. The guide for your case, from the table above.

You operate it in production, and want it bounded, fast and safe:

  1. Using workers: untrusted code with a deadline.
  2. Hold modules and instances: loading, ownership, supervision.
  3. Configuration: every setting, with its default, and Worker configuration for the worker settings.
  4. Security: what the sandbox covers, and what is yours.
  5. Snapshots, The compiled tier and Tuning, once you have measured.
  6. Upgrading and the Changelog before each upgrade.

You change it:

  1. CONTRIBUTING.md: the checks and the rules, and Design principles: the rules a change must keep.
  2. Where things are: every module, and the suites for each area.
  3. Architecture: how the modules depend on each other, and the path of a call.
  4. The internals page for your area: Memory, Garbage collection, Worker internals or Component internals.
  5. Recipes: the files, the test and the page for each kind of change.
  6. Testing and Measuring.
  7. Design notes: why it is built this way.

Supported API

A supported module is one you may call and rely on: when it changes in a way that breaks your code, the changelog says so. The guides tell you to call these modules and no others.

areasupported modules
load, run and limit moduleswasm, wasi, wasi_preview1, wasm_limits, wasm_engine, wasm_error
componentswasm_component, wasi_preview2, wasi_preview3; wasm_component reads a component without running it with describe/1 and custom_sections/1, and checks arguments with check/2
HTTPwasi_http, wasi_http_listener, and the transports wasi_http_h1 and wasi_http_h2
inferencewasi_nn, a release candidate: it follows wasi:nn, which is one upstream, and may change with it
workerswasm_instance_worker, wasm_actor, wasm_script_worker, wasm_worker_error
language adapterswasm_javascript, wasm_javascript_command, wasm_python, wasm_python_command, wasm_lua

An extension point is a behaviour: a list of callbacks your own module exports so that the runtime can use it. These are supported too:

behaviourwhat your module doespage
wasm_worker_adapterteaches the worker kernel another language; check it with the test kit wasm_adapter_conformanceWriting an adapter
wasi_http_transportsends the outbound HTTP requests of a componentHTTP
wasi_nn, a release candidateruns the models a component asks for through wasi:nnwasi-nn

A few internal modules have one or two functions that a guide documents. Those functions are supported; the rest of the module is not:

functionwhat it is forpage
wasm_jit:await/2, counts/0, diagnostics/0, shards/1, shard_count/2waiting for and inspecting the compiled tierThe compiled tier
wasm_jit:compiled_functions/1, executed/1the functions to build compiled code for ahead of timeThe compiled tier
wasm_async:host_read/3, host_sink/2reading a stream that a component returnsComponents
wasm_memory:new/1a shared memory for threadsFeatures and conformance
wasi_fs:backend/0which file backend you gotWASI preview 1
wasm_snapshot_store:purge/0emptying the snapshot directorySnapshots
wasm_code_cache:purge/0emptying the compiled code directoryThe compiled tier

Everything else is internal. The site lists those modules under groups whose names start with "Internals". They may change in any release, without a note.

Settings you may want in production

Two settings matter on most nodes: scratch_roots, where request files live, and code_cache_dir, which keeps compiled code across restarts. Configuration shows them in a sys.config and lists every other setting.

Read this next

Concepts, then Getting started.