mirror of
https://github.com/janet-lang/janet
synced 2024-11-14 12:44:49 +00:00
90 lines
3.0 KiB
C
90 lines
3.0 KiB
C
|
/*
|
||
|
* Copyright (c) 2017 Calvin Rose
|
||
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
#include "internal.h"
|
||
|
#include <dst/dst.h>
|
||
|
|
||
|
/* Iniializes an array. Assumes the pointer to the array is already on the stack. */
|
||
|
DstArray *dst_array(Dst *vm, uint32_t capacity) {
|
||
|
DstArray *array = dst_alloc(vm, DST_MEMORY_ARRAY, sizeof(DstArray));
|
||
|
DstValue *data = (DstValue *) malloc(sizeof(DstValue) * capacity);
|
||
|
if (NULL == array || NULL == data) {
|
||
|
DST_OUT_OF_MEMORY;
|
||
|
}
|
||
|
array->count = 0;
|
||
|
array->capacity = capacity;
|
||
|
array->data = data;
|
||
|
return array;
|
||
|
}
|
||
|
|
||
|
/* Ensure the array has enough capacity for elements */
|
||
|
void dst_array_ensure(Dst *vm, DstArray *array, uint32_t capacity) {
|
||
|
DstValue *newData;
|
||
|
DstValue *old = array->data;
|
||
|
if (capacity <= array->capacity) return;
|
||
|
newData = realloc(old, capacity * sizeof(DstValue));
|
||
|
if (NULL == newData) {
|
||
|
DST_OUT_OF_MEMORY;
|
||
|
}
|
||
|
array->data = newData;
|
||
|
array->capacity = capacity;
|
||
|
}
|
||
|
|
||
|
/* Set the count of an array. Extend with nil if needed. */
|
||
|
void dst_array_setcount(Dst *vm, DstArray *array, uint32_t count) {
|
||
|
if (count > array->count) {
|
||
|
uint32_t i;
|
||
|
dst_array_ensure(vm, array, count + 1);
|
||
|
for (i = array->count; i < count; ++i)
|
||
|
array->data[i].type = DST_NIL;
|
||
|
}
|
||
|
array->count = count;
|
||
|
}
|
||
|
|
||
|
/* Push a value to the top of the array */
|
||
|
void dst_array_push(Dst *vm, DstArray *array, DstValue x) {
|
||
|
uint32_t newcount = array->count + 1;
|
||
|
if (newcount >= array->capacity) }
|
||
|
dst_array_ensure(vm, array, newcount * 2);
|
||
|
}
|
||
|
array->data[array->count] = x;
|
||
|
array->count = newcount;
|
||
|
}
|
||
|
|
||
|
/* Pop a value from the top of the array */
|
||
|
DstValue dst_array_pop(DstArray *array) {
|
||
|
if (array->count) {
|
||
|
return array->data[--array->count];
|
||
|
} else {
|
||
|
return dst_wrap_nil();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Look at the last value in the array */
|
||
|
DstValue dst_array_peek(DstArray *array) {
|
||
|
if (array->count) {
|
||
|
return array->data[array->count - 1];
|
||
|
} else {
|
||
|
return dst_wrap_nil();
|
||
|
}
|
||
|
}
|