2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-11-01 21:53:43 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
#include <string.h>
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2018-04-30 00:13:04 +00:00
|
|
|
/* Initializes an array */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *janet_array_init(JanetArray *array, int32_t capacity) {
|
|
|
|
Janet *data = NULL;
|
2017-12-17 04:11:51 +00:00
|
|
|
if (capacity > 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
data = (Janet *) malloc(sizeof(Janet) * capacity);
|
2017-12-17 04:11:51 +00:00
|
|
|
if (NULL == data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->count = 0;
|
|
|
|
array->capacity = capacity;
|
|
|
|
array->data = data;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_deinit(JanetArray *array) {
|
2017-11-06 03:05:47 +00:00
|
|
|
free(array->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a new array */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *janet_array(int32_t capacity) {
|
|
|
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
|
|
|
return janet_array_init(array, capacity);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Creates a new array from n elements. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *janet_array_n(const Janet *elements, int32_t n) {
|
|
|
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
2018-07-04 03:07:35 +00:00
|
|
|
array->capacity = n;
|
|
|
|
array->count = n;
|
2018-09-06 02:18:42 +00:00
|
|
|
array->data = malloc(sizeof(Janet) * n);
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!array->data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
memcpy(array->data, elements, sizeof(Janet) * n);
|
2018-07-04 03:07:35 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Ensure the array has enough capacity for elements */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_ensure(JanetArray *array, int32_t capacity) {
|
|
|
|
Janet *newData;
|
|
|
|
Janet *old = array->data;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (capacity <= array->capacity) return;
|
2018-09-06 02:18:42 +00:00
|
|
|
newData = realloc(old, capacity * sizeof(Janet));
|
2017-11-01 21:53:43 +00:00
|
|
|
if (NULL == newData) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->data = newData;
|
|
|
|
array->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the count of an array. Extend with nil if needed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_setcount(JanetArray *array, int32_t count) {
|
2017-11-29 20:17:56 +00:00
|
|
|
if (count < 0)
|
|
|
|
return;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (count > array->count) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_ensure(array, count);
|
2018-01-05 21:17:55 +00:00
|
|
|
for (i = array->count; i < count; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
array->data[i] = janet_wrap_nil();
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
array->count = count;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a value to the top of the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_push(JanetArray *array, Janet x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newcount = array->count + 1;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (newcount >= array->capacity) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_ensure(array, newcount * 2);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->data[array->count] = x;
|
|
|
|
array->count = newcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop a value from the top of the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_array_pop(JanetArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[--array->count];
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the last value in the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_array_peek(JanetArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[array->count - 1];
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
|
|
|
/* C Functions */
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_new(JanetArgs args) {
|
2018-05-09 21:01:58 +00:00
|
|
|
int32_t cap;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array;
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_INTEGER(cap, args, 0);
|
|
|
|
array = janet_array(cap);
|
|
|
|
JANET_RETURN_ARRAY(args, array);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_pop(JanetArgs args) {
|
|
|
|
JanetArray *array;
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
|
|
|
JANET_RETURN(args, janet_array_pop(array));
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_peek(JanetArgs args) {
|
|
|
|
JanetArray *array;
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
|
|
|
JANET_RETURN(args, janet_array_peek(array));
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_push(JanetArgs args) {
|
|
|
|
JanetArray *array;
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t newcount;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
2018-01-18 22:25:45 +00:00
|
|
|
newcount = array->count - 1 + args.n;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_ensure(array, newcount);
|
|
|
|
if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Janet));
|
2018-01-18 22:25:45 +00:00
|
|
|
array->count = newcount;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_setcount(JanetArgs args) {
|
|
|
|
JanetArray *array;
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t newcount;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 2);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
|
|
|
JANET_ARG_INTEGER(newcount, args, 1);
|
|
|
|
if (newcount < 0) JANET_THROW(args, "expected positive integer");
|
|
|
|
janet_array_setcount(array, newcount);
|
|
|
|
JANET_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_ensure(JanetArgs args) {
|
|
|
|
JanetArray *array;
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t newcount;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 2);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
|
|
|
JANET_ARG_INTEGER(newcount, args, 1);
|
|
|
|
if (newcount < 0) JANET_THROW(args, "expected positive integer");
|
|
|
|
janet_array_ensure(array, newcount);
|
|
|
|
JANET_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_slice(JanetArgs args) {
|
|
|
|
const Janet *vals;
|
2018-01-27 20:15:09 +00:00
|
|
|
int32_t len;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *ret;
|
2018-01-27 20:15:09 +00:00
|
|
|
int32_t start, end;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 3);
|
|
|
|
if (!janet_indexed_view(args.v[0], &vals, &len))
|
|
|
|
JANET_THROW(args, "expected array|tuple");
|
2018-01-27 20:15:09 +00:00
|
|
|
/* Get start */
|
|
|
|
if (args.n < 2) {
|
|
|
|
start = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(args.v[1], JANET_INTEGER)) {
|
|
|
|
start = janet_unwrap_integer(args.v[1]);
|
2018-01-27 20:15:09 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "expected integer");
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
/* Get end */
|
|
|
|
if (args.n < 3) {
|
|
|
|
end = -1;
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(args.v[2], JANET_INTEGER)) {
|
|
|
|
end = janet_unwrap_integer(args.v[2]);
|
2018-01-27 20:15:09 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "expected integer");
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
if (start < 0) start = len + start;
|
|
|
|
if (end < 0) end = len + end + 1;
|
|
|
|
if (end >= start) {
|
|
|
|
int32_t i, j;
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janet_array(end - start);
|
2018-01-27 20:15:09 +00:00
|
|
|
for (j = 0, i = start; i < end; j++, i++) {
|
|
|
|
ret->data[j] = vals[i];
|
|
|
|
}
|
|
|
|
ret->count = j;
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janet_array(0);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_ARRAY(args, ret);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_concat(JanetArgs args) {
|
2018-03-12 04:26:13 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array;
|
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_ARG_ARRAY(array, args, 0);
|
2018-03-12 04:26:13 +00:00
|
|
|
for (i = 1; i < args.n; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
switch (janet_type(args.v[i])) {
|
2018-03-12 04:26:13 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_push(array, args.v[i]);
|
2018-03-12 04:26:13 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_ARRAY:
|
|
|
|
case JANET_TUPLE:
|
2018-03-12 04:26:13 +00:00
|
|
|
{
|
2018-05-20 01:16:00 +00:00
|
|
|
int32_t j, len;
|
2018-09-06 02:18:42 +00:00
|
|
|
const Janet *vals;
|
|
|
|
janet_indexed_view(args.v[i], &vals, &len);
|
2018-03-12 04:26:13 +00:00
|
|
|
for (j = 0; j < len; j++)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_push(array, vals[j]);
|
2018-03-12 04:26:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_ARRAY(args, array);
|
2018-03-12 04:26:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2018-05-09 21:01:58 +00:00
|
|
|
{"array.new", cfun_new},
|
2018-05-08 23:40:28 +00:00
|
|
|
{"array.pop", cfun_pop},
|
|
|
|
{"array.peek", cfun_peek},
|
|
|
|
{"array.push", cfun_push},
|
|
|
|
{"array.setcount", cfun_setcount},
|
|
|
|
{"array.ensure", cfun_ensure},
|
|
|
|
{"array.slice", cfun_slice},
|
|
|
|
{"array.concat", cfun_concat},
|
2018-05-01 15:06:31 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Load the array module */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_lib_array(JanetArgs args) {
|
|
|
|
JanetTable *env = janet_env(args);
|
|
|
|
janet_cfuns(env, NULL, cfuns);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|