Skip to content

Commit 104eabb

Browse files
committed
add InstrEncoder::{get_mut, bump_fuel_consumption} methods
1 parent 1d73285 commit 104eabb

File tree

1 file changed

+33
-1
lines changed
  • crates/wasmi/src/engine/translator/func2

1 file changed

+33
-1
lines changed

crates/wasmi/src/engine/translator/func2/instrs.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::{Instr, Reset};
2-
use crate::ir::Instruction;
2+
use crate::{
3+
core::FuelCostsProvider,
4+
engine::translator::{utils::FuelInfo, BumpFuelConsumption},
5+
ir::Instruction,
6+
Error,
7+
};
38
use alloc::vec::{self, Vec};
49

510
/// Creates and encodes the list of [`Instruction`]s for a function.
@@ -47,6 +52,33 @@ impl InstrEncoder {
4752
&self.instrs[instr.into_usize()]
4853
}
4954

55+
/// Returns an exclusive reference to the [`Instruction`] associated to [`Instr`].
56+
///
57+
/// # Panics
58+
///
59+
/// If `instr` is out of bounds for `self`.
60+
pub fn get_mut(&mut self, instr: Instr) -> &mut Instruction {
61+
&mut self.instrs[instr.into_usize()]
62+
}
63+
64+
/// Bumps consumed fuel for [`Instruction::ConsumeFuel`] of `instr` by `delta`.
65+
///
66+
/// # Errors
67+
///
68+
/// If consumed fuel is out of bounds after this operation.
69+
pub fn bump_fuel_consumption<F>(&mut self, fuel_info: &FuelInfo, f: F) -> Result<(), Error>
70+
where
71+
F: FnOnce(&FuelCostsProvider) -> u64,
72+
{
73+
let FuelInfo::Some { costs, instr } = fuel_info else {
74+
// Fuel metering is disabled so we can bail out.
75+
return Ok(());
76+
};
77+
let fuel_consumed = f(costs);
78+
self.get_mut(*instr).bump_fuel_consumption(fuel_consumed)?;
79+
Ok(())
80+
}
81+
5082
/// Returns an iterator yielding all [`Instruction`]s of the [`InstrEncoder`].
5183
///
5284
/// # Note

0 commit comments

Comments
 (0)