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 #17657.

Signed-off-by: Andrea Giammarchi <andrea.giammarchi@gmail.com>
This commit is contained in:
webreflection 2025-07-15 10:40:15 +02:00 committed by Damien George
parent 554f114f18
commit c72a3e528d
3 changed files with 46 additions and 26 deletions

View file

@ -165,34 +165,35 @@ const py_proxy_handler = {
if (prop === "_ref") { if (prop === "_ref") {
return target._ref; return target._ref;
} }
if (prop === "then") {
return null;
}
if (prop === Symbol.iterator) { // ignore both then and all symbols but Symbol.iterator
// Get the Python object iterator, and return a JavaScript generator. if (prop === "then" || typeof prop !== "string") {
const iter_ref = Module.ccall( if (prop === Symbol.iterator) {
"proxy_c_to_js_get_iter", // Get the Python object iterator, and return a JavaScript generator.
"number", const iter_ref = Module.ccall(
["number"], "proxy_c_to_js_get_iter",
[target._ref], "number",
); ["number"],
return function* () { [target._ref],
const value = Module._malloc(3 * 4); );
while (true) { return function* () {
const valid = Module.ccall( const value = Module._malloc(3 * 4);
"proxy_c_to_js_iternext", while (true) {
"number", const valid = Module.ccall(
["number", "pointer"], "proxy_c_to_js_iternext",
[iter_ref, value], "number",
); ["number", "pointer"],
if (!valid) { [iter_ref, value],
break; );
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); const value = Module._malloc(3 * 4);

View file

@ -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);

View file

@ -0,0 +1,5 @@
true
true
true
true
true