Tweak C API for fuel
* Ensure `store` is in the function names * Don't abort the process on `add_fuel` when fuel isn't configured * Allow learning about failure in both `add_fuel` and `fuel_consumed`
This commit is contained in:
@@ -655,17 +655,23 @@ WASM_API_EXTERN own wasmtime_interrupt_handle_t *wasmtime_interrupt_handle_new(w
|
||||
*
|
||||
* Note that at this time when fuel is entirely consumed it will cause
|
||||
* wasm to trap. More usages of fuel are planned for the future.
|
||||
*
|
||||
* If fuel is not enabled within this store then an error is returned. If fuel
|
||||
* is successfully added then NULL is returned.
|
||||
*/
|
||||
WASM_API_EXTERN void wasmtime_add_fuel(wasm_store_t *store, uint64_t fuel);
|
||||
WASM_API_EXTERN own wasmtime_error_t *wasmtime_store_add_fuel(wasm_store_t *store, uint64_t fuel);
|
||||
|
||||
/**
|
||||
* \brief Returns the amount of fuel consumed by this store's execution so far.
|
||||
*
|
||||
* If fuel consumption is not enabled via #wasmtime_config_consume_fuel_set
|
||||
* then this function will return 0. Also note that fuel, if enabled, must be
|
||||
* originally configured via #wasmtime_add_fuel.
|
||||
* then this function will return false. Otherwise true is returned and the
|
||||
* fuel parameter is filled in with fuel consuemd so far.
|
||||
*
|
||||
* Also note that fuel, if enabled, must be originally configured via
|
||||
* #wasmtime_store_add_fuel.
|
||||
*/
|
||||
WASM_API_EXTERN uint64_t wasmtime_fuel_consumed(wasm_store_t *store);
|
||||
WASM_API_EXTERN bool wasmtime_store_fuel_consumed(wasm_store_t *store, uint64_t *fuel);
|
||||
|
||||
/**
|
||||
* \brief Requests that WebAssembly code running in the store attached to this
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::wasm_engine_t;
|
||||
use crate::{wasm_engine_t, wasmtime_error_t};
|
||||
use wasmtime::{InterruptHandle, Store};
|
||||
|
||||
#[repr(C)]
|
||||
@@ -44,11 +44,20 @@ pub extern "C" fn wasmtime_interrupt_handle_interrupt(handle: &wasmtime_interrup
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_add_fuel(store: &wasm_store_t, fuel: u64) {
|
||||
store.store.add_fuel(fuel);
|
||||
pub extern "C" fn wasmtime_store_add_fuel(
|
||||
store: &wasm_store_t,
|
||||
fuel: u64,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
crate::handle_result(store.store.add_fuel(fuel), |()| {})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_fuel_consumed(store: &wasm_store_t) -> u64 {
|
||||
store.store.fuel_consumed().unwrap_or(0)
|
||||
pub extern "C" fn wasmtime_store_fuel_consumed(store: &wasm_store_t, fuel: &mut u64) -> bool {
|
||||
match store.store.fuel_consumed() {
|
||||
Some(amt) => {
|
||||
*fuel = amt;
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user