mirror of
https://github.com/pyscript/pyscript.git
synced 2026-03-31 17:03:24 -04:00
Worker sync utility (#1511)
* patched an issue with wasmoon randomly asking to resolve proxy references * simplified pyodide and micropython dance by grouping their common utilities together * created an integration test around a worker to main thread input between MicroPython and Lua * commented some weird bugs / funny behaviors around both MicroPython and Pyodide * other minor clean ups
This commit is contained in:
committed by
GitHub
parent
0cdbfbeb30
commit
af72e232c3
27
pyscript.core/esm/runtime/_python.js
Normal file
27
pyscript.core/esm/runtime/_python.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { clean, writeFile as writeFileUtil } from "./_utils.js";
|
||||
|
||||
// REQUIRES INTEGRATION TEST
|
||||
/* c8 ignore start */
|
||||
export const run = (runtime, code) => runtime.runPython(clean(code));
|
||||
|
||||
export const runAsync = (runtime, code) => runtime.runPythonAsync(clean(code));
|
||||
|
||||
export function runEvent(runtime, code, key) {
|
||||
code = `import js;event=js.__events.get(${key});${code}`;
|
||||
return this.run(runtime, code);
|
||||
}
|
||||
|
||||
const worker = (method) =>
|
||||
function (runtime, code, xworker) {
|
||||
code = `from js import xworker;${code}`;
|
||||
globalThis.xworker = xworker;
|
||||
return this[method](runtime, code);
|
||||
};
|
||||
|
||||
export const runWorker = worker("run");
|
||||
|
||||
export const runWorkerAsync = worker("runAsync");
|
||||
|
||||
export const writeFile = ({ FS }, path, buffer) =>
|
||||
writeFileUtil(FS, path, buffer);
|
||||
/* c8 ignore stop */
|
||||
@@ -1,10 +1,26 @@
|
||||
import { getBuffer } from "../fetch-utils.js";
|
||||
import { absoluteURL } from "../utils.js";
|
||||
import { absoluteURL, defineProperty } from "../utils.js";
|
||||
import "@ungap/with-resolvers";
|
||||
|
||||
// REQUIRES INTEGRATION TEST
|
||||
/* c8 ignore start */
|
||||
// TODO: this should *NOT* be needed as the polyfill
|
||||
// already patches on demand the Promise object
|
||||
const { withResolvers } = Promise;
|
||||
defineProperty(globalThis, "Promise", {
|
||||
configurable: true,
|
||||
value: class extends Promise {
|
||||
withResolvers() {
|
||||
return withResolvers.call(this);
|
||||
}
|
||||
},
|
||||
});
|
||||
/* c8 ignore stop */
|
||||
|
||||
/**
|
||||
* Trim code only if it's a single line that prettier or other tools might have modified.
|
||||
* @param {string} code code that might be a single line
|
||||
* @returns {strong}
|
||||
* @returns {string}
|
||||
*/
|
||||
export const clean = (code) =>
|
||||
code.replace(/^[^\r\n]+$/, (line) => line.trim());
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { clean, fetchPaths, stdio, writeFile } from "./_utils.js";
|
||||
import { fetchPaths, stdio } from "./_utils.js";
|
||||
import {
|
||||
run,
|
||||
runAsync,
|
||||
runEvent,
|
||||
runWorker,
|
||||
runWorkerAsync,
|
||||
writeFile,
|
||||
} from "./_python.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`,
|
||||
@@ -20,16 +22,11 @@ export default {
|
||||
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),
|
||||
run,
|
||||
runAsync,
|
||||
runEvent,
|
||||
runWorker,
|
||||
runWorkerAsync,
|
||||
writeFile,
|
||||
};
|
||||
/* c8 ignore stop */
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { clean, fetchPaths, stdio, writeFile } from "./_utils.js";
|
||||
import { fetchPaths, stdio } from "./_utils.js";
|
||||
import {
|
||||
run,
|
||||
runAsync,
|
||||
runEvent,
|
||||
runWorker,
|
||||
runWorkerAsync,
|
||||
writeFile,
|
||||
} from "./_python.js";
|
||||
|
||||
const type = "pyodide";
|
||||
|
||||
// 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, "py"],
|
||||
module: (version = "0.22.1") =>
|
||||
@@ -26,16 +28,11 @@ export default {
|
||||
}
|
||||
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),
|
||||
run,
|
||||
runAsync,
|
||||
runEvent,
|
||||
runWorker,
|
||||
runWorkerAsync,
|
||||
writeFile,
|
||||
};
|
||||
/* c8 ignore stop */
|
||||
|
||||
Reference in New Issue
Block a user