Wasmex v0.1.0 Wasmex.Instance

Instantiates a WebAssembly module represented by bytes and allows calling exported functions on it.

# Get the Wasm module as bytes.
{:ok, bytes } = File.read("wasmex_test.wasm")

# Instantiates the Wasm module.
{:ok, instance } = Wasmex.Instance.from_bytes(bytes)

# Call a function on it.
result = Wasmex.Instance.call_exported_function(instance, "sum", [1, 2])

IO.puts result # 3

All exported functions are accessible via the call_exported_function function. Arguments of these functions are automatically casted to WebAssembly values. Note that WebAssembly only knows number datatypes (floats and integers of various sizes).

You can pass arbitrary data to WebAssembly, though, by writing this data into its memory. The memory function returns a Memory struct representing the memory of that particular instance, e.g.:

{:ok, memory} = Wasmex.Instance.memory(instance, :uint8, 0)

Link to this section Summary

Link to this section Types

Link to this type

t()

t() :: %Wasmex.Instance{reference: reference(), resource: binary()}

Link to this section Functions

Link to this function

call_exported_function(instance, name)

call_exported_function(Wasmex.Instance.t(), binary()) :: any()
Link to this function

call_exported_function(instance, name, params)

call_exported_function(Wasmex.Instance.t(), binary(), [any()]) :: any()
Link to this function

from_bytes(bytes)

from_bytes(binary()) :: {:error, binary()} | {:ok, Wasmex.Instance.t()}
Link to this function

function_export_exists(instance, name)

function_export_exists(Wasmex.Instance.t(), binary()) :: boolean()
Link to this function

memory(instance, size, offset)

memory(Wasmex.Instance.t(), atom(), pos_integer()) ::
  {:error, binary()} | {:ok, Wasmex.Memory.t()}