summaryrefslogtreecommitdiffhomepage
path: root/types.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-07-27 14:17:55 +0200
committerJo-Philipp Wich <jo@mein.io>2021-07-27 14:22:46 +0200
commit3315b1f31831a82176885861f78190c15c803b4b (patch)
tree4cf80649d09e1059c149d2c2cf466b66d0ca1f6c /types.c
parente4871c661f0bfb979f1b235d7b6e59b70ed1aca6 (diff)
types: allow negative array indexes
Support array[-1], array[-2] etc. to retrieve elements relative to the end of the array values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'types.c')
-rw-r--r--types.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/types.c b/types.c
index 82c93cb..06f1957 100644
--- a/types.c
+++ b/types.c
@@ -1980,6 +1980,9 @@ ucv_key_get(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key)
if (ucv_type(scope) == UC_ARRAY) {
idx = ucv_key_to_index(key);
+ if (idx < 0 && idx >= -INT64_MAX && llabs(idx) <= ucv_array_length(scope))
+ idx += ucv_array_length(scope);
+
if (idx >= 0 && (uint64_t)idx < ucv_array_length(scope))
return ucv_get(ucv_array_get(scope, idx));
}