diff options
Diffstat (limited to 'src/stdlib')
| -rw-r--r-- | src/stdlib/lists.h | 10 | ||||
| -rw-r--r-- | src/stdlib/tables.h | 12 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/stdlib/lists.h b/src/stdlib/lists.h index 74314aa6..1386b2fa 100644 --- a/src/stdlib/lists.h +++ b/src/stdlib/lists.h @@ -10,6 +10,16 @@ #include "util.h" // Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 +#define List_get_checked(list_expr, index_expr, item_type, start, end) \ + ({ \ + const List_t list = list_expr; \ + int64_t index = index_expr; \ + int64_t off = index + (index < 0) * (list.length + 1) - 1; \ + if (unlikely(off < 0 || off >= list.length)) \ + fail_source(__SOURCE_FILE__, start, end, "Invalid list index: ", index, " (list has length ", \ + (int64_t)list.length, ")\n"); \ + *(item_type *)(list.data + list.stride * off); \ + }) #define List_get(list_expr, index_expr, item_type, var, optional_expr, none_expr) \ ({ \ const List_t list = list_expr; \ diff --git a/src/stdlib/tables.h b/src/stdlib/tables.h index 208bf6b2..129aa5ec 100644 --- a/src/stdlib/tables.h +++ b/src/stdlib/tables.h @@ -49,6 +49,18 @@ void *Table$get(Table_t t, const void *key, const TypeInfo_t *type); val_t *nonnull_var = Table$get(t, &k, info_expr); \ nonnull_var ? nonnull_expr : null_expr; \ }) +#define Table$get_checked(table_expr, key_t, val_t, key_expr, start, end, info_expr) \ + ({ \ + const Table_t t = table_expr; \ + const key_t key = key_expr; \ + const TypeInfo_t *info = info_expr; \ + val_t *value = Table$get(t, &key, info); \ + if (unlikely(value == NULL)) \ + fail_source(__SOURCE_FILE__, start, end, \ + "This key was not found in the table: ", generic_as_text(&key, false, info->TableInfo.key), \ + "\n"); \ + *value; \ + }) #define Table$get_or_setdefault(table_expr, key_t, val_t, key_expr, default_expr, info_expr) \ ({ \ Table_t *t = table_expr; \ |
