Bug 1170760 part 3. Add an @@species getter on Promise. r=peterv

This commit is contained in:
Boris Zbarsky 2015-11-25 15:48:08 -05:00
parent 6a4cd2a0cf
commit cd3eff21f4
5 changed files with 53 additions and 1 deletions

View File

@ -2979,6 +2979,21 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
else:
unforgeableHolderSetup = None
if self.descriptor.name == "Promise":
speciesSetup = CGGeneric(fill(
"""
JS::Rooted<JSObject*> promiseConstructor(aCx, *interfaceCache);
JS::Rooted<jsid> species(aCx,
SYMBOL_TO_JSID(JS::GetWellKnownSymbol(aCx, JS::SymbolCode::species)));
if (!JS_DefinePropertyById(aCx, promiseConstructor, species, JS::UndefinedHandleValue,
JSPROP_SHARED, Promise::PromiseSpecies, nullptr)) {
$*{failureCode}
}
""",
failureCode=failureCode))
else:
speciesSetup = None
if (self.descriptor.interface.isOnGlobalProtoChain() and
needInterfacePrototypeObject):
makeProtoPrototypeImmutable = CGGeneric(fill(
@ -3004,7 +3019,7 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
return CGList(
[getParentProto, CGGeneric(getConstructorProto), initIds,
prefCache, CGGeneric(call), defineAliases, unforgeableHolderSetup,
makeProtoPrototypeImmutable],
speciesSetup, makeProtoPrototypeImmutable],
"\n").define()

View File

@ -1174,6 +1174,15 @@ Promise::Race(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
return promise.forget();
}
/* static */
bool
Promise::PromiseSpecies(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
{
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
args.rval().set(args.thisv());
return true;
}
void
Promise::AppendNativeHandler(PromiseNativeHandler* aRunnable)
{

View File

@ -198,6 +198,9 @@ public:
Race(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
static bool
PromiseSpecies(JSContext* aCx, unsigned aArgc, JS::Value* aVp);
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
JSObject* GlobalJSObject() const;

View File

@ -11,3 +11,4 @@
support-files = file_promise_and_timeout_ordering.js
[test_promise_and_timeout_ordering_workers.html]
support-files = file_promise_and_timeout_ordering.js
[test_species_getter.html]

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Test for ...</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
var desc = Object.getOwnPropertyDescriptor(Promise, Symbol.species);
assert_not_equals(desc, undefined, "Should have a property");
assert_equals(desc.configurable, true, "Property should be configurable");
assert_equals(desc.enumerable, false, "Property should not be enumerable");
assert_equals(desc.set, undefined, "Should not have a setter");
var getter = desc.get;
var things = [undefined, null, 5, "xyz", Promise, Object];
for (var thing of things) {
assert_equals(getter.call(thing), thing,
"Getter should return its this value");
}
}, "Promise should have an @@species getter that works per spec");
</script>