diff --git a/features.txt b/features.txt index 28a9fbdcd37..6599e733279 100644 --- a/features.txt +++ b/features.txt @@ -105,6 +105,10 @@ iterator-helpers # https://github.com/tc39/proposal-promise-with-resolvers promise-with-resolvers +# Promise.try +# https://github.com/tc39/proposal-promise-try +promise-try + # Set methods # https://github.com/tc39/proposal-set-methods set-methods diff --git a/test/built-ins/Promise/race/resolved-sequence-with-rejections.js b/test/built-ins/Promise/race/resolved-sequence-with-rejections.js index cb8bf5bb988..74e4ad364c3 100644 --- a/test/built-ins/Promise/race/resolved-sequence-with-rejections.js +++ b/test/built-ins/Promise/race/resolved-sequence-with-rejections.js @@ -21,7 +21,7 @@ info: | Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], resultCapability.[[Reject]] »). flags: [async] -includes: [compareArray.js,promiseHelper.js] +includes: [compareArray.js, promiseHelper.js] ---*/ let a = new Promise((_, reject) => reject('a')); diff --git a/test/built-ins/Promise/try/args.js b/test/built-ins/Promise/try/args.js new file mode 100644 index 00000000000..f0a32743797 --- /dev/null +++ b/test/built-ins/Promise/try/args.js @@ -0,0 +1,32 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try forwards arguments +esid: sec-promise.try +features: [promise-try, async] +includes: [compareArray.js] +---*/ + +var sentinel = { sentinel: true }; + +Promise.try(function () { + assert.compareArray( + Array.prototype.slice.call(arguments), + [1, 2, Test262Error, sentinel] + ); + + return sentinel; +}, 1, 2, Test262Error, sentinel) +.then(function(v) { + assert.sameValue(v, sentinel); +}) +.then( + function(v) { + $DONE(); + }, + function() { + $DONE('The promise should not be rejected.'); + } +) + diff --git a/test/built-ins/Promise/try/ctx-ctor-throws.js b/test/built-ins/Promise/try/ctx-ctor-throws.js new file mode 100644 index 00000000000..f8a4e03fd0b --- /dev/null +++ b/test/built-ins/Promise/try/ctx-ctor-throws.js @@ -0,0 +1,16 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + `Promise.try` invoked on a constructor value that throws an error +features: [promise-try] +---*/ + +var CustomPromise = function() { + throw new Test262Error(); +}; + +assert.throws(Test262Error, function() { + Promise.try.call(CustomPromise, function () {}); +}); diff --git a/test/built-ins/Promise/try/ctx-ctor.js b/test/built-ins/Promise/try/ctx-ctor.js new file mode 100644 index 00000000000..45a1391d489 --- /dev/null +++ b/test/built-ins/Promise/try/ctx-ctor.js @@ -0,0 +1,27 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try produces instances of the receiver +esid: sec-promise.try +features: [promise-try, class] +---*/ + +var executor = null; +var callCount = 0; + +class SubPromise extends Promise { + constructor(a) { + super(a); + executor = a; + callCount += 1; + } +} + +var instance = Promise.try.call(SubPromise, function () {}); + +assert.sameValue(instance.promise.constructor, SubPromise); +assert.sameValue(instance.promise instanceof SubPromise, true); + +assert.sameValue(callCount, 1); +assert.sameValue(typeof executor, 'function'); diff --git a/test/built-ins/Promise/try/ctx-non-ctor.js b/test/built-ins/Promise/try/ctx-non-ctor.js new file mode 100644 index 00000000000..380d65af2f3 --- /dev/null +++ b/test/built-ins/Promise/try/ctx-non-ctor.js @@ -0,0 +1,12 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try errors when the receiver is not a constructor +esid: sec-promise.try +features: [promise-try] +---*/ + +assert.throws(TypeError, function() { + Promise.try.call(eval); +}); diff --git a/test/built-ins/Promise/try/ctx-non-object.js b/test/built-ins/Promise/try/ctx-non-object.js new file mode 100644 index 00000000000..2361c91bb04 --- /dev/null +++ b/test/built-ins/Promise/try/ctx-non-object.js @@ -0,0 +1,32 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try errors when the receiver is not an object +esid: sec-promise.try +features: [promise-try] +---*/ + +assert.throws(TypeError, function() { + Promise.try.call(undefined); +}); + +assert.throws(TypeError, function() { + Promise.try.call(null); +}); + +assert.throws(TypeError, function() { + Promise.try.call(86); +}); + +assert.throws(TypeError, function() { + Promise.try.call('string'); +}); + +assert.throws(TypeError, function() { + Promise.try.call(true); +}); + +assert.throws(TypeError, function() { + Promise.try.call(Symbol()); +}); diff --git a/test/built-ins/Promise/try/length.js b/test/built-ins/Promise/try/length.js new file mode 100644 index 00000000000..283ff238e3c --- /dev/null +++ b/test/built-ins/Promise/try/length.js @@ -0,0 +1,14 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: Promise.try `length` property +includes: [propertyHelper.js] +features: [promise-try] +---*/ + +verifyProperty(Promise.try, "length", { + value: 1, + writable: false, + enumerable: false, + configurable: true +}); diff --git a/test/built-ins/Promise/try/name.js b/test/built-ins/Promise/try/name.js new file mode 100644 index 00000000000..79ab45b1468 --- /dev/null +++ b/test/built-ins/Promise/try/name.js @@ -0,0 +1,14 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: Promise.try `name` property +includes: [propertyHelper.js] +features: [promise-try] +---*/ + +verifyProperty(Promise.try, "name", { + value: "try", + writable: false, + enumerable: false, + configurable: true +}); diff --git a/test/built-ins/Promise/try/not-a-constructor.js b/test/built-ins/Promise/try/not-a-constructor.js new file mode 100644 index 00000000000..923f36ae5eb --- /dev/null +++ b/test/built-ins/Promise/try/not-a-constructor.js @@ -0,0 +1,16 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Promise.try does not implement [[Construct]], is not new-able +includes: [isConstructor.js] +features: [Reflect.construct, promise-try] +---*/ + +assert.sameValue(isConstructor(Promise.try), false, 'isConstructor(Promise.all) must return false'); + +assert.throws(TypeError, function () { + new Promise.try(function () {}); +}); + diff --git a/test/built-ins/Promise/try/promise.js b/test/built-ins/Promise/try/promise.js new file mode 100644 index 00000000000..167146e513c --- /dev/null +++ b/test/built-ins/Promise/try/promise.js @@ -0,0 +1,12 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try return value is a Promise +features: [promise-try] +---*/ + +var instance = Promise.try(function () {}); + +assert.sameValue(instance.promise.constructor, Promise); +assert.sameValue(instance.promise instanceof Promise, true); diff --git a/test/built-ins/Promise/try/prop-desc.js b/test/built-ins/Promise/try/prop-desc.js new file mode 100644 index 00000000000..416d6f379b4 --- /dev/null +++ b/test/built-ins/Promise/try/prop-desc.js @@ -0,0 +1,16 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// See LICENSE for details. + +/*--- +author: Jordan Harband +description: Promise.try property descriptor +features: [promise-try] +includes: [propertyHelper.js] +---*/ + +verifyProperty(Promise, 'try', { + value: Promise.try, + writable: true, + enumerable: false, + configurable: true +}) diff --git a/test/built-ins/Promise/try/throws.js b/test/built-ins/Promise/try/throws.js new file mode 100644 index 00000000000..bf93410073e --- /dev/null +++ b/test/built-ins/Promise/try/throws.js @@ -0,0 +1,27 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Promise.try returns a Promise that rejects when the function throws +esid: sec-promise.try +features: [promise-try, async] +---*/ + + +Promise.try(function () { throw new Test262Error(); }) + .then( + function() { + $DONE('The promise should not be fulfilled.'); + }, + function(err) { + assert.throws(Test262Error, function () { throw err; }); + } + ) + .then( + function() { + $DONE(); + }, + function() { + $DONE('The promise should not be rejected.'); + } + ) diff --git a/test/built-ins/Promise/withResolvers/ctx-ctor.js b/test/built-ins/Promise/withResolvers/ctx-ctor.js index 93da4e8b2e9..a650dd251c3 100644 --- a/test/built-ins/Promise/withResolvers/ctx-ctor.js +++ b/test/built-ins/Promise/withResolvers/ctx-ctor.js @@ -4,7 +4,7 @@ /*--- description: Promise.withResolvers produces instances of the receiver esid: sec-promise.withresolvers -features: [promise-with-resolvers] +features: [promise-with-resolvers, class] ---*/ class SubPromise extends Promise {}