From cdc614d433378100f0b3cbfa868eda8068e3eac7 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Mon, 18 Mar 2024 15:39:57 +0700 Subject: [PATCH] add a fix of Bun `SuppressedError` extra arguments support and arity https://github.com/oven-sh/bun/issues/9283 https://github.com/oven-sh/bun/issues/9282 --- CHANGELOG.md | 1 + packages/core-js-compat/src/data.mjs | 5 +++- .../esnext.suppressed-error.constructor.js | 24 ++++++++++++++++--- tests/compat/tests.js | 4 +++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abf23e76a6aa..5b3d8dd49643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fixed the order of validations in `Array.from`, [#1331](https://github.com/zloirock/core-js/pull/1331), thanks [**@minseok-choe**](https://github.com/minseok-choe) - Added a fix of [Bun `queueMicrotask` arity](https://github.com/oven-sh/bun/issues/9249) - Added a fix of [Bun `URL.canParse` arity](https://github.com/oven-sh/bun/issues/9250) +- Added a fix of Bun `SuppressedError` [extra arguments support](https://github.com/oven-sh/bun/issues/9283) and [arity](https://github.com/oven-sh/bun/issues/9282) - Compat data improvements: - [`value` argument of `URLSearchParams.prototype.{ has, delete }`](https://url.spec.whatwg.org/#dom-urlsearchparams-delete) marked as supported [from Bun 1.0.31](https://github.com/oven-sh/bun/issues/9263) - Added React Native 0.74 Hermes compat data, `Array.prototype.{ toSpliced, toReversed, with }` and `atob` marked as supported diff --git a/packages/core-js-compat/src/data.mjs b/packages/core-js-compat/src/data.mjs index fad2242b449a..6bb4f0aa4917 100644 --- a/packages/core-js-compat/src/data.mjs +++ b/packages/core-js-compat/src/data.mjs @@ -1913,7 +1913,10 @@ export const data = { // TODO: Remove from `core-js@4` 'esnext.aggregate-error': null, 'esnext.suppressed-error.constructor': { - bun: '1.0.23', + // Bun ~ 1.0.33 issues + // https://github.com/oven-sh/bun/issues/9282 + // https://github.com/oven-sh/bun/issues/9283 + // bun: '1.0.23', }, 'esnext.array.from-async': { bun: '0.3.0', diff --git a/packages/core-js/modules/esnext.suppressed-error.constructor.js b/packages/core-js/modules/esnext.suppressed-error.constructor.js index 1fe4919f449e..c72bbe6f131b 100644 --- a/packages/core-js/modules/esnext.suppressed-error.constructor.js +++ b/packages/core-js/modules/esnext.suppressed-error.constructor.js @@ -1,5 +1,6 @@ 'use strict'; var $ = require('../internals/export'); +var globalThis = require('../internals/global'); var isPrototypeOf = require('../internals/object-is-prototype-of'); var getPrototypeOf = require('../internals/object-get-prototype-of'); var setPrototypeOf = require('../internals/object-set-prototype-of'); @@ -10,15 +11,30 @@ var createPropertyDescriptor = require('../internals/create-property-descriptor' var installErrorStack = require('../internals/error-stack-install'); var normalizeStringArgument = require('../internals/normalize-string-argument'); var wellKnownSymbol = require('../internals/well-known-symbol'); +var fails = require('../internals/fails'); +var IS_PURE = require('../internals/is-pure'); +var NativeSuppressedError = globalThis.SuppressedError; var TO_STRING_TAG = wellKnownSymbol('toStringTag'); var $Error = Error; +// https://github.com/oven-sh/bun/issues/9282 +var WRONG_ARITY = !!NativeSuppressedError && NativeSuppressedError.length !== 3; + +// https://github.com/oven-sh/bun/issues/9283 +var EXTRA_ARGS_SUPPORT = !!NativeSuppressedError && fails(function () { + return NativeSuppressedError(1, 2, 3, { cause: 4 }).cause === 4; +}); + +var PATCH = WRONG_ARITY || EXTRA_ARGS_SUPPORT; + var $SuppressedError = function SuppressedError(error, suppressed, message) { var isInstance = isPrototypeOf(SuppressedErrorPrototype, this); var that; if (setPrototypeOf) { - that = setPrototypeOf(new $Error(), isInstance ? getPrototypeOf(this) : SuppressedErrorPrototype); + that = PATCH && (!isInstance || getPrototypeOf(this) === SuppressedErrorPrototype) + ? new NativeSuppressedError() + : setPrototypeOf(new $Error(), isInstance ? getPrototypeOf(this) : SuppressedErrorPrototype); } else { that = isInstance ? this : create(SuppressedErrorPrototype); createNonEnumerableProperty(that, TO_STRING_TAG, 'Error'); @@ -33,14 +49,16 @@ var $SuppressedError = function SuppressedError(error, suppressed, message) { if (setPrototypeOf) setPrototypeOf($SuppressedError, $Error); else copyConstructorProperties($SuppressedError, $Error, { name: true }); -var SuppressedErrorPrototype = $SuppressedError.prototype = create($Error.prototype, { +var SuppressedErrorPrototype = $SuppressedError.prototype = PATCH ? NativeSuppressedError.prototype : create($Error.prototype, { constructor: createPropertyDescriptor(1, $SuppressedError), message: createPropertyDescriptor(1, ''), name: createPropertyDescriptor(1, 'SuppressedError') }); +if (PATCH && !IS_PURE) SuppressedErrorPrototype.constructor = $SuppressedError; + // `SuppressedError` constructor // https://github.com/tc39/proposal-explicit-resource-management -$({ global: true, constructor: true, arity: 3 }, { +$({ global: true, constructor: true, arity: 3, forced: PATCH }, { SuppressedError: $SuppressedError }); diff --git a/tests/compat/tests.js b/tests/compat/tests.js index 7f06686d6f22..3426cd4f4488 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1511,7 +1511,9 @@ GLOBAL.tests = { && set[Symbol.toStringTag]; }], 'esnext.suppressed-error.constructor': function () { - return typeof SuppressedError == 'function'; + return typeof SuppressedError == 'function' + && SuppressedError.length === 3 + && SuppressedError(1, 2, 3, { cause: 4 }).cause !== 4; }, 'esnext.array.from-async': function () { return Array.fromAsync;