summaryrefslogtreecommitdiffhomepage
path: root/vm.h
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-04-21 15:07:16 +0200
committerJo-Philipp Wich <jo@mein.io>2021-04-25 20:48:40 +0200
commit35af4ba4fc21a4b2357c50e6b02a2e3e4b236e88 (patch)
tree445f9fdf2e96e490cd681dca36d5cc9912474ed3 /vm.h
parentf2c4b79feaffd7b2fdb4041f47c9cd0f4cc3bc6e (diff)
treewide: rework internal data type system
Instead of relying on json_object values internally, use custom types to represent the different ucode value types which brings a number of advantages compared to the previous approach: - Due to the use of tagged pointers, small integer, string and bool values can be stored directly in the pointer addresses, vastly reducing required heap memory - Ability to create circular data structures such as `let o; o = { test: o };` - Ability to register custom `tostring()` function through prototypes - Initial mark/sweep GC implementation to tear down circular object graphs on VM deinit The change also paves the way for possible future extensions such as constant variables and meta methods for custom ressource types. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'vm.h')
-rw-r--r--vm.h25
1 files changed, 12 insertions, 13 deletions
diff --git a/vm.h b/vm.h
index 5672cbb..e4de4f9 100644
--- a/vm.h
+++ b/vm.h
@@ -21,7 +21,6 @@
#include <stdarg.h>
#include "chunk.h"
-#include "object.h"
#include "util.h"
#include "lexer.h"
@@ -102,7 +101,6 @@ typedef struct {
int8_t stack_pop;
int8_t stack_push;
int8_t operand_bytes;
- bool operand_is_skip;
} uc_insn_definition;
typedef enum {
@@ -116,30 +114,31 @@ typedef enum {
typedef struct {
uc_exception_type_t type;
- json_object *stacktrace;
+ uc_value_t *stacktrace;
char *message;
} uc_exception;
typedef struct {
uint8_t *ip;
- uc_closure *closure;
- uc_cfunction *cfunction;
+ uc_closure_t *closure;
+ uc_cfunction_t *cfunction;
size_t stackframe;
- json_object *ctx;
+ uc_value_t *ctx;
bool mcall;
} uc_callframe;
uc_declare_vector(uc_callframes, uc_callframe);
-uc_declare_vector(uc_stack, json_object *);
+uc_declare_vector(uc_stack, uc_value_t *);
typedef struct uc_vm {
uc_stack stack;
uc_exception exception;
uc_callframes callframes;
- uc_upvalref *open_upvals;
+ uc_upvalref_t *open_upvals;
uc_parse_config *config;
- uc_prototype *globals;
+ uc_value_t *globals;
uc_source *sources;
+ uc_weakref_t values;
union {
uint32_t u32;
int32_t s32;
@@ -163,15 +162,15 @@ extern uint32_t insns[__I_MAX];
void uc_vm_init(uc_vm *vm, uc_parse_config *config);
void uc_vm_free(uc_vm *vm);
-void uc_vm_stack_push(uc_vm *vm, json_object *value);
-json_object *uc_vm_stack_pop(uc_vm *vm);
-json_object *uc_vm_stack_peek(uc_vm *vm, size_t offset);
+void uc_vm_stack_push(uc_vm *vm, uc_value_t *value);
+uc_value_t *uc_vm_stack_pop(uc_vm *vm);
+uc_value_t *uc_vm_stack_peek(uc_vm *vm, size_t offset);
uc_exception_type_t uc_vm_call(uc_vm *vm, bool mcall, size_t nargs);
void __attribute__((format(printf, 3, 0)))
uc_vm_raise_exception(uc_vm *vm, uc_exception_type_t type, const char *fmt, ...);
-uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function *fn, uc_prototype *globals, json_object *modules);
+uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *modules);
#endif /* __VM_H_ */