refactor(tvix/eval): directly return builtin tuples from macro

All invocations of the builtin macro had to previously filter through
the `builtin_tuple` function, but it's more sensible to directly
return these from the macro.

Change-Id: I45600ba84d56c9528d3e92570461c319eea595ce
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7825
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Vincent Ambo 2023-01-13 14:18:01 +03:00 committed by tazjin
parent 972c867b36
commit f12f938166
5 changed files with 23 additions and 34 deletions

View file

@ -209,8 +209,8 @@ pub fn builtins(_args: TokenStream, item: TokenStream) -> TokenStream {
} }
items.push(parse_quote! { items.push(parse_quote! {
pub fn builtins() -> Vec<crate::Builtin> { pub fn builtins() -> Vec<(&'static str, Value)> {
vec![#(#builtins),*] vec![#(#builtins),*].into_iter().map(|b| (b.name(), Value::Builtin(b))).collect()
} }
}); });

View file

@ -1,11 +1,9 @@
pub use tvix_eval::internal; pub use tvix_eval::{Builtin, BuiltinArgument, Value, VM};
pub use tvix_eval::Value;
use tvix_eval_builtin_macros::builtins; use tvix_eval_builtin_macros::builtins;
#[builtins] #[builtins]
mod builtins { mod builtins {
use tvix_eval::internal::VM; use tvix_eval::{ErrorKind, Value, VM};
use tvix_eval::{ErrorKind, Value};
/// Test docstring. /// Test docstring.
/// ///
@ -26,13 +24,21 @@ fn builtins() {
let builtins = builtins::builtins(); let builtins = builtins::builtins();
assert_eq!(builtins.len(), 2); assert_eq!(builtins.len(), 2);
let identity = builtins.iter().find(|b| b.name() == "identity").unwrap(); let (_, identity) = builtins
assert_eq!( .iter()
.find(|(name, _)| *name == "identity")
.unwrap();
match identity {
Value::Builtin(identity) => assert_eq!(
identity.documentation(), identity.documentation(),
Some( Some(
r#" Test docstring. r#" Test docstring.
It has multiple lines!"# It has multiple lines!"#
) )
); ),
_ => panic!("builtin was not a builtin"),
}
} }

View file

@ -65,10 +65,7 @@ mod impure_builtins {
/// Return all impure builtins, that is all builtins which may perform I/O /// 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). /// outside of the VM and so cannot be used in all contexts (e.g. WASM).
pub fn impure_builtins() -> Vec<(&'static str, Value)> { pub fn impure_builtins() -> Vec<(&'static str, Value)> {
let mut result = impure_builtins::builtins() let mut result = impure_builtins::builtins();
.into_iter()
.map(super::builtin_tuple)
.collect::<Vec<_>>();
result.push(( result.push((
"storeDir", "storeDir",

View file

@ -14,7 +14,7 @@ use crate::arithmetic_op;
use crate::warnings::WarningKind; use crate::warnings::WarningKind;
use crate::{ use crate::{
errors::{ErrorKind, EvalResult}, errors::{ErrorKind, EvalResult},
value::{Builtin, CoercionKind, NixAttrs, NixList, NixString, Value}, value::{CoercionKind, NixAttrs, NixList, NixString, Value},
vm::VM, vm::VM,
}; };
@ -968,17 +968,10 @@ mod pure_builtins {
} }
} }
pub(crate) fn builtin_tuple(builtin: Builtin) -> (&'static str, Value) {
(builtin.name(), Value::Builtin(builtin))
}
/// The set of standard pure builtins in Nix, mostly concerned with /// The set of standard pure builtins in Nix, mostly concerned with
/// data structure manipulation (string, attrs, list, etc. functions). /// data structure manipulation (string, attrs, list, etc. functions).
pub fn pure_builtins() -> Vec<(&'static str, Value)> { pub fn pure_builtins() -> Vec<(&'static str, Value)> {
let mut result = pure_builtins::builtins() let mut result = pure_builtins::builtins();
.into_iter()
.map(builtin_tuple)
.collect::<Vec<_>>();
// Pure-value builtins // Pure-value builtins
result.push(("nixVersion", Value::String("2.3-compat-tvix-0.1".into()))); result.push(("nixVersion", Value::String("2.3-compat-tvix-0.1".into())));
@ -1038,7 +1031,4 @@ mod placeholder_builtins {
pub fn placeholders() -> Vec<(&'static str, Value)> { pub fn placeholders() -> Vec<(&'static str, Value)> {
placeholder_builtins::builtins() placeholder_builtins::builtins()
.into_iter()
.map(builtin_tuple)
.collect()
} }

View file

@ -48,11 +48,7 @@ fn eval_test(code_path: &str, expect_success: bool) {
} }
let mut eval = crate::Evaluation::new_impure(&code, Some(code_path.into())); let mut eval = crate::Evaluation::new_impure(&code, Some(code_path.into()));
eval.builtins.extend( eval.builtins.extend(mock_builtins::builtins());
mock_builtins::builtins()
.into_iter()
.map(crate::builtins::builtin_tuple),
);
let result = eval.evaluate(); let result = eval.evaluate();