Skip to content

Commit

Permalink
workers
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Feb 11, 2024
1 parent 51db654 commit 53b0999
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 90 deletions.
12 changes: 0 additions & 12 deletions emcc.mak

This file was deleted.

8 changes: 6 additions & 2 deletions vm/backend/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ EM_ASYNC_JS(void, vm_compile_c_to_wasm, (int n), {
// })
// });

#if 1
#if 0
void vm_compile_c_to_wasm(int n);
#endif

#if 0
void vm_compile_c_to_wasm(int n);
#endif

#if 1
EM_JS(void, vm_compile_c_to_wasm, (int n), {
window.vm_compile_c_to_wasm(n);
globalThis.vm_compile_c_to_wasm(n);
});
#endif

Expand Down
6 changes: 5 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/preset-env": "^7.23.9",
"babel-loader": "^9.1.3",
"compression-webpack-plugin": "^11.0.0",
Expand All @@ -15,7 +18,8 @@
"webpack-node": "^0.0.0",
"worker-loader": "^3.0.8",
"xterm": "^5.3.0",
"xterm-addon-fit": "^0.8.0"
"xterm-addon-fit": "^0.8.0",
"xterm-readline": "^1.1.1"
},
"type": "module"
}
99 changes: 88 additions & 11 deletions web/src/app/App.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,105 @@

<script lang="ts">
<script lang="js">
import 'xterm/css/xterm.css';
import { onMount } from 'svelte';
import * as xterm from 'xterm';
import * as fit from 'xterm-addon-fit';
import { Readline } from 'xterm-readline';
let terminalElement;
let terminalController;
let termFit;
$: {
if (terminalController) {
// ...
const thens = [];
const comp = new Worker(new URL('../lib/wcomp.js', import.meta.url));
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],
});
break;
}
}
};
});
}
}
const initalizeXterm = () => {
};
const initalizeXterm = async() => {
const rl = new Readline();
terminalController = new xterm.Terminal();
termFit = new fit.FitAddon();
terminalController.loadAddon(rl);
terminalController.loadAddon(termFit);
terminalController.open(terminalElement);
termFit.fit();
terminalController.write('I am a terminal!');
terminalController.onData((e) => {
console.log(e);
})
await new Promise((ok, err) => {
comp.onmessage = ({data}) => {
switch (data.type) {
case 'result': {
thens[data.number](data.output);
break;
}
case 'ready': {
ok();
break;
}
}
};
});
loop(rl);
};
onMount(async () => {
Expand Down
1 change: 1 addition & 0 deletions web/src/app/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ html {
body {
margin: 0;
height: 100%;
background-color: black;
}
18 changes: 18 additions & 0 deletions web/src/lib/comp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Emception from "./emception.js";

const emception = new Emception();

await emception.init();

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

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}`)
}
return emception.fileSystem.readFile(`/working/out${comps}.wasm`);
}
48 changes: 21 additions & 27 deletions web/src/lib/lua.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@

import Module from '../../../build/bin/minivm.js';
import Emception from "./emception.js";
import wasmBinary from '../../../build/bin/minivm.wasm';

const wasmBuffer = await (await fetch(wasmBinary)).arrayBuffer();

const emception = new Emception();
await emception.init();

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

// emception.onstdout = (str) => console.log(str);
// emception.onstderr = (str) => console.error(str);
export const run = (...args) => {
let comps = 0;

export const run = (args, opts) => {
const stdinFunc = () => {
return null;
};

let stdout = '';
const stdoutFunc = (c) => {
stdout += String.fromCharCode(c);
if (c == 10) {
opts.stdout(stdout);
stdout = '';
} else {
stdout += String.fromCharCode(c);
}
};

let stderr = '';
const stderrFunc = (c) => {
stderr += String.fromCharCode(c);
if (c == 10) {
opts.stdout(stdout);
stderr = '';
} else {
stderr += String.fromCharCode(c);
}
};

const comp = (n) => {
const cBuf = new TextDecoder().decode(mod.FS.readFile(`/in${n}.c`));
const soBuf = opts.comp(cBuf);
mod.FS.writeFile(`/out${n}.so`, soBuf);
};

globalThis.vm_compile_c_to_wasm = comp;

const mod = Module({
noInitialRun: true,
Expand All @@ -37,19 +45,5 @@ export const run = (...args) => {
},
});

mod._vm_compile_c_to_wasm = (n) => {
comps += 1;
const cBuf = new TextDecoder().decode(mod.FS.readFile(`/in${n}.c`));
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 soBuf = emception.fileSystem.readFile(`/working/out${comps}.wasm`);
mod.FS.writeFile(`/out${n}.so`, soBuf);
};

mod.callMain(args);

return { stdout, stderr };
};
2 changes: 1 addition & 1 deletion web/src/lib/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const spawn = (...args) => new Promise((ok, err) => {
break;
}
}
}
};
});

export const run = (lua) => {
Expand Down
17 changes: 17 additions & 0 deletions web/src/lib/wcomp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import { comp } from './comp.js';

self.onmessage = ({data}) => {
switch (data.type) {
case 'comp': {
const output = comp(data.input);
self.postMessage({
type: 'result',
output: output,
number: data.number,
});
}
}
};

self.postMessage({type: 'ready'});
58 changes: 58 additions & 0 deletions web/src/lib/wlua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

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

const stdout = (msg) => {
self.postMessage({
type: 'stdout',
stdout: msg,
});
};

const stderr = (msg) => {
self.postMessage({
type: 'stderr',
stderr: msg,
});
};

let wait;
let ret;
const comp = (input) => {
self.postMessage({
type: 'comp',
input: input,
});
new Int32Array(wait)[0] = 0;
Atomics.wait(new Int32Array(wait), 0, 0);
const len = new Int32Array(wait)[0];
const old = new Uint8Array(ret);
const tmp = new Uint8Array(len);
for (let i = 0; i < len; i++) {
tmp[i] = old[i];
}
return tmp;
};

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

self.onmessage = ({data}) => {
switch(data.type) {
case 'buffer': {
self.postMessage({type: 'get-args'});
wait = data.wait;
ret = data.ret;
break;
}
case 'args': {
onArgs(data.args);
break;
}
}
};

self.postMessage({type: 'get-buffer'});
31 changes: 0 additions & 31 deletions web/src/lib/worker.js

This file was deleted.

Loading

0 comments on commit 53b0999

Please sign in to comment.