feat(tvix/eval): add EvalIO to public crate API
This lets users set the `io_handle` field on an `Evaluation`, which is then propagated to the VM. Change-Id: I616d7140724fb2b4db47c2ebf95451d5303a487a Reviewed-on: https://cl.tvl.fyi/c/depot/+/7566 Reviewed-by: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI
This commit is contained in:
parent
25fc6b7c25
commit
c3c4d752c9
6 changed files with 42 additions and 14 deletions
|
@ -27,7 +27,7 @@ pub trait EvalIO {
|
|||
|
||||
/// Implementation of [`EvalIO`] that simply uses the equivalent
|
||||
/// standard library functions, i.e. does local file-IO.
|
||||
struct StdIO;
|
||||
pub struct StdIO;
|
||||
|
||||
impl EvalIO for StdIO {
|
||||
fn read_to_string(&self, path: PathBuf) -> Result<String, ErrorKind> {
|
||||
|
@ -41,7 +41,7 @@ impl EvalIO for StdIO {
|
|||
|
||||
/// Dummy implementation of [`EvalIO`], can be used in contexts where
|
||||
/// IO is not available but code should "pretend" that it is.
|
||||
struct DummyIO;
|
||||
pub struct DummyIO;
|
||||
|
||||
impl EvalIO for DummyIO {
|
||||
fn read_to_string(&self, _: PathBuf) -> Result<String, ErrorKind> {
|
||||
|
|
|
@ -45,6 +45,7 @@ use std::sync::Arc;
|
|||
pub use crate::builtins::global_builtins;
|
||||
pub use crate::compiler::{compile, prepare_globals};
|
||||
pub use crate::errors::{Error, ErrorKind, EvalResult};
|
||||
pub use crate::io::{DummyIO, EvalIO, StdIO};
|
||||
use crate::observer::{CompilerObserver, RuntimeObserver};
|
||||
pub use crate::pretty_ast::pretty_print_expr;
|
||||
pub use crate::source::SourceCode;
|
||||
|
@ -86,6 +87,12 @@ pub struct Evaluation<'code, 'co, 'ro> {
|
|||
/// Top-level file reference for this code inside the source map.
|
||||
file: Arc<codemap::File>,
|
||||
|
||||
/// Implementation of file-IO to use during evaluation, e.g. for
|
||||
/// impure builtins.
|
||||
///
|
||||
/// Defaults to [`DummyIO`] if not set explicitly.
|
||||
pub io_handle: Box<dyn EvalIO>,
|
||||
|
||||
/// (optional) Nix search path, e.g. the value of `NIX_PATH` used
|
||||
/// for resolving items on the search path (such as `<nixpkgs>`).
|
||||
pub nix_path: Option<String>,
|
||||
|
@ -137,6 +144,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
|
|||
location,
|
||||
source_map,
|
||||
file,
|
||||
io_handle: Box::new(DummyIO {}),
|
||||
nix_path: None,
|
||||
compiler_observer: None,
|
||||
runtime_observer: None,
|
||||
|
@ -216,7 +224,12 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
|
|||
.unwrap_or_else(|| Default::default());
|
||||
|
||||
let runtime_observer = self.runtime_observer.take().unwrap_or(&mut noop_observer);
|
||||
let vm_result = run_lambda(nix_path, runtime_observer, compiler_result.lambda);
|
||||
let vm_result = run_lambda(
|
||||
nix_path,
|
||||
self.io_handle,
|
||||
runtime_observer,
|
||||
compiler_result.lambda,
|
||||
);
|
||||
|
||||
match vm_result {
|
||||
Ok(mut runtime_result) => {
|
||||
|
|
|
@ -17,7 +17,10 @@ fn eval_test(code_path: &str, expect_success: bool) {
|
|||
return;
|
||||
}
|
||||
|
||||
let result = crate::Evaluation::new(&code, Some(code_path.into())).evaluate();
|
||||
let mut eval = crate::Evaluation::new(&code, Some(code_path.into()));
|
||||
eval.io_handle = Box::new(crate::StdIO);
|
||||
|
||||
let result = eval.evaluate();
|
||||
|
||||
if expect_success && !result.errors.is_empty() {
|
||||
panic!(
|
||||
|
@ -64,7 +67,10 @@ fn eval_test(code_path: &str, expect_success: bool) {
|
|||
fn identity(code_path: &str) {
|
||||
let code = std::fs::read_to_string(code_path).expect("should be able to read test code");
|
||||
|
||||
let result = crate::Evaluation::new(&code, None).evaluate();
|
||||
let mut eval = crate::Evaluation::new(&code, None);
|
||||
eval.io_handle = Box::new(crate::StdIO);
|
||||
|
||||
let result = eval.evaluate();
|
||||
assert!(
|
||||
result.errors.is_empty(),
|
||||
"evaluation of identity test failed: {:?}",
|
||||
|
|
|
@ -10,7 +10,7 @@ mod nix_eq {
|
|||
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
|
||||
fn reflexive(x: NixAttrs) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
assert!(x.nix_eq(&x, &mut vm).unwrap())
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ mod nix_eq {
|
|||
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
|
||||
fn symmetric(x: NixAttrs, y: NixAttrs) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
assert_eq!(
|
||||
x.nix_eq(&y, &mut vm).unwrap(),
|
||||
|
@ -29,7 +29,7 @@ mod nix_eq {
|
|||
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
|
||||
fn transitive(x: NixAttrs, y: NixAttrs, z: NixAttrs) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
if x.nix_eq(&y, &mut vm).unwrap() && y.nix_eq(&z, &mut vm).unwrap() {
|
||||
assert!(x.nix_eq(&z, &mut vm).unwrap())
|
||||
|
|
|
@ -565,7 +565,7 @@ mod tests {
|
|||
#[proptest(ProptestConfig { cases: 5, ..Default::default() })]
|
||||
fn reflexive(x: Value) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
assert!(x.nix_eq(&x, &mut vm).unwrap())
|
||||
}
|
||||
|
@ -573,7 +573,7 @@ mod tests {
|
|||
#[proptest(ProptestConfig { cases: 5, ..Default::default() })]
|
||||
fn symmetric(x: Value, y: Value) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
assert_eq!(
|
||||
x.nix_eq(&y, &mut vm).unwrap(),
|
||||
|
@ -584,7 +584,7 @@ mod tests {
|
|||
#[proptest(ProptestConfig { cases: 5, ..Default::default() })]
|
||||
fn transitive(x: Value, y: Value, z: Value) {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
if x.nix_eq(&y, &mut vm).unwrap() && y.nix_eq(&z, &mut vm).unwrap() {
|
||||
assert!(x.nix_eq(&z, &mut vm).unwrap())
|
||||
|
@ -594,7 +594,7 @@ mod tests {
|
|||
#[test]
|
||||
fn list_int_float_fungibility() {
|
||||
let mut observer = NoOpObserver {};
|
||||
let mut vm = VM::new(Default::default(), &mut observer);
|
||||
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
|
||||
|
||||
let v1 = Value::List(NixList::from(vec![Value::Integer(1)]));
|
||||
let v2 = Value::List(NixList::from(vec![Value::Float(1.0)]));
|
||||
|
|
|
@ -7,6 +7,7 @@ use std::{cmp::Ordering, collections::BTreeMap, ops::DerefMut, path::PathBuf, rc
|
|||
use crate::{
|
||||
chunk::Chunk,
|
||||
errors::{Error, ErrorKind, EvalResult},
|
||||
io::EvalIO,
|
||||
nix_search_path::NixSearchPath,
|
||||
observer::RuntimeObserver,
|
||||
opcode::{CodeIdx, Count, JumpOffset, OpCode, StackIdx, UpvalueIdx},
|
||||
|
@ -65,6 +66,8 @@ pub struct VM<'o> {
|
|||
|
||||
nix_search_path: NixSearchPath,
|
||||
|
||||
io_handle: Box<dyn EvalIO>,
|
||||
|
||||
observer: &'o mut dyn RuntimeObserver,
|
||||
}
|
||||
|
||||
|
@ -150,7 +153,11 @@ macro_rules! cmp_op {
|
|||
}
|
||||
|
||||
impl<'o> VM<'o> {
|
||||
pub fn new(nix_search_path: NixSearchPath, observer: &'o mut dyn RuntimeObserver) -> Self {
|
||||
pub fn new(
|
||||
nix_search_path: NixSearchPath,
|
||||
io_handle: Box<dyn EvalIO>,
|
||||
observer: &'o mut dyn RuntimeObserver,
|
||||
) -> Self {
|
||||
// Backtrace-on-stack-overflow is some seriously weird voodoo and
|
||||
// very unsafe. This double-guard prevents it from accidentally
|
||||
// being enabled on release builds.
|
||||
|
@ -162,6 +169,7 @@ impl<'o> VM<'o> {
|
|||
|
||||
Self {
|
||||
nix_search_path,
|
||||
io_handle,
|
||||
observer,
|
||||
frames: vec![],
|
||||
stack: vec![],
|
||||
|
@ -1084,10 +1092,11 @@ impl<'o> VM<'o> {
|
|||
|
||||
pub fn run_lambda(
|
||||
nix_search_path: NixSearchPath,
|
||||
io_handle: Box<dyn EvalIO>,
|
||||
observer: &mut dyn RuntimeObserver,
|
||||
lambda: Rc<Lambda>,
|
||||
) -> EvalResult<RuntimeResult> {
|
||||
let mut vm = VM::new(nix_search_path, observer);
|
||||
let mut vm = VM::new(nix_search_path, io_handle, observer);
|
||||
|
||||
// Retain the top-level span of the expression in this lambda, as
|
||||
// synthetic "calls" in deep_force will otherwise not have a span
|
||||
|
|
Loading…
Reference in a new issue