Skip to content

Polyscript workers by name #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions esm/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { registry as defaultRegistry, prefixes, configs } from './interpreters.j
import { getRuntimeID } from './loader.js';
import { addAllListeners } from './listeners.js';
import { Hook, XWorker as XW } from './xworker.js';
import { workers, workersHandler } from './workers.js';
import { polluteJS, js as jsHooks, code as codeHooks } from './hooks.js';
import workerURL from './worker/url.js';

Expand Down Expand Up @@ -72,6 +73,8 @@ export const handleCustomType = async (node) => {
});
defineProperty(node, 'xworker', { value: xworker });
resolve({ type, xworker });
const workerName = node.getAttribute('name');
if (workerName) workers[workerName].resolve(xworker.ready);
return;
}
}
Expand Down Expand Up @@ -118,6 +121,7 @@ export const handleCustomType = async (node) => {
config: resolved.config,
currentScript: type.startsWith('_') ? null : node,
js_modules: JSModules,
workers: workersHandler,
});

// patch methods accordingly to hooks (and only if needed)
Expand Down
10 changes: 7 additions & 3 deletions esm/script-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from '@webreflection/fetch';
import { $ } from 'basic-devtools';

import { workers, workersHandler } from './workers.js';
import $xworker from './worker/class.js';
import workerURL from './worker/url.js';
import { getRuntime, getRuntimeID } from './loader.js';
Expand Down Expand Up @@ -65,6 +66,7 @@ const execute = async (currentScript, source, XWorker, isAsync) => {
XWorker,
currentScript,
js_modules: JSModules,
workers: workersHandler,
});
dispatch(currentScript, type, 'ready');
const result = module[isAsync ? 'runAsync' : 'run'](interpreter, content);
Expand Down Expand Up @@ -122,7 +124,7 @@ export const handle = async (script) => {
// allow a shared config among scripts, beside interpreter,
// and/or source code with different config or interpreter
const {
attributes: { async: isAsync, config, env, target, version },
attributes: { async: isAsync, config, env, name: wn, target, version },
src,
type,
} = script;
Expand All @@ -140,12 +142,14 @@ export const handle = async (script) => {
const xworker = new XWorker(url, {
...nodeInfo(script, type),
async: !!isAsync,
config: configValue
config: configValue,
});
handled.set(
defineProperty(script, 'xworker', { value: xworker }),
{ xworker }
{ xworker },
);
const workerName = wn?.value;
if (workerName) workers[workerName].resolve(xworker.ready);
return;
}
/* c8 ignore stop */
Expand Down
22 changes: 22 additions & 0 deletions esm/worker/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
// run either sync or async code in the worker
await details[name](interpreter, code);

if (['micropython', 'pyodide'].includes(details.type)) {
// this dance is required due Pyodide issues with runtime sync exports
// or MicroPython issue with `runPython` not returning values
const polyscript = 'polyscript';
const workers = `__${polyscript}_workers__`;
const exports = '__export__';
interpreter.runPython([
`import js as ${workers}`,
`${workers}.${workers} = "${exports}" in locals() and ${exports} or []`,
`del ${workers}`,
].join('\n'));
const list = [...globalThis[workers]];
delete globalThis[workers];
if (list.length) {
interpreter.runPython([
`from ${polyscript} import xworker as ${workers}`,
...list.map(util => `${workers}.sync.${util} = ${util}`),
`del ${workers}`,
].join('\n'));
}
}

// notify worker done executing
if (currentScript) notify('done');
postMessage('polyscript:done');
Expand Down
14 changes: 14 additions & 0 deletions esm/workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const workers = new Proxy(new Map, {
get(map, name) {
if (!map.has(name))
map.set(name, Promise.withResolvers());
return map.get(name);
},
});

export const workersHandler = new Proxy(workers, {
get: (_, name) => workers[name].promise.then(w => w.sync),
});
/* c8 ignore stop */
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-b+rQo4UjotSb69yF8u3W6zCnHV8eW55yotbdVICsICY="
"blob": "sha256-zrvckOW/DJa6Ds33kWWyNZtvV7aajCLCG+5LnBkpLiA="
}
}
2 changes: 1 addition & 1 deletion test/integration.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>polyscript integration tests</title>
</head>
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-json.html">config-json</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/current-script.html">current-script</a></li><li><a href="/test/integration/interpreter/micropython/custom-hooks.html">custom-hooks</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/interpreter-local.html">interpreter-local</a></li><li><a href="/test/integration/interpreter/micropython/mip.html">mip</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/ready-done.html">ready-done</a></li><li><a href="/test/integration/interpreter/micropython/storage.html">storage</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/button.html">button</a></li><li><a href="/test/integration/interpreter/pyodide/config-json.html">config-json</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul><li><strong>webr</strong><ul><li><a href="/test/integration/interpreter/webr/just-click.html">just-click</a></li></ul></ul></body>
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-json.html">config-json</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/current-script.html">current-script</a></li><li><a href="/test/integration/interpreter/micropython/custom-hooks.html">custom-hooks</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/interpreter-local.html">interpreter-local</a></li><li><a href="/test/integration/interpreter/micropython/mip.html">mip</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/ready-done.html">ready-done</a></li><li><a href="/test/integration/interpreter/micropython/storage.html">storage</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li><li><a href="/test/integration/interpreter/micropython/workers.html">workers</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/button.html">button</a></li><li><a href="/test/integration/interpreter/pyodide/config-json.html">config-json</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul><li><strong>webr</strong><ul><li><a href="/test/integration/interpreter/webr/just-click.html">just-click</a></li></ul></ul></body>
</html>
37 changes: 37 additions & 0 deletions test/integration/interpreter/micropython/workers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module">
import { init } from '../utils.js';
init('micropython');
</script>
</head>
<body>
<script type="micropython" async>
import js
from polyscript import workers

js.document.body.append("waiting Pyodide version ... ")

print("waiting for test worker ...")
test = await workers["test"]
print("test worker ready ...")

print("waiting for test.pyodide_version() ...")
version = await test.pyodide_version()
print("done")

js.document.body.append(version)
js.document.documentElement.classList.add("ok")
</script>
<script type="pyodide" name="test" worker>
def pyodide_version():
import sys
return sys.version

__export__ = ['pyodide_version']
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions test/integration/micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ module.exports = (playwright, baseURL) => {
await page.goto(`${baseURL}/storage.html`);
await page.waitForSelector(`html.ready.main.worker`);
});

test('MicroPython using workers', async ({ page }) => {
await page.goto(`${baseURL}/workers.html`);
await page.waitForSelector(`html.ok`);
});
};