Files
micropython/tests/ports/webassembly/iterator.mjs
Damien George c056840ee8 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>
2024-05-07 00:20:56 +10:00

22 lines
534 B
JavaScript

// 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))");