From 7e1aa3840c1106534f3c5dd63e97dfad5f160413 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 31 Aug 2021 20:16:46 -0500 Subject: [PATCH] added basic standard input with "read_line" --- read_line.onyx | 16 ++++++++++++++++ serve.py | 12 ++++++++++-- static/css/index.css | 42 +++++++++++++++++++++++++++++++++++++++++- static/src/index.js | 44 +++++++++++++++++++++++++++++++++++++++++++- static/src/worker.js | 27 +++++++++++++++++++++++++++ templates/index.html | 2 ++ 6 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 read_line.onyx diff --git a/read_line.onyx b/read_line.onyx new file mode 100644 index 0000000..a972273 --- /dev/null +++ b/read_line.onyx @@ -0,0 +1,16 @@ +package builtin + +read_line :: () -> str { + #persist buffer: [1024] u8; + + (package core).__flush_stdio(); + + read_line_available :: () -> bool #foreign "host" "read_line_available" --- + read_line :: (buffer: [] u8) -> u32 #foreign "host" "read_line" --- + + read_line_available(); + length := read_line(~~buffer); + + return str.{ ~~buffer, length }; +} + diff --git a/serve.py b/serve.py index be64eb4..3d27024 100644 --- a/serve.py +++ b/serve.py @@ -8,9 +8,17 @@ app = Flask( app.config.from_object("config.default") +@app.after_request +def require_coep(response): + response.headers['Cross-Origin-Embedder-Policy'] = "require-corp" + return response + @app.route("/") def get_homepage(): - return flask.render_template("index.html") + response = flask.Response(flask.render_template("index.html")) + response.headers['Cross-Origin-Embedder-Policy'] = "require-corp" + response.headers['Cross-Origin-Opener-Policy'] = "same-origin" + return response @app.route("/compile", methods=['POST']) @@ -24,7 +32,7 @@ def post_compile_onyx(): with os.fdopen(code_file_desc, "w+b") as code_file: code_file.write(code.encode('utf-8')) - compile_process = subprocess.run(f"timeout -s KILL 10s onyx -r js --no-file-contents --no-colors -o {wasm_file_name} {code_file_name}", shell=True, capture_output=True) + compile_process = subprocess.run(f"timeout -s KILL 10s onyx -r js --no-file-contents --no-colors -o {wasm_file_name} {code_file_name} ./read_line.onyx", shell=True, capture_output=True) os.remove(code_file_name) diff --git a/static/css/index.css b/static/css/index.css index 3cb273b..b6401b6 100644 --- a/static/css/index.css +++ b/static/css/index.css @@ -1,5 +1,6 @@ :root { --menu-bar-height: 32px; + --input-bar-height: 32px; --divider-size: 4px; --left-half-width: 70%; } @@ -80,7 +81,7 @@ button:hover { #code-result { height: 100%; - max-height: calc(100% - 24px); /* subtract the padding */ + max-height: calc(100% - var(--menu-bar-height) - var(--input-bar-height)); /* subtract the padding */ background: #070707; color: #fff; font-size: 16px; @@ -105,3 +106,42 @@ button:hover { display: inline-block; float: left; } + +#input-bar { + background-color: #181818; + color: #fff; + outline: none; + border: none; + + min-height: var(--input-bar-height); + width: 80%; + padding: 8px; + float: left; + + font-size: 16px; + font-family: Monaco, Menlo, "Ubuntu Mono", Consolas, source-code-pro, monospace; +} + +#input-submit { + background-color: #272727; + color: #fff; + outline: none; + border: none; + + float: left; + width: 20%; + padding: 8px; + margin: 0; + min-height: var(--input-bar-height); + + font-size: 16px; + font-family: Monaco, Menlo, "Ubuntu Mono", Consolas, source-code-pro, monospace; + + transition: all 0.5s; +} + +#input-submit:hover { + background: #444; + cursor: pointer; +} + diff --git a/static/src/index.js b/static/src/index.js index 830352f..f5a85e7 100644 --- a/static/src/index.js +++ b/static/src/index.js @@ -2,6 +2,8 @@ let wasm_worker = null; let editor_keybind_mode = "normal"; let editor_theme = "chrome"; +let input_shared_buffer = new SharedArrayBuffer(1024 * Uint8Array.BYTES_PER_ELEMENT); + async function clear_output() { let elem = document.getElementById('code-result'); elem.className = ""; // NOTE: This clears all the classes, not just the "errored" one! @@ -21,6 +23,7 @@ async function run_wasm(wasm_bytes) { } update_running_msg(); + input_shared_buffer = new SharedArrayBuffer(1024 * Uint8Array.BYTES_PER_ELEMENT); wasm_worker = new Worker(window.ROOT_ENDPOINT + '/static/src/worker.js'); wasm_worker.onmessage = (e) => { @@ -40,10 +43,25 @@ async function run_wasm(wasm_bytes) { update_running_msg(); break; } + + case 'input': { + let input_array = new Uint8Array(input_shared_buffer); + Atomics.store(input_array, 0, 0); + let msg = prompt("Line: "); + input_array.fill(0); + + for (let i=0; i { let editor = ace.edit('code-editor'); @@ -208,4 +244,10 @@ window.onload = () => { make_resizer("horizontal-divider", "--left-half-width", (e) => { editor.resize(true); }); + + document.getElementById("input-bar").addEventListener("keyup", (ev) => { + if (ev.keyCode === 13) { + submit_input(); + } + }); }; diff --git a/static/src/worker.js b/static/src/worker.js index 716eaff..453e908 100644 --- a/static/src/worker.js +++ b/static/src/worker.js @@ -1,3 +1,4 @@ +let input_shared_buffer = null; let wasm_memory = null; let import_obj = { @@ -12,6 +13,27 @@ let import_obj = { return Date.now(); }, + read_line_available: async function() { + let input_array = new Uint8Array(input_shared_buffer); + while (Atomics.load(input_array, 0) == 0); + }, + + read_line(msgptr, msglen) { // Returns actual length + let data = new Uint8Array(wasm_memory.buffer, msgptr, msglen); + let input_array = new Uint8Array(input_shared_buffer); + console.log(Atomics.load(input_array, 0), " == 1"); + + let i; + for (i=0; input_array[i + 1] != 0 && i { diff --git a/templates/index.html b/templates/index.html index 83cb65a..e87824a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -54,6 +54,8 @@ main :: (args: [] cstr) {
             
+ +
-- 2.25.1