-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add VM hooks as rust conditional feature
- Loading branch information
Showing
5 changed files
with
166 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
use felt::Felt; | ||
|
||
use crate::{ | ||
hint_processor::hint_processor_definition::HintReference, | ||
serde::deserialize_program::ApTracking, types::exec_scope::ExecutionScopes, | ||
}; | ||
|
||
use super::{errors::vm_errors::VirtualMachineError, vm_core::VirtualMachine}; | ||
|
||
type HookFunc = Arc< | ||
dyn Fn( | ||
&mut CairoRunner, | ||
&mut VirtualMachine, | ||
&HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> | ||
+ Sync | ||
+ Send, | ||
>; | ||
|
||
#[derive(Clone)] | ||
pub struct Hooks { | ||
before_first_step: Option<HookFunc>, | ||
pre_step_instruction: Option<HookFunc>, | ||
post_step_instruction: Option<HookFunc>, | ||
} | ||
|
||
impl Hooks { | ||
pub fn new( | ||
before_first_step: Option<HookFunc>, | ||
pre_step_instruction: Option<HookFunc>, | ||
post_step_instruction: Option<HookFunc>, | ||
) -> Self { | ||
Hooks { | ||
before_first_step, | ||
pre_step_instruction, | ||
post_step_instruction, | ||
} | ||
} | ||
|
||
pub fn execute_pre_step_instruction( | ||
&self, | ||
runner: &mut CairoRunner, | ||
vm: &mut VirtualMachine, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.pre_step_instruction { | ||
(hook_func)(runner, vm, hint_data_dictionary)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn execute_post_step_instruction( | ||
&self, | ||
runner: &mut CairoRunner, | ||
vm: &mut VirtualMachine, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.post_step_instruction { | ||
(hook_func)(runner, vm, hint_data_dictionary)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn execute_before_first_step( | ||
&self, | ||
runner: &mut CairoRunner, | ||
vm: &mut VirtualMachine, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.before_first_step { | ||
(hook_func)(runner, vm, hint_data_dictionary)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ pub mod security; | |
pub mod trace; | ||
pub mod vm_core; | ||
pub mod vm_memory; | ||
|
||
#[cfg(hooks)] | ||
pub mod hooks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters