Skip to content

Commit

Permalink
webassembly/proxy_c: Return undefined if dict lookup failed on JS side.
Browse files Browse the repository at this point in the history
Instead of raising KeyError.  These semantics match JavaScript behaviour
and make it much more seamless to pass Python dicts through to JavaScript
as though they were JavaScript {} objects.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed May 16, 2024
1 parent aa2e388 commit cfd5a8e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ports/webassembly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,6 @@ A Python `dict` instance is proxied such that:
}

works as expected on the JavaScript side and iterates through the keys of the
Python `dict`.
Python `dict`. Furthermore, when JavaScript accesses a key that does not exist
in the Python dict, the JavaScript code receives `undefined` instead of a
`KeyError` exception being raised.
11 changes: 10 additions & 1 deletion ports/webassembly/proxy_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,17 @@ void proxy_c_to_js_lookup_attr(uint32_t c_ref, const char *attr_in, uint32_t *ou
qstr attr = qstr_from_str(attr_in);
mp_obj_t member;
if (mp_obj_is_dict_or_ordereddict(obj)) {
member = mp_obj_dict_get(obj, MP_OBJ_NEW_QSTR(attr));
// Lookup the requested attribute as a key in the target dict, and
// return `undefined` if not found (instead of raising `KeyError`).
mp_obj_dict_t *self = MP_OBJ_TO_PTR(obj);
mp_map_elem_t *elem = mp_map_lookup(&self->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem == NULL) {
member = mp_const_undefined;
} else {
member = elem->value;
}
} else {
// Lookup the requested attribute as a member/method of the target object.
member = mp_load_attr(obj, attr);
}
nlr_pop();
Expand Down
34 changes: 34 additions & 0 deletions tests/ports/webassembly/py_proxy_dict_undefined.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Test passing a Python dict into JavaScript, how it behaves with undefined keys.
// If JavaScript accesses a key that does not exist, `undefined` should be returned.
// This is different to Python-side behaviour, where `KeyError` is raised.

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

// Create a JavaScript function with default arguments.
// When `value` is `undefined` it will receive its default.
function withDefault({ value = "OK" } = {}) {
console.log(value);
}

globalThis.withDefault = withDefault;

// Call the function from JavaScript with various arguments.
withDefault();
withDefault({});
withDefault({ value: null });
withDefault({ value: undefined });
withDefault({ value: () => {} });

console.log("====");

// Call the function from Python with the same arguments as above.
// The results should be the same.
mp.runPython(`
import js
js.withDefault()
js.withDefault({})
js.withDefault({"value": None})
js.withDefault({"value": js.undefined})
js.withDefault({"value": (lambda: {})})
`);
11 changes: 11 additions & 0 deletions tests/ports/webassembly/py_proxy_dict_undefined.mjs.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
OK
OK
null
OK
[Function: value]
====
OK
OK
null
OK
[Function: obj] { _ref: 7 }

0 comments on commit cfd5a8e

Please sign in to comment.