2017-02-09 20:02:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "value.h"
|
2017-02-09 23:50:47 +00:00
|
|
|
#include "ds.h"
|
|
|
|
#include "vm.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint8_t * load_cstring(Gst *vm, const char *string, uint32_t len) {
|
|
|
|
uint8_t *data = gst_alloc(vm, len + 2 * sizeof(uint32_t));
|
2017-02-09 20:02:59 +00:00
|
|
|
data += 2 * sizeof(uint32_t);
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_string_hash(data) = 0;
|
|
|
|
gst_string_length(data) = len;
|
2017-02-09 20:02:59 +00:00
|
|
|
memcpy(data, string, len);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_load_cstring(Gst *vm, const char *string) {
|
|
|
|
GstValue ret;
|
|
|
|
ret.type = GST_STRING;
|
|
|
|
ret.data.string = load_cstring(vm, string, strlen(string));
|
2017-02-09 20:02:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint8_t * number_to_string(Gst *vm, GstNumber x) {
|
2017-02-09 20:02:59 +00:00
|
|
|
static const uint32_t SIZE = 20;
|
2017-02-16 02:02:00 +00:00
|
|
|
uint8_t *data = gst_alloc(vm, SIZE + 2 * sizeof(uint32_t));
|
2017-02-09 20:02:59 +00:00
|
|
|
data += 2 * sizeof(uint32_t);
|
|
|
|
snprintf((char *) data, SIZE, "%.17g", x);
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_string_hash(data) = 0;
|
|
|
|
gst_string_length(data) = strlen((char *) data);
|
2017-02-09 20:02:59 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
static const char *HEX_CHARACTERS = "0123456789abcdef";
|
2017-02-09 20:02:59 +00:00
|
|
|
#define HEX(i) (((uint8_t *) HEX_CHARACTERS)[(i)])
|
|
|
|
|
|
|
|
/* Returns a string description for a pointer */
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint8_t *string_description(Gst *vm, const char *title, uint32_t titlelen, void *pointer) {
|
2017-02-13 05:11:30 +00:00
|
|
|
uint32_t len = 5 + titlelen + sizeof(void *) * 2;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
2017-02-16 02:02:00 +00:00
|
|
|
uint8_t *data = gst_alloc(vm, len + 2 * sizeof(uint32_t));
|
|
|
|
uint8_t *c;
|
2017-02-09 20:02:59 +00:00
|
|
|
union {
|
|
|
|
uint8_t bytes[sizeof(void *)];
|
2017-02-16 02:02:00 +00:00
|
|
|
void *p;
|
2017-02-09 20:02:59 +00:00
|
|
|
} buf;
|
|
|
|
buf.p = pointer;
|
|
|
|
data += 2 * sizeof(uint32_t);
|
|
|
|
c = data;
|
|
|
|
*c++ = '<';
|
|
|
|
for (i = 0; i < titlelen; ++i) {
|
|
|
|
*c++ = ((uint8_t *)title) [i];
|
|
|
|
}
|
|
|
|
*c++ = ' ';
|
2017-02-13 05:11:30 +00:00
|
|
|
*c++ = '0';
|
|
|
|
*c++ = 'x';
|
|
|
|
for (i = sizeof(void *); i > 0; --i) {
|
|
|
|
uint8_t byte = buf.bytes[i - 1];
|
|
|
|
if (!byte) continue;
|
2017-02-09 20:02:59 +00:00
|
|
|
*c++ = HEX(byte >> 4);
|
|
|
|
*c++ = HEX(byte & 0xF);
|
|
|
|
}
|
|
|
|
*c++ = '>';
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_string_hash(data) = 0;
|
|
|
|
gst_string_length(data) = c - data;
|
2017-02-09 20:02:59 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns a string pointer or NULL if could not allocate memory. */
|
2017-02-16 02:02:00 +00:00
|
|
|
uint8_t *gst_to_string(Gst *vm, GstValue x) {
|
2017-02-10 04:28:11 +00:00
|
|
|
switch (x.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NIL:
|
|
|
|
return load_cstring(vm, "nil", 3);
|
|
|
|
case GST_BOOLEAN:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.boolean) {
|
2017-02-16 02:02:00 +00:00
|
|
|
return load_cstring(vm, "true", 4);
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
return load_cstring(vm, "false", 5);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NUMBER:
|
|
|
|
return number_to_string(vm, x.data.number);
|
|
|
|
case GST_ARRAY:
|
2017-02-13 23:58:56 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstBuffer * b = gst_buffer(vm, 40);
|
|
|
|
gst_buffer_push(vm, b, '(');
|
2017-02-13 23:58:56 +00:00
|
|
|
for (i = 0; i < x.data.array->count; ++i) {
|
2017-02-16 02:02:00 +00:00
|
|
|
uint8_t * substr = gst_to_string(vm, x.data.array->data[i]);
|
|
|
|
gst_buffer_append(vm, b, substr, gst_string_length(substr));
|
2017-02-13 23:58:56 +00:00
|
|
|
if (i < x.data.array->count - 1)
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_buffer_push(vm, b, ' ');
|
2017-02-13 23:58:56 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_buffer_push(vm, b, ')');
|
|
|
|
return gst_buffer_to_string(vm, b);
|
2017-02-13 23:58:56 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_STRING:
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.string;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BYTEBUFFER:
|
|
|
|
return string_description(vm, "buffer", 6, x.data.pointer);
|
|
|
|
case GST_CFUNCTION:
|
|
|
|
return string_description(vm, "cfunction", 9, x.data.pointer);
|
|
|
|
case GST_FUNCTION:
|
|
|
|
return string_description(vm, "function", 8, x.data.pointer);
|
|
|
|
case GST_OBJECT:
|
|
|
|
return string_description(vm, "object", 6, x.data.pointer);
|
|
|
|
case GST_THREAD:
|
|
|
|
return string_description(vm, "thread", 6, x.data.pointer);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simple hash function */
|
|
|
|
uint32_t djb2(const uint8_t * str) {
|
2017-02-16 02:02:00 +00:00
|
|
|
const uint8_t * end = str + gst_string_length(str);
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (str < end)
|
|
|
|
hash = (hash << 5) + hash + *str++;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if two values are equal. This is strict equality with no conversion. */
|
2017-02-16 02:02:00 +00:00
|
|
|
int gst_equals(GstValue x, GstValue y) {
|
2017-02-09 23:50:47 +00:00
|
|
|
int result = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.type != y.type) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
switch (x.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NIL:
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 1;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BOOLEAN:
|
2017-02-12 15:27:18 +00:00
|
|
|
result = (x.data.boolean == y.data.boolean);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NUMBER:
|
2017-02-12 15:27:18 +00:00
|
|
|
result = (x.data.number == y.data.number);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Assume that when strings are created, equal strings
|
|
|
|
* are set to the same string */
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_STRING:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.string == y.data.string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (gst_hash(x) != gst_hash(y) ||
|
|
|
|
gst_string_length(x.data.string) != gst_string_length(y.data.string)) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (!strncmp((char *) x.data.string, (char *) y.data.string, gst_string_length(x.data.string))) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = 0;
|
|
|
|
break;
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2017-02-09 20:02:59 +00:00
|
|
|
/* compare pointers */
|
2017-02-12 15:27:18 +00:00
|
|
|
result = (x.data.array == y.data.array);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes a hash value for a function */
|
2017-02-16 02:02:00 +00:00
|
|
|
uint32_t gst_hash(GstValue x) {
|
2017-02-09 23:50:47 +00:00
|
|
|
uint32_t hash = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
switch (x.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NIL:
|
2017-02-09 20:02:59 +00:00
|
|
|
hash = 0;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BOOLEAN:
|
2017-02-10 04:28:11 +00:00
|
|
|
hash = x.data.boolean;
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NUMBER:
|
2017-02-09 20:02:59 +00:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
uint32_t hash;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstNumber number;
|
2017-02-09 20:02:59 +00:00
|
|
|
} u;
|
2017-02-10 04:28:11 +00:00
|
|
|
u.number = x.data.number;
|
2017-02-09 20:02:59 +00:00
|
|
|
hash = u.hash;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* String hashes */
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_STRING:
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Assume 0 is not hashed. */
|
2017-02-16 02:02:00 +00:00
|
|
|
if (gst_string_hash(x.data.string))
|
|
|
|
hash = gst_string_hash(x.data.string);
|
2017-02-09 20:02:59 +00:00
|
|
|
else
|
2017-02-16 02:02:00 +00:00
|
|
|
hash = gst_string_hash(x.data.string) = djb2(x.data.string);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Cast the pointer */
|
2017-02-09 20:56:45 +00:00
|
|
|
{
|
2017-02-12 20:16:55 +00:00
|
|
|
union {
|
|
|
|
void * pointer;
|
|
|
|
uint32_t hash;
|
|
|
|
} u;
|
|
|
|
u.pointer = x.data.pointer;
|
|
|
|
hash = u.hash;
|
2017-02-09 20:56:45 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compares x to y. If they are equal retuns 0. If x is less, returns -1.
|
|
|
|
* If y is less, returns 1. All types are comparable
|
|
|
|
* and should have strict ordering. */
|
2017-02-16 02:02:00 +00:00
|
|
|
int gst_compare(GstValue x, GstValue y) {
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.type == y.type) {
|
|
|
|
switch (x.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NIL:
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BOOLEAN:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.boolean == y.data.boolean) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.boolean ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_NUMBER:
|
2017-02-09 20:02:59 +00:00
|
|
|
/* TODO: define behavior for NaN and infinties. */
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.number == y.data.number) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.number > y.data.number ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_STRING:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.string == y.data.string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
uint32_t xlen = gst_string_length(x.data.string);
|
|
|
|
uint32_t ylen = gst_string_length(y.data.string);
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t len = xlen > ylen ? ylen : xlen;
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < len; ++i) {
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.string[i] == y.data.string[i]) {
|
2017-02-09 20:02:59 +00:00
|
|
|
continue;
|
2017-02-10 04:28:11 +00:00
|
|
|
} else if (x.data.string[i] < y.data.string[i]) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1; /* x is less then y */
|
|
|
|
} else {
|
|
|
|
return -1; /* y is less than x */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xlen == ylen) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return xlen < ylen ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.string == y.data.string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.string > y.data.string ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 04:28:11 +00:00
|
|
|
} else if (x.type < y.type) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-12 20:53:52 +00:00
|
|
|
|
2017-02-13 02:54:18 +00:00
|
|
|
/* Allow negative indexing to get from end of array like structure */
|
|
|
|
/* This probably isn't very fast - look at Lua conversion function.
|
|
|
|
* I would like to keep this standard C for as long as possible, though. */
|
2017-02-16 02:02:00 +00:00
|
|
|
static int32_t to_index(GstNumber raw, int64_t len) {
|
2017-02-13 02:54:18 +00:00
|
|
|
int32_t toInt = raw;
|
2017-02-16 02:02:00 +00:00
|
|
|
if ((GstNumber) toInt == raw) {
|
2017-02-13 02:54:18 +00:00
|
|
|
/* We were able to convert */
|
|
|
|
if (toInt < 0) {
|
|
|
|
/* Index from end */
|
|
|
|
if (toInt < -len) return -1;
|
|
|
|
return len + toInt;
|
|
|
|
} else {
|
|
|
|
/* Normal indexing */
|
|
|
|
if (toInt >= len) return -1;
|
|
|
|
return toInt;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a number into a byte. */
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint8_t to_byte(GstNumber raw) {
|
2017-02-13 02:54:18 +00:00
|
|
|
if (raw > 255) return 255;
|
|
|
|
if (raw < 0) return 0;
|
|
|
|
return (uint8_t) raw;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:53:52 +00:00
|
|
|
/* Get a value out af an associated data structure. Can throw VM error. */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_get(Gst *vm, GstValue ds, GstValue key) {
|
2017-02-13 02:54:18 +00:00
|
|
|
int32_t index;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue ret;
|
2017-02-12 20:53:52 +00:00
|
|
|
switch (ds.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_ARRAY:
|
|
|
|
gst_assert_type(vm, key, GST_NUMBER);
|
|
|
|
index = to_index(key.data.number, ds.data.array->count);
|
|
|
|
if (index == -1) gst_error(vm, "Invalid array access");
|
2017-02-13 02:54:18 +00:00
|
|
|
return ds.data.array->data[index];
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BYTEBUFFER:
|
|
|
|
gst_assert_type(vm, key, GST_NUMBER);
|
|
|
|
index = to_index(key.data.number, ds.data.buffer->count);
|
|
|
|
if (index == -1) gst_error(vm, "Invalid buffer access");
|
|
|
|
ret.type = GST_NUMBER;
|
2017-02-13 02:54:18 +00:00
|
|
|
ret.data.number = ds.data.buffer->data[index];
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_STRING:
|
|
|
|
gst_assert_type(vm, key, GST_NUMBER);
|
|
|
|
index = to_index(key.data.number, gst_string_length(ds.data.string));
|
|
|
|
if (index == -1) gst_error(vm, "Invalid string access");
|
|
|
|
ret.type = GST_NUMBER;
|
2017-02-13 02:54:18 +00:00
|
|
|
ret.data.number = ds.data.string[index];
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OBJECT:
|
|
|
|
return gst_object_get(ds.data.object, key);
|
2017-02-12 20:53:52 +00:00
|
|
|
default:
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_error(vm, "Cannot get.");
|
2017-02-12 20:53:52 +00:00
|
|
|
}
|
2017-02-13 02:54:18 +00:00
|
|
|
return ret;
|
2017-02-12 20:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set a value in an associative data structure. Can throw VM error. */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_set(Gst *vm, GstValue ds, GstValue key, GstValue value) {
|
2017-02-13 02:54:18 +00:00
|
|
|
int32_t index;
|
|
|
|
switch (ds.type) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_ARRAY:
|
|
|
|
gst_assert_type(vm, key, GST_NUMBER);
|
|
|
|
index = to_index(key.data.number, ds.data.array->count);
|
|
|
|
if (index == -1) gst_error(vm, "Invalid array access");
|
2017-02-13 02:54:18 +00:00
|
|
|
ds.data.array->data[index] = value;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_BYTEBUFFER:
|
|
|
|
gst_assert_type(vm, key, GST_NUMBER);
|
|
|
|
gst_assert_type(vm, value, GST_NUMBER);
|
|
|
|
index = to_index(key.data.number, ds.data.buffer->count);
|
|
|
|
if (index == -1) gst_error(vm, "Invalid buffer access");
|
|
|
|
ds.data.buffer->data[index] = to_byte(value.data.number);
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OBJECT:
|
|
|
|
gst_object_put(vm, ds.data.object, key, value);
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_error(vm, "Cannot set.");
|
2017-02-13 02:54:18 +00:00
|
|
|
}
|
2017-02-12 20:53:52 +00:00
|
|
|
}
|
2017-02-19 16:19:39 +00:00
|
|
|
|
|
|
|
/* Get the meta value associated with a value */
|
|
|
|
GstValue gst_meta(Gst *vm, GstValue x) {
|
|
|
|
switch (x.type) {
|
|
|
|
default: return vm->metas[x.type];
|
|
|
|
case GST_OBJECT:
|
|
|
|
return x.data.object->meta;
|
|
|
|
}
|
|
|
|
}
|