Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webassembly: Revert conversion of Python None to JavaScript null, and make dict proxy return undefined when key doesn't exist #14483

Merged

Conversation

dpgeorge
Copy link
Member

There are 3 related commits in this PR:

  • Revert back to converting Python None to JavaScript null. This essentially reverts fa23e4b which made None convert to undefined. So now it's back to how it was before that commit, and Py None converts to JS null. That's consistent with how the json module converts these values.
  • Create a special "undefined" type/instance for Python visibility. This corresponds exactly to JS undefined. It's accessible via js.undefined. Passing this from Py to JS, JS will receive undefined. And when JS passes undefined to Py, Py will see the proxy js.undefined value.
  • Make proxy Py dicts return undefined to JS when JS looks up a key that doesn't exist (instead of raising KeyError). These semantics match better the JS behaviour of {} objects.

The first two changes mean the following:

  • There's a one-to-one correspondence between Py None and JS null.
  • There's a one-to-one correspondence between Py js.undefined and JS undefined.
  • Conversion of these values matches the json module.

All three changes together mean that, given the following JavaScript function with an optional argument:

function withDefault({ value = 'OK' } = {}) {
  console.log(value);
}

the JavaScript and Python code to call this function have equivalent behaviour, namely, in JavaScript:

withDefault();
withDefault({});
withDefault({ value: null });
withDefault({ value: undefined });

and in Python:

js.withDefault()
js.withDefault({})
js.withDefault({"value": None})
js.withDefault({"value": js.undefined})

@dpgeorge
Copy link
Member Author

@WebReflection please take a look at this PR and see what you think. It's a bit different to what we discussed on Discord, the difference being that undefined is not converted to Py None, but rather to a distinct object js.undefined. That means that JS functions that return their default value of undefined will pass that value through to Python. But, that has very little impact (no tests we have are impacted by that), and doing it this way keeps a proper separation between undefined and null on both sides (Py and JS sides). So IMO it feels nice and consistent, a one-to-one correspondence between null and None.

Copy link

codecov bot commented May 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (c10a74b) to head (cfd5a8e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14483   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21204    21204           
=======================================
  Hits        20864    20864           
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dpgeorge dpgeorge added this to the release-1.23.0 milestone May 14, 2024
@@ -228,7 +241,15 @@ 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 dict key (not a method),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that not as method kinda bothers me ... function some({ thing = () => {} } = {}) {} is a valid JS expectation, if this is covered in here I am OK with that comment (if explained somewhere else), otherwise I wonder "why"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's just that the comment I wrote here is confusing. I meant that here the code is looking up the attribute as a key within the dict (or ordereddict). That's opposed to the standard case of looking up the attribute as the name of a member/function/method of the target object.

Eg looking up attr on target will have the following logic:

  • If target is a dict or ordereddict instance then lookup target[attr], and return the result. If the lookup failed, return js.undefined.
  • Otherwise, lookup target.foo, raising an exception if it can't be found.

I've updated the comments to try and make it clearer.

I also added a test for passing through a function in the dict.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, 👍 for me!

@WebReflection
Copy link

it's true that methods (or utilities?) are rare, but not that much neither for JS related APIs ... I've left a comment that, once resolved or explained, would make me virtually approve this PR as I think it's awesome in both intent and expectations 👍

@dpgeorge dpgeorge force-pushed the webassembly-expose-undefined-to-python branch 2 times, most recently from ede7f72 to 22cb120 Compare May 15, 2024 00:52
This reverts part of commit fa23e4b, to
make it so that Python `None` converts to JavaScript `null` (and JavaScript
`null` already converts to Python `None`).  That's consistent with how the
`json` module converts these values back and forth.

Signed-off-by: Damien George <damien@micropython.org>
This adds a new undefined singleton to Python, that corresponds directly to
JavaScript `undefined`.  It's accessible via `js.undefined`.

Signed-off-by: Damien George <damien@micropython.org>
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>
@dpgeorge dpgeorge force-pushed the webassembly-expose-undefined-to-python branch from 22cb120 to cfd5a8e Compare May 16, 2024 02:50
@dpgeorge dpgeorge merged commit cfd5a8e into micropython:master May 16, 2024
26 checks passed
@dpgeorge dpgeorge deleted the webassembly-expose-undefined-to-python branch May 16, 2024 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants