diff options
Diffstat (limited to 'tvix/eval')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 14 | ||||
-rw-r--r-- | tvix/eval/src/errors.rs | 4 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.nix | 5 |
4 files changed, 22 insertions, 2 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 149669015b6a..3f8b73f7d7fd 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -140,6 +140,20 @@ fn pure_builtins() -> Vec<Builtin> { let a = args.pop().unwrap(); arithmetic_op!(a, b, /) }), + Builtin::new("elemAt", 2, |args, vm| { + force!(vm, &args[0], value, { + let xs = value.to_list()?; + let i = args[1].as_int()?; + if i < 0 { + Err(ErrorKind::IndexOutOfBounds { index: i }) + } else { + match xs.get(i as usize) { + Some(x) => Ok(x.clone()), + None => Err(ErrorKind::IndexOutOfBounds { index: i }), + } + } + }) + }), Builtin::new("length", 1, |args, vm| { if let Value::Thunk(t) = &args[0] { t.force(vm)?; diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 23ac6abbf4de..d3d41cbbcd48 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -26,9 +26,9 @@ pub enum ErrorKind { name: String, }, - // Attempted to index into a list beyond its boundaries. + /// Attempted to index into a list beyond its boundaries. IndexOutOfBounds { - index: usize, + index: i64, }, TypeError { diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.exp new file mode 100644 index 000000000000..3701c9d75f94 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.exp @@ -0,0 +1 @@ +[ "foo" "bar" "baz" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.nix new file mode 100644 index 000000000000..762adeebbf0a --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-elemat.nix @@ -0,0 +1,5 @@ +[ + (builtins.elemAt [ "foo" "bar" "baz" ] 0) + (builtins.elemAt [ "foo" "bar" "baz" ] 1) + (builtins.elemAt [ "foo" "bar" "baz" ] 2) +] |