2017-03-22 04:27:18 +00:00
|
|
|
#include <gst/gst.h>
|
2017-02-09 23:50:47 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* 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;
|
|
|
|
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-03-22 22:35:54 +00:00
|
|
|
void gst_buffer_append(Gst *vm, GstBuffer *buffer, const 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-03-22 22:35:54 +00:00
|
|
|
const uint8_t *gst_buffer_to_string(Gst *vm, GstBuffer *buffer) {
|
2017-04-15 20:05:59 +00:00
|
|
|
return gst_string_b(vm, buffer->data, buffer->count);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* 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;
|
|
|
|
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-09 18:49:46 +00:00
|
|
|
/****/
|
|
|
|
/* Userdata functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Create new userdata */
|
|
|
|
void *gst_userdata(Gst *vm, uint32_t size, GstObject *meta) {
|
2017-03-10 05:17:34 +00:00
|
|
|
char *data = gst_alloc(vm, sizeof(GstUserdataHeader) + size);
|
|
|
|
GstUserdataHeader *header = (GstUserdataHeader *)data;
|
|
|
|
void *user = data + sizeof(GstUserdataHeader);
|
|
|
|
header->size = size;
|
|
|
|
header->meta = meta;
|
|
|
|
return user;
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
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));
|
2017-04-16 13:39:41 +00:00
|
|
|
GstValue *data = gst_zalloc(vm, capacity * sizeof(GstValue));
|
|
|
|
o->data = data;
|
2017-02-16 02:02:00 +00:00
|
|
|
o->capacity = capacity;
|
|
|
|
o->count = 0;
|
2017-04-12 14:31:50 +00:00
|
|
|
o->parent = NULL;
|
2017-04-16 13:39:41 +00:00
|
|
|
o->deleted = 0;
|
2017-02-16 02:02:00 +00:00
|
|
|
return o;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 13:39:41 +00:00
|
|
|
/* Find the bucket that contains the given key. Will also return
|
|
|
|
* bucket where key should go if not in object. */
|
|
|
|
static GstValue *gst_object_find(GstObject *o, GstValue key) {
|
|
|
|
uint32_t index = (gst_hash(key) % (o->capacity / 2)) * 2;
|
|
|
|
uint32_t i, j;
|
|
|
|
uint32_t start[2], end[2];
|
|
|
|
start[0] = index; end[0] = o->capacity;
|
|
|
|
start[1] = 0; end[1] = index;
|
|
|
|
for (j = 0; j < 2; ++j)
|
|
|
|
for (i = start[j]; i < end[j]; i += 2) {
|
|
|
|
if (o->data[i].type == GST_NIL) {
|
|
|
|
if (o->data[i + 1].type == GST_NIL) {
|
|
|
|
/* Empty */
|
|
|
|
return o->data + i;
|
|
|
|
}
|
|
|
|
} else if (gst_equals(o->data[i], key)) {
|
|
|
|
return o->data + i;
|
|
|
|
}
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-04-16 13:39:41 +00:00
|
|
|
return NULL;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 13:39:41 +00:00
|
|
|
/* Resize the dictionary table. */
|
|
|
|
static void gst_object_rehash(Gst *vm, GstObject *o, uint32_t size) {
|
|
|
|
GstValue *olddata = o->data;
|
|
|
|
GstValue *newdata = gst_zalloc(vm, size * sizeof(GstValue));
|
|
|
|
uint32_t i, oldcapacity;
|
|
|
|
oldcapacity = o->capacity;
|
|
|
|
o->data = newdata;
|
|
|
|
o->capacity = size;
|
|
|
|
o->deleted = 0;
|
|
|
|
for (i = 0; i < oldcapacity; i += 2) {
|
|
|
|
if (olddata[i].type != GST_NIL) {
|
|
|
|
GstValue *bucket = gst_object_find(o, olddata[i]);
|
|
|
|
bucket[0] = olddata[i];
|
|
|
|
bucket[1] = olddata[i + 1];
|
|
|
|
}
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
/* Get a value out of the object */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_object_get(GstObject *o, GstValue key) {
|
2017-04-16 13:39:41 +00:00
|
|
|
GstValue *bucket = gst_object_find(o, key);
|
|
|
|
if (bucket && bucket[0].type != GST_NIL) {
|
|
|
|
return bucket[1];
|
2017-02-09 23:50:47 +00:00
|
|
|
} 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-04-16 13:39:41 +00:00
|
|
|
GstValue gst_object_remove(GstObject *o, GstValue key) {
|
|
|
|
GstValue *bucket = gst_object_find(o, key);
|
|
|
|
if (bucket && bucket[0].type != GST_NIL) {
|
|
|
|
GstValue ret = bucket[1];
|
|
|
|
o->count--;
|
|
|
|
o->deleted++;
|
|
|
|
bucket[0].type = GST_NIL;
|
|
|
|
bucket[1].type = GST_BOOLEAN;
|
|
|
|
return ret;
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 13:39:41 +00:00
|
|
|
/* Put a value into the object */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_object_put(Gst *vm, GstObject *o, GstValue key, GstValue value) {
|
|
|
|
if (key.type == GST_NIL) return;
|
|
|
|
if (value.type == GST_NIL) {
|
2017-04-16 13:39:41 +00:00
|
|
|
gst_object_remove(o, key);
|
2017-02-09 23:50:47 +00:00
|
|
|
} else {
|
2017-04-16 13:39:41 +00:00
|
|
|
GstValue *bucket = gst_object_find(o, key);
|
|
|
|
if (bucket && bucket[0].type != GST_NIL) {
|
|
|
|
bucket[1] = value;
|
2017-02-09 23:50:47 +00:00
|
|
|
} else {
|
2017-04-16 13:39:41 +00:00
|
|
|
if (!bucket || 4 * (o->count + o->deleted) >= o->capacity) {
|
|
|
|
gst_object_rehash(vm, o, 4 * o->count + 6);
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
2017-04-16 13:39:41 +00:00
|
|
|
bucket = gst_object_find(o, key);
|
|
|
|
bucket[0] = key;
|
|
|
|
bucket[1] = value;
|
2017-02-16 02:02:00 +00:00
|
|
|
++o->count;
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-15 20:05:59 +00:00
|
|
|
|
2017-04-16 13:39:41 +00:00
|
|
|
/* Find next key in an object. Returns nil if no next key. */
|
|
|
|
GstValue gst_object_next(GstObject *o, GstValue key) {
|
|
|
|
GstValue ret;
|
|
|
|
GstValue *bucket;
|
|
|
|
if (key.type == GST_NIL)
|
|
|
|
bucket = o->data - 2;
|
|
|
|
else
|
|
|
|
bucket = gst_object_find(o, key);
|
|
|
|
if (bucket && bucket[0].type != GST_NIL) {
|
|
|
|
GstValue *nextbucket, *end;
|
|
|
|
end = o->data + o->capacity;
|
|
|
|
for (nextbucket = bucket + 2; nextbucket < end; nextbucket += 2) {
|
|
|
|
if (nextbucket[0].type != GST_NIL)
|
|
|
|
return nextbucket[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.type = GST_NIL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|