Start here
View SourceThis 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
| use | what it looks like |
|---|---|
| portable plugins | add behaviour to a running system without loading native code into the VM |
| language runtimes | run JavaScript, Python or Lua code that arrives with a request, each request in its own sandbox |
| rule evaluation | give rules explicit inputs, explicit permissions and a deadline |
| shared validators | run the same checks in several services and languages |
| small domain languages | compile a small business-specific language to WebAssembly and run it here |
| runtime research | study 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 have | you use | read |
|---|---|---|
a core .wasm module | wasm, and wasi_preview1 if it imports WASI | Getting started |
a module in the text format, or one built in Erlang, that you want as a .wasm | wasm:encode/1, and wasm:load/1 with {wat, Source} | Turn text into a .wasm file |
a program with a main (a WASI command) | wasi | WASI preview 1 |
| a WASI 0.2 or 0.3 component | wasm_component, wasi_preview2, wasi_preview3 | Components, WASI 0.2, WASI 0.3 |
| a component whose exports and imports you want listed with their types, without running it | wasm_component:describe/1 | Describe a component |
| untrusted code, one instance per request | wasm_instance_worker | Using workers |
| a component that keeps state and handles messages from other actors | wasm_actor | Actors |
| a script (JavaScript, Python, Lua) sent with each request | wasm_script_worker and an adapter | Hosting scripting languages |
| an HTTP handler component | wasi_http_listener | HTTP |
| a model, and a component that should call it | wasi_nn, and a backend for your engine | wasi-nn |
| a change to make in the runtime | CONTRIBUTING.md | Where 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:
- Concepts: the words every other page uses.
- Design principles: the rules that explain the API, such as why errors are values and why you load a module once.
- Getting started: an empty project to a running module.
- Producing a guest module: building a
.wasmthis runtime can run. - The examples, one task per page.
- Hold modules and instances, Host functions and Pass data in and out.
- The guide for your case, from the table above.
You operate it in production, and want it bounded, fast and safe:
- Using workers: untrusted code with a deadline.
- Hold modules and instances: loading, ownership, supervision.
- Configuration: every setting, with its default, and Worker configuration for the worker settings.
- Security: what the sandbox covers, and what is yours.
- Snapshots, The compiled tier and Tuning, once you have measured.
- Upgrading and the Changelog before each upgrade.
You change it:
- CONTRIBUTING.md: the checks and the rules, and Design principles: the rules a change must keep.
- Where things are: every module, and the suites for each area.
- Architecture: how the modules depend on each other, and the path of a call.
- The internals page for your area: Memory, Garbage collection, Worker internals or Component internals.
- Recipes: the files, the test and the page for each kind of change.
- Testing and Measuring.
- 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.
| area | supported modules |
|---|---|
| load, run and limit modules | wasm, wasi, wasi_preview1, wasm_limits, wasm_engine, wasm_error |
| components | wasm_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 |
| HTTP | wasi_http, wasi_http_listener, and the transports wasi_http_h1 and wasi_http_h2 |
| inference | wasi_nn, a release candidate: it follows wasi:nn, which is one upstream, and may change with it |
| workers | wasm_instance_worker, wasm_actor, wasm_script_worker, wasm_worker_error |
| language adapters | wasm_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:
| behaviour | what your module does | page |
|---|---|---|
wasm_worker_adapter | teaches the worker kernel another language; check it with the test kit wasm_adapter_conformance | Writing an adapter |
wasi_http_transport | sends the outbound HTTP requests of a component | HTTP |
wasi_nn, a release candidate | runs the models a component asks for through wasi:nn | wasi-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:
| function | what it is for | page |
|---|---|---|
wasm_jit:await/2, counts/0, diagnostics/0, shards/1, shard_count/2 | waiting for and inspecting the compiled tier | The compiled tier |
wasm_jit:compiled_functions/1, executed/1 | the functions to build compiled code for ahead of time | The compiled tier |
wasm_async:host_read/3, host_sink/2 | reading a stream that a component returns | Components |
wasm_memory:new/1 | a shared memory for threads | Features and conformance |
wasi_fs:backend/0 | which file backend you got | WASI preview 1 |
wasm_snapshot_store:purge/0 | emptying the snapshot directory | Snapshots |
wasm_code_cache:purge/0 | emptying the compiled code directory | The 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.