Elixir (Wasm)
Elixir is a dynamic, functional language for building scalable and maintainable applications, powered by the Erlang VM.
In LiveCodes, Elixir runs in the browser using WebAssembly: the Elixir compiler and standard library execute on AtomVM, a lightweight Erlang VM with a WebAssembly target. No server is involved.
Usage
Demo:
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.elixir object is the result page's interface to that worker:
livecodes.elixir.input: the standard input passed to the program.livecodes.elixir.loaded: a promise that resolves after the initial program run has completed. For a non-empty program, that includes downloading and booting the runtime; an empty initial program resolves before the runtime is downloaded, and initialization starts on the first non-empty run. It rejects if initialization fails. Other helpers should be used after it resolves.livecodes.elixir.output: the program's standard output.livecodes.elixir.value: the inspected return value of the program.livecodes.elixir.warnings: warnings emitted while compiling or running the program.livecodes.elixir.error: the syntax or runtime error, if the program failed.livecodes.elixir.exitCode: the exit code of the program.livecodes.elixir.run: runs the current code with new input, reusing the booted VM, and returns a promise that resolves withoutput,value,warnings,errorandexitCode.livecodes.elixir.retry: restarts the runtime after a failed initialization. A VM crash in a normal run is recovered automatically on the next run.
Example:
IO.puts("Hello, Elixir!")
squares = Enum.map(1..5, &(&1 * &1))
IO.inspect(squares, label: "squares")
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 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
elixir-wasm
Aliases / Extensions
ex, exs, elixir, elixir-wasm, wasm.ex
Editor
script
Compiler
The Elixir toolchain compiled to WebAssembly, running on
AtomVM with the
Popcorn browser runtime. The single-threaded runtime and the
Elixir/Erlang bytecode bundle are built from source in
live-codes/browser-elixir; this playground pins the
npm package @live-codes/[email protected].
Version
Elixir 1.17.3 on OTP 27, with AtomVM 0.7.0-dev (32-bit).
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.
Examples include File.write!/2 (not_implemented), Date.utc_today/0, Task.async/await,
Regex.match?/2, and the JSON module. System.halt/1 terminates the VM instead of returning an
exit status; the runtime reports the crash and the next run boots a fresh worker. Frameworks that
require a server, such as Phoenix and LiveView, 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 Elixir 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=elixir-wasm