Skip to content

Commit c72a3e5

Browse files
WebReflectiondpgeorge
authored andcommitted
webassembly/objpyproxy: Avoid throwing on implicit symbols access.
This commit improves get handling by guarding against implicit unknown symbols accessed directly by specific JS native APIs. Fixes issue micropython#17657. Signed-off-by: Andrea Giammarchi <[email protected]>
1 parent 554f114 commit c72a3e5

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

ports/webassembly/objpyproxy.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,34 +165,35 @@ const py_proxy_handler = {
165165
if (prop === "_ref") {
166166
return target._ref;
167167
}
168-
if (prop === "then") {
169-
return null;
170-
}
171168

172-
if (prop === Symbol.iterator) {
173-
// Get the Python object iterator, and return a JavaScript generator.
174-
const iter_ref = Module.ccall(
175-
"proxy_c_to_js_get_iter",
176-
"number",
177-
["number"],
178-
[target._ref],
179-
);
180-
return function* () {
181-
const value = Module._malloc(3 * 4);
182-
while (true) {
183-
const valid = Module.ccall(
184-
"proxy_c_to_js_iternext",
185-
"number",
186-
["number", "pointer"],
187-
[iter_ref, value],
188-
);
189-
if (!valid) {
190-
break;
169+
// ignore both then and all symbols but Symbol.iterator
170+
if (prop === "then" || typeof prop !== "string") {
171+
if (prop === Symbol.iterator) {
172+
// Get the Python object iterator, and return a JavaScript generator.
173+
const iter_ref = Module.ccall(
174+
"proxy_c_to_js_get_iter",
175+
"number",
176+
["number"],
177+
[target._ref],
178+
);
179+
return function* () {
180+
const value = Module._malloc(3 * 4);
181+
while (true) {
182+
const valid = Module.ccall(
183+
"proxy_c_to_js_iternext",
184+
"number",
185+
["number", "pointer"],
186+
[iter_ref, value],
187+
);
188+
if (!valid) {
189+
break;
190+
}
191+
yield proxy_convert_mp_to_js_obj_jsside(value);
191192
}
192-
yield proxy_convert_mp_to_js_obj_jsside(value);
193-
}
194-
Module._free(value);
195-
};
193+
Module._free(value);
194+
};
195+
}
196+
return undefined;
196197
}
197198

198199
const value = Module._malloc(3 * 4);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Test `<py-obj> get <attr>` on the JavaScript side, which tests PyProxy.get.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
mp.runPython(`
6+
x = {"a": 1}
7+
`);
8+
9+
const x = mp.globals.get("x");
10+
console.log(x.a === 1);
11+
console.log(x.b === undefined);
12+
console.log(typeof x[Symbol.iterator] === "function");
13+
console.log(x[Symbol.toStringTag] === undefined);
14+
console.log(x.then === undefined);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
true
2+
true
3+
true
4+
true
5+
true

0 commit comments

Comments
 (0)