about summary refs log tree commit diff
path: root/tvix/eval/src/builtins/mod.rs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-09-06T18·58-0700
committerclbot <clbot@tvl.fyi>2022-09-17T12·54+0000
commit3a67a140093335e93cee560c1d2f9873035ffa95 (patch)
treeb8f8bf10d78aaf6fd1c45a0c2b368c6d87c75290 /tvix/eval/src/builtins/mod.rs
parent53fbc75df9ee4aa06eccc9f12bfe9cab8eab5755 (diff)
feat(tvix/eval): Support builtins.elemAt r/4882
(Attempt to) index into a list.

Change-Id: I3592e60a79e64d265e34100d4062041b0b410e00
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6551
Reviewed-by: wpcarro <wpcarro@gmail.com>
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins/mod.rs')
-rw-r--r--tvix/eval/src/builtins/mod.rs14
1 files changed, 14 insertions, 0 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)?;