webassembly: Implement replInit() and replProcessChar().

This is the JavaScript API for starting and running a REPL.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2024-03-06 10:16:52 +11:00
parent 625b17a410
commit 6ff3e356e2
4 changed files with 38 additions and 135 deletions

View File

@@ -150,6 +150,27 @@ export async function loadMicroPython(options) {
);
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
},
replInit() {
Module.ccall("mp_js_repl_init", "null", ["null"]);
},
replProcessChar(chr) {
return Module.ccall(
"mp_js_repl_process_char",
"number",
["number"],
[chr],
);
},
// Needed if the GC/asyncify is enabled.
async replProcessCharWithAsyncify(chr) {
return Module.ccall(
"mp_js_repl_process_char",
"number",
["number"],
[chr],
{ async: true },
);
},
};
}
@@ -191,11 +212,11 @@ async function runCLI() {
});
if (repl) {
mp_js_init_repl();
mp.replInit();
process.stdin.setRawMode(true);
process.stdin.on("data", (data) => {
for (let i = 0; i < data.length; i++) {
mp_js_process_char(data[i]).then((result) => {
mp.replProcessCharWithAsyncify(data[i]).then((result) => {
if (result) {
process.exit();
}