JSPromiseReaction.cpp 6.03 KB
Newer Older
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 29 30 31 32 33 34 35 36 37 38 39
/*
 * 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 "JSPromiseReaction.h"

#include "Error.h"
#include "JSCJSValueInlines.h"
#include "JSCellInlines.h"
#include "JSGlobalObject.h"
#include "JSPromiseDeferred.h"
#include "Microtask.h"
#include "SlotVisitorInlines.h"
#include "StrongInlines.h"

namespace JSC {

40
class ExecutePromiseReactionMicrotask final : public Microtask {
41 42 43 44 45 46 47 48 49 50 51 52
public:
    ExecutePromiseReactionMicrotask(VM& vm, JSPromiseReaction* reaction, JSValue argument)
    {
        m_reaction.set(vm, reaction);
        m_argument.set(vm, argument);
    }

    virtual ~ExecutePromiseReactionMicrotask()
    {
    }

private:
53
    virtual void run(ExecState*) override;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    Strong<JSPromiseReaction> m_reaction;
    Strong<Unknown> m_argument;
};

PassRefPtr<Microtask> createExecutePromiseReactionMicrotask(VM& vm, JSPromiseReaction* reaction, JSValue argument)
{
    return adoptRef(new ExecutePromiseReactionMicrotask(vm, reaction, argument));
}

void ExecutePromiseReactionMicrotask::run(ExecState* exec)
{
    // 1. Let 'deferred' be reaction.[[Deferred]].
    JSPromiseDeferred* deferred = m_reaction->deferred();
    
    // 2. Let 'handler' be reaction.[[Handler]].
    JSValue handler = m_reaction->handler();

    // 3. Let 'handlerResult' be the result of calling the [[Call]] internal method of
    //    handler passing undefined as thisArgument and a List containing argument as
    //    argumentsList.

    CallData handlerCallData;
    CallType handlerCallType = getCallData(handler, handlerCallData);
    ASSERT(handlerCallType != CallTypeNone);

    MarkedArgumentBuffer handlerArguments;
    handlerArguments.append(m_argument.get());

    JSValue handlerResult = call(exec, handler, handlerCallType, handlerCallData, jsUndefined(), handlerArguments);

    // 4. If handlerResult is an abrupt completion, return the result of calling the
    //    [[Call]] internal method of deferred.[[Reject]] passing undefined as thisArgument
    //    and a List containing handlerResult.[[value]] as argumentsList.
    if (exec->hadException()) {
        JSValue exception = exec->exception();
        exec->clearException();

92
        performDeferredReject(exec, deferred, exception);
93 94 95 96 97 98 99 100 101
    }
    
    // 5. Let 'handlerResult' be handlerResult.[[value]].
    // Note: Nothing to do.

    // 6. If SameValue(handlerResult, deferred.[[Promise]]) is true,
    if (sameValue(exec, handlerResult, deferred->promise())) {
        // i. Let 'selfResolutionError' be a newly-created TypeError object.
        JSObject* selfResolutionError = createTypeError(exec, ASCIILiteral("Resolve a promise with itself"));
102

103 104
        // ii. Return the result of calling the [[Call]] internal method of deferred.[[Reject]] passing
        //     undefined as thisArgument and a List containing selfResolutionError as argumentsList.
105
        performDeferredReject(exec, deferred, selfResolutionError);
106 107 108 109 110 111 112 113 114 115 116 117 118
    }

    // 7. Let 'updateResult' be the result of calling UpdateDeferredFromPotentialThenable(handlerResult, deferred).
    ThenableStatus updateResult = updateDeferredFromPotentialThenable(exec, handlerResult, deferred);
    
    // 8. ReturnIfAbrupt(updateResult).
    if (exec->hadException())
        return;

    // 9. If 'updateResult' is "not a thenable",
    if (updateResult == NotAThenable) {
        // i. Return the result of calling the [[Call]] internal method of deferred.[[Resolve]]
        //    passing undefined as thisArgument and a List containing handlerResult as argumentsList.
119
        performDeferredResolve(exec, deferred, handlerResult);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }
}


const ClassInfo JSPromiseReaction::s_info = { "JSPromiseReaction", 0, 0, 0, CREATE_METHOD_TABLE(JSPromiseReaction) };

JSPromiseReaction* JSPromiseReaction::create(VM& vm, JSPromiseDeferred* deferred, JSValue handler)
{
    JSPromiseReaction* promiseReaction = new (NotNull, allocateCell<JSPromiseReaction>(vm.heap)) JSPromiseReaction(vm);
    promiseReaction->finishCreation(vm, deferred, handler);
    return promiseReaction;
}

JSPromiseReaction::JSPromiseReaction(VM& vm)
    : Base(vm, vm.promiseReactionStructure.get())
{
}

void JSPromiseReaction::finishCreation(VM& vm, JSPromiseDeferred* deferred, JSValue handler)
{
    Base::finishCreation(vm);
    m_deferred.set(vm, this, deferred);
    m_handler.set(vm, this, handler);
}

void JSPromiseReaction::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
    JSPromiseReaction* thisObject = jsCast<JSPromiseReaction*>(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_deferred);
    visitor.append(&thisObject->m_handler);
}

} // namespace JSC