From b530e496a5962a3998773343d7ed6a9dd84b7753 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 4 Oct 2022 18:27:49 +0300 Subject: feat(tvix/eval): initial implementation of `builtins.import` This adds an initial working version of builtins.import which encapsulates the entire functionality of `import` within the builtin itself, without requiring any changes in the compiler or VM. The key insight that enables this is that we can simply return a Thunk from `import` that is constructed from the output of running the compiler and - ta-da! - no other component needs to know about it. A couple of notes: * builtins.import needs to capture variables like the SourceCode structure. This means it can not currently be constructed the same way as other builtins and has special handling, which leaks out to `eval.rs`. I have postponed dealing with that until we have this working a bit more. * the `globals` are not yet passed through * the error representation for the new variants is absolutely not done yet, we probably want to switch to something that supports cause-chaining now (like miette) * there is no mechanism for emitting warnings at runtime; we need to add that Change-Id: I3117a7ae3ff2432bf44f5ff05ad35f47faca31d5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6857 Reviewed-by: sterni Reviewed-by: wpcarro Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/eval.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'tvix/eval/src/eval.rs') diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs index 7b7d3983d4ec..aed4292282b9 100644 --- a/tvix/eval/src/eval.rs +++ b/tvix/eval/src/eval.rs @@ -58,12 +58,21 @@ pub fn interpret(code: &str, location: Option, options: Options) -> Eva println!("{:?}", root_expr); } + // TODO: encapsulate this import weirdness in builtins + let mut builtins = global_builtins(); + + #[cfg(feature = "impure")] + builtins.insert( + "import", + Value::Builtin(crate::builtins::impure::builtins_import(source.clone())), + ); + let result = if options.dump_bytecode { crate::compiler::compile( &root_expr, location, file.clone(), - global_builtins(), + builtins, &mut DisassemblingObserver::new(source.clone(), std::io::stderr()), ) } else { @@ -71,7 +80,7 @@ pub fn interpret(code: &str, location: Option, options: Options) -> Eva &root_expr, location, file.clone(), - global_builtins(), + builtins, &mut NoOpObserver::default(), ) }?; -- cgit 1.4.1