2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +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:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2018-06-12 18:24:45 +00:00
|
|
|
#include "state.h"
|
2018-01-19 21:43:19 +00:00
|
|
|
#include "gc.h"
|
2017-04-26 14:21:03 +00:00
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Base 64 lookup table for digits */
|
|
|
|
const char dst_base64[65] =
|
|
|
|
"0123456789"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"_=";
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* The DST value types in order. These types can be used as
|
|
|
|
* mnemonics instead of a bit pattern for type checking */
|
2018-03-31 20:40:36 +00:00
|
|
|
const char *const dst_type_names[16] = {
|
2018-02-03 18:55:55 +00:00
|
|
|
":nil",
|
2018-05-13 00:31:28 +00:00
|
|
|
":boolean",
|
|
|
|
":boolean",
|
2018-02-03 18:55:55 +00:00
|
|
|
":fiber",
|
|
|
|
":integer",
|
|
|
|
":real",
|
|
|
|
":string",
|
|
|
|
":symbol",
|
|
|
|
":array",
|
|
|
|
":tuple",
|
|
|
|
":table",
|
|
|
|
":struct",
|
|
|
|
":buffer",
|
|
|
|
":function",
|
|
|
|
":cfunction",
|
|
|
|
":abstract"
|
2017-11-06 03:05:47 +00:00
|
|
|
};
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Calculate hash for string */
|
|
|
|
|
|
|
|
int32_t dst_string_calchash(const uint8_t *str, int32_t len) {
|
|
|
|
const uint8_t *end = str + len;
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (str < end)
|
|
|
|
hash = (hash << 5) + hash + *str++;
|
|
|
|
return (int32_t) hash;
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Computes hash of an array of values */
|
2018-01-06 16:09:15 +00:00
|
|
|
int32_t dst_array_calchash(const Dst *array, int32_t len) {
|
|
|
|
const Dst *end = array + len;
|
2017-11-27 19:03:34 +00:00
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (array < end)
|
|
|
|
hash = (hash << 5) + hash + dst_hash(*array++);
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) hash;
|
2017-11-27 19:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Computes hash of an array of values */
|
|
|
|
int32_t dst_kv_calchash(const DstKV *kvs, int32_t len) {
|
|
|
|
const DstKV *end = kvs + len;
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (kvs < end) {
|
|
|
|
hash = (hash << 5) + hash + dst_hash(kvs->key);
|
|
|
|
hash = (hash << 5) + hash + dst_hash(kvs->value);
|
|
|
|
kvs++;
|
|
|
|
}
|
|
|
|
return (int32_t) hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate next power of 2. May overflow. If n is 0,
|
|
|
|
* will return 0. */
|
|
|
|
int32_t dst_tablen(int32_t n) {
|
|
|
|
n |= n >> 1;
|
|
|
|
n |= n >> 2;
|
|
|
|
n |= n >> 4;
|
|
|
|
n |= n >> 8;
|
|
|
|
n |= n >> 16;
|
|
|
|
return n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare a dst string with a cstring. more efficient than loading
|
|
|
|
* c string as a dst string. */
|
|
|
|
int dst_cstrcmp(const uint8_t *str, const char *other) {
|
|
|
|
int32_t len = dst_string_length(str);
|
|
|
|
int32_t index;
|
|
|
|
for (index = 0; index < len; index++) {
|
|
|
|
uint8_t c = str[index];
|
|
|
|
uint8_t k = ((const uint8_t *)other)[index];
|
|
|
|
if (c < k) return -1;
|
|
|
|
if (c > k) return 1;
|
|
|
|
if (k == '\0') break;
|
|
|
|
}
|
|
|
|
return (other[index] == '\0') ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Do a binary search on a static array of structs. Each struct must
|
|
|
|
* have a string as its first element, and the struct must be sorted
|
|
|
|
* lexogrpahically by that element. */
|
|
|
|
const void *dst_strbinsearch(
|
|
|
|
const void *tab,
|
|
|
|
size_t tabcount,
|
|
|
|
size_t itemsize,
|
|
|
|
const uint8_t *key) {
|
|
|
|
size_t low = 0;
|
|
|
|
size_t hi = tabcount;
|
|
|
|
const char *t = (const char *)tab;
|
|
|
|
while (low < hi) {
|
|
|
|
size_t mid = low + ((hi - low) / 2);
|
|
|
|
const char **item = (const char **)(t + mid * itemsize);
|
|
|
|
const char *name = *item;
|
|
|
|
int comp = dst_cstrcmp(key, name);
|
|
|
|
if (comp < 0) {
|
|
|
|
hi = mid;
|
|
|
|
} else if (comp > 0) {
|
|
|
|
low = mid + 1;
|
|
|
|
} else {
|
|
|
|
return (const void *)item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Register a value in the global registry */
|
2018-08-23 01:41:25 +00:00
|
|
|
void dst_register(const char *name, Dst value) {
|
2018-08-23 02:12:47 +00:00
|
|
|
Dst regkey = dst_csymbolv(name);
|
2018-08-23 01:41:25 +00:00
|
|
|
dst_table_put(dst_vm_registry, regkey, value);
|
|
|
|
dst_table_put(dst_vm_registry, value, regkey);
|
|
|
|
}
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Add a def to an environment */
|
|
|
|
void dst_def(DstTable *env, const char *name, Dst val) {
|
2018-01-16 01:14:21 +00:00
|
|
|
DstTable *subt = dst_table(1);
|
2018-03-18 18:01:58 +00:00
|
|
|
dst_table_put(subt, dst_csymbolv(":value"), val);
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_table_put(env, dst_csymbolv(name), dst_wrap_table(subt));
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Add a var to the environment */
|
2018-08-26 18:35:01 +00:00
|
|
|
void dst_var(DstTable *env, const char *name, Dst val) {
|
2018-01-16 01:14:21 +00:00
|
|
|
DstArray *array = dst_array(1);
|
|
|
|
DstTable *subt = dst_table(1);
|
|
|
|
dst_array_push(array, val);
|
2018-03-18 18:01:58 +00:00
|
|
|
dst_table_put(subt, dst_csymbolv(":ref"), dst_wrap_array(array));
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_table_put(env, dst_csymbolv(name), dst_wrap_table(subt));
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Load many cfunctions at once */
|
2018-08-26 18:35:01 +00:00
|
|
|
void dst_cfuns(DstTable *env, const char *regprefix, const DstReg *cfuns) {
|
2018-01-19 21:43:19 +00:00
|
|
|
while (cfuns->name) {
|
2018-06-12 18:24:45 +00:00
|
|
|
Dst name = dst_csymbolv(cfuns->name);
|
2018-08-26 18:35:01 +00:00
|
|
|
Dst longname = name;
|
|
|
|
if (regprefix) {
|
|
|
|
int32_t reglen = 0;
|
|
|
|
int32_t nmlen = 0;
|
|
|
|
while (regprefix[reglen]) reglen++;
|
|
|
|
while (cfuns->name[nmlen]) nmlen++;
|
|
|
|
uint8_t *longname_buffer =
|
|
|
|
dst_string_begin(reglen + 1 + nmlen);
|
|
|
|
memcpy(longname_buffer, regprefix, reglen);
|
|
|
|
longname_buffer[reglen] = '.';
|
|
|
|
memcpy(longname_buffer + reglen + 1, cfuns->name, nmlen);
|
|
|
|
longname = dst_wrap_symbol(dst_string_end(longname_buffer));
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
Dst fun = dst_wrap_cfunction(cfuns->cfun);
|
2018-08-26 18:35:01 +00:00
|
|
|
dst_def(env, cfuns->name, fun);
|
|
|
|
dst_table_put(dst_vm_registry, longname, fun);
|
|
|
|
dst_table_put(dst_vm_registry, fun, longname);
|
2018-01-19 21:43:19 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:55:02 +00:00
|
|
|
/* Resolve a symbol in the environment */
|
2018-08-26 18:35:01 +00:00
|
|
|
DstBindingType dst_resolve(DstTable *env, const uint8_t *sym, Dst *out) {
|
2018-01-18 22:25:45 +00:00
|
|
|
Dst ref;
|
2018-06-17 17:55:02 +00:00
|
|
|
DstTable *entry_table;
|
|
|
|
Dst entry = dst_table_get(env, dst_wrap_symbol(sym));
|
|
|
|
if (!dst_checktype(entry, DST_TABLE))
|
|
|
|
return DST_BINDING_NONE;
|
|
|
|
entry_table = dst_unwrap_table(entry);
|
|
|
|
if (!dst_checktype(
|
|
|
|
dst_table_get(entry_table, dst_csymbolv(":macro")),
|
|
|
|
DST_NIL)) {
|
|
|
|
*out = dst_table_get(entry_table, dst_csymbolv(":value"));
|
|
|
|
return DST_BINDING_MACRO;
|
|
|
|
}
|
|
|
|
ref = dst_table_get(entry_table, dst_csymbolv(":ref"));
|
2018-01-18 22:25:45 +00:00
|
|
|
if (dst_checktype(ref, DST_ARRAY)) {
|
2018-06-17 17:55:02 +00:00
|
|
|
*out = ref;
|
|
|
|
return DST_BINDING_VAR;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2018-06-17 17:55:02 +00:00
|
|
|
*out = dst_table_get(entry_table, dst_csymbolv(":value"));
|
|
|
|
return DST_BINDING_DEF;
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get module from the arguments passed to library */
|
2018-08-26 18:35:01 +00:00
|
|
|
DstTable *dst_env(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
DstTable *module;
|
|
|
|
if (args.n >= 1 && dst_checktype(args.v[0], DST_TABLE)) {
|
|
|
|
module = dst_unwrap_table(args.v[0]);
|
|
|
|
} else {
|
|
|
|
module = dst_table(0);
|
|
|
|
}
|
|
|
|
*args.ret = dst_wrap_table(module);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
/* Read both tuples and arrays as c pointers + int32_t length. Return 1 if the
|
2017-04-24 17:12:55 +00:00
|
|
|
* view can be constructed, 0 if an invalid type. */
|
2018-07-04 17:15:52 +00:00
|
|
|
int dst_indexed_view(Dst seq, const Dst **data, int32_t *len) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(seq, DST_ARRAY)) {
|
|
|
|
*data = dst_unwrap_array(seq)->data;
|
|
|
|
*len = dst_unwrap_array(seq)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(seq, DST_TUPLE)) {
|
2018-01-05 21:17:55 +00:00
|
|
|
*data = dst_unwrap_tuple(seq);
|
2017-11-28 23:27:55 +00:00
|
|
|
*len = dst_tuple_length(dst_unwrap_struct(seq));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2017-07-02 01:51:16 +00:00
|
|
|
}
|
2017-04-24 17:12:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
/* Read both strings and buffer as unsigned character array + int32_t len.
|
2017-04-24 17:12:55 +00:00
|
|
|
* Returns 1 if the view can be constructed and 0 if the type is invalid. */
|
2018-07-04 17:15:52 +00:00
|
|
|
int dst_bytes_view(Dst str, const uint8_t **data, int32_t *len) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(str, DST_STRING) || dst_checktype(str, DST_SYMBOL)) {
|
|
|
|
*data = dst_unwrap_string(str);
|
|
|
|
*len = dst_string_length(dst_unwrap_string(str));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(str, DST_BUFFER)) {
|
|
|
|
*data = dst_unwrap_buffer(str)->data;
|
|
|
|
*len = dst_unwrap_buffer(str)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
/* Read both structs and tables as the entries of a hashtable with
|
2017-04-24 17:12:55 +00:00
|
|
|
* identical structure. Returns 1 if the view can be constructed and
|
|
|
|
* 0 if the type is invalid. */
|
2018-07-04 17:15:52 +00:00
|
|
|
int dst_dictionary_view(Dst tab, const DstKV **data, int32_t *len, int32_t *cap) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(tab, DST_TABLE)) {
|
|
|
|
*data = dst_unwrap_table(tab)->data;
|
|
|
|
*cap = dst_unwrap_table(tab)->capacity;
|
|
|
|
*len = dst_unwrap_table(tab)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(tab, DST_STRUCT)) {
|
|
|
|
*data = dst_unwrap_struct(tab);
|
|
|
|
*cap = dst_struct_capacity(dst_unwrap_struct(tab));
|
|
|
|
*len = dst_struct_length(dst_unwrap_struct(tab));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-28 22:10:57 +00:00
|
|
|
|
2018-04-30 00:13:04 +00:00
|
|
|
/* Get actual type name of a value for debugging purposes */
|
|
|
|
static const char *typestr(DstArgs args, int32_t n) {
|
2018-04-28 22:10:57 +00:00
|
|
|
DstType actual = n < args.n ? dst_type(args.v[n]) : DST_NIL;
|
2018-05-13 00:31:28 +00:00
|
|
|
return ((actual == DST_ABSTRACT)
|
2018-04-30 00:13:04 +00:00
|
|
|
? dst_abstract_type(dst_unwrap_abstract(args.v[n]))->name
|
2018-05-13 00:31:28 +00:00
|
|
|
: dst_type_names[actual]) + 1;
|
2018-04-30 00:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_type_err(DstArgs args, int32_t n, DstType expected) {
|
2018-04-28 22:10:57 +00:00
|
|
|
const uint8_t *message = dst_formatc(
|
2018-05-13 00:31:28 +00:00
|
|
|
"bad slot #%d, expected %t, got %s",
|
2018-04-28 22:10:57 +00:00
|
|
|
n,
|
|
|
|
expected,
|
2018-04-30 00:13:04 +00:00
|
|
|
typestr(args, n));
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROWV(args, dst_wrap_string(message));
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
void dst_buffer_push_types(DstBuffer *buffer, int types) {
|
2018-04-28 22:10:57 +00:00
|
|
|
int first = 1;
|
2018-07-04 03:07:35 +00:00
|
|
|
int i = 0;
|
|
|
|
while (types) {
|
|
|
|
if (1 & types) {
|
2018-04-28 22:10:57 +00:00
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
} else {
|
2018-07-04 03:07:35 +00:00
|
|
|
dst_buffer_push_u8(buffer, '|');
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
dst_buffer_push_cstring(buffer, dst_type_names[i] + 1);
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
|
|
|
i++;
|
2018-07-04 03:07:35 +00:00
|
|
|
types >>= 1;
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_typemany_err(DstArgs args, int32_t n, int expected) {
|
|
|
|
const uint8_t *message;
|
|
|
|
DstBuffer buf;
|
|
|
|
dst_buffer_init(&buf, 20);
|
|
|
|
dst_buffer_push_string(&buf, dst_formatc("bad slot #%d, expected ", n));
|
|
|
|
dst_buffer_push_types(&buf, expected);
|
2018-04-28 22:10:57 +00:00
|
|
|
dst_buffer_push_cstring(&buf, ", got ");
|
2018-04-30 00:13:04 +00:00
|
|
|
dst_buffer_push_cstring(&buf, typestr(args, n));
|
2018-04-28 22:10:57 +00:00
|
|
|
message = dst_string(buf.data, buf.count);
|
|
|
|
dst_buffer_deinit(&buf);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROWV(args, dst_wrap_string(message));
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_arity_err(DstArgs args, int32_t n, const char *prefix) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROWV(args,
|
2018-04-28 22:10:57 +00:00
|
|
|
dst_wrap_string(dst_formatc(
|
2018-05-20 01:16:00 +00:00
|
|
|
"expected %s%d argument%s, got %d",
|
2018-04-28 22:10:57 +00:00
|
|
|
prefix, n, n == 1 ? "" : "s", args.n)));
|
|
|
|
}
|
2018-04-30 00:13:04 +00:00
|
|
|
|
2018-05-06 03:51:29 +00:00
|
|
|
int dst_typeabstract_err(DstArgs args, int32_t n, const DstAbstractType *at) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROWV(args,
|
2018-04-30 00:13:04 +00:00
|
|
|
dst_wrap_string(dst_formatc(
|
2018-05-20 01:16:00 +00:00
|
|
|
"bad slot #%d, expected %s, got %s",
|
|
|
|
n, at->name, typestr(args, n))));
|
2018-04-30 00:13:04 +00:00
|
|
|
}
|