2017-03-01 01:20:29 +00:00
|
|
|
#include "util.h"
|
2017-02-09 23:50:47 +00:00
|
|
|
#include "ds.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include "vm.h"
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Buffer functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Create a new Buffer */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstBuffer *gst_buffer(Gst *vm, uint32_t capacity) {
|
|
|
|
GstBuffer *buffer = gst_alloc(vm, sizeof(GstBuffer));
|
|
|
|
uint8_t *data = gst_alloc(vm, sizeof(uint8_t) * capacity);
|
2017-02-09 23:50:47 +00:00
|
|
|
buffer->data = data;
|
|
|
|
buffer->count = 0;
|
|
|
|
buffer->capacity = capacity;
|
2017-02-15 01:45:34 +00:00
|
|
|
buffer->flags = 0;
|
2017-02-09 23:50:47 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the buffer has enough internal capacity */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_buffer_ensure(Gst *vm, GstBuffer *buffer, uint32_t capacity) {
|
2017-02-09 23:50:47 +00:00
|
|
|
uint8_t * newData;
|
|
|
|
if (capacity <= buffer->capacity) return;
|
2017-02-16 02:02:00 +00:00
|
|
|
newData = gst_alloc(vm, capacity * sizeof(uint8_t));
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(newData, buffer->data, buffer->count * sizeof(uint8_t));
|
2017-02-09 23:50:47 +00:00
|
|
|
buffer->data = newData;
|
|
|
|
buffer->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a byte from an index in the buffer */
|
2017-02-16 02:02:00 +00:00
|
|
|
int gst_buffer_get(GstBuffer *buffer, uint32_t index) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (index < buffer->count) {
|
|
|
|
return buffer->data[index];
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a byte into the buffer */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_buffer_push(Gst *vm, GstBuffer * buffer, uint8_t c) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (buffer->count >= buffer->capacity) {
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_buffer_ensure(vm, buffer, 2 * buffer->count);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
buffer->data[buffer->count++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push multiple bytes into the buffer */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_buffer_append(Gst *vm, GstBuffer *buffer, uint8_t *string, uint32_t length) {
|
2017-02-09 23:50:47 +00:00
|
|
|
uint32_t newSize = buffer->count + length;
|
|
|
|
if (newSize > buffer->capacity) {
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_buffer_ensure(vm, buffer, 2 * newSize);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(buffer->data + buffer->count, string, length);
|
2017-02-09 23:50:47 +00:00
|
|
|
buffer->count = newSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the buffer to a string */
|
2017-02-16 02:02:00 +00:00
|
|
|
uint8_t *gst_buffer_to_string(Gst *vm, GstBuffer *buffer) {
|
|
|
|
uint8_t *data = gst_alloc(vm, buffer->count + 2 * sizeof(uint32_t));
|
2017-02-09 23:50:47 +00:00
|
|
|
data += 2 * sizeof(uint32_t);
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_string_length(data) = buffer->count;
|
|
|
|
gst_string_hash(data) = 0;
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(data, buffer->data, buffer->count * sizeof(uint8_t));
|
2017-02-09 23:50:47 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Array functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Creates a new array */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstArray *gst_array(Gst * vm, uint32_t capacity) {
|
|
|
|
GstArray *array = gst_alloc(vm, sizeof(GstArray));
|
|
|
|
GstValue *data = gst_alloc(vm, capacity * sizeof(GstValue));
|
2017-02-09 23:50:47 +00:00
|
|
|
array->data = data;
|
|
|
|
array->count = 0;
|
|
|
|
array->capacity = capacity;
|
2017-02-15 01:45:34 +00:00
|
|
|
array->flags = 0;
|
2017-02-09 23:50:47 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure the array has enough capacity for capacity elements */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_array_ensure(Gst *vm, GstArray *array, uint32_t capacity) {
|
|
|
|
GstValue *newData;
|
2017-02-09 23:50:47 +00:00
|
|
|
if (capacity <= array->capacity) return;
|
2017-02-16 02:02:00 +00:00
|
|
|
newData = gst_alloc(vm, capacity * sizeof(GstValue));
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(newData, array->data, array->capacity * sizeof(GstValue));
|
2017-02-09 23:50:47 +00:00
|
|
|
array->data = newData;
|
|
|
|
array->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value of an array with bounds checking. */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_array_get(GstArray *array, uint32_t index) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (index < array->count) {
|
|
|
|
return array->data[index];
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue v;
|
|
|
|
v.type = GST_NIL;
|
2017-02-09 23:50:47 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to set an index in the array. Return 1 if successful, 0
|
|
|
|
* on failiure */
|
2017-02-16 02:02:00 +00:00
|
|
|
int gst_array_set(GstArray *array, uint32_t index, GstValue x) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (index < array->count) {
|
|
|
|
array->data[index] = x;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an item to the end of the array */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_array_push(Gst *vm, GstArray *array, GstValue x) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (array->count >= array->capacity) {
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_array_ensure(vm, array, 2 * array->count);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
array->data[array->count++] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the last item from the Array and return it */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_array_pop(GstArray *array) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[--array->count];
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue v;
|
|
|
|
v.type = GST_NIL;
|
2017-02-09 23:50:47 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the last item in the Array */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_array_peek(GstArray *array) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[array->count - 1];
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue v;
|
|
|
|
v.type = GST_NIL;
|
2017-02-09 23:50:47 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:29:40 +00:00
|
|
|
/****/
|
|
|
|
/* Tuple functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Create a new emoty tuple of the given size. Expected to be
|
|
|
|
* mutated immediately */
|
|
|
|
GstValue *gst_tuple(Gst *vm, uint32_t length) {
|
|
|
|
char *data = gst_alloc(vm, 2 * sizeof(uint32_t) + length * sizeof(GstValue));
|
|
|
|
GstValue *tuple = (GstValue *)(data + (2 * sizeof(uint32_t)));
|
|
|
|
gst_tuple_length(tuple) = length;
|
|
|
|
gst_tuple_hash(tuple) = 0;
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:50:47 +00:00
|
|
|
/****/
|
|
|
|
/* Dictionary functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Create a new dictionary */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstObject* gst_object(Gst *vm, uint32_t capacity) {
|
|
|
|
GstObject *o = gst_alloc(vm, sizeof(GstObject));
|
|
|
|
GstBucket **buckets = gst_zalloc(vm, capacity * sizeof(GstBucket *));
|
|
|
|
o->buckets = buckets;
|
|
|
|
o->capacity = capacity;
|
|
|
|
o->count = 0;
|
|
|
|
o->flags = 0;
|
|
|
|
return o;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize the dictionary table. */
|
2017-02-16 02:02:00 +00:00
|
|
|
static void gst_object_rehash(Gst *vm, GstObject *o, uint32_t size) {
|
|
|
|
GstBucket **newBuckets = gst_zalloc(vm, size * sizeof(GstBucket *));
|
2017-02-09 23:50:47 +00:00
|
|
|
uint32_t i, count;
|
2017-02-16 02:02:00 +00:00
|
|
|
for (i = 0, count = o->capacity; i < count; ++i) {
|
|
|
|
GstBucket *bucket = o->buckets[i];
|
2017-02-09 23:50:47 +00:00
|
|
|
while (bucket) {
|
|
|
|
uint32_t index;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstBucket *next = bucket->next;
|
|
|
|
index = gst_hash(bucket->key) % size;
|
2017-02-09 23:50:47 +00:00
|
|
|
bucket->next = newBuckets[index];
|
|
|
|
newBuckets[index] = bucket;
|
|
|
|
bucket = next;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
o->buckets = newBuckets;
|
|
|
|
o->capacity = size;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the bucket that contains the given key */
|
2017-02-16 02:02:00 +00:00
|
|
|
static GstBucket *gst_object_find(GstObject *o, GstValue key) {
|
|
|
|
uint32_t index = gst_hash(key) % o->capacity;
|
|
|
|
GstBucket *bucket = o->buckets[index];
|
2017-02-09 23:50:47 +00:00
|
|
|
while (bucket) {
|
2017-02-16 02:02:00 +00:00
|
|
|
if (gst_equals(bucket->key, key))
|
2017-02-09 23:50:47 +00:00
|
|
|
return bucket;
|
|
|
|
bucket = bucket->next;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
return (GstBucket *)0;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value out of the dictionary */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_object_get(GstObject *o, GstValue key) {
|
|
|
|
GstBucket *bucket = gst_object_find(o, key);
|
2017-02-09 23:50:47 +00:00
|
|
|
if (bucket) {
|
|
|
|
return bucket->value;
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue nil;
|
|
|
|
nil.type = GST_NIL;
|
2017-02-09 23:50:47 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove an entry from the dictionary */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_object_remove(Gst * vm, GstObject *o, GstValue key) {
|
|
|
|
GstBucket *bucket, *previous;
|
|
|
|
uint32_t index = gst_hash(key) % o->capacity;
|
|
|
|
bucket = o->buckets[index];
|
|
|
|
previous = (GstBucket *)0;
|
2017-02-09 23:50:47 +00:00
|
|
|
while (bucket) {
|
2017-02-16 02:02:00 +00:00
|
|
|
if (gst_equals(bucket->key, key)) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (previous) {
|
|
|
|
previous->next = bucket->next;
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
o->buckets[index] = bucket->next;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (o->count < o->capacity / 4) {
|
|
|
|
gst_object_rehash(vm, o, o->capacity / 2);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
--o->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
return bucket->value;
|
|
|
|
}
|
|
|
|
previous = bucket;
|
|
|
|
bucket = bucket->next;
|
|
|
|
}
|
|
|
|
/* Return nil if we found nothing */
|
|
|
|
{
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue nil;
|
|
|
|
nil.type = GST_NIL;
|
2017-02-09 23:50:47 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value into the dictionary. Returns 1 if successful, 0 if out of memory.
|
|
|
|
* The VM pointer is needed for memory allocation. */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_object_put(Gst *vm, GstObject *o, GstValue key, GstValue value) {
|
|
|
|
GstBucket *bucket, *previous;
|
|
|
|
uint32_t index = gst_hash(key) % o->capacity;
|
|
|
|
if (key.type == GST_NIL) return;
|
2017-02-09 23:50:47 +00:00
|
|
|
/* Do a removal if value is nil */
|
2017-02-16 02:02:00 +00:00
|
|
|
if (value.type == GST_NIL) {
|
|
|
|
bucket = o->buckets[index];
|
|
|
|
previous = (GstBucket *)0;
|
2017-02-09 23:50:47 +00:00
|
|
|
while (bucket) {
|
2017-02-16 02:02:00 +00:00
|
|
|
if (gst_equals(bucket->key, key)) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (previous) {
|
|
|
|
previous->next = bucket->next;
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
o->buckets[index] = bucket->next;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (o->count < o->capacity / 4) {
|
|
|
|
gst_object_rehash(vm, o, o->capacity / 2);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
--o->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
previous = bucket;
|
|
|
|
bucket = bucket->next;
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
bucket = gst_object_find(o, key);
|
2017-02-09 23:50:47 +00:00
|
|
|
if (bucket) {
|
2017-02-10 04:28:11 +00:00
|
|
|
bucket->value = value;
|
2017-02-09 23:50:47 +00:00
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
if (o->count >= 2 * o->capacity) {
|
|
|
|
gst_object_rehash(vm, o, 2 * o->capacity);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
bucket = gst_alloc(vm, sizeof(GstBucket));
|
|
|
|
bucket->next = o->buckets[index];
|
2017-02-10 04:28:11 +00:00
|
|
|
bucket->value = value;
|
|
|
|
bucket->key = key;
|
2017-02-16 02:02:00 +00:00
|
|
|
o->buckets[index] = bucket;
|
|
|
|
++o->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|