Skip to main content

Erlang (Wasm)

Erlang is a functional, concurrent language for building scalable and fault-tolerant systems.

In LiveCodes, Erlang runs in the browser using WebAssembly on AtomVM, the same lightweight Erlang VM used by Elixir (Wasm). No server is involved.

Usage​

Demo:

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "erlang-wasm",
"content": "io:format(\"Hello, Erlang!~n\")."
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);

Communication with JavaScript​

The program runs in a persistent worker owned by the result page, not in the result page's JavaScript context. The browser global livecodes.erlang object is the result page's interface to that worker:

  • livecodes.erlang.input: the standard input passed to the program.
  • livecodes.erlang.loaded: a promise that resolves after the runtime has downloaded and the first program run has completed. It rejects if the environment fails to load. Other helpers should be used after it resolves.
  • livecodes.erlang.output: the program's standard output.
  • livecodes.erlang.value: the inspected return value of the program.
  • livecodes.erlang.warnings: warnings emitted while compiling or running the program.
  • livecodes.erlang.error: the syntax or runtime error, if the program failed.
  • livecodes.erlang.exitCode: the exit code of the program.
  • livecodes.erlang.run: runs the current code with new input, reusing the booted VM, and returns a promise that resolves with output, value, warnings, error and exitCode.
  • livecodes.erlang.retry: restarts the runtime after a failed initialization. A VM crash in a normal run is recovered automatically on the next run.

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "erlang-wasm",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);

io:format("Hello, Erlang!~n"),
[X * X || X <- lists:seq(1, 5)].

Modules and stdin​

Erlang expressions are comma-separated and must end with a dot. Code that starts with -module( is compiled and loaded into the running VM, so it stays callable in later runs:

-module(m).
-export([double/1]).

double(X) -> X * 2.

Then run m:double(21). in a later run. io:get_line("") reads the standard input, and erlang:halt(0) (or a bare halt(0)) sets the exit code.

The shared stdin device returns binaries. Use binary_to_list/1 when an API needs a charlist, and remove the trailing newline with binary:replace(Value, <<"\n">>, <<>>, [global]) (string:trim/1 is not reliable on AtomVM).

Runtime lifecycle​

The VM runs in a persistent worker owned by the result window. Updating the code rewrites the result document but does not destroy the worker, so normal edits reuse the booted VM instead of downloading and initializing it again.

Each worker boot compiles a small Elixir prelude that provides the shared I/O device for Erlang and Elixir. That takes a few seconds on the first run; subsequent runs only evaluate the user code. Evaluations time out after 60 seconds. If a newer run is waiting and the active evaluation does not finish within 10 seconds, the worker is terminated and recreated so the newer run is not blocked. Pending edits are coalesced to the latest one. A VM crash is reported in error; the next run starts a fresh worker automatically. Use retry() when initialization itself failed.

Language Info​

Name​

erlang-wasm

Aliases / Extensions​

erl, erlang, erlang-wasm, wasm.erl

Editor​

script

Compiler​

The Erlang runtime compiled to WebAssembly, running on AtomVM with the Popcorn browser runtime. The single-threaded runtime and the shared Elixir/Erlang bytecode bundle are built from source in live-codes/browser-elixir; this playground pins the npm package @live-codes/[email protected].

Version​

AtomVM 0.7.0-dev (32-bit), with the Erlang runtime bundled in the shared Elixir/Erlang Wasm bundle.

Limitations​

AtomVM implements a subset of the Erlang VM and its standard library. Features that depend on a full operating system (for example file I/O and OS processes) may be missing or behave differently. Frameworks and applications that require a server are out of scope for the playground.

The runtime is a single-threaded AtomVM build. It does not use WebAssembly threads, SharedArrayBuffer, or Cross-Origin-Opener-Policy / Cross-Origin-Embedder-Policy headers, so it runs in normal (non-isolated) result pages and keeps popup-based OAuth working.

Code Formatting​

Not currently supported.

Live Reload​

By default, new code changes are sent to the result page for re-evaluation without a full page reload or a VM restart. This behavior can be disabled by adding the code comment % __livecodes_reload__ to the Erlang code, which forces a full page reload.

This comment can be added in the hiddenContent property of the editor for embedded playgrounds.

Starter Template​

https://livecodes.io/?template=erlang-wasm