| @@ -10,10 +10,11 @@ expression: b.c |
| 10 | 10 | #include "tilrt.h" |
| 11 | 11 | #include "kitchen.h" |
| 12 | 12 | |
| 13 | | −/* Int to the C integer type T and back: a value out of range panics. */ |
| 13 | +/* Int to the C integer type T and back: a value out of range panics. An |
| 14 | + * Int may be big (ADR 0014): it is read with tl_int_to_i64 and made with |
| 15 | + * tl_int_from_i64. */ |
| 14 | 16 | #define TILCAYO_FITS(T, x) ((int64_t)(T)(x) == (x) && ((T)(x) < 0) == ((x) < 0)) |
| 15 | | −#define TILCAYO_INT_OK(x) \ |
| 16 | | − ((x) >= 0 ? (uint64_t)(x) <= (uint64_t)TL_INT_MAX : (int64_t)(x) >= TL_INT_MIN) |
| 17 | +#define TILCAYO_I64_OK(x) ((x) < 0 || (uint64_t)(x) <= (uint64_t)INT64_MAX) |
| 17 | 18 | |
| 18 | 19 | static void tilcayo_finalize_KMap(void *p) { k_map_free(p); } |
| 19 | 20 | |
| @@ -22,8 +23,8 @@ static void tilcayo_trampoline_0(const char *a0, int64_t a1, void *env) { |
| 22 | 23 | const tl_callback *cb = env; |
| 23 | 24 | if (tl_callback_failed(cb)) return; |
| 24 | 25 | if (!a0) { tl_panic_str(cb->ctx, "a callback argument is NULL"); return; } |
| 25 | | − if (!TILCAYO_INT_OK(a1)) { tl_panic_str(cb->ctx, "a callback argument is out of Int's range"); return; } |
| 26 | | − tl_value args[] = {tl_string_cstr(cb->ctx, a0), tl_int((int64_t)a1)}; |
| 26 | + if (!TILCAYO_I64_OK(a1)) { tl_panic_str(cb->ctx, "a callback argument is out of Int's range"); return; } |
| 27 | + tl_value args[] = {tl_string_cstr(cb->ctx, a0), tl_int_from_i64(cb->ctx, (int64_t)a1)}; |
| 27 | 28 | tl_value r = tl_callback_call(cb, 2, args); |
| 28 | 29 | tl_drop(cb->ctx, r); |
| 29 | 30 | } |
| @@ -42,28 +43,28 @@ static double tilcayo_trampoline_1(void *env, double a1, bool a2) { |
| 42 | 43 | |
| 43 | 44 | /* int k_add(int a, int b) */ |
| 44 | 45 | tl_value tilcayo_k_add(tl_ctx *ctx, tl_value p0, tl_value p1) { |
| 45 | | − if (!TILCAYO_FITS(int, tl_int_get(p0))) return tl_panic_str(ctx, "k_add: `a` is out of range for int"); |
| 46 | | − if (!TILCAYO_FITS(int, tl_int_get(p1))) return tl_panic_str(ctx, "k_add: `b` is out of range for int"); |
| 47 | | − int r = k_add((int)tl_int_get(p0), (int)tl_int_get(p1)); |
| 48 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_add: the result is out of Int's range"); |
| 49 | | − return tl_int((int64_t)r); |
| 46 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(int, vp0)) return tl_panic_str(ctx, "k_add: `a` is out of range for int"); |
| 47 | + int64_t vp1; if (!tl_int_to_i64(p1, &vp1) || !TILCAYO_FITS(int, vp1)) return tl_panic_str(ctx, "k_add: `b` is out of range for int"); |
| 48 | + int r = k_add((int)vp0, (int)vp1); |
| 49 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_add: the result is out of Int's range"); |
| 50 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 50 | 51 | } |
| 51 | 52 | |
| 52 | 53 | /* unsigned long k_hash(uint64_t seed, short salt) */ |
| 53 | 54 | tl_value tilcayo_k_hash(tl_ctx *ctx, tl_value p0, tl_value p1) { |
| 54 | | − if (!TILCAYO_FITS(uint64_t, tl_int_get(p0))) return tl_panic_str(ctx, "k_hash: `seed` is out of range for uint64_t"); |
| 55 | | − if (!TILCAYO_FITS(short, tl_int_get(p1))) return tl_panic_str(ctx, "k_hash: `salt` is out of range for short"); |
| 56 | | − unsigned long r = k_hash((uint64_t)tl_int_get(p0), (short)tl_int_get(p1)); |
| 57 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_hash: the result is out of Int's range"); |
| 58 | | − return tl_int((int64_t)r); |
| 55 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(uint64_t, vp0)) return tl_panic_str(ctx, "k_hash: `seed` is out of range for uint64_t"); |
| 56 | + int64_t vp1; if (!tl_int_to_i64(p1, &vp1) || !TILCAYO_FITS(short, vp1)) return tl_panic_str(ctx, "k_hash: `salt` is out of range for short"); |
| 57 | + unsigned long r = k_hash((uint64_t)vp0, (short)vp1); |
| 58 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_hash: the result is out of Int's range"); |
| 59 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | 62 | /* long long k_wide(long long x) */ |
| 62 | 63 | tl_value tilcayo_k_wide(tl_ctx *ctx, tl_value p0) { |
| 63 | | − if (!TILCAYO_FITS(long long, tl_int_get(p0))) return tl_panic_str(ctx, "k_wide: `x` is out of range for long long"); |
| 64 | | − long long r = k_wide((long long)tl_int_get(p0)); |
| 65 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_wide: the result is out of Int's range"); |
| 66 | | − return tl_int((int64_t)r); |
| 64 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(long long, vp0)) return tl_panic_str(ctx, "k_wide: `x` is out of range for long long"); |
| 65 | + long long r = k_wide((long long)vp0); |
| 66 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_wide: the result is out of Int's range"); |
| 67 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 67 | 68 | } |
| 68 | 69 | |
| 69 | 70 | /* double k_scale(double x, float by) */ |
| @@ -74,8 +75,8 @@ tl_value tilcayo_k_scale(tl_ctx *ctx, tl_value p0, tl_value p1) { |
| 74 | 75 | |
| 75 | 76 | /* bool k_is_even(int64_t n) */ |
| 76 | 77 | tl_value tilcayo_k_is_even(tl_ctx *ctx, tl_value p0) { |
| 77 | | − if (!TILCAYO_FITS(int64_t, tl_int_get(p0))) return tl_panic_str(ctx, "k_is_even: `n` is out of range for int64_t"); |
| 78 | | − bool r = k_is_even((int64_t)tl_int_get(p0)); |
| 78 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(int64_t, vp0)) return tl_panic_str(ctx, "k_is_even: `n` is out of range for int64_t"); |
| 79 | + bool r = k_is_even((int64_t)vp0); |
| 79 | 80 | return tl_bool(r); |
| 80 | 81 | } |
| 81 | 82 | |
| @@ -89,38 +90,38 @@ tl_value tilcayo_k_reset(tl_ctx *ctx) { |
| 89 | 90 | /* k_code k_last_error(void) */ |
| 90 | 91 | tl_value tilcayo_k_last_error(tl_ctx *ctx) { |
| 91 | 92 | k_code r = k_last_error(); |
| 92 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_last_error: the result is out of Int's range"); |
| 93 | | − return tl_int((int64_t)r); |
| 93 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_last_error: the result is out of Int's range"); |
| 94 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | /* size_t k_count(const char *text, size_t text_len, char c) */ |
| 97 | 98 | tl_value tilcayo_k_count(tl_ctx *ctx, tl_value p0, tl_value p1) { |
| 98 | 99 | if (!TILCAYO_FITS(size_t, (int64_t)tl_string_len(p0))) return tl_panic_str(ctx, "k_count: `text` is too long"); |
| 99 | | − if (!TILCAYO_FITS(char, tl_int_get(p1))) return tl_panic_str(ctx, "k_count: `c` is out of range for char"); |
| 100 | | − size_t r = k_count(tl_string_ptr(p0), (size_t)tl_string_len(p0), (char)tl_int_get(p1)); |
| 101 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_count: the result is out of Int's range"); |
| 102 | | − return tl_int((int64_t)r); |
| 100 | + int64_t vp1; if (!tl_int_to_i64(p1, &vp1) || !TILCAYO_FITS(char, vp1)) return tl_panic_str(ctx, "k_count: `c` is out of range for char"); |
| 101 | + size_t r = k_count(tl_string_ptr(p0), (size_t)tl_string_len(p0), (char)vp1); |
| 102 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_count: the result is out of Int's range"); |
| 103 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 103 | 104 | } |
| 104 | 105 | |
| 105 | 106 | /* int k_parse(const char *s) */ |
| 106 | 107 | tl_value tilcayo_k_parse(tl_ctx *ctx, tl_value p0) { |
| 107 | 108 | int r = k_parse(tl_string_ptr(p0)); |
| 108 | | − if (!TILCAYO_INT_OK(r)) return tl_panic_str(ctx, "k_parse: the result is out of Int's range"); |
| 109 | | − return tl_int((int64_t)r); |
| 109 | + if (!TILCAYO_I64_OK(r)) return tl_panic_str(ctx, "k_parse: the result is out of Int's range"); |
| 110 | + return tl_int_from_i64(ctx, (int64_t)r); |
| 110 | 111 | } |
| 111 | 112 | |
| 112 | 113 | /* const char *k_name(int code) */ |
| 113 | 114 | tl_value tilcayo_k_name(tl_ctx *ctx, tl_value p0) { |
| 114 | | − if (!TILCAYO_FITS(int, tl_int_get(p0))) return tl_panic_str(ctx, "k_name: `code` is out of range for int"); |
| 115 | | − const char *r = k_name((int)tl_int_get(p0)); |
| 115 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(int, vp0)) return tl_panic_str(ctx, "k_name: `code` is out of range for int"); |
| 116 | + const char *r = k_name((int)vp0); |
| 116 | 117 | if (!r) return tl_panic_str(ctx, "k_name returned NULL"); |
| 117 | 118 | return tl_string_cstr(ctx, r); |
| 118 | 119 | } |
| 119 | 120 | |
| 120 | 121 | /* k_map *k_map_new(size_t capacity) */ |
| 121 | 122 | tl_value tilcayo_k_map_new(tl_ctx *ctx, tl_value p0) { |
| 122 | | − if (!TILCAYO_FITS(size_t, tl_int_get(p0))) return tl_panic_str(ctx, "k_map_new: `capacity` is out of range for size_t"); |
| 123 | | − k_map *r = k_map_new((size_t)tl_int_get(p0)); |
| 123 | + int64_t vp0; if (!tl_int_to_i64(p0, &vp0) || !TILCAYO_FITS(size_t, vp0)) return tl_panic_str(ctx, "k_map_new: `capacity` is out of range for size_t"); |
| 124 | + k_map *r = k_map_new((size_t)vp0); |
| 124 | 125 | if (!r) return tl_panic_str(ctx, "k_map_new returned NULL"); |
| 125 | 126 | return tl_resource_new(ctx, (void *)r, tilcayo_finalize_KMap, false); |
| 126 | 127 | } |
| @@ -134,8 +135,8 @@ tl_value tilcayo_k_map_copy(tl_ctx *ctx, tl_value p0) { |
| 134 | 135 | |
| 135 | 136 | /* bool k_map_put(k_map *m, const char *key, int64_t value) */ |
| 136 | 137 | tl_value tilcayo_k_map_put(tl_ctx *ctx, tl_value p0, tl_value p1, tl_value p2) { |
| 137 | | − if (!TILCAYO_FITS(int64_t, tl_int_get(p2))) return tl_panic_str(ctx, "k_map_put: `value` is out of range for int64_t"); |
| 138 | | − bool r = k_map_put(tl_resource_get(p0), tl_string_ptr(p1), (int64_t)tl_int_get(p2)); |
| 138 | + int64_t vp2; if (!tl_int_to_i64(p2, &vp2) || !TILCAYO_FITS(int64_t, vp2)) return tl_panic_str(ctx, "k_map_put: `value` is out of range for int64_t"); |
| 139 | + bool r = k_map_put(tl_resource_get(p0), tl_string_ptr(p1), (int64_t)vp2); |
| 139 | 140 | return tl_bool(r); |
| 140 | 141 | } |
| 141 | 142 | |