Bug 1145058 - Annotate the SpeciesConstructor utility function with step-by-step comment numbering to make clear where we do (and do not!) follow the spec. Also fix issues related to our half-pretense of implementing this method without having first implemented the well-known @@species symbol. r=efaust

This commit is contained in:
Jeff Walden 2015-03-25 23:33:49 -04:00
parent 88c59897fd
commit 971697a3c0
2 changed files with 37 additions and 12 deletions

View File

@ -154,13 +154,35 @@ function GetIterator(obj, method) {
return iterator;
}
// ES6 draft 20150317 7.3.20.
function SpeciesConstructor(obj, defaultConstructor) {
var C = obj.constructor;
if (C === undefined) {
// Step 1.
assert(IsObject(obj), "not passed an object");
// Steps 2-3.
var ctor = obj.constructor;
// Step 4.
if (ctor === undefined)
return defaultConstructor;
}
if (!IsConstructor(C)) {
ThrowError(JSMSG_NOT_CONSTRUCTOR, DecompileArg(1, C));
}
return C;
// Step 5.
if (!IsObject(ctor))
ThrowError(JSMSG_NOT_NONNULL_OBJECT, "object's 'constructor' property");
// Steps 6-7. We don't yet implement @@species and Symbol.species, so we
// don't implement this correctly right now. Somebody fix this!
var s = /* ctor[Symbol.species] */ undefined;
// Step 8.
if (s === undefined || s === null)
return defaultConstructor;
// Step 9.
if (IsConstructor(s))
return s;
// Step 10.
ThrowError(JSMSG_NOT_CONSTRUCTOR,
"@@species property of object's constructor");
}

View File

@ -61,12 +61,15 @@ for (var constructor of constructors) {
strConstructor.constructor = "not a constructor";
strConstructor.slice(123);
}, TypeError, "Assert that we have an invalid constructor");
assertThrowsInstanceOf(() => {
var mathConstructor = new constructor;
mathConstructor.constructor = Math.sin;
mathConstructor.slice(123);
}, TypeError, "Assert that we have an invalid constructor");
// If obj.constructor[@@species] is undefined or null -- which it has to be
// if we don't implement @@species -- then the default constructor is used.
var mathConstructor = new constructor(8);
mathConstructor.constructor = Math.sin;
assertDeepEq(mathConstructor.slice(4), new constructor(4));
assertEq("species" in Symbol, false,
"you've implemented @@species -- add real tests here!");
}
if (typeof reportCompare === "function")