Commit cfd5a8ea authored by Damien George's avatar Damien George

webassembly/proxy_c: Return undefined if dict lookup failed on JS side.

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: default avatarDamien George <damien@micropython.org>
parent aa2e3880
......@@ -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.
......@@ -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();
......
// 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: {})})
`);
OK
OK
null
OK
[Function: value]
====
OK
OK
null
OK
[Function: obj] { _ref: 7 }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment