about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-10-03T13·08+0300
committertazjin <tazjin@tvl.su>2022-10-04T21·27+0000
commitb69b50feb1a1323188cf1f6da2141c5e8d21999a (patch)
tree6ff7a8dd8ac3182673e71913c7e8bd90aed9df44 /tvix
parentd6643f66b1cb8db5fc802ad53ef0d2633a51e815 (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')
-rw-r--r--tvix/eval/src/compiler/mod.rs8
-rw-r--r--tvix/eval/src/observer.rs24
-rw-r--r--tvix/eval/src/vm.rs8
3 files changed, 22 insertions, 18 deletions
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<PathBuf>,
         file: Arc<codemap::File>,
         globals: HashMap<&'static str, Value>,
-        observer: &'observer mut dyn Observer,
+        observer: &'observer mut dyn CompilerObserver,
     ) -> EvalResult<Self> {
         let mut root_dir = match location {
             Some(dir) => Ok(dir),
@@ -1146,7 +1146,7 @@ pub fn compile(
     location: Option<PathBuf>,
     file: Arc<codemap::File>,
     globals: HashMap<&'static str, Value>,
-    observer: &mut dyn Observer,
+    observer: &mut dyn CompilerObserver,
 ) -> EvalResult<CompilationOutput> {
     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<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,
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<usize>,
 
-    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<T: Clone>(rc: Rc<T>) -> T {
     Rc::try_unwrap(rc).unwrap_or_else(|rc| (*rc).clone())
 }
 
-pub fn run_lambda(observer: &mut dyn Observer, lambda: Rc<Lambda>) -> EvalResult<Value> {
+pub fn run_lambda(observer: &mut dyn RuntimeObserver, lambda: Rc<Lambda>) -> EvalResult<Value> {
     let mut vm = VM::new(observer);
     let value = vm.call(lambda, Upvalues::with_capacity(0), 0)?;
     vm.force_for_output(&value)?;