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:
Alex Crichton
2021-02-09 07:16:59 -08:00
parent 5b55ba8053
commit de27fbe20f
5 changed files with 37 additions and 17 deletions

View File

@@ -463,8 +463,11 @@ impl Store {
///
/// This function will panic if the store's [`Config`](crate::Config) did
/// not have fuel consumption enabled.
pub fn add_fuel(&self, fuel: u64) {
assert!(self.engine().config().tunables.consume_fuel);
pub fn add_fuel(&self, fuel: u64) -> Result<()> {
anyhow::ensure!(
self.engine().config().tunables.consume_fuel,
"fuel is not configured in this store"
);
// Fuel is stored as an i64, so we need to cast it. If the provided fuel
// value overflows that just assume that i64::max will suffice. Wasm
@@ -490,6 +493,8 @@ impl Store {
*consumed_ptr = (*consumed_ptr + adj) - i64::max_value();
}
}
Ok(())
}
}