Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use size_t for Janet types length and capacity and the API those types use #1438

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ee8a169
refactor(c-api): Move `JanetArray` to use `size_t` on `count` and `ca…
GrayJack Apr 15, 2024
f077af8
refactor(c-api): Move `JanetBuffer` to use `size_t` on `count` and `c…
GrayJack Apr 15, 2024
88765ee
refactor(c-api): Use `size_t` on some `JanetTable` fields
GrayJack Apr 15, 2024
a58fc4c
refactor(c-api): Update header
GrayJack Apr 15, 2024
7b0e5e3
refactor(c-api): Use `size_t` on some `JanetTupleHead` fields
GrayJack Apr 15, 2024
ed800bd
refactor(c-api): Use `size_t` on some `JanetStringHead` fields
GrayJack Apr 15, 2024
b3010e6
refactor(c-api): Use `size_t` on some `JanetStructHead` fields
GrayJack Apr 15, 2024
af872a4
refactor(c-api): Use `size_t` on some `JanetFiber` fields
GrayJack Apr 15, 2024
3f4f71c
refactor(c-api): Use `size_t` in other structures and functions used…
GrayJack Apr 16, 2024
5991e48
refactor(c-api): backfit the changes in the API functions on the plac…
GrayJack Apr 16, 2024
e08e3ba
fix: Gcc found some extra warnings
GrayJack Apr 16, 2024
3be71dc
refactor(c-api): Remove unneded checks due to changes to use size_t
GrayJack Apr 16, 2024
46bf5d1
fix: Fix tests warnings on Mingw
GrayJack Apr 16, 2024
3f54b28
feat(c-api): Introduce ssize_t and limits constants for `size_t` as `…
GrayJack Apr 16, 2024
b483c9e
refactor(c-api): Dogfooding usage of size types and limits
GrayJack Apr 16, 2024
c5af2a9
fix: remove dead code
GrayJack Apr 16, 2024
8d35740
fix(c-api): Fix the limits for size on 32bit platforms
GrayJack Apr 16, 2024
a3e5afd
refactor(c-api): Make JanetRange use `size_t` as well
GrayJack Apr 17, 2024
3558237
refactor(c-api): Make `JanetQueue` use `size_t`
GrayJack Apr 17, 2024
2d17e08
refactor(c-api): Missed a few function of `JanetFiber`
GrayJack Apr 17, 2024
ef04bf5
refactor(c-api): Use `size_t` on `janet_cstrcmp`
GrayJack Apr 18, 2024
09c384a
refactor(c-api): Marshal/Unmarshal fiber size_t fields
GrayJack Apr 18, 2024
6eb371e
fix: Fix some windows warnings
GrayJack Apr 18, 2024
a53921b
fix: Fix another windows warning
GrayJack Apr 18, 2024
1baa045
refactor(c-api): Handle the changes in the structures into {un}marsha…
GrayJack Apr 19, 2024
d0acf64
refactor(c-api): Requested changes from review
GrayJack Apr 21, 2024
6e82123
refactor(c-api): Check for overflow on `janet_struct_begin`
GrayJack Apr 22, 2024
421f7a2
Update src/core/string.c
bakpakin May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/numarray/numarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void num_array_put(void *p, Janet key, Janet value) {
if (!janet_checktype(value, JANET_NUMBER))
janet_panic("expected number value");

index = (size_t)janet_unwrap_integer(key);
index = janet_unwrap_size(key);
if (index < array->size) {
array->data[index] = janet_unwrap_number(value);
}
Expand All @@ -96,7 +96,7 @@ int num_array_get(void *p, Janet key, Janet *out) {
return janet_getmethod(janet_unwrap_keyword(key), methods, out);
if (!janet_checkint(key))
janet_panic("expected integer key");
index = (size_t)janet_unwrap_integer(key);
index = janet_unwrap_size(key);
if (index >= array->size) {
return 0;
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/boot/array_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "tests.h"

int array_test() {

int i;
JanetArray *array1, *array2;

array1 = janet_array(10);
Expand All @@ -53,7 +51,7 @@ int array_test() {
janet_array_push(array2, janet_cstringv("six"));
janet_array_push(array2, janet_cstringv("seven"));

for (i = 0; i < array2->count; i++) {
for (size_t i = 0; i < array2->count; i++) {
assert(janet_equals(array1->data[i], array2->data[i]));
}

Expand Down
2 changes: 1 addition & 1 deletion src/boot/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 1 addition & 3 deletions src/boot/buffer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "tests.h"

int buffer_test() {

int i;
JanetBuffer *buffer1, *buffer2;

buffer1 = janet_buffer(100);
Expand All @@ -54,7 +52,7 @@ int buffer_test() {
assert(buffer1->capacity >= buffer1->count);
assert(buffer2->capacity >= buffer2->count);

for (i = 0; i < buffer1->count; i++) {
for (size_t i = 0; i < buffer1->count; i++) {
assert(buffer1->data[i] == buffer2->data[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/boot/number_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
73 changes: 34 additions & 39 deletions src/core/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

#include <string.h>

static void janet_array_impl(JanetArray *array, int32_t capacity) {
static void janet_array_impl(JanetArray *array, size_t capacity) {
Janet *data = NULL;
if (capacity > 0) {
janet_vm.next_collection += capacity * sizeof(Janet);
data = (Janet *) janet_malloc(sizeof(Janet) * (size_t) capacity);
data = (Janet *) janet_malloc(sizeof(Janet) * capacity);
if (NULL == data) {
JANET_OUT_OF_MEMORY;
}
Expand All @@ -45,25 +45,25 @@ static void janet_array_impl(JanetArray *array, int32_t capacity) {
}

/* Creates a new array */
JanetArray *janet_array(int32_t capacity) {
JanetArray *janet_array(size_t capacity) {
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
janet_array_impl(array, capacity);
return array;
}

/* Creates a new array with weak references */
JanetArray *janet_array_weak(int32_t capacity) {
JanetArray *janet_array_weak(size_t capacity) {
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY_WEAK, sizeof(JanetArray));
janet_array_impl(array, capacity);
return array;
}

/* Creates a new array from n elements. */
JanetArray *janet_array_n(const Janet *elements, int32_t n) {
JanetArray *janet_array_n(const Janet *elements, size_t n) {
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
array->capacity = n;
array->count = n;
array->data = janet_malloc(sizeof(Janet) * (size_t) n);
array->data = janet_malloc(sizeof(Janet) * n);
if (!array->data) {
JANET_OUT_OF_MEMORY;
}
Expand All @@ -72,13 +72,13 @@ JanetArray *janet_array_n(const Janet *elements, int32_t n) {
}

/* Ensure the array has enough capacity for elements */
void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) {
void janet_array_ensure(JanetArray *array, size_t capacity, size_t growth) {
Janet *newData;
Janet *old = array->data;
if (capacity <= array->capacity) return;
int64_t new_capacity = ((int64_t) capacity) * growth;
if (new_capacity > INT32_MAX) new_capacity = INT32_MAX;
capacity = (int32_t) new_capacity;
size_t new_capacity = (capacity) * growth;
GrayJack marked this conversation as resolved.
Show resolved Hide resolved
if (new_capacity > JANET_INTMAX_SIZE) new_capacity = JANET_INTMAX_SIZE;
capacity = new_capacity;
newData = janet_realloc(old, capacity * sizeof(Janet));
if (NULL == newData) {
JANET_OUT_OF_MEMORY;
Expand All @@ -89,11 +89,9 @@ void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) {
}

/* Set the count of an array. Extend with nil if needed. */
void janet_array_setcount(JanetArray *array, int32_t count) {
if (count < 0)
return;
void janet_array_setcount(JanetArray *array, size_t count) {
if (count > array->count) {
int32_t i;
size_t i;
janet_array_ensure(array, count, 1);
for (i = array->count; i < count; i++) {
array->data[i] = janet_wrap_nil();
Expand All @@ -104,10 +102,10 @@ void janet_array_setcount(JanetArray *array, int32_t count) {

/* Push a value to the top of the array */
void janet_array_push(JanetArray *array, Janet x) {
if (array->count == INT32_MAX) {
if (array->count == JANET_INTMAX_SIZE) {
janet_panic("array overflow");
}
int32_t newcount = array->count + 1;
size_t newcount = array->count + 1;
janet_array_ensure(array, newcount, 2);
array->data[array->count] = x;
array->count = newcount;
Expand Down Expand Up @@ -138,7 +136,7 @@ JANET_CORE_FN(cfun_array_new,
"Creates a new empty array with a pre-allocated capacity. The same as "
"`(array)` but can be more efficient if the maximum size of an array is known.") {
janet_fixarity(argc, 1);
int32_t cap = janet_getinteger(argv, 0);
size_t cap = janet_getsize(argv, 0);
JanetArray *array = janet_array(cap);
return janet_wrap_array(array);
}
Expand All @@ -147,7 +145,7 @@ JANET_CORE_FN(cfun_array_weak,
"(array/weak capacity)",
"Creates a new empty array with a pre-allocated capacity and support for weak references. Similar to `array/new`.") {
janet_fixarity(argc, 1);
int32_t cap = janet_getinteger(argv, 0);
size_t cap = janet_getsize(argv, 0);
JanetArray *array = janet_array_weak(cap);
return janet_wrap_array(array);
}
Expand All @@ -156,10 +154,10 @@ JANET_CORE_FN(cfun_array_new_filled,
"(array/new-filled count &opt value)",
"Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array.") {
janet_arity(argc, 1, 2);
int32_t count = janet_getnat(argv, 0);
size_t count = janet_getsize(argv, 0);
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
JanetArray *array = janet_array(count);
for (int32_t i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++) {
array->data[i] = x;
}
array->count = count;
Expand All @@ -173,7 +171,7 @@ JANET_CORE_FN(cfun_array_fill,
janet_arity(argc, 1, 2);
JanetArray *array = janet_getarray(argv, 0);
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
for (int32_t i = 0; i < array->count; i++) {
for (size_t i = 0; i < array->count; i++) {
array->data[i] = x;
}
return argv[0];
Expand Down Expand Up @@ -201,10 +199,10 @@ JANET_CORE_FN(cfun_array_push,
"Push all the elements of xs to the end of an array. Modifies the input array and returns it.") {
janet_arity(argc, 1, -1);
JanetArray *array = janet_getarray(argv, 0);
if (INT32_MAX - argc + 1 <= array->count) {
if ((size_t) JANET_INTMAX_SIZE - argc + 1 <= array->count) {
janet_panic("array overflow");
}
int32_t newcount = array->count - 1 + argc;
size_t newcount = array->count - 1 + argc;
janet_array_ensure(array, newcount, 2);
if (argc > 1) memcpy(array->data + array->count, argv + 1, (size_t)(argc - 1) * sizeof(Janet));
array->count = newcount;
Expand All @@ -219,8 +217,8 @@ JANET_CORE_FN(cfun_array_ensure,
"Otherwise, the backing memory will be reallocated so that there is enough space.") {
janet_fixarity(argc, 3);
JanetArray *array = janet_getarray(argv, 0);
int32_t newcount = janet_getinteger(argv, 1);
int32_t growth = janet_getinteger(argv, 2);
size_t newcount = janet_getsize(argv, 1);
size_t growth = janet_getsize(argv, 2);
if (newcount < 1) janet_panic("expected positive integer");
janet_array_ensure(array, newcount, growth);
return argv[0];
Expand Down Expand Up @@ -248,21 +246,20 @@ JANET_CORE_FN(cfun_array_concat,
"which must be an array. If any of the parts are arrays or tuples, their elements will "
"be inserted into the array. Otherwise, each part in `parts` will be appended to `arr` in order. "
"Return the modified array `arr`.") {
int32_t i;
janet_arity(argc, 1, -1);
JanetArray *array = janet_getarray(argv, 0);
for (i = 1; i < argc; i++) {
for (int32_t i = 1; i < argc; i++) {
switch (janet_type(argv[i])) {
default:
janet_array_push(array, argv[i]);
break;
case JANET_ARRAY:
case JANET_TUPLE: {
int32_t j, len = 0;
size_t j, len = 0;
const Janet *vals = NULL;
janet_indexed_view(argv[i], &vals, &len);
if (array->data == vals) {
int32_t newcount = array->count + len;
size_t newcount = array->count + len;
janet_array_ensure(array, newcount, 2);
janet_indexed_view(argv[i], &vals, &len);
}
Expand All @@ -284,15 +281,15 @@ JANET_CORE_FN(cfun_array_insert,
size_t chunksize, restsize;
janet_arity(argc, 2, -1);
JanetArray *array = janet_getarray(argv, 0);
int32_t at = janet_getinteger(argv, 1);
ssize_t at = janet_getssize(argv, 1);
if (at < 0) {
at = array->count + at + 1;
}
if (at < 0 || at > array->count)
janet_panicf("insertion index %d out of range [0,%d]", at, array->count);
if (at < 0 || (size_t) at > array->count)
bakpakin marked this conversation as resolved.
Show resolved Hide resolved
janet_panicf("insertion index %d out of range [0,%d]", at, array->count);
chunksize = (argc - 2) * sizeof(Janet);
restsize = (array->count - at) * sizeof(Janet);
if (INT32_MAX - (argc - 2) < array->count) {
if ((size_t) JANET_INTMAX_SIZE - (argc - 2) < array->count) {
janet_panic("array overflow");
}
janet_array_ensure(array, array->count + argc - 2, 2);
Expand All @@ -314,17 +311,15 @@ JANET_CORE_FN(cfun_array_remove,
"Returns the array.") {
janet_arity(argc, 2, 3);
JanetArray *array = janet_getarray(argv, 0);
int32_t at = janet_getinteger(argv, 1);
int32_t n = 1;
ssize_t at = janet_getssize(argv, 1);
size_t n = 1;
if (at < 0) {
at = array->count + at;
}
if (at < 0 || at > array->count)
if (at < 0 || (size_t) at > array->count)
janet_panicf("removal index %d out of range [0,%d]", at, array->count);
if (argc == 3) {
n = janet_getinteger(argv, 2);
if (n < 0)
janet_panicf("expected non-negative integer for argument n, got %v", argv[2]);
n = janet_getsize(argv, 2);
}
if (at + n > array->count) {
n = array->count - at;
Expand Down
16 changes: 7 additions & 9 deletions src/core/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
case JANET_TUPLE: {
const Janet *t = janet_unwrap_tuple(x);
if (argtype == JANET_OAT_TYPE) {
int32_t i = 0;
size_t i = 0;
ret = 0;
for (i = 0; i < janet_tuple_length(t); i++) {
ret |= doarg_1(a, JANET_OAT_SIMPLETYPE, t[i]);
Expand Down Expand Up @@ -492,7 +492,7 @@
JanetAssembler a;
Janet s = source;
JanetFuncDef *def;
int32_t count, i;
size_t count, i;
const Janet *arr;
Janet x;
(void) flags;
Expand Down Expand Up @@ -578,8 +578,7 @@
Janet v = arr[i];
if (janet_checktype(v, JANET_TUPLE)) {
const Janet *t = janet_unwrap_tuple(v);
int32_t j;
for (j = 0; j < janet_tuple_length(t); j++) {
for (size_t j = 0; j < janet_tuple_length(t); j++) {
if (!janet_checktype(t[j], JANET_SYMBOL))
janet_asm_error(&a, "slot names must be symbols");
janet_table_put(&a.slots, t[j], janet_wrap_integer(i));
Expand All @@ -596,7 +595,7 @@
x = janet_get1(s, janet_ckeywordv("constants"));
if (janet_indexed_view(x, &arr, &count)) {
def->constants_length = count;
def->constants = janet_malloc(sizeof(Janet) * (size_t) count);
def->constants = janet_malloc(sizeof(Janet) * count);
if (NULL == def->constants) {
JANET_OUT_OF_MEMORY;
}
Expand All @@ -615,8 +614,7 @@
x = janet_get1(s, janet_ckeywordv("defs"));
}
if (janet_indexed_view(x, &arr, &count)) {
int32_t i;
for (i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++) {
GrayJack marked this conversation as resolved.
Show resolved Hide resolved
JanetAssembleResult subres;
Janet subname;
int32_t newlen;
Expand Down Expand Up @@ -701,7 +699,7 @@
/* Check for source mapping */
x = janet_get1(s, janet_ckeywordv("sourcemap"));
if (janet_indexed_view(x, &arr, &count)) {
janet_asm_assert(&a, count == def->bytecode_length, "sourcemap must have the same length as the bytecode");
janet_asm_assert(&a, count == (size_t) def->bytecode_length, "sourcemap must have the same length as the bytecode");
def->sourcemap = janet_malloc(sizeof(JanetSourceMapping) * (size_t) count);
if (NULL == def->sourcemap) {
JANET_OUT_OF_MEMORY;
Expand Down Expand Up @@ -775,7 +773,7 @@
if (def->environments_length) {
def->environments = janet_realloc(def->environments, def->environments_length * sizeof(int32_t));
}
for (int32_t i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++) {
GrayJack marked this conversation as resolved.
Show resolved Hide resolved
if (!janet_checkint(arr[i])) {
janet_asm_error(&a, "expected integer");
}
Expand Down
Loading
Loading