feat(tvix/eval): insert import into the builtins itself

Adding `import` to builtins causes causes a bootstrap cycle because
the `import` builtin needs to be initialised with the set of globals
before being inserted into the globals, which also must contain
itself.

To break out of the cycle this hack wraps the builtins passed to the
compiler in an `Rc` (probably sensible anyways, as they will end up
getting cloned a bunch), containing a RefCell which gives us mutable
access to the builtins.

This opens up a potentially dangerous footgun in which we could mutate
the builtins at runtime leading to different compiler invocations
seeing different builtins, so it'd be nice to have some kind of
"finalised" status for them or some such, but I'm not sure how to
represent that atm.

Change-Id: I25f8d4d2a7e8472d401c8ba2f4bbf9d86ab2abcb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6867
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-10-04 23:58:21 +03:00 committed by tazjin
parent 880ea8a8fe
commit 4b9178fa2a
4 changed files with 32 additions and 14 deletions

View file

@ -1,4 +1,5 @@
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap},
rc::Rc,
time::{SystemTime, UNIX_EPOCH},
@ -43,7 +44,10 @@ pub(super) fn builtins() -> BTreeMap<NixString, Value> {
/// it needs to capture the [crate::SourceCode] structure to correctly track
/// source code locations while invoking a compiler.
// TODO: need to be able to pass through a CompilationObserver, too.
pub fn builtins_import(source: SourceCode) -> Builtin {
pub fn builtins_import(
globals: Rc<RefCell<HashMap<&'static str, Value>>>,
source: SourceCode,
) -> Builtin {
Builtin::new(
"import",
&[true],
@ -83,7 +87,7 @@ pub fn builtins_import(source: SourceCode) -> Builtin {
&parsed.tree().expr().unwrap(),
Some(path.clone()),
file,
HashMap::new(), // TODO: pass through globals
globals.clone(),
&mut NoOpObserver::default(),
)
.map_err(|err| ErrorKind::ImportCompilerError {