refactor(tvix/eval): introduce source::SourceCode type

This type hides away the lower-level handling of most codemap data
structures, especially to library consumers (see corresponding changes
in tvixbolt).

This will help with implement `import` by giving us central control
over how the codemap works.

Change-Id: Ifcea36776879725871b30c518aeb96ab5fda035a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6855
Tested-by: BuildkiteCI
Reviewed-by: wpcarro <wpcarro@gmail.com>
This commit is contained in:
Vincent Ambo 2022-10-04 17:05:34 +03:00 committed by tazjin
parent 2ff764ceb7
commit 3530404a4a
8 changed files with 105 additions and 57 deletions

View file

@ -6,7 +6,6 @@
//!
//! 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;
use tabwriter::TabWriter;
@ -14,6 +13,7 @@ use tabwriter::TabWriter;
use crate::chunk::Chunk;
use crate::opcode::{CodeIdx, OpCode};
use crate::value::Lambda;
use crate::SourceCode;
use crate::Value;
/// Implemented by types that wish to observe internal happenings of
@ -69,14 +69,14 @@ impl RuntimeObserver for NoOpObserver {}
/// internal writer whenwever the compiler emits a toplevel function,
/// closure or thunk.
pub struct DisassemblingObserver<W: Write> {
codemap: Rc<CodeMap>,
source: SourceCode,
writer: TabWriter<W>,
}
impl<W: Write> DisassemblingObserver<W> {
pub fn new(codemap: Rc<CodeMap>, writer: W) -> Self {
pub fn new(source: SourceCode, writer: W) -> Self {
Self {
codemap,
source,
writer: TabWriter::new(writer),
}
}
@ -96,7 +96,7 @@ impl<W: Write> DisassemblingObserver<W> {
let width = format!("{:#x}", chunk.code.len() - 1).len();
for (idx, _) in chunk.code.iter().enumerate() {
let _ = chunk.disassemble_op(&mut self.writer, &self.codemap, width, CodeIdx(idx));
let _ = chunk.disassemble_op(&mut self.writer, &self.source, width, CodeIdx(idx));
}
}
}