Skip to content

Commit

Permalink
add a fix of Bun SuppressedError extra arguments support and arity
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Mar 18, 2024
1 parent f696edb commit cdc614d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/core-js-compat/src/data.mjs
Expand Up @@ -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',
Expand Down
24 changes: 21 additions & 3 deletions 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');
Expand All @@ -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');
Expand All @@ -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
});
4 changes: 3 additions & 1 deletion tests/compat/tests.js
Expand Up @@ -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;
Expand Down

0 comments on commit cdc614d

Please sign in to comment.