From 0e97555644f54bf6991b5d44ff297b6343dbc7e0 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sat, 8 Oct 2022 15:20:27 -0400 Subject: feat(tvix/eval): Implement builtins.genList Change-Id: Iabe28656229f508226b244d81382e517961eb3cf Reviewed-on: https://cl.tvl.fyi/c/depot/+/6901 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/builtins/mod.rs | 11 +++++++++++ tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix | 1 + tvix/eval/src/value/mod.rs | 6 ++++++ 4 files changed, 19 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index e047ef599b2d..93b04bb37b44 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -222,6 +222,17 @@ fn pure_builtins() -> Vec { .map(|list| Value::List(NixList::from(list))) .map_err(Into::into) }), + Builtin::new("genList", &[true, true], |args: Vec, vm: &mut VM| { + let len = args[1].as_int()?; + (0..len) + .map(|i| { + vm.push(i.into()); + vm.call_value(&args[0]) + }) + .collect::, _>>() + .map(|list| Value::List(NixList::from(list))) + .map_err(Into::into) + }), Builtin::new("getAttr", &[true, true], |args: Vec, _: &mut VM| { let k = args[0].to_str()?; let xs = args[1].to_attrs()?; diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp new file mode 100644 index 000000000000..cd4ca34f14df --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp @@ -0,0 +1 @@ +[ 0 1 4 9 16 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix new file mode 100644 index 000000000000..2c4dfba20324 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix @@ -0,0 +1 @@ +builtins.genList (x: x * x) 5 diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index dd61ecf0bbea..ea9e173ebc40 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -359,6 +359,12 @@ impl Display for Value { } } +impl From for Value { + fn from(i: i64) -> Self { + Self::Integer(i) + } +} + fn type_error(expected: &'static str, actual: &Value) -> ErrorKind { ErrorKind::TypeError { expected, -- cgit 1.4.1