MCPcopy Index your code
hub / github.com/python/cpython / StdinBuffer

Class StdinBuffer

Platforms/emscripten/web_example/python.worker.mjs:3–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import createEmscriptenModule from "./python.mjs";
2
3class 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
40const stdout = (charCode) => {
41 if (charCode) {

Callers

nothing calls this directly

Calls 1

promptMethod · 0.95

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…