feat(tvix/eval): Support builtins.bitAnd

Bitwise `and` on integers.

Change-Id: I9f2a9182a057af26906683acd97a40dfabbdded8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6548
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
William Carroll 2022-09-05 22:16:17 -07:00 committed by clbot
parent 5a6d9ee1a0
commit f5f6bd319d
4 changed files with 19 additions and 0 deletions

View file

@ -101,6 +101,13 @@ fn pure_builtins() -> Vec<Builtin> {
Ok(Value::List(NixList::construct(output.len(), output))) Ok(Value::List(NixList::construct(output.len(), output)))
}) })
}), }),
Builtin::new("bitAnd", 2, |args, vm| {
force!(vm, &args[0], x, {
force!(vm, &args[1], y, {
Ok(Value::Integer(x.as_int()? & y.as_int()?))
})
})
}),
Builtin::new("catAttrs", 2, |mut args, _| { Builtin::new("catAttrs", 2, |mut args, _| {
let list = args.pop().unwrap().to_list()?; let list = args.pop().unwrap().to_list()?;
let key = args.pop().unwrap().to_str()?; let key = args.pop().unwrap().to_str()?;

View file

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

View file

@ -0,0 +1,10 @@
[
(builtins.bitAnd 0 0)
(builtins.bitAnd 0 1)
(builtins.bitAnd 1 0)
(builtins.bitAnd 1 1)
(builtins.bitAnd 8 8)
(builtins.bitAnd 8 (builtins.add 4 4))
(builtins.bitAnd (builtins.add 4 4) 8)
(builtins.bitAnd (builtins.add 4 4) (builtins.add 4 4))
]

View file

@ -246,6 +246,7 @@ impl Value {
} }
gen_cast!(as_bool, bool, "bool", Value::Bool(b), *b); gen_cast!(as_bool, bool, "bool", Value::Bool(b), *b);
gen_cast!(as_int, i64, "int", Value::Integer(x), *x);
gen_cast!(to_str, NixString, "string", Value::String(s), s.clone()); 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_attrs, Rc<NixAttrs>, "set", Value::Attrs(a), a.clone());
gen_cast!(to_list, NixList, "list", Value::List(l), l.clone()); gen_cast!(to_list, NixList, "list", Value::List(l), l.clone());