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-09-09 18:39:51 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
static const char DST_EXPECTED_INTEGER[] = "expected integer";
|
|
|
|
static const char DST_EXPECTED_STRING[] = "expected string";
|
|
|
|
|
|
|
|
static const char *types[] = {
|
|
|
|
"nil",
|
|
|
|
"real",
|
|
|
|
"integer",
|
|
|
|
"boolean",
|
|
|
|
"string",
|
|
|
|
"symbol",
|
|
|
|
"array",
|
|
|
|
"tuple",
|
|
|
|
"table",
|
|
|
|
"struct",
|
|
|
|
"thread",
|
|
|
|
"buffer",
|
|
|
|
"function",
|
|
|
|
"cfunction",
|
|
|
|
"userdata",
|
|
|
|
"funcenv",
|
|
|
|
"funcdef"
|
|
|
|
};
|
|
|
|
|
|
|
|
static DstValue nil() {
|
|
|
|
DstValue n;
|
|
|
|
n.type = DST_NIL;
|
2017-11-01 21:53:43 +00:00
|
|
|
n.as.integer = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DstValue integer(DstInteger i) {
|
|
|
|
DstValue n;
|
|
|
|
n.type = DST_INTEGER;
|
2017-11-01 21:53:43 +00:00
|
|
|
n.as.integer = i;
|
2017-09-09 18:39:51 +00:00
|
|
|
return n;
|
|
|
|
}
|
2017-04-24 17:12:55 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstValue real(DstReal r) {
|
|
|
|
DstValue n;
|
|
|
|
n.type = DST_REAL;
|
2017-11-01 21:53:43 +00:00
|
|
|
n.as.real = r;
|
2017-09-09 18:39:51 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DstValue boolean(int b) {
|
|
|
|
DstValue n;
|
|
|
|
n.type = DST_BOOLEAN;
|
2017-11-01 21:53:43 +00:00
|
|
|
n.as.boolean = b;
|
2017-09-09 18:39:51 +00:00
|
|
|
return n;
|
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
|
|
|
|
/***/
|
|
|
|
/* Arithmetic */
|
|
|
|
/***/
|
|
|
|
|
2017-04-24 20:02:54 +00:00
|
|
|
#define MAKE_BINOP(name, op)\
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstValue dst_stl_binop_##name(DstValue lhs, DstValue rhs) {\
|
|
|
|
if (lhs.type == DST_INTEGER)\
|
|
|
|
if (rhs.type == DST_INTEGER)\
|
2017-11-01 21:53:43 +00:00
|
|
|
return integer(lhs.as.integer op rhs.as.integer);\
|
2017-09-09 18:39:51 +00:00
|
|
|
else if (rhs.type == DST_REAL)\
|
2017-11-01 21:53:43 +00:00
|
|
|
return real(lhs.as.integer op rhs.as.real);\
|
2017-04-24 20:02:54 +00:00
|
|
|
else\
|
2017-09-09 18:39:51 +00:00
|
|
|
return nil();\
|
|
|
|
else if (lhs.type == DST_REAL)\
|
|
|
|
if (rhs.type == DST_INTEGER)\
|
2017-11-01 21:53:43 +00:00
|
|
|
return real(lhs.as.real op rhs.as.integer);\
|
2017-09-09 18:39:51 +00:00
|
|
|
else if (rhs.type == DST_REAL)\
|
2017-11-01 21:53:43 +00:00
|
|
|
return real(lhs.as.real op rhs.as.real);\
|
2017-04-24 20:02:54 +00:00
|
|
|
else\
|
2017-09-09 18:39:51 +00:00
|
|
|
return nil();\
|
2017-04-24 20:02:54 +00:00
|
|
|
else\
|
2017-09-09 18:39:51 +00:00
|
|
|
return nil();\
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 16:08:48 +00:00
|
|
|
#define SIMPLE_ACCUM_FUNCTION(name, op)\
|
2017-04-24 20:02:54 +00:00
|
|
|
MAKE_BINOP(name, op)\
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_##name(Dst* vm) {\
|
|
|
|
DstValue lhs, rhs;\
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t j, count;\
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);\
|
|
|
|
lhs = dst_arg(vm, 0);\
|
2017-04-12 14:31:50 +00:00
|
|
|
for (j = 1; j < count; ++j) {\
|
2017-09-09 18:39:51 +00:00
|
|
|
rhs = dst_arg(vm, j);\
|
|
|
|
lhs = dst_stl_binop_##name(lhs, rhs);\
|
2017-04-12 14:31:50 +00:00
|
|
|
}\
|
2017-09-09 18:39:51 +00:00
|
|
|
if (lhs.type == DST_NIL)\
|
|
|
|
dst_c_throwc(vm, "expected integer/real");\
|
|
|
|
dst_c_return(vm, lhs);\
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 16:08:48 +00:00
|
|
|
SIMPLE_ACCUM_FUNCTION(add, +)
|
|
|
|
SIMPLE_ACCUM_FUNCTION(mul, *)
|
|
|
|
SIMPLE_ACCUM_FUNCTION(sub, -)
|
|
|
|
|
|
|
|
/* Detect division by zero */
|
|
|
|
MAKE_BINOP(div, /)
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_div(Dst *vm) {
|
|
|
|
DstValue lhs, rhs;
|
2017-05-08 16:08:48 +00:00
|
|
|
uint32_t j, count;
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);
|
|
|
|
lhs = dst_arg(vm, 0);
|
2017-05-08 16:08:48 +00:00
|
|
|
for (j = 1; j < count; ++j) {
|
2017-09-09 18:39:51 +00:00
|
|
|
rhs = dst_arg(vm, j);
|
2017-11-01 21:53:43 +00:00
|
|
|
if (lhs.type == DST_INTEGER && rhs.type == DST_INTEGER && rhs.as.integer == 0)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "cannot integer divide by 0");
|
|
|
|
lhs = dst_stl_binop_div(lhs, rhs);
|
2017-05-08 16:08:48 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (lhs.type == DST_NIL)
|
|
|
|
dst_c_throwc(vm, "expected integer/real");
|
|
|
|
dst_c_return(vm, lhs);
|
2017-05-08 16:08:48 +00:00
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
|
2017-04-24 20:02:54 +00:00
|
|
|
#undef SIMPLE_ACCUM_FUNCTION
|
2017-04-12 14:31:50 +00:00
|
|
|
|
2017-06-11 13:38:37 +00:00
|
|
|
#define BITWISE_FUNCTION(name, op) \
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_##name(Dst *vm) {\
|
|
|
|
DstValue ret;\
|
2017-06-11 13:38:37 +00:00
|
|
|
uint32_t i, count;\
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);\
|
|
|
|
ret = dst_arg(vm, 0);\
|
|
|
|
if (ret.type != DST_INTEGER) {\
|
|
|
|
dst_c_throwc(vm, "expected integer");\
|
2017-06-11 13:38:37 +00:00
|
|
|
}\
|
|
|
|
if (count < 2) {\
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ret);\
|
2017-06-11 13:38:37 +00:00
|
|
|
}\
|
|
|
|
for (i = 1; i < count; ++i) {\
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue next = dst_arg(vm, i);\
|
|
|
|
if (next.type != DST_INTEGER) {\
|
|
|
|
dst_c_throwc(vm, "expected integer");\
|
2017-06-11 13:38:37 +00:00
|
|
|
}\
|
2017-11-01 21:53:43 +00:00
|
|
|
ret.as.integer = ret.as.integer op next.as.integer;\
|
2017-06-11 13:38:37 +00:00
|
|
|
}\
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ret);\
|
2017-06-11 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BITWISE_FUNCTION(band, &)
|
|
|
|
BITWISE_FUNCTION(bor, |)
|
|
|
|
BITWISE_FUNCTION(bxor, ^)
|
|
|
|
BITWISE_FUNCTION(blshift, <<)
|
|
|
|
BITWISE_FUNCTION(brshift, >>)
|
|
|
|
|
|
|
|
#undef BITWISE_FUNCTION
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_bnot(Dst *vm) {
|
|
|
|
DstValue in = dst_arg(vm, 0);
|
|
|
|
uint32_t count = dst_args(vm);
|
|
|
|
if (count != 1 || in.type != DST_INTEGER) {
|
|
|
|
dst_c_throwc(vm, "expected 1 integer argument");
|
2017-06-11 13:38:37 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
in.as.integer = ~in.as.integer;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, in);
|
2017-06-11 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
#define COMPARE_FUNCTION(name, check)\
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_##name(Dst *vm) {\
|
|
|
|
DstValue ret;\
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t i, count;\
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);\
|
2017-11-01 21:53:43 +00:00
|
|
|
ret.as.boolean = 1;\
|
2017-09-09 18:39:51 +00:00
|
|
|
ret.type = DST_BOOLEAN;\
|
2017-04-12 14:31:50 +00:00
|
|
|
if (count < 2) {\
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ret);\
|
2017-04-12 14:31:50 +00:00
|
|
|
}\
|
|
|
|
for (i = 1; i < count; ++i) {\
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue lhs = dst_arg(vm, i - 1);\
|
|
|
|
DstValue rhs = dst_arg(vm, i);\
|
2017-04-12 14:31:50 +00:00
|
|
|
if (!(check)) {\
|
2017-11-01 21:53:43 +00:00
|
|
|
ret.as.boolean = 0;\
|
2017-04-12 14:31:50 +00:00
|
|
|
break;\
|
|
|
|
}\
|
|
|
|
}\
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ret);\
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
COMPARE_FUNCTION(lessthan, dst_compare(lhs, rhs) < 0)
|
|
|
|
COMPARE_FUNCTION(greaterthan, dst_compare(lhs, rhs) > 0)
|
|
|
|
COMPARE_FUNCTION(equal, dst_equals(lhs, rhs))
|
|
|
|
COMPARE_FUNCTION(notequal, !dst_equals(lhs, rhs))
|
|
|
|
COMPARE_FUNCTION(lessthaneq, dst_compare(lhs, rhs) <= 0)
|
|
|
|
COMPARE_FUNCTION(greaterthaneq, dst_compare(lhs, rhs) >= 0)
|
2017-04-12 14:31:50 +00:00
|
|
|
|
|
|
|
#undef COMPARE_FUNCTION
|
|
|
|
|
2017-04-24 22:09:23 +00:00
|
|
|
/* Boolean not */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_not(Dst *vm) {
|
|
|
|
dst_c_return(vm, boolean(!dst_truthy(dst_arg(vm, 0))));
|
2017-04-24 22:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Core */
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
|
2017-07-15 16:21:06 +00:00
|
|
|
/* Empty a mutable datastructure */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_clear(Dst *vm) {
|
|
|
|
DstValue x = dst_arg(vm, 0);
|
2017-07-15 16:21:06 +00:00
|
|
|
switch (x.type) {
|
|
|
|
default:
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "cannot clear");
|
|
|
|
case DST_ARRAY:
|
2017-11-01 21:53:43 +00:00
|
|
|
x.as.array->count = 0;
|
2017-07-15 16:21:06 +00:00
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_BUFFER:
|
|
|
|
x.as.buffer->count = 0;
|
2017-07-15 16:21:06 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TABLE:
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_table_clear(x.as.table);
|
2017-07-15 16:21:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, x);
|
2017-07-15 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Get length of object */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_length(Dst *vm) {
|
|
|
|
dst_set_integer(vm, 0, dst_length(vm, 0));
|
|
|
|
dst_return(vm, 0);
|
|
|
|
return 0;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 16:26:58 +00:00
|
|
|
/* Get hash of a value */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_hash(Dst *vm) {
|
|
|
|
dst_set_integer(vm, 0, dst_hash(vm, 0););
|
|
|
|
dst_return(vm, 0);
|
|
|
|
return 0;
|
2017-04-25 16:26:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 20:02:54 +00:00
|
|
|
/* Convert to integer */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_to_int(Dst *vm) {
|
|
|
|
DstValue x = dst_arg(vm, 0);
|
|
|
|
if (x.type == DST_INTEGER) dst_c_return(vm, x);
|
|
|
|
if (x.type == DST_REAL)
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, integer((DstInteger) x.as.real));
|
2017-04-24 20:02:54 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected number");
|
2017-04-24 20:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert to integer */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_to_real(Dst *vm) {
|
|
|
|
DstValue x = dst_arg(vm, 0);
|
|
|
|
if (x.type == DST_REAL) dst_c_return(vm, x);
|
|
|
|
if (x.type == DST_INTEGER)
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_wrap_real((DstReal) x.as.integer));
|
2017-04-24 20:02:54 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected number");
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
/* Get a slice of a sequence */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_slice(Dst *vm) {
|
|
|
|
uint32_t count = dst_args(vm);
|
2017-04-19 16:56:29 +00:00
|
|
|
int32_t from, to;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue x;
|
|
|
|
const DstValue *data;
|
2017-05-06 21:46:28 +00:00
|
|
|
const uint8_t *cdata;
|
2017-04-19 16:56:29 +00:00
|
|
|
uint32_t length;
|
|
|
|
uint32_t newlength;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstInteger num;
|
2017-04-19 16:56:29 +00:00
|
|
|
|
|
|
|
/* Get data */
|
2017-09-09 18:39:51 +00:00
|
|
|
x = dst_arg(vm, 0);
|
|
|
|
if (!dst_seq_view(x, &data, &length) &&
|
|
|
|
!dst_chararray_view(x, &cdata, &length)) {
|
|
|
|
dst_c_throwc(vm, "expected array/tuple/buffer/symbol/string");
|
2017-05-06 21:46:28 +00:00
|
|
|
}
|
2017-04-19 16:56:29 +00:00
|
|
|
|
|
|
|
/* Get from index */
|
|
|
|
if (count < 2) {
|
|
|
|
from = 0;
|
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!dst_check_integer(vm, 1, &num))
|
|
|
|
dst_c_throwc(vm, DST_EXPECTED_INTEGER);
|
|
|
|
from = dst_startrange(num, length);
|
2017-04-19 16:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get to index */
|
|
|
|
if (count < 3) {
|
|
|
|
to = length;
|
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!dst_check_integer(vm, 2, &num))
|
|
|
|
dst_c_throwc(vm, DST_EXPECTED_INTEGER);
|
|
|
|
to = dst_endrange(num, length);
|
2017-04-19 16:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check from bad bounds */
|
2017-05-06 21:46:28 +00:00
|
|
|
if (from < 0 || to < 0 || to < from)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "index out of bounds");
|
2017-04-19 16:56:29 +00:00
|
|
|
|
|
|
|
/* Build slice */
|
|
|
|
newlength = to - from;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (x.type == DST_TUPLE) {
|
|
|
|
DstValue *tup = dst_tuple_begin(vm, newlength);
|
|
|
|
dst_memcpy(tup, data + from, newlength * sizeof(DstValue));
|
|
|
|
dst_c_return(vm, dst_wrap_tuple(dst_tuple_end(vm, tup)));
|
|
|
|
} else if (x.type == DST_ARRAY) {
|
|
|
|
DstArray *arr = dst_array(vm, newlength);
|
2017-04-19 16:56:29 +00:00
|
|
|
arr->count = newlength;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_memcpy(arr->data, data + from, newlength * sizeof(DstValue));
|
|
|
|
dst_c_return(vm, dst_wrap_array(arr));
|
|
|
|
} else if (x.type == DST_STRING) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_wrap_string(dst_string_b(vm, x.as.string + from, newlength)));
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (x.type == DST_SYMBOL) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_wrap_symbol(dst_string_b(vm, x.as.string + from, newlength)));
|
2017-05-06 21:46:28 +00:00
|
|
|
} else { /* buffer */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *b = dst_buffer(vm, newlength);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_memcpy(b->data, x.as.buffer->data, newlength);
|
2017-05-06 21:46:28 +00:00
|
|
|
b->count = newlength;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_buffer(b));
|
2017-04-19 16:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Get type of object */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_type(Dst *vm) {
|
|
|
|
DstValue x;
|
2017-04-12 14:31:50 +00:00
|
|
|
const char *typestr = "nil";
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
2017-04-12 14:31:50 +00:00
|
|
|
if (count == 0)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected at least 1 argument");
|
|
|
|
x = dst_arg(vm, 0);
|
2017-04-12 14:31:50 +00:00
|
|
|
switch (x.type) {
|
|
|
|
default:
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-04-24 20:02:54 +00:00
|
|
|
typestr = "real";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-04-24 20:02:54 +00:00
|
|
|
typestr = "integer";
|
2017-04-12 14:31:50 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_BOOLEAN:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "boolean";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "string";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_SYMBOL:
|
2017-07-02 21:17:24 +00:00
|
|
|
typestr = "symbol";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_ARRAY:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "array";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "tuple";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_THREAD:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "thread";
|
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_BUFFER:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "buffer";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_FUNCTION:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "function";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_CFUNCTION:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "cfunction";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TABLE:
|
2017-04-25 01:00:56 +00:00
|
|
|
typestr = "table";
|
2017-04-12 14:31:50 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_USERDATA:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "userdata";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_FUNCENV:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "funcenv";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_FUNCDEF:
|
2017-04-12 14:31:50 +00:00
|
|
|
typestr = "funcdef";
|
|
|
|
break;
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_string_cv(vm, typestr));
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Create array */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_array(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
|
|
|
DstArray *array = dst_array(vm, count);
|
2017-04-17 22:46:28 +00:00
|
|
|
for (i = 0; i < count; ++i)
|
2017-09-09 18:39:51 +00:00
|
|
|
array->data[i] = dst_arg(vm, i);
|
|
|
|
dst_c_return(vm, dst_wrap_array(array));
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Create tuple */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_tuple(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
|
|
|
DstValue *tuple= dst_tuple_begin(vm, count);
|
2017-04-17 22:46:28 +00:00
|
|
|
for (i = 0; i < count; ++i)
|
2017-09-09 18:39:51 +00:00
|
|
|
tuple[i] = dst_arg(vm, i);
|
|
|
|
dst_c_return(vm, dst_wrap_tuple(dst_tuple_end(vm, tuple)));
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create object */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_table(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
|
|
|
DstTable *table;
|
2017-04-17 22:46:28 +00:00
|
|
|
if (count % 2 != 0)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected even number of arguments");
|
|
|
|
table = dst_table(vm, 4 * count);
|
2017-04-17 22:46:28 +00:00
|
|
|
for (i = 0; i < count; i += 2)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_table_put(vm, table, dst_arg(vm, i), dst_arg(vm, i + 1));
|
|
|
|
dst_c_return(vm, dst_wrap_table(table));
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 20:05:59 +00:00
|
|
|
/* Create struct */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_struct(Dst *vm) {
|
2017-04-15 20:05:59 +00:00
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
|
|
|
DstValue *st;
|
2017-04-17 22:46:28 +00:00
|
|
|
if (count % 2 != 0)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected even number of arguments");
|
|
|
|
st = dst_struct_begin(vm, count / 2);
|
2017-04-17 22:46:28 +00:00
|
|
|
for (i = 0; i < count; i += 2)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_struct_put(st, dst_arg(vm, i), dst_arg(vm, i + 1));
|
|
|
|
dst_c_return(vm, dst_wrap_struct(dst_struct_end(vm, st)));
|
2017-04-15 20:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 03:06:38 +00:00
|
|
|
/* Create a buffer */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_buffer(Dst *vm) {
|
2017-03-21 03:06:38 +00:00
|
|
|
uint32_t i, count;
|
2017-04-26 14:21:03 +00:00
|
|
|
const uint8_t *dat;
|
|
|
|
uint32_t slen;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstBuffer *buf = dst_buffer(vm, 10);
|
|
|
|
count = dst_args(vm);
|
2017-03-21 03:06:38 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_chararray_view(dst_arg(vm, i), &dat, &slen))
|
|
|
|
dst_buffer_append(vm, buf, dat, slen);
|
2017-04-26 14:21:03 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, DST_EXPECTED_STRING);
|
2017-03-21 03:06:38 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_buffer(buf));
|
2017-03-21 03:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:21:03 +00:00
|
|
|
/* Create a string */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_string(Dst *vm) {
|
2017-04-24 17:12:55 +00:00
|
|
|
uint32_t j;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t count = dst_args(vm);
|
2017-04-24 17:12:55 +00:00
|
|
|
uint32_t length = 0;
|
|
|
|
uint32_t index = 0;
|
2017-04-12 14:31:50 +00:00
|
|
|
uint8_t *str;
|
2017-04-24 17:12:55 +00:00
|
|
|
const uint8_t *dat;
|
|
|
|
uint32_t slen;
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Find length and assert string arguments */
|
|
|
|
for (j = 0; j < count; ++j) {
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!dst_chararray_view(dst_arg(vm, j), &dat, &slen)) {
|
|
|
|
DstValue newarg;
|
|
|
|
dat = dst_to_string(vm, dst_arg(vm, j));
|
|
|
|
slen = dst_string_length(dat);
|
|
|
|
newarg.type = DST_STRING;
|
2017-11-01 21:53:43 +00:00
|
|
|
newarg.as.string = dat;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_set_arg(vm, j, newarg);
|
2017-07-04 02:20:39 +00:00
|
|
|
}
|
|
|
|
length += slen;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
/* Make string */
|
2017-09-09 18:39:51 +00:00
|
|
|
str = dst_string_begin(vm, length);
|
2017-04-12 14:31:50 +00:00
|
|
|
for (j = 0; j < count; ++j) {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_chararray_view(dst_arg(vm, j), &dat, &slen);
|
|
|
|
dst_memcpy(str + index, dat, slen);
|
2017-04-12 14:31:50 +00:00
|
|
|
index += slen;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_string(dst_string_end(vm, str)));
|
2017-03-21 03:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 15:21:10 +00:00
|
|
|
/* Create a symbol */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_symbol(Dst *vm) {
|
|
|
|
int ret = dst_stl_string(vm);
|
|
|
|
if (ret == DST_RETURN_OK) {
|
|
|
|
vm->ret.type = DST_SYMBOL;
|
2017-07-03 15:21:10 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:07:16 +00:00
|
|
|
/* Create a thread */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_thread(Dst *vm) {
|
|
|
|
DstThread *t;
|
|
|
|
DstValue callee = dst_arg(vm, 0);
|
|
|
|
DstValue parent = dst_arg(vm, 1);
|
|
|
|
DstValue errorParent = dst_arg(vm, 2);
|
|
|
|
t = dst_thread(vm, callee, 10);
|
|
|
|
if (callee.type != DST_FUNCTION && callee.type != DST_CFUNCTION)
|
|
|
|
dst_c_throwc(vm, "expected function in thread constructor");
|
|
|
|
if (parent.type == DST_THREAD) {
|
2017-11-01 21:53:43 +00:00
|
|
|
t->parent = parent.as.thread;
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (parent.type != DST_NIL) {
|
|
|
|
dst_c_throwc(vm, "expected thread/nil as parent");
|
2017-06-30 01:57:09 +00:00
|
|
|
} else {
|
|
|
|
t->parent = vm->thread;
|
2017-06-20 03:01:34 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_thread(t));
|
2017-05-08 21:07:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 17:18:07 +00:00
|
|
|
/* Get current thread */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_current(Dst *vm) {
|
|
|
|
dst_c_return(vm, dst_wrap_thread(vm->thread));
|
2017-05-09 17:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get parent of a thread */
|
|
|
|
/* TODO - consider implications of this function
|
|
|
|
* for sandboxing */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_parent(Dst *vm) {
|
|
|
|
DstThread *t;
|
|
|
|
if (!dst_check_thread(vm, 0, &t))
|
|
|
|
dst_c_throwc(vm, "expected thread");
|
2017-05-09 17:18:07 +00:00
|
|
|
if (t->parent == NULL)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_nil());
|
|
|
|
dst_c_return(vm, dst_wrap_thread(t->parent));
|
2017-05-09 17:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the status of a thread */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_status(Dst *vm) {
|
|
|
|
DstThread *t;
|
2017-05-09 17:18:07 +00:00
|
|
|
const char *cstr;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!dst_check_thread(vm, 0, &t))
|
|
|
|
dst_c_throwc(vm, "expected thread");
|
2017-05-09 17:18:07 +00:00
|
|
|
switch (t->status) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_THREAD_PENDING:
|
2017-05-09 17:18:07 +00:00
|
|
|
cstr = "pending";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_THREAD_ALIVE:
|
2017-05-09 17:18:07 +00:00
|
|
|
cstr = "alive";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_THREAD_DEAD:
|
2017-05-09 17:18:07 +00:00
|
|
|
cstr = "dead";
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_THREAD_ERROR:
|
2017-05-09 17:18:07 +00:00
|
|
|
cstr = "error";
|
|
|
|
break;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_string_cv(vm, cstr));
|
2017-05-09 17:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 13:44:52 +00:00
|
|
|
/* Associative get */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_get(Dst *vm) {
|
|
|
|
DstValue ret;
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t count;
|
|
|
|
const char *err;
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);
|
2017-04-24 17:12:55 +00:00
|
|
|
if (count != 2)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expects 2 arguments");
|
|
|
|
err = dst_get(dst_arg(vm, 0), dst_arg(vm, 1), &ret);
|
2017-04-12 14:31:50 +00:00
|
|
|
if (err != NULL)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, err);
|
2017-04-12 14:31:50 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ret);
|
2017-03-22 04:27:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 13:44:52 +00:00
|
|
|
/* Associative set */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_set(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t count;
|
|
|
|
const char *err;
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);
|
2017-04-24 17:12:55 +00:00
|
|
|
if (count != 3)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expects 3 arguments");
|
|
|
|
err = dst_set(vm, dst_arg(vm, 0), dst_arg(vm, 1), dst_arg(vm, 2));
|
2017-04-24 17:12:55 +00:00
|
|
|
if (err != NULL)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, err);
|
2017-04-24 17:12:55 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_arg(vm, 0));
|
2017-04-24 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push to end of array */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_push(Dst *vm) {
|
|
|
|
DstValue ds = dst_arg(vm, 0);
|
|
|
|
if (ds.type != DST_ARRAY)
|
|
|
|
dst_c_throwc(vm, "expected array");
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_array_push(vm, ds.as.array, dst_arg(vm, 1));
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ds);
|
2017-04-24 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop from end of array */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_pop(Dst *vm) {
|
|
|
|
DstValue ds = dst_arg(vm, 0);
|
|
|
|
if (ds.type != DST_ARRAY)
|
|
|
|
dst_c_throwc(vm, "expected array");
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_array_pop(ds.as.array));
|
2017-04-24 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Peek at end of array */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_peek(Dst *vm) {
|
|
|
|
DstValue ds = dst_arg(vm, 0);
|
|
|
|
if (ds.type != DST_ARRAY)
|
|
|
|
dst_c_throwc(vm, "expected array");
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_array_peek(ds.as.array));
|
2017-04-24 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure array capacity */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_ensure(Dst *vm) {
|
|
|
|
DstValue ds = dst_arg(vm, 0);
|
|
|
|
DstValue cap = dst_arg(vm, 1);
|
|
|
|
if (ds.type != DST_ARRAY)
|
|
|
|
dst_c_throwc(vm, "expected array");
|
|
|
|
if (cap.type != DST_INTEGER)
|
|
|
|
dst_c_throwc(vm, DST_EXPECTED_INTEGER);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_array_ensure(vm, ds.as.array, (uint32_t) cap.as.integer);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, ds);
|
2017-03-22 04:27:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
/* Get next key in struct or table */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_next(Dst *vm) {
|
|
|
|
DstValue ds = dst_arg(vm, 0);
|
|
|
|
DstValue key = dst_arg(vm, 1);
|
|
|
|
if (ds.type == DST_TABLE) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_table_next(ds.as.table, key));
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (ds.type == DST_STRUCT) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_c_return(vm, dst_struct_next(ds.as.st, key));
|
2017-04-16 13:39:41 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "expected table or struct");
|
2017-04-16 13:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Print values for inspection */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_print(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
uint32_t j, count;
|
2017-09-09 18:39:51 +00:00
|
|
|
count = dst_args(vm);
|
2017-04-12 14:31:50 +00:00
|
|
|
for (j = 0; j < count; ++j) {
|
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
const uint8_t *string = dst_to_string(vm, dst_arg(vm, j));
|
|
|
|
uint32_t len = dst_string_length(string);
|
2017-04-12 14:31:50 +00:00
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
fputc(string[i], stdout);
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
fputc('\n', stdout);
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 18:16:37 +00:00
|
|
|
/* Long description */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_description(Dst *vm) {
|
|
|
|
DstValue x = dst_arg(vm, 0);
|
|
|
|
const uint8_t *buf = dst_description(vm, x);
|
|
|
|
dst_c_return(vm, dst_wrap_string(buf));
|
2017-07-02 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Short description */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_short_description(Dst *vm) {
|
|
|
|
DstValue x = dst_arg(vm, 0);
|
|
|
|
const uint8_t *buf = dst_short_description(vm, x);
|
|
|
|
dst_c_return(vm, dst_wrap_string(buf));
|
2017-07-02 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Exit */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_exit(Dst *vm) {
|
2017-04-12 14:31:50 +00:00
|
|
|
int ret;
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue x = dst_arg(vm, 0);
|
2017-11-01 21:53:43 +00:00
|
|
|
ret = x.type == DST_INTEGER ? x.as.integer : (x.type == DST_REAL ? x.as.real : 0);
|
2017-04-12 14:31:50 +00:00
|
|
|
exit(ret);
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Throw error */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_error(Dst *vm) {
|
|
|
|
dst_c_throw(vm, dst_arg(vm, 0));
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:08:29 +00:00
|
|
|
/***/
|
|
|
|
/* Function reflection */
|
|
|
|
/***/
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_funcenv(Dst *vm) {
|
|
|
|
DstFunction *fn;
|
|
|
|
if (!dst_check_function(vm, 0, &fn))
|
|
|
|
dst_c_throwc(vm, "expected function");
|
2017-05-12 01:30:18 +00:00
|
|
|
if (fn->env)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_funcenv(fn->env));
|
2017-05-12 01:30:18 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-05-11 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_funcdef(Dst *vm) {
|
|
|
|
DstFunction *fn;
|
|
|
|
if (!dst_check_function(vm, 0, &fn))
|
|
|
|
dst_c_throwc(vm, "expected function");
|
|
|
|
dst_c_return(vm, dst_wrap_funcdef(fn->def));
|
2017-05-11 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_funcparent(Dst *vm) {
|
|
|
|
DstFunction *fn;
|
|
|
|
if (!dst_check_function(vm, 0, &fn))
|
|
|
|
dst_c_throwc(vm, "expected function");
|
2017-05-11 21:08:29 +00:00
|
|
|
if (fn->parent)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_function(fn->parent));
|
2017-05-11 21:08:29 +00:00
|
|
|
else
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-05-11 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_def(Dst *vm) {
|
|
|
|
DstValue key = dst_arg(vm, 0);
|
|
|
|
if (dst_args(vm) != 2) {
|
|
|
|
dst_c_throwc(vm, "expected 2 arguments to global-def");
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (key.type != DST_STRING && key.type != DST_SYMBOL) {
|
|
|
|
dst_c_throwc(vm, "expected string/symbol as first argument");
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
key.type = DST_SYMBOL;
|
|
|
|
dst_env_put(vm, vm->env, key, dst_arg(vm, 1));
|
|
|
|
dst_c_return(vm, dst_arg(vm, 1));
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_var(Dst *vm) {
|
|
|
|
DstValue key = dst_arg(vm, 0);
|
|
|
|
if (dst_args(vm) != 2) {
|
|
|
|
dst_c_throwc(vm, "expected 2 arguments to global-var");
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (key.type != DST_STRING && key.type != DST_SYMBOL) {
|
|
|
|
dst_c_throwc(vm, "expected string as first argument");
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
key.type = DST_SYMBOL;
|
|
|
|
dst_env_putvar(vm, vm->env, key, dst_arg(vm, 1));
|
|
|
|
dst_c_return(vm, dst_arg(vm, 1));
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
/* IO */
|
|
|
|
/****/
|
|
|
|
|
2017-04-26 14:21:03 +00:00
|
|
|
/* File type definition */
|
2017-09-09 18:39:51 +00:00
|
|
|
static DstUserType dst_stl_filetype = {
|
2017-05-11 21:08:29 +00:00
|
|
|
"std.file",
|
2017-04-26 14:21:03 +00:00
|
|
|
NULL,
|
2017-07-02 01:51:16 +00:00
|
|
|
NULL,
|
2017-05-03 23:57:06 +00:00
|
|
|
NULL,
|
2017-04-26 14:21:03 +00:00
|
|
|
NULL
|
|
|
|
};
|
2017-03-09 18:49:46 +00:00
|
|
|
|
2017-04-24 17:12:55 +00:00
|
|
|
/* Open a a file and return a userdata wrapper arounf the C file API. */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_open(Dst *vm) {
|
|
|
|
const uint8_t *fname = dst_to_string(vm, dst_arg(vm, 0));
|
|
|
|
const uint8_t *fmode = dst_to_string(vm, dst_arg(vm, 1));
|
2017-04-18 02:14:35 +00:00
|
|
|
FILE *f;
|
2017-04-18 20:55:03 +00:00
|
|
|
FILE **fp;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_args(vm) < 2 || dst_arg(vm, 0).type != DST_STRING
|
|
|
|
|| dst_arg(vm, 1).type != DST_STRING)
|
|
|
|
dst_c_throwc(vm, "expected filename and filemode");
|
2017-04-18 02:14:35 +00:00
|
|
|
f = fopen((const char *)fname, (const char *)fmode);
|
|
|
|
if (!f)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "could not open file");
|
|
|
|
fp = dst_userdata(vm, sizeof(FILE *), &dst_stl_filetype);
|
2017-04-24 17:12:55 +00:00
|
|
|
*fp = f;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_userdata(fp));
|
2017-04-18 02:14:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:21:03 +00:00
|
|
|
/* Read an entire file into memory */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_slurp(Dst *vm) {
|
|
|
|
DstBuffer *b;
|
2017-04-26 14:21:03 +00:00
|
|
|
long fsize;
|
|
|
|
FILE *f;
|
2017-09-09 18:39:51 +00:00
|
|
|
FILE **fp = dst_check_userdata(vm, 0, &dst_stl_filetype);
|
|
|
|
if (fp == NULL) dst_c_throwc(vm, "expected file");
|
|
|
|
if (!dst_check_buffer(vm, 1, &b)) b = dst_buffer(vm, 10);
|
2017-04-26 14:21:03 +00:00
|
|
|
f = *fp;
|
|
|
|
/* Read whole file */
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
fsize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
/* Ensure buffer size */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_ensure(vm, b, b->count + fsize);
|
2017-04-26 14:21:03 +00:00
|
|
|
fread((char *)(b->data + b->count), fsize, 1, f);
|
|
|
|
b->count += fsize;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_buffer(b));
|
2017-04-26 14:21:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 13:46:26 +00:00
|
|
|
/* Read a certain number of bytes into memory */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_read(Dst *vm) {
|
|
|
|
DstBuffer *b;
|
2017-04-27 13:46:26 +00:00
|
|
|
FILE *f;
|
|
|
|
int64_t len;
|
2017-09-09 18:39:51 +00:00
|
|
|
FILE **fp = dst_check_userdata(vm, 0, &dst_stl_filetype);
|
|
|
|
if (fp == NULL) dst_c_throwc(vm, "expected file");
|
|
|
|
if (!(dst_check_integer(vm, 1, &len))) dst_c_throwc(vm, "expected integer");
|
|
|
|
if (!dst_check_buffer(vm, 2, &b)) b = dst_buffer(vm, 10);
|
2017-04-27 13:46:26 +00:00
|
|
|
f = *fp;
|
|
|
|
/* Ensure buffer size */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_buffer_ensure(vm, b, b->count + len);
|
2017-04-27 13:46:26 +00:00
|
|
|
b->count += fread((char *)(b->data + b->count), len, 1, f) * len;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_buffer(b));
|
2017-04-27 13:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytes to a file */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_write(Dst *vm) {
|
2017-04-27 13:46:26 +00:00
|
|
|
FILE *f;
|
|
|
|
const uint8_t *data;
|
|
|
|
uint32_t len;
|
2017-09-09 18:39:51 +00:00
|
|
|
FILE **fp = dst_check_userdata(vm, 0, &dst_stl_filetype);
|
|
|
|
if (fp == NULL) dst_c_throwc(vm, "expected file");
|
|
|
|
if (!dst_chararray_view(dst_arg(vm, 1), &data, &len)) dst_c_throwc(vm, "expected string|buffer");
|
2017-04-27 13:46:26 +00:00
|
|
|
f = *fp;
|
|
|
|
fwrite(data, len, 1, f);
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-04-27 13:46:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:21:03 +00:00
|
|
|
/* Close a file */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_close(Dst *vm) {
|
|
|
|
FILE **fp = dst_check_userdata(vm, 0, &dst_stl_filetype);
|
|
|
|
if (fp == NULL) dst_c_throwc(vm, "expected file");
|
2017-04-26 14:21:03 +00:00
|
|
|
fclose(*fp);
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_nil());
|
2017-04-26 14:21:03 +00:00
|
|
|
}
|
2017-04-24 17:12:55 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Temporary */
|
|
|
|
/****/
|
|
|
|
|
2017-05-03 23:57:06 +00:00
|
|
|
/* Force garbage collection */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_stl_gcollect(Dst *vm) {
|
|
|
|
dst_collect(vm);
|
|
|
|
return DST_RETURN_OK;
|
2017-06-24 18:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***/
|
|
|
|
/* Compilation */
|
|
|
|
/***/
|
|
|
|
|
2017-07-16 15:00:20 +00:00
|
|
|
/* Generate a unique symbol */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int dst_stl_gensym(Dst *vm) {
|
|
|
|
DstValue source = dst_arg(vm, 0);
|
2017-07-16 15:00:20 +00:00
|
|
|
const uint8_t *sym = NULL;
|
|
|
|
uint32_t len;
|
|
|
|
const uint8_t *data;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (source.type == DST_NIL) {
|
|
|
|
sym = dst_string_cu(vm, "");
|
|
|
|
} else if (dst_chararray_view(source, &data, &len)) {
|
|
|
|
sym = dst_string_bu(vm, data, len);
|
2017-07-16 15:00:20 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_throwc(vm, "exepcted string/buffer/symbol/nil");
|
2017-07-16 15:00:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_wrap_symbol(sym));
|
2017-07-16 15:00:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 18:27:29 +00:00
|
|
|
/* Compile a value */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int dst_stl_compile(Dst *vm) {
|
|
|
|
DstTable *env = vm->env;
|
|
|
|
if (dst_arg(vm, 1).type == DST_TABLE) {
|
2017-11-01 21:53:43 +00:00
|
|
|
env = dst_arg(vm, 1).as.table;
|
2017-06-25 20:36:20 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_c_return(vm, dst_compile(vm, env, dst_arg(vm, 0)));
|
2017-06-24 18:27:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 20:56:03 +00:00
|
|
|
/* Get vm->env */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int dst_stl_getenv(Dst *vm) {
|
|
|
|
dst_c_return(vm, dst_wrap_table(vm->env));
|
2017-07-15 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set vm->env */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int dst_stl_setenv(Dst *vm) {
|
|
|
|
DstValue newEnv = dst_arg(vm, 0);
|
|
|
|
if (newEnv.type != DST_TABLE) {
|
|
|
|
dst_c_throwc(vm, "expected table");
|
2017-07-15 20:56:03 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
vm->env = newEnv.as.table;
|
2017-09-09 18:39:51 +00:00
|
|
|
return DST_RETURN_OK;
|
2017-07-15 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/****/
|
|
|
|
/* Bootstraping */
|
|
|
|
/****/
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
static const DstModuleItem std_module[] = {
|
2017-06-11 13:38:37 +00:00
|
|
|
/* Arithmetic */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"+", dst_stl_add},
|
|
|
|
{"*", dst_stl_mul},
|
|
|
|
{"-", dst_stl_sub},
|
|
|
|
{"/", dst_stl_div},
|
2017-06-11 13:38:37 +00:00
|
|
|
/* Comparisons */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"<", dst_stl_lessthan},
|
|
|
|
{">", dst_stl_greaterthan},
|
|
|
|
{"=", dst_stl_equal},
|
|
|
|
{"not=", dst_stl_notequal},
|
|
|
|
{"<=", dst_stl_lessthaneq},
|
|
|
|
{">=", dst_stl_greaterthaneq},
|
2017-06-11 13:38:37 +00:00
|
|
|
/* Bitwise arithmetic */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"band", dst_stl_band},
|
|
|
|
{"bor", dst_stl_bor},
|
|
|
|
{"bxor", dst_stl_bxor},
|
|
|
|
{"blshift", dst_stl_blshift},
|
|
|
|
{"brshift", dst_stl_brshift},
|
|
|
|
{"bnot", dst_stl_bnot},
|
2017-06-24 18:27:29 +00:00
|
|
|
/* IO */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"open", dst_stl_open},
|
|
|
|
{"slurp", dst_stl_slurp},
|
|
|
|
{"read", dst_stl_read},
|
|
|
|
{"write", dst_stl_write},
|
2017-06-24 18:27:29 +00:00
|
|
|
/* Compile */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"gensym", dst_stl_gensym},
|
|
|
|
{"getenv", dst_stl_getenv},
|
|
|
|
{"setenv", dst_stl_setenv},
|
|
|
|
{"compile", dst_stl_compile},
|
2017-06-11 13:38:37 +00:00
|
|
|
/* Other */
|
2017-09-09 18:39:51 +00:00
|
|
|
{"not", dst_stl_not},
|
|
|
|
{"clear", dst_stl_clear},
|
|
|
|
{"length", dst_stl_length},
|
|
|
|
{"hash", dst_stl_hash},
|
|
|
|
{"integer", dst_stl_to_int},
|
|
|
|
{"real", dst_stl_to_real},
|
|
|
|
{"type", dst_stl_type},
|
|
|
|
{"slice", dst_stl_slice},
|
|
|
|
{"array", dst_stl_array},
|
|
|
|
{"tuple", dst_stl_tuple},
|
|
|
|
{"table", dst_stl_table},
|
|
|
|
{"struct", dst_stl_struct},
|
|
|
|
{"buffer", dst_stl_buffer},
|
|
|
|
{"string", dst_stl_string},
|
|
|
|
{"symbol", dst_stl_symbol},
|
|
|
|
{"thread", dst_stl_thread},
|
|
|
|
{"status", dst_stl_status},
|
|
|
|
{"current", dst_stl_current},
|
|
|
|
{"parent", dst_stl_parent},
|
|
|
|
{"print", dst_stl_print},
|
|
|
|
{"description", dst_stl_description},
|
|
|
|
{"short-description", dst_stl_short_description},
|
|
|
|
{"exit!", dst_stl_exit},
|
|
|
|
{"get", dst_stl_get},
|
|
|
|
{"set!", dst_stl_set},
|
|
|
|
{"next", dst_stl_next},
|
|
|
|
{"error", dst_stl_error},
|
|
|
|
{"push!", dst_stl_push},
|
|
|
|
{"pop!", dst_stl_pop},
|
|
|
|
{"peek", dst_stl_peek},
|
|
|
|
{"ensure!", dst_stl_ensure},
|
|
|
|
{"open", dst_stl_open},
|
|
|
|
{"slurp", dst_stl_slurp},
|
|
|
|
{"read", dst_stl_read},
|
|
|
|
{"write", dst_stl_write},
|
|
|
|
{"close", dst_stl_close},
|
|
|
|
{"funcenv", dst_stl_funcenv},
|
|
|
|
{"funcdef", dst_stl_funcdef},
|
|
|
|
{"funcparent", dst_stl_funcparent},
|
|
|
|
{"gcollect", dst_stl_gcollect},
|
|
|
|
{"global-def", dst_stl_def},
|
|
|
|
{"global-var", dst_stl_var},
|
2017-04-12 14:31:50 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2017-07-09 20:10:47 +00:00
|
|
|
/* Load stl library into the current environment. Create stl module object
|
|
|
|
* only if it is not yet created. */
|
2017-09-09 18:39:51 +00:00
|
|
|
void dst_stl_load(Dst *vm) {
|
|
|
|
DstValue maybeEnv = dst_table_get(vm->modules, dst_string_cvs(vm, "std"));
|
|
|
|
if (maybeEnv.type == DST_TABLE) {
|
2017-07-09 20:10:47 +00:00
|
|
|
/* Module already created, so merge into main vm. */
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_env_merge(vm, vm->env, maybeEnv.as.table);
|
2017-07-09 20:10:47 +00:00
|
|
|
} else {
|
|
|
|
/* Module not yet created */
|
|
|
|
/* Load the normal c functions */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_module_mutable(vm, "std", std_module);
|
2017-07-09 20:10:47 +00:00
|
|
|
/* Wrap stdin and stdout */
|
2017-09-09 18:39:51 +00:00
|
|
|
FILE **inp = dst_userdata(vm, sizeof(FILE *), &dst_stl_filetype);
|
|
|
|
FILE **outp = dst_userdata(vm, sizeof(FILE *), &dst_stl_filetype);
|
|
|
|
FILE **errp = dst_userdata(vm, sizeof(FILE *), &dst_stl_filetype);
|
2017-07-09 20:10:47 +00:00
|
|
|
*inp = stdin;
|
|
|
|
*outp = stdout;
|
|
|
|
*errp = stderr;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_module_put(vm, "std", "stdin", dst_wrap_userdata(inp));
|
|
|
|
dst_module_put(vm, "std", "stdout", dst_wrap_userdata(outp));
|
|
|
|
dst_module_put(vm, "std", "stderr", dst_wrap_userdata(outp));
|
2017-07-16 15:00:20 +00:00
|
|
|
/* Now merge */
|
2017-09-09 18:39:51 +00:00
|
|
|
maybeEnv = dst_table_get(vm->modules, dst_string_cvs(vm, "std"));
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_env_merge(vm, vm->env, maybeEnv.as.table);
|
2017-07-09 20:10:47 +00:00
|
|
|
}
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|