Wasmex.Instance (Wasmex v0.4.0)
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)
# Test for existence of a function
true = Wasmex.Instance.function_export_exists(instance, "sum")
All exported functions are accessible via call_exported_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)
This module, especially call_exported_function
is assumed to be called within a GenServer context.
Link to this section Summary
Functions
Calls a function with the given name
and params
on the WebAssembly instance
.
This function assumes to be called within a GenServer context, it expects a from
argument
as given by handle_call
etc.
Link to this section Types
Specs
Link to this section Functions
call_exported_function(instance, name, params, from)
Specs
call_exported_function(t(), binary(), [any()], GenServer.from()) :: any()
Calls a function with the given name
and params
on the WebAssembly instance
.
This function assumes to be called within a GenServer context, it expects a from
argument
as given by handle_call
etc.
The WebAssembly function will be invoked asynchronously in a new OS thread.
The calling process will receive a {:returned_function_call, result, from}
message once
the execution finished.
The result either is an {:error, reason}
or {:ok, results}
tuple with results
containing a list of the results form the called WebAssembly function.
Calling call_exported_function
usually returns an :ok
atom but may throw a BadArg exception when given
unexpected input data.
from_bytes(bytes, imports)
Specs
function_export_exists(instance, name)
Specs
memory(instance, size, offset)
Specs
memory(t(), atom(), pos_integer()) :: {:error, binary()} | {:ok, Wasmex.Memory.t()}