Description
Open the following page in your favorite COI-enabled dev server. The code works in the main thread, but fails in a worker:
<!doctype html>
<html lang="en">
<head>
<title>Polyscript</title>
<script type="module" src="https://cdn.jsdelivr.net/npm/polyscript"></script>
</head>
<body>
<script>
function aFunc(myArray) {
console.log(myArray[0], myArray[1])
}
</script>
<script type="module">
import { XWorker } from "https://cdn.jsdelivr.net/npm/polyscript";
const w = new XWorker("./workertest.py", { type: "pyodide"});
</script>
</body>
</html>
import polyscript
f = polyscript.xworker.window.aFunc
print(f([1,2]))
Specifically, there error I see is:
pyodide.ffi.JsException: TypeError: 'set' on proxy: trap returned falsish for property 'length'
This (very similar) code runs fine in Pyodide (0.24.1) in the main thread, or in a <script type="pyodide"> tag in PolyScript:
<script type="pyodide">
import js
f = js.aFunc
print(f([1,2]))
</script>
Context
First, you might have noticed that grabbing polyscript.xworker.window is basically what PyScript does in magic_js
. This is no accident, I was working on getting matplotlib to work in workers in PyScript, but am attempting to follow the issue upstream.
Second: if the issue I was trying to solve was code I controlled, the problem can be solved by manually converting the Python list [1,2]
into a JavaScript array inside worker.py
.
import polyscript
from pyodide.ffi import to_js
f = polyscript.xworker.window.aFunc
print(f(to_js([1,2])))
HOWEVER the original code is not code I control, it's code within the matplotlib-pyodide
backend that gets Matplotlib working in the browser, which passes a list to canvas.setLineDash()
.
From my understanding, the same code Python code should work in the main thread and in workers, generally, with the exception of some dances around some global objects (document, devicePixelRatio
, etc) needing to be brought in via xworker. Those exceptions I could figure out, but this one is stumping me.
Finally, as usual, I can't tell if this is an actual "bug" or just something I don't understand about js Proxies. Apologies in advance if it's the latter.