Skip to content

Commit

Permalink
[Fix] ES2022+: StringToBigInt: invalid BigInts should be `undefin…
Browse files Browse the repository at this point in the history
…ed`, not `NaN` as in previous years
  • Loading branch information
ljharb committed Jan 6, 2023
1 parent d1acb45 commit 127b60d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,6 @@
/2022/StringGetOwnProperty.js spackled linguist-generated=true
/2022/StringIndexOf.js spackled linguist-generated=true
/2022/StringPad.js spackled linguist-generated=true
/2022/StringToBigInt.js spackled linguist-generated=true
/2022/StringToCodePoints.js spackled linguist-generated=true
/2022/SymbolDescriptiveString.js spackled linguist-generated=true
/2022/TestIntegrityLevel.js spackled linguist-generated=true
Expand Down
4 changes: 2 additions & 2 deletions 2022/IsLessThan.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ module.exports = function IsLessThan(x, y, LeftFirst) {
var ny;
if (pxType === 'BigInt' && pyType === 'String') {
ny = StringToBigInt(py);
if ($isNaN(ny)) {
if (typeof ny === 'undefined') {
return void undefined;
}
return BigIntLessThan(px, ny);
}
if (pxType === 'String' && pyType === 'BigInt') {
nx = StringToBigInt(px);
if ($isNaN(nx)) {
if (typeof nx === 'undefined') {
return void undefined;
}
return BigIntLessThan(nx, py);
Expand Down
3 changes: 1 addition & 2 deletions 2022/IsLooselyEqual.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var isFinite = require('../helpers/isFinite');
var isNaN = require('../helpers/isNaN');

var IsStrictlyEqual = require('./IsStrictlyEqual');
var StringToBigInt = require('./StringToBigInt');
Expand All @@ -28,7 +27,7 @@ module.exports = function IsLooselyEqual(x, y) {
}
if (xType === 'BigInt' && yType === 'String') {
var n = StringToBigInt(y);
if (isNaN(n)) {
if (typeof n === 'undefined') {
return false;
}
return IsLooselyEqual(x, n);
Expand Down
4 changes: 2 additions & 2 deletions 2022/StringToBigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var $BigInt = GetIntrinsic('%BigInt%', true);
var $TypeError = GetIntrinsic('%TypeError%');
var $SyntaxError = GetIntrinsic('%SyntaxError%');

// https://262.ecma-international.org/11.0/#sec-stringtobigint
// https://262.ecma-international.org/14.0/#sec-stringtobigint

module.exports = function StringToBigInt(argument) {
if (typeof argument !== 'string') {
Expand All @@ -18,6 +18,6 @@ module.exports = function StringToBigInt(argument) {
try {
return $BigInt(argument);
} catch (e) {
return NaN;
return void undefined;
}
};
75 changes: 73 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10213,6 +10213,9 @@ var es2020 = function ES2020(ES, ops, expectedMissing, skips) {
);
});

st.equal(ES.StringToBigInt(''), BigInt(0), 'empty string becomes 0n');
st.equal(ES.StringToBigInt('Infinity'), NaN, 'non-finite numeric string becomes NaN');

st.end();
});

Expand Down Expand Up @@ -11189,7 +11192,8 @@ var es2022 = function ES2022(ES, ops, expectedMissing, skips) {
'Abstract Equality Comparison': true,
'Abstract Relational Comparison': true,
'Strict Equality Comparison': true,
SplitMatch: true
SplitMatch: true,
StringToBigInt: true
}));

var test = makeTest(ES, skips);
Expand Down Expand Up @@ -11456,7 +11460,20 @@ var es2022 = function ES2022(ES, ops, expectedMissing, skips) {
[Number(String(v.coercibleObject)), v.coercibleObject],
[Number(v.coercibleObject), v.coercibleObject],
[String(Number(v.coercibleObject)), v.coercibleObject]
];
].concat(hasBigInts ? [
[BigInt(0), 0],
[0, BigInt(0)],
[BigInt(1), 1],
[1, BigInt(1)],
[BigInt(0), '0'],
['0', BigInt(0)],
[BigInt(1), '1'],
['1', BigInt(1)],
[BigInt(0), Infinity],
[Infinity, BigInt(0)],
[BigInt(0), 'Infinity'],
['Infinity', BigInt(0)]
] : []);
forEach(pairs, function (pair) {
var a = pair[0];
var b = pair[1];
Expand Down Expand Up @@ -11747,6 +11764,60 @@ var es2022 = function ES2022(ES, ops, expectedMissing, skips) {
t.end();
});

test('StringToBigInt', function (t) {
test('actual BigInts', { skip: !hasBigInts }, function (st) {
forEach(v.bigints, function (bigint) {
st.equal(
ES.StringToBigInt(String(bigint)),
bigint,
debug(String(bigint)) + ' becomes ' + debug(bigint)
);
});

forEach(v.integerNumbers, function (int) {
var bigint = safeBigInt(int);
st.equal(
ES.StringToBigInt(String(int)),
bigint,
debug(String(int)) + ' becomes ' + debug(bigint)
);
});

forEach(v.nonIntegerNumbers, function (nonInt) {
st.equal(
ES.StringToBigInt(String(nonInt)),
undefined,
debug(String(nonInt)) + ' becomes undefined'
);
});

st.equal(ES.StringToBigInt(''), BigInt(0), 'empty string becomes 0n');
st.equal(ES.StringToBigInt('Infinity'), undefined, 'non-finite numeric string becomes undefined');

st.end();
});

test('BigInt not supported', { skip: hasBigInts }, function (st) {
st['throws'](
function () { ES.StringToBigInt('0'); },
SyntaxError,
'throws a SyntaxError when BigInt is not available'
);

st.end();
});

forEach(v.nonStrings, function (nonString) {
t['throws'](
function () { ES.StringToBigInt(nonString); },
TypeError,
debug(nonString) + ' is not a string'
);
});

t.end();
});

test('StringToNumber', function (t) {
forEach(v.nonStrings, function (nonString) {
t['throws'](
Expand Down

0 comments on commit 127b60d

Please sign in to comment.