Skip to content

Commit 9fbad84

Browse files
authored
Update to the new fuel APIs introduced in bytecodealliance/wasmtime#7298. (#281)
1 parent 4eccd70 commit 9fbad84

File tree

5 files changed

+111
-178
lines changed

5 files changed

+111
-178
lines changed

examples/consumefuel/Program.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
"expensive",
1313
Function.FromCallback(store, (Caller caller) =>
1414
{
15-
var remaining = caller.ConsumeFuel(1000UL);
15+
checked
16+
{
17+
caller.Fuel -= 1000UL;
18+
}
19+
20+
var remaining = caller.Fuel;
1621
Console.WriteLine($"Called an expensive function which consumed 1000 fuel. {remaining} units of fuel remaining.");
1722
}
1823
));
@@ -26,7 +31,7 @@
2631
return;
2732
}
2833

29-
store.AddFuel(5000UL);
34+
store.Fuel += 5000UL;
3035
Console.WriteLine("Added 5000 units of fuel");
3136

3237
for (var i = 0; i < 4; i++)

src/Caller.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,23 @@ public bool TryGetMemorySpan<T>(string name, long address, int length, out Span<
137137
public Store Store => store;
138138

139139
/// <summary>
140-
/// Adds fuel to this store for WebAssembly code to consume while executing.
140+
/// Gets or sets the fuel available for WebAssembly code to consume while executing.
141141
/// </summary>
142-
/// <param name="fuel">The fuel to add to the store.</param>
143-
public void AddFuel(ulong fuel) => context.AddFuel(fuel);
144-
145-
/// <summary>
146-
/// Synthetically consumes fuel from this store.
147-
///
148-
/// For this method to work fuel consumption must be enabled via <see cref="Config.WithFuelConsumption(bool)"/>.
149-
///
142+
/// <remarks>
143+
/// <para>
144+
/// For this property to work, fuel consumption must be enabled via <see cref="Config.WithFuelConsumption(bool)"/>.
145+
/// </para>
146+
/// <para>
150147
/// WebAssembly execution will automatically consume fuel but if so desired the embedder can also consume fuel manually
151148
/// to account for relative costs of host functions, for example.
152-
///
153-
/// This method will attempt to consume <paramref name="fuel"/> units of fuel from within this store. If the remaining
154-
/// amount of fuel allows this then the amount of remaining fuel is returned. Otherwise, a <see cref="WasmtimeException"/>
155-
/// is thrown and no fuel is consumed.
156-
/// </summary>
157-
/// <param name="fuel">The fuel to consume from the store.</param>
158-
/// <returns>Returns the remaining amount of fuel.</returns>
159-
/// <exception cref="WasmtimeException">Thrown if more fuel is consumed than the store currently has.</exception>
160-
public ulong ConsumeFuel(ulong fuel) => context.ConsumeFuel(fuel);
161-
162-
/// <summary>
163-
/// Gets the fuel consumed by the executing WebAssembly code.
164-
/// </summary>
165-
/// <returns>Returns the fuel consumed by the executing WebAssembly code or 0 if fuel consumption was not enabled.</returns>
166-
public ulong GetConsumedFuel() => context.GetConsumedFuel();
149+
/// </para>
150+
/// </remarks>
151+
/// <value>The fuel available for WebAssembly code to consume while executing.</value>
152+
public ulong Fuel
153+
{
154+
get => context.GetFuel();
155+
set => context.SetFuel(value);
156+
}
167157

168158
/// <summary>
169159
/// Gets the user-defined data from the Store.

src/Store.cs

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,20 @@ internal void GC()
2020
Native.wasmtime_context_gc(handle);
2121
}
2222

23-
internal void AddFuel(ulong fuel)
23+
internal ulong GetFuel()
2424
{
25-
var error = Native.wasmtime_context_add_fuel(handle, fuel);
25+
var error = Native.wasmtime_context_get_fuel(handle, out ulong fuel);
26+
if (error != IntPtr.Zero)
27+
{
28+
throw WasmtimeException.FromOwnedError(error);
29+
}
30+
31+
return fuel;
32+
}
33+
34+
internal void SetFuel(ulong fuel)
35+
{
36+
var error = Native.wasmtime_context_set_fuel(handle, fuel);
2637
if (error != IntPtr.Zero)
2738
{
2839
throw WasmtimeException.FromOwnedError(error);
@@ -49,27 +60,6 @@ internal Store Store
4960
}
5061
}
5162

52-
internal ulong ConsumeFuel(ulong fuel)
53-
{
54-
var error = Native.wasmtime_context_consume_fuel(handle, fuel, out var remaining);
55-
if (error != IntPtr.Zero)
56-
{
57-
throw WasmtimeException.FromOwnedError(error);
58-
}
59-
60-
return remaining;
61-
}
62-
63-
internal ulong GetConsumedFuel()
64-
{
65-
if (!Native.wasmtime_context_fuel_consumed(handle, out var fuel))
66-
{
67-
return 0;
68-
}
69-
70-
return fuel;
71-
}
72-
7363
internal void SetWasiConfiguration(WasiConfiguration config)
7464
{
7565
var wasi = config.Build();
@@ -97,14 +87,10 @@ private static class Native
9787
public static extern void wasmtime_context_gc(IntPtr handle);
9888

9989
[DllImport(Engine.LibraryName)]
100-
public static extern IntPtr wasmtime_context_add_fuel(IntPtr handle, ulong fuel);
90+
public static extern IntPtr wasmtime_context_set_fuel(IntPtr handle, ulong fuel);
10191

10292
[DllImport(Engine.LibraryName)]
103-
public static extern IntPtr wasmtime_context_consume_fuel(IntPtr handle, ulong fuel, out ulong remaining);
104-
105-
[DllImport(Engine.LibraryName)]
106-
[return: MarshalAs(UnmanagedType.I1)]
107-
public static extern bool wasmtime_context_fuel_consumed(IntPtr handle, out ulong fuel);
93+
public static extern IntPtr wasmtime_context_get_fuel(IntPtr handle, out ulong fuel);
10894

10995
[DllImport(Engine.LibraryName)]
11096
public static extern IntPtr wasmtime_context_set_wasi(IntPtr handle, IntPtr config);
@@ -158,6 +144,35 @@ public Store(Engine engine, object? data)
158144
handle = new Handle(Native.wasmtime_store_new(engine.NativeHandle, (IntPtr)storeHandle, Finalizer));
159145
}
160146

147+
/// <summary>
148+
/// Gets or sets the fuel available for WebAssembly code to consume while executing.
149+
/// </summary>
150+
/// <remarks>
151+
/// <para>
152+
/// For this property to work fuel consumption must be enabled via <see cref="Config.WithFuelConsumption(bool)"/>.
153+
/// </para>
154+
/// <para>
155+
/// WebAssembly execution will automatically consume fuel but if so desired the embedder can also consume fuel manually
156+
/// to account for relative costs of host functions, for example.
157+
/// </para>
158+
/// </remarks>
159+
/// <value>The fuel available for WebAssembly code to consume while executing.</value>
160+
public ulong Fuel
161+
{
162+
get
163+
{
164+
ulong fuel = Context.GetFuel();
165+
System.GC.KeepAlive(this);
166+
return fuel;
167+
}
168+
169+
set
170+
{
171+
Context.SetFuel(value);
172+
System.GC.KeepAlive(this);
173+
}
174+
}
175+
161176
/// <summary>
162177
/// Limit the resources that this store may consume. Note that the limits are only used to limit the creation/growth of resources in the future,
163178
/// this does not retroactively attempt to apply limits to the store.
@@ -212,49 +227,6 @@ public void GC()
212227
System.GC.KeepAlive(this);
213228
}
214229

215-
/// <summary>
216-
/// Adds fuel to this store for WebAssembly code to consume while executing.
217-
/// </summary>
218-
/// <param name="fuel">The fuel to add to the store.</param>
219-
public void AddFuel(ulong fuel)
220-
{
221-
Context.AddFuel(fuel);
222-
System.GC.KeepAlive(this);
223-
}
224-
225-
/// <summary>
226-
/// Synthetically consumes fuel from this store.
227-
///
228-
/// For this method to work fuel consumption must be enabled via <see cref="Config.WithFuelConsumption(bool)"/>.
229-
///
230-
/// WebAssembly execution will automatically consume fuel but if so desired the embedder can also consume fuel manually
231-
/// to account for relative costs of host functions, for example.
232-
///
233-
/// This method will attempt to consume <paramref name="fuel"/> units of fuel from within this store. If the remaining
234-
/// amount of fuel allows this then the amount of remaining fuel is returned. Otherwise, a <see cref="WasmtimeException"/>
235-
/// is thrown and no fuel is consumed.
236-
/// </summary>
237-
/// <param name="fuel">The fuel to consume from the store.</param>
238-
/// <returns>Returns the remaining amount of fuel.</returns>
239-
/// <exception cref="WasmtimeException">Thrown if more fuel is consumed than the store currently has.</exception>
240-
public ulong ConsumeFuel(ulong fuel)
241-
{
242-
var result = Context.ConsumeFuel(fuel);
243-
System.GC.KeepAlive(this);
244-
return result;
245-
}
246-
247-
/// <summary>
248-
/// Gets the fuel consumed by the executing WebAssembly code.
249-
/// </summary>
250-
/// <returns>Returns the fuel consumed by the executing WebAssembly code or 0 if fuel consumption was not enabled.</returns>
251-
public ulong GetConsumedFuel()
252-
{
253-
var result = Context.GetConsumedFuel();
254-
System.GC.KeepAlive(this);
255-
return result;
256-
}
257-
258230
/// <summary>
259231
/// Configures WASI within the store.
260232
/// </summary>

tests/CallerTests.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public CallerTests(CallerFixture fixture)
3030
Linker = new Linker(Fixture.Engine);
3131
Store = new Store(Fixture.Engine);
3232

33-
Store.AddFuel(1000000);
33+
Store.Fuel = 1000000;
3434
}
3535

3636
[Fact]
@@ -309,11 +309,12 @@ public void ItCanSetData()
309309

310310

311311
[Fact]
312-
public void ItCanConsumeFuel()
312+
public void ItCanRemoveFuel()
313313
{
314314
Linker.DefineFunction("env", "callback", (Caller c) =>
315315
{
316-
c.ConsumeFuel(10).Should().Be(1000000 - (10 + 2));
316+
c.Fuel -= 10;
317+
c.Fuel.Should().Be(1000000 - (10 + 2));
317318
});
318319

319320
var instance = Linker.Instantiate(Store, Fixture.Module);
@@ -323,16 +324,16 @@ public void ItCanConsumeFuel()
323324

324325
// 10 is consumed by the explicit fuel consumption
325326
// 2 is consumed by the rest of the WASM which executes behind the scenes in this test
326-
Store.GetConsumedFuel().Should().Be(10 + 2);
327+
Store.Fuel.Should().Be(1000000 - (10 + 2));
327328
}
328329

329330
[Fact]
330331
public void ItCanAddFuel()
331332
{
332333
Linker.DefineFunction("env", "callback", (Caller c) =>
333334
{
334-
c.AddFuel(2);
335-
c.ConsumeFuel(0).Should().Be(1000000);
335+
c.Fuel += 2;
336+
c.Fuel.Should().Be(1000000);
336337
});
337338

338339
var instance = Linker.Instantiate(Store, Fixture.Module);
@@ -341,15 +342,15 @@ public void ItCanAddFuel()
341342
callback.Invoke();
342343

343344
// 2 is consumed by the WASM which executes behind the scenes in this test
344-
Store.GetConsumedFuel().Should().Be(2);
345+
Store.Fuel.Should().Be(1000000);
345346
}
346347

347348
[Fact]
348-
public void ItCanGetConsumedFuel()
349+
public void ItCanGetFuel()
349350
{
350351
Linker.DefineFunction("env", "callback", (Caller c) =>
351352
{
352-
c.GetConsumedFuel().Should().Be(2);
353+
c.Fuel.Should().Be(1000000 - 2);
353354
});
354355

355356
var instance = Linker.Instantiate(Store, Fixture.Module);

0 commit comments

Comments
 (0)