Skip to content

Commit 3837bf8

Browse files
committed
some %TypedArray% methods fixes
1 parent 6870c2a commit 3837bf8

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Changelog
22
##### Unreleased
3+
- Changed the order of operations in `%TypedArray%.prototype.with` following [proposal-change-array-by-copy/86](https://github.com/tc39/proposal-change-array-by-copy/issues/86)
4+
- Fixed possible multiple call of `ToBigInt` / `ToNumber` conversion of the argument passed to `%TypedArray%.prototype.fill` polyfill
35
- Fixed the kind of error (`TypeError` instead of `Error`) on incorrect `exec` result in `RegExp.prototype.test` polyfill
46
- Fixed dependencies of `{ actual, full, features }/typed-array/at` entries
57
- Added Electron 20.0 compat data mapping
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var toPrimitive = require('../internals/to-primitive');
2+
3+
var $TypeError = TypeError;
4+
5+
// `ToBigInt` abstract operation
6+
// https://tc39.es/ecma262/#sec-tobigint
7+
module.exports = function (argument) {
8+
var prim = toPrimitive(argument, 'number');
9+
if (typeof prim == 'number') throw $TypeError("Can't convert number to bigint");
10+
// eslint-disable-next-line es-x/no-bigint -- safe
11+
return BigInt(prim);
12+
};
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict';
22
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
3-
var call = require('../internals/function-call');
43
var $fill = require('../internals/array-fill');
4+
var toBigInt = require('../internals/to-big-int');
5+
var classof = require('../internals/classof');
6+
var call = require('../internals/function-call');
7+
var uncurryThis = require('../internals/function-uncurry-this');
58

69
var aTypedArray = ArrayBufferViewCore.aTypedArray;
710
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
11+
var slice = uncurryThis(''.slice);
812

913
// `%TypedArray%.prototype.fill` method
1014
// https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill
1115
exportTypedArrayMethod('fill', function fill(value /* , start, end */) {
1216
var length = arguments.length;
13-
return call(
14-
$fill,
15-
aTypedArray(this),
16-
value,
17-
length > 1 ? arguments[1] : undefined,
18-
length > 2 ? arguments[2] : undefined
19-
);
17+
aTypedArray(this);
18+
var actualValue = slice(classof(this), 0, 3) === 'Big' ? toBigInt(value) : +value;
19+
return call($fill, this, actualValue, length > 1 ? arguments[1] : undefined, length > 2 ? arguments[2] : undefined);
2020
});
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
'use strict';
22
var arrayWith = require('../internals/array-with');
33
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
4+
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
5+
var toBigInt = require('../internals/to-big-int');
6+
var classof = require('../internals/classof');
7+
var uncurryThis = require('../internals/function-uncurry-this');
48

59
var aTypedArray = ArrayBufferViewCore.aTypedArray;
610
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
711
var TYPED_ARRAY_CONSTRUCTOR = ArrayBufferViewCore.TYPED_ARRAY_CONSTRUCTOR;
12+
var slice = uncurryThis(''.slice);
813

914
// `%TypedArray%.prototype.with` method
1015
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.with
1116
exportTypedArrayMethod('with', { 'with': function (index, value) {
12-
return arrayWith(aTypedArray(this), this[TYPED_ARRAY_CONSTRUCTOR], index, value);
17+
aTypedArray(this);
18+
var relativeIndex = toIntegerOrInfinity(index);
19+
var actualValue = slice(classof(this), 0, 3) === 'Big' ? toBigInt(value) : +value;
20+
return arrayWith(this, this[TYPED_ARRAY_CONSTRUCTOR], relativeIndex, actualValue);
1321
} }['with']);

tests/tests/es.typed-array.fill.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createConversionChecker } from '../helpers/helpers';
12
import { DESCRIPTORS, GLOBAL, TYPED_ARRAYS } from '../helpers/constants';
23

34
if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.fill', assert => {
@@ -17,5 +18,10 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.fill', assert => {
1718
assert.arrayEqual(new TypedArray(5).fill(5, 6, 1), [0, 0, 0, 0, 0], 'start > end');
1819
assert.arrayEqual(new TypedArray(5).fill(5, -3, 4), [0, 0, 5, 5, 0], 'negative start index');
1920
assert.throws(() => fill.call([0], 1), "isn't generic");
21+
22+
const checker = createConversionChecker(10);
23+
assert.same(new TypedArray(5).fill(checker)[2], 10);
24+
assert.same(checker.$valueOf, 1, 'valueOf calls');
25+
assert.same(checker.$toString, 0, 'toString calls');
2026
}
2127
});

tests/tests/esnext.typed-array.with.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createConversionChecker } from '../helpers/helpers';
12
import { DESCRIPTORS, GLOBAL, TYPED_ARRAYS } from '../helpers/constants';
23

34
if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.with', assert => {
@@ -24,5 +25,10 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.with', assert => {
2425
assert.throws(() => withAt.call(null, 1, 2), TypeError, "isn't generic #1");
2526
assert.throws(() => withAt.call(undefined, 1, 2), TypeError, "isn't generic #2");
2627
assert.throws(() => withAt.call([1, 2], 1, 3), TypeError, "isn't generic #3");
28+
29+
const checker = createConversionChecker(10);
30+
assert.same(new TypedArray(5).with(2, checker)[2], 10);
31+
assert.same(checker.$valueOf, 1, 'valueOf calls');
32+
assert.same(checker.$toString, 0, 'toString calls');
2733
}
2834
});

0 commit comments

Comments
 (0)