-
Notifications
You must be signed in to change notification settings - Fork 4
/
_io.js
44 lines (43 loc) · 1.35 KB
/
_io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const io = new WeakMap();
export const stdio = (init) => {
const context = init || console;
const localIO = {
// allow plugins or other io manipulating logic to reuse
// the buffered utility exposed in here (see py-editor)
buffered,
stderr: (context.stderr || console.error).bind(context),
stdout: (context.stdout || console.log).bind(context),
};
return {
stderr: (...args) => localIO.stderr(...args),
stdout: (...args) => localIO.stdout(...args),
async get(engine) {
const interpreter = await engine;
io.set(interpreter, localIO);
return interpreter;
},
};
};
const decoder = new TextDecoder();
export const buffered = (callback, EOL = 10) => {
const buffer = [];
return (maybeUI8) => {
if (maybeUI8 instanceof Uint8Array) {
for (const c of maybeUI8) {
if (c === EOL)
callback(decoder.decode(new Uint8Array(buffer.splice(0))));
else
buffer.push(c);
}
}
// if io.stderr(error) is passed instead
// or any io.stdout("thing") this should
// still work as expected
else {
callback(maybeUI8);
}
};
};
/* c8 ignore stop */