From 3f4f71cf87a1f6a4df95ca626ed1eb6290778a4d Mon Sep 17 00:00:00 2001 From: GrayJack Date: Tue, 16 Apr 2024 00:51:01 -0300 Subject: [PATCH] refactor(c-api): Use `size_t` in other structures and functions used by lib --- src/boot/boot.c | 2 +- src/boot/number_test.c | 2 +- src/core/compile.c | 4 +-- src/core/ffi.c | 2 +- src/core/math.c | 4 +-- src/core/parse.c | 12 +++---- src/core/peg.c | 4 +-- src/core/pp.c | 12 +++---- src/core/run.c | 4 +-- src/core/specials.c | 18 +++++----- src/core/state.h | 4 +-- src/core/string.c | 20 +++++------ src/core/strtod.c | 10 +++--- src/core/symcache.c | 6 ++-- src/core/util.c | 22 ++++++------ src/core/util.h | 16 ++++----- src/core/value.c | 77 +++++++++++++++++++++++------------------- src/core/wrap.c | 12 +++---- src/include/janet.h | 43 ++++++++++++----------- 19 files changed, 143 insertions(+), 131 deletions(-) diff --git a/src/boot/boot.c b/src/boot/boot.c index 30255ac6..3aa204ef 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -104,7 +104,7 @@ int main(int argc, const char **argv) { } fclose(boot_file); - status = janet_dobytes(env, boot_buffer, (int32_t) boot_size, boot_filename, NULL); + status = janet_dobytes(env, boot_buffer, boot_size, boot_filename, NULL); janet_free(boot_buffer); /* Deinitialize vm */ diff --git a/src/boot/number_test.c b/src/boot/number_test.c index 77053704..53c8ad07 100644 --- a/src/boot/number_test.c +++ b/src/boot/number_test.c @@ -38,7 +38,7 @@ static void test_valid_str(const char *str) { double cnum, jnum; jnum = 0.0; cnum = atof(str); - err = janet_scan_number((const uint8_t *) str, (int32_t) strlen(str), &jnum); + err = janet_scan_number((const uint8_t *) str, strlen(str), &jnum); assert(!err); assert(cnum == jnum); } diff --git a/src/core/compile.c b/src/core/compile.c index 4f45ff1f..2d9a3ebb 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -435,9 +435,9 @@ JanetSlot *janetc_toslotskv(JanetCompiler *c, Janet ds) { JanetFopts subopts = janetc_fopts_default(c); subopts.flags |= JANET_FOPTS_ACCEPT_SPLICE; const JanetKV *kvs = NULL; - int32_t cap = 0, len = 0; + size_t cap = 0, len = 0; janet_dictionary_view(ds, &kvs, &len, &cap); - for (int32_t i = 0; i < cap; i++) { + for (size_t i = 0; i < cap; i++) { if (janet_checktype(kvs[i].key, JANET_NIL)) continue; janet_v_push(ret, janetc_value(subopts, kvs[i].key)); janet_v_push(ret, janetc_value(subopts, kvs[i].value)); diff --git a/src/core/ffi.c b/src/core/ffi.c index 03fccfd1..5078f011 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -432,7 +432,7 @@ static JanetFFIType decode_ffi_type(Janet x) { ret.st = janet_unwrap_abstract(x); return ret; } - int32_t len; + size_t len; const Janet *els; if (janet_indexed_view(x, &els, &len)) { if (janet_checktype(x, JANET_ARRAY)) { diff --git a/src/core/math.c b/src/core/math.c index da81d770..1afa67a0 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -81,9 +81,9 @@ void janet_rng_seed(JanetRNG *rng, uint32_t seed) { for (int i = 0; i < 16; i++) janet_rng_u32(rng); } -void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, int32_t len) { +void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, size_t len) { uint8_t state[16] = {0}; - for (int32_t i = 0; i < len; i++) + for (size_t i = 0; i < len; i++) state[i & 0xF] ^= bytes[i]; rng->a = state[0] + (state[1] << 8) + (state[2] << 16) + (state[3] << 24); rng->b = state[4] + (state[5] << 8) + (state[6] << 16) + (state[7] << 24); diff --git a/src/core/parse.c b/src/core/parse.c index 40ccfbf2..4819191a 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -59,11 +59,11 @@ int janet_is_symbol_char(uint8_t c) { /* Validate some utf8. Useful for identifiers. Only validates * the encoding, does not check for valid code points (they * are less well defined than the encoding). */ -int janet_valid_utf8(const uint8_t *str, int32_t len) { - int32_t i = 0; - int32_t j; +int janet_valid_utf8(const uint8_t *str, size_t len) { + size_t i = 0; + size_t j; while (i < len) { - int32_t nexti; + size_t nexti; uint8_t c = str[i]; /* Check the number of bytes in code point */ @@ -449,14 +449,14 @@ static int check_str_const(const char *cstr, const uint8_t *str, int32_t len) { static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) { Janet ret; double numval; - int32_t blen; + size_t blen; if (janet_is_symbol_char(c)) { push_buf(p, (uint8_t) c); if (c > 127) state->argn = 1; /* Use to indicate non ascii */ return 1; } /* Token finished */ - blen = (int32_t) p->bufcount; + blen = p->bufcount; int start_dig = p->buf[0] >= '0' && p->buf[0] <= '9'; int start_num = start_dig || p->buf[0] == '-' || p->buf[0] == '+' || p->buf[0] == '.'; if (p->buf[0] == ':') { diff --git a/src/core/peg.c b/src/core/peg.c index 35a36166..ff1227f0 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -423,10 +423,10 @@ tail: /* check number parsing */ double x = 0.0; int32_t base = (int32_t) rule[2]; - if (janet_scan_number_base(text, (int32_t)(result - text), base, &x)) return NULL; + if (janet_scan_number_base(text, (size_t)(result - text), base, &x)) return NULL; /* Specialized pushcap - avoid intermediate string creation */ if (!s->has_backref && s->mode == PEG_MODE_ACCUMULATE) { - janet_buffer_push_bytes(s->scratch, text, (int32_t)(result - text)); + janet_buffer_push_bytes(s->scratch, text, (size_t)(result - text)); } else { uint32_t tag = rule[3]; pushcap(s, janet_wrap_number(x), tag); diff --git a/src/core/pp.c b/src/core/pp.c index 89ecc141..c7302ac3 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -279,10 +279,10 @@ void janet_to_string_b(JanetBuffer *buffer, Janet x) { /* Check if a symbol or keyword contains no symbol characters */ static int contains_bad_chars(const uint8_t *sym, int issym) { - int32_t len = janet_string_length(sym); + size_t len = janet_string_length(sym); if (len && issym && sym[0] >= '0' && sym[0] <= '9') return 1; if (!janet_valid_utf8(sym, len)) return 1; - for (int32_t i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { if (!janet_is_symbol_char(sym[i])) return 1; } return 0; @@ -536,7 +536,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { } case JANET_ARRAY: case JANET_TUPLE: { - int32_t i = 0, len = 0; + size_t i = 0, len = 0; const Janet *arr = NULL; int isarray = janet_checktype(x, JANET_ARRAY); janet_indexed_view(x, &arr, &len); @@ -587,7 +587,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (NULL != proto) { Janet name = janet_table_get(proto, janet_ckeywordv("_name")); const uint8_t *n; - int32_t len; + size_t len; if (janet_bytes_view(name, &n, &len)) { if (S->flags & JANET_PRETTY_COLOR) { janet_buffer_push_cstring(S->buffer, janet_class_color); @@ -604,7 +604,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (NULL != proto) { Janet name = janet_struct_get(proto, janet_ckeywordv("_name")); const uint8_t *n; - int32_t len; + size_t len; if (janet_bytes_view(name, &n, &len)) { if (S->flags & JANET_PRETTY_COLOR) { janet_buffer_push_cstring(S->buffer, janet_class_color); @@ -623,7 +623,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (S->depth == 0) { janet_buffer_push_cstring(S->buffer, "..."); } else { - int32_t i = 0, len = 0, cap = 0; + size_t i = 0, len = 0, cap = 0; const JanetKV *kvs = NULL; janet_dictionary_view(x, &kvs, &len, &cap); if (!istable && !(S->flags & JANET_PRETTY_ONELINE) && len >= JANET_PRETTY_DICT_ONELINE) diff --git a/src/core/run.c b/src/core/run.c index c0698231..49dcdeff 100644 --- a/src/core/run.c +++ b/src/core/run.c @@ -27,7 +27,7 @@ #endif /* Run a string */ -int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out) { +int janet_dobytes(JanetTable *env, const uint8_t *bytes, size_t len, const char *sourcePath, Janet *out) { JanetParser parser; int errflags = 0, done = 0; int32_t index = 0; @@ -128,7 +128,7 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char } int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out) { - int32_t len = 0; + size_t len = 0; while (str[len]) ++len; return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out); } diff --git a/src/core/specials.c b/src/core/specials.c index bae6e4a2..59a0886a 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -105,7 +105,7 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x, int depth, int level) { case JANET_TABLE: case JANET_STRUCT: { const JanetKV *kv = NULL, *kvs = NULL; - int32_t len, cap = 0; + size_t len, cap = 0; janet_dictionary_view(x, &kvs, &len, &cap); while ((kv = janet_dictionary_next(kvs, cap, kv))) { JanetSlot key = quasiquote(subopts, kv->key, depth - 1, level); @@ -156,10 +156,10 @@ static int destructure(JanetCompiler *c, return leaf(c, janet_unwrap_symbol(left), right, attr); case JANET_TUPLE: case JANET_ARRAY: { - int32_t len = 0; + size_t len = 0; const Janet *values = NULL; janet_indexed_view(left, &values, &len); - for (int32_t i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { JanetSlot nextright = janetc_farslot(c); Janet subval = values[i]; @@ -170,11 +170,11 @@ static int destructure(JanetCompiler *c, } if (i + 2 < len) { - int32_t num_extra = len - i - 1; + size_t num_extra = len - i - 1; Janet *extra = janet_tuple_begin(num_extra); janet_tuple_flag(extra) |= JANET_TUPLE_FLAG_BRACKETCTOR; - for (int32_t j = 0; j < num_extra; ++j) { + for (size_t j = 0; j < num_extra; ++j) { extra[j] = values[j + i + 1]; } @@ -237,9 +237,9 @@ static int destructure(JanetCompiler *c, case JANET_TABLE: case JANET_STRUCT: { const JanetKV *kvs = NULL; - int32_t cap = 0, len = 0; + size_t cap = 0, len = 0; janet_dictionary_view(left, &kvs, &len, &cap); - for (int32_t i = 0; i < cap; i++) { + for (size_t i = 0; i < cap; i++) { if (janet_checktype(kvs[i].key, JANET_NIL)) continue; JanetSlot nextright = janetc_farslot(c); JanetSlot k = janetc_value(janetc_fopts_default(c), kvs[i].key); @@ -362,7 +362,7 @@ SlotHeadPair *dohead_destructure(JanetCompiler *c, SlotHeadPair *into, JanetFopt janet_indexed_view(lhs, &view_lhs.items, &view_lhs.len); janet_indexed_view(rhs, &view_rhs.items, &view_rhs.len); int found_amp = 0; - for (int32_t i = 0; i < view_lhs.len; i++) { + for (size_t i = 0; i < view_lhs.len; i++) { if (janet_symeq(view_lhs.items[i], "&")) { found_amp = 1; /* Good error will be generated later. */ @@ -370,7 +370,7 @@ SlotHeadPair *dohead_destructure(JanetCompiler *c, SlotHeadPair *into, JanetFopt } } if (!found_amp) { - for (int32_t i = 0; i < view_lhs.len; i++) { + for (size_t i = 0; i < view_lhs.len; i++) { Janet sub_rhs = view_rhs.len <= i ? janet_wrap_nil() : view_rhs.items[i]; into = dohead_destructure(c, into, subopts, view_lhs.items[i], sub_rhs); } diff --git a/src/core/state.h b/src/core/state.h index a272b2d2..abb96133 100644 --- a/src/core/state.h +++ b/src/core/state.h @@ -42,8 +42,8 @@ typedef struct JanetScratch { typedef struct { JanetGCObject *self; JanetGCObject *other; - int32_t index; - int32_t index2; + size_t index; + size_t index2; } JanetTraversalNode; typedef struct { diff --git a/src/core/string.c b/src/core/string.c index 4f2ecd77..33b2d1e1 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -94,8 +94,8 @@ const uint8_t *janet_cstring(const char *str) { struct kmp_state { int32_t i; int32_t j; - int32_t textlen; - int32_t patlen; + size_t textlen; + size_t patlen; int32_t *lookup; const uint8_t *text; const uint8_t *pat; @@ -103,8 +103,8 @@ struct kmp_state { static void kmp_init( struct kmp_state *s, - const uint8_t *text, int32_t textlen, - const uint8_t *pat, int32_t patlen) { + const uint8_t *text, size_t textlen, + const uint8_t *pat, size_t patlen) { if (patlen == 0) { janet_panic("expected non-empty pattern"); } @@ -142,8 +142,8 @@ static void kmp_seti(struct kmp_state *state, int32_t i) { static int32_t kmp_next(struct kmp_state *state) { int32_t i = state->i; int32_t j = state->j; - int32_t textlen = state->textlen; - int32_t patlen = state->patlen; + size_t textlen = state->textlen; + size_t patlen = state->patlen; const uint8_t *text = state->text; const uint8_t *pat = state->pat; int32_t *lookup = state->lookup; @@ -251,7 +251,7 @@ JANET_CORE_FN(cfun_string_asciilower, janet_fixarity(argc, 1); JanetByteView view = janet_getbytes(argv, 0); uint8_t *buf = janet_string_begin(view.len); - for (int32_t i = 0; i < view.len; i++) { + for (size_t i = 0; i < view.len; i++) { uint8_t c = view.bytes[i]; if (c >= 65 && c <= 90) { buf[i] = c + 32; @@ -568,21 +568,21 @@ JANET_CORE_FN(cfun_string_format, } static int trim_help_checkset(JanetByteView set, uint8_t x) { - for (int32_t j = 0; j < set.len; j++) + for (size_t j = 0; j < set.len; j++) if (set.bytes[j] == x) return 1; return 0; } static int32_t trim_help_leftedge(JanetByteView str, JanetByteView set) { - for (int32_t i = 0; i < str.len; i++) + for (size_t i = 0; i < str.len; i++) if (!trim_help_checkset(set, str.bytes[i])) return i; return str.len; } static int32_t trim_help_rightedge(JanetByteView str, JanetByteView set) { - for (int32_t i = str.len - 1; i >= 0; i--) + for (size_t i = str.len - 1; i >= 0; i--) if (!trim_help_checkset(set, str.bytes[i])) return i + 1; return 0; diff --git a/src/core/strtod.c b/src/core/strtod.c index 38cc442c..c41ba583 100644 --- a/src/core/strtod.c +++ b/src/core/strtod.c @@ -249,7 +249,7 @@ static double convert( * and integer, return 0. */ int janet_scan_number_base( const uint8_t *str, - int32_t len, + size_t len, int32_t base, double *out) { const uint8_t *end = str + len; @@ -385,7 +385,7 @@ error: int janet_scan_number( const uint8_t *str, - int32_t len, + size_t len, double *out) { return janet_scan_number_base(str, len, 0, out); } @@ -394,7 +394,7 @@ int janet_scan_number( static int scan_uint64( const uint8_t *str, - int32_t len, + size_t len, uint64_t *out, int *neg) { const uint8_t *end = str + len; @@ -457,7 +457,7 @@ static int scan_uint64( return 1; } -int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out) { +int janet_scan_int64(const uint8_t *str, size_t len, int64_t *out) { int neg; uint64_t bi; if (scan_uint64(str, len, &bi, &neg)) { @@ -477,7 +477,7 @@ int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out) { return 0; } -int janet_scan_uint64(const uint8_t *str, int32_t len, uint64_t *out) { +int janet_scan_uint64(const uint8_t *str, size_t len, uint64_t *out) { int neg; uint64_t bi; if (scan_uint64(str, len, &bi, &neg)) { diff --git a/src/core/symcache.c b/src/core/symcache.c index a45e3202..41ce036e 100644 --- a/src/core/symcache.c +++ b/src/core/symcache.c @@ -169,14 +169,14 @@ void janet_symbol_deinit(const uint8_t *sym) { } /* Create a symbol from a byte string */ -const uint8_t *janet_symbol(const uint8_t *str, int32_t len) { +const uint8_t *janet_symbol(const uint8_t *str, size_t len) { int32_t hash = janet_string_calchash(str, len); uint8_t *newstr; int success = 0; const uint8_t **bucket = janet_symcache_findmem(str, len, hash, &success); if (success) return *bucket; - JanetStringHead *head = janet_gcalloc(JANET_MEMORY_SYMBOL, sizeof(JanetStringHead) + (size_t) len + 1); + JanetStringHead *head = janet_gcalloc(JANET_MEMORY_SYMBOL, sizeof(JanetStringHead) + len + 1); head->hash = hash; head->length = len; newstr = (uint8_t *)(head->data); @@ -188,7 +188,7 @@ const uint8_t *janet_symbol(const uint8_t *str, int32_t len) { /* Get a symbol from a cstring */ const uint8_t *janet_csymbol(const char *cstr) { - return janet_symbol((const uint8_t *)cstr, (int32_t) strlen(cstr)); + return janet_symbol((const uint8_t *)cstr, strlen(cstr)); } /* Increment the gensym buffer */ diff --git a/src/core/util.c b/src/core/util.c index 94bf864a..7c4d8bea 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -117,7 +117,7 @@ const char *const janet_status_names[16] = { #ifndef JANET_PRF -int32_t janet_string_calchash(const uint8_t *str, int32_t len) { +int32_t janet_string_calchash(const uint8_t *str, size_t len) { if (NULL == str) return 5381; const uint8_t *end = str + len; uint32_t hash = 5381; @@ -230,7 +230,7 @@ void janet_init_hash_key(uint8_t new_key[JANET_HASH_KEY_SIZE]) { /* Calculate hash for string */ -int32_t janet_string_calchash(const uint8_t *str, int32_t len) { +int32_t janet_string_calchash(const uint8_t *str, size_t len) { uint32_t hash; hash = halfsiphash(str, len, hash_key); return (int32_t)hash; @@ -244,7 +244,7 @@ uint32_t janet_hash_mix(uint32_t input, uint32_t more) { } /* Computes hash of an array of values */ -int32_t janet_array_calchash(const Janet *array, int32_t len) { +int32_t janet_array_calchash(const Janet *array, size_t len) { const Janet *end = array + len; uint32_t hash = 33; while (array < end) { @@ -254,7 +254,7 @@ int32_t janet_array_calchash(const Janet *array, int32_t len) { } /* Computes hash of an array of values */ -int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len) { +int32_t janet_kv_calchash(const JanetKV *kvs, size_t len) { const JanetKV *end = kvs + len; uint32_t hash = 33; while (kvs < end) { @@ -284,9 +284,9 @@ void safe_memcpy(void *dest, const void *src, size_t len) { /* Helper to find a value in a Janet struct or table. Returns the bucket * containing the key, or the first empty bucket if there is no such key. */ -const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key) { +const JanetKV *janet_dict_find(const JanetKV *buckets, size_t cap, Janet key) { int32_t index = janet_maphash(cap, janet_hash(key)); - int32_t i; + size_t i; const JanetKV *first_bucket = NULL; /* Higher half */ for (i = index; i < cap; i++) { @@ -318,7 +318,7 @@ const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key) { } /* Get a value from a janet struct or table. */ -Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key) { +Janet janet_dictionary_get(const JanetKV *data, size_t cap, Janet key) { const JanetKV *kv = janet_dict_find(data, cap, key); if (kv && !janet_checktype(kv->key, JANET_NIL)) { return kv->value; @@ -327,7 +327,7 @@ Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key) { } /* Iterate through a struct or dictionary generically */ -const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv) { +const JanetKV *janet_dictionary_next(const JanetKV *kvs, size_t cap, const JanetKV *kv) { const JanetKV *end = kvs + cap; kv = (kv == NULL) ? kvs : kv + 1; while (kv < end) { @@ -740,7 +740,7 @@ Janet janet_resolve_core(const char *name) { /* Read both tuples and arrays as c pointers + int32_t length. Return 1 if the * view can be constructed, 0 if an invalid type. */ -int janet_indexed_view(Janet seq, const Janet **data, int32_t *len) { +int janet_indexed_view(Janet seq, const Janet **data, size_t *len) { if (janet_checktype(seq, JANET_ARRAY)) { *data = janet_unwrap_array(seq)->data; *len = janet_unwrap_array(seq)->count; @@ -755,7 +755,7 @@ int janet_indexed_view(Janet seq, const Janet **data, int32_t *len) { /* Read both strings and buffer as unsigned character array + int32_t len. * Returns 1 if the view can be constructed and 0 if the type is invalid. */ -int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len) { +int janet_bytes_view(Janet str, const uint8_t **data, size_t *len) { JanetType t = janet_type(str); if (t == JANET_STRING || t == JANET_SYMBOL || t == JANET_KEYWORD) { *data = janet_unwrap_string(str); @@ -782,7 +782,7 @@ int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len) { /* Read both structs and tables as the entries of a hashtable with * identical structure. Returns 1 if the view can be constructed and * 0 if the type is invalid. */ -int janet_dictionary_view(Janet tab, const JanetKV **data, int32_t *len, int32_t *cap) { +int janet_dictionary_view(Janet tab, const JanetKV **data, size_t *len, size_t *cap) { if (janet_checktype(tab, JANET_TABLE)) { *data = janet_unwrap_table(tab)->data; *cap = janet_unwrap_table(tab)->capacity; diff --git a/src/core/util.h b/src/core/util.h index 06cbb962..3ac69a69 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -66,18 +66,18 @@ /* Utils */ uint32_t janet_hash_mix(uint32_t input, uint32_t more); #define janet_maphash(cap, hash) ((uint32_t)(hash) & (cap - 1)) -int janet_valid_utf8(const uint8_t *str, int32_t len); +int janet_valid_utf8(const uint8_t *str, size_t len); int janet_is_symbol_char(uint8_t c); extern const char janet_base64[65]; -int32_t janet_array_calchash(const Janet *array, int32_t len); -int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len); -int32_t janet_string_calchash(const uint8_t *str, int32_t len); -int32_t janet_tablen(int32_t n); +int32_t janet_array_calchash(const Janet *array, size_t len); +int32_t janet_kv_calchash(const JanetKV *kvs, size_t len); +int32_t janet_string_calchash(const uint8_t *str, size_t len); +size_t janet_tablen(size_t n); void safe_memcpy(void *dest, const void *src, size_t len); void janet_buffer_push_types(JanetBuffer *buffer, int types); -const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key); -void janet_memempty(JanetKV *mem, int32_t count); -void *janet_memalloc_empty(int32_t count); +const JanetKV *janet_dict_find(const JanetKV *buckets, size_t cap, Janet key); +void janet_memempty(JanetKV *mem, size_t count); +void *janet_memalloc_empty(size_t count); JanetTable *janet_get_core_table(const char *name); void janet_def_addflags(JanetFuncDef *def); const void *janet_strbinsearch( diff --git a/src/core/value.c b/src/core/value.c index ed66ee11..91cf84a6 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -31,7 +31,7 @@ #include -static void push_traversal_node(void *lhs, void *rhs, int32_t index2) { +static void push_traversal_node(void *lhs, void *rhs, size_t index2) { JanetTraversalNode node; node.self = (JanetGCObject *) lhs; node.other = (JanetGCObject *) rhs; @@ -75,7 +75,7 @@ static int traversal_next(Janet *x, Janet *y) { if ((self->flags & JANET_MEM_TYPEBITS) == JANET_MEMORY_TUPLE) { /* Node is a tuple at index t->index */ if (t->index < tself->length && t->index < tother->length) { - int32_t index = t->index++; + size_t index = t->index++; *x = tself->data[index]; *y = tother->data[index]; janet_vm.traversal = t; @@ -88,13 +88,13 @@ static int traversal_next(Janet *x, Janet *y) { /* Node is a struct at index t->index: if t->index2 is true, we should return the values. */ if (t->index2) { t->index2 = 0; - int32_t index = t->index++; + size_t index = t->index++; *x = sself->data[index].value; *y = sother->data[index].value; janet_vm.traversal = t; return 0; } - for (int32_t i = t->index; i < sself->capacity; i++) { + for (size_t i = t->index; i < sself->capacity; i++) { t->index2 = 1; *x = sself->data[t->index].key; *y = sother->data[t->index].key; @@ -135,7 +135,7 @@ Janet janet_next_impl(Janet ds, Janet key, int is_interpreter) { case JANET_TABLE: case JANET_STRUCT: { const JanetKV *start; - int32_t cap; + size_t cap; if (t == JANET_TABLE) { JanetTable *tab = janet_unwrap_table(ds); cap = tab->capacity; @@ -161,7 +161,7 @@ Janet janet_next_impl(Janet ds, Janet key, int is_interpreter) { case JANET_BUFFER: case JANET_ARRAY: case JANET_TUPLE: { - int32_t i; + size_t i; if (janet_checktype(key, JANET_NIL)) { i = 0; } else if (janet_checkint(key)) { @@ -169,7 +169,7 @@ Janet janet_next_impl(Janet ds, Janet key, int is_interpreter) { } else { break; } - int32_t len; + size_t len; if (t == JANET_BUFFER) { len = janet_unwrap_buffer(ds)->count; } else if (t == JANET_ARRAY) { @@ -180,7 +180,7 @@ Janet janet_next_impl(Janet ds, Janet key, int is_interpreter) { len = janet_string_length(janet_unwrap_string(ds)); } if (i < len && i >= 0) { - return janet_wrap_integer(i); + return janet_wrap_size(i); } break; } @@ -423,8 +423,8 @@ int janet_compare(Janet x, Janet y) { case JANET_STRUCT: { const JanetKV *lhs = janet_unwrap_struct(x); const JanetKV *rhs = janet_unwrap_struct(y); - int32_t llen = janet_struct_capacity(lhs); - int32_t rlen = janet_struct_capacity(rhs); + size_t llen = janet_struct_capacity(lhs); + size_t rlen = janet_struct_capacity(rhs); int32_t lhash = janet_struct_hash(lhs); int32_t rhash = janet_struct_hash(rhs); if (llen < rlen) return -1; @@ -449,6 +449,15 @@ bad: janet_panicf("expected integer key for %s in range [0, %d), got %v", janet_type_names[type], max, key); } +static size_t getter_checksize(JanetType type, Janet key, size_t max) { + if (!janet_checksize(key)) goto bad; + size_t ret = janet_unwrap_size(key); + if (ret >= max) goto bad; + return ret; +bad: + janet_panicf("expected integer key for %s in range [0, %d), got %v", janet_type_names[type], max, key); +} + /* Gets a value and returns. Can panic. */ Janet janet_in(Janet ds, Janet key) { Janet value; @@ -465,19 +474,19 @@ Janet janet_in(Janet ds, Janet key) { break; case JANET_ARRAY: { JanetArray *array = janet_unwrap_array(ds); - int32_t index = getter_checkint(type, key, array->count); + size_t index = getter_checksize(type, key, array->count); value = array->data[index]; break; } case JANET_TUPLE: { const Janet *tuple = janet_unwrap_tuple(ds); - int32_t len = janet_tuple_length(tuple); - value = tuple[getter_checkint(type, key, len)]; + size_t len = janet_tuple_length(tuple); + value = tuple[getter_checksize(type, key, len)]; break; } case JANET_BUFFER: { JanetBuffer *buffer = janet_unwrap_buffer(ds); - int32_t index = getter_checkint(type, key, buffer->count); + size_t index = getter_checksize(type, key, buffer->count); value = janet_wrap_integer(buffer->data[index]); break; } @@ -485,7 +494,7 @@ Janet janet_in(Janet ds, Janet key) { case JANET_SYMBOL: case JANET_KEYWORD: { const uint8_t *str = janet_unwrap_string(ds); - int32_t index = getter_checkint(type, key, janet_string_length(str)); + size_t index = getter_checksize(type, key, janet_string_length(str)); value = janet_wrap_integer(str[index]); break; } @@ -519,8 +528,8 @@ Janet janet_get(Janet ds, Janet key) { case JANET_STRING: case JANET_SYMBOL: case JANET_KEYWORD: { - if (!janet_checkint(key)) return janet_wrap_nil(); - int32_t index = janet_unwrap_integer(key); + if (!janet_checksize(key)) return janet_wrap_nil(); + size_t index = janet_unwrap_size(key); if (index < 0) return janet_wrap_nil(); const uint8_t *str = janet_unwrap_string(ds); if (index >= janet_string_length(str)) return janet_wrap_nil(); @@ -539,7 +548,7 @@ Janet janet_get(Janet ds, Janet key) { case JANET_TUPLE: case JANET_BUFFER: { if (!janet_checkint(key)) return janet_wrap_nil(); - int32_t index = janet_unwrap_integer(key); + size_t index = janet_unwrap_size(key); if (index < 0) return janet_wrap_nil(); if (t == JANET_ARRAY) { JanetArray *a = janet_unwrap_array(ds); @@ -573,7 +582,7 @@ Janet janet_get(Janet ds, Janet key) { } } -Janet janet_getindex(Janet ds, int32_t index) { +Janet janet_getindex(Janet ds, size_t index) { Janet value; if (index < 0) janet_panic("expected non-negative index"); switch (janet_type(ds)) { @@ -638,7 +647,7 @@ Janet janet_getindex(Janet ds, int32_t index) { return value; } -int32_t janet_length(Janet x) { +size_t janet_length(Janet x) { switch (janet_type(x)) { default: janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, x); @@ -661,14 +670,14 @@ int32_t janet_length(Janet x) { const JanetAbstractType *type = janet_abstract_type(abst); if (type->length != NULL) { size_t len = type->length(abst, janet_abstract_size(abst)); - if (len > INT32_MAX) { + if (len > JANET_INTMAX_INT64) { janet_panicf("invalid integer length %u", len); } - return (int32_t)(len); + return len; } Janet argv[1] = { x }; Janet len = janet_mcall("length", 1, argv); - if (!janet_checkint(len)) + if (!janet_checksize(len)) janet_panicf("invalid integer length %v", len); return janet_unwrap_integer(len); } @@ -682,17 +691,17 @@ Janet janet_lengthv(Janet x) { case JANET_STRING: case JANET_SYMBOL: case JANET_KEYWORD: - return janet_wrap_integer(janet_string_length(janet_unwrap_string(x))); + return janet_wrap_size(janet_string_length(janet_unwrap_string(x))); case JANET_ARRAY: - return janet_wrap_integer(janet_unwrap_array(x)->count); + return janet_wrap_size(janet_unwrap_array(x)->count); case JANET_BUFFER: - return janet_wrap_integer(janet_unwrap_buffer(x)->count); + return janet_wrap_size(janet_unwrap_buffer(x)->count); case JANET_TUPLE: - return janet_wrap_integer(janet_tuple_length(janet_unwrap_tuple(x))); + return janet_wrap_size(janet_tuple_length(janet_unwrap_tuple(x))); case JANET_STRUCT: - return janet_wrap_integer(janet_struct_length(janet_unwrap_struct(x))); + return janet_wrap_size(janet_struct_length(janet_unwrap_struct(x))); case JANET_TABLE: - return janet_wrap_integer(janet_unwrap_table(x)->count); + return janet_wrap_size(janet_unwrap_table(x)->count); case JANET_ABSTRACT: { void *abst = janet_unwrap_abstract(x); const JanetAbstractType *type = janet_abstract_type(abst); @@ -715,7 +724,7 @@ Janet janet_lengthv(Janet x) { } } -void janet_putindex(Janet ds, int32_t index, Janet value) { +void janet_putindex(Janet ds, size_t index, Janet value) { switch (janet_type(ds)) { default: janet_panicf("expected %T, got %v", @@ -742,13 +751,13 @@ void janet_putindex(Janet ds, int32_t index, Janet value) { } case JANET_TABLE: { JanetTable *table = janet_unwrap_table(ds); - janet_table_put(table, janet_wrap_integer(index), value); + janet_table_put(table, janet_wrap_size(index), value); break; } case JANET_ABSTRACT: { JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds)); if (type->put) { - (type->put)(janet_unwrap_abstract(ds), janet_wrap_integer(index), value); + (type->put)(janet_unwrap_abstract(ds), janet_wrap_size(index), value); } else { janet_panicf("no setter for %v ", ds); } @@ -765,7 +774,7 @@ void janet_put(Janet ds, Janet key, Janet value) { JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds); case JANET_ARRAY: { JanetArray *array = janet_unwrap_array(ds); - int32_t index = getter_checkint(type, key, INT32_MAX - 1); + size_t index = getter_checksize(type, key, JANET_INTMAX_INT64 - 1); if (index >= array->count) { janet_array_setcount(array, index + 1); } @@ -774,7 +783,7 @@ void janet_put(Janet ds, Janet key, Janet value) { } case JANET_BUFFER: { JanetBuffer *buffer = janet_unwrap_buffer(ds); - int32_t index = getter_checkint(type, key, INT32_MAX - 1); + size_t index = getter_checksize(type, key, JANET_INTMAX_INT64 - 1); if (!janet_checkint(value)) janet_panicf("can only put integers in buffers, got %v", value); if (index >= buffer->count) { diff --git a/src/core/wrap.c b/src/core/wrap.c index f936865a..2edae297 100644 --- a/src/core/wrap.c +++ b/src/core/wrap.c @@ -160,10 +160,10 @@ Janet(janet_wrap_number)(double x) { /*****/ -void *janet_memalloc_empty(int32_t count) { - int32_t i; - void *mem = janet_malloc((size_t) count * sizeof(JanetKV)); - janet_vm.next_collection += (size_t) count * sizeof(JanetKV); +void *janet_memalloc_empty(size_t count) { + size_t i; + void *mem = janet_malloc(count * sizeof(JanetKV)); + janet_vm.next_collection += count * sizeof(JanetKV); if (NULL == mem) { JANET_OUT_OF_MEMORY; } @@ -176,8 +176,8 @@ void *janet_memalloc_empty(int32_t count) { return mem; } -void janet_memempty(JanetKV *mem, int32_t count) { - int32_t i; +void janet_memempty(JanetKV *mem, size_t count) { + size_t i; for (i = 0; i < count; i++) { mem[i].key = janet_wrap_nil(); mem[i].value = janet_wrap_nil(); diff --git a/src/include/janet.h b/src/include/janet.h index 08bb48c1..973bc22f 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -900,8 +900,11 @@ JANET_API JanetAbstract janet_checkabstract(Janet x, const JanetAbstractType *at #define janet_checkuintrange(x) ((x) >= 0 && (x) <= UINT32_MAX && (x) == (uint32_t)(x)) #define janet_checkint64range(x) ((x) >= JANET_INTMIN_DOUBLE && (x) <= JANET_INTMAX_DOUBLE && (x) == (int64_t)(x)) #define janet_checkuint64range(x) ((x) >= 0 && (x) <= JANET_INTMAX_DOUBLE && (x) == (uint64_t)(x)) +#define janet_checksizerange(x) ((x) >= 0 && (x) <= JANET_INTMAX_INT64 && (x) == (size_t)(x)) #define janet_unwrap_integer(x) ((int32_t) janet_unwrap_number(x)) #define janet_wrap_integer(x) janet_wrap_number((int32_t)(x)) +#define janet_unwrap_size(x) ((size_t) janet_unwrap_number(x)) +#define janet_wrap_size(x) janet_wrap_number((size_t)(x)) #define janet_checktypes(x, tps) ((1 << janet_type(x)) & (tps)) @@ -1201,18 +1204,18 @@ struct JanetMethod { struct JanetView { const Janet *items; - int32_t len; + size_t len; }; struct JanetByteView { const uint8_t *bytes; - int32_t len; + size_t len; }; struct JanetDictView { const JanetKV *kvs; - int32_t len; - int32_t cap; + size_t len; + size_t cap; }; struct JanetRange { @@ -1576,17 +1579,17 @@ JANET_API JanetTable *janet_core_env(JanetTable *replacements); JANET_API JanetTable *janet_core_lookup_table(JanetTable *replacements); /* Execute strings */ -JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out); +JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, size_t len, const char *sourcePath, Janet *out); JANET_API int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out); /* Run the entrypoint of a wrapped program */ JANET_API int janet_loop_fiber(JanetFiber *fiber); /* Number scanning */ -JANET_API int janet_scan_number(const uint8_t *str, int32_t len, double *out); -JANET_API int janet_scan_number_base(const uint8_t *str, int32_t len, int32_t base, double *out); -JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out); -JANET_API int janet_scan_uint64(const uint8_t *str, int32_t len, uint64_t *out); +JANET_API int janet_scan_number(const uint8_t *str, size_t len, double *out); +JANET_API int janet_scan_number_base(const uint8_t *str, size_t len, int32_t base, double *out); +JANET_API int janet_scan_int64(const uint8_t *str, size_t len, int64_t *out); +JANET_API int janet_scan_uint64(const uint8_t *str, size_t len, uint64_t *out); /* Debugging */ JANET_API void janet_debug_break(JanetFuncDef *def, int32_t pc); @@ -1599,7 +1602,7 @@ JANET_API void janet_debug_find( extern JANET_API const JanetAbstractType janet_rng_type; JANET_API JanetRNG *janet_default_rng(void); JANET_API void janet_rng_seed(JanetRNG *rng, uint32_t seed); -JANET_API void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, int32_t len); +JANET_API void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, size_t len); JANET_API uint32_t janet_rng_u32(JanetRNG *rng); JANET_API double janet_rng_double(JanetRNG *rng); @@ -1667,7 +1670,7 @@ JANET_API JanetBuffer *janet_formatb(JanetBuffer *bufp, const char *format, ...) JANET_API void janet_formatbv(JanetBuffer *bufp, const char *format, va_list args); /* Symbol functions */ -JANET_API JanetSymbol janet_symbol(const uint8_t *str, int32_t len); +JANET_API JanetSymbol janet_symbol(const uint8_t *str, size_t len); JANET_API JanetSymbol janet_csymbol(const char *str); JANET_API JanetSymbol janet_symbol_gen(void); #define janet_symbolv(str, len) janet_wrap_symbol(janet_symbol((str), (len))) @@ -1721,11 +1724,11 @@ JANET_API JanetFiber *janet_current_fiber(void); JANET_API JanetFiber *janet_root_fiber(void); /* Treat similar types through uniform interfaces for iteration */ -JANET_API int janet_indexed_view(Janet seq, const Janet **data, int32_t *len); -JANET_API int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len); -JANET_API int janet_dictionary_view(Janet tab, const JanetKV **data, int32_t *len, int32_t *cap); -JANET_API Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key); -JANET_API const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv); +JANET_API int janet_indexed_view(Janet seq, const Janet **data, size_t *len); +JANET_API int janet_bytes_view(Janet str, const uint8_t **data, size_t *len); +JANET_API int janet_dictionary_view(Janet tab, const JanetKV **data, size_t *len, size_t *cap); +JANET_API Janet janet_dictionary_get(const JanetKV *data, size_t cap, Janet key); +JANET_API const JanetKV *janet_dictionary_next(const JanetKV *kvs, size_t cap, const JanetKV *kv); /* Abstract */ #define janet_abstract_head(u) ((JanetAbstractHead *)((char *)u - offsetof(JanetAbstractHead, data))) @@ -1801,8 +1804,8 @@ JANET_API int janet_cstrcmp(JanetString str, const char *other); JANET_API Janet janet_in(Janet ds, Janet key); JANET_API Janet janet_get(Janet ds, Janet key); JANET_API Janet janet_next(Janet ds, Janet key); -JANET_API Janet janet_getindex(Janet ds, int32_t index); -JANET_API int32_t janet_length(Janet x); +JANET_API Janet janet_getindex(Janet ds, size_t index); +JANET_API size_t janet_length(Janet x); JANET_API Janet janet_lengthv(Janet x); JANET_API void janet_put(Janet ds, Janet key, Janet value); JANET_API void janet_putindex(Janet ds, int32_t index, Janet value); @@ -2171,8 +2174,8 @@ JANET_API Janet janet_wrap_s64(int64_t x); JANET_API Janet janet_wrap_u64(uint64_t x); JANET_API int64_t janet_unwrap_s64(Janet x); JANET_API uint64_t janet_unwrap_u64(Janet x); -JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out); -JANET_API int janet_scan_uint64(const uint8_t *str, int32_t len, uint64_t *out); +JANET_API int janet_scan_int64(const uint8_t *str, size_t len, int64_t *out); +JANET_API int janet_scan_uint64(const uint8_t *str, size_t len, uint64_t *out); #endif