aboutsummaryrefslogtreecommitdiff
path: root/builtins/array.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-18 14:53:52 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-18 14:53:52 -0500
commit69d41fa068a08fcb468bd50babc43178faf0e114 (patch)
tree03febfb2e86eb81ada90a03648a8bfd5a18dc920 /builtins/array.h
parentf77a45095a76b99c9d470bc861bd69f5cb5b8742 (diff)
Add source information to runtime errors
Diffstat (limited to 'builtins/array.h')
-rw-r--r--builtins/array.h11
1 files changed, 6 insertions, 5 deletions
diff --git a/builtins/array.h b/builtins/array.h
index 84edcb41..65b7028c 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -8,11 +8,12 @@
#include "types.h"
// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1
-#define $Array_get(type, x, i) ({ const array_t *$arr = x; int64_t $index = (int64_t)(i); \
- int64_t $off = $index + ($index < 0) * ($arr->length + 1) - 1; \
- if (__builtin_expect($off < 0 || $off >= $arr->length, 0)) \
- fail("Invalid array index: %ld (array has length %ld)\n", $index, $arr->length); \
- *(type*)($arr->data + $arr->stride * $off);})
+#define $Array_get(type, x, i, filename, start, end) ({ \
+ const array_t *$arr = x; int64_t $index = (int64_t)(i); \
+ int64_t $off = $index + ($index < 0) * ($arr->length + 1) - 1; \
+ if (__builtin_expect($off < 0 || $off >= $arr->length, 0)) \
+ fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int64__as_str(&$index, USE_COLOR, NULL), $arr->length); \
+ *(type*)($arr->data + $arr->stride * $off);})
#define $Array_get_unchecked(type, x, i) ({ const array_t *$arr = x; int64_t $index = (int64_t)(i); \
int64_t $off = $index + ($index < 0) * ($arr->length + 1) - 1; \
*(type*)($arr->data + $arr->stride * $off);})