Skip to content
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ coverage/
node_modules/
cjs/*
!cjs/package.json
core.js
core.js.map
core.*
!docs/core.js
!docs/core.js.map
pyscript.js
Expand Down
1 change: 0 additions & 1 deletion core.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion docs/core.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,24 @@ const mo = new MutationObserver((records) => {
if (node.nodeType === 1) {
addAllListeners(node);
if (selector && node.matches(selector)) handle(node);
else {
if (selector) $$(selector, node).forEach(handle);
if (!CUSTOM_SELECTORS.length) continue;
handleCustomType(node);
$$(CUSTOM_SELECTORS.join(','), node).forEach(
handleCustomType,
);
}
else bootstrap(selector, node, true);
}
}
/* c8 ignore stop */
}
});

/* c8 ignore start */
const bootstrap = (selector, node, shouldHandle) => {
if (selector) $$(selector, node).forEach(handle);
selector = CUSTOM_SELECTORS.join(',');
if (selector) {
if (shouldHandle) handleCustomType(node);
$$(selector, node).forEach(handleCustomType);
}
};
/* c8 ignore stop */

const observe = (root) => {
mo.observe(root, { childList: true, subtree: true, attributes: true });
return root;
Expand All @@ -69,6 +73,5 @@ assign(Element.prototype, {
// give 3rd party a chance to apply changes before this happens
queueMicrotask(() => {
addAllListeners(observe(document));
const selector = selectors.join(',');
if (selector) $$(selector, document).forEach(handle);
bootstrap(selectors.join(','), document, false);
});
4 changes: 2 additions & 2 deletions esm/worker/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ add('message', ({ data: { options, config: baseURL, code, hooks } }) => {
const { beforeRun, beforeRunAsync, afterRun, afterRunAsync } =
hooks;

const after = afterRun || afterRunAsync;
const before = beforeRun || beforeRunAsync;
const after = isAsync ? afterRunAsync : afterRun;
const before = isAsync ? beforeRunAsync : beforeRun;

// append code that should be executed *after* first
if (after) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@
"coincident": "^0.11.6"
},
"worker": {
"blob": "sha256-1DM1q+m8AQYdQnBem2ugixNUGX1u/ZO0A7a+evjdrIg="
"blob": "sha256-ymu4meyO3/xEFoZ1GF534LI+VCPKr7ELxnXOkS5hEIQ="
}
}
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-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</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/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></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-object.html">config-object</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/no-type.html">no-type</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/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></ul></body>
</html>
49 changes: 49 additions & 0 deletions test/integration/_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,55 @@ exports.python = {
await expect(logs[0]).toBe('hello from A');
},

customHooks: ({ expect }, baseURL) => async ({ page }) => {
// Test that a config passed as object works out of the box.
const logs = [];
page.on('console', msg => logs.push(msg.text()));
await page.goto(`${baseURL}/custom-hooks.html`);
await page.waitForSelector('html.done');
await expect(logs.join(',')).toBe([
'onInterpreterReady',
'onBeforeRun',
'script',
'onAfterRun',

'onInterpreterReady',
'onBeforeRunAsync',
'script-async',
'onAfterRunAsync',

'onWorkerReady',
'codeBeforeRunWorker',
'script-worker',
'codeAfterRunWorker',

'onWorkerReady',
'codeBeforeRunWorkerAsync',
'script-async-worker',
'codeAfterRunWorkerAsync',

'onInterpreterReady',
'onBeforeRun',
'mpy',
'onAfterRun',

'onInterpreterReady',
'onBeforeRunAsync',
'mpy-async',
'onAfterRunAsync',

'onWorkerReady',
'codeBeforeRunWorker',
'mpy-worker',
'codeAfterRunWorker',

'onWorkerReady',
'codeBeforeRunWorkerAsync',
'mpy-async-worker',
'codeAfterRunWorkerAsync',
].join(','));
},

disabledUntilReady: ({ expect }, baseURL) => async ({ page }) => {
await page.goto(`${baseURL}/button.html`);
await page.waitForSelector('button[disabled]');
Expand Down
74 changes: 74 additions & 0 deletions test/integration/interpreter/micropython/custom-hooks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>mpy-script{display:none}</style>
<script type="module">
import { define } from '/core.js';

define('mpy', {
interpreter: 'micropython',
onInterpreterReady: ({ run, runAsync }, element) => {
console.log('onInterpreterReady');
dispatchEvent(new Event('mpy:ready'));
const exec = element.hasAttribute('async') ? runAsync : run;
exec(element.textContent.trim());
},
onWorkerReady: () => console.log('onWorkerReady'),
onBeforeRun: () => console.log('onBeforeRun'),
onBeforeRunAsync: () => console.log('onBeforeRunAsync'),
onAfterRun: () => console.log('onAfterRun'),
onAfterRunAsync: () => console.log('onAfterRunAsync'),
codeBeforeRunWorker: () => 'print("codeBeforeRunWorker")',
codeBeforeRunWorkerAsync: () => 'print("codeBeforeRunWorkerAsync")',
codeAfterRunWorker: () => 'print("codeAfterRunWorker")',
codeAfterRunWorkerAsync: () => 'print("codeAfterRunWorkerAsync")',
});

const cases = [
{ type: 'script' },
{ type: 'script', async: true},
{ type: 'script', worker: true },
{ type: 'script', async: true, worker: true },
{ type: 'mpy' },
{ type: 'mpy', async: true},
{ type: 'mpy', worker: true },
{ type: 'mpy', async: true, worker: true },
];

(function inject() {
if (cases.length) {
addEventListener(
'mpy:ready',
() => {
// give enough time to run the `print` statement
// then invoke the inject function again
setTimeout(inject, 100);
},
{ once: true }
);
const current = cases.shift();
let type = current.type;
const isScript = type === 'script';
const tag = document.createElement(isScript ? 'script' : type + '-script');
if (isScript) tag.type = 'mpy';
if (current.async) {
type += '-async';
tag.setAttribute('async', '');
}
if (current.worker) {
type += '-worker';
tag.setAttribute('worker', '');
}
tag.textContent = `print("${type}")`
document.body.append(tag);
}
else {
document.documentElement.classList.add('done');
}
})();
</script>
</head>
<body></body>
</html>
2 changes: 2 additions & 0 deletions test/integration/micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module.exports = (playwright, baseURL) => {

test('MicroPython bootstrap', python.bootstrap(playwright, baseURL));

test('MicroPython custom hooks', python.customHooks(playwright, baseURL));

test('MicroPython fetch', python.fetch(playwright, `${baseURL}/fetch.html`));

test('MicroPython to MicroPython Worker', shared.worker(playwright, `${baseURL}/worker.html`));
Expand Down