Skip to content

Commit

Permalink
Made arguments to functions in run injected through vm and not code.
Browse files Browse the repository at this point in the history
commit-id:f972f5b1
  • Loading branch information
orizi committed Oct 15, 2024
1 parent 087c498 commit b6bceae
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 133 deletions.
20 changes: 4 additions & 16 deletions crates/cairo-lang-runner/src/casm_run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2184,25 +2184,16 @@ pub fn extract_buffer(buffer: &ResOperand) -> (&CellRef, Felt252) {
(cell, base_offset)
}

/// Provides context for the `additional_initialization` callback function of [run_function].
pub struct RunFunctionContext<'a> {
pub vm: &'a mut VirtualMachine,
pub data_len: usize,
}

/// Runs CairoRunner on layout with prime.
/// Allows injecting custom CairoRunner.
pub fn run_function_with_runner(
data_len: usize,
additional_initialization: fn(
context: RunFunctionContext<'_>,
) -> Result<(), Box<CairoRunError>>,
additional_initialization: impl FnOnce(&mut VirtualMachine) -> Result<(), Box<CairoRunError>>,
hint_processor: &mut dyn HintProcessor,
runner: &mut CairoRunner,
) -> Result<(), Box<CairoRunError>> {
let end = runner.initialize(true).map_err(CairoRunError::from)?;

additional_initialization(RunFunctionContext { vm: &mut runner.vm, data_len })?;
additional_initialization(&mut runner.vm)?;

runner.run_until_pc(end, hint_processor).map_err(CairoRunError::from)?;
runner.end_run(true, false, hint_processor).map_err(CairoRunError::from)?;
Expand Down Expand Up @@ -2249,18 +2240,15 @@ pub struct RunFunctionResult {
pub fn run_function<'a, 'b: 'a>(
bytecode: impl Iterator<Item = &'a BigInt> + Clone,
builtins: Vec<BuiltinName>,
additional_initialization: fn(
context: RunFunctionContext<'_>,
) -> Result<(), Box<CairoRunError>>,
additional_initialization: impl FnOnce(&mut VirtualMachine) -> Result<(), Box<CairoRunError>>,
hint_processor: &mut dyn HintProcessor,
hints_dict: HashMap<usize, Vec<HintParams>>,
) -> Result<RunFunctionResult, Box<CairoRunError>> {
let data: Vec<MaybeRelocatable> =
bytecode.map(Felt252::from).map(MaybeRelocatable::from).collect();
let data_len = data.len();
let mut runner = build_cairo_runner(data, builtins, hints_dict)?;

run_function_with_runner(data_len, additional_initialization, hint_processor, &mut runner)?;
run_function_with_runner(additional_initialization, hint_processor, &mut runner)?;

let used_resources = runner
.get_execution_resources()
Expand Down
Loading

0 comments on commit b6bceae

Please sign in to comment.