From 6521ee69bd34c8512561e27df95826ca41c394c7 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 19 Feb 2017 11:19:39 -0500 Subject: [PATCH] Various small changes. --- datatypes.h | 2 ++ value.c | 30 ++++++++++++------------------ value.h | 3 +++ vm.c | 36 ++++++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/datatypes.h b/datatypes.h index f6be01e6..4be1f744 100644 --- a/datatypes.h +++ b/datatypes.h @@ -140,6 +140,8 @@ struct GstStackFrame { uint16_t size; uint16_t prevSize; uint16_t ret; + uint16_t errorSlot; + uint16_t *errorJump; GstFuncEnv *env; uint16_t *pc; }; diff --git a/value.c b/value.c index 037e0c86..f301a3e9 100644 --- a/value.c +++ b/value.c @@ -152,12 +152,7 @@ int gst_equals(GstValue x, GstValue y) { } result = 0; break; - case GST_ARRAY: - case GST_BYTEBUFFER: - case GST_CFUNCTION: - case GST_OBJECT: - case GST_FUNCTION: - case GST_THREAD: + default: /* compare pointers */ result = (x.data.array == y.data.array); break; @@ -194,12 +189,7 @@ uint32_t gst_hash(GstValue x) { else hash = gst_string_hash(x.data.string) = djb2(x.data.string); break; - case GST_ARRAY: - case GST_BYTEBUFFER: - case GST_CFUNCTION: - case GST_OBJECT: - case GST_FUNCTION: - case GST_THREAD: + default: /* Cast the pointer */ { union { @@ -258,12 +248,7 @@ int gst_compare(GstValue x, GstValue y) { return xlen < ylen ? -1 : 1; } } - case GST_ARRAY: - case GST_BYTEBUFFER: - case GST_CFUNCTION: - case GST_FUNCTION: - case GST_OBJECT: - case GST_THREAD: + default: if (x.data.string == y.data.string) { return 0; } else { @@ -360,3 +345,12 @@ void gst_set(Gst *vm, GstValue ds, GstValue key, GstValue value) { gst_error(vm, "Cannot set."); } } + +/* Get the meta value associated with a value */ +GstValue gst_meta(Gst *vm, GstValue x) { + switch (x.type) { + default: return vm->metas[x.type]; + case GST_OBJECT: + return x.data.object->meta; + } +} diff --git a/value.h b/value.h index 8314f7ee..45ec9c93 100644 --- a/value.h +++ b/value.h @@ -26,4 +26,7 @@ uint8_t *gst_to_string(Gst *vm, GstValue x); /* Generate a hash value for a gst object */ uint32_t gst_hash(GstValue x); +/* Get the meta value for a given value */ +GstValue gst_meta(Gst *vm, GstValue x); + #endif /* end of include guard: VALUE_H_1RJPQKFM */ diff --git a/vm.c b/vm.c index 482e2917..092acfce 100644 --- a/vm.c +++ b/vm.c @@ -5,11 +5,11 @@ #include "value.h" #include "ds.h" -static const char OOM[] = "Out of memory"; -static const char NO_UPVALUE[] = "No upvalue"; -static const char EXPECTED_FUNCTION[] = "Expected function"; -static const char VMS_EXPECTED_NUMBER_ROP[] = "Expected right operand to be number"; -static const char VMS_EXPECTED_NUMBER_LOP[] = "Expected left operand to be number"; +static const char OOM[] = "out of memory"; +static const char NO_UPVALUE[] = "no upvalue"; +static const char EXPECTED_FUNCTION[] = "expected function"; +static const char VMS_EXPECTED_NUMBER_ROP[] = "expected right operand to be number"; +static const char VMS_EXPECTED_NUMBER_LOP[] = "expected left operand to be number"; /* The size of a StackFrame in units of Values. */ #define FRAME_SIZE ((sizeof(GstStackFrame) + sizeof(GstValue) - 1) / sizeof(GstValue)) @@ -55,6 +55,7 @@ static void thread_push(Gst *vm, GstThread *thread, GstValue callee, uint32_t si frame->size = size; frame->env = NULL; frame->callee = callee; + frame->errorJump = NULL; } /* Copy the current function stack to the current closure @@ -81,7 +82,7 @@ static void thread_pop(Gst *vm) { if (thread->count) { thread_split_env(vm); } else { - gst_error(vm, "Nothing to pop from stack."); + gst_crash(vm, "stack underflow"); } thread->count -= delta; vm->base -= delta; @@ -445,7 +446,7 @@ static GstValue gst_vm_closure(Gst *vm, uint16_t literal) { current = vm->frame->callee.data.function; constant = gst_vm_literal(vm, current, literal); if (constant.type != GST_NIL) { - gst_error(vm, "Error trying to create closure"); + gst_error(vm, "cannot create closure"); } fn = gst_alloc(vm, sizeof(GstFunction)); fn->def = (GstFuncDef *) constant.data.pointer; @@ -464,12 +465,23 @@ int gst_start(Gst *vm) { { int n; if ((n = setjmp(vm->jump))) { - vm->lock = 0; /* Good return */ if (n == 1) { + vm->lock = 0; return 0; + } else if (n == 2) { + /* Error. Handling TODO. */ + do { + if (vm->thread->count == 0) + return n; + thread_pop(vm); + } while (!vm->frame->errorJump); + /* Jump to the error location */ + vm->pc = vm->frame->errorJump; + vm->lock = 0; } else { - /* Error or crash. Handling TODO. */ + /* Crash. just return */ + vm->lock = 0; return n; } } @@ -726,7 +738,7 @@ int gst_start(Gst *vm) { break; default: - gst_error(vm, "Unknown opcode"); + gst_error(vm, "unknown opcode"); break; } @@ -739,14 +751,14 @@ int gst_start(Gst *vm) { /* Get an argument from the stack */ GstValue gst_arg(Gst *vm, uint16_t index) { uint16_t frameSize = vm->frame->size; - gst_assert(vm, frameSize > index, "Cannot get arg out of stack bounds"); + gst_assert(vm, frameSize > index, "cannot get arg out of stack bounds"); return vm->base[index]; } /* Put a value on the stack */ void gst_set_arg(Gst* vm, uint16_t index, GstValue x) { uint16_t frameSize = vm->frame->size; - gst_assert(vm, frameSize > index, "Cannot set arg out of stack bounds"); + gst_assert(vm, frameSize > index, "cannot set arg out of stack bounds"); vm->base[index] = x; }