JSPromisePrototype.cpp [plain text]
#include "config.h"
#include "JSPromisePrototype.h"
#if ENABLE(PROMISES)
#include "Error.h"
#include "JSCJSValueInlines.h"
#include "JSCellInlines.h"
#include "JSGlobalObject.h"
#include "JSPromise.h"
#include "JSPromiseDeferred.h"
#include "JSPromiseFunctions.h"
#include "JSPromiseReaction.h"
#include "Microtask.h"
#include "StructureInlines.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSPromisePrototype);
static EncodedJSValue JSC_HOST_CALL JSPromisePrototypeFuncThen(ExecState*);
}
#include "JSPromisePrototype.lut.h"
namespace JSC {
const ClassInfo JSPromisePrototype::s_info = { "PromisePrototype", &JSNonFinalObject::s_info, 0, ExecState::promisePrototypeTable, CREATE_METHOD_TABLE(JSPromisePrototype) };
JSPromisePrototype* JSPromisePrototype::create(ExecState* exec, JSGlobalObject*, Structure* structure)
{
VM& vm = exec->vm();
JSPromisePrototype* object = new (NotNull, allocateCell<JSPromisePrototype>(vm.heap)) JSPromisePrototype(exec, structure);
object->finishCreation(vm, structure);
return object;
}
Structure* JSPromisePrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
JSPromisePrototype::JSPromisePrototype(ExecState* exec, Structure* structure)
: JSNonFinalObject(exec->vm(), structure)
{
}
void JSPromisePrototype::finishCreation(VM& vm, Structure*)
{
Base::finishCreation(vm);
}
bool JSPromisePrototype::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
return getStaticFunctionSlot<JSObject>(exec, ExecState::promisePrototypeTable(exec->vm()), jsCast<JSPromisePrototype*>(object), propertyName, slot);
}
EncodedJSValue JSC_HOST_CALL JSPromisePrototypeFuncThen(ExecState* exec)
{
JSPromise* promise = jsDynamicCast<JSPromise*>(exec->thisValue());
if (!promise)
return JSValue::encode(throwTypeError(exec));
JSValue C = promise->get(exec, exec->propertyNames().constructor);
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSValue deferred = createJSPromiseDeferredFromConstructor(exec, C);
if (exec->hadException())
return JSValue::encode(jsUndefined());
VM& vm = exec->vm();
JSGlobalObject* globalObject = promise->globalObject();
JSValue onRejected = exec->argument(1);
CallData onRejectedCallData;
CallType onRejectedCallType = getCallData(onRejected, onRejectedCallData);
JSObject* rejectionHandler = (onRejectedCallType == CallTypeNone) ? createThrowerFunction(vm, globalObject) : asObject(onRejected);
JSValue onFulfilled = exec->argument(0);
CallData onFulfilledCallData;
CallType onFulfilledCallType = getCallData(onFulfilled, onFulfilledCallData);
JSObject* fulfillmentHandler = (onFulfilledCallType == CallTypeNone) ? createIdentifyFunction(vm, globalObject) : asObject(onFulfilled);
JSObject* resolutionHandler = createPromiseResolutionHandlerFunction(vm, globalObject);
resolutionHandler->putDirect(vm, vm.propertyNames->promisePrivateName, promise);
resolutionHandler->putDirect(vm, vm.propertyNames->fulfillmentHandlerPrivateName, fulfillmentHandler);
resolutionHandler->putDirect(vm, vm.propertyNames->rejectionHandlerPrivateName, rejectionHandler);
JSPromiseReaction* resolveReaction = JSPromiseReaction::create(vm, jsCast<JSPromiseDeferred*>(deferred), resolutionHandler);
JSPromiseReaction* rejectReaction = JSPromiseReaction::create(vm, jsCast<JSPromiseDeferred*>(deferred), rejectionHandler);
switch (promise->status()) {
case JSPromise::Status::Unresolved: {
promise->appendResolveReaction(vm, resolveReaction);
promise->appendRejectReaction(vm, rejectReaction);
break;
}
case JSPromise::Status::HasResolution: {
JSValue resolution = promise->result();
globalObject->queueMicrotask(createExecutePromiseReactionMicrotask(vm, resolveReaction, resolution));
break;
}
case JSPromise::Status::HasRejection: {
JSValue reason = promise->result();
globalObject->queueMicrotask(createExecutePromiseReactionMicrotask(vm, rejectReaction, reason));
break;
}
}
return JSValue::encode(jsCast<JSPromiseDeferred*>(deferred)->promise());
}
}
#endif // ENABLE(PROMISES)