Skip to content

Commit ba48cea

Browse files
committed
update wasmtime from from 14.0.4 to 18.0.2 and follow wasmtimes fuel API changes
1 parent 59d0926 commit ba48cea

10 files changed

+170
-661
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ Types of changes
1414
- `Fixed` for any bug fixes.
1515
- `Security` in case of vulnerabilities.
1616

17-
## unreleased
17+
## [0.9.0 - unreleased]
1818

19-
put your changes here
19+
Wasmtime rewrote their fuel-related API and simplified it. To remain consistent with Wasmtime, we follow this change in this release. A Wasmex `Store` now only implements `set_fuel/2` and `get_fuel/1`. All other methods are removed with this release.
20+
21+
The underlying implementation of the fuel system got rewritten as well. If you are using fuel in your app,
22+
please check your fuel consumption values.
2023

2124
* Thanks to @RoyalIcing for helping us keeping our dependencies up to date for this release 💜
2225

2326
### Added
2427

2528
* official support for Elixir 1.15 and 1.16
29+
* fuel-related API got rewritten, because the underlying Wasm library (wasmtime) changed their API and we want to be consistent. Added `Store.get_fuel/1` and `Store.set_fuel/2` which is a much simpler API than before.
2630

2731
### Removed
2832

2933
* removed support for Elixir 1.12
34+
* with the fuel-related API changed, the existing methods on `Store` (`consume_fuel`, `fuel_remaining`, `add_fuel`) were removed. Please call `set_fuel/2` and `get_fuel/1` instead.
3035

3136
### Changed
3237

lib/wasmex/engine_config.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule Wasmex.EngineConfig do
3636
3737
Note that a `Wasmex.Store` starts with no fuel, so if you enable this option
3838
you'll have to be sure to pour some fuel into `Wasmex.Store` before
39-
executing some code. See `Wasmex.StoreOrCaller.add_fuel/2`.
39+
executing some code. See `Wasmex.StoreOrCaller.set_fuel/2`.
4040
4141
## Example
4242

lib/wasmex/native.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ defmodule Wasmex.Native do
6969
def store_new(_store_limits, _engine_resource), do: error()
7070
def store_new_wasi(_wasi_options, _store_limits, _engine_resource), do: error()
7171

72-
def store_or_caller_add_fuel(_store_or_caller_resource, _fuel), do: error()
73-
def store_or_caller_consume_fuel(_store_or_caller_resource, _fuel), do: error()
74-
def store_or_caller_fuel_consumed(_store_or_caller_resource), do: error()
72+
def store_or_caller_get_fuel(_store_or_caller_resource), do: error()
73+
def store_or_caller_set_fuel(_store_or_caller_resource, _fuel), do: error()
7574

7675
# When the NIF is loaded, it will override functions in this module.
7776
# Calling error is handles the case when the nif could not be loaded.

lib/wasmex/store_or_caller.ex

+11-78
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Wasmex.StoreOrCaller do
3232
end
3333

3434
@doc ~S"""
35-
Adds fuel to this Store for Wasm to consume while executing.
35+
Sets fuel to for Wasm to consume while executing.
3636
3737
For this method to work, fuel consumption must be enabled via
3838
`Wasmex.EngineConfig.consume_fuel/2. By default a `Wasmex.Store`
@@ -58,100 +58,33 @@ defmodule Wasmex.StoreOrCaller do
5858
5959
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
6060
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
61-
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
61+
iex> Wasmex.StoreOrCaller.set_fuel(store, 10)
6262
:ok
6363
"""
64-
@spec add_fuel(__MODULE__.t(), pos_integer()) :: :ok | {:error, binary()}
65-
def add_fuel(%__MODULE__{resource: resource}, fuel) do
66-
case Wasmex.Native.store_or_caller_add_fuel(resource, fuel) do
64+
@spec set_fuel(__MODULE__.t(), pos_integer()) :: :ok | {:error, binary()}
65+
def set_fuel(%__MODULE__{resource: resource}, fuel) do
66+
case Wasmex.Native.store_or_caller_set_fuel(resource, fuel) do
6767
{} -> :ok
6868
{:error, reason} -> {:error, reason}
6969
end
7070
end
7171

72-
@doc ~S"""
73-
Synthetically consumes fuel from this Store.
74-
75-
For this method to work fuel, consumption must be enabled via
76-
`Wasmex.EngineConfig.consume_fuel/2`.
77-
78-
WebAssembly execution will automatically consume fuel but if so desired
79-
the embedder can also consume fuel manually to account for relative
80-
costs of host functions, for example.
81-
82-
This function will attempt to consume `fuel` units of fuel from within
83-
this store. If the remaining amount of fuel allows this then `{:ok, N}`
84-
is returned where `N` is the amount of remaining fuel. Otherwise an
85-
error is returned and no fuel is consumed.
86-
87-
## Errors
88-
89-
This function will return an error either if fuel consumption is not
90-
enabled via `Wasmex.EngineConfig.consume_fuel/2` or if `fuel` exceeds
91-
the amount of remaining fuel within this store.
92-
93-
## Examples
94-
95-
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
96-
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
97-
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
98-
iex> Wasmex.StoreOrCaller.fuel_remaining(store)
99-
{:ok, 10}
100-
"""
101-
@spec consume_fuel(__MODULE__.t(), pos_integer() | 0) ::
102-
{:ok, pos_integer()} | {:error, binary()}
103-
def consume_fuel(%__MODULE__{resource: resource}, fuel) do
104-
case Wasmex.Native.store_or_caller_consume_fuel(resource, fuel) do
105-
{:error, reason} -> {:error, reason}
106-
fuel_remaining -> {:ok, fuel_remaining}
107-
end
108-
end
109-
11072
@doc ~S"""
11173
Returns the amount of fuel available for future execution of this store.
11274
11375
## Examples
11476
11577
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
11678
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
117-
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
118-
iex> Wasmex.StoreOrCaller.fuel_remaining(store)
79+
iex> Wasmex.StoreOrCaller.set_fuel(store, 10)
80+
iex> Wasmex.StoreOrCaller.get_fuel(store)
11981
{:ok, 10}
12082
"""
121-
@spec fuel_remaining(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()}
122-
def fuel_remaining(%__MODULE__{} = store_or_caller) do
123-
consume_fuel(store_or_caller, 0)
124-
end
125-
126-
@doc ~S"""
127-
Returns the amount of fuel consumed by this store's execution so far.
128-
129-
Note that fuel, if enabled, must be initially added via
130-
`Wasmex.StoreOrCaller.add_fuel/2`.
131-
132-
## Errors
133-
134-
If fuel consumption is not enabled via
135-
`Wasmex.EngineConfig.consume_fuel/2` then this function will return
136-
an error tuple.
137-
138-
## Examples
139-
140-
iex> {:ok, engine} = Wasmex.Engine.new(%Wasmex.EngineConfig{consume_fuel: true})
141-
iex> {:ok, store} = Wasmex.Store.new(nil, engine)
142-
iex> Wasmex.StoreOrCaller.fuel_consumed(store)
143-
{:ok, 0}
144-
iex> Wasmex.StoreOrCaller.add_fuel(store, 10)
145-
iex> {:ok, _fuel} = Wasmex.StoreOrCaller.consume_fuel(store, 8)
146-
iex> Wasmex.StoreOrCaller.fuel_consumed(store)
147-
{:ok, 8}
148-
"""
149-
@spec fuel_consumed(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()}
150-
def fuel_consumed(%__MODULE__{resource: resource}) do
151-
case Wasmex.Native.store_or_caller_fuel_consumed(resource) do
83+
@spec get_fuel(__MODULE__.t()) :: {:ok, pos_integer()} | {:error, binary()}
84+
def get_fuel(%__MODULE__{resource: resource}) do
85+
case Wasmex.Native.store_or_caller_get_fuel(resource) do
15286
{:error, reason} -> {:error, reason}
153-
nil -> {:error, "Could not consume fuel: fuel is not configured in this store"}
154-
fuel_consumed -> {:ok, fuel_consumed}
87+
get_fuel -> {:ok, get_fuel}
15588
end
15689
end
15790
end

0 commit comments

Comments
 (0)