webassembly/objpyproxy: Implement JS iterator protocol for Py iterables.

This allows using JavaScript for..of on Python iterables.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2024-05-06 13:47:48 +10:00
parent e860e32e24
commit c056840ee8
5 changed files with 97 additions and 4 deletions

View File

@@ -0,0 +1,21 @@
// Test accessing Python iterables from JavaScript via the JavaScript iterator protocol.
const mp = await (await import(process.argv[2])).loadMicroPython();
mp.runPython(`
s = "abc"
l = [1, 2, 3]
`);
// Iterate a Python string.
for (const value of mp.globals.get("s")) {
console.log(value);
}
// Iterate a Python list.
for (const value of mp.globals.get("l")) {
console.log(value);
}
// Iterate a Python list from a built-in JavaScript constructor.
mp.runPython("import js; print(js.Set.new([1, 2, 3]).has(3))");

View File

@@ -0,0 +1,7 @@
a
b
c
1
2
3
True