1 | /* |
2 | * Copyright (C) 2013-2017 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 | |
29 | #include "BuiltinNames.h" |
30 | #include "Error.h" |
31 | #include "JSCInlines.h" |
32 | #include "JSPromiseConstructor.h" |
33 | #include "Microtask.h" |
34 | |
35 | namespace JSC { |
36 | |
37 | const ClassInfo JSPromise::s_info = { "Promise" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSPromise) }; |
38 | |
39 | JSPromise* JSPromise::create(VM& vm, Structure* structure) |
40 | { |
41 | JSPromise* promise = new (NotNull, allocateCell<JSPromise>(vm.heap)) JSPromise(vm, structure); |
42 | promise->finishCreation(vm); |
43 | return promise; |
44 | } |
45 | |
46 | Structure* JSPromise::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
47 | { |
48 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
49 | } |
50 | |
51 | JSPromise::JSPromise(VM& vm, Structure* structure) |
52 | : Base(vm, structure) |
53 | { |
54 | } |
55 | |
56 | void JSPromise::finishCreation(VM& vm) |
57 | { |
58 | Base::finishCreation(vm); |
59 | putDirect(vm, vm.propertyNames->builtinNames().promiseStatePrivateName(), jsNumber(static_cast<unsigned>(Status::Pending))); |
60 | putDirect(vm, vm.propertyNames->builtinNames().promiseReactionsPrivateName(), jsUndefined()); |
61 | putDirect(vm, vm.propertyNames->builtinNames().promiseResultPrivateName(), jsUndefined()); |
62 | } |
63 | |
64 | void JSPromise::initialize(ExecState* exec, JSGlobalObject* globalObject, JSValue executor) |
65 | { |
66 | JSFunction* initializePromise = globalObject->initializePromiseFunction(); |
67 | CallData callData; |
68 | CallType callType = JSC::getCallData(exec->vm(), initializePromise, callData); |
69 | ASSERT(callType != CallType::None); |
70 | |
71 | MarkedArgumentBuffer arguments; |
72 | arguments.append(executor); |
73 | ASSERT(!arguments.hasOverflowed()); |
74 | call(exec, initializePromise, callType, callData, this, arguments); |
75 | } |
76 | |
77 | auto JSPromise::status(VM& vm) const -> Status |
78 | { |
79 | JSValue value = getDirect(vm, vm.propertyNames->builtinNames().promiseStatePrivateName()); |
80 | ASSERT(value.isUInt32()); |
81 | return static_cast<Status>(value.asUInt32()); |
82 | } |
83 | |
84 | JSValue JSPromise::result(VM& vm) const |
85 | { |
86 | return getDirect(vm, vm.propertyNames->builtinNames().promiseResultPrivateName()); |
87 | } |
88 | |
89 | bool JSPromise::isHandled(VM& vm) const |
90 | { |
91 | JSValue value = getDirect(vm, vm.propertyNames->builtinNames().promiseIsHandledPrivateName()); |
92 | ASSERT(value.isBoolean()); |
93 | return value.asBoolean(); |
94 | } |
95 | |
96 | JSPromise* JSPromise::resolve(JSGlobalObject& globalObject, JSValue value) |
97 | { |
98 | auto* exec = globalObject.globalExec(); |
99 | auto& vm = exec->vm(); |
100 | auto scope = DECLARE_THROW_SCOPE(vm); |
101 | |
102 | auto* promiseResolveFunction = globalObject.promiseResolveFunction(); |
103 | CallData callData; |
104 | auto callType = JSC::getCallData(vm, promiseResolveFunction, callData); |
105 | ASSERT(callType != CallType::None); |
106 | |
107 | MarkedArgumentBuffer arguments; |
108 | arguments.append(value); |
109 | ASSERT(!arguments.hasOverflowed()); |
110 | auto result = call(exec, promiseResolveFunction, callType, callData, globalObject.promiseConstructor(), arguments); |
111 | RETURN_IF_EXCEPTION(scope, nullptr); |
112 | ASSERT(result.inherits<JSPromise>(vm)); |
113 | return jsCast<JSPromise*>(result); |
114 | } |
115 | |
116 | } // namespace JSC |
117 | |