diff options
Diffstat (limited to '')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 3 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.nix | 5 | ||||
-rw-r--r-- | tvix/eval/src/value/list.rs | 4 | ||||
-rw-r--r-- | tvix/eval/src/value/mod.rs | 11 |
5 files changed, 24 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 1a0443cb6012..3c32129e0537 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -45,6 +45,9 @@ fn pure_builtins() -> Vec<Builtin> { let a = args.pop().unwrap(); Ok(arithmetic_op!(a, b, /)) }), + Builtin::new("length", 1, |args| { + Ok(Value::Integer(args[0].as_list()?.len() as i64)) + }), Builtin::new("isAttrs", 1, |args| { Ok(Value::Bool(matches!(args[0], Value::Attrs(_)))) }), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.exp new file mode 100644 index 000000000000..e80eb6ef1465 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.exp @@ -0,0 +1 @@ +[ 0 1 3 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.nix new file mode 100644 index 000000000000..ef1f638cbbdc --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-length.nix @@ -0,0 +1,5 @@ +[ + (builtins.length []) + (builtins.length [ 1 ]) + (builtins.length [ "one" "two" "three" ]) +] diff --git a/tvix/eval/src/value/list.rs b/tvix/eval/src/value/list.rs index b8682a5b3d5a..da86fdda26ec 100644 --- a/tvix/eval/src/value/list.rs +++ b/tvix/eval/src/value/list.rs @@ -28,6 +28,10 @@ impl NixList { lhs } + pub fn len(&self) -> usize { + self.0.len() + } + pub fn construct(count: usize, stack_slice: Vec<Value>) -> Self { debug_assert!( count == stack_slice.len(), diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index c5565e6176b3..a8bfc164cdf9 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -103,6 +103,17 @@ impl Value { } } + pub fn as_list(&self) -> EvalResult<&NixList> { + match self { + Value::List(xs) => Ok(xs), + other => Err(ErrorKind::TypeError { + expected: "list", + actual: other.type_of(), + } + .into()), + } + } + pub fn to_string(self) -> EvalResult<NixString> { match self { Value::String(s) => Ok(s), |