JSPromise.cpp 6.32 KB
Newer Older
weinig@apple.com's avatar
weinig@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "JSPromise.h"

29 30
#if ENABLE(PROMISES)

weinig@apple.com's avatar
weinig@apple.com committed
31 32 33
#include "Error.h"
#include "JSCJSValueInlines.h"
#include "JSCellInlines.h"
34 35 36
#include "JSPromiseConstructor.h"
#include "JSPromiseReaction.h"
#include "Microtask.h"
weinig@apple.com's avatar
weinig@apple.com committed
37 38 39 40 41
#include "SlotVisitorInlines.h"
#include "StructureInlines.h"

namespace JSC {

42
static void triggerPromiseReactions(VM&, JSGlobalObject*, Vector<WriteBarrier<JSPromiseReaction>>&, JSValue);
weinig@apple.com's avatar
weinig@apple.com committed
43 44 45

const ClassInfo JSPromise::s_info = { "Promise", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSPromise) };

46
JSPromise* JSPromise::create(VM& vm, JSGlobalObject* globalObject, JSPromiseConstructor* constructor)
weinig@apple.com's avatar
weinig@apple.com committed
47 48
{
    JSPromise* promise = new (NotNull, allocateCell<JSPromise>(vm.heap)) JSPromise(vm, globalObject->promiseStructure());
49
    promise->finishCreation(vm, constructor);
weinig@apple.com's avatar
weinig@apple.com committed
50 51 52 53 54 55 56 57 58 59
    return promise;
}

Structure* JSPromise::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
    return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}

JSPromise::JSPromise(VM& vm, Structure* structure)
    : JSDestructibleObject(vm, structure)
60
    , m_status(Status::Unresolved)
weinig@apple.com's avatar
weinig@apple.com committed
61 62 63
{
}

64
void JSPromise::finishCreation(VM& vm, JSPromiseConstructor* constructor)
weinig@apple.com's avatar
weinig@apple.com committed
65 66 67
{
    Base::finishCreation(vm);
    ASSERT(inherits(info()));
68 69
    
    m_constructor.set(vm, this, constructor);
weinig@apple.com's avatar
weinig@apple.com committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

void JSPromise::destroy(JSCell* cell)
{
    static_cast<JSPromise*>(cell)->JSPromise::~JSPromise();
}

void JSPromise::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
    JSPromise* thisObject = jsCast<JSPromise*>(cell);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());

    Base::visitChildren(thisObject, visitor);

    visitor.append(&thisObject->m_result);
87 88 89
    visitor.append(&thisObject->m_constructor);
    visitor.append(thisObject->m_resolveReactions.begin(), thisObject->m_resolveReactions.end());
    visitor.append(thisObject->m_rejectReactions.begin(), thisObject->m_rejectReactions.end());
weinig@apple.com's avatar
weinig@apple.com committed
90 91
}

92
void JSPromise::reject(VM& vm, JSValue reason)
weinig@apple.com's avatar
weinig@apple.com committed
93
{
94 95 96
    // 1. If the value of promise's internal slot [[PromiseStatus]] is not "unresolved", return.
    if (m_status != Status::Unresolved)
        return;
weinig@apple.com's avatar
weinig@apple.com committed
97

98
    DeferGC deferGC(vm.heap);
weinig@apple.com's avatar
weinig@apple.com committed
99

100 101 102
    // 2. Let 'reactions' be the value of promise's [[RejectReactions]] internal slot.
    Vector<WriteBarrier<JSPromiseReaction>> reactions;
    reactions.swap(m_rejectReactions);
weinig@apple.com's avatar
weinig@apple.com committed
103

104 105
    // 3. Set the value of promise's [[Result]] internal slot to reason.
    m_result.set(vm, this, reason);
weinig@apple.com's avatar
weinig@apple.com committed
106

107 108
    // 4. Set the value of promise's [[ResolveReactions]] internal slot to undefined.
    m_resolveReactions.clear();
weinig@apple.com's avatar
weinig@apple.com committed
109

110 111 112 113 114 115 116 117
    // 5. Set the value of promise's [[RejectReactions]] internal slot to undefined.
    // NOTE: Handled by the swap above.

    // 6. Set the value of promise's [[PromiseStatus]] internal slot to "has-rejection".
    m_status = Status::HasRejection;

    // 7. Return the result of calling TriggerPromiseReactions(reactions, reason).
    triggerPromiseReactions(vm, globalObject(), reactions, reason);
weinig@apple.com's avatar
weinig@apple.com committed
118 119
}

120
void JSPromise::resolve(VM& vm, JSValue resolution)
weinig@apple.com's avatar
weinig@apple.com committed
121
{
122 123
    // 1. If the value of promise's internal slot [[PromiseStatus]] is not "unresolved", return.
    if (m_status != Status::Unresolved)
weinig@apple.com's avatar
weinig@apple.com committed
124 125
        return;

126
    DeferGC deferGC(vm.heap);
weinig@apple.com's avatar
weinig@apple.com committed
127

128 129 130 131 132 133
    // 2. Let 'reactions' be the value of promise's [[ResolveReactions]] internal slot.
    Vector<WriteBarrier<JSPromiseReaction>> reactions;
    reactions.swap(m_resolveReactions);
    
    // 3. Set the value of promise's [[Result]] internal slot to resolution.
    m_result.set(vm, this, resolution);
weinig@apple.com's avatar
weinig@apple.com committed
134

135 136
    // 4. Set the value of promise's [[ResolveReactions]] internal slot to undefined.
    // NOTE: Handled by the swap above.
weinig@apple.com's avatar
weinig@apple.com committed
137

138 139 140 141 142
    // 5. Set the value of promise's [[RejectReactions]] internal slot to undefined.
    m_rejectReactions.clear();

    // 6. Set the value of promise's [[PromiseStatus]] internal slot to "has-resolution".
    m_status = Status::HasResolution;
weinig@apple.com's avatar
weinig@apple.com committed
143

144 145
    // 7. Return the result of calling TriggerPromiseReactions(reactions, resolution).
    triggerPromiseReactions(vm, globalObject(), reactions, resolution);
weinig@apple.com's avatar
weinig@apple.com committed
146 147
}

148
void JSPromise::appendResolveReaction(VM& vm, JSPromiseReaction* reaction)
weinig@apple.com's avatar
weinig@apple.com committed
149
{
150
    m_resolveReactions.append(WriteBarrier<JSPromiseReaction>(vm, this, reaction));
weinig@apple.com's avatar
weinig@apple.com committed
151 152
}

153
void JSPromise::appendRejectReaction(VM& vm, JSPromiseReaction* reaction)
weinig@apple.com's avatar
weinig@apple.com committed
154
{
155
    m_rejectReactions.append(WriteBarrier<JSPromiseReaction>(vm, this, reaction));
weinig@apple.com's avatar
weinig@apple.com committed
156 157
}

158
void triggerPromiseReactions(VM& vm, JSGlobalObject* globalObject, Vector<WriteBarrier<JSPromiseReaction>>& reactions, JSValue argument)
weinig@apple.com's avatar
weinig@apple.com committed
159
{
160 161 162 163
    // 1. Repeat for each reaction in reactions, in original insertion order
    for (auto& reaction : reactions) {
        // i. Call QueueMicrotask(ExecutePromiseReaction, (reaction, argument)).
        globalObject->queueMicrotask(createExecutePromiseReactionMicrotask(vm, reaction.get(), argument));
weinig@apple.com's avatar
weinig@apple.com committed
164 165
    }
    
166
    // 2. Return.
weinig@apple.com's avatar
weinig@apple.com committed
167 168 169
}

} // namespace JSC
170 171

#endif // ENABLE(PROMISES)