Reject exceptional input to `isConstructor` (#3748)

* Reject exceptional input to `isConstructor`

Prior to this commit, the `isConstructor` harness function would return
`false` when invoked with a value that lacked a [[Call]] internal
method. While it's true that such values are not constructors, there are
no tests which benefit from using `isConstructor` to make such an
assertion.

Extend `isConstructor` to throw an error when invoked with a
non-function object. Update a test which was misleadingly invoking the
function with the value `undefined`.

* fixup! Reject exceptional input to `isConstructor`
This commit is contained in:
jugglinmike 2022-12-15 22:53:34 -05:00 committed by GitHub
parent dac6956348
commit 5093c3037d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 13 deletions

View File

@ -8,6 +8,10 @@ defines: [isConstructor]
---*/ ---*/
function isConstructor(f) { function isConstructor(f) {
if (typeof f !== "function") {
throw new Test262Error("isConstructor invoked with a non-function value");
}
try { try {
Reflect.construct(function(){}, [], f); Reflect.construct(function(){}, [], f);
} catch (e) { } catch (e) {

View File

@ -40,10 +40,6 @@ var expected = [
var then = Promise.prototype.then; var then = Promise.prototype.then;
Promise.prototype.then = function(resolve, reject) { Promise.prototype.then = function(resolve, reject) {
assert.sameValue(isConstructor(reject), false, 'isConstructor(reject) must return false');
assert.throws(TypeError, () => {
new reject();
}, '`new reject()` throws TypeError');
assert.sameValue( assert.sameValue(
resolve.length, resolve.length,
@ -57,6 +53,13 @@ Promise.prototype.then = function(resolve, reject) {
); );
if (calls === 0) { if (calls === 0) {
assert.throws(MyError, resolve, '`resolve()` throws `MyError`'); assert.throws(MyError, resolve, '`resolve()` throws `MyError`');
assert.sameValue(arguments.length, 1, '`then` invoked with one argument');
} else {
assert.sameValue(isConstructor(reject), false, 'isConstructor(reject) must return false');
assert.throws(TypeError, () => {
new reject();
}, '`new reject()` throws TypeError');
assert.sameValue(arguments.length, 2, '`then` invoked with two arguments');
} }
calls += 1; calls += 1;

View File

@ -13,16 +13,16 @@ features: [generators, Reflect.construct]
assert.sameValue(typeof isConstructor, "function"); assert.sameValue(typeof isConstructor, "function");
assert.sameValue(isConstructor(), false); assert.throws(Test262Error, () => isConstructor(), "no argument");
assert.sameValue(isConstructor(undefined), false); assert.throws(Test262Error, () => isConstructor(undefined), "undefined");
assert.sameValue(isConstructor(null), false); assert.throws(Test262Error, () => isConstructor(null), "null");
assert.sameValue(isConstructor(123), false); assert.throws(Test262Error, () => isConstructor(123), "number");
assert.sameValue(isConstructor(true), false); assert.throws(Test262Error, () => isConstructor(true), "boolean - true");
assert.sameValue(isConstructor(false), false); assert.throws(Test262Error, () => isConstructor(false), "boolean - false");
assert.sameValue(isConstructor("string"), false); assert.throws(Test262Error, () => isConstructor("string"), "string");
assert.sameValue(isConstructor({}), false); assert.throws(Test262Error, () => isConstructor({}), "Object instance");
assert.sameValue(isConstructor([]), false); assert.throws(Test262Error, () => isConstructor([]), "Array instance");
assert.sameValue(isConstructor(function(){}), true); assert.sameValue(isConstructor(function(){}), true);
assert.sameValue(isConstructor(function*(){}), false); assert.sameValue(isConstructor(function*(){}), false);