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

Disallow -0 in algorithm for checking array index #517

Merged
merged 4 commits into from
Feb 9, 2018

Conversation

TimothyGu
Copy link
Member

@TimothyGu TimothyGu commented Jan 31, 2018

Fixes: #516

CC @foolip


Preview | Diff

index.bs Outdated
1. If |P| is "<code>-0</code>", then return <emu-val>false</emu-val>.

Note: <a abstract-op>CanonicalNumericIndexString</a> allows "<code>-0</code>" as a special
case, even though it is not an [=integer index=] or by extension an [=array index=].
Copy link
Member

Choose a reason for hiding this comment

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

If this indeed matches implementations, wouldn't it be cleaner to return false if index is -0 after the following step?

Copy link
Member

Choose a reason for hiding this comment

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

The implementation for this in Chromium:
https://chromium.googlesource.com/v8/v8.git/+/842d424d99060375bf4a8b6fdfd2a0177e4c0477/src/api.cc#4128
https://chromium.googlesource.com/v8/v8.git/+/842d424d99060375bf4a8b6fdfd2a0177e4c0477/src/utils-inl.h#35

So no special handling of "-0" there because the code uses unsigned integers. ES uses ToNumber where negative zero is possible, so whatever this says won't be a close match.

@foolip
Copy link
Member

foolip commented Jan 31, 2018

I have tests for the implications fo document.all in https://chromium-review.googlesource.com/c/chromium/src/+/880621. They already pass in Safari, and will pass in Chrome with those changes.

@annevk
Copy link
Member

annevk commented Jan 31, 2018

FWIW, w(document.querySelectorAll("body")["-0"]) (unlike w(document.querySelectorAll("body")[-0])) indeed returns undefined so this is indeed a bug.

I wonder how many other algorithms have subtle -0 issues...

Copy link
Member

@foolip foolip left a comment

Choose a reason for hiding this comment

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

This special-casing LGTM. Checking for -0 would also work, but something more like https://tc39.github.io/ecma262/#array-index would also be fine as long as the note makes it clear that it's "-0" that is special.

@tobie
Copy link
Collaborator

tobie commented Jan 31, 2018

Wouldn't just changing step 4 from:

If |index| is less than 0 or is greater than or equal to 2<sup>32</sup> − 1, then return false .

to:

If |index| is not in the range +0 ≤ |index| < 2<sup>32</sup> − 1, then return false.

have the same effect, and match the ES spec more closely?

Maybe we could also mention -0 in the note right below, or just say that the range corresponds to the range of supported array indices in ECMAScript.

@foolip
Copy link
Member

foolip commented Jan 31, 2018

With a note saying that it's supposed to be equivalent to https://tc39.github.io/ecma262/#array-index anything would do, but using just the ≤ operator in the normative definition seems wrong. Whether operating on IEEE doubles or real numbers, positive zero equals negative zero. So it's not possible to write that expression out as-is in code or in mathematics and have -0 not be in the range.

@annevk
Copy link
Member

annevk commented Jan 31, 2018

Hopefully this will all be a bit clearer once whatwg/infra#87 is solved.

@tobie
Copy link
Collaborator

tobie commented Jan 31, 2018

but using just the ≤ operator in the normative definition seems wrong.

Yeah, I tend to agree, but as I lifted that off of the ES spec I get to blame someone else. :D

@foolip
Copy link
Member

foolip commented Feb 6, 2018

Who can make a decision on this? Anything WFM.

@tobie
Copy link
Collaborator

tobie commented Feb 6, 2018

A bunch of suggestions were made to improve the initial PR. I'll make a last one: replace step 5 with the following:

1.  If |index| is not an [=array index=], then return false.

and fix or remove the related note.

@tobie
Copy link
Collaborator

tobie commented Feb 7, 2018

Realized how tautological my previous comment was. Pushed some changes to @TimothyGu's branch. Fixed the issue that we weren't filtering out non integers before.

@foolip can you give the latest a look?

Copy link
Member Author

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

Otherwise looks good to me.

index.bs Outdated
1. If |index| is -0, then return <emu-val>false</emu-val>.
1. If |index| is smaller than 0, then return <emu-val>false</emu-val>.
1. If |index| is greater than or equal to 2<sup>32</sup> − 1,
then return <emu-val>false</emu-val>.
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 would use < and ≥, just to make things a bit more obvious. If you prefer English over mathematical symbols, s/smaller than/less than/.

Also I'd like to keep the note:

Note: 232 − 1 is the maximum array length allowed by ECMAScript.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was hesitant about removing the note, but if it's not just me, then I'll keep it around.

@tobie
Copy link
Collaborator

tobie commented Feb 8, 2018

Yay, thanks. Writing the test and merging this.

tobie added a commit to tobie/web-platform-tests that referenced this pull request Feb 8, 2018
@tobie tobie merged commit 3750d7a into whatwg:master Feb 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants