| 1 | import createEmscriptenModule from "./python.mjs"; |
| 2 | |
| 3 | class StdinBuffer { |
| 4 | constructor() { |
| 5 | this.sab = new SharedArrayBuffer(128 * Int32Array.BYTES_PER_ELEMENT); |
| 6 | this.buffer = new Int32Array(this.sab); |
| 7 | this.readIndex = 1; |
| 8 | this.numberOfCharacters = 0; |
| 9 | this.sentNull = true; |
| 10 | } |
| 11 | |
| 12 | prompt() { |
| 13 | this.readIndex = 1; |
| 14 | Atomics.store(this.buffer, 0, -1); |
| 15 | postMessage({ |
| 16 | type: "stdin", |
| 17 | buffer: this.sab, |
| 18 | }); |
| 19 | Atomics.wait(this.buffer, 0, -1); |
| 20 | this.numberOfCharacters = this.buffer[0]; |
| 21 | } |
| 22 | |
| 23 | stdin = () => { |
| 24 | while (this.numberOfCharacters + 1 === this.readIndex) { |
| 25 | if (!this.sentNull) { |
| 26 | // Must return null once to indicate we're done for now. |
| 27 | this.sentNull = true; |
| 28 | return null; |
| 29 | } |
| 30 | this.sentNull = false; |
| 31 | // Prompt will reset this.readIndex to 1 |
| 32 | this.prompt(); |
| 33 | } |
| 34 | const char = this.buffer[this.readIndex]; |
| 35 | this.readIndex += 1; |
| 36 | return char; |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | const stdout = (charCode) => { |
| 41 | if (charCode) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…