Skip to content

Commit

Permalink
Access and reset fuel amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Jun 1, 2023
1 parent 96a7cae commit 195f2b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
18 changes: 17 additions & 1 deletion crates/wasmi/src/func/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Caller<'a, T> {

impl<'a, T> Caller<'a, T> {
/// Creates a new [`Caller`] from the given store context and [`Instance`] handle.
pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
pub fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
where
C: AsContextMut<UserState = T>,
{
Expand Down Expand Up @@ -69,6 +69,13 @@ impl<'a, T> Caller<'a, T> {
self.ctx.store.fuel_consumed()
}

/// Returns the amount of total [`Fuel`] supplied to the [`Store`](crate::Store).
///
/// Returns `None` if fuel metering is disabled.
pub fn fuel_total(&self) -> Option<u64> {
self.ctx.store.fuel_total()
}

/// Synthetically consumes an amount of fuel for the [`Store`](crate::Store).
///
/// Returns the remaining amount of fuel after this operation.
Expand All @@ -84,6 +91,15 @@ impl<'a, T> Caller<'a, T> {
pub fn consume_fuel(&mut self, delta: u64) -> Result<u64, FuelError> {
self.ctx.store.consume_fuel(delta)
}

/// Resets the total and consumed amounts of fuel to 0 for the [`Store`](crate::Store).
///
/// # Errors
///
/// - If fuel metering is disabled.
pub fn reset_fuel(&mut self) -> Result<(), FuelError> {
self.ctx.store.reset_fuel()
}
}

impl<T> AsContext for Caller<'_, T> {
Expand Down
26 changes: 25 additions & 1 deletion crates/wasmi/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct Fuel {
/// The remaining fuel.
remaining: u64,
/// The total amount of fuel so far.
total: u64,
pub(crate) total: u64,
}

impl Fuel {
Expand Down Expand Up @@ -219,6 +219,12 @@ impl Fuel {
.ok_or(TrapCode::OutOfFuel)?;
Ok(self.remaining)
}

/// Reset the total and remaining fuel amounts to 0.
pub fn reset_fuel(&mut self) {
self.remaining = 0;
self.total = 0;
}
}

impl StoreInner {
Expand Down Expand Up @@ -752,6 +758,14 @@ impl<T> Store<T> {
Some(self.inner.fuel.fuel_consumed())
}

/// Returns the amount of total [`Fuel`] supplied to the [`Store`].
///
/// Returns `None` if fuel metering is disabled.
pub fn fuel_total(&self) -> Option<u64> {
self.check_fuel_metering_enabled().ok()?;
Some(self.inner.fuel.total)
}

/// Synthetically consumes an amount of fuel for the [`Store`].
///
/// Returns the remaining amount of fuel after this operation.
Expand All @@ -772,6 +786,16 @@ impl<T> Store<T> {
.map_err(|_error| FuelError::out_of_fuel())
}

/// Resets the total and consumed amounts of fuel to 0 for the [`Store`].
///
/// # Errors
///
/// - If fuel metering is disabled.
pub fn reset_fuel(&mut self) -> Result<(), FuelError> {
self.check_fuel_metering_enabled()?;
Ok(self.inner.fuel.reset_fuel())
}

/// Allocates a new [`TrampolineEntity`] and returns a [`Trampoline`] reference to it.
pub(super) fn alloc_trampoline(&mut self, func: TrampolineEntity<T>) -> Trampoline {
let idx = self.trampolines.alloc(func);
Expand Down

0 comments on commit 195f2b4

Please sign in to comment.