Skip to content

Commit

Permalink
add a fix of Safari bug with double call of constructor in `Array.fro…
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 11, 2024
1 parent 92f8618 commit 5b908c2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,8 @@
- Added [`URL.parse`](https://url.spec.whatwg.org/#dom-url-parse), [url/825](https://github.com/whatwg/url/pull/825)
- [`RegExp.escape`](https://github.com/tc39/proposal-regex-escaping) [moved to hex-escape semantics](https://github.com/tc39/proposal-regex-escaping/pull/67)
- Some minor updates of [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) Stage 3 proposal like [explicit-resource-management/217](https://github.com/tc39/proposal-explicit-resource-management/pull/217)
- Engines bugs fixes:
- Added a fix of [Safari bug with double call of constructor in `Array.fromAsync`](https://bugs.webkit.org/show_bug.cgi?id=271703)
- Compat data improvements:
- [`URL.parse`](https://url.spec.whatwg.org/#dom-url-parse) added and marked as supported [from FF 126](https://bugzilla.mozilla.org/show_bug.cgi?id=1887611)
- `URL.canParse` fixed and marked as supported [from Bun 1.1.0](https://github.com/oven-sh/bun/pull/9710)
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -1982,11 +1982,13 @@ export const data = {
// bun: '1.0.23',
},
'esnext.array.from-async': {
bun: '0.3.0',
// https://bugs.webkit.org/show_bug.cgi?id=271703
bun: '1.1.2', // bun: '0.3.0',
chrome: '121',
deno: '1.38',
firefox: '115',
safari: '16.4',
// https://bugs.webkit.org/show_bug.cgi?id=271703
// safari: '16.4',
},
// TODO: Remove from `core-js@4`
'esnext.array.at': null,
Expand Down
14 changes: 13 additions & 1 deletion packages/core-js/modules/esnext.array.from-async.js
@@ -1,9 +1,21 @@
'use strict';
var $ = require('../internals/export');
var fromAsync = require('../internals/array-from-async');
var fails = require('../internals/fails');

var nativeFromAsync = Array.fromAsync;
// https://bugs.webkit.org/show_bug.cgi?id=271703
var INCORRECT_CONSTRUCTURING = !nativeFromAsync || fails(function () {
var counter = 0;
nativeFromAsync.call(function () {
counter++;
return [];
}, { length: 0 });
return counter !== 1;
});

// `Array.fromAsync` method
// https://github.com/tc39/proposal-array-from-async
$({ target: 'Array', stat: true }, {
$({ target: 'Array', stat: true, forced: INCORRECT_CONSTRUCTURING }, {
fromAsync: fromAsync
});
8 changes: 7 additions & 1 deletion tests/compat/tests.js
Expand Up @@ -1525,7 +1525,13 @@ GLOBAL.tests = {
&& SuppressedError(1, 2, 3, { cause: 4 }).cause !== 4;
},
'esnext.array.from-async': function () {
return Array.fromAsync;
// https://bugs.webkit.org/show_bug.cgi?id=271703
var counter = 0;
Array.fromAsync.call(function () {
counter++;
return [];
}, { length: 0 });
return counter === 1;
},
'esnext.array.filter-reject': function () {
return [].filterReject;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit-global/esnext.array.from-async.js
Expand Up @@ -10,6 +10,15 @@ QUnit.test('Array.fromAsync', assert => {
assert.looksNative(fromAsync);
assert.nonEnumerable(Array, 'fromAsync');

let counter = 0;
// eslint-disable-next-line prefer-arrow-callback -- constructor
fromAsync.call(function () {
counter++;
return [];
}, { length: 0 });

assert.same(counter, 1, 'proper number of constructor calling');

function C() { /* empty */ }

return fromAsync(createAsyncIterable([1, 2, 3]), it => it ** 2).then(it => {
Expand Down
9 changes: 9 additions & 0 deletions tests/unit-pure/esnext.array.from-async.js
Expand Up @@ -9,6 +9,15 @@ QUnit.test('Array.fromAsync', assert => {
assert.arity(fromAsync, 1);
assert.name(fromAsync, 'fromAsync');

let counter = 0;
// eslint-disable-next-line prefer-arrow-callback -- constructor
fromAsync.call(function () {
counter++;
return [];
}, { length: 0 });

assert.same(counter, 1, 'proper number of constructor calling');

function C() { /* empty */ }

return fromAsync(createAsyncIterable([1, 2, 3]), it => it ** 2).then(it => {
Expand Down

0 comments on commit 5b908c2

Please sign in to comment.