mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
import { clean, fetchPaths, stdio, writeFile } from "./_utils.js";
|
|
|
|
const type = "micropython";
|
|
|
|
// REQUIRES INTEGRATION TEST
|
|
/* c8 ignore start */
|
|
const worker = (method) =>
|
|
function (runtime, code, xworker) {
|
|
globalThis.xworker = xworker;
|
|
return this[method](runtime, `from js import xworker;${code}`);
|
|
};
|
|
|
|
export default {
|
|
type: [type, "mpy"],
|
|
module: () => `http://localhost:8080/micropython/micropython.mjs`,
|
|
async engine({ loadMicroPython }, config, url) {
|
|
const { stderr, stdout, get } = stdio();
|
|
url = url.replace(/\.m?js$/, ".wasm");
|
|
const runtime = await get(loadMicroPython({ stderr, stdout, url }));
|
|
if (config.fetch) await fetchPaths(this, runtime, config.fetch);
|
|
return runtime;
|
|
},
|
|
run: (runtime, code) => runtime.runPython(clean(code)),
|
|
runAsync: (runtime, code) => runtime.runPythonAsync(clean(code)),
|
|
runEvent(runtime, code, key) {
|
|
return this.run(
|
|
runtime,
|
|
`import js;event=js.__events.get(${key});${code}`,
|
|
);
|
|
},
|
|
runWorker: worker("run"),
|
|
runWorkerAsync: worker("runAsync"),
|
|
writeFile: ({ FS }, path, buffer) => writeFile(FS, path, buffer),
|
|
};
|
|
/* c8 ignore stop */
|