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

More careful impl of hiwire_bool, added test cases #1061

Merged
merged 5 commits into from Jan 8, 2021

Conversation

hoodmane
Copy link
Member

@hoodmane hoodmane commented Jan 7, 2021

For a jsproxy, bool(val) is defined as (val && (val.length === undefined || val.length)). This seems to encode the assumption that anything with a length field is some sort of container. However, there are many things that are not a container that have a length field. window.length is the number of elements in the window, typically zero, so it considers window to be falsey. Functions also have a length field which counts their arguments, zero argument javascript functions are currently falsey.

Also, this implementation doesn't know about Set or Map, empty javascript sets and maps are truthy. I replaced it with a smarter implementation.

I also replaced the == and != in hiwire_eq resp hiwire_neq with === and !==. On the code paths that use hiwire_eq and hiwire_neq, these do the same thing, but === and !== are less confusing.

There is one more != operator:
https://github.com/iodide-project/pyodide/blob/1cb32c304d3901a57e95226e6b528501047cff01/src/core/hiwire.c#L428

However, the one place that != operator is used is in JsProxy_IterNext:

  JsRef iddone = hiwire_get_member_string(idresult, "done");
  int done = hiwire_nonzero(iddone);
  hiwire_decref(iddone);

Here iddone is either true or false and false !== 0 is true but false != 0 is false. So if we corrected hiwire_nonzero to be sensible it would break JsProxy_IterNext.

@dalcde
Copy link
Contributor

dalcde commented Jan 7, 2021 via email

@hoodmane
Copy link
Member Author

hoodmane commented Jan 7, 2021

They wanted empty containers to be falsey. !![] === true in javascript. Question is whether we are trying to be like javascript or like python in our answers.

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Thanks! Please add a changelog entry.

src/core/hiwire.c Outdated Show resolved Hide resolved
docs/changelog.md Outdated Show resolved Hide resolved
@dalcde dalcde merged commit cb29e5e into pyodide:master Jan 8, 2021
@hoodmane
Copy link
Member Author

hoodmane commented Jan 8, 2021

Thanks!

@hoodmane hoodmane deleted the fix-jsproxy-bool branch January 8, 2021 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants