Skip to content

Commit

Permalink
faster comp, but broken app.svelte
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Feb 12, 2024
1 parent c3cab09 commit 27402f9
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 154 deletions.
14 changes: 6 additions & 8 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ EMSCRIPTEN_KEEPALIVE vm_main_t *vm_main_new(void) {
return ret;
}

EMSCRIPTEN_KEEPALIVE const char *vm_main_lua_eval(vm_main_t *main, const char *src) {
EMSCRIPTEN_KEEPALIVE void vm_main_lua_eval(vm_main_t *main, const char *src) {
vm_ast_node_t node = vm_lang_lua_parse(&main->config, src);
vm_ast_comp_more(node, &main->blocks);
vm_std_value_t value = vm_tb_run_main(&main->config, main->blocks.entry, &main->blocks, main->std);
vm_io_buffer_t buf = {0};
vm_io_debug(&buf, 0, "", value, NULL);
return buf.buf;
if (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);
}
}

#endif
Expand All @@ -59,11 +61,7 @@ int main(int argc, char **argv) {
vm_blocks_t *blocks = &val_blocks;
vm_config_t *config = &val_config;
bool echo = false;
#if defined(EMSCRIPTEN)
bool isrepl = false;
#else
bool isrepl = true;
#endif
vm_table_t *std = vm_std_new();
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
Expand Down
2 changes: 2 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/preset-env": "^7.23.9",
"babel-loader": "^9.1.3",
"bash-parser": "^0.5.0",
"compression-webpack-plugin": "^11.0.0",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^6.10.0",
"emception": "^1.0.11",
"fengari-web": "^0.1.4",
"mini-css-extract-plugin": "^2.8.0",
"svelte": "^4.2.10",
"svelte-loader": "^3.1.9",
Expand Down
128 changes: 74 additions & 54 deletions web/src/app/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as xterm from 'xterm';
import * as fit from 'xterm-addon-fit';
import { Readline } from 'xterm-readline';
import parse from 'bash-parser';
let terminalElement;
let terminalController;
Expand All @@ -12,65 +13,84 @@
const thens = [];
const comp = new Worker(new URL('../lib/wcomp.js', import.meta.url));
const lua = (args) => new Promise((ok, err) => {
const wait = new SharedArrayBuffer(4);
const ret = new SharedArrayBuffer(65536);
const worker = new Worker(new URL('../lib/wlua.js', import.meta.url));
const number = thens.length;
thens.push((buf) => {
const len = buf.byteLength;
new Uint8Array(ret).set(new Uint8Array(buf));
const w32 = new Int32Array(wait);
w32[0] = len
Atomics.notify(w32, 0, 1);
});
worker.onmessage = async ({data}) => {
switch (data.type) {
case 'stdout': {
rl.println(data.stdout);
break;
}
case 'stderr': {
rl.println(data.stdout);
break;
}
case 'exit-err': {
err();
break;
}
case 'exit-ok': {
ok();
break;
}
case 'comp': {
comp.postMessage({
type: 'comp',
number: number,
input: data.input,
});
break;
}
case 'get-buffer': {
worker.postMessage({
type: 'buffer',
ret: ret,
wait: wait,
});
break;
}
case 'get-args': {
worker.postMessage({
type: 'args',
args: args,
});
break;
}
}
};
});
const loop = async (rl) => {
while (true) {
const res = await rl.read('lua> ');
await new Promise((ok, err) => {
const wait = new SharedArrayBuffer(4);
const ret = new SharedArrayBuffer(65536);
const worker = new Worker(new URL('../lib/wlua.js', import.meta.url));
const number = thens.length;
thens.push((buf) => {
const len = buf.byteLength;
new Uint8Array(ret).set(new Uint8Array(buf));
const w32 = new Int32Array(wait);
w32[0] = len
Atomics.notify(w32, 0, 1);
});
worker.onmessage = async ({data}) => {
switch (data.type) {
case 'stdout': {
rl.println(data.stdout);
break;
}
case 'stderr': {
rl.println(data.stdout);
break;
}
case 'exit-err': {
err();
break;
}
case 'exit-ok': {
ok();
break;
}
case 'comp': {
comp.postMessage({
type: 'comp',
number: number,
input: data.input,
});
break;
}
case 'get-buffer': {
worker.postMessage({
type: 'buffer',
ret: ret,
wait: wait,
});
break;
}
case 'get-args': {
worker.postMessage({
type: 'args',
args: ['-e', res],
});
const res = await rl.read('$ ');
for (const {type, name, suffix} of parse(res).commands) {
if (type === 'SimpleCommand') {
switch (name) {
case 'lua': {
const args = [];
for (const obj of suffix) {
if (obj.type == 'Word') {
args.push(obj.text);
} else {
console.log(obj);
}
}
await lua(args);
break;
}
}
};
});
}
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<meta charset="utf-8">
<title>MiniVM</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="minivm.css">
</head>

<body>
<script type="module" src="index.js"></script>
<script type="module" src="minivm.js"></script>
</body>

</html>
22 changes: 22 additions & 0 deletions web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
// stdout;
// };

import {lua} from './lib/spawn.js';


import {load} from 'fengari-web';


import './app/global.css';
import App from './app/App.svelte';

new App({
target: document.body,
});

const fengari = (str) => {
load(str)();
};

window.minivm = lua;
window.fengari = fengari;
window.bench = (func, ...args) => {
console.time(func.name);
const ret = func(...args);
console.timeEnd(func.name);
return ret;
};
console.log('window.{minvim, fengari, time} available');

minivm('return print');
23 changes: 18 additions & 5 deletions web/src/lib/comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@ const emception = new Emception();

await emception.init();

emception.run('emcc --check -Wno-version-check');
emception.onstdout = (s) => console.log(s);
emception.onstderr = (s) => console.error(s);

// emception.run('emcc --check -Wno-version-check');

let flags1 = '-target wasm32-unknown-emscripten -fignore-exceptions -fPIC -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -Wno-incompatible-library-redeclaration -Wno-parentheses-equality'
let flags2 = '-L/lazy/emscripten/cache/sysroot/lib/wasm32-emscripten/pic --no-whole-archive -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --strip-debug --export-dynamic --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=__wasm_apply_data_relocs --export=__wasm_call_ctors --experimental-pic -shared'

let comps = 0;
export const comp = (cBuf) => {
comps += 1;
emception.fileSystem.writeFile(`/working/in${comps}.c`, cBuf);
const result = emception.run(`emcc -O3 -s EXPORT_ALL=1 -s SIDE_MODULE=1 /working/in${comps}.c -o /working/out${comps}.wasm -Wno-version-check -Wno-incompatible-library-redeclaration -Wno-parentheses-equality`);
if (result.returncode !== 0) {
console.error(`emcc exited with code ${result.returncode}`)
const result1 = emception.runx(`/usr/bin/clang -c ${flags1} /working/in${comps}.c -o /working/mid${comps}.o`);
if (result1.returncode !== 0) {
console.error(`clang exited with code ${result1.returncode}`);
console.log(result1);
}
const result2 = emception.runx(`/usr/bin/wasm-ld --whole-archive /working/mid${comps}.o ${flags2} -o /working/out${comps}.wasm`);
if (result2.returncode !== 0) {
console.error(`wasm-ld exited with code ${result2.returncode}`)
}
return emception.fileSystem.readFile(`/working/out${comps}.wasm`);
}
};

comp('int main() {}');
71 changes: 23 additions & 48 deletions web/src/lib/emception.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
import FileSystem from "emception/FileSystem.mjs";

import LlvmBoxProcess from "emception/LlvmBoxProcess.mjs";
import BinaryenBoxProcess from "emception/BinaryenBoxProcess.mjs";
import Python3Process from "emception/Python3Process.mjs";
import NodeProcess from "emception/QuickNodeProcess.mjs";

import packs from "emception/packs.mjs";
import wasm from "emception/packages/wasm.pack.br";

const packs = {
"wasm": wasm,
};

const tools_info = {
"/usr/bin/clang": "llvm-box",
"/usr/bin/clang++": "llvm-box",
"/usr/bin/llc": "llvm-box",
"/usr/bin/lld": "llvm-box",
"/usr/bin/llvm-ar": "llvm-box",
"/usr/bin/llvm-nm": "llvm-box",
"/usr/bin/llvm-objcopy": "llvm-box",
"/usr/bin/wasm-ld": "llvm-box",
"/usr/bin/node": "node",
"/usr/bin/python": "python",
"/usr/bin/wasm-as": "binaryen-box",
"/usr/bin/wasm2js": "binaryen-box",
"/usr/bin/wasm-ctor-eval": "binaryen-box",
"/usr/bin/wasm-emscripten-finalize": "binaryen-box",
"/usr/bin/wasm-metadce": "binaryen-box",
"/usr/bin/wasm-opt": "binaryen-box",
"/usr/bin/wasm-shell": "binaryen-box",
};

// packages needed for the startup example
const preloads = [
"cpython",
"emscripten",
"emscripten_node_modules",
"emscripten_sysroot_lib_wasm32-emscripten_libGL.a",
"emscripten_sysroot_lib_wasm32-emscripten_libal.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc++.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc++abi.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc.a",
"emscripten_sysroot_lib_wasm32-emscripten_libcompiler_rt.a",
"emscripten_sysroot_lib_wasm32-emscripten_libdlmalloc.a",
"emscripten_sysroot_lib_wasm32-emscripten_libhtml5.a",
"emscripten_sysroot_lib_wasm32-emscripten_libsockets.a",
"emscripten_sysroot_lib_wasm32-emscripten_libstubs.a",
"emscripten_system_include",
"emscripten_system_include_SDL",
"emscripten_system_include_compat",
"emscripten_system_lib_compiler-rt_lib",
"emscripten_system_lib_libcxx_include",
"emscripten_third_party",
"wasm"
];
const preloads = ["wasm"];

class Emception {
fileSystem = null;
Expand Down Expand Up @@ -88,13 +54,6 @@ class Emception {

const tools = {
"llvm-box": new LlvmBoxProcess(processConfig),
"binaryen-box": new BinaryenBoxProcess(processConfig),
"node": new NodeProcess(processConfig),
"python": [
new Python3Process(processConfig),
new Python3Process(processConfig),
new Python3Process(processConfig),
],
};
this.tools = tools;

Expand Down Expand Up @@ -124,6 +83,19 @@ class Emception {
});
};

runx(...args) {
if (this.fileSystem.exists("/emscripten/cache/cache.lock")) {
this.fileSystem.unlink("/emscripten/cache/cache.lock");
}
if (args.length == 1) args = args[0].split(/ +/);
return this._run_process_impl(args, {
print: (...args) => this.onstdout(...args),
printErr: (...args) => this.onstderr(...args),
cwd: "/working",
path: ["/emscripten"],
});
}

_run_process(argv, opts = {}) {
this.onprocessstart(argv);
const result = this._run_process_impl(argv, opts);
Expand Down Expand Up @@ -157,7 +129,10 @@ class Emception {
const result = tool.exec(argv, {
...opts,
cwd: opts.cwd || "/",
path: ["/emscripten"]
path: ["/emscripten"],
preRun: (mod) => {
console.log(mod);
},
});

this.fileSystem.push();
Expand Down
Loading

0 comments on commit 27402f9

Please sign in to comment.