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

Fix ES6 polyfills to use ToInteger and ToLength #3216

Closed
wants to merge 1 commit into from

Conversation

teppeis
Copy link
Contributor

@teppeis teppeis commented Jan 26, 2019

from #2902 (comment)

This change improves compatibility with ECMAScript spec.

Use ToInteger to support coercing, null, undefined, float, NaN and larger-than-Int32.
Also use ToLength to handle negative values and larger-than-2^53.

@@ -58,6 +58,7 @@ testSuite({
assertFails(RangeError, () => String.fromCodePoint(noCheck({})));
assertFails(RangeError, () => String.fromCodePoint(NaN));
assertFails(RangeError, () => String.fromCodePoint(noCheck(/./)));
assertFails(RangeError, () => String.fromCodePoint(noCheck(undefined)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although it is already working, I added a test just in case.

@tjgq tjgq requested a review from shicks January 28, 2019 21:46
@blickly
Copy link
Contributor

blickly commented Jan 31, 2019

Created internal issue b/123659800

@blickly blickly added the internal-issue-created An internal Google issue has been created to track this GitHub issue label Jan 31, 2019
opt_end = Number(opt_end);
if (opt_end < 0) opt_end = Math.max(0, length + opt_end);
for (var i = Number(opt_start || 0); i < opt_end; i++) {
var end = opt_end === undefined ? length : $jscomp.toInteger(opt_end);
Copy link
Member

@shicks shicks Mar 4, 2019

Choose a reason for hiding this comment

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

There's a lot of repetitive code here and in other files. What do you think about any of the following:

  • $jscomp.toInteger takes a second optional argument for what to return on undefined (may not be quite as smooth as just checking isNaN tho since we still want NaN to map to zero even if undefined maps to length - so not sure if it ends up being worth it).
  • add a $jscomp.toPosition(index, length) that handles negative indices and clamping all in one place (but would be nice to handle undefined here as well, and sometimes that's 0 and sometimes it's length - but at least we could just call $jscomp.toPosition(opt_start !== undefined ? opt_start : length, length))

@brad4d brad4d added the customer issue customer question, problem, or pull request label Apr 3, 2020
@lauraharker
Copy link
Contributor

Closing this since there have been no comments for over a year.

@lauraharker lauraharker closed this Apr 8, 2020
@teppeis teppeis deleted the to-integer branch September 15, 2022 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes customer issue customer question, problem, or pull request internal-issue-created An internal Google issue has been created to track this GitHub issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants