diff --git a/example/src/editor.ts b/example/src/editor.ts index 5cd17b7..a26f1d8 100644 --- a/example/src/editor.ts +++ b/example/src/editor.ts @@ -1,4 +1,4 @@ -export function setupEditor(input: HTMLTextAreaElement) { +export function setupEditor(input: HTMLTextAreaElement, runButton: HTMLButtonElement) { input.addEventListener("keydown", (e) => { switch (e.key) { case "Tab": { @@ -8,6 +8,7 @@ export function setupEditor(input: HTMLTextAreaElement) { input.setRangeText(" ", start, end, "end"); break; } + case "Backspace": { let [start, end] = [input.selectionStart, input.selectionEnd]; if (start !== end) { @@ -22,6 +23,20 @@ export function setupEditor(input: HTMLTextAreaElement) { } start >= 0 && input.setRangeText("", start, end, "end"); input.dispatchEvent(new Event("input")) + break; + } + + case "Enter": { + e.preventDefault(); + e.stopPropagation(); + if (e.ctrlKey) { + runButton.click(); + } else { + // TODO: Add indentation based on previous line + input.setRangeText("\n" + " ".repeat(0), input.selectionStart, input.selectionEnd, "end"); + } + input.dispatchEvent(new Event("input")); + break; } } }) diff --git a/example/src/interpreter/index.ts b/example/src/interpreter/index.ts index 7b63da8..f11210c 100644 --- a/example/src/interpreter/index.ts +++ b/example/src/interpreter/index.ts @@ -57,11 +57,11 @@ export class Interpreter { * * @param code Code that will be interpreted. */ - public run(code: string) { + public run(code: string, printLast: boolean = false) { for (const ev of this.onrun) { ev() } - this.worker.postMessage(code); + this.worker.postMessage({code, printLast}); } /** diff --git a/example/src/interpreter/interpreter.ts b/example/src/interpreter/interpreter.ts index 1286aca..22791b3 100644 --- a/example/src/interpreter/interpreter.ts +++ b/example/src/interpreter/interpreter.ts @@ -73,13 +73,17 @@ class Interpreter { this.next_thread_id = 1; } - async run(code: string): Promise { + async run(code: string, printLast: boolean = false): Promise { this.stdin.clear(); this.stdout.clear(); this.stderr.clear(); // Set the contents of the `main.rs` file to the new code - this.fds[5].dir.get_file("main.rs")!.data = new TextEncoder().encode(`fn main() {\n${code}\n}`); + code = `let _code = (|| {\n${code}\n})();`; + if (printLast) { + code += '\nif std::any::Any::type_id(&_code) != std::any::TypeId::of::<()>() { println!("{_code:?}") }'; + } + this.fds[5].dir.get_file("main.rs")!.data = encode(`fn main() {\n${code}\n}`); // Instantiate Miri const inst = await WebAssembly.instantiate(this.miri, { @@ -219,4 +223,12 @@ async function buildSysroot(): Promise { ])], ])], ]) +} + +function encode(text: string): Uint8Array { + return new TextEncoder().encode(text); +} + +function file(name: string, text: string): [string, File] { + return [name, new File(encode(text))] } \ No newline at end of file diff --git a/example/src/interpreter/worker.ts b/example/src/interpreter/worker.ts index 1dd4e41..566a64c 100644 --- a/example/src/interpreter/worker.ts +++ b/example/src/interpreter/worker.ts @@ -6,8 +6,11 @@ import { initInterpreter } from "./interpreter"; // When code is received run it addEventListener("message", async (event) => { - const code = event.data; - const result = await interpreter.run(code); + const { + code, + printLast + } = event.data; + const result = await interpreter.run(code, printLast); postMessage({ result }); }); diff --git a/example/src/pages/index.astro b/example/src/pages/index.astro index 68311ef..fd4acc3 100644 --- a/example/src/pages/index.astro +++ b/example/src/pages/index.astro @@ -20,6 +20,8 @@ const base = import.meta.env.BASE_URL === "/" ? "" : import.meta.env.BASE_URL;
+ +