2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 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>
|
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 */
|
2017-12-03 17:52:09 +00:00
|
|
|
const char *dst_type_names[16] = {
|
2017-11-06 03:05:47 +00:00
|
|
|
"nil",
|
2017-12-03 17:52:09 +00:00
|
|
|
"false",
|
|
|
|
"true",
|
|
|
|
"fiber",
|
2017-11-06 03:05:47 +00:00
|
|
|
"integer",
|
2017-12-03 17:52:09 +00:00
|
|
|
"real",
|
2017-11-06 03:05:47 +00:00
|
|
|
"string",
|
|
|
|
"symbol",
|
|
|
|
"array",
|
|
|
|
"tuple",
|
|
|
|
"table",
|
|
|
|
"struct",
|
|
|
|
"buffer",
|
|
|
|
"function",
|
|
|
|
"cfunction",
|
|
|
|
"userdata"
|
|
|
|
};
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Computes hash of an array of values */
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t dst_array_calchash(const DstValue *array, int32_t len) {
|
2017-11-27 19:03:34 +00:00
|
|
|
const DstValue *end = array + len;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate hash for string */
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t dst_string_calchash(const uint8_t *str, int32_t len) {
|
2017-11-27 19:03:34 +00:00
|
|
|
const uint8_t *end = str + len;
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (str < end)
|
|
|
|
hash = (hash << 5) + hash + *str++;
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) hash;
|
2017-11-27 19:03:34 +00:00
|
|
|
}
|
|
|
|
|
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. */
|
2017-11-28 23:27:55 +00:00
|
|
|
int dst_seq_view(DstValue seq, const DstValue **data, int32_t *len) {
|
|
|
|
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)) {
|
|
|
|
*data = dst_unwrap_struct(seq);
|
|
|
|
*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. */
|
2017-11-28 23:27:55 +00:00
|
|
|
int dst_chararray_view(DstValue str, const uint8_t **data, int32_t *len) {
|
|
|
|
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. */
|
2017-11-28 23:27:55 +00:00
|
|
|
int dst_hashtable_view(DstValue tab, const DstValue **data, int32_t *len, int32_t *cap) {
|
|
|
|
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;
|
|
|
|
}
|
2017-12-08 20:57:02 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Load c functions into an environment */
|
|
|
|
DstValue dst_loadreg(DstReg *regs, size_t count) {
|
|
|
|
size_t i;
|
|
|
|
DstTable *t = dst_table(count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
DstValue sym = dst_csymbolv(regs[i].name);
|
|
|
|
DstValue func = dst_wrap_cfunction(regs[i].function);
|
|
|
|
DstTable *subt = dst_table(1);
|
|
|
|
dst_table_put(subt, dst_csymbolv("value"), func);
|
|
|
|
dst_table_put(t, sym, dst_wrap_table(subt));
|
|
|
|
}
|
|
|
|
return dst_wrap_table(t);
|
|
|
|
}
|