Skip to content

Commit

Permalink
Update ranges on Number to string functions.
Browse files Browse the repository at this point in the history
Summary:
Starting with ES9.0 (ES2018), the range on toFixed, toPrecision and
toExponential is larger than it used to be.

Account for this.

Reviewed By: tmikov

Differential Revision: D16213486

fbshipit-source-id: 9b0d3878c8a7cb79bae3733733a314163cf6da2c
  • Loading branch information
avp authored and facebook-github-bot committed Jul 12, 2019
1 parent deaf761 commit d61e5b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
15 changes: 9 additions & 6 deletions lib/VM/JSLib/Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,10 @@ numberPrototypeToFixed(void *, Runtime *runtime, NativeArgs args) {
}
double fDouble = intRes->getNumber();

if (LLVM_UNLIKELY(fDouble < 0 || fDouble > 20)) {
// 3. If f < 0 or f > 100, throw a RangeError exception.
if (LLVM_UNLIKELY(fDouble < 0 || fDouble > 100)) {
return runtime->raiseRangeError(
"toFixed argument must be between 0 and 20");
"toFixed argument must be between 0 and 100");
}
/// Number of digits after the decimal point.
/// Because we checked, 0 <= f <= 20.
Expand Down Expand Up @@ -559,10 +560,11 @@ numberPrototypeToExponential(void *, Runtime *runtime, NativeArgs args) {
runtime->getPredefinedString(Predefined::NegativeInfinity));
}

// 8. If f < 0 or f > 100, throw a RangeError exception.
if (LLVM_UNLIKELY(
!args.getArg(0).isUndefined() && (fDouble < 0 || fDouble > 20))) {
!args.getArg(0).isUndefined() && (fDouble < 0 || fDouble > 100))) {
return runtime->raiseRangeError(
"toFixed argument must be between 0 and 20");
"toExponential argument must be between 0 and 20");
}
/// Number of digits after the decimal point.
/// Because we checked, 0 <= f <= 20.
Expand Down Expand Up @@ -694,9 +696,10 @@ numberPrototypeToPrecision(void *, Runtime *runtime, NativeArgs args) {
runtime->getPredefinedString(Predefined::NegativeInfinity));
}

if (pDouble < 1 || pDouble > 21) {
// 8. If p < 1 or p > 100, throw a RangeError exception.
if (pDouble < 1 || pDouble > 100) {
return runtime->raiseRangeError(
"toPrecision argument must be between 1 and 21");
"toPrecision argument must be between 1 and 100");
}
/// Number of significant digits in the result.
/// Because we checked, 1 <= p <= 21.
Expand Down
16 changes: 11 additions & 5 deletions test/hermes/number-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ print((0.0005).toFixed(3));
// CHECK-NEXT: 0.001
print((1234.567).toFixed(20));
// CHECK-NEXT: 1234.56700000000000727596
print((1234.567).toFixed(25));
// CHECK-NEXT: 1234.5670000000000072759576142
print((1000000000000000128).toFixed(0));
// CHECK-NEXT: 1000000000000000128
print((1000000000000000128.19238).toFixed(1));
// CHECK-NEXT: 1000000000000000128.0
try {(123).toFixed(100);} catch (e) {print('caught', e)}
try {(123).toFixed(101);} catch (e) {print('caught', e)}
// CHECK-NEXT: caught RangeError: {{.*}}
try {(123).toFixed(-1);} catch (e) {print('caught', e)}
// CHECK-NEXT: caught RangeError: {{.*}}
Expand Down Expand Up @@ -253,6 +255,8 @@ print((-1234.567).toExponential(7));
// CHECK-NEXT: -1.2345670e+3
print((-1234.567).toExponential(20));
// CHECK-NEXT: -1.23456700000000000728e+3
print((-1234.567).toExponential(25));
// CHECK-NEXT: -1.2345670000000000072759576e+3
print((.0015).toExponential());
// CHECK-NEXT: 1.5e-3
print((.0015).toExponential(0));
Expand All @@ -263,9 +267,9 @@ print((.0015).toExponential(2));
// CHECK-NEXT: 1.50e-3
print((.0015).toExponential(3));
// CHECK-NEXT: 1.500e-3
print((.0015).toExponential(20));
// CHECK-NEXT: 1.50000000000000003123e-3
try {(123).toExponential(100);} catch (e) {print('caught', e)}
print((.0015).toExponential(25));
// CHECK-NEXT: 1.5000000000000000312250226e-3
try {(123).toExponential(101);} catch (e) {print('caught', e)}
// CHECK-NEXT: caught RangeError: {{.*}}
try {(123).toExponential(-1);} catch (e) {print('caught', e)}
// CHECK-NEXT: caught RangeError: {{.*}}
Expand Down Expand Up @@ -328,6 +332,8 @@ print((-1234.567).toPrecision(8));
// CHECK-NEXT: -1234.5670
print((-1234.567).toPrecision(21));
// CHECK-NEXT: -1234.56700000000000728
print((-1234.567).toPrecision(25));
// CHECK-NEXT: -1234.567000000000007275958
print((.0015).toPrecision(1));
// CHECK-NEXT: 0.002
print((.0015).toPrecision(2));
Expand All @@ -338,7 +344,7 @@ print((-.000000015).toPrecision(2));
// CHECK-NEXT: -1.5e-8
try {print((123).toPrecision(0));} catch (e) {print('caught', e);}
// CHECK-NEXT: caught RangeError: {{.*}}
try {print((123).toPrecision(22));} catch (e) {print('caught', e);}
try {print((123).toPrecision(101));} catch (e) {print('caught', e);}
// CHECK-NEXT: caught RangeError: {{.*}}

print('isFinite');
Expand Down
3 changes: 0 additions & 3 deletions utils/testsuite/testsuite_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,6 @@
"mjsunit/es6/tail-call.js",
"mjsunit/modules-exports2.js",
"test262/test/built-ins/ArrayIteratorPrototype/next/detach-typedarray-in-progress.js",
"test262/test/built-ins/Number/prototype/toFixed/range.js",
"test262/test/built-ins/Number/prototype/toExponential/range.js",
"test262/test/built-ins/Number/prototype/toPrecision/range.js",
"test262/test/language/expressions/tagged-template/invalid-escape-sequences.js",
"test262/test/harness/verifyProperty-restore-accessor.js",
"test262/test/harness/detachArrayBuffer-host-detachArrayBuffer.js",
Expand Down

0 comments on commit d61e5b0

Please sign in to comment.