about summary refs log blame commit diff
path: root/tvix/eval/src/builtins/mod.rs
blob: b5e8bb289b0e4b380b2ae9f7efded3ea40186ea1 (plain) (tree)


















                                                                               




                                                                  






                                                             
//! This module implements the builtins exposed in the Nix language.
//!
//! See //tvix/eval/docs/builtins.md for a some context on the
//! available builtins in Nix.

use std::collections::HashMap;

use crate::value::{Builtin, Value};

macro_rules! builtin {
    ( $map:ident, $name:literal, $arity:literal, $body:expr ) => {
        $map.insert($name, Value::Builtin(Builtin::new($name, $arity, $body)));
    };
}

/// Set of Nix builtins that are globally available.
pub fn global_builtins() -> HashMap<&'static str, Value> {
    let mut globals = HashMap::new();

    builtin!(globals, "isNull", 1, |args| Ok(Value::Bool(matches!(
        args[0],
        Value::Null
    ))));

    builtin!(globals, "toString", 1, |args| {
        // TODO: toString is actually not the same as Display
        Ok(Value::String(format!("{}", args[0]).into()))
    });

    globals
}