From d0acf6426d48841bdcc12f5999f82900663c8d79 Mon Sep 17 00:00:00 2001 From: GrayJack Date: Sun, 21 Apr 2024 17:29:33 -0300 Subject: [PATCH] refactor(c-api): Requested changes from review --- src/core/buffer.c | 6 ++-- src/core/fiber.c | 67 ++++++++++++++++++++++----------------------- src/core/fiber.h | 4 +-- src/core/marsh.c | 32 +++++++++++----------- src/core/string.c | 2 +- src/core/struct.c | 7 ++++- src/core/util.c | 2 +- src/core/util.h | 2 +- src/core/wrap.c | 12 ++++++++ src/include/janet.h | 16 +++++++---- 10 files changed, 84 insertions(+), 66 deletions(-) diff --git a/src/core/buffer.c b/src/core/buffer.c index dfe1859f..16f8cc29 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -90,8 +90,8 @@ void janet_buffer_ensure(JanetBuffer *buffer, size_t capacity, size_t growth) { uint8_t *old = buffer->data; if (capacity <= buffer->capacity) return; janet_buffer_can_realloc(buffer); - size_t big_capacity = (capacity) * growth; - capacity = big_capacity > JANET_INTMAX_SIZE ? JANET_INTMAX_SIZE : big_capacity; + uint64_t big_capacity = (uint64_t) capacity*growth; + capacity = big_capacity > JANET_INTMAX_SIZE ? JANET_INTMAX_SIZE : (size_t) big_capacity; janet_gcpressure(capacity - buffer->capacity); new_data = janet_realloc(old, capacity * sizeof(uint8_t)); if (NULL == new_data) { @@ -115,7 +115,7 @@ void janet_buffer_setcount(JanetBuffer *buffer, size_t count) { * next n bytes pushed to the buffer will not cause a reallocation */ void janet_buffer_extra(JanetBuffer *buffer, size_t n) { /* Check for buffer overflow */ - if (n + buffer->count > JANET_INTMAX_SIZE) { + if ((int64_t)n + buffer->count > JANET_INTMAX_SIZE) { janet_panic("buffer overflow"); } size_t new_size = buffer->count + n; diff --git a/src/core/fiber.c b/src/core/fiber.c index 33a945eb..10a0af46 100644 --- a/src/core/fiber.c +++ b/src/core/fiber.c @@ -54,11 +54,8 @@ static JanetFiber *fiber_alloc(size_t capacity) { if (capacity < 32) { capacity = 32; } - if (capacity > JANET_INTMAX_SIZE) { - capacity = JANET_INTMAX_SIZE; - } fiber->capacity = capacity; - data = janet_malloc(sizeof(Janet) * capacity); + data = janet_malloc(sizeof(Janet) * (size_t) capacity); if (NULL == data) { JANET_OUT_OF_MEMORY; } @@ -69,7 +66,7 @@ static JanetFiber *fiber_alloc(size_t capacity) { /* Create a new fiber with argn values on the stack by reusing a fiber. */ JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv) { - size_t newstacktop; + int32_t newstacktop; fiber_reset(fiber); if (argc) { newstacktop = fiber->stacktop + argc; @@ -96,7 +93,7 @@ JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t } /* Create a new fiber with argn values on the stack. */ -JanetFiber *janet_fiber(JanetFunction *callee, size_t capacity, int32_t argc, const Janet *argv) { +JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv) { return janet_fiber_reset(fiber_alloc(capacity), callee, argc, argv); } @@ -117,9 +114,9 @@ static void janet_fiber_refresh_memory(JanetFiber *fiber) { #endif /* Ensure that the fiber has enough extra capacity */ -void janet_fiber_setcapacity(JanetFiber *fiber, size_t n) { - size_t old_size = fiber->capacity; - size_t diff = n - old_size; +void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n) { + int32_t old_size = fiber->capacity; + int32_t diff = n - old_size; Janet *newData = janet_realloc(fiber->data, sizeof(Janet) * n); if (NULL == newData) { JANET_OUT_OF_MEMORY; @@ -130,14 +127,14 @@ void janet_fiber_setcapacity(JanetFiber *fiber, size_t n) { } /* Grow fiber if needed */ -static void janet_fiber_grow(JanetFiber *fiber, size_t needed) { - size_t cap = needed > (JANET_INTMAX_SIZE / 2) ? JANET_INTMAX_SIZE : 2 * needed; +static void janet_fiber_grow(JanetFiber *fiber, int32_t needed) { + int32_t cap = needed > (INT32_MAX / 2) ? INT32_MAX : 2 * needed; janet_fiber_setcapacity(fiber, cap); } /* Push a value on the next stack frame */ void janet_fiber_push(JanetFiber *fiber, Janet x) { - if (fiber->stacktop == JANET_INTMAX_SIZE) janet_panic("stack overflow"); + if (fiber->stacktop == INT32_MAX) janet_panic("stack overflow"); if (fiber->stacktop >= fiber->capacity) { janet_fiber_grow(fiber, fiber->stacktop); } @@ -146,8 +143,8 @@ void janet_fiber_push(JanetFiber *fiber, Janet x) { /* Push 2 values on the next stack frame */ void janet_fiber_push2(JanetFiber *fiber, Janet x, Janet y) { - if (fiber->stacktop >= JANET_INTMAX_SIZE - 1) janet_panic("stack overflow"); - size_t newtop = fiber->stacktop + 2; + if (fiber->stacktop >= INT32_MAX - 1) janet_panic("stack overflow"); + int32_t newtop = fiber->stacktop + 2; if (newtop > fiber->capacity) { janet_fiber_grow(fiber, newtop); } @@ -158,8 +155,8 @@ void janet_fiber_push2(JanetFiber *fiber, Janet x, Janet y) { /* Push 3 values on the next stack frame */ void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z) { - if (fiber->stacktop >= JANET_INTMAX_SIZE - 2) janet_panic("stack overflow"); - size_t newtop = fiber->stacktop + 3; + if (fiber->stacktop >= INT32_MAX - 2) janet_panic("stack overflow"); + int32_t newtop = fiber->stacktop + 3; if (newtop > fiber->capacity) { janet_fiber_grow(fiber, newtop); } @@ -170,9 +167,9 @@ void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z) { } /* Push an array on the next stack frame */ -void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, size_t n) { - if (fiber->stacktop > JANET_INTMAX_SIZE - n) janet_panic("stack overflow"); - size_t newtop = fiber->stacktop + n; +void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n) { + if (fiber->stacktop > INT32_MAX - n) janet_panic("stack overflow"); + int32_t newtop = fiber->stacktop + n; if (newtop > fiber->capacity) { janet_fiber_grow(fiber, newtop); } @@ -181,8 +178,8 @@ void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, size_t n) { } /* Create a struct with n values. If n is odd, the last value is ignored. */ -static Janet make_struct_n(const Janet *args, size_t n) { - size_t i = 0; +static Janet make_struct_n(const Janet *args, int32_t n) { + int32_t i = 0; JanetKV *st = janet_struct_begin(n & (~1)); for (; i < n; i += 2) { janet_struct_put(st, args[i], args[i + 1]); @@ -194,11 +191,11 @@ static Janet make_struct_n(const Janet *args, size_t n) { int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func) { JanetStackFrame *newframe; - size_t i; - size_t oldtop = fiber->stacktop; - size_t oldframe = fiber->frame; - size_t nextframe = fiber->stackstart; - size_t nextstacktop = nextframe + func->def->slotcount + JANET_FRAME_SIZE; + int32_t i; + int32_t oldtop = fiber->stacktop; + int32_t oldframe = fiber->frame; + int32_t nextframe = fiber->stackstart; + int32_t nextstacktop = nextframe + func->def->slotcount + JANET_FRAME_SIZE; int32_t next_arity = (int32_t) fiber->stacktop - fiber->stackstart; /* Check strict arity before messing with state */ @@ -230,7 +227,7 @@ int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func) { /* Check varargs */ if (func->def->flags & JANET_FUNCDEF_FLAG_VARARG) { - size_t tuplehead = fiber->frame + (size_t) func->def->arity; + int32_t tuplehead = fiber->frame + func->def->arity; int st = func->def->flags & JANET_FUNCDEF_FLAG_STRUCTARG; if (tuplehead >= oldtop) { fiber->data[tuplehead] = st @@ -331,11 +328,11 @@ void janet_env_maybe_detach(JanetFuncEnv *env) { /* Create a tail frame for a function */ int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) { - size_t i; - size_t nextframetop = fiber->frame + func->def->slotcount; - size_t nextstacktop = nextframetop + JANET_FRAME_SIZE; + int32_t i; + int32_t nextframetop = fiber->frame + func->def->slotcount; + int32_t nextstacktop = nextframetop + JANET_FRAME_SIZE; int32_t next_arity = (int32_t) fiber->stacktop - fiber->stackstart; - size_t stacksize; + int32_t stacksize; /* Check strict arity before messing with state */ if (next_arity < func->def->min_arity) return 1; @@ -359,7 +356,7 @@ int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) { /* Check varargs */ if (func->def->flags & JANET_FUNCDEF_FLAG_VARARG) { - size_t tuplehead = fiber->stackstart + (size_t) func->def->arity; + int32_t tuplehead = fiber->stackstart + func->def->arity; int st = func->def->flags & JANET_FUNCDEF_FLAG_STRUCTARG; if (tuplehead >= fiber->stacktop) { if (tuplehead >= fiber->capacity) janet_fiber_setcapacity(fiber, 2 * (tuplehead + 1)); @@ -403,9 +400,9 @@ int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) { void janet_fiber_cframe(JanetFiber *fiber, JanetCFunction cfun) { JanetStackFrame *newframe; - size_t oldframe = fiber->frame; - size_t nextframe = fiber->stackstart; - size_t nextstacktop = fiber->stacktop + JANET_FRAME_SIZE; + int32_t oldframe = fiber->frame; + int32_t nextframe = fiber->stackstart; + int32_t nextstacktop = fiber->stacktop + JANET_FRAME_SIZE; if (fiber->capacity < nextstacktop) { janet_fiber_setcapacity(fiber, 2 * nextstacktop); diff --git a/src/core/fiber.h b/src/core/fiber.h index c1067eba..8adfa04f 100644 --- a/src/core/fiber.h +++ b/src/core/fiber.h @@ -70,11 +70,11 @@ #define janet_stack_frame(s) ((JanetStackFrame *)((s) - JANET_FRAME_SIZE)) #define janet_fiber_frame(f) janet_stack_frame((f)->data + (f)->frame) -void janet_fiber_setcapacity(JanetFiber *fiber, size_t n); +void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n); void janet_fiber_push(JanetFiber *fiber, Janet x); void janet_fiber_push2(JanetFiber *fiber, Janet x, Janet y); void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z); -void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, size_t n); +void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n); int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func); int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func); void janet_fiber_cframe(JanetFiber *fiber, JanetCFunction cfun); diff --git a/src/core/marsh.c b/src/core/marsh.c index 6e8dd1b0..d07c96e5 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -338,13 +338,13 @@ static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) { if (janet_fiber_status(fiber) == JANET_STATUS_ALIVE) janet_panic("cannot marshal alive fiber"); pushint(st, fflags); - pushsize(st, fiber->frame); - pushsize(st, fiber->stackstart); - pushsize(st, fiber->stacktop); - pushsize(st, fiber->maxstack); + pushint(st, fiber->frame); + pushint(st, fiber->stackstart); + pushint(st, fiber->stacktop); + pushint(st, fiber->maxstack); /* Do frames */ - size_t i = fiber->frame; - size_t j = fiber->stackstart - JANET_FRAME_SIZE; + int32_t i = fiber->frame; + int32_t j = fiber->stackstart - JANET_FRAME_SIZE; while (i > 0) { JanetStackFrame *frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE); if (frame->env) frame->flags |= JANET_STACKFRAME_HASENV; @@ -356,7 +356,7 @@ static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) { marshal_one(st, janet_wrap_function(frame->func), flags + 1); if (frame->env) marshal_one_env(st, frame->env, flags + 1); /* Marshal all values in the stack frame */ - for (size_t k = i; k < j; k++) + for (int32_t k = i; k < j; k++) marshal_one(st, fiber->data[k], flags + 1); j = i - JANET_FRAME_SIZE; i = frame->prevframe; @@ -1084,10 +1084,10 @@ static const uint8_t *unmarshal_one_fiber( /* Read ints */ int32_t fiber_flags = readint(st, &data); - size_t frame = readsize(st, &data); - size_t fiber_stackstart = readsize(st, &data); - size_t fiber_stacktop = readsize(st, &data); - size_t fiber_maxstack = readsize(st, &data); + int32_t frame = readnat(st, &data); + int32_t fiber_stackstart = readnat(st, &data); + int32_t fiber_stacktop = readnat(st, &data); + int32_t fiber_maxstack = readnat(st, &data); JanetTable *fiber_env = NULL; /* Check for bad flags and ints */ @@ -1103,19 +1103,19 @@ static const uint8_t *unmarshal_one_fiber( if (!fiber->data) { JANET_OUT_OF_MEMORY; } - for (size_t i = 0; i < fiber->capacity; i++) { + for (int32_t i = 0; i < fiber->capacity; i++) { fiber->data[i] = janet_wrap_nil(); } /* get frames */ - ssize_t stack = frame; - size_t stacktop = fiber_stackstart - JANET_FRAME_SIZE; + int32_t stack = frame; + int32_t stacktop = fiber_stackstart - JANET_FRAME_SIZE; while (stack > 0) { JanetFunction *func = NULL; JanetFuncDef *def = NULL; JanetFuncEnv *env = NULL; int32_t frameflags = readint(st, &data); - size_t prevframe = readsize(st, &data); + int32_t prevframe = readnat(st, &data); int32_t pcdiff = readnat(st, &data); /* Get frame items */ @@ -1148,7 +1148,7 @@ static const uint8_t *unmarshal_one_fiber( } /* Get stack items */ - for (ssize_t i = stack; i < (ssize_t)stacktop; i++) + for (int32_t i = stack; i < stacktop; i++) data = unmarshal_one(st, data, fiber->data + i, flags + 1); /* Set frame */ diff --git a/src/core/string.c b/src/core/string.c index a4badf8f..3d171423 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -207,7 +207,7 @@ JANET_CORE_FN(cfun_string_repeat, JanetByteView view = janet_getbytes(argv, 0); size_t rep = janet_getsize(argv, 1); if (rep == 0) return janet_cstringv(""); - size_t mulres = rep * view.len; + uint64_t mulres = (uint64_t) rep * view.len; if (mulres > JANET_INTMAX_SIZE) janet_panic("result string is too long"); uint8_t *newbuf = janet_string_begin(mulres); uint8_t *end = newbuf + mulres; diff --git a/src/core/struct.c b/src/core/struct.c index 4ed3a371..7d559558 100644 --- a/src/core/struct.c +++ b/src/core/struct.c @@ -31,7 +31,12 @@ /* Begin creation of a struct */ JanetKV *janet_struct_begin(size_t count) { /* Calculate capacity as power of 2 after 2 * count. */ - size_t capacity = janet_tablen(2 * count); + + uint64_t double_count = + (count > JANET_INTMAX_SIZE / 2) ? JANET_INTMAX_SIZE : 2 * count; + uint64_t cap = janet_tablen(double_count); + size_t capacity = + (cap > JANET_INTMAX_SIZE) ? JANET_INTMAX_SIZE : (size_t)cap; size_t size = sizeof(JanetStructHead) + capacity * sizeof(JanetKV); JanetStructHead *head = janet_gcalloc(JANET_MEMORY_STRUCT, size); diff --git a/src/core/util.c b/src/core/util.c index 2d6a7325..1077cdca 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -267,7 +267,7 @@ int32_t janet_kv_calchash(const JanetKV *kvs, size_t len) { /* Calculate next power of 2. May overflow. If n is 0, * will return 0. */ -size_t janet_tablen(size_t n) { +uint64_t janet_tablen(uint64_t n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; diff --git a/src/core/util.h b/src/core/util.h index 0a8cbf88..74759e02 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -72,7 +72,7 @@ extern const char janet_base64[65]; 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); +uint64_t janet_tablen(uint64_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, size_t cap, Janet key); diff --git a/src/core/wrap.c b/src/core/wrap.c index 2edae297..59d17835 100644 --- a/src/core/wrap.c +++ b/src/core/wrap.c @@ -88,6 +88,12 @@ int (janet_unwrap_boolean)(Janet x) { int32_t (janet_unwrap_integer)(Janet x) { return janet_unwrap_integer(x); } +size_t (janet_unwrap_size)(Janet x) { + return janet_unwrap_size(x); +} +ssize_t (janet_unwrap_ssize)(Janet x) { + return janet_unwrap_ssize(x); +} #if defined(JANET_NANBOX_32) || defined(JANET_NANBOX_64) Janet(janet_wrap_nil)(void) { @@ -144,6 +150,12 @@ Janet(janet_wrap_pointer)(void *x) { Janet(janet_wrap_integer)(int32_t x) { return janet_wrap_integer(x); } +Janet(janet_wrap_size)(size_t x) { + return janet_wrap_size(x); +} +Janet(janet_wrap_ssize)(ssize_t x) { + return janet_wrap_ssize(x); +} #endif #ifndef JANET_NANBOX_32 diff --git a/src/include/janet.h b/src/include/janet.h index bb50bdc4..f555e142 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -717,6 +717,8 @@ JANET_API JanetCFunction janet_unwrap_cfunction(Janet x); JANET_API int janet_unwrap_boolean(Janet x); JANET_API double janet_unwrap_number(Janet x); JANET_API int32_t janet_unwrap_integer(Janet x); +JANET_API size_t janet_unwrap_size(Janet x); +JANET_API ssize_t janet_unwrap_ssize(Janet x); JANET_API Janet janet_wrap_nil(void); JANET_API Janet janet_wrap_number(double x); @@ -737,6 +739,8 @@ JANET_API Janet janet_wrap_table(JanetTable *x); JANET_API Janet janet_wrap_abstract(JanetAbstract x); JANET_API Janet janet_wrap_pointer(void *x); JANET_API Janet janet_wrap_integer(int32_t x); +JANET_API Janet janet_wrap_size(size_t x); +JANET_API Janet janet_wrap_ssize(ssize_t x); /***** END SECTION NON-C API *****/ @@ -942,11 +946,11 @@ struct JanetGCObject { struct JanetFiber { JanetGCObject gc; /* GC Object stuff */ int32_t flags; /* More flags */ - size_t frame; /* Index of the stack frame */ - size_t stackstart; /* Beginning of next args */ - size_t stacktop; /* Top of stack. Where values are pushed and popped from. */ - size_t capacity; /* How big is the stack memory */ - size_t maxstack; /* Arbitrary defined limit for stack overflow */ + int32_t frame; /* Index of the stack frame */ + int32_t stackstart; /* Beginning of next args */ + int32_t stacktop; /* Top of stack. Where values are pushed and popped from. */ + int32_t capacity; /* How big is the stack memory */ + int32_t maxstack; /* Arbitrary defined limit for stack overflow */ JanetTable *env; /* Dynamic bindings table (usually current environment). */ Janet *data; /* Dynamically resized stack memory */ JanetFiber *child; /* Keep linked list of fibers for restarting pending fibers */ @@ -1734,7 +1738,7 @@ JANET_API JanetTable *janet_table_clone(JanetTable *table); JANET_API void janet_table_clear(JanetTable *table); /* Fiber */ -JANET_API JanetFiber *janet_fiber(JanetFunction *callee, size_t capacity, int32_t argc, const Janet *argv); +JANET_API JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv); JANET_API JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv); JANET_API JanetFiberStatus janet_fiber_status(JanetFiber *fiber); JANET_API int janet_fiber_can_resume(JanetFiber *fiber);