Skip to content

Commit 8c46fca

Browse files
Updating polyscript to its latest (#2128)
* Updating polyscript to its latest
1 parent e4ff4d8 commit 8c46fca

10 files changed

Lines changed: 286 additions & 196 deletions

File tree

pyscript.core/package-lock.json

Lines changed: 168 additions & 175 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.4.56",
3+
"version": "0.5.0",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",
@@ -44,7 +44,7 @@
4444
"dependencies": {
4545
"@ungap/with-resolvers": "^0.1.0",
4646
"basic-devtools": "^0.1.6",
47-
"polyscript": "^0.13.10",
47+
"polyscript": "^0.14.2",
4848
"sticky-module": "^0.1.1",
4949
"to-json-callback": "^0.1.1",
5050
"type-checked-collections": "^0.1.7"
@@ -54,24 +54,24 @@
5454
"@codemirror/lang-python": "^6.1.6",
5555
"@codemirror/language": "^6.10.2",
5656
"@codemirror/state": "^6.4.1",
57-
"@codemirror/view": "^6.28.5",
58-
"@playwright/test": "^1.45.2",
57+
"@codemirror/view": "^6.29.1",
58+
"@playwright/test": "^1.45.3",
5959
"@rollup/plugin-commonjs": "^26.0.1",
6060
"@rollup/plugin-node-resolve": "^15.2.3",
6161
"@rollup/plugin-terser": "^0.4.4",
6262
"@webreflection/toml-j0.4": "^1.1.3",
6363
"@xterm/addon-fit": "^0.10.0",
6464
"@xterm/addon-web-links": "^0.11.0",
65-
"bun": "^1.1.20",
65+
"bun": "^1.1.21",
6666
"chokidar": "^3.6.0",
6767
"codemirror": "^6.0.1",
68-
"eslint": "^9.7.0",
68+
"eslint": "^9.8.0",
6969
"flatted": "^3.3.1",
70-
"rollup": "^4.18.1",
70+
"rollup": "^4.19.1",
7171
"rollup-plugin-postcss": "^4.0.2",
7272
"rollup-plugin-string": "^3.0.0",
7373
"static-handler": "^0.4.3",
74-
"typescript": "^5.5.3",
74+
"typescript": "^5.5.4",
7575
"xterm": "^5.3.0",
7676
"xterm-readline": "^1.1.1"
7777
},

pyscript.core/src/plugins/py-editor.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ async function execute({ currentTarget }) {
3434

3535
if (!envs.has(env)) {
3636
const srcLink = URL.createObjectURL(new Blob([""]));
37-
const details = { type: this.interpreter };
37+
const details = {
38+
type: this.interpreter,
39+
serviceWorker: this.serviceWorker,
40+
};
3841
const { config } = this;
3942
if (config) {
4043
details.configURL = relative_url(config);
@@ -163,8 +166,17 @@ const init = async (script, type, interpreter) => {
163166

164167
let isSetup = script.hasAttribute("setup");
165168
const hasConfig = script.hasAttribute("config");
169+
const serviceWorker = script.getAttribute("service-worker");
166170
const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;
167171

172+
// helps preventing too lazy ServiceWorker initialization on button run
173+
if (serviceWorker) {
174+
new XWorker("data:application/javascript,postMessage(0)", {
175+
type: "dummy",
176+
serviceWorker,
177+
}).onmessage = ({ target }) => target.terminate();
178+
}
179+
168180
if (hasConfig && configs.has(env)) {
169181
throw new SyntaxError(
170182
configs.get(env)
@@ -181,6 +193,7 @@ const init = async (script, type, interpreter) => {
181193
const context = {
182194
// allow the listener to be overridden at distance
183195
handleEvent: execute,
196+
serviceWorker,
184197
interpreter,
185198
env,
186199
config: hasConfig && script.getAttribute("config"),

pyscript.core/src/stdlib/pyscript/magic_js.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def __getattr__(self, field):
3636
)
3737

3838
try:
39-
globalThis.SharedArrayBuffer.new(4)
4039
import js
4140

4241
window = polyscript.xworker.window
@@ -47,17 +46,11 @@ def __getattr__(self, field):
4746
"return (...urls) => Promise.all(urls.map((url) => import(url)))"
4847
)()
4948
except:
50-
globalThis.console.debug("SharedArrayBuffer is not available")
51-
# in this scenario none of the utilities would work
52-
# as expected so we better export these as NotSupported
53-
window = NotSupported(
54-
"pyscript.window",
55-
"pyscript.window in workers works only via SharedArrayBuffer",
56-
)
57-
document = NotSupported(
58-
"pyscript.document",
59-
"pyscript.document in workers works only via SharedArrayBuffer",
60-
)
49+
message = "Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer"
50+
globalThis.console.warn(message)
51+
window = NotSupported("pyscript.window", message)
52+
document = NotSupported("pyscript.document", message)
53+
js_import = None
6154

6255
sync = polyscript.xworker.sync
6356

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="../../dist/core.css">
7+
<script type="module" src="../../dist/core.js"></script>
8+
</head>
9+
<body>
10+
<script type="mpy-editor" service-worker="./sw.js">
11+
from pyscript import document
12+
13+
document.body.append("OK")
14+
</script>
15+
</body>
16+
</html>

pyscript.core/test/py-editor/sw.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Service Worker</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width,initial-scale=1">
7+
<link rel="stylesheet" href="../../dist/core.css">
8+
<script type="module" src="../../dist/core.js"></script>
9+
</head>
10+
<body>
11+
<script type="mpy" service-worker="./sabayon.js" worker>
12+
from pyscript import document
13+
document.body.append('OK')
14+
</script>
15+
</body>
16+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
2+
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */
3+
(({ document: d, navigator: { serviceWorker: s } }) => {
4+
if (d) {
5+
const { currentScript: c } = d;
6+
s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => {
7+
r.addEventListener('updatefound', () => location.reload());
8+
if (r.active && !s.controller) location.reload();
9+
});
10+
}
11+
else {
12+
addEventListener('install', () => skipWaiting());
13+
addEventListener('activate', e => e.waitUntil(clients.claim()));
14+
addEventListener('fetch', e => {
15+
const { request: r } = e;
16+
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
17+
e.respondWith(fetch(r).then(r => {
18+
const { body, status, statusText } = r;
19+
if (!status || status > 399) return r;
20+
const h = new Headers(r.headers);
21+
h.set('Cross-Origin-Opener-Policy', 'same-origin');
22+
h.set('Cross-Origin-Embedder-Policy', 'require-corp');
23+
h.set('Cross-Origin-Resource-Policy', 'cross-origin');
24+
return new Response(body, { status, statusText, headers: h });
25+
}));
26+
});
27+
}
28+
})(self);

pyscript.core/test/service-worker/sabayon.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
6+
<title>named workers</title>
7+
<script type="module" src="../../dist/core.js"></script>
8+
</head>
9+
<body>
10+
<script type="mpy" async>
11+
from pyscript import workers
12+
13+
await (await workers["mpy"]).greetings()
14+
await (await workers["py"]).greetings()
15+
</script>
16+
<script type="mpy" worker name="mpy">
17+
def greetings():
18+
print("micropython")
19+
20+
__export__ = ['greetings']
21+
</script>
22+
<script type="py" worker name="py">
23+
def greetings():
24+
print("pyodide")
25+
26+
__export__ = ['greetings']
27+
</script>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)