From a934455f3f2804272c58082f8efc05a8cb6a8c9d Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Fri, 17 May 2024 15:40:34 -0400 Subject: [PATCH] sync web --- main/minivm.c | 2 +- vm/backend/exec.c | 8 ++++---- vm/lua/repl.c | 48 +++++++++++++++++++++++++++++++++++---------- vm/std/std.c | 6 +++++- web.mak | 4 ++-- web/package.json | 4 +++- web/src/lib/lua.js | 10 ++++++++++ web/src/lib/repl.js | 12 ++++++++++++ web/src/lib/wlua.js | 8 +++++++- 9 files changed, 82 insertions(+), 20 deletions(-) diff --git a/main/minivm.c b/main/minivm.c index 1852d1ec..276f9532 100644 --- a/main/minivm.c +++ b/main/minivm.c @@ -17,7 +17,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) int main(int argc, char **argv) { vm_config_t val_config = (vm_config_t) { .use_num = VM_USE_NUM_I32, - .tb_lbbv = true, + .tb_lbbv = false, .tb_regs_cast = true, .tb_recompile = true, #if defined(EMSCRIPTEN) diff --git a/vm/backend/exec.c b/vm/backend/exec.c index ca18fcc4..5ce86904 100644 --- a/vm/backend/exec.c +++ b/vm/backend/exec.c @@ -29,7 +29,7 @@ void *vm_cache_comp(const char *comp, const char *flags, const char *src, const fclose(out); vm_io_buffer_t *cmd_buf = vm_io_buffer_new(); // vm_io_buffer_format(cmd_buf, "%s -shared -g3 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); - vm_io_buffer_format(cmd_buf, "%s -shared -O3 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); + vm_io_buffer_format(cmd_buf, "%s -shared -O2 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); int res = system(cmd_buf->buf); if (res) { return NULL; @@ -97,17 +97,17 @@ void *vm_cache_comp(const char *comp, const char *flags, const char *src, const fwrite(src, len, 1, out); fclose(out); vm_io_buffer_t *cmd_buf = vm_io_buffer_new(); - vm_io_buffer_format(cmd_buf, "%s -shared -O3 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); + vm_io_buffer_format(cmd_buf, "%s -shared -O2 -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); // vm_io_buffer_format(cmd_buf, "%s -shared -g3 -fsanitize=memory -foptimize-sibling-calls -fPIC %s %s -o %s -w -pipe", comp, flags, c_file, so_file); int res = system(cmd_buf->buf); if (res) { return NULL; } - // remove(c_file); + remove(c_file); } void *handle = dlopen(so_file, RTLD_LAZY); void *sym = dlsym(handle, entry); - // remove(so_file); + remove(so_file); return sym; } #endif diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 85873d82..c58b467a 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -10,6 +10,8 @@ #include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h" #if defined(EMSCRIPTEN) +#include "../save/value.h" + #include #endif @@ -177,19 +179,15 @@ void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, voi // ic_highlight(henv, 1, strlen(input) - 2, "keyword"); } +#if defined(EMSCRIPTEN) +EM_JS(void, vm_lang_lua_repl_sync, (void), { + Module._vm_lang_lua_repl_sync(); +}); +#endif + void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) { config->is_repl = true; - // vm_table_t *repl = vm_table_new(); - // vm_lang_lua_repl_table_set_config(repl, config); - // VM_TABLE_SET(repl, str, "echo", b, true); - // vm_table_t *parens = vm_table_new(); - // VM_TABLE_SET(parens, i32, 1, str, "yellow"); - // VM_TABLE_SET(parens, i32, 2, str, "magenta"); - // VM_TABLE_SET(parens, i32, 3, str, "blue"); - // VM_TABLE_SET(repl, str, "parens", table, parens); - // VM_TABLE_SET(std, str, "config", table, repl); - ic_set_history(".minivm-history", 2000); vm_lang_lua_repl_complete_state_t complete_state = (vm_lang_lua_repl_complete_state_t){ @@ -207,6 +205,23 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) // setvbuf(stderr, NULL, _IONBF, 0); // #endif + #if defined(EMSCRIPTEN) + { + FILE *f = fopen("/wasm.bin", "rb"); + if (f != NULL) { + vm_save_t save = vm_save_load(f); + fclose(f); + 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); + } + } + } + #endif + const char *arr[] = { // "f = function() return 2 end", "f()", @@ -215,6 +230,19 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) const char **inputs = &arr[0]; while (true) { +#if defined(EMSCRIPTEN) + { + + vm_save_t save = vm_save_value(config, blocks, (vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std}); + FILE *f = fopen("/wasm.bin", "wb"); + if (f != NULL) { + fwrite(save.buf, 1, save.len, f); + fclose(f); + } + vm_lang_lua_repl_sync(); + } +#endif + #if 0 const char *input = *inputs++; #else diff --git a/vm/std/std.c b/vm/std/std.c index ab8ecb9a..f9e83088 100644 --- a/vm/std/std.c +++ b/vm/std/std.c @@ -90,6 +90,9 @@ void vm_std_vm_print(vm_std_closure_t *closure, vm_std_value_t *args) { vm_io_debug(&buf, 0, "", args[i], NULL); printf("%.*s", (int)buf.len, buf.buf); } + args[0] = (vm_std_value_t) { + .tag = VM_TAG_NIL, + }; } void vm_std_vm_concat(vm_std_closure_t *closure, vm_std_value_t *args) { @@ -524,8 +527,8 @@ 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_REF(config, vm_std_os_exit)); + VM_TABLE_SET(std, str, "os", table, os); } VM_TABLE_SET(std, str, "tostring", ffi, VM_STD_REF(config, vm_std_tostring)); @@ -534,6 +537,7 @@ vm_table_t *vm_std_new(vm_config_t *config) { 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)); + VM_TABLE_SET(std, str, "_G", table, std); VM_STD_REF(config, vm_std_vm_concat); VM_STD_REF(config, vm_lua_comp_op_std_pow); diff --git a/web.mak b/web.mak index 5250fa9e..34ccaa58 100644 --- a/web.mak +++ b/web.mak @@ -4,8 +4,8 @@ CC = emcc TCC_SRCS = CFLAGS_TB = -CFLAGS := -fPIC $(CFLAGS) -LDFLAGS := -s MALLOC=mimalloc -s STACK_SIZE=64mb -s INITIAL_MEMORY=256mb -s SINGLE_FILE=1 -s BINARYEN_ASYNC_COMPILATION=0 -s ASYNCIFY=0 -s ENVIRONMENT=worker -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" $(LDFLAGS) +CFLAGS += -fPIC -DNDEBUG +LDFLAGS += -fPIC -s MALLOC=mimalloc -s STACK_SIZE=64mb -s INITIAL_MEMORY=256mb -s SINGLE_FILE=1 -s BINARYEN_ASYNC_COMPILATION=0 -s ASYNCIFY=0 -s ENVIRONMENT=worker -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" GCCJIT = NO diff --git a/web/package.json b/web/package.json index c35979c8..c4a68c8e 100644 --- a/web/package.json +++ b/web/package.json @@ -12,6 +12,8 @@ "scripts": { "start": "npm run watch", + "base": "concurrently -n vite,server 'npm run vite:watch' 'npm run serve'", + "watch": "concurrently -n make,vite,server 'npm run make:watch' 'npm run vite:watch' 'npm run serve'", "serve": "python3 server.py", @@ -19,7 +21,7 @@ "build:watch": "concurrently -n make,vite 'npm run make:watch' 'npm run vite:watch'", "make:watch": "npx nodemon --watch ../vm --watch ../vendor --watch ../main --ext '*' --exec 'npm run make'", - "make": "make -C .. -f web.mak -Bj", + "make": "make -C .. -f web.mak -Bj CFLAGS='-DNDEBUG -fPIC' OPT='-g3'", "vite:watch": "npx vite build --watch", "vite": "npx vite build" diff --git a/web/src/lib/lua.js b/web/src/lib/lua.js index 892e113c..9a7972bf 100644 --- a/web/src/lib/lua.js +++ b/web/src/lib/lua.js @@ -1,12 +1,17 @@ import Module from '../../../build/bin/minivm.mjs'; + export const run = (args, opts) => { const mod = Module({ noInitialRun: true, _vm_compile_c_to_wasm(n) { mod.FS.writeFile(`/out${n}.so`, opts.comp(mod.FS.readFile(`/in${n}.c`))); }, + _vm_lang_lua_repl_sync() { + const buf = mod.FS.readFile('/wasm.bin'); + self.postMessage({type: 'sync', buf: buf}); + }, stdin() { return opts.stdin(); }, @@ -18,5 +23,10 @@ export const run = (args, opts) => { }, }); + if (opts.sync) { + console.log(opts.sync); + mod.FS.writeFile('/wasm.bin', new Uint8Array(opts.sync)); + } + mod.callMain(args); }; diff --git a/web/src/lib/repl.js b/web/src/lib/repl.js index 3c4dfc09..a8393ab9 100644 --- a/web/src/lib/repl.js +++ b/web/src/lib/repl.js @@ -98,6 +98,12 @@ export const repl = ({putchar}) => { break; } case 'get-buffer': { + let sync = null; + try { + sync = Uint8Array.from(JSON.parse(localStorage.getItem('minivm.state'))); + } catch (e) { + console.error(e); + } worker.postMessage({ type: 'buffer', ret: ret, @@ -105,6 +111,7 @@ export const repl = ({putchar}) => { inbuf: inbuf, want: want, has: has, + sync: sync, }); break; } @@ -115,6 +122,11 @@ export const repl = ({putchar}) => { }); break; } + case 'sync': { + const buf = data.buf; + localStorage.setItem('minivm.state', JSON.stringify(Array.from(buf))); + break; + } } }; }); diff --git a/web/src/lib/wlua.js b/web/src/lib/wlua.js index 878f57ba..c6d6b6ac 100644 --- a/web/src/lib/wlua.js +++ b/web/src/lib/wlua.js @@ -1,6 +1,7 @@ import {run} from './lua.js'; +let sync = null; let has; let want; let inbuf; @@ -48,7 +49,7 @@ const comp = (input) => { }; const onArgs = (args) => { - run(args, {stdin, stdout, stderr, comp}); + run(args, {stdin, stdout, stderr, comp, sync}); self.postMessage({ type: 'exit-ok', }); @@ -56,6 +57,10 @@ const onArgs = (args) => { self.onmessage = ({data}) => { switch(data.type) { + case 'sync': { + sync = data.buf; + break; + } case 'buffer': { self.postMessage({type: 'get-args'}); wait = data.wait; @@ -63,6 +68,7 @@ self.onmessage = ({data}) => { has = data.has; want = data.want; inbuf = data.inbuf; + sync = data.sync; break; } case 'args': {