diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 75080371..779821d7 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -1,24 +1,24 @@ #include "./repl.h" -#include "./parser.h" -#include "../ir/ir.h" +#include "../../vendor/trees/api.h" #include "../ast/ast.h" #include "../ast/comp.h" #include "../ast/print.h" #include "../backend/tb.h" +#include "../ir/ir.h" #include "../std/io.h" #include "../std/util.h" -#include "../../vendor/trees/api.h" +#include "./parser.h" const TSLanguage *tree_sitter_lua(void); vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str); vm_std_value_t vm_lang_lua_repl_table_get(vm_table_t *table, const char *key) { - vm_pair_t pair = (vm_pair_t) { + vm_pair_t pair = (vm_pair_t){ .key_tag = VM_TAG_STR, .key_val.str = key, }; vm_table_get_pair(table, &pair); - return (vm_std_value_t) { + return (vm_std_value_t){ .value = pair.val_val, .tag = pair.val_tag, }; @@ -62,11 +62,12 @@ void vm_lang_lua_repl_table_set_config(vm_table_t *table, vm_config_t *config) { VM_STD_SET_BOOL(dump, "time", config->dump_time); } +#if !defined(EMSCRIPTEN) void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) { vm_lang_lua_repl_complete_state_t *state = cenv->arg; ptrdiff_t len = strlen(prefix); ptrdiff_t head = len - 1; - while (head >= 0 &&(iswalnum(prefix[head]) || prefix[head] == '.')) { + while (head >= 0 && (iswalnum(prefix[head]) || prefix[head] == '.')) { head -= 1; } head += 1; @@ -81,7 +82,7 @@ with_new_std:; while (got[i] != '\0') { if (last_word[i] == '\0') { const char *completions[2]; - completions[0] = got+i; + completions[0] = got + i; completions[1] = NULL; if (!ic_add_completions(cenv, "", completions)) { goto ret; @@ -97,7 +98,7 @@ with_new_std:; if (pair->val_tag == VM_TAG_TAB) { if (last_word[i] == '.') { std = pair->val_val.table; - last_word = &last_word[i+1]; + last_word = &last_word[i + 1]; goto with_new_std; } if (!strcmp(last_word, got)) { @@ -129,7 +130,7 @@ const char *vm_lang_lua_repl_highlight_bracket_color(vm_table_t *repl, size_t de return ""; } vm_table_t *tab = value->val_val.table; - vm_pair_t *sub = vm_table_lookup(tab, (vm_value_t) {.i32 = (int32_t) depth % (int32_t) tab->len + 1}, VM_TAG_I32); + vm_pair_t *sub = vm_table_lookup(tab, (vm_value_t){.i32 = (int32_t)depth % (int32_t)tab->len + 1}, VM_TAG_I32); if (sub == NULL || sub->val_tag != VM_TAG_STR) { return ""; } @@ -214,6 +215,7 @@ void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, voi // fclose(out); // ic_highlight(henv, 1, strlen(input) - 2, "keyword"); } +#endif void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) { config->is_repl = true; @@ -222,13 +224,15 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) vm_lang_lua_repl_table_set_config(repl, config); VM_STD_SET_BOOL(repl, "echo", true); vm_table_t *parens = vm_table_new(); - vm_table_set(parens, (vm_value_t) {.i32 = 1}, (vm_value_t) {.str = "yellow"}, VM_TAG_I32, VM_TAG_STR); - vm_table_set(parens, (vm_value_t) {.i32 = 2}, (vm_value_t) {.str = "magenta"}, VM_TAG_I32, VM_TAG_STR); - vm_table_set(parens, (vm_value_t) {.i32 = 3}, (vm_value_t) {.str = "blue"}, VM_TAG_I32, VM_TAG_STR); + vm_table_set(parens, (vm_value_t){.i32 = 1}, (vm_value_t){.str = "yellow"}, VM_TAG_I32, VM_TAG_STR); + vm_table_set(parens, (vm_value_t){.i32 = 2}, (vm_value_t){.str = "magenta"}, VM_TAG_I32, VM_TAG_STR); + vm_table_set(parens, (vm_value_t){.i32 = 3}, (vm_value_t){.str = "blue"}, VM_TAG_I32, VM_TAG_STR); VM_STD_SET_TAB(repl, "parens", parens); VM_STD_SET_TAB(std, "config", repl); +#if !defined(EMSCRIPTEN) ic_set_history(".minivm-history", 2000); +#endif vm_lang_lua_repl_complete_state_t complete_state = (vm_lang_lua_repl_complete_state_t){ .config = config, @@ -239,25 +243,25 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) .std = std, }; - #if defined(EMSCRIPTEN) +#if defined(EMSCRIPTEN) setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - #endif +#endif while (true) { - #if !defined(EMSCRIPTEN) +#if !defined(EMSCRIPTEN) char *input = ic_readline_ex( - "lua", + "lua", vm_lang_lua_repl_completer, &complete_state, - vm_lang_lua_repl_highlight, + vm_lang_lua_repl_highlight, &highlight_state ); if (input == NULL) { break; } - #else +#else printf("lua> "); char input[256]; size_t head = 0; @@ -270,16 +274,18 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) printf("\x1B[D \x1B[D"); head -= 1; } else { - printf("%c", (int) c); + printf("%c", (int)c); } input[head++] = c; if (c == '\0') { break; } } - #endif +#endif vm_lang_lua_repl_table_get_config(repl, config); +#if !defined(EMSCRIPTEN) ic_history_add(input); +#endif clock_t start = clock(); if (config->dump_src) { @@ -288,14 +294,14 @@ 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); - #if !defined(EMSCRIPTEN) +#if !defined(EMSCRIPTEN) free(input); - #endif +#endif if (config->dump_ast) { vm_io_buffer_t buf = {0}; vm_ast_print_node(&buf, 0, "", node); - printf("\n--- ast ---\n%.*s", (int) buf.len, buf.buf); + printf("\n--- ast ---\n%.*s", (int)buf.len, buf.buf); } vm_ast_comp_more(node, blocks); @@ -304,7 +310,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) if (config->dump_ir) { vm_io_buffer_t buf = {0}; vm_io_format_blocks(&buf, blocks); - printf("%.*s", (int) buf.len, buf.buf); + printf("%.*s", (int)buf.len, buf.buf); } vm_std_value_t value = vm_tb_run_repl(config, blocks->entry, blocks, std); @@ -313,7 +319,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) } else if (vm_lang_lua_repl_table_get_bool(repl, "echo") && value.tag != VM_TAG_NIL) { vm_io_buffer_t buf = {0}; vm_io_debug(&buf, 0, "", value, NULL); - printf("%.*s", (int) buf.len, buf.buf); + printf("%.*s", (int)buf.len, buf.buf); } if (config->dump_time) { diff --git a/vm/lua/repl.h b/vm/lua/repl.h index 65492786..d3c9441a 100644 --- a/vm/lua/repl.h +++ b/vm/lua/repl.h @@ -4,8 +4,10 @@ #include "../config.h" #include "../obj.h" #include "../lib.h" +#if !defined(EMSCRIPTEN) #include "../../vendor/isocline/isocline.h" #include "../../vendor/isocline/completions.h" +#endif struct vm_lang_lua_repl_complete_state_t; struct vm_lang_lua_repl_complete_state_t; diff --git a/web.mak b/web.mak index a30e91fb..dab817f5 100644 --- a/web.mak +++ b/web.mak @@ -2,9 +2,10 @@ EXE ?= .js CC = emcc TCC_SRCS = +ISOCLINE_SRCS = CFLAGS := -fPIC $(CFLAGS) -LDFLAGS := -s BINARYEN_ASYNC_COMPILATION=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s ENVIRONMENT=web -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" $(LDFLAGS) +LDFLAGS := -s BINARYEN_ASYNC_COMPILATION=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s ENVIRONMENT=worker -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" $(LDFLAGS) include makefile diff --git a/web/src/lib/comp.js b/web/src/lib/comp.js index d74fc0c5..e805af84 100644 --- a/web/src/lib/comp.js +++ b/web/src/lib/comp.js @@ -15,7 +15,6 @@ let flags2 = '-L/lazy/emscripten/cache/sysroot/lib/wasm32-emscripten/pic --no-wh let comps = 0; export const comp = (cBuf) => { comps += 1; - console.log(cBuf); emception.fileSystem.writeFile(`/working/in${comps}.c`, cBuf); const result1 = emception.runx(`/usr/bin/clang -O2 -c ${flags1} /working/in${comps}.c -o /working/mid${comps}.o`); if (result1.returncode !== 0) { diff --git a/web/src/lib/emception.js b/web/src/lib/emception.js index d3eb4b93..53425312 100644 --- a/web/src/lib/emception.js +++ b/web/src/lib/emception.js @@ -31,8 +31,8 @@ class Emception { } fileSystem.mkdirTree("/usr/local"); - fileSystem.symlink("/lazy/emscripten", "/emscripten"); - fileSystem.symlink("/lazy/cpython", "/usr/local/lib"); + // fileSystem.symlink("/lazy/emscripten", "/emscripten"); + // fileSystem.symlink("/lazy/cpython", "/usr/local/lib"); fileSystem.symlink("/lazy/wasm", "/wasm"); for (const preload of preloads) { diff --git a/web/src/lib/lua.js b/web/src/lib/lua.js index f3236e6d..93005505 100644 --- a/web/src/lib/lua.js +++ b/web/src/lib/lua.js @@ -23,7 +23,7 @@ export const run = (args, opts) => { mod.FS.writeFile(`/out${n}.so`, soBuf); }; - globalThis.vm_compile_c_to_wasm = comp; + self.vm_compile_c_to_wasm = comp; const mod = Module({ noInitialRun: true, diff --git a/web/webpack.config.js b/web/webpack.config.js index dc4e8439..0890bc9e 100644 --- a/web/webpack.config.js +++ b/web/webpack.config.js @@ -2,8 +2,10 @@ import CompressionPlugin from 'compression-webpack-plugin'; import CopyPlugin from 'copy-webpack-plugin'; import MiniCssExtractPlugin from 'mini-css-extract-plugin'; import path from 'path'; +import { fileURLToPath } from 'url'; +import webpack from 'webpack'; -const dev = false; +const dev = true; export default { mode: dev ? 'development' : 'production', @@ -29,6 +31,10 @@ export default { process: false, }, }, + externals: { + 'brotli.mjs': false, + 'brotli.wasm': false, + }, module: { rules: [ { @@ -53,6 +59,9 @@ export default { { test: /\.wasm$/, type: "asset/resource", + generator: { + filename: 'wasm/[name].wasm', + }, }, { test: /\.(pack|br|a)$/, @@ -90,9 +99,13 @@ export default { { from: "src/index.html", to: "index.html" }, ], }), - new CompressionPlugin({ - exclude: /\.br$/, - }), + new webpack.NormalModuleReplacementPlugin( + /emception\/brotli\/brotli\.mjs$/, + path.resolve(fileURLToPath(new URL('.', import.meta.url)), './src/empty.js'), + ), + // new CompressionPlugin({ + // exclude: /\.br$/, + // }), ], devServer: { headers: {