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 <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-10-03 16:08:59 +03:00 committed by tazjin
parent d6643f66b1
commit b69b50feb1
3 changed files with 22 additions and 18 deletions

View file

@ -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)?;

View file

@ -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,

View file

@ -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)?;