feat(tvix/eval): implement builtins.all

Change-Id: I19ec2b2194681efd73041f4aa1e5f2c893e839c2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6844
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Vincent Ambo 2022-10-02 19:59:37 +03:00 committed by tazjin
parent 939b2194fe
commit 844d288949
3 changed files with 30 additions and 0 deletions

View file

@ -61,6 +61,20 @@ fn pure_builtins() -> Vec<Builtin> {
Builtin::new("abort", &[true], |args, _| { Builtin::new("abort", &[true], |args, _| {
Err(ErrorKind::Abort(args[0].to_str()?.to_string())) Err(ErrorKind::Abort(args[0].to_str()?.to_string()))
}), }),
Builtin::new("all", &[true, true], |args, vm| {
for value in args[1].to_list()?.into_iter() {
let pred_result = {
vm.push(value);
vm.call_value(&args[0])
}?;
if !pred_result.force(vm)?.as_bool()? {
return Ok(Value::Bool(false));
}
}
Ok(Value::Bool(true))
}),
Builtin::new("attrNames", &[true], |args, _| { Builtin::new("attrNames", &[true], |args, _| {
let xs = args[0].to_attrs()?; let xs = args[0].to_attrs()?;
let mut output = Vec::with_capacity(xs.len()); let mut output = Vec::with_capacity(xs.len());

View file

@ -0,0 +1 @@
[ true true false false false false true true false ]

View file

@ -0,0 +1,15 @@
[
(builtins.all (x: x) [ ])
(builtins.all (x: x) [ true true true ])
(builtins.all (x: x) [ false false false ])
(builtins.all (x: x) [ true true false ])
(builtins.all (x: x) [ false true true ])
# evaluation should short-circuit
(builtins.all (x: x) [ true false (builtins.abort "should be unreachable") ])
# arbitrary functions supported
(builtins.all (x: x * 2 == 42) [ ])
(builtins.all (x: x * 2 == 42) [ 21 21 21 ])
(builtins.all (x: x * 2 == 42) [ 1 2 3 ])
]