Skip to content

Commit

Permalink
address TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Feb 3, 2025
1 parent 1716652 commit 4b27efd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
3 changes: 1 addition & 2 deletions miden/benches/program_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ fn program_execution(c: &mut Criterion) {
},
Err(_) => (DefaultHost::default(), StackInputs::default()),
};
host.load_mast_forest(StdLibrary::default().as_ref().mast_forest().clone())
.unwrap();
host.load_library(&StdLibrary::default()).unwrap();

// the name of the file without the extension
let source = std::fs::read_to_string(entry.path()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion miden/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ProveCmd {
let stack_inputs = input_data.parse_stack_inputs().map_err(Report::msg)?;
let mut host = DefaultHost::default()
.with_advice_provider(input_data.parse_advice_provider().map_err(Report::msg)?);
host.load_mast_forest(StdLibrary::default().mast_forest().clone()).unwrap();
host.load_library(&StdLibrary::default()).unwrap();

let proving_options =
self.get_proof_options().map_err(|err| Report::msg(format!("{err}")))?;
Expand Down
3 changes: 1 addition & 2 deletions miden/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ impl Analyze {
let stack_inputs = input_data.parse_stack_inputs().map_err(Report::msg)?;
let mut host = DefaultHost::default()
.with_advice_provider(input_data.parse_advice_provider().map_err(Report::msg)?);
host.load_mast_forest(StdLibrary::default().mast_forest().clone())
.into_diagnostic()?;
host.load_library(&StdLibrary::default()).into_diagnostic()?;

let execution_details: ExecutionDetails = analyze(program.as_str(), stack_inputs, host)
.expect("Could not retrieve execution details");
Expand Down
17 changes: 13 additions & 4 deletions processor/src/host/event_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ use std::collections::BTreeMap;

use crate::{ExecutionError, ProcessState};

/// Defines an interface for handling events emitted by the VM.
pub trait EventHandler<A> {
/// Returns the ID of the event this handler is responsible for.
fn id(&self) -> u32;

// TODO(plafer): `ProcessState` is a processor type, which can't be moved to core as-is. But we
// want `EventHandler` to be in core. How to fix this? The solution is probably to provide a
// `ProcessState` trait in core.
/// Mutates the advice provider based on the event emitted by the VM.
fn on_event(
&mut self,
process: ProcessState,
advice_provider: &mut A,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>>;
}

/// A registry of event handlers, indexed by event id.
#[derive(Default)]
pub struct EventHandlerRegistry<A> {
handlers: BTreeMap<u32, Box<dyn EventHandler<A>>>,
}

impl<A> EventHandlerRegistry<A> {
/// Register an event handler with the registry.
pub fn register_event_handler(
&mut self,
handler: Box<dyn EventHandler<A>>,
Expand All @@ -38,6 +40,7 @@ impl<A> EventHandlerRegistry<A> {
}
}

/// Register a set of event handlers with the registry.
pub fn register_event_handlers(
&mut self,
handlers: impl Iterator<Item = Box<dyn EventHandler<A>>> + 'static,
Expand All @@ -49,6 +52,7 @@ impl<A> EventHandlerRegistry<A> {
Ok(())
}

/// Returns a mutable reference to the event handler for the specified event ID.
pub fn get_event_handler(&mut self, event_id: u32) -> Option<&mut Box<dyn EventHandler<A>>> {
self.handlers.get_mut(&event_id)
}
Expand All @@ -67,7 +71,12 @@ impl<A> NoopEventHandler<A>
where
A: 'static,
{
/// Creates a new dummy event handler with the specified ID.
/// Creates an event handler with the specified ID.
pub fn new(id: u32) -> Self {
Self { id, _advice_provider: PhantomData }
}

/// Creates an event handler with the specified ID.
pub fn new_boxed(id: u32) -> Box<dyn EventHandler<A>> {
Box::new(Self { id, _advice_provider: PhantomData })
}
Expand Down
1 change: 0 additions & 1 deletion processor/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ where
A: AdviceProvider + 'static,
D: DebugHandler,
{
// TODO(plafer): make sure we use this in the tests when it makes sense
/// Loads the specified library into the host.
pub fn load_library(&mut self, library: &impl HostLibrary) -> Result<(), ExecutionError> {
self.load_mast_forest(library.get_mast_forest())?;
Expand Down
7 changes: 7 additions & 0 deletions test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ impl Test {
for library in &self.libraries {
host.load_mast_forest(library.mast_forest().clone()).unwrap();
}
host.load_library(&StdLibrary::default()).unwrap();
host.register_event_handlers([NoopEventHandler::new_boxed(1)].into_iter())
.unwrap();

// execute the test
let mut process = Process::new(
Expand Down Expand Up @@ -419,6 +422,10 @@ impl Test {
for library in &self.libraries {
host.load_mast_forest(library.mast_forest().clone()).unwrap();
}
host.load_library(&StdLibrary::default()).unwrap();
host.register_event_handlers([NoopEventHandler::new_boxed(1)].into_iter())
.unwrap();

processor::execute_iter(&program, self.stack_inputs.clone(), &mut host)
}

Expand Down

0 comments on commit 4b27efd

Please sign in to comment.