blob: 9f182c954b331c48754aaa80d81b4a384561c3dc [file] [log] [blame]
weinig@apple.combd760692013-08-26 19:19:50 +00001/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSPromise.h"
28
weinig@apple.comb7188852013-08-29 22:54:38 +000029#if ENABLE(PROMISES)
30
weinig@apple.combd760692013-08-26 19:19:50 +000031#include "Error.h"
32#include "JSCJSValueInlines.h"
33#include "JSCellInlines.h"
weinig@apple.com349af4e2014-01-03 00:40:16 +000034#include "JSPromiseConstructor.h"
35#include "JSPromiseReaction.h"
36#include "Microtask.h"
weinig@apple.combd760692013-08-26 19:19:50 +000037#include "SlotVisitorInlines.h"
weinig@apple.combd760692013-08-26 19:19:50 +000038#include "StructureInlines.h"
39
40namespace JSC {
41
weinig@apple.com349af4e2014-01-03 00:40:16 +000042static void triggerPromiseReactions(VM&, JSGlobalObject*, Vector<WriteBarrier<JSPromiseReaction>>&, JSValue);
weinig@apple.combd760692013-08-26 19:19:50 +000043
44const ClassInfo JSPromise::s_info = { "Promise", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSPromise) };
45
weinig@apple.com349af4e2014-01-03 00:40:16 +000046JSPromise* JSPromise::create(VM& vm, JSGlobalObject* globalObject, JSPromiseConstructor* constructor)
weinig@apple.combd760692013-08-26 19:19:50 +000047{
48 JSPromise* promise = new (NotNull, allocateCell<JSPromise>(vm.heap)) JSPromise(vm, globalObject->promiseStructure());
weinig@apple.com349af4e2014-01-03 00:40:16 +000049 promise->finishCreation(vm, constructor);
weinig@apple.combd760692013-08-26 19:19:50 +000050 return promise;
51}
52
53Structure* JSPromise::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
54{
55 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
56}
57
58JSPromise::JSPromise(VM& vm, Structure* structure)
59 : JSDestructibleObject(vm, structure)
weinig@apple.com349af4e2014-01-03 00:40:16 +000060 , m_status(Status::Unresolved)
weinig@apple.combd760692013-08-26 19:19:50 +000061{
62}
63
weinig@apple.com349af4e2014-01-03 00:40:16 +000064void JSPromise::finishCreation(VM& vm, JSPromiseConstructor* constructor)
weinig@apple.combd760692013-08-26 19:19:50 +000065{
66 Base::finishCreation(vm);
67 ASSERT(inherits(info()));
weinig@apple.com349af4e2014-01-03 00:40:16 +000068
69 m_constructor.set(vm, this, constructor);
weinig@apple.combd760692013-08-26 19:19:50 +000070}
71
72void JSPromise::destroy(JSCell* cell)
73{
74 static_cast<JSPromise*>(cell)->JSPromise::~JSPromise();
75}
76
77void JSPromise::visitChildren(JSCell* cell, SlotVisitor& visitor)
78{
79 JSPromise* thisObject = jsCast<JSPromise*>(cell);
80 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
81 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
82 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
83
84 Base::visitChildren(thisObject, visitor);
85
weinig@apple.combd760692013-08-26 19:19:50 +000086 visitor.append(&thisObject->m_result);
weinig@apple.com349af4e2014-01-03 00:40:16 +000087 visitor.append(&thisObject->m_constructor);
88 visitor.append(thisObject->m_resolveReactions.begin(), thisObject->m_resolveReactions.end());
89 visitor.append(thisObject->m_rejectReactions.begin(), thisObject->m_rejectReactions.end());
weinig@apple.combd760692013-08-26 19:19:50 +000090}
91
weinig@apple.com349af4e2014-01-03 00:40:16 +000092void JSPromise::reject(VM& vm, JSValue reason)
weinig@apple.combd760692013-08-26 19:19:50 +000093{
weinig@apple.com349af4e2014-01-03 00:40:16 +000094 // 1. If the value of promise's internal slot [[PromiseStatus]] is not "unresolved", return.
95 if (m_status != Status::Unresolved)
weinig@apple.combd760692013-08-26 19:19:50 +000096 return;
weinig@apple.combd760692013-08-26 19:19:50 +000097
weinig@apple.com349af4e2014-01-03 00:40:16 +000098 DeferGC deferGC(vm.heap);
99
100 // 2. Let 'reactions' be the value of promise's [[RejectReactions]] internal slot.
101 Vector<WriteBarrier<JSPromiseReaction>> reactions;
102 reactions.swap(m_rejectReactions);
103
104 // 3. Set the value of promise's [[Result]] internal slot to reason.
105 m_result.set(vm, this, reason);
106
107 // 4. Set the value of promise's [[ResolveReactions]] internal slot to undefined.
108 m_resolveReactions.clear();
109
110 // 5. Set the value of promise's [[RejectReactions]] internal slot to undefined.
111 // NOTE: Handled by the swap above.
112
113 // 6. Set the value of promise's [[PromiseStatus]] internal slot to "has-rejection".
114 m_status = Status::HasRejection;
115
116 // 7. Return the result of calling TriggerPromiseReactions(reactions, reason).
117 triggerPromiseReactions(vm, globalObject(), reactions, reason);
118}
119
120void JSPromise::resolve(VM& vm, JSValue resolution)
121{
122 // 1. If the value of promise's internal slot [[PromiseStatus]] is not "unresolved", return.
123 if (m_status != Status::Unresolved)
weinig@apple.combd760692013-08-26 19:19:50 +0000124 return;
weinig@apple.com349af4e2014-01-03 00:40:16 +0000125
126 DeferGC deferGC(vm.heap);
127
128 // 2. Let 'reactions' be the value of promise's [[ResolveReactions]] internal slot.
129 Vector<WriteBarrier<JSPromiseReaction>> reactions;
130 reactions.swap(m_resolveReactions);
131
132 // 3. Set the value of promise's [[Result]] internal slot to resolution.
133 m_result.set(vm, this, resolution);
134
135 // 4. Set the value of promise's [[ResolveReactions]] internal slot to undefined.
136 // NOTE: Handled by the swap above.
137
138 // 5. Set the value of promise's [[RejectReactions]] internal slot to undefined.
139 m_rejectReactions.clear();
140
141 // 6. Set the value of promise's [[PromiseStatus]] internal slot to "has-resolution".
142 m_status = Status::HasResolution;
143
144 // 7. Return the result of calling TriggerPromiseReactions(reactions, resolution).
145 triggerPromiseReactions(vm, globalObject(), reactions, resolution);
weinig@apple.combd760692013-08-26 19:19:50 +0000146}
147
weinig@apple.com349af4e2014-01-03 00:40:16 +0000148void JSPromise::appendResolveReaction(VM& vm, JSPromiseReaction* reaction)
weinig@apple.combd760692013-08-26 19:19:50 +0000149{
weinig@apple.com349af4e2014-01-03 00:40:16 +0000150 m_resolveReactions.append(WriteBarrier<JSPromiseReaction>(vm, this, reaction));
weinig@apple.combd760692013-08-26 19:19:50 +0000151}
152
weinig@apple.com349af4e2014-01-03 00:40:16 +0000153void JSPromise::appendRejectReaction(VM& vm, JSPromiseReaction* reaction)
weinig@apple.combd760692013-08-26 19:19:50 +0000154{
weinig@apple.com349af4e2014-01-03 00:40:16 +0000155 m_rejectReactions.append(WriteBarrier<JSPromiseReaction>(vm, this, reaction));
weinig@apple.combd760692013-08-26 19:19:50 +0000156}
157
weinig@apple.com349af4e2014-01-03 00:40:16 +0000158void triggerPromiseReactions(VM& vm, JSGlobalObject* globalObject, Vector<WriteBarrier<JSPromiseReaction>>& reactions, JSValue argument)
weinig@apple.combd760692013-08-26 19:19:50 +0000159{
weinig@apple.com349af4e2014-01-03 00:40:16 +0000160 // 1. Repeat for each reaction in reactions, in original insertion order
161 for (auto& reaction : reactions) {
162 // i. Call QueueMicrotask(ExecutePromiseReaction, (reaction, argument)).
163 globalObject->queueMicrotask(createExecutePromiseReactionMicrotask(vm, reaction.get(), argument));
weinig@apple.combd760692013-08-26 19:19:50 +0000164 }
165
weinig@apple.com349af4e2014-01-03 00:40:16 +0000166 // 2. Return.
weinig@apple.combd760692013-08-26 19:19:50 +0000167}
168
169} // namespace JSC
weinig@apple.comb7188852013-08-29 22:54:38 +0000170
171#endif // ENABLE(PROMISES)