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

Editorial: Early return & throw in Number.prototype.to* methods #1938

Merged
merged 1 commit into from
May 21, 2020

Conversation

shvaikalesh
Copy link
Member

@shvaikalesh shvaikalesh commented Apr 6, 2020

This change makes handling of non-finite this values more concise (by using Number::toString) and moves it earlier in steps, as well as argument range checks. This makes observable steps grouped and order of observable steps easier to read (especially if a runtime uses something like std::isfinite as JSC does).

@shvaikalesh
Copy link
Member Author

shvaikalesh commented Apr 6, 2020

With this change, it is easier to spot a discrepancy in handling of non-finite this values:

0..toExponential(-1) // RangeError
0..toFixed(-1) // RangeError
0..toPrecision(-1) // RangeError

NaN.toExponential(-1) // => "NaN"
NaN.toFixed(-1) // RangeError
NaN.toPrecision(-1) // => "NaN"

Would you consider a normative PR to align it?

We can either a) move range check after finiteness check in toFixed or b) move range checks before finiteness checks in both toExponential and toPrecision.

a) is most certainly web-compatible, and it makes sense to perform all checks on this value before arguments.

spec.html Outdated Show resolved Hide resolved
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This looks observably equivalent.

spec.html Outdated Show resolved Hide resolved
@ljharb ljharb requested review from syg, michaelficarra, bakkot and a team April 6, 2020 23:51
@ljharb
Copy link
Member

ljharb commented Apr 13, 2020

@shvaikalesh regarding #1938 (comment), it seems like you're suggesting either a) making NaN.toFixed(-1) return NaN instead of throwing a RangeError, or b) making NaN.toExponential(-1) and NaN.toPrecision(-1) both throw a RangeError.

The latter seems unlikely to be web compatible, and the former does not seem desirable. Can you elaborate?

Copy link
Contributor

@bakkot bakkot left a comment

Choose a reason for hiding this comment

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

I like handling special cases as early as possible and this does appear to be equivalent.

@shvaikalesh
Copy link
Member Author

The latter seems unlikely to be web compatible, and the former does not seem desirable. Can you elaborate?

My only argument for the normative change is consistency, but it doesn't justify adding undesirable behavior. While all built-in methods check this value before arguments, they never return early for a subset of values.

@ljharb ljharb self-assigned this May 21, 2020
@ljharb ljharb force-pushed the number-prototype-early-return-throw branch from d838104 to 1ac5c6e Compare May 21, 2020 20:54
@ljharb ljharb merged commit 1ac5c6e into tc39:master May 21, 2020
jmdyck added a commit to jmdyck/ecma262 that referenced this pull request May 29, 2020
PR tc39#1938 (among other things) modified the steps of
Number.prototype.toExponential, but didn't update
the step-reference in the following Note.
jmdyck added a commit to jmdyck/ecma262 that referenced this pull request May 29, 2020
PR tc39#1938 (among other things) modified the steps of
Number.prototype.toExponential, but didn't update
the step-reference in the following Note.
jmdyck added a commit to jmdyck/ecma262 that referenced this pull request Jun 4, 2020
PR tc39#1938 (among other things) modified the steps of
Number.prototype.toExponential, but didn't update
the step-reference in the following Note.
ljharb pushed a commit to jmdyck/ecma262 that referenced this pull request Jun 15, 2020
…c39#2022)

PR tc39#1938 (among other things) modified the steps of Number.prototype.toExponential, but didn't update the step-reference in the following Note.
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.

4 participants