diff options
author | Vincent Ambo <mail@tazj.in> | 2022-10-03T13·08+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2022-10-04T21·27+0000 |
commit | b69b50feb1a1323188cf1f6da2141c5e8d21999a (patch) | |
tree | 6ff7a8dd8ac3182673e71913c7e8bd90aed9df44 /tvix/eval/src/observer.rs | |
parent | d6643f66b1cb8db5fc802ad53ef0d2633a51e815 (diff) |
refactor(tvix/eval): split observer traits in two r/5033
There are actually two different types of observers, the ones that observe the compiler (and emitted chunks from different kinds of expressions), and the ones that trace runtime execution. Use of the NoOpObserver is unchanged, it simply implements both traits. Change-Id: I4277b82674c259ec55238a0de3bb1cdf5e21a258 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6852 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval/src/observer.rs')
-rw-r--r-- | tvix/eval/src/observer.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/tvix/eval/src/observer.rs b/tvix/eval/src/observer.rs index e5562e3697d3..ce237529f5b5 100644 --- a/tvix/eval/src/observer.rs +++ b/tvix/eval/src/observer.rs @@ -1,9 +1,11 @@ -//! Implements a trait for things that wish to observe internal state +//! Implements traits for things that wish to observe internal state //! changes of tvix-eval. //! //! This can be used to gain insights from compilation, to trace the //! runtime, and so on. - +//! +//! All methods are optional, that is, observers can implement only +/// what they are interested in observing. use codemap::CodeMap; use std::io::Write; use std::rc::Rc; @@ -15,11 +17,8 @@ use crate::value::Lambda; use crate::Value; /// Implemented by types that wish to observe internal happenings of -/// Tvix. -/// -/// All methods are optional, that is, observers can implement only -/// what they are interested in observing. -pub trait Observer { +/// the Tvix compiler. +pub trait CompilerObserver { /// Called when the compiler finishes compilation of the top-level /// of an expression (usually the root Nix expression of a file). fn observe_compiled_toplevel(&mut self, _: &Rc<Lambda>) {} @@ -34,7 +33,11 @@ pub trait Observer { /// Called when the compiler finishes compilation of a thunk. fn observe_compiled_thunk(&mut self, _: &Rc<Lambda>) {} +} +/// Implemented by types that wish to observe internal happenings of +/// the Tvix virtual machine at runtime. +pub trait RuntimeObserver { /// Called when the runtime enters a new call frame. fn observe_enter_frame(&mut self, _arg_count: usize, _: &Rc<Lambda>, _call_depth: usize) {} @@ -59,7 +62,8 @@ pub trait Observer { #[derive(Default)] pub struct NoOpObserver {} -impl Observer for NoOpObserver {} +impl CompilerObserver for NoOpObserver {} +impl RuntimeObserver for NoOpObserver {} /// An observer that prints disassembled chunk information to its /// internal writer whenwever the compiler emits a toplevel function, @@ -97,7 +101,7 @@ impl<W: Write> DisassemblingObserver<W> { } } -impl<W: Write> Observer for DisassemblingObserver<W> { +impl<W: Write> CompilerObserver for DisassemblingObserver<W> { fn observe_compiled_toplevel(&mut self, lambda: &Rc<Lambda>) { self.lambda_header("toplevel", lambda); self.disassemble_chunk(&lambda.chunk); @@ -131,7 +135,7 @@ impl<W: Write> TracingObserver<W> { } } -impl<W: Write> Observer for TracingObserver<W> { +impl<W: Write> RuntimeObserver for TracingObserver<W> { fn observe_enter_frame(&mut self, arg_count: usize, lambda: &Rc<Lambda>, call_depth: usize) { let _ = writeln!( &mut self.writer, |