From b69b50feb1a1323188cf1f6da2141c5e8d21999a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 3 Oct 2022 16:08:59 +0300 Subject: refactor(tvix/eval): split observer traits in two 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 --- tvix/eval/src/compiler/mod.rs | 8 ++++---- tvix/eval/src/observer.rs | 24 ++++++++++++++---------- tvix/eval/src/vm.rs | 8 ++++---- 3 files changed, 22 insertions(+), 18 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 6a3d10acf62d..d69566f070d2 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use crate::chunk::Chunk; use crate::errors::{Error, ErrorKind, EvalResult}; -use crate::observer::Observer; +use crate::observer::CompilerObserver; use crate::opcode::{CodeIdx, Count, JumpOffset, OpCode, UpvalueIdx}; use crate::value::{Closure, Lambda, Thunk, Value}; use crate::warnings::{EvalWarning, WarningKind}; @@ -95,7 +95,7 @@ struct Compiler<'observer> { /// Carry an observer for the compilation process, which is called /// whenever a chunk is emitted. - observer: &'observer mut dyn Observer, + observer: &'observer mut dyn CompilerObserver, } /// Compiler construction @@ -104,7 +104,7 @@ impl<'observer> Compiler<'observer> { location: Option, file: Arc, globals: HashMap<&'static str, Value>, - observer: &'observer mut dyn Observer, + observer: &'observer mut dyn CompilerObserver, ) -> EvalResult { let mut root_dir = match location { Some(dir) => Ok(dir), @@ -1146,7 +1146,7 @@ pub fn compile( location: Option, file: Arc, globals: HashMap<&'static str, Value>, - observer: &mut dyn Observer, + observer: &mut dyn CompilerObserver, ) -> EvalResult { let mut c = Compiler::new(location, file, globals, observer)?; 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) {} @@ -34,7 +33,11 @@ pub trait Observer { /// Called when the compiler finishes compilation of a thunk. fn observe_compiled_thunk(&mut self, _: &Rc) {} +} +/// 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, _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 DisassemblingObserver { } } -impl Observer for DisassemblingObserver { +impl CompilerObserver for DisassemblingObserver { fn observe_compiled_toplevel(&mut self, lambda: &Rc) { self.lambda_header("toplevel", lambda); self.disassemble_chunk(&lambda.chunk); @@ -131,7 +135,7 @@ impl TracingObserver { } } -impl Observer for TracingObserver { +impl RuntimeObserver for TracingObserver { fn observe_enter_frame(&mut self, arg_count: usize, lambda: &Rc, call_depth: usize) { let _ = writeln!( &mut self.writer, diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index 39c9c6e822df..41f51a987459 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -6,7 +6,7 @@ use std::{cell::RefMut, rc::Rc}; use crate::{ chunk::Chunk, errors::{Error, ErrorKind, EvalResult}, - observer::Observer, + observer::RuntimeObserver, opcode::{CodeIdx, Count, JumpOffset, OpCode, StackIdx, UpvalueIdx}, upvalues::{UpvalueCarrier, Upvalues}, value::{Builtin, Closure, CoercionKind, Lambda, NixAttrs, NixList, Thunk, Value}, @@ -43,7 +43,7 @@ pub struct VM<'o> { /// dynamically resolved (`with`). with_stack: Vec, - observer: &'o mut dyn Observer, + observer: &'o mut dyn RuntimeObserver, } /// This macro wraps a computation that returns an ErrorKind or a @@ -127,7 +127,7 @@ macro_rules! cmp_op { } impl<'o> VM<'o> { - pub fn new(observer: &'o mut dyn Observer) -> Self { + pub fn new(observer: &'o mut dyn RuntimeObserver) -> Self { Self { observer, frames: vec![], @@ -780,7 +780,7 @@ fn unwrap_or_clone_rc(rc: Rc) -> T { Rc::try_unwrap(rc).unwrap_or_else(|rc| (*rc).clone()) } -pub fn run_lambda(observer: &mut dyn Observer, lambda: Rc) -> EvalResult { +pub fn run_lambda(observer: &mut dyn RuntimeObserver, lambda: Rc) -> EvalResult { let mut vm = VM::new(observer); let value = vm.call(lambda, Upvalues::with_capacity(0), 0)?; vm.force_for_output(&value)?; -- cgit 1.4.1