Skip to content

Commit

Permalink
new svelte, abstrace repl
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 5ddaf98 commit 4667976
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 245 deletions.
2 changes: 1 addition & 1 deletion vm/backend/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void *vm_cache_comp(const char *comp, const char **srcs, const char *entry) {
remove(c_file);
remove(so_file);
// dlclose(handle);
// printf("%p\n", sym);
// printf("<raw function: %p>\n", sym);
return sym;
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions vm/backend/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void vm_tb_func_body_once_as(vm_tb_state_t *state, TB_Node **regs, vm_block_t *b
vm_tb_inst_call(
state,
proto,
tb_inst_get_symbol_address(state->fun, state->vm_table_set),
tb_inst_get_symbol_address(state->fun, state->vm_table_iset),
5,
args
);
Expand Down Expand Up @@ -1432,12 +1432,12 @@ void vm_tb_new_module(vm_tb_state_t *state) {

state->vm_tb_rfunc_comp = tb_extern_create(mod, -1, "vm_tb_rfunc_comp", TB_EXTERNAL_SO_LOCAL);
state->vm_table_new = tb_extern_create(mod, -1, "vm_table_new", TB_EXTERNAL_SO_LOCAL);
state->vm_table_set = tb_extern_create(mod, -1, "vm_table_set", TB_EXTERNAL_SO_LOCAL);
state->vm_table_iset = tb_extern_create(mod, -1, "vm_table_iset", TB_EXTERNAL_SO_LOCAL);
state->vm_table_get_pair = tb_extern_create(mod, -1, "vm_table_get_pair", TB_EXTERNAL_SO_LOCAL);
state->vm_tb_print = tb_extern_create(mod, -1, "vm_tb_print", TB_EXTERNAL_SO_LOCAL);
tb_symbol_bind_ptr(state->vm_tb_rfunc_comp, (void *)&vm_tb_rfunc_comp);
tb_symbol_bind_ptr(state->vm_table_new, (void *)&vm_table_new);
tb_symbol_bind_ptr(state->vm_table_set, (void *)&vm_table_set);
tb_symbol_bind_ptr(state->vm_table_iset, (void *)&vm_table_iset);
tb_symbol_bind_ptr(state->vm_table_get_pair, (void *)&vm_table_get_pair);
tb_symbol_bind_ptr(state->vm_tb_print, (void *)&vm_tb_print);

Expand Down
2 changes: 1 addition & 1 deletion vm/backend/tb.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct vm_tb_state_t {
// externals
void *vm_tb_rfunc_comp;
void *vm_table_new;
void *vm_table_set;
void *vm_table_iset;
void *vm_table_get_pair;
void *vm_tb_print;
void *std;
Expand Down
9 changes: 6 additions & 3 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
.std = std,
};

#if defined(EMSCRIPTEN)
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
#endif

while (true) {
#if !defined(EMSCRIPTEN)
char *input = ic_readline_ex(
Expand All @@ -252,9 +258,6 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
break;
}
#else
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
printf("lua> ");
char input[256];
size_t head = 0;
Expand Down
4 changes: 4 additions & 0 deletions vm/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ vm_pair_t *vm_table_lookup(vm_table_t *table, vm_value_t key_val, uint32_t key_t
return NULL;
}

void vm_table_iset(vm_table_t *restrict table, uint64_t key_ival, uint64_t val_ival, uint32_t key_tag, uint32_t val_tag) {
vm_table_set(table, *(vm_value_t *)&key_ival, *(vm_value_t *)&val_ival, key_tag, val_tag);
}

void vm_table_set(vm_table_t *restrict table, vm_value_t key_val, vm_value_t val_val, uint32_t key_tag, uint32_t val_tag) {
if (table->alloc == 0) {
return;
Expand Down
1 change: 1 addition & 0 deletions vm/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int64_t vm_value_to_i64(vm_std_value_t arg);
void vm_free_table(vm_table_t *table);
vm_table_t *vm_table_new(void);
vm_pair_t *vm_table_lookup(vm_table_t *table, vm_value_t key_val, uint32_t key_tag);
void vm_table_iset(vm_table_t *table, uint64_t key_ival, uint64_t val_ival, uint32_t key_tag, uint32_t val_tag);
void vm_table_set(vm_table_t *table, vm_value_t key_val, vm_value_t val_val, uint32_t key_tag, uint32_t val_tag);
void vm_table_set_pair(vm_table_t *table, vm_pair_t *pair);
void vm_table_get_pair(vm_table_t *table, vm_pair_t *pair);
Expand Down
2 changes: 1 addition & 1 deletion vm/std/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_
}
case VM_TAG_CLOSURE: {
vm_indent(out, indent, prefix);
vm_io_buffer_format(out, "<closure: %p>\n", value.value.all);
vm_io_buffer_format(out, "<function: %p>\n", value.value.all);
break;
}
case VM_TAG_FUN: {
Expand Down
5 changes: 2 additions & 3 deletions web.mak
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ EXE ?= .js
CC = emcc
TCC_SRCS =

GLOBAL := -s EXPORT_ALL=1
CFLAGS := -fPIC -DNDEBUG $(GLOBLAL) $(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']" $(GLOBLAL) $(LDFLAGS)
CFLAGS := -fPIC -DNDEBUG $(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)

include makefile

7 changes: 1 addition & 6 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"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",
"bash-parser": "^0.5.0",
"compression-webpack-plugin": "^11.0.0",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^6.10.0",
Expand All @@ -20,8 +16,7 @@
"webpack-node": "^0.0.0",
"worker-loader": "^3.0.8",
"xterm": "^5.3.0",
"xterm-addon-fit": "^0.8.0",
"xterm-readline": "^1.1.1"
"xterm-addon-fit": "^0.8.0"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def end_headers(self):


if __name__ == "__main__":
test(CORSRequestHandler, HTTPServer, port=8000)
test(CORSRequestHandler, HTTPServer, port=8080)
181 changes: 5 additions & 176 deletions web/src/app/App.svelte
Original file line number Diff line number Diff line change
@@ -1,178 +1,7 @@
<script lang="js">
import 'xterm/css/xterm.css';
import { onMount } from 'svelte';
import * as xterm from 'xterm';
import * as fit from 'xterm-addon-fit';
let terminalElement;
let terminalController;
let termFit;
const thens = [];
const comp = new Worker(new URL('../lib/wcomp.js', import.meta.url));
const unmap = (c) => {
if (c === '\n') {
return '\r\n';
} else {
return c;
}
};
const lua = (args) => new Promise(async (ok, err) => {
const inexists = new SharedArrayBuffer(4);
const inbuf = new SharedArrayBuffer(4);
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 'got-stdin': {
Atomics.notify()
break;
}
case 'stdout': {
terminalController.write(unmap(data.stdout));
break;
}
case 'stderr': {
terminalController.write(unmap(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,
inbuf: inbuf,
});
break;
}
case 'get-args': {
worker.postMessage({
type: 'args',
args: args,
});
break;
}
}
};
const inArray = [];
const i32buf = new Int32Array(inbuf);
const i32exists = new Int32Array(inexists);
terminalController.onData((data) => {
for (const c of data) {
inArray.push(c.charCodeAt(0));
Atomics.notify(i32exists, 0, 1);
}
});
while (true) {
if (inArray.length === 0) {
await Atomics.waitAsync(i32exists, 0, 0).value;
}
i32buf[0] = inArray.shift();
Atomics.notify(i32buf, 0, 1);
await Atomics.waitAsync(i32buf, 0, i32buf[0]).value;
}
});
const loop = async () => {
await lua(['--repl']);
// while (true) {
// 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;
// }
// }
// }
// }
// }
};
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();
await new Promise((ok, err) => {
comp.onmessage = ({data}) => {
switch (data.type) {
case 'result': {
thens[data.number](data.output);
break;
}
case 'ready': {
ok();
break;
}
}
};
});
loop();
};
onMount(async () => {
initalizeXterm();
setInterval(handleTermResize, 500);
});
const handleTermResize = () => {
if (termFit) {
termFit.fit();
}
};
<script>
import New from "./New.svelte";
import Term from "./Term.svelte";
</script>

<style>
.terminal {
height: 100%;
}
</style>
<div
class="terminal"
bind:this="{terminalElement}"
/>
<!-- <Term/> -->
<New/>
67 changes: 67 additions & 0 deletions web/src/app/New.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<script>
import { onMount } from "svelte";
import { repl } from '../lib/repl.js';
let data = '';
let src = '';
const obj = repl({
putchar: (str) => {
data += str;
}
});
const key = (event) => {
if (event.key === 'Enter') {
obj.input(src + '\n');
src = '';
}
};
onMount(() => {
obj.start();
});
</script>

<style>
input {
width: 100%;
font-family: monospace;
padding: 1em;
border: 0;
border-color: transparent;
outline: none;
background-color: black;
color: greenyellow;
user-select: none;
}
textarea {
width: 100%;
height: 100%;
resize: none;
background-color: black;
color: greenyellow;
border: 0;
padding: 1em;
padding-top: 0em;
outline: none;
height: min-content;
flex-grow: 1;
}
.term {
display: flex;
flex-direction: column;
background-color: #111;
width: 100%;
padding: 0em;
border: 0em;
height: 100%;
}
</style>

<div class="term">
<input type="text" on:keypress={key} bind:value={src}/>
<textarea readonly>{data}</textarea>
</div>
Loading

0 comments on commit 4667976

Please sign in to comment.