From 7b3bda9e089fe9de2d28a28f64ef8532abb3ac9c Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 23 Oct 2022 12:28:23 -0400 Subject: feat(tvix/eval): Implement builtins.mapAttrs I played around a little bit with doing this in-place, but ended up going with this perhaps slightly clone-heavy approach for now because ideally most clones on Value are cheap - but later we should benchmark alternate approaches that get to reuse allocations better if necessary or possible. Change-Id: If998eb2056cedefdf2fb480b0568ac8329ccfc44 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7068 Autosubmit: grfn Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 13 +++++++++++++ tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.exp | 1 + tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.nix | 3 +++ .../tests/nix_tests/notyetpassing/eval-okay-mapattrs.exp | 1 - .../tests/nix_tests/notyetpassing/eval-okay-mapattrs.nix | 3 --- tvix/eval/src/value/attrs.rs | 10 ++++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.exp create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.nix delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.exp delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.nix (limited to 'tvix/eval') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index d6b52e20a2a3..e7eb4e8bf953 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -435,6 +435,19 @@ fn pure_builtins() -> Vec { .map(|list| Value::List(NixList::from(list))) .map_err(Into::into) }), + Builtin::new( + "mapAttrs", + &[true, true], + |args: Vec, vm: &mut VM| { + let attrs = args[1].to_attrs()?; + let mut res = BTreeMap::new(); + for (key, value) in attrs.as_ref() { + let value = vm.call_with(&args[0], [key.clone().into(), value.clone()])?; + res.insert(key.clone(), value); + } + Ok(Value::attrs(NixAttrs::from_map(res))) + }, + ), Builtin::new( "match", &[true, true], diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.exp b/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.exp new file mode 100644 index 000000000000..3f113f17bab1 --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.exp @@ -0,0 +1 @@ +{ x = "x-foo"; y = "y-bar"; } diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.nix b/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.nix new file mode 100644 index 000000000000..f075b6275e5a --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-mapattrs.nix @@ -0,0 +1,3 @@ +with import ./lib.nix; + +builtins.mapAttrs (name: value: name + "-" + value) { x = "foo"; y = "bar"; } diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.exp b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.exp deleted file mode 100644 index 3f113f17bab1..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.exp +++ /dev/null @@ -1 +0,0 @@ -{ x = "x-foo"; y = "y-bar"; } diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.nix b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.nix deleted file mode 100644 index f075b6275e5a..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-mapattrs.nix +++ /dev/null @@ -1,3 +0,0 @@ -with import ./lib.nix; - -builtins.mapAttrs (name: value: name + "-" + value) { x = "foo"; y = "bar"; } diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index 17c7b422cb0d..c0f82b921449 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -517,3 +517,13 @@ impl<'a> Iterator for Keys<'a> { } } } + +impl<'a> IntoIterator for &'a NixAttrs { + type Item = (&'a NixString, &'a Value); + + type IntoIter = Iter>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} -- cgit 1.4.1