Skip to content

Commit

Permalink
sync web
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed May 17, 2024
1 parent 611a13d commit a934455
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 20 deletions.
2 changes: 1 addition & 1 deletion main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions vm/backend/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
48 changes: 38 additions & 10 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h"

#if defined(EMSCRIPTEN)
#include "../save/value.h"

#include <emscripten.h>
#endif

Expand Down Expand Up @@ -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){
Expand All @@ -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()",
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion vm/std/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions web.mak
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
"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",

"build": "npm run make && npm run vite",
"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"
Expand Down
10 changes: 10 additions & 0 deletions web/src/lib/lua.js
Original file line number Diff line number Diff line change
@@ -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();
},
Expand All @@ -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);
};
12 changes: 12 additions & 0 deletions web/src/lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ 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,
wait: wait,
inbuf: inbuf,
want: want,
has: has,
sync: sync,
});
break;
}
Expand All @@ -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;
}
}
};
});
Expand Down
8 changes: 7 additions & 1 deletion web/src/lib/wlua.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import {run} from './lua.js';

let sync = null;
let has;
let want;
let inbuf;
Expand Down Expand Up @@ -48,21 +49,26 @@ const comp = (input) => {
};

const onArgs = (args) => {
run(args, {stdin, stdout, stderr, comp});
run(args, {stdin, stdout, stderr, comp, sync});
self.postMessage({
type: 'exit-ok',
});
};

self.onmessage = ({data}) => {
switch(data.type) {
case 'sync': {
sync = data.buf;
break;
}
case 'buffer': {
self.postMessage({type: 'get-args'});
wait = data.wait;
ret = data.ret;
has = data.has;
want = data.want;
inbuf = data.inbuf;
sync = data.sync;
break;
}
case 'args': {
Expand Down

0 comments on commit a934455

Please sign in to comment.