about summary refs log tree commit diff
path: root/tvix/eval/src/builtins/impure.rs
blob: 348963e8931389ff140953a72f74b06520fd3283 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::{
    cell::RefCell,
    collections::{BTreeMap, HashMap},
    rc::Rc,
    time::{SystemTime, UNIX_EPOCH},
};

use crate::{
    errors::ErrorKind,
    observer::NoOpObserver,
    value::{Builtin, NixString, Thunk},
    vm::VM,
    SourceCode, Value,
};

fn impure_builtins() -> Vec<Builtin> {
    vec![]
}

/// Return all impure builtins, that is all builtins which may perform I/O
/// outside of the VM and so cannot be used in all contexts (e.g. WASM).
pub(super) fn builtins() -> BTreeMap<NixString, Value> {
    let mut map: BTreeMap<NixString, Value> = impure_builtins()
        .into_iter()
        .map(|b| (b.name().into(), Value::Builtin(b)))
        .collect();

    // currentTime pins the time at which evaluation was started
    {
        let seconds = match SystemTime::now().duration_since(UNIX_EPOCH) {
            Ok(dur) => dur.as_secs() as i64,

            // This case is hit if the system time is *before* epoch.
            Err(err) => -(err.duration().as_secs() as i64),
        };

        map.insert(NixString::from("currentTime"), Value::Integer(seconds));
    }

    map
}

/// Constructs and inserts the `import` builtin. This builtin is special in that
/// 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(
    globals: Rc<RefCell<HashMap<&'static str, Value>>>,
    source: SourceCode,
) -> Builtin {
    Builtin::new(
        "import",
        &[true],
        move |mut args: Vec<Value>, vm: &mut VM| {
            let path = super::coerce_value_to_path(&args.pop().unwrap(), vm)?;

            let contents =
                std::fs::read_to_string(&path).map_err(|err| ErrorKind::ReadFileError {
                    path: path.clone(),
                    error: Rc::new(err),
                })?;

            let parsed = rnix::ast::Root::parse(&contents);
            let errors = parsed.errors();

            if !errors.is_empty() {
                return Err(ErrorKind::ImportParseError {
                    path,
                    errors: errors.to_vec(),
                });
            }

            let file = source.add_file(path.to_string_lossy().to_string(), contents);

            let result = crate::compile(
                &parsed.tree().expr().unwrap(),
                Some(path.clone()),
                file,
                globals.clone(),
                &mut NoOpObserver::default(),
            )
            .map_err(|err| ErrorKind::ImportCompilerError {
                path: path.clone(),
                errors: vec![err],
            })?;

            if !result.errors.is_empty() {
                return Err(ErrorKind::ImportCompilerError {
                    path,
                    errors: result.errors,
                });
            }

            for warning in result.warnings {
                vm.push_warning(warning);
            }

            // Compilation succeeded, we can construct a thunk from whatever it spat
            // out and return that.
            Ok(Value::Thunk(Thunk::new(result.lambda)))
        },
    )
}