Wasmex.StoreOrCaller (wasmex v0.8.3)
Either a Wasmex.Store
or "Caller" for imported functions.
A Store is a collection of Wasm instances and host-defined state, see Wasmex.Store
.
A Caller takes the place of a Store in imported function calls. If a Store is needed in
Elixir-provided imported functions, always use the provided Caller because
using the Store will cause a deadlock (the running Wasm instance locks the Stores Mutex).
When configured, a StoreOrCaller can consume fuel to halt or yield execution as desired.
See Wasmex.EngineConfig.consume_fuel/2
for more information on fuel consumption.
Link to this section Summary
Functions
Adds fuel to this Store for Wasm to consume while executing.
Synthetically consumes fuel from this Store.
Returns the amount of fuel consumed by this store's execution so far.
Returns the amount of fuel available for future execution of this store.
Link to this section Types
Link to this section Functions
add_fuel(store_or_caller, fuel)
@spec add_fuel(t(), pos_integer()) :: :ok | {:error, binary()}
Adds fuel to this Store for Wasm to consume while executing.
For this method to work, fuel consumption must be enabled via
Wasmex.EngineConfig.consume_fuel/2. By default a
Wasmex.Storestarts with 0 fuel for Wasm to execute with (meaning it will immediately trap and halt execution). This function must be called for the store to have some fuel to allow WebAssembly to execute. Most Wasm instructions consume 1 unit of fuel. Some instructions, such as
nop,
drop,
block, and
loop, consume 0 units, as any execution cost associated with them involves other instructions which do consume fuel. Note that at this time when fuel is entirely consumed it will cause Wasm to trap. ## Errors This function will return an error if fuel consumption is not enabled via
Wasmex.EngineConfig.consume_fuel/2`.
## Examples
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
:ok
consume_fuel(store_or_caller, fuel)
@spec consume_fuel(t(), pos_integer() | 0) :: {:ok, pos_integer()} | {:error, binary()}
Synthetically consumes fuel from this Store.
For this method to work fuel, consumption must be enabled via
Wasmex.EngineConfig.consume_fuel/2
.
WebAssembly execution will automatically consume fuel but if so desired the embedder can also consume fuel manually to account for relative costs of host functions, for example.
This function will attempt to consume fuel
units of fuel from within
this store. If the remaining amount of fuel allows this then {:ok, N}
is returned where N
is the amount of remaining fuel. Otherwise an
error is returned and no fuel is consumed.
errors
Errors
This function will return an error either if fuel consumption is not
enabled via Wasmex.EngineConfig.consume_fuel/2
or if fuel
exceeds
the amount of remaining fuel within this store.
examples
Examples
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
iex> Wasmex.StoreOrCaller.fuel_remaining(store)
{:ok, 10}
fuel_consumed(store_or_caller)
@spec fuel_consumed(t()) :: {:ok, pos_integer()} | {:error, binary()}
Returns the amount of fuel consumed by this store's execution so far.
Note that fuel, if enabled, must be initially added via
Wasmex.StoreOrCaller.add_fuel/2
.
errors
Errors
If fuel consumption is not enabled via
Wasmex.EngineConfig.consume_fuel/2
then this function will return
an error tuple.
examples
Examples
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
iex> Wasmex.StoreOrCaller.fuel_consumed(store)
{:ok, 0}
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
iex> {:ok, _fuel} = Wasmex.StoreOrCaller.consume_fuel(store, 8)
iex> Wasmex.StoreOrCaller.fuel_consumed(store)
{:ok, 8}
fuel_remaining(store_or_caller)
@spec fuel_remaining(t()) :: {:ok, pos_integer()} | {:error, binary()}
Returns the amount of fuel available for future execution of this store.
examples
Examples
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
iex> Wasmex.StoreOrCaller.fuel_remaining(store)
{:ok, 10}