diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index c4c3e0690..aef6f5f06 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -44,6 +44,7 @@ use std::sync::Arc; pub use crate::builtins::global_builtins; pub use crate::compiler::{compile, prepare_globals}; pub use crate::errors::{Error, ErrorKind, EvalResult}; +use crate::observer::{CompilerObserver, RuntimeObserver}; pub use crate::pretty_ast::pretty_print_expr; pub use crate::source::SourceCode; pub use crate::value::Value; @@ -69,10 +70,9 @@ pub(crate) fn unwrap_or_clone_rc(rc: Rc) -> T { /// /// Public fields are intended to be set by the caller. Setting all /// fields is optional. -#[derive(Clone)] -pub struct Evaluation<'a> { +pub struct Evaluation<'code, 'co, 'ro> { /// The Nix source code to be evaluated. - code: &'a str, + code: &'code str, /// Optional location of the source code (i.e. path to the file it was read /// from). Used for error reporting, and for resolving relative paths in @@ -91,6 +91,14 @@ pub struct Evaluation<'a> { /// (optional) Nix search path, e.g. the value of `NIX_PATH` used /// for resolving items on the search path (such as ``). pub nix_path: Option, + + /// (optional) compiler observer for reporting on compilation + /// details, like the emitted bytecode. + pub compiler_observer: Option<&'co mut dyn CompilerObserver>, + + /// (optional) runtime observer, for reporting on execution steps + /// of Nix code. + pub runtime_observer: Option<&'ro mut dyn RuntimeObserver>, } /// Result of evaluating a piece of Nix code. If evaluation succeeded, a value @@ -109,11 +117,11 @@ pub struct EvaluationResult { pub warnings: Vec, } -impl<'a> Evaluation<'a> { +impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { /// Initialise an `Evaluation` for the given Nix source code snippet, and /// an optional code location. /// reporting the location of errors in the code. - pub fn new(code: &'a str, location: Option) -> Self { + pub fn new(code: &'code str, location: Option) -> Self { let source_map = SourceCode::new(); let location_str = location @@ -130,6 +138,8 @@ impl<'a> Evaluation<'a> { file, expr: None, nix_path: None, + compiler_observer: None, + runtime_observer: None, } } @@ -163,12 +173,15 @@ impl<'a> Evaluation<'a> { let builtins = crate::compiler::prepare_globals(Box::new(global_builtins(self.source_map()))); + let mut noop_observer = observer::NoOpObserver::default(); + let compiler_observer = self.compiler_observer.take().unwrap_or(&mut noop_observer); + let compiler_result = match compiler::compile( self.expr.as_ref().unwrap(), self.location.take(), self.file.clone(), builtins, - &mut observer::NoOpObserver::default(), // TODO: compilation observer + compiler_observer, ) { Ok(result) => result, Err(err) => { @@ -202,11 +215,8 @@ impl<'a> Evaluation<'a> { }) .unwrap_or_else(|| Default::default()); - let vm_result = run_lambda( - nix_path, - &mut observer::NoOpObserver::default(), // TODO: runtime observer - compiler_result.lambda, - ); + let runtime_observer = self.runtime_observer.take().unwrap_or(&mut noop_observer); + let vm_result = run_lambda(nix_path, runtime_observer, compiler_result.lambda); match vm_result { Ok(mut runtime_result) => {