Skip to content

Commit bf92fda

Browse files
committed
added working Py2JS converter for future changes
1 parent 99c11f5 commit bf92fda

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

test/raw/converter.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { symbol } from 'https://esm.run/@ungap/serialization-registry';
2+
3+
const { construct } = Reflect;
4+
const { defineProperty, fromEntries } = Object;
5+
6+
const name = '#Py2JS:Proxy';
7+
const patch = Symbol.for(name);
8+
const patched = patch in globalThis;
9+
10+
// pyodide
11+
const toJsOptions = { dict_converter: fromEntries };
12+
13+
export const converter = patched ? globalThis[patch] : [
14+
// pyodide
15+
target => {
16+
if ('toJs' in target)
17+
return target.toJs(toJsOptions);
18+
},
19+
20+
// micropython
21+
target => {
22+
const { constructor } = target;
23+
if (constructor && 'toJs' in constructor)
24+
return constructor.toJs(target);
25+
},
26+
];
27+
28+
if (!patched) {
29+
defineProperty(globalThis, patch, { value: converter });
30+
defineProperty(globalThis, 'Proxy', {
31+
value: new Proxy(Proxy, {
32+
construct(target, args, newTarget) {
33+
const original = args[1]?.get;
34+
if (original && !original.name !== name) {
35+
args[1].get = defineProperty(
36+
function (target, prop, receiver) {
37+
if (prop === symbol) {
38+
for (let value, i = 0; i < converter.length; i++) {
39+
value = converter[i](target);
40+
if (value) return value;
41+
}
42+
}
43+
return original.call(this, target, prop, receiver);
44+
},
45+
'name',
46+
{ value: name }
47+
);
48+
}
49+
return construct(target, args, newTarget);
50+
}
51+
})
52+
});
53+
}

test/raw/micropython/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width,initial-scale=1.0">
66
<script type="module">
7+
import { serialize } from 'https://esm.run/@ungap/serialization-registry';
8+
import '../converter.js';
9+
10+
globalThis.test = arg => {
11+
console.log(...serialize(arg));
12+
};
13+
714
const base = 'https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@latest';
815
const { loadMicroPython } = await import(`${base}/micropython.mjs`);
916
const interpreter = await loadMicroPython({ url: `${base}/micropython.wasm` });
@@ -12,7 +19,7 @@
1219
</script>
1320
<script type="micropython" async>
1421
import js
15-
js.document.body.textContent = "MicroPython"
22+
js.test([{'a': 123}, {'a': 456}])
1623
</script>
1724
</head>
1825
</html>

test/raw/pyodide/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width,initial-scale=1.0">
66
<script type="module">
7+
import { serialize } from 'https://esm.run/@ungap/serialization-registry';
8+
import '../converter.js';
9+
10+
globalThis.test = arg => {
11+
console.log(...serialize(arg));
12+
};
13+
714
const base = 'https://cdn.jsdelivr.net/npm/pyodide@latest';
815
const { loadPyodide } = await import(`${base}/pyodide.mjs`);
916
const interpreter = await loadPyodide();
@@ -12,7 +19,7 @@
1219
</script>
1320
<script type="pyodide" async>
1421
import js
15-
js.document.body.textContent = "Pyodide"
22+
js.test([{'a': 123}, {'a': 456}])
1623
</script>
1724
</head>
1825
</html>

0 commit comments

Comments
 (0)