From 230658ee94f81bcf133777d902c4f37a018fac85 Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Wed, 24 Jul 2024 04:29:36 -0400 Subject: [PATCH] more renames, once per function code check --- vm/backend/backend.c | 19 +++++++-- vm/gc.c | 18 ++++----- vm/gc.h | 2 +- vm/io.c | 4 +- vm/io.h | 2 +- vm/lua/repl.c | 2 +- vm/obj.c | 34 +++++++--------- vm/obj.h | 12 +++--- vm/std.c | 34 ++++++++-------- vm/std.h | 2 +- vm/vm.h | 96 ++++---------------------------------------- 11 files changed, 75 insertions(+), 150 deletions(-) diff --git a/vm/backend/backend.c b/vm/backend/backend.c index eea3bb5a..134b7bfa 100644 --- a/vm/backend/backend.c +++ b/vm/backend/backend.c @@ -693,6 +693,18 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { #define vm_run_repl_out(value) (regs[vm_run_repl_read(vm_interp_reg_t)] = (value)) +void vm_interp_renumber_blocks(vm_t *vm, void **ptrs, vm_ir_block_t *block) { + if (block->code == NULL) { + block->code = vm_interp_renumber_block(vm, &ptrs[0], block); + if (block->branch.targets[0] != NULL) { + vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[0]); + } + if (block->branch.targets[1] != NULL) { + vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[1]); + } + } +} + vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) { vm_obj_t *regs = vm->regs; static void *ptrs[VM_MAX_OP] = { @@ -736,6 +748,9 @@ vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) { [VM_OP_GET] = &&VM_OP_GET, [VM_OP_CALL] = &&VM_OP_CALL, }; + + vm_interp_renumber_blocks(vm, ptrs, block); + vm_obj_t *next_regs = ®s[VM_NREGS]; #if VM_DEBUG_BACKEND_BLOCKS { @@ -759,10 +774,6 @@ new_block:; new_block_no_print:; uint8_t *code = block->code; - if (block->code == NULL) { - code = vm_interp_renumber_block(vm, &ptrs[0], block); - block->code = code; - } goto *vm_run_repl_read(void *); diff --git a/vm/gc.c b/vm/gc.c index 33cd997f..bd348d7e 100644 --- a/vm/gc.c +++ b/vm/gc.c @@ -19,7 +19,7 @@ struct vm_gc_objs_t { struct vm_gc_table_cache_t { size_t alloc; size_t len; - vm_table_t **tables; + vm_obj_table_t **tables; }; struct vm_gc_t { @@ -59,7 +59,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) { vm_io_buffer_t *buffer = vm_obj_get_string(obj); buffer->mark = true; } else if (vm_obj_is_closure(obj)) { - vm_closure_t *closure = vm_obj_get_closure(obj); + vm_obj_closure_t *closure = vm_obj_get_closure(obj); if (!closure->mark) { closure->mark = true; for (size_t i = 0; i < closure->len; i++) { @@ -68,7 +68,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) { } vm_gc_mark_block(closure->block); } else if (vm_obj_is_table(obj)) { - vm_table_t *table = vm_obj_get_table(obj); + vm_obj_table_t *table = vm_obj_get_table(obj); if (!table->mark) { table->mark = true; for (size_t i = 0; i < (1 << table->alloc); i++) { @@ -106,7 +106,7 @@ void vm_gc_sweep(vm_t *vm) { gc->objs.objs[write++] = obj; } } else if (vm_obj_is_closure(obj)) { - vm_closure_t *closure = vm_obj_get_closure(obj); + vm_obj_closure_t *closure = vm_obj_get_closure(obj); if (!closure->mark) { vm_free(closure); } else { @@ -114,7 +114,7 @@ void vm_gc_sweep(vm_t *vm) { gc->objs.objs[write++] = obj; } } else if (vm_obj_is_table(obj)) { - vm_table_t *table = vm_obj_get_table(obj); + vm_obj_table_t *table = vm_obj_get_table(obj); if (!table->mark) { if (!table->pairs_auto) { vm_free(table->pairs); @@ -148,11 +148,9 @@ void vm_gc_sweep(vm_t *vm) { } } -void vm_table_init_size(vm_table_t *ret, size_t pow2); - -vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { +vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { vm_gc_t *gc = vm->gc; - vm_table_t *ret = vm_malloc(sizeof(vm_table_t) + sizeof(vm_table_pair_t) * (1 << pow2)); + vm_obj_table_t *ret = vm_malloc(sizeof(vm_obj_table_t) + sizeof(vm_table_pair_t) * (1 << pow2)); ret->pairs = (vm_table_pair_t *)&ret[1]; memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2)); ret->alloc = pow2; @@ -164,7 +162,7 @@ vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { return ret; } -vm_table_t *vm_table_new(vm_t *vm) { +vm_obj_table_t *vm_table_new(vm_t *vm) { return vm_table_new_size(vm, 2); } diff --git a/vm/gc.h b/vm/gc.h index 8e574014..f72a4d32 100644 --- a/vm/gc.h +++ b/vm/gc.h @@ -12,6 +12,6 @@ void vm_gc_deinit(vm_t *vm); void vm_gc_add(vm_t *vm, vm_obj_t obj); -vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2); +vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2); #endif diff --git a/vm/io.c b/vm/io.c index 8aaaa122..73f6d6e5 100644 --- a/vm/io.c +++ b/vm/io.c @@ -154,7 +154,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ vm_io_buffer_format(out, "\n", vm_obj_get_block(value)); } if (vm_obj_is_table(value)) { - vm_table_t *tab = vm_obj_get_table(value); + vm_obj_table_t *tab = vm_obj_get_table(value); if (tab == NULL) { vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "table(NULL)\n"); @@ -201,7 +201,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ } } -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) { +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) { if (vm_obj_is_nil(value)) { vm_io_buffer_format(buf, "nil"); } diff --git a/vm/io.h b/vm/io.h index 2d74ba00..7224f647 100644 --- a/vm/io.h +++ b/vm/io.h @@ -21,7 +21,7 @@ void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap); void vm_io_buffer_format(vm_io_buffer_t *buf, const char *fmt, ...); char *vm_io_vformat(const char *fmt, va_list ap); char *vm_io_format(const char *fmt, ...); -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); vm_io_buffer_t *vm_io_buffer_from_str(const char *str); #endif diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 6cffbde8..194245eb 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -25,7 +25,7 @@ void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) { } head += 1; const char *last_word = &prefix[head]; - vm_table_t *std = vm_obj_get_table(vm->std); + vm_obj_table_t *std = vm_obj_get_table(vm->std); with_new_std:; for (size_t i = 0; i < ((size_t)1 << std->alloc); i++) { vm_table_pair_t *pair = &std->pairs[i]; diff --git a/vm/obj.c b/vm/obj.c index c6d269e3..1e1152d8 100644 --- a/vm/obj.c +++ b/vm/obj.c @@ -22,7 +22,7 @@ bool vm_obj_eq(vm_obj_t v1, vm_obj_t v2) { } } -size_t vm_value_hash(vm_obj_t value) { +size_t vm_obj_hash(vm_obj_t value) { if (vm_obj_is_nil(value)) { return 0; } @@ -59,10 +59,10 @@ size_t vm_value_hash(vm_obj_t value) { // return 0; } -vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) { +vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key) { size_t len = 1 << table->alloc; size_t and = len - 1; - size_t stop = vm_value_hash(key) & and; + size_t stop = vm_obj_hash(key) & and; size_t next = stop; do { vm_table_pair_t *pair = &table->pairs[next]; @@ -79,23 +79,13 @@ vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) { return NULL; } -void vm_table_init_size(vm_table_t *ret, size_t pow2) { - ret->pairs = vm_malloc(sizeof(vm_table_pair_t) * (1 << pow2)); - memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2)); - ret->alloc = pow2; - ret->used = 0; - ret->len = 0; - ret->mark = false; - ret->pairs_auto = false; -} - -void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { +void vm_table_set(vm_obj_table_t *restrict table, vm_obj_t key, vm_obj_t value) { if (table->alloc == 0) { return; } size_t len = 1 << table->alloc; size_t and = len - 1; - size_t stop = vm_value_hash(key) & and; + size_t stop = vm_obj_hash(key) & and; size_t next = stop & and; do { vm_table_pair_t *pair = &table->pairs[next]; @@ -140,8 +130,14 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { } } } else if ((table->used + 1) * 100 >= ((uint32_t)1 << table->alloc) * 76) { - vm_table_t ret; - vm_table_init_size(&ret, table->alloc + 1); + vm_obj_table_t ret; + ret.pairs = vm_malloc(sizeof(vm_table_pair_t) * (2 << table->alloc)); + memset(ret.pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (2 << table->alloc)); + ret.alloc = table->alloc + 1; + ret.used = 0; + ret.len = 0; + ret.mark = false; + ret.pairs_auto = false; for (size_t i = 0; i < len; i++) { vm_table_pair_t *in_pair = &table->pairs[i]; if (!vm_obj_is_empty(in_pair->key) && !vm_obj_is_nil(in_pair->key)) { @@ -173,11 +169,11 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { } } -void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair) { +void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair) { vm_table_set(table, pair->key, pair->value); } -void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *out) { +void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *out) { vm_table_pair_t *pair = vm_table_lookup(table, out->key); if (pair != NULL) { out->value = pair->value; diff --git a/vm/obj.h b/vm/obj.h index c3f95ab2..fb6b5aea 100644 --- a/vm/obj.h +++ b/vm/obj.h @@ -7,12 +7,12 @@ struct vm_table_pair_t; typedef struct vm_table_pair_t vm_table_pair_t; bool vm_obj_eq(vm_obj_t lhs, vm_obj_t rhs); -bool vm_value_is_int(vm_obj_t val); +bool vm_obj_is_int(vm_obj_t val); -vm_table_t *vm_table_new(vm_t *vm); -vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key); -void vm_table_set(vm_table_t *table, vm_obj_t key, vm_obj_t value); -void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair); -void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *pair); +vm_obj_table_t *vm_table_new(vm_t *vm); +vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key); +void vm_table_set(vm_obj_table_t *table, vm_obj_t key, vm_obj_t value); +void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair); +void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *pair); #endif diff --git a/vm/std.c b/vm/std.c index cd2a510f..7e66f912 100644 --- a/vm/std.c +++ b/vm/std.c @@ -39,7 +39,7 @@ void vm_std_load(vm_t *vm, vm_obj_t *args) { const char *str = vm_obj_get_string(args[0])->buf; vm_ir_block_t *entry = vm_lang_lua_compile(vm, str, "__load__"); - vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t)); + vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t)); closure->block = entry; closure->len = 0; vm_obj_t ret = vm_obj_of_closure(closure); @@ -86,7 +86,7 @@ void vm_std_vm_closure(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_nil(); return; } - vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t) + sizeof(vm_obj_t) * (nargs - 1)); + vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t) + sizeof(vm_obj_t) * (nargs - 1)); closure->block = vm_obj_get_block(args[0]); closure->len = nargs - 1; for (size_t i = 1; !vm_obj_is_empty(args[i]); i++) { @@ -162,7 +162,7 @@ void vm_std_type(vm_t *vm, vm_obj_t *args) { void vm_std_tostring(vm_t *vm, vm_obj_t *args) { (void)vm; vm_io_buffer_t out = {0}; - vm_value_buffer_tostring(&out, *args); + vm_obj_buffer_tostring(&out, *args); *args = vm_str(vm, out.buf); vm_free(out.buf); } @@ -190,7 +190,7 @@ void vm_std_print(vm_t *vm, vm_obj_t *args) { if (!first) { vm_io_buffer_format(&out, "\t"); } - vm_value_buffer_tostring(&out, *args++); + vm_obj_buffer_tostring(&out, *args++); first = false; } fprintf(stdout, "%.*s\n", (int)out.len, out.buf); @@ -204,7 +204,7 @@ void vm_std_io_write(vm_t *vm, vm_obj_t *args) { vm_obj_t *ret = args; vm_io_buffer_t out = {0}; while (!vm_obj_is_empty(args[0])) { - vm_value_buffer_tostring(&out, *args++); + vm_obj_buffer_tostring(&out, *args++); } fprintf(stdout, "%.*s", (int)out.len, out.buf); *ret = vm_obj_of_nil(); @@ -378,7 +378,7 @@ void vm_std_string_format(vm_t *vm, vm_obj_t *args) { } void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv) { - vm_table_t *arg = vm_table_new(vm); + vm_obj_table_t *arg = vm_table_new(vm); if (prog != NULL) { vm_table_set(arg, vm_obj_of_number(-1), vm_str(vm, prog)); } @@ -397,8 +397,8 @@ void vm_std_table_keys(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table")); return; } - vm_table_t *ret = vm_table_new(vm); - vm_table_t *tab = vm_obj_get_table(args[0]); + vm_obj_table_t *ret = vm_table_new(vm); + vm_obj_table_t *tab = vm_obj_get_table(args[0]); size_t len = 1 << tab->alloc; size_t write_head = 1; for (size_t i = 0; i < len; i++) { @@ -418,8 +418,8 @@ void vm_std_table_values(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table")); return; } - vm_table_t *ret = vm_table_new(vm); - vm_table_t *tab = vm_obj_get_table(args[0]); + vm_obj_table_t *ret = vm_table_new(vm); + vm_obj_table_t *tab = vm_obj_get_table(args[0]); size_t len = 1 << tab->alloc; size_t write_head = 1; for (size_t i = 0; i < len; i++) { @@ -453,24 +453,24 @@ void vm_std_vm_import(vm_t *vm, vm_obj_t *args) { } void vm_std_new(vm_t *vm) { - vm_table_t *std = vm_table_new(vm); + vm_obj_table_t *std = vm_table_new(vm); srand(0); { - vm_table_t *io = vm_table_new(vm); + vm_obj_table_t *io = vm_table_new(vm); vm_table_set(std, vm_str(vm, "io"), vm_obj_of_table(io)); vm_table_set(io, vm_str(vm, "write"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_io_write))); } { - vm_table_t *string = vm_table_new(vm); + vm_obj_table_t *string = vm_table_new(vm); vm_table_set(std, vm_str(vm, "string"), vm_obj_of_table(string)); vm_table_set(string, vm_str(vm, "format"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_string_format))); } { - vm_table_t *tvm = vm_table_new(vm); + vm_obj_table_t *tvm = vm_table_new(vm); vm_table_set(std, vm_str(vm, "vm"), vm_obj_of_table(tvm)); vm_table_set(tvm, vm_str(vm, "import"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_import))); vm_table_set(tvm, vm_str(vm, "gc"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_gc))); @@ -480,19 +480,19 @@ void vm_std_new(vm_t *vm) { } { - vm_table_t *math = vm_table_new(vm); + vm_obj_table_t *math = vm_table_new(vm); vm_table_set(std, vm_str(vm, "math"), vm_obj_of_table(math)); vm_table_set(math, vm_str(vm, "randint"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_math_rand_int))); } { - vm_table_t *os = vm_table_new(vm); + vm_obj_table_t *os = vm_table_new(vm); vm_table_set(std, vm_str(vm, "os"), vm_obj_of_table(os)); vm_table_set(os, vm_str(vm, "exit"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_os_exit))); } { - vm_table_t *table = vm_table_new(vm); + vm_obj_table_t *table = vm_table_new(vm); vm_table_set(std, vm_str(vm, "table"), vm_obj_of_table(table)); vm_table_set(table, vm_str(vm, "keys"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_keys))); vm_table_set(table, vm_str(vm, "values"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_values))); diff --git a/vm/std.h b/vm/std.h index 95f08d21..a814524f 100644 --- a/vm/std.h +++ b/vm/std.h @@ -5,7 +5,7 @@ #define VM_STD_REF(vm, x) (vm_config_add_extern((vm), &(x)), &(x)) -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv); void vm_std_new(vm_t *vm); diff --git a/vm/vm.h b/vm/vm.h index a85a2eaf..7f3c4923 100644 --- a/vm/vm.h +++ b/vm/vm.h @@ -29,10 +29,9 @@ struct vm_t; struct vm_ir_block_t; struct vm_ir_blocks_t; -struct vm_closure_t; +struct vm_obj_closure_t; struct vm_externs_t; -union vm_value_t; -struct vm_table_t; +struct vm_obj_table_t; struct vm_table_pair_t; struct vm_table_pair_t; struct vm_io_buffer_t; @@ -40,14 +39,11 @@ struct vm_io_buffer_t; typedef struct vm_io_buffer_t vm_io_buffer_t; typedef struct vm_t vm_t; typedef struct vm_ir_blocks_t vm_ir_blocks_t; -typedef struct vm_closure_t vm_closure_t; +typedef struct vm_obj_closure_t vm_obj_closure_t; typedef struct vm_externs_t vm_externs_t; -typedef union vm_value_t vm_value_t; -typedef struct vm_table_t vm_table_t; +typedef struct vm_obj_table_t vm_obj_table_t; typedef struct vm_table_pair_t vm_table_pair_t; -#if VM_OBJ_FAST - #include "../vendor/nanbox/nanbox.h" #define VM_EMPTY_BYTE NANBOX_EMPTY_BYTE @@ -80,58 +76,18 @@ typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); #define vm_obj_get_boolean(o) nanbox_to_boolean(o) #define vm_obj_get_number(o) nanbox_to_double(o) #define vm_obj_get_string(o) ((vm_io_buffer_t *) nanbox_to_aux(o)) -#define vm_obj_get_table(o) ((vm_table_t *) nanbox_to_aux(o)) -#define vm_obj_get_closure(o) ((vm_closure_t *) nanbox_to_aux(o)) +#define vm_obj_get_table(o) ((vm_obj_table_t *) nanbox_to_aux(o)) +#define vm_obj_get_closure(o) ((vm_obj_closure_t *) nanbox_to_aux(o)) #define vm_obj_get_ffi(o) ((vm_ffi_t *) nanbox_to_aux(o)) #define vm_obj_get_block(o) ((vm_ir_block_t *) nanbox_to_aux(o)) #define vm_obj_get_error(o) ((vm_error_t *) nanbox_to_pointer(o)) -#else - -struct vm_obj_t; -typedef struct vm_obj_t vm_obj_t; - -enum { - VM_TAG_UNK, - VM_TAG_NIL, - VM_TAG_BOOL, - VM_TAG_NUMBER, - VM_TAG_STR, - VM_TAG_CLOSURE, - VM_TAG_FUN, - VM_TAG_TAB, - VM_TAG_FFI, - VM_TAG_ERROR, - VM_TAG_MAX, -}; - -typedef uint8_t vm_tag_t; - -typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); - -union vm_value_t { - bool boolean; - double f64; - vm_ffi_t *ffi; - vm_io_buffer_t *str; - vm_table_t *table; - vm_closure_t *closure; - struct vm_ir_block_t *fun; - struct vm_error_t *error; -}; - -struct vm_obj_t { - vm_value_t VM_OBJ_FIELD_VALUE; - vm_tag_t VM_OBJ_FIELD_TAG; -}; -#endif - struct vm_table_pair_t { vm_obj_t key; vm_obj_t value; }; -struct vm_table_t { +struct vm_obj_table_t { vm_table_pair_t *pairs; uint32_t len; uint32_t used; @@ -140,7 +96,7 @@ struct vm_table_t { bool pairs_auto: 1; }; -struct vm_closure_t { +struct vm_obj_closure_t { struct vm_ir_block_t *block; bool mark: 1; uint32_t len: 31; @@ -184,40 +140,4 @@ void vm_state_delete(vm_t *vm); void vm_lang_lua_repl(vm_t *vm); vm_obj_t vm_str(vm_t *vm, const char *str); -#if VM_OBJ_FAST -#else -#define VM_EMPTY_BYTE 0x00 - -#define vm_obj_of_empty() ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_UNK}) -#define vm_obj_of_nil() ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_NIL}) -#define vm_obj_of_boolean(b) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_BOOL, .VM_OBJ_FIELD_VALUE.boolean = (b)}) -#define vm_obj_of_number(n) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_NUMBER, .VM_OBJ_FIELD_VALUE.f64 = (n)}) -#define vm_obj_of_string(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_STR, .VM_OBJ_FIELD_VALUE.str = (o)}) -#define vm_obj_of_table(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_TAB, .VM_OBJ_FIELD_VALUE.table = (o)}) -#define vm_obj_of_closure(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_CLOSURE, .VM_OBJ_FIELD_VALUE.closure = (o)}) -#define vm_obj_of_ffi(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FFI, .VM_OBJ_FIELD_VALUE.ffi = (o)}) -#define vm_obj_of_block(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FUN, .VM_OBJ_FIELD_VALUE.fun = (o)}) -#define vm_obj_of_error(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FUN, .VM_OBJ_FIELD_VALUE.error = (o)}) - -#define vm_obj_get_boolean(o) ((o).VM_OBJ_FIELD_VALUE.boolean) -#define vm_obj_get_number(o) ((o).VM_OBJ_FIELD_VALUE.f64) -#define vm_obj_get_string(o) ((o).VM_OBJ_FIELD_VALUE.str) -#define vm_obj_get_table(o) ((o).VM_OBJ_FIELD_VALUE.table) -#define vm_obj_get_closure(o) ((o).VM_OBJ_FIELD_VALUE.closure) -#define vm_obj_get_ffi(o) ((o).VM_OBJ_FIELD_VALUE.ffi) -#define vm_obj_get_block(o) ((o).VM_OBJ_FIELD_VALUE.fun) -#define vm_obj_get_error(o) ((o).VM_OBJ_FIELD_VALUE.error) - -#define vm_obj_is_empty(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_UNK) -#define vm_obj_is_nil(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_NIL) -#define vm_obj_is_boolean(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_BOOL) -#define vm_obj_is_number(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_NUMBER) -#define vm_obj_is_string(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_STR) -#define vm_obj_is_table(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_TAB) -#define vm_obj_is_closure(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_CLOSURE) -#define vm_obj_is_ffi(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_FFI) -#define vm_obj_is_block(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_FUN) -#define vm_obj_is_error(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_ERROR) -#endif - #endif