@@ -20,9 +20,20 @@ internal void GC()
20
20
Native . wasmtime_context_gc ( handle ) ;
21
21
}
22
22
23
- internal void AddFuel ( ulong fuel )
23
+ internal ulong GetFuel ( )
24
24
{
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 ) ;
26
37
if ( error != IntPtr . Zero )
27
38
{
28
39
throw WasmtimeException . FromOwnedError ( error ) ;
@@ -49,27 +60,6 @@ internal Store Store
49
60
}
50
61
}
51
62
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
-
73
63
internal void SetWasiConfiguration ( WasiConfiguration config )
74
64
{
75
65
var wasi = config . Build ( ) ;
@@ -97,14 +87,10 @@ private static class Native
97
87
public static extern void wasmtime_context_gc ( IntPtr handle ) ;
98
88
99
89
[ 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 ) ;
101
91
102
92
[ 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 ) ;
108
94
109
95
[ DllImport ( Engine . LibraryName ) ]
110
96
public static extern IntPtr wasmtime_context_set_wasi ( IntPtr handle , IntPtr config ) ;
@@ -158,6 +144,35 @@ public Store(Engine engine, object? data)
158
144
handle = new Handle ( Native . wasmtime_store_new ( engine . NativeHandle , ( IntPtr ) storeHandle , Finalizer ) ) ;
159
145
}
160
146
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
+
161
176
/// <summary>
162
177
/// 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,
163
178
/// this does not retroactively attempt to apply limits to the store.
@@ -212,49 +227,6 @@ public void GC()
212
227
System . GC . KeepAlive ( this ) ;
213
228
}
214
229
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
-
258
230
/// <summary>
259
231
/// Configures WASI within the store.
260
232
/// </summary>
0 commit comments