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>
|
|
|
|
#include "opcodes.h"
|
2017-11-27 19:03:34 +00:00
|
|
|
#include "symcache.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* VM State */
|
|
|
|
DstFiber *dst_vm_fiber;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-25 04:17:04 +00:00
|
|
|
/* Helper to ensure proper fiber is activated after returning */
|
|
|
|
static int dst_update_fiber() {
|
|
|
|
if (dst_vm_fiber->frame == 0) {
|
|
|
|
dst_vm_fiber->status = DST_FIBER_DEAD;
|
|
|
|
}
|
|
|
|
while (dst_vm_fiber->status == DST_FIBER_DEAD ||
|
|
|
|
dst_vm_fiber->status == DST_FIBER_ERROR) {
|
|
|
|
if (NULL != dst_vm_fiber->parent) {
|
|
|
|
dst_vm_fiber = dst_vm_fiber->parent;
|
|
|
|
if (dst_vm_fiber->status == DST_FIBER_ALIVE) {
|
|
|
|
/* If the parent thread is still alive,
|
|
|
|
we are inside a cfunction */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The root thread has termiated */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst_vm_fiber->status = DST_FIBER_ALIVE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-27 00:31:40 +00:00
|
|
|
/* Eventually use computed gotos for more effient vm loop. */
|
|
|
|
#define vm_next() continue
|
|
|
|
#define vm_checkgc_next() dst_maybe_collect(); continue
|
|
|
|
|
|
|
|
|
2017-04-19 13:02:12 +00:00
|
|
|
/* Start running the VM from where it left off. */
|
2017-11-21 02:39:44 +00:00
|
|
|
int dst_continue() {
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* VM state */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue *stack;
|
2017-11-06 03:05:47 +00:00
|
|
|
uint32_t *pc;
|
|
|
|
DstFunction *func;
|
|
|
|
|
|
|
|
/* Used to extract bits from the opcode that correspond to arguments.
|
|
|
|
* Pulls out unsigned integers */
|
2017-11-21 02:39:44 +00:00
|
|
|
#define oparg(shift, mask) (((*pc) >> ((shift) << 3)) & (mask))
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
#define vm_throw(e) do { dst_vm_fiber->ret = dst_cstringv((e)); goto vm_error; } while (0)
|
2017-11-06 03:05:47 +00:00
|
|
|
#define vm_assert(cond, e) do {if (!(cond)) vm_throw((e)); } while (0)
|
|
|
|
|
|
|
|
#define vm_binop_integer(op) \
|
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(\
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_unwrap_integer(stack[oparg(2, 0xFF)]) op dst_unwrap_integer(stack[oparg(3, 0xFF)])\
|
2017-11-06 03:05:47 +00:00
|
|
|
);\
|
|
|
|
pc++;\
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
#define vm_binop_real(op)\
|
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_real(\
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_unwrap_real(stack[oparg(2, 0xFF)]) op dst_unwrap_real(stack[oparg(3, 0xFF)])\
|
2017-11-06 03:05:47 +00:00
|
|
|
);\
|
|
|
|
pc++;\
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
#define vm_binop_immediate(op)\
|
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(\
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_unwrap_integer(stack[oparg(2, 0xFF)]) op (*((int32_t *)pc) >> 24)\
|
2017-11-06 03:05:47 +00:00
|
|
|
);\
|
|
|
|
pc++;\
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
#define vm_binop(op)\
|
|
|
|
{\
|
|
|
|
DstValue op1 = stack[oparg(2, 0xFF)];\
|
|
|
|
DstValue op2 = stack[oparg(3, 0xFF)];\
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert(dst_checktype(op1, DST_INTEGER) || dst_checktype(op1, DST_REAL), "expected number");\
|
|
|
|
vm_assert(dst_checktype(op2, DST_INTEGER) || dst_checktype(op2, DST_REAL), "expected number");\
|
|
|
|
stack[oparg(1, 0xFF)] = dst_checktype(op1, DST_INTEGER)\
|
|
|
|
? (dst_checktype(op2, DST_INTEGER)\
|
|
|
|
? dst_wrap_integer(dst_unwrap_integer(op1) op dst_unwrap_integer(op2))\
|
|
|
|
: dst_wrap_real((double)dst_unwrap_integer(op1) op dst_unwrap_real(op2)))\
|
|
|
|
: (dst_checktype(op2, DST_INTEGER)\
|
|
|
|
? dst_wrap_real(dst_unwrap_real(op1) op (double)dst_unwrap_integer(op2))\
|
|
|
|
: dst_wrap_real(dst_unwrap_real(op1) op dst_unwrap_real(op2)));\
|
2017-11-06 03:05:47 +00:00
|
|
|
pc++;\
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();\
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-05-09 23:21:30 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#define vm_init_fiber_state() \
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_vm_fiber->status = DST_FIBER_ALIVE;\
|
|
|
|
stack = dst_vm_fiber->data + dst_vm_fiber->frame;\
|
2017-11-06 03:05:47 +00:00
|
|
|
pc = dst_stack_frame(stack)->pc;\
|
|
|
|
func = dst_stack_frame(stack)->func;
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
vm_init_fiber_state();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Main interpreter loop. It is large, but it is
|
|
|
|
* is maintainable. Adding new opcodes is mostly just adding newcases
|
|
|
|
* to this loop, adding the opcode to opcodes.h, and adding it to the assembler.
|
|
|
|
* Some opcodes, especially ones that do arithmetic, are almost entirely
|
|
|
|
* templated by the above macros. */
|
2017-02-09 20:02:59 +00:00
|
|
|
for (;;) {
|
2017-07-02 01:51:16 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
switch (*pc & 0xFF) {
|
2017-02-12 15:27:18 +00:00
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
default:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_throw("unknown opcode");
|
2017-07-02 01:51:16 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_NOOP:
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_ERROR:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_vm_fiber->ret = stack[oparg(1, 0xFF)];
|
|
|
|
goto vm_error;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_TYPECHECK:
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert((1 << dst_type(stack[oparg(1, 0xFF)])) & oparg(2, 0xFFFF),
|
2017-11-27 00:31:40 +00:00
|
|
|
"typecheck failed");
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_RETURN:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_vm_fiber->ret = stack[oparg(1, 0xFFFFFF)];
|
|
|
|
goto vm_return;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_RETURN_NIL:
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_vm_fiber->ret = dst_wrap_nil();
|
2017-11-27 00:31:40 +00:00
|
|
|
goto vm_return;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_ADD_INTEGER:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_ADD_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_immediate(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_ADD_REAL:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_real(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_ADD:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop(+);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SUBTRACT_INTEGER:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(-);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SUBTRACT_REAL:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_real(-);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SUBTRACT:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop(-);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_MULTIPLY_INTEGER:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_MULTIPLY_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_immediate(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_MULTIPLY_REAL:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_real(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_MULTIPLY:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop(*);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_DIVIDE_INTEGER:
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert(dst_unwrap_integer(stack[oparg(3, 0xFF)]) != 0, "integer divide by zero");
|
|
|
|
vm_assert(!(dst_unwrap_integer(stack[oparg(3, 0xFF)]) == -1 &&
|
|
|
|
dst_unwrap_integer(stack[oparg(2, 0xFF)]) == DST_INTEGER_MIN),
|
2017-11-27 00:31:40 +00:00
|
|
|
"integer divide overflow");
|
|
|
|
vm_binop_integer(/);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_DIVIDE_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t op1 = dst_unwrap_integer(stack[oparg(2, 0xFF)]);
|
|
|
|
int32_t op2 = *((int32_t *)pc) >> 24;
|
2017-11-27 00:31:40 +00:00
|
|
|
/* Check for degenerate integer division (divide by zero, and dividing
|
|
|
|
* min value by -1). These checks could be omitted if the arg is not
|
|
|
|
* 0 or -1. */
|
|
|
|
if (op2 == 0)
|
|
|
|
vm_throw("integer divide by zero");
|
|
|
|
if (op2 == -1)
|
|
|
|
vm_throw("integer divide overflow");
|
|
|
|
else
|
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(op1 / op2);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_DIVIDE_REAL:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_real(/);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_DIVIDE:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
|
|
|
DstValue op1 = stack[oparg(2, 0xFF)];
|
|
|
|
DstValue op2 = stack[oparg(3, 0xFF)];
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert(dst_checktype(op1, DST_INTEGER) || dst_checktype(op1, DST_REAL), "expected number");
|
|
|
|
vm_assert(dst_checktype(op2, DST_INTEGER) || dst_checktype(op2, DST_REAL), "expected number");
|
|
|
|
if (dst_checktype(op2, DST_INTEGER) && dst_unwrap_integer(op2) == 0)
|
2017-11-27 00:31:40 +00:00
|
|
|
op2 = dst_wrap_real(0.0);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(op2, DST_INTEGER) && dst_unwrap_integer(op2) == -1 &&
|
|
|
|
dst_checktype(op1, DST_INTEGER) && dst_unwrap_integer(op1) == DST_INTEGER_MIN)
|
|
|
|
op2 = dst_wrap_real(-1.0);
|
|
|
|
stack[oparg(1, 0xFF)] = dst_checktype(op1, DST_INTEGER)
|
|
|
|
? (dst_checktype(op2, DST_INTEGER)
|
|
|
|
? dst_wrap_integer(dst_unwrap_integer(op1) / dst_unwrap_integer(op2))
|
|
|
|
: dst_wrap_real((double)dst_unwrap_integer(op1) / dst_unwrap_real(op2)))
|
|
|
|
: (dst_checktype(op2, DST_INTEGER)
|
|
|
|
? dst_wrap_real(dst_unwrap_real(op1) / (double)dst_unwrap_integer(op2))
|
|
|
|
: dst_wrap_real(dst_unwrap_real(op1) / dst_unwrap_real(op2)));
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_BAND:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(&);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_BOR:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(|);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_BXOR:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(^);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_BNOT:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(~dst_unwrap_integer(stack[oparg(2, 0xFFFF)]));
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SHIFT_RIGHT_UNSIGNED:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(
|
2017-11-28 23:27:55 +00:00
|
|
|
(int32_t)(((uint32_t)dst_unwrap_integer(stack[oparg(2, 0xFF)]))
|
2017-11-27 00:31:40 +00:00
|
|
|
>>
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_unwrap_integer(stack[oparg(3, 0xFF)]))
|
2017-11-27 00:31:40 +00:00
|
|
|
);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(
|
2017-11-28 23:27:55 +00:00
|
|
|
(int32_t) (((uint32_t)dst_unwrap_integer(stack[oparg(2, 0xFF)])) >> oparg(3, 0xFF))
|
2017-11-27 00:31:40 +00:00
|
|
|
);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SHIFT_RIGHT:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(>>);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SHIFT_RIGHT_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(
|
2017-11-28 23:27:55 +00:00
|
|
|
(int32_t)(dst_unwrap_integer(stack[oparg(2, 0xFF)]) >> oparg(3, 0xFF))
|
2017-11-27 00:31:40 +00:00
|
|
|
);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_SHIFT_LEFT:
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_binop_integer(<<);
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2017-11-25 04:17:04 +00:00
|
|
|
case DOP_SHIFT_LEFT_IMMEDIATE:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_unwrap_integer(stack[oparg(2, 0xFF)]) << oparg(3, 0xFF)
|
2017-11-27 00:31:40 +00:00
|
|
|
);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_MOVE:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = stack[oparg(2, 0xFFFF)];
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_JUMP:
|
2017-11-27 00:31:40 +00:00
|
|
|
pc += (*(int32_t *)pc) >> 8;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_JUMP_IF:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (dst_truthy(stack[oparg(1, 0xFF)])) {
|
|
|
|
pc += (*(int32_t *)pc) >> 16;
|
|
|
|
} else {
|
|
|
|
pc++;
|
|
|
|
}
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-25 04:17:04 +00:00
|
|
|
case DOP_JUMP_IF_NOT:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (dst_truthy(stack[oparg(1, 0xFF)])) {
|
|
|
|
pc++;
|
|
|
|
} else {
|
|
|
|
pc += (*(int32_t *)pc) >> 16;
|
|
|
|
}
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
|
|
|
case DOP_LESS_THAN:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_boolean(dst_compare(
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]
|
2017-11-28 23:27:55 +00:00
|
|
|
) < 0);
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_GREATER_THAN:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_boolean(dst_compare(
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]
|
2017-11-28 23:27:55 +00:00
|
|
|
) > 0);
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_EQUALS:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_boolean(dst_equals(
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]
|
2017-11-28 23:27:55 +00:00
|
|
|
));
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_COMPARE:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(dst_compare(
|
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]
|
|
|
|
));
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_NIL:
|
2017-11-28 23:27:55 +00:00
|
|
|
stack[oparg(1, 0xFFFFFF)] = dst_wrap_nil();
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_BOOLEAN:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_boolean(oparg(2, 0xFFFF));
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-04-24 20:02:54 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_INTEGER:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_integer(*((int32_t *)pc) >> 16);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_CONSTANT:
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert((int32_t)oparg(2, 0xFFFF) < func->def->constants_length, "invalid constant");
|
|
|
|
stack[oparg(1, 0xFF)] = func->def->constants[(int32_t)oparg(2, 0xFFFF)];
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_UPVALUE:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t eindex = oparg(2, 0xFF);
|
|
|
|
int32_t vindex = oparg(3, 0xFF);
|
2017-11-27 00:31:40 +00:00
|
|
|
DstFuncEnv *env;
|
|
|
|
vm_assert(func->def->environments_length > eindex, "invalid upvalue");
|
|
|
|
env = func->envs[eindex];
|
|
|
|
vm_assert(env->length > vindex, "invalid upvalue");
|
|
|
|
if (env->offset) {
|
|
|
|
/* On stack */
|
|
|
|
stack[oparg(1, 0xFF)] = env->as.fiber->data[env->offset + vindex];
|
|
|
|
} else {
|
|
|
|
/* Off stack */
|
|
|
|
stack[oparg(1, 0xFF)] = env->as.values[vindex];
|
2017-02-26 16:47:50 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_SET_UPVALUE:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t eindex = oparg(2, 0xFF);
|
|
|
|
int32_t vindex = oparg(3, 0xFF);
|
2017-11-27 00:31:40 +00:00
|
|
|
DstFuncEnv *env;
|
|
|
|
vm_assert(func->def->environments_length > eindex, "invalid upvalue");
|
|
|
|
env = func->envs[eindex];
|
|
|
|
vm_assert(env->length > vindex, "invalid upvalue");
|
|
|
|
if (env->offset) {
|
|
|
|
env->as.fiber->data[env->offset + vindex] = stack[oparg(1, 0xFF)];
|
|
|
|
} else {
|
|
|
|
env->as.values[vindex] = stack[oparg(1, 0xFF)];
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_next();
|
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_CLOSURE:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2017-11-27 00:31:40 +00:00
|
|
|
DstFunction *fn;
|
|
|
|
DstFuncDef *fd;
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert((int32_t)oparg(2, 0xFFFF) < func->def->constants_length, "invalid constant");
|
|
|
|
vm_assert(dst_checktype(func->def->constants[oparg(2, 0xFFFF)], DST_NIL), "constant must be funcdef");
|
|
|
|
fd = (DstFuncDef *)(dst_unwrap_pointer(func->def->constants[(int32_t)oparg(2, 0xFFFF)]));
|
2017-11-27 00:31:40 +00:00
|
|
|
fn = dst_alloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
|
|
|
|
fn->envs = malloc(sizeof(DstFuncEnv *) * fd->environments_length);
|
|
|
|
if (NULL == fn->envs) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (fd->flags & DST_FUNCDEF_FLAG_NEEDSENV) {
|
|
|
|
/* Delayed capture of current stack frame */
|
|
|
|
DstFuncEnv *env = dst_alloc(DST_MEMORY_FUNCENV, sizeof(DstFuncEnv));
|
|
|
|
env->offset = dst_vm_fiber->frame;
|
|
|
|
env->as.fiber = dst_vm_fiber;
|
|
|
|
env->length = func->def->slotcount;
|
|
|
|
fn->envs[0] = env;
|
|
|
|
} else {
|
|
|
|
fn->envs[0] = NULL;
|
|
|
|
}
|
|
|
|
for (i = 1; i < fd->environments_length; ++i) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t inherit = fd->environments[i];
|
2017-11-27 00:31:40 +00:00
|
|
|
fn->envs[i] = func->envs[inherit];
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_function(fn);
|
|
|
|
pc++;
|
|
|
|
vm_checkgc_next();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_PUSH:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_fiber_push(dst_vm_fiber, stack[oparg(1, 0xFFFFFF)]);
|
|
|
|
pc++;
|
|
|
|
vm_checkgc_next();
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_PUSH_2:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_fiber_push2(dst_vm_fiber,
|
|
|
|
stack[oparg(1, 0xFF)],
|
|
|
|
stack[oparg(2, 0xFFFF)]);
|
|
|
|
pc++;
|
|
|
|
vm_checkgc_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_PUSH_3:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_fiber_push3(dst_vm_fiber,
|
|
|
|
stack[oparg(1, 0xFF)],
|
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]);
|
|
|
|
pc++;
|
|
|
|
vm_checkgc_next();
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_PUSH_ARRAY:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t count;
|
2017-11-27 00:31:40 +00:00
|
|
|
const DstValue *array;
|
|
|
|
if (dst_seq_view(stack[oparg(1, 0xFFFFFF)], &array, &count)) {
|
|
|
|
dst_fiber_pushn(dst_vm_fiber, array, count);
|
|
|
|
} else {
|
|
|
|
vm_throw("expected array or tuple");
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
pc++;
|
|
|
|
vm_checkgc_next();
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
case DOP_CALL:
|
|
|
|
{
|
|
|
|
DstValue callee = stack[oparg(2, 0xFFFF)];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(callee, DST_FUNCTION)) {
|
|
|
|
func = dst_unwrap_function(callee);
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_fiber_funcframe(dst_vm_fiber, func);
|
|
|
|
stack = dst_vm_fiber->data + dst_vm_fiber->frame;
|
2017-11-06 03:05:47 +00:00
|
|
|
pc = func->def->bytecode;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_checkgc_next();
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(callee, DST_CFUNCTION)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_fiber_cframe(dst_vm_fiber);
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_vm_fiber->ret = dst_wrap_nil();
|
|
|
|
if (dst_unwrap_cfunction(callee)(
|
2017-11-25 04:17:04 +00:00
|
|
|
dst_vm_fiber->data + dst_vm_fiber->frame,
|
|
|
|
dst_vm_fiber->frametop - dst_vm_fiber->frame)) {
|
2017-09-09 18:39:51 +00:00
|
|
|
goto vm_error;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
goto vm_return_cfunc;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_throw("cannot call non-function type");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case DOP_TAILCALL:
|
|
|
|
{
|
|
|
|
DstValue callee = stack[oparg(2, 0xFFFF)];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(callee, DST_FUNCTION)) {
|
|
|
|
func = dst_unwrap_function(callee);
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_fiber_funcframe_tail(dst_vm_fiber, func);
|
|
|
|
stack = dst_vm_fiber->data + dst_vm_fiber->frame;
|
2017-11-06 03:05:47 +00:00
|
|
|
pc = func->def->bytecode;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_checkgc_next();
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(callee, DST_CFUNCTION)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_fiber_cframe_tail(dst_vm_fiber);
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_vm_fiber->ret = dst_wrap_nil();
|
|
|
|
if (dst_unwrap_cfunction(callee)(
|
2017-11-25 04:17:04 +00:00
|
|
|
dst_vm_fiber->data + dst_vm_fiber->frame,
|
|
|
|
dst_vm_fiber->frametop - dst_vm_fiber->frame)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
goto vm_error;
|
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
goto vm_return_cfunc;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_throw("expected function");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_SYSCALL:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
|
|
|
DstCFunction f = dst_vm_syscalls[oparg(2, 0xFF)];
|
|
|
|
vm_assert(NULL != f, "invalid syscall");
|
|
|
|
dst_fiber_cframe(dst_vm_fiber);
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_vm_fiber->ret = dst_wrap_nil();
|
2017-11-27 00:31:40 +00:00
|
|
|
if (f(dst_vm_fiber->data + dst_vm_fiber->frame,
|
|
|
|
dst_vm_fiber->frametop - dst_vm_fiber->frame)) {
|
|
|
|
goto vm_error;
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
goto vm_return_cfunc;
|
|
|
|
}
|
2017-07-02 01:51:16 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_LOAD_SYSCALL:
|
2017-11-27 00:31:40 +00:00
|
|
|
{
|
|
|
|
DstCFunction f = dst_vm_syscalls[oparg(2, 0xFF)];
|
|
|
|
vm_assert(NULL != f, "invalid syscall");
|
|
|
|
stack[oparg(1, 0xFF)] = dst_wrap_cfunction(f);
|
|
|
|
pc++;
|
|
|
|
vm_next();
|
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
case DOP_TRANSFER:
|
|
|
|
{
|
|
|
|
DstFiber *nextfiber;
|
|
|
|
DstStackFrame *frame = dst_stack_frame(stack);
|
|
|
|
DstValue temp = stack[oparg(2, 0xFF)];
|
|
|
|
DstValue retvalue = stack[oparg(3, 0xFF)];
|
2017-11-28 23:27:55 +00:00
|
|
|
vm_assert(dst_checktype(temp, DST_FIBER) ||
|
|
|
|
dst_checktype(temp, DST_NIL), "expected fiber");
|
|
|
|
nextfiber = dst_checktype(temp, DST_FIBER)
|
|
|
|
? dst_unwrap_fiber(temp)
|
2017-11-21 02:39:44 +00:00
|
|
|
: dst_vm_fiber->parent;
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Check for root fiber */
|
|
|
|
if (NULL == nextfiber) {
|
|
|
|
frame->pc = pc;
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_vm_fiber->ret = retvalue;
|
2017-09-09 18:39:51 +00:00
|
|
|
return 0;
|
2017-07-01 15:17:29 +00:00
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
vm_assert(nextfiber->status == DST_FIBER_PENDING, "can only transfer to pending fiber");
|
|
|
|
frame->pc = pc;
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_vm_fiber->status = DST_FIBER_PENDING;
|
|
|
|
dst_vm_fiber = nextfiber;
|
2017-11-06 03:05:47 +00:00
|
|
|
vm_init_fiber_state();
|
|
|
|
stack[oparg(1, 0xFF)] = retvalue;
|
|
|
|
pc++;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-05-09 23:21:30 +00:00
|
|
|
|
2017-11-25 04:17:04 +00:00
|
|
|
case DOP_PUT:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_put(stack[oparg(1, 0xFF)],
|
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]);
|
|
|
|
++pc;
|
|
|
|
vm_checkgc_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
|
|
|
case DOP_PUT_INDEX:
|
2017-11-27 00:31:40 +00:00
|
|
|
dst_setindex(stack[oparg(1, 0xFF)],
|
2017-11-25 04:17:04 +00:00
|
|
|
stack[oparg(3, 0xFF)],
|
|
|
|
oparg(3, 0xFF));
|
2017-11-27 00:31:40 +00:00
|
|
|
++pc;
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
|
|
|
case DOP_GET:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_get(
|
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
stack[oparg(3, 0xFF)]);
|
|
|
|
++pc;
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
|
|
|
case DOP_GET_INDEX:
|
2017-11-27 00:31:40 +00:00
|
|
|
stack[oparg(1, 0xFF)] = dst_getindex(
|
2017-11-25 04:17:04 +00:00
|
|
|
stack[oparg(2, 0xFF)],
|
|
|
|
oparg(3, 0xFF));
|
2017-11-27 00:31:40 +00:00
|
|
|
++pc;
|
|
|
|
vm_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
|
|
|
|
/* Return from c function. Simpler than retuning from dst function */
|
|
|
|
vm_return_cfunc:
|
|
|
|
{
|
|
|
|
DstValue ret = dst_vm_fiber->ret;
|
|
|
|
dst_fiber_popframe(dst_vm_fiber);
|
|
|
|
if (dst_update_fiber())
|
|
|
|
return 0;
|
|
|
|
stack[oparg(1, 0xFF)] = ret;
|
|
|
|
pc++;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_checkgc_next();
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Handle returning from stack frame. Expect return value in fiber->ret */
|
2017-05-09 23:21:30 +00:00
|
|
|
vm_return:
|
2017-11-06 03:05:47 +00:00
|
|
|
{
|
2017-11-21 02:39:44 +00:00
|
|
|
DstValue ret = dst_vm_fiber->ret;
|
|
|
|
dst_fiber_popframe(dst_vm_fiber);
|
2017-11-25 04:17:04 +00:00
|
|
|
if (dst_update_fiber())
|
|
|
|
return 0;
|
2017-11-21 02:39:44 +00:00
|
|
|
stack = dst_vm_fiber->data + dst_vm_fiber->frame;
|
2017-11-06 03:05:47 +00:00
|
|
|
pc = dst_stack_frame(stack)->pc;
|
|
|
|
stack[oparg(1, 0xFF)] = ret;
|
|
|
|
pc++;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_checkgc_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
/* Handle errors from c functions and vm opcodes */
|
|
|
|
vm_error:
|
2017-11-06 03:05:47 +00:00
|
|
|
{
|
2017-11-21 02:39:44 +00:00
|
|
|
DstValue ret = dst_vm_fiber->ret;
|
|
|
|
dst_vm_fiber->status = DST_FIBER_ERROR;
|
2017-11-25 04:17:04 +00:00
|
|
|
if (dst_update_fiber())
|
|
|
|
return 1;
|
2017-11-21 02:39:44 +00:00
|
|
|
stack = dst_vm_fiber->data + dst_vm_fiber->frame;
|
2017-11-06 03:05:47 +00:00
|
|
|
pc = dst_stack_frame(stack)->pc;
|
|
|
|
stack[oparg(1, 0xFF)] = ret;
|
|
|
|
pc++;
|
2017-11-27 00:31:40 +00:00
|
|
|
vm_checkgc_next();
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:55:50 +00:00
|
|
|
} /* end switch */
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
} /* end for */
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#undef oparg
|
|
|
|
|
|
|
|
#undef vm_error
|
|
|
|
#undef vm_assert
|
|
|
|
#undef vm_binop
|
|
|
|
#undef vm_binop_real
|
|
|
|
#undef vm_binop_integer
|
|
|
|
#undef vm_binop_immediate
|
|
|
|
#undef vm_init_fiber_state
|
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Run the vm with a given function. This function is
|
|
|
|
* called to start the vm. */
|
2017-11-06 03:05:47 +00:00
|
|
|
int dst_run(DstValue callee) {
|
2017-11-21 02:39:44 +00:00
|
|
|
if (NULL == dst_vm_fiber) {
|
|
|
|
dst_vm_fiber = dst_fiber(0);
|
|
|
|
} else {
|
|
|
|
dst_fiber_reset(dst_vm_fiber);
|
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(callee, DST_CFUNCTION)) {
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_vm_fiber->ret = dst_wrap_nil();
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_fiber_cframe(dst_vm_fiber);
|
2017-11-28 23:27:55 +00:00
|
|
|
return dst_unwrap_cfunction(callee)(dst_vm_fiber->data + dst_vm_fiber->frame, 0);
|
|
|
|
} else if (dst_checktype(callee, DST_FUNCTION)) {
|
|
|
|
dst_fiber_funcframe(dst_vm_fiber, dst_unwrap_function(callee));
|
2017-11-21 02:39:44 +00:00
|
|
|
return dst_continue();
|
2017-07-01 15:17:29 +00:00
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_vm_fiber->ret = dst_cstringv("expected function");
|
2017-11-21 02:39:44 +00:00
|
|
|
return 1;
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Setup functions */
|
2017-11-06 03:05:47 +00:00
|
|
|
int dst_init() {
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Garbage collection */
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_blocks = NULL;
|
|
|
|
dst_vm_next_collection = 0;
|
2017-04-18 20:55:03 +00:00
|
|
|
/* Setting memoryInterval to zero forces
|
2017-02-13 05:11:30 +00:00
|
|
|
* a collection pretty much every cycle, which is
|
2017-07-03 00:51:52 +00:00
|
|
|
* horrible for performance, but helps ensure
|
2017-02-13 05:11:30 +00:00
|
|
|
* there are no memory bugs during dev */
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_memory_interval = 0;
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_symcache_init();
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Set thread */
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_fiber = NULL;
|
|
|
|
return 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all memory associated with the VM */
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_deinit() {
|
|
|
|
dst_clear_memory();
|
|
|
|
dst_vm_fiber = NULL;
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_symcache_deinit();
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|