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:
21
tests/ports/webassembly/iterator.mjs
Normal file
21
tests/ports/webassembly/iterator.mjs
Normal 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))");
|
||||
7
tests/ports/webassembly/iterator.mjs.exp
Normal file
7
tests/ports/webassembly/iterator.mjs.exp
Normal file
@@ -0,0 +1,7 @@
|
||||
a
|
||||
b
|
||||
c
|
||||
1
|
||||
2
|
||||
3
|
||||
True
|
||||
Reference in New Issue
Block a user