Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions ports/webassembly/objpyproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,34 +165,35 @@ const py_proxy_handler = {
if (prop === "_ref") {
return target._ref;
}
if (prop === "then") {
return null;
}

if (prop === Symbol.iterator) {
// Get the Python object iterator, and return a JavaScript generator.
const iter_ref = Module.ccall(
"proxy_c_to_js_get_iter",
"number",
["number"],
[target._ref],
);
return function* () {
const value = Module._malloc(3 * 4);
while (true) {
const valid = Module.ccall(
"proxy_c_to_js_iternext",
"number",
["number", "pointer"],
[iter_ref, value],
);
if (!valid) {
break;
// ignore both then and all symbols but Symbol.iterator
if (prop === "then" || typeof prop !== "string") {
if (prop === Symbol.iterator) {
// Get the Python object iterator, and return a JavaScript generator.
const iter_ref = Module.ccall(
"proxy_c_to_js_get_iter",
"number",
["number"],
[target._ref],
);
return function* () {
const value = Module._malloc(3 * 4);
while (true) {
const valid = Module.ccall(
"proxy_c_to_js_iternext",
"number",
["number", "pointer"],
[iter_ref, value],
);
if (!valid) {
break;
}
yield proxy_convert_mp_to_js_obj_jsside(value);
}
yield proxy_convert_mp_to_js_obj_jsside(value);
}
Module._free(value);
};
Module._free(value);
};
}
return undefined;
}

const value = Module._malloc(3 * 4);
Expand Down
14 changes: 14 additions & 0 deletions tests/ports/webassembly/py_proxy_get.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Test `<py-obj> get <attr>` on the JavaScript side, which tests PyProxy.get.

const mp = await (await import(process.argv[2])).loadMicroPython();

mp.runPython(`
x = {"a": 1}
`);

const x = mp.globals.get("x");
console.log(x.a === 1);
console.log(x.b === undefined);
console.log(typeof x[Symbol.iterator] === "function");
console.log(x[Symbol.toStringTag] === undefined);
console.log(x.then === undefined);
5 changes: 5 additions & 0 deletions tests/ports/webassembly/py_proxy_get.mjs.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
true
true
true
true
true
Loading