From 45608709cd50ca34d9f8308a268f3eddaacc7104 Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Thu, 16 May 2024 00:42:20 -0400 Subject: [PATCH] snapshot code --- core.mak | 11 ++++-- main/minivm.c | 5 +-- vm/backend/tb.c | 8 +++-- vm/backend/tb_dyn.h | 2 +- vm/config.h | 11 ++++++ vm/ir/ir.h | 15 +++++++++ vm/lib.h | 14 ++++++++ vm/lua/repl.c | 28 +++++++++++---- vm/obj.c | 3 ++ vm/save/io.c | 24 +++++++++++++ vm/save/{save.h => io.h} | 7 ++-- vm/save/read.c | 73 ++++++++++++++++++++++++++++++---------- vm/save/value.h | 18 ++++++++++ vm/save/write.c | 71 ++++++++++++++++++++------------------ vm/std/std.c | 21 ++++++------ vm/std/std.h | 2 ++ 16 files changed, 233 insertions(+), 80 deletions(-) create mode 100644 vm/save/io.c rename vm/save/{save.h => io.h} (50%) create mode 100644 vm/save/value.h diff --git a/core.mak b/core.mak index ff9d3bb3..57207272 100644 --- a/core.mak +++ b/core.mak @@ -43,7 +43,14 @@ GC_OBJS += $(GC_SRCS:%.c=$(OBJ_DIR)/%.o) VENDOR_SRCS += $(ISOCLINE_SRCS) $(XXH_SRCS) $(TREES_SRCS) $(GC_OBJS) VENDOR_OBJS = $(VENDOR_SRCS:%.c=$(OBJ_DIR)/%.o) -VM_SRCS += vm/std/io.c vm/std/std.c vm/save/write.c vm/save/read.c vm/ir/ir.c vm/lib.c vm/ir/type.c vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c vm/obj.c vm/backend/tb.c vm/backend/exec.c vm/ir/check.c vm/ir/rblock.c vm/lua/parser/parser.c vm/lua/parser/scan.c vm/lua/ast.c vm/lua/repl.c $(STD_SRCS) +VM_AST_SRCS += vm/ast/build.c vm/ast/ast.c vm/ast/comp.c vm/ast/print.c +VM_BACKEND_SRCS += vm/backend/tb.c vm/backend/exec.c +VM_BASE_SRCS += vm/lib.c vm/obj.c +VM_DATA_SRCS += vm/save/io.c vm/save/write.c vm/save/read.c +VM_IR_SRCS += vm/ir/ir.c vm/ir/type.c vm/ir/rblock.c vm/ir/check.c +VM_LUA_SRCS += vm/lua/parser/parser.c vm/lua/parser/scan.c vm/lua/repl.c vm/lua/ast.c +VM_STD_SRCS += vm/std/io.c vm/std/std.c +VM_SRCS += $(VM_AST_SRCS) $(VM_BASE_SRCS) $(VM_BACKEND_SRCS) $(VM_DATA_SRCS) $(VM_IR_SRCS) $(VM_LUA_SRCS) $(VM_STD_SRCS) VM_OBJS = $(VM_SRCS:%.c=$(OBJ_DIR)/%.o) TCC_SRCS ?= $(TCC_DIR)/libtcc.c @@ -83,7 +90,7 @@ CFLAGS_TB_WASM_YES = -DTB_HAS_WASM CFLAGS_MACOS = -I/opt/homebrew/include CFLAGS := $(CFLAGS_$(OS)) $(CFLAGS_GCCJIT_$(GCCJIT)) $(CFLAGS_TB_WASM_$(TB_WASM)) $(CFLAGS) -LDFLAGS := -no-pie $(FLAGS) $(LDFLAGS) +LDFLAGS := $(FLAGS) $(LDFLAGS) CFLAGS := $(FLAGS) $(CFLAGS) MKDIR = @mkdir -p diff --git a/main/minivm.c b/main/minivm.c index ed0f2aa3..a5a8b5f6 100644 --- a/main/minivm.c +++ b/main/minivm.c @@ -197,10 +197,7 @@ int main(int argc, char **argv) { } vm_ast_node_t node = vm_lang_lua_parse(config, src); - - if (name != NULL) { - vm_free(src); - } + vm_blocks_add_src(blocks, src); if (config->dump_ast) { vm_io_buffer_t buf = {0}; diff --git a/vm/backend/tb.c b/vm/backend/tb.c index 5b99e624..957bac12 100644 --- a/vm/backend/tb.c +++ b/vm/backend/tb.c @@ -122,13 +122,17 @@ vm_std_value_t vm_tb_run_repl(vm_config_t *config, vm_block_t *entry, vm_blocks_ vm_tb_dyn_func_t *fn = (vm_tb_dyn_func_t *)vm_tb_dyn_comp(state, entry); + vm_std_value_t values[1] = { + [0].tag = VM_TAG_UNK, + }; + #if defined(_WIN32) caller(&value, fn); #else - value = fn(); + value = fn(&values[0]); #endif - blocks->len = 0; + // blocks->len = 0; vm_free(state); } diff --git a/vm/backend/tb_dyn.h b/vm/backend/tb_dyn.h index 74a5bb76..5745341c 100644 --- a/vm/backend/tb_dyn.h +++ b/vm/backend/tb_dyn.h @@ -13,7 +13,7 @@ struct vm_tb_dyn_state_t; typedef struct vm_tb_dyn_pair_t vm_tb_dyn_pair_t; typedef struct vm_tb_dyn_state_t vm_tb_dyn_state_t; -typedef vm_std_value_t VM_CDECL vm_tb_dyn_func_t(void); +typedef vm_std_value_t VM_CDECL vm_tb_dyn_func_t(vm_std_value_t *values); struct vm_tb_dyn_pair_t { TB_Node *val; diff --git a/vm/config.h b/vm/config.h index f49eb737..b5f93397 100644 --- a/vm/config.h +++ b/vm/config.h @@ -7,6 +7,9 @@ #define VM_USE_DUMP 1 #define VM_FORMAT_FLOAT "%.14g" +struct vm_externs_t; +typedef struct vm_externs_t vm_externs_t; + struct vm_config_t; typedef struct vm_config_t vm_config_t; @@ -42,7 +45,15 @@ enum { VM_USE_VERSION_COUNT_FINE, }; +struct vm_externs_t { + size_t id; + void *value; + vm_externs_t *last; +}; + struct vm_config_t { + vm_externs_t *externs; + const char *cflags; unsigned int target : 4; diff --git a/vm/ir/ir.h b/vm/ir/ir.h index 5a343aec..10b011d1 100644 --- a/vm/ir/ir.h +++ b/vm/ir/ir.h @@ -8,6 +8,7 @@ struct vm_instr_t; struct vm_block_t; struct vm_rblock_t; struct vm_cache_t; +struct vm_blocks_srcs_t; struct vm_blocks_t; typedef struct vm_rblock_t vm_rblock_t; @@ -16,6 +17,7 @@ typedef struct vm_arg_t vm_arg_t; typedef struct vm_branch_t vm_branch_t; typedef struct vm_instr_t vm_instr_t; typedef struct vm_block_t vm_block_t; +typedef struct vm_blocks_srcs_t vm_blocks_srcs_t; typedef struct vm_blocks_t vm_blocks_t; #include "../lib.h" @@ -157,11 +159,17 @@ struct vm_block_t { bool checked : 1; }; +struct vm_blocks_srcs_t { + const char *src; + vm_blocks_srcs_t *last; +}; + struct vm_blocks_t { size_t len; vm_block_t **blocks; size_t alloc; vm_block_t *entry; + vm_blocks_srcs_t *srcs; }; void vm_block_realloc(vm_block_t *block, vm_instr_t instr); @@ -181,4 +189,11 @@ void vm_free_block(vm_block_t *block); #define vm_arg_nil() ((vm_arg_t){.type = (VM_ARG_LIT), .lit.tag = (VM_TAG_NIL)}) +static inline void vm_blocks_add_src(vm_blocks_t *blocks, const char *src) { + vm_blocks_srcs_t *next = vm_malloc(sizeof(vm_blocks_srcs_t)); + next->last = blocks->srcs; + next->src = src; + blocks->srcs = next; +} + #endif diff --git a/vm/lib.h b/vm/lib.h index cfce9794..04084b2b 100644 --- a/vm/lib.h +++ b/vm/lib.h @@ -70,4 +70,18 @@ static char *vm_strdup(const char *str) { #define VM_CDECL #endif +static inline void vm_config_add_extern(vm_config_t *config, void *value) { + vm_externs_t *last = config->externs; + for (vm_externs_t *cur = last; cur; cur = cur->last) { + if (cur->value == value) { + return; + } + } + vm_externs_t *next = vm_malloc(sizeof(vm_externs_t)); + next->id = last == NULL ? 0 : last->id + 1; + next->value = value; + next->last = last; + config->externs = next; +} + #endif diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 529a423b..9f1d6ba5 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -6,6 +6,7 @@ #include "../backend/tb.h" #include "../ir/ir.h" #include "../std/io.h" +#include "../save/value.h" #include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h" @@ -13,7 +14,6 @@ #include #endif -#include "../save/save.h" const TSLanguage *tree_sitter_lua(void); vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str); @@ -241,9 +241,13 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) if (f != NULL) { vm_save_t save = vm_save_load(f); fclose(f); - vm_std_value_t val = vm_load_value(save); - if (val.tag == VM_TAG_TAB) { - std = val.value.table; + vm_save_loaded_t ld = vm_load_value(config, save); + if (ld.blocks != NULL) { + *blocks = *ld.blocks; + *std = *ld.env.value.table; + vm_io_buffer_t *buf = vm_io_buffer_new(); + vm_io_format_blocks(buf, blocks); + printf("%s", buf->buf); } } } @@ -254,7 +258,17 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) // setvbuf(stderr, NULL, _IONBF, 0); // #endif + const char *arr[] = { + // "f = function() return 2 end", + "f()", + NULL, + }; + const char **inputs = &arr[0]; + while (true) { +#if 0 + const char *input = *inputs++; +#else char *input = ic_readline_ex( "lua", vm_lang_lua_repl_completer, @@ -262,6 +276,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) vm_lang_lua_repl_highlight, &highlight_state ); +#endif if (input == NULL) { break; } @@ -274,7 +289,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) } vm_ast_node_t node = vm_lang_lua_parse(config, input); - free(input); + vm_blocks_add_src(blocks, input); if (config->dump_ast) { vm_io_buffer_t buf = {0}; @@ -282,6 +297,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) printf("\n--- ast ---\n%.*s", (int)buf.len, buf.buf); } + vm_ast_comp_more(node, blocks); vm_ast_free_node(node); @@ -311,7 +327,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) printf("took: %.3fms\n", diff); } - vm_save_t save = vm_save_value((vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std}); + vm_save_t save = vm_save_value(config, blocks, (vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std}); FILE *f = fopen("out.bin", "wb"); if (f != NULL) { fwrite(save.buf, 1, save.len, f); diff --git a/vm/obj.c b/vm/obj.c index 7a088c26..c4b70019 100644 --- a/vm/obj.c +++ b/vm/obj.c @@ -251,6 +251,9 @@ bool vm_value_eq(vm_std_value_t lhs, vm_std_value_t rhs) { case VM_TAG_STR: { return vm_type_eq(rhs.tag, VM_TAG_STR) && !strcmp(lhs.value.str, rhs.value.str); } + case VM_TAG_FUN: { + return rhs.tag == VM_TAG_FUN && lhs.value.i32 == rhs.value.i32; + } default: { return vm_type_eq(lhs.tag, rhs.tag) && lhs.value.all == rhs.value.all; } diff --git a/vm/save/io.c b/vm/save/io.c new file mode 100644 index 00000000..cbde110c --- /dev/null +++ b/vm/save/io.c @@ -0,0 +1,24 @@ + +#include "io.h" + +vm_save_t vm_save_load(FILE *in) { + size_t nalloc = 512; + uint8_t *ops = vm_malloc(sizeof(char) * nalloc); + size_t nops = 0; + size_t size; + while (true) { + if (nops + 256 >= nalloc) { + nalloc = (nops + 256) * 2; + ops = vm_realloc(ops, sizeof(char) * nalloc); + } + size = fread(&ops[nops], 1, 256, in); + nops += size; + if (size < 256) { + break; + } + } + return (vm_save_t) { + .len = nops, + .buf = ops, + }; +} diff --git a/vm/save/save.h b/vm/save/io.h similarity index 50% rename from vm/save/save.h rename to vm/save/io.h index 3c9a4bf1..3868c385 100644 --- a/vm/save/save.h +++ b/vm/save/io.h @@ -1,8 +1,7 @@ -#if !defined(VM_HEADER_SAVE_SAVE) -#define VM_HEADER_SAVE_SAVE +#if !defined(VM_HEADER_SAVE_IO) +#define VM_HEADER_SAVE_IO #include "../lib.h" -#include "../std/io.h" struct vm_save_t; typedef struct vm_save_t vm_save_t; @@ -13,7 +12,5 @@ struct vm_save_t { }; vm_save_t vm_save_load(FILE *file); -vm_std_value_t vm_load_value(vm_save_t arg); -vm_save_t vm_save_value(vm_std_value_t arg); #endif diff --git a/vm/save/read.c b/vm/save/read.c index ab40f33c..6a216e25 100644 --- a/vm/save/read.c +++ b/vm/save/read.c @@ -1,5 +1,9 @@ -#include "save.h" +#include "value.h" +#include "../ast/ast.h" +#include "../ast/comp.h" + +vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str); struct vm_save_read_t; typedef struct vm_save_read_t vm_save_read_t; @@ -44,18 +48,17 @@ static uint64_t vm_save_read_uleb(vm_save_read_t *read) { } static int64_t vm_save_read_sleb(vm_save_read_t *read) { - int64_t x = 0; - size_t shift = 0; + int64_t result = 0; + uint8_t shift = 0; while (true) { - uint8_t buf = vm_save_read_byte(read); - x += (buf & 0x7f) << shift; + uint8_t byte = vm_save_read_byte(read); + result |= (byte & 0x7f) << shift; shift += 7; - if (buf < 0x80) { - if (shift < 64 && (buf & 0x40)) { - return (int64_t)(x - ((int64_t)1 << shift)); - } else { - return x; + if ((0x80 & byte) == 0) { + if (shift < 64 && (byte & 0x40) != 0) { + return result | (~0 << shift); } + return result; } } } @@ -64,7 +67,7 @@ bool vm_save_read_is_done(vm_save_read_t *read) { return read->buf.read == read->buf.len; } -vm_std_value_t vm_load_value(vm_save_t arg) { +vm_save_loaded_t vm_load_value(vm_config_t *config, vm_save_t arg) { vm_save_read_t read = (vm_save_read_t){ .buf.len = arg.len, .buf.bytes = arg.buf, @@ -73,13 +76,14 @@ vm_std_value_t vm_load_value(vm_save_t arg) { .values.ptr = NULL, .values.alloc = 0, }; - while (!vm_save_read_is_done(&read)) { + while (true) { size_t start = read.buf.read; vm_tag_t tag = (vm_tag_t) vm_save_read_byte(&read); vm_value_t value; + // printf("object #%zu at [0x%zX] with type %zu\n", read.values.len, start, (size_t) tag); switch (tag) { case VM_TAG_UNK: { - break; + goto outer; } case VM_TAG_NIL: { value.all = NULL; @@ -127,7 +131,13 @@ vm_std_value_t vm_load_value(vm_save_t arg) { break; } case VM_TAG_FFI: { - value.all = (void *) (size_t) vm_save_read_uleb(&read); + size_t id = vm_save_read_uleb(&read); + value.all = NULL; + for (vm_externs_t *cur = config->externs; cur; cur = cur->last) { + if (cur->id == id) { + value.all = cur->value; + } + } break; } case VM_TAG_CLOSURE: { @@ -142,6 +152,7 @@ vm_std_value_t vm_load_value(vm_save_t arg) { vm_save_read_uleb(&read); } value.closure = closure; + break; } case VM_TAG_TAB: { uint64_t real = vm_save_read_uleb(&read); @@ -154,7 +165,7 @@ vm_std_value_t vm_load_value(vm_save_t arg) { break; } default: { - fprintf(stderr, "unhandled object #%zu at [%zu]: type %zu\n", read.values.len, start, (size_t) tag); + fprintf(stderr, "unhandled object #%zu at [0x%zX]: type %zu\n", read.values.len, start, (size_t) tag); goto error; } } @@ -170,6 +181,8 @@ vm_std_value_t vm_load_value(vm_save_t arg) { }, }; } +outer:; + size_t loc = read.buf.read; for (size_t i = 0; i < read.values.len; i++) { vm_save_value_t save = read.values.ptr[i]; vm_value_t value = save.value.value; @@ -188,7 +201,7 @@ vm_std_value_t vm_load_value(vm_save_t arg) { case VM_TAG_TAB: { uint64_t real = vm_save_read_uleb(&read); vm_table_t *table = value.table; - for (size_t i = 0; i < real; i++) { + for (uint64_t i = 0; i < real; i++) { size_t key_index = vm_save_read_uleb(&read); size_t value_index = vm_save_read_uleb(&read); VM_TABLE_SET_VALUE(table, read.values.ptr[key_index].value, read.values.ptr[value_index].value); @@ -200,7 +213,31 @@ vm_std_value_t vm_load_value(vm_save_t arg) { } } } - return read.values.ptr[0].value; + read.buf.read = loc; + vm_blocks_t *blocks = vm_malloc(sizeof(vm_blocks_t)); + blocks->len = 0; + blocks->alloc = 0; + blocks->blocks = NULL; + blocks->srcs = NULL; + uint64_t nsrcs = vm_save_read_uleb(&read); + for (uint64_t i = 0; i < nsrcs; i++) { + uint64_t len = vm_save_read_uleb(&read); + char *src = vm_malloc(sizeof(char) * len); + for (uint64_t j = 0; j < len; j++) { + src[j] = (char) vm_save_read_byte(&read); + } + vm_ast_node_t node = vm_lang_lua_parse(config, src); + vm_ast_comp_more(node, blocks); + // vm_ast_free_node(node); + vm_blocks_add_src(blocks, src); + } + return (vm_save_loaded_t) { + .blocks = blocks, + .env = read.values.ptr[0].value, + }; error:; - return (vm_std_value_t) {.tag = VM_TAG_NIL}; + return (vm_save_loaded_t) { + .blocks = NULL, + .env = (vm_std_value_t) {.tag = VM_TAG_NIL}, + }; } diff --git a/vm/save/value.h b/vm/save/value.h new file mode 100644 index 00000000..c28514b4 --- /dev/null +++ b/vm/save/value.h @@ -0,0 +1,18 @@ +#if !defined(VM_HEADER_SAVE_DATA_VALUE) +#define VM_HEADER_SAVE_DATA_VALUE + +#include "./io.h" +#include "../obj.h" + +struct vm_save_loaded_t; +typedef struct vm_save_loaded_t vm_save_loaded_t; + +struct vm_save_loaded_t { + vm_blocks_t *blocks; + vm_std_value_t env; +}; + +vm_save_loaded_t vm_load_value(vm_config_t *config, vm_save_t arg); +vm_save_t vm_save_value(vm_config_t *config, vm_blocks_t *blocks, vm_std_value_t arg); + +#endif diff --git a/vm/save/write.c b/vm/save/write.c index a6fb1d8d..b0637529 100644 --- a/vm/save/write.c +++ b/vm/save/write.c @@ -1,5 +1,5 @@ -#include "save.h" +#include "value.h" struct vm_save_write_t; typedef struct vm_save_write_t vm_save_write_t; @@ -63,20 +63,21 @@ void vm_save_write_uleb(vm_save_write_t *write, uint64_t value) { } void vm_save_write_sleb(vm_save_write_t *write, int64_t value) { - size_t len = 0; - uint8_t result[10]; while (true) { - uint8_t byte = value & 0x7F; + uint8_t byte_ = value & 0x7f; value >>= 7; - if ((value == 0 && (byte & 0x40) == 0) || (value == -1 && (byte & 0x40) != 0)) { - vm_save_write_uleb(write, byte); + if ( + (value == 0 && (byte_ & 0x40) == 0) || + (value == -1 && (byte_ & 0x40) != 0) + ) { + vm_save_write_byte(write, byte_); break; } - vm_save_write_uleb(write, byte | 0x80); + vm_save_write_byte(write, byte_ | 0x80); } } -vm_save_t vm_save_value(vm_std_value_t arg) { +vm_save_t vm_save_value(vm_config_t *config, vm_blocks_t *blocks, vm_std_value_t arg) { vm_save_write_t write = (vm_save_write_t){ .buf.len = 0, .buf.bytes = NULL, @@ -88,6 +89,7 @@ vm_save_t vm_save_value(vm_std_value_t arg) { }; vm_save_write_push(&write, arg); while (!vm_save_write_is_done(&write)) { + // printf("object #%zu at [0x%zX]\n", write.values.read, write.buf.len); vm_std_value_t value = vm_save_write_shift(&write); vm_save_write_byte(&write, value.tag); switch (value.tag) { @@ -119,7 +121,7 @@ vm_save_t vm_save_value(vm_std_value_t arg) { break; } case VM_TAG_F32: { - vm_save_write_uleb(&write, (uint64_t) *(uint32_t *) &value.value.f64); + vm_save_write_uleb(&write, (uint64_t) *(uint32_t *) &value.value.f32); break; } case VM_TAG_F64: { @@ -171,7 +173,14 @@ vm_save_t vm_save_value(vm_std_value_t arg) { break; } case VM_TAG_FFI: { - vm_save_write_uleb(&write, (uint64_t) (size_t) value.value.all); + for (vm_externs_t *cur = config->externs; cur; cur = cur->last) { + if (cur->value == value.value.all) { + vm_save_write_uleb(&write, cur->id); + goto has_ffi; + } + } + fprintf(stderr, "error unknown ffi: %p", value.value.all); + has_ffi:; break; } default: { @@ -182,30 +191,28 @@ vm_save_t vm_save_value(vm_std_value_t arg) { } } } +outer:; + vm_save_write_byte(&write, VM_TAG_UNK); + uint64_t nsrcs = 0; + for (vm_blocks_srcs_t *cur = blocks->srcs; cur; cur = cur->last) { + nsrcs += 1; + } + vm_save_write_uleb(&write, nsrcs); + const char **srcs = vm_malloc(sizeof(const char *) * nsrcs); + for (vm_blocks_srcs_t *cur = blocks->srcs; cur; cur = cur->last) { + srcs[--nsrcs] = cur->src; + } + for (vm_blocks_srcs_t *cur = blocks->srcs; cur; cur = cur->last) { + const char *src = srcs[nsrcs++]; + size_t len = strlen(src) + 1; + vm_save_write_uleb(&write, (uint64_t) len); + for (size_t i = 0; i < len; i++) { + vm_save_write_byte(&write, (uint8_t) src[i]); + } + } + vm_free(srcs); return (vm_save_t){ .len = write.buf.len, .buf = write.buf.bytes, }; } - -vm_save_t vm_save_load(FILE *in) { - size_t nalloc = 512; - uint8_t *ops = vm_malloc(sizeof(char) * nalloc); - size_t nops = 0; - size_t size; - while (true) { - if (nops + 256 >= nalloc) { - nalloc = (nops + 256) * 2; - ops = vm_realloc(ops, sizeof(char) * nalloc); - } - size = fread(&ops[nops], 1, 256, in); - nops += size; - if (size < 256) { - break; - } - } - return (vm_save_t) { - .len = nops, - .buf = ops, - }; -} diff --git a/vm/std/std.c b/vm/std/std.c index 5e0896b6..a186307d 100644 --- a/vm/std/std.c +++ b/vm/std/std.c @@ -20,6 +20,7 @@ void vm_std_load(vm_std_closure_t *closure, vm_std_value_t *args) { } const char *str = args[0].value.str; vm_ast_node_t node = vm_lang_lua_parse(closure->config, str); + vm_blocks_add_src(closure->blocks, str); vm_ast_comp_more(node, closure->blocks); vm_ast_free_node(node); @@ -500,19 +501,19 @@ vm_table_t *vm_std_new(vm_config_t *config) { { vm_table_t *io = vm_table_new(); VM_TABLE_SET(std, str, "io", table, io); - VM_TABLE_SET(io, str, "write", ffi, &vm_std_io_write); + VM_TABLE_SET(io, str, "write", ffi, VM_STD_REF(config, vm_std_io_write)); } { vm_table_t *string = vm_table_new(); VM_TABLE_SET(std, str, "string", table, string); - VM_TABLE_SET(string, str, "format", ffi, &vm_std_string_format); + VM_TABLE_SET(string, str, "format", ffi, VM_STD_REF(config, vm_std_string_format)); } { vm_table_t *vm = vm_table_new(); VM_TABLE_SET(std, str, "vm", table, vm); - VM_TABLE_SET(vm, str, "print", ffi, &vm_std_vm_print); + VM_TABLE_SET(vm, str, "print", ffi, VM_STD_REF(config, vm_std_vm_print)); { vm_table_t *vm_ver = vm_table_new(); VM_TABLE_SET(vm, str, "version", table, vm_ver); @@ -522,15 +523,15 @@ vm_table_t *vm_std_new(vm_config_t *config) { { vm_table_t *os = vm_table_new(); VM_TABLE_SET(std, str, "os", table, os); - VM_TABLE_SET(os, str, "exit", ffi, &vm_std_os_exit); + VM_TABLE_SET(os, str, "exit", ffi, VM_STD_REF(config, vm_std_os_exit)); } - VM_TABLE_SET(std, str, "tostring", ffi, &vm_std_tostring); - VM_TABLE_SET(std, str, "tonumber", ffi, &vm_std_tonumber); - VM_TABLE_SET(std, str, "type", ffi, &vm_std_type); - VM_TABLE_SET(std, str, "print", ffi, &vm_std_print); - VM_TABLE_SET(std, str, "assert", ffi, &vm_std_assert); - VM_TABLE_SET(std, str, "load", ffi, &vm_std_load); + VM_TABLE_SET(std, str, "tostring", ffi, VM_STD_REF(config, vm_std_tostring)); + VM_TABLE_SET(std, str, "tonumber", ffi, VM_STD_REF(config, vm_std_tonumber)); + VM_TABLE_SET(std, str, "type", ffi, VM_STD_REF(config, vm_std_type)); + VM_TABLE_SET(std, str, "print", ffi, VM_STD_REF(config, vm_std_print)); + VM_TABLE_SET(std, str, "assert", ffi, VM_STD_REF(config, vm_std_assert)); + VM_TABLE_SET(std, str, "load", ffi, VM_STD_REF(config, vm_std_load)); return std; } diff --git a/vm/std/std.h b/vm/std/std.h index 9a3f0f72..69e85525 100644 --- a/vm/std/std.h +++ b/vm/std/std.h @@ -4,6 +4,8 @@ #include "../obj.h" #include "./io.h" +#define VM_STD_REF(cfg, x) (vm_config_add_extern(cfg, &x), &x) + void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_std_value_t value); void vm_std_set_arg(vm_config_t *config, vm_table_t *std, const char *prog, const char *file, int argc, char **argv); vm_table_t *vm_std_new(vm_config_t *config);