Skip to content

Commit 9142384

Browse files
Merge pull request #91 from pyscript/better-micropython-stdio
Better MicroPython stdio for REPL
2 parents f02d14c + fa8a1a0 commit 9142384

File tree

10 files changed

+114
-76
lines changed

10 files changed

+114
-76
lines changed

docs/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-BVYJ4_a2.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-BVYJ4_a2.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CVv62MiA.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/zip-CVv62MiA.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

esm/interpreter/_io.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,25 @@ export const stdio = (init) => {
1717
},
1818
};
1919
};
20+
21+
const decoder = new TextDecoder();
22+
export const buffered = (callback, EOL = 10) => {
23+
const buffer = [];
24+
return (maybeUI8) => {
25+
if (maybeUI8 instanceof Uint8Array) {
26+
for (const c of maybeUI8) {
27+
if (c === EOL)
28+
callback(decoder.decode(new Uint8Array(buffer.splice(0))));
29+
else
30+
buffer.push(c);
31+
}
32+
}
33+
// if io.stderr(error) is passed instead
34+
// or any io.stdout("thing") this should
35+
// still work as expected
36+
else {
37+
callback(maybeUI8);
38+
}
39+
};
40+
};
2041
/* c8 ignore stop */

esm/interpreter/micropython.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// import fetch from '@webreflection/fetch';
22
import { fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
33
import { getFormat, registerJSModule, run, runAsync, runEvent } from './_python.js';
4-
import { stdio } from './_io.js';
4+
import { stdio, buffered } from './_io.js';
55
import mip from '../python/mip.js';
66
import zip from '../zip.js';
77

@@ -14,9 +14,12 @@ export default {
1414
module: (version = '1.22.0-272') =>
1515
`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${version}/micropython.mjs`,
1616
async engine({ loadMicroPython }, config, url) {
17-
const { stderr, stdout, get } = stdio();
17+
const { stderr, stdout, get } = stdio({
18+
stderr: buffered(console.error),
19+
stdout: buffered(console.log),
20+
});
1821
url = url.replace(/\.m?js$/, '.wasm');
19-
const interpreter = await get(loadMicroPython({ stderr, stdout, url }));
22+
const interpreter = await get(loadMicroPython({ linebuffer: false, stderr, stdout, url }));
2023
if (config.files) await fetchFiles(this, interpreter, config.files);
2124
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
2225
if (config.js_modules) await fetchJSModules(config.js_modules);

package-lock.json

Lines changed: 78 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
"@playwright/test": "^1.42.1",
4343
"@rollup/plugin-node-resolve": "^15.2.3",
4444
"@rollup/plugin-terser": "^0.4.4",
45-
"@zip.js/zip.js": "^2.7.40",
45+
"@zip.js/zip.js": "^2.7.41",
4646
"ascjs": "^6.0.3",
4747
"c8": "^9.1.0",
4848
"chokidar": "^3.6.0",
4949
"eslint": "^8.57.0",
5050
"linkedom": "^0.16.11",
51-
"rollup": "^4.13.1",
51+
"rollup": "^4.14.0",
5252
"static-handler": "^0.4.3",
5353
"typescript": "^5.4.3"
5454
},
@@ -89,6 +89,6 @@
8989
"to-json-callback": "^0.1.1"
9090
},
9191
"worker": {
92-
"blob": "sha256-sLy5/Xw+ZYT89h91afdza9NKwCEChCfAYUlvUl76emA="
92+
"blob": "sha256-16iVq8sMsDeuh75NeaBghJiiiri1XHV0F442mkx5LF0="
9393
}
9494
}

0 commit comments

Comments
 (0)