feat(tvix/eval): add builtins.{floor,ceil}

Change-Id: I4e6c4f96f6f5097a5c637eb3dbbd7bb8b34b7d52
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7032
Autosubmit: j4m3s <james.landrein@gmail.com>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
James Landrein 2022-10-17 23:37:47 +02:00 committed by j4m3s
parent bd0bf6ea7d
commit 8425c2016c
7 changed files with 13 additions and 0 deletions

View file

@ -134,6 +134,9 @@ fn pure_builtins() -> Vec<Builtin> {
Ok(Value::List(NixList::construct(output.len(), output)))
},
),
Builtin::new("ceil", &[true], |args: Vec<Value>, _: &mut VM| {
Ok(Value::Integer(args[0].as_float()?.ceil() as i64))
}),
Builtin::new(
"compareVersions",
&[true, true],
@ -269,6 +272,9 @@ fn pure_builtins() -> Vec<Builtin> {
.map(|list| Value::List(NixList::from(list)))
.map_err(Into::into)
}),
Builtin::new("floor", &[true], |args: Vec<Value>, _: &mut VM| {
Ok(Value::Integer(args[0].as_float()?.floor() as i64))
}),
Builtin::new(
"foldl'",
&[true, false, true],

View file

@ -0,0 +1 @@
[ 4 ]

View file

@ -0,0 +1 @@
[ (builtins.ceil 3.4) ]

View file

@ -0,0 +1 @@
[ 3 ]

View file

@ -0,0 +1 @@
[ (builtins.floor 3.4) ]

View file

@ -293,6 +293,7 @@ impl Value {
gen_cast!(as_bool, bool, "bool", Value::Bool(b), *b);
gen_cast!(as_int, i64, "int", Value::Integer(x), *x);
gen_cast!(as_float, f64, "float", Value::Float(x), *x);
gen_cast!(to_str, NixString, "string", Value::String(s), s.clone());
gen_cast!(to_attrs, Rc<NixAttrs>, "set", Value::Attrs(a), a.clone());
gen_cast!(to_list, NixList, "list", Value::List(l), l.clone());

View file

@ -46,7 +46,9 @@ let
# Ordering change after 2.3
"eval-okay-xml.nix" = [ nix ];
# Missing builtins in Nix 2.3
"eval-okay-ceil.nix" = [ nix ];
"eval-okay-floor-ceil.nix" = [ nix ];
"eval-okay-floor.nix" = [ nix ];
"eval-okay-groupBy.nix" = [ nix ];
"eval-okay-zipAttrsWith.nix" = [ nix ];
# Comparable lists are not in Nix 2.3