From 0537d88078bcc547e8459463c581dfdb4bef2f81 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 2 Oct 2022 20:02:05 +0300 Subject: feat(tvix/eval): implement `builtins.any` Change-Id: I640ee20e7c0a68c4e024a577e429fed9b3a49ece Reviewed-on: https://cl.tvl.fyi/c/depot/+/6845 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/builtins/mod.rs | 14 ++++++++++++++ tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.nix | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.nix (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index bdfa791574bb..cd4200a13d3b 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -75,6 +75,20 @@ fn pure_builtins() -> Vec { Ok(Value::Bool(true)) }), + Builtin::new("any", &[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(true)); + } + } + + Ok(Value::Bool(false)) + }), Builtin::new("attrNames", &[true], |args, _| { let xs = args[0].to_attrs()?; let mut output = Vec::with_capacity(xs.len()); diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.exp new file mode 100644 index 000000000000..d6846ac3f756 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.exp @@ -0,0 +1 @@ +[ false true false true true true false true false ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.nix new file mode 100644 index 000000000000..2c659f130b65 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-any.nix @@ -0,0 +1,15 @@ +[ + (builtins.any (x: x) [ ]) + (builtins.any (x: x) [ true true true ]) + (builtins.any (x: x) [ false false false ]) + (builtins.any (x: x) [ true true false ]) + (builtins.any (x: x) [ false true true ]) + + # evaluation should short-circuit + (builtins.any (x: x) [ false true (builtins.abort "should be unreachable") ]) + + # arbitrary functions supported + (builtins.any (x: x * 2 == 42) [ ]) + (builtins.any (x: x * 2 == 42) [ 7 21 42 ]) + (builtins.any (x: x * 2 == 42) [ 1 2 3 ]) +] -- cgit 1.4.1