Skip to content

Commit

Permalink
Only run the main function once
Browse files Browse the repository at this point in the history
Also add a default factorial program
  • Loading branch information
cbebe committed Sep 24, 2024
1 parent 09aa3a7 commit 0f3e0ea
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/website/assets/js/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
const WASM_URL = "web-interpreter-opt.wasm";

class WebInterpreter extends HTMLElement {
/**
* @type {WebAssembly.WebAssemblyInstantiatedSource['instance']}
*/
#wasm;

/**
Expand Down Expand Up @@ -38,8 +41,7 @@ class WebInterpreter extends HTMLElement {
return text;
}

connectedCallback() {
// @ts-expect-error Go is defined in wasm_exec.js
#newRunner() {
const go = new Go();
go.importObject["main.go.printError"] = (addr, length) => {
console.error(this.logText(addr, length));
Expand All @@ -52,10 +54,32 @@ class WebInterpreter extends HTMLElement {
printOut: go.importObject["main.go.printOut"],
};

return go;
}

connectedCallback() {
this.querySelector("textarea.input").value = `><//> Calculate 5!
><number> ≈ ><{({°> ~ ><//> b101 = 5
><factorial> ≈ ><(({°> ~ ><//> b001 = 1
><//> while number > 1
><(((@> [><number> o~ ><(({°>]
><>
><//> factorial = factorial * number
><factorial> ≈ ><factorial> ♡ ><number> ~
><//> number -= 1
<number><< ~
<><
(+o ><factorial> ~ ><//> Should be 120
`
const go = this.#newRunner();
if ("instantiateStreaming" in WebAssembly) {
WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(
(obj) => {
this.#wasm = obj.instance;
go.run(this.#wasm);
}
);
} else {
Expand All @@ -64,12 +88,12 @@ class WebInterpreter extends HTMLElement {
.then((bytes) =>
WebAssembly.instantiate(bytes, go.importObject).then((obj) => {
this.#wasm = obj.instance;
go.run(this.#wasm);
})
);
}
this.querySelector("button.play")?.addEventListener("click", () => {
if (!this.#wasm) return;
go.run(this.#wasm);
// @ts-expect-error
const inputText = this.querySelector("textarea.input").value;
const [addr, len] = this.insertText(inputText);
Expand Down

0 comments on commit 0f3e0ea

Please sign in to comment.