Wasmex.Store (wasmex v0.9.2)

A Store is a collection of Wasm instances and host-defined state.

All Wasm instances and items will be attached to and refer to a Store. For example instances, functions, globals, and tables are all attached to a Store. Instances are created by instantiating a Module within a Store. Many functions of the Wasmex API require a Store in the form of a Wasmex.StoreOrCaller to be passed in.

A Store is intended to be a short-lived object in a program. No form of GC is implemented at this time so once an instance is created within a Store it will not be deallocated until the Store itself is garbage collected. This makes Store unsuitable for creating an unbounded number of instances in it because Store will never release this memory. It’s recommended to have a Store correspond roughly to the lifetime of a “main instance”.

Summary

Functions

Creates a new Wasm store.

Creates a new Wasm store with WASI support.

Functions

Link to this function

new(store_limits \\ nil, engine \\ nil)

@spec new(Wasmex.StoreLimits.t() | nil, Wasmex.Engine.t() | nil) ::
  {:error, reason :: binary()} | {:ok, Wasmex.StoreOrCaller.t()}

Creates a new Wasm store.

Returns a Wasmex.StoreOrCaller even though we know it’s definitely a Store. This allows Elixir-provided imported functions, which only have a "Caller", to use the same Wasmex APIs.

Optionally, a Wasmex.StoreLimits can be passed in to limit the resources that can be created within the store.

Examples

iex> {:ok, %Wasmex.StoreOrCaller{}} = Wasmex.Store.new()

iex> limits = %Wasmex.StoreLimits{memory_size: 1_000_000}
iex> {:ok, %Wasmex.StoreOrCaller{}} = Wasmex.Store.new(limits)
Link to this function

new_wasi(options, store_limits \\ nil, engine \\ nil)

@spec new_wasi(
  Wasmex.Wasi.WasiOptions.t(),
  Wasmex.StoreLimits.t() | nil,
  Wasmex.Engine.t() | nil
) ::
  {:error, reason :: binary()} | {:ok, Wasmex.StoreOrCaller.t()}

Creates a new Wasm store with WASI support.

Returns a Wasmex.StoreOrCaller even though we know it’s definitely a Store. This allows Elixir-provided imported functions, which only have a "Caller", to use the same Wasmex APIs.

See Wasmex.Wasi.WasiOptions for WASI configuration options.

Examples

iex> {:ok, %Wasmex.StoreOrCaller{}} = Wasmex.Store.new_wasi(%Wasmex.Wasi.WasiOptions{})

iex> wasi_opts = %Wasmex.Wasi.WasiOptions{args: ["arg1", "arg2"]}
iex> limits = %Wasmex.StoreLimits{memory_size: 1_000_000}
iex> {:ok, %Wasmex.StoreOrCaller{}} = Wasmex.Store.new_wasi(wasi_opts, limits)