From 3315b1f31831a82176885861f78190c15c803b4b Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 27 Jul 2021 14:17:55 +0200 Subject: 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 --- types.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'types.c') 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)); } -- cgit v1.2.3