diff --git a/src/core/array.c b/src/core/array.c index faddab86..cbfb6bd7 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -63,10 +63,11 @@ 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) { +void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) { Janet *newData; Janet *old = array->data; if (capacity <= array->capacity) return; + capacity *= growth; newData = realloc(old, capacity * sizeof(Janet)); if (NULL == newData) { JANET_OUT_OF_MEMORY; @@ -81,7 +82,7 @@ void janet_array_setcount(JanetArray *array, int32_t count) { return; if (count > array->count) { int32_t i; - janet_array_ensure(array, count); + janet_array_ensure(array, count, 1); for (i = array->count; i < count; i++) { array->data[i] = janet_wrap_nil(); } @@ -92,9 +93,7 @@ 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) { int32_t newcount = array->count + 1; - if (newcount >= array->capacity) { - janet_array_ensure(array, newcount * 2); - } + janet_array_ensure(array, newcount, 2); array->data[array->count] = x; array->count = newcount; } @@ -148,7 +147,7 @@ static int cfun_push(JanetArgs args) { JANET_MINARITY(args, 1); JANET_ARG_ARRAY(array, args, 0); newcount = array->count - 1 + args.n; - janet_array_ensure(array, newcount); + janet_array_ensure(array, newcount, 2); if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Janet)); array->count = newcount; JANET_RETURN(args, args.v[0]); @@ -168,11 +167,13 @@ static int cfun_setcount(JanetArgs args) { static int cfun_ensure(JanetArgs args) { JanetArray *array; int32_t newcount; - JANET_FIXARITY(args, 2); + int32_t growth; + JANET_FIXARITY(args, 3); JANET_ARG_ARRAY(array, args, 0); JANET_ARG_INTEGER(newcount, args, 1); + JANET_ARG_INTEGER(growth, args, 2); if (newcount < 0) JANET_THROW(args, "expected positive integer"); - janet_array_ensure(array, newcount); + janet_array_ensure(array, newcount, growth); JANET_RETURN(args, args.v[0]); } diff --git a/src/core/buffer.c b/src/core/buffer.c index 909d7d7d..5556cc36 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -50,10 +50,11 @@ JanetBuffer *janet_buffer(int32_t capacity) { } /* Ensure that the buffer has enough internal capacity */ -void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity) { +void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth) { uint8_t *new_data; uint8_t *old = buffer->data; if (capacity <= buffer->capacity) return; + capacity *= growth; new_data = realloc(old, capacity * sizeof(uint8_t)); if (NULL == new_data) { JANET_OUT_OF_MEMORY; @@ -68,7 +69,7 @@ void janet_buffer_setcount(JanetBuffer *buffer, int32_t count) { return; if (count > buffer->count) { int32_t oldcount = buffer->count; - janet_buffer_ensure(buffer, count); + janet_buffer_ensure(buffer, count, 1); memset(buffer->data + oldcount, 0, count - oldcount); } buffer->count = count; diff --git a/src/core/core.janet b/src/core/core.janet index 5b3410d7..d7a50bb8 100644 --- a/src/core/core.janet +++ b/src/core/core.janet @@ -864,7 +864,7 @@ value, one key will be ignored." (def complex? (> (length y) 4)) ((if complex? pp-dict-nested pp-dict-simple) y)) - (def printers + (def printers {:array (fn [y] (do-ds y "@[" "]" true pp-seq)) :tuple (fn [y] (do-ds y "(" ")" false pp-seq)) :table (fn [y] (do-ds y "@{" "}" true pp-dict)) @@ -902,8 +902,8 @@ value, one key will be ignored." (defn expand-bindings [x] (case (type x) - :array (map expand-bindings x) - :tuple (tuple.slice (map expand-bindings x) 0) + :array (mapa expand-bindings x) + :tuple (map expand-bindings x) :table (dotable x expand-bindings) :struct (table.to-struct (dotable x expand-bindings)) (macroexpand-1 x))) @@ -916,11 +916,11 @@ value, one key will be ignored." @[(expand-bindings last2) (macroexpand-1 last)]) 0)) (defn expandall [t] - (def args (map macroexpand-1 (tuple.slice t 1))) + (def args (mapa macroexpand-1 (tuple.slice t 1))) (apply tuple (get t 0) args)) (defn expandfn [t] - (def args (map macroexpand-1 (tuple.slice t 2))) + (def args (mapa macroexpand-1 (tuple.slice t 2))) (apply tuple 'fn (get t 1) args)) (def specs @@ -942,12 +942,12 @@ value, one key will be ignored." (cond s (s t) m? (apply m (tuple.slice t 1)) - (tuple.slice (map macroexpand-1 t) 0))) + (map macroexpand-1 t) 0)) (def ret (case (type x) :tuple (dotup x) - :array (map macroexpand-1 x) + :array (mapa macroexpand-1 x) :struct (table.to-struct (dotable x macroexpand-1)) :table (dotable x macroexpand-1) x)) @@ -1001,7 +1001,7 @@ value, one key will be ignored." ### ### -(defn make-env +(defn make-env @[parent] (def parent (if parent parent _env)) (def newenv (table.setproto @{} parent)) @@ -1166,7 +1166,7 @@ value, one key will be ignored." (string syspath janet.version "/?/??.so") (string syspath "/?.so") (string syspath "/?/??.so")])) - + (if (= :windows (os.which)) (loop [i :range [0 (length module.native-paths)]] (def x (get module.native-paths i)) diff --git a/src/core/string.c b/src/core/string.c index b89da70e..485e8ca1 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -112,7 +112,7 @@ static int32_t real_to_string_impl(uint8_t *buf, double x) { } static void real_to_string_b(JanetBuffer *buffer, double x) { - janet_buffer_ensure(buffer, buffer->count + BUFSIZE); + janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2); buffer->count += real_to_string_impl(buffer->data + buffer->count, x); } @@ -202,7 +202,7 @@ static int32_t string_description_impl(uint8_t *buf, const char *title, void *po } static void string_description_b(JanetBuffer *buffer, const char *title, void *pointer) { - janet_buffer_ensure(buffer, buffer->count + BUFSIZE); + janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2); buffer->count += string_description_impl(buffer->data + buffer->count, title, pointer); } diff --git a/src/core/vm.c b/src/core/vm.c index 11f95c30..3ac99e2a 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -906,20 +906,26 @@ static void *op_lookup[255] = { retreg = ds; goto vm_type_error; case JANET_ARRAY: - if (index >= janet_unwrap_array(ds)->count) { - janet_array_ensure(janet_unwrap_array(ds), 2 * index); - janet_unwrap_array(ds)->count = index + 1; + { + JanetArray *array = janet_unwrap_array(ds); + if (index >= array->count) { + janet_array_ensure(array, index + 1, 2); + array->count = index + 1; + } + array->data[index] = value; + break; } - janet_unwrap_array(ds)->data[index] = value; - break; case JANET_BUFFER: - vm_assert_type(value, JANET_INTEGER); - if (index >= janet_unwrap_buffer(ds)->count) { - janet_buffer_ensure(janet_unwrap_buffer(ds), 2 * index); - janet_unwrap_buffer(ds)->count = index + 1; + { + JanetBuffer *buffer = janet_unwrap_buffer(ds); + vm_assert_type(value, JANET_INTEGER); + if (index >= buffer->count) { + janet_buffer_ensure(buffer, index + 1, 2); + buffer->count = index + 1; + } + buffer->data[index] = janet_unwrap_integer(value); + break; } - janet_unwrap_buffer(ds)->data[index] = janet_unwrap_integer(value); - break; } ++pc; vm_checkgc_next(); diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index 4c360370..1a1899ff 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -916,7 +916,7 @@ JANET_API JanetArray *janet_array(int32_t capacity); JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n); JANET_API JanetArray *janet_array_init(JanetArray *array, int32_t capacity); JANET_API void janet_array_deinit(JanetArray *array); -JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity); +JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth); JANET_API void janet_array_setcount(JanetArray *array, int32_t count); JANET_API void janet_array_push(JanetArray *array, Janet x); JANET_API Janet janet_array_pop(JanetArray *array); @@ -926,7 +926,7 @@ JANET_API Janet janet_array_peek(JanetArray *array); JANET_API JanetBuffer *janet_buffer(int32_t capacity); JANET_API JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity); JANET_API void janet_buffer_deinit(JanetBuffer *buffer); -JANET_API void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity); +JANET_API void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth); JANET_API void janet_buffer_setcount(JanetBuffer *buffer, int32_t count); JANET_API int janet_buffer_extra(JanetBuffer *buffer, int32_t n); JANET_API int janet_buffer_push_bytes(JanetBuffer *buffer, const uint8_t *string, int32_t len); @@ -1185,7 +1185,7 @@ JANET_API int janet_typeabstract_err(JanetArgs args, int32_t n, const JanetAbstr #define JANET_ARG_ARRAY(DEST, A, N) _JANET_ARG(JANET_ARRAY, array, DEST, A, N) #define JANET_ARG_TUPLE(DEST, A, N) _JANET_ARG(JANET_TUPLE, tuple, DEST, A, N) #define JANET_ARG_TABLE(DEST, A, N) _JANET_ARG(JANET_TABLE, table, DEST, A, N) -#define JANET_ARG_STRUCT(DEST, A, N) _JANET_ARG(JANET_STRUCT, st, DEST, A, N) +#define JANET_ARG_STRUCT(DEST, A, N) _JANET_ARG(JANET_STRUCT, struct, DEST, A, N) #define JANET_ARG_BUFFER(DEST, A, N) _JANET_ARG(JANET_BUFFER, buffer, DEST, A, N) #define JANET_ARG_FUNCTION(DEST, A, N) _JANET_ARG(JANET_FUNCTION, function, DEST, A, N) #define JANET_ARG_CFUNCTION(DEST, A, N) _JANET_ARG(JANET_CFUNCTION, cfunction, DEST, A, N) diff --git a/src/mainclient/line.c b/src/mainclient/line.c index b04d17f7..23463f21 100644 --- a/src/mainclient/line.c +++ b/src/mainclient/line.c @@ -465,7 +465,7 @@ void janet_line_get(const uint8_t *p, JanetBuffer *buffer) { } norawmode(); fputc('\n', stdout); - janet_buffer_ensure(buffer, len + 1); + janet_buffer_ensure(buffer, len + 1, 2); memcpy(buffer->data, buf, len); buffer->data[len] = '\n'; buffer->count = len + 1;