hack hack hack until pyterminal works also in workers

This commit is contained in:
Antonio Cuni
2023-09-29 19:09:20 +02:00
parent 1867b8b89f
commit eaf4da7c21
4 changed files with 18 additions and 41 deletions

View File

@@ -75,9 +75,19 @@ const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
interpreter.runPython(stdlib, { globals: interpreter.runPython("{}") });
};
// this is a super-ugly workaround for a polyscript issue: in theory I should
// define a codeBeforeRunWorker hook inside py-terminal.js, but since it's a
// lazy plugin, it is too late: https://github.com/pyscript/polyscript/issues/52
const py_terminal_codeBeforeRunner = `
from pyscript import pyterminal
pyterminal.init()
`;
const workerHooks = {
codeBeforeRunWorker: () =>
[stdlib, ...hooks.codeBeforeRunWorker].map(dedent).join("\n"),
[stdlib, py_terminal_codeBeforeRunner, ...hooks.codeBeforeRunWorker]
.map(dedent)
.join("\n"),
codeBeforeRunWorkerAsync: () =>
[stdlib, ...hooks.codeBeforeRunWorkerAsync].map(dedent).join("\n"),
codeAfterRunWorker: () =>
@@ -135,7 +145,8 @@ for (const [TYPE, interpreter] of TYPES) {
errors.set(element, error);
},
...workerHooks,
onWorkerReady(_, xworker) {
async onWorkerReady(_, xworker) {
await plugins; // workaround for a polyscript bug
assign(xworker.sync, sync);
for (const callback of hooks.onWorkerReady)
callback(_, xworker);