1 | /* |
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 | #pragma once |
27 | |
28 | #include "JSInternalFieldObjectImpl.h" |
29 | |
30 | namespace JSC { |
31 | |
32 | class JSPromiseConstructor; |
33 | class JSPromise : public JSInternalFieldObjectImpl<2> { |
34 | public: |
35 | using Base = JSInternalFieldObjectImpl<2>; |
36 | |
37 | JS_EXPORT_PRIVATE static JSPromise* create(VM&, Structure*); |
38 | static Structure* createStructure(VM&, JSGlobalObject*, JSValue); |
39 | |
40 | DECLARE_EXPORT_INFO; |
41 | |
42 | enum class Status : unsigned { |
43 | Pending = 0, // Making this as 0, so that, we can change the status from Pending to others without masking. |
44 | Fulfilled = 1, |
45 | Rejected = 2, |
46 | }; |
47 | static constexpr uint32_t isHandledFlag = 4; |
48 | static constexpr uint32_t isFirstResolvingFunctionCalledFlag = 8; |
49 | static constexpr uint32_t stateMask = 0b11; |
50 | |
51 | enum class Field : unsigned { |
52 | Flags = 0, |
53 | ReactionsOrResult = 1, |
54 | }; |
55 | static_assert(numberOfInternalFields == 2); |
56 | |
57 | JS_EXPORT_PRIVATE Status status(VM&) const; |
58 | JS_EXPORT_PRIVATE JSValue result(VM&) const; |
59 | JS_EXPORT_PRIVATE bool isHandled(VM&) const; |
60 | |
61 | JS_EXPORT_PRIVATE static JSPromise* resolvedPromise(JSGlobalObject*, JSValue); |
62 | |
63 | JS_EXPORT_PRIVATE void resolve(JSGlobalObject*, JSValue); |
64 | JS_EXPORT_PRIVATE void reject(JSGlobalObject*, JSValue); |
65 | JS_EXPORT_PRIVATE void reject(JSGlobalObject*, Exception*); |
66 | |
67 | struct DeferredData { |
68 | WTF_FORBID_HEAP_ALLOCATION; |
69 | public: |
70 | JSPromise* promise { nullptr }; |
71 | JSFunction* resolve { nullptr }; |
72 | JSFunction* reject { nullptr }; |
73 | }; |
74 | static DeferredData createDeferredData(JSGlobalObject*, JSPromiseConstructor*); |
75 | |
76 | static void visitChildren(JSCell*, SlotVisitor&); |
77 | |
78 | protected: |
79 | JSPromise(VM&, Structure*); |
80 | void finishCreation(VM&); |
81 | |
82 | uint32_t flags() const; |
83 | }; |
84 | |
85 | } // namespace JSC |
86 | |