๐ŸŒถ Javex

Run JavaScript inside your Elixir app, sandboxed in WebAssembly.

Hex.pmLicense: MITCI

Javex compiles JavaScript to WebAssembly with Javy and runs it on wasmtime, so you can hand a snippet of user-written JS to a Wasm sandbox and get a typed result back. Each call gets a fresh instance โ€” no shared state, nothing leaks between requests.

โœจ Why you might want this

๐Ÿš€ Quick start

In your mix.exs:

def deps do
  [{:javex, "~> 0.1"}]
end

Add a runtime to your supervision tree:

# lib/my_app/application.ex
children = [
  Javex.Runtime
]

Compile and run:

js = ~S"""
Javy.IO.writeSync(1, new TextEncoder().encode(JSON.stringify({hello: "world"})));
"""

{:ok, mod} = Javex.compile(js)
{:ok, %{"hello" => "world"}} = Javex.run(mod, nil)

๐Ÿ’ก Reading input in your JS requires a small stdin helper โ€” see Javy's README for the canonical readInput() / writeOutput() snippet to drop at the top of your script.

โš™๏ธ The bits you'll reach for

You want toโ€ฆ Use
Compile a JS snippet Javex.compile/2
Run a compiled module with JSON I/O Javex.run(mod, input)
Run with raw bytes Javex.run(mod, bytes, encoding: :raw)
Cap fuel, memory, or timeout Javex.run(mod, input, fuel: โ€ฆ, max_memory: โ€ฆ, timeout: โ€ฆ)
Run multiple tiers (trusted / untrusted) Javex.Runtime.start_link(name: :strict, default_fuel: โ€ฆ, default_max_memory: โ€ฆ)

Persisting compiled modules? %Javex.Module{} is a plain struct โ€” :erlang.term_to_binary/1 round-trips it, no helper needed.

๐Ÿ›ก Safety knobs

{:ok, output} =
  Javex.run(mod, input,
    timeout: 250,                 # wall-clock ms
    fuel: 5_000_000,              # wasmtime fuel units
    max_memory: 8 * 1024 * 1024   # bytes
  )

Errors come back as a tagged tuple, never a process exit:

{:error, %Javex.RuntimeError{kind: :timeout}}        # epoch deadline elapsed
{:error, %Javex.RuntimeError{kind: :fuel_exhausted}}
{:error, %Javex.RuntimeError{kind: :oom}}
{:error, %Javex.RuntimeError{kind: :js_error}}       # uncaught JS exception

๐Ÿง  How it works (in 60 seconds)

๐Ÿ“„ License

MIT.