Skip to content

Commit

Permalink
[Fix] ES2017+: RawBytesToNumber, RawBytesToNumeric: properly ha…
Browse files Browse the repository at this point in the history
…ndle some scenarios
  • Loading branch information
ljharb committed Jul 5, 2023
1 parent 156bf41 commit 47eb5eb
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 803 deletions.
6 changes: 3 additions & 3 deletions 2017/RawBytesToNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Return the Number value that corresponds to value.
// this is common to both branches
var intValue = 0;
for (var i = 0; i < rawBytes.length; i++) {
intValue |= rawBytes[i] << (8 * i);
intValue += rawBytes[i] * $pow(2, 8 * i);
}
/*
Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
Expand All @@ -144,8 +144,8 @@ Return the Number value that corresponds to value.
if ($charAt(type, 0) !== 'U') { // steps 5-6
// Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8.
var bitLength = elementSize * 8;
if (bitLength < 32) {
intValue = (intValue << (32 - bitLength)) >> (32 - bitLength);
if (rawBytes[elementSize - 1] & 0x80) {
intValue -= $pow(2, bitLength);
}
}

Expand Down
6 changes: 3 additions & 3 deletions 2018/RawBytesToNumber.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 2019/RawBytesToNumber.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions 2020/RawBytesToNumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var $pow = GetIntrinsic('%Math.pow%');
var $RangeError = GetIntrinsic('%RangeError%');
var $SyntaxError = GetIntrinsic('%SyntaxError%');
var $TypeError = GetIntrinsic('%TypeError%');
var $Number = GetIntrinsic('%Number%');
var $BigInt = GetIntrinsic('%BigInt%', true);

var hasOwnProperty = require('./HasOwnProperty');
Expand Down Expand Up @@ -142,10 +143,12 @@ Return the Number value that corresponds to value.
return sign * (1 + (mantissa / 0x10000000000000)) * $pow(2, exponent);
}

var Z = isBigInt ? $BigInt : $Number;

// this is common to both branches
var intValue = isBigInt ? $BigInt(0) : 0;
var intValue = Z(0);
for (var i = 0; i < rawBytes.length; i++) {
intValue |= isBigInt ? $BigInt(rawBytes[i]) << $BigInt(8 * i) : rawBytes[i] << (8 * i);
intValue += Z(rawBytes[i]) * Z($pow(2, 8 * i));
}
/*
Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
Expand All @@ -154,9 +157,9 @@ Return the Number value that corresponds to value.
if (!IsUnsignedElementType(type)) { // steps 5-6
// Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8.
var bitLength = elementSize * 8;
if (bitLength < 32) {
var x = isBigInt ? $BigInt(32 - bitLength) : 32 - bitLength;
intValue = (intValue << x) >> x;

if (rawBytes[elementSize - 1] & 0x80) {
intValue -= Z($pow(2, bitLength));
}
}

Expand Down
13 changes: 8 additions & 5 deletions 2021/RawBytesToNumeric.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions 2022/RawBytesToNumeric.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 47eb5eb

Please sign in to comment.