V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
promise.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_OBJECTS_PROMISE_H_
6 #define V8_OBJECTS_PROMISE_H_
7 
8 #include "src/objects/microtask.h"
9 
10 // Has to be the last include (doesn't have include guards):
11 #include "src/objects/object-macros.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class JSPromise;
17 
18 // Struct to hold state required for PromiseReactionJob. See the comment on the
19 // PromiseReaction below for details on how this is being managed to reduce the
20 // memory and allocation overhead. This is the base class for the concrete
21 //
22 // - PromiseFulfillReactionJobTask
23 // - PromiseRejectReactionJobTask
24 //
25 // classes, which are used to represent either reactions, and we distinguish
26 // them by their instance types.
28  public:
29  DECL_ACCESSORS(argument, Object)
30  DECL_ACCESSORS2(context, Context)
31  DECL_ACCESSORS(handler, HeapObject)
32  // [promise_or_capability]: Either a JSPromise (in case of native promises),
33  // a PromiseCapability (general case), or undefined (in case of await).
34  DECL_ACCESSORS(promise_or_capability, HeapObject)
35 
36  static const int kArgumentOffset = Microtask::kHeaderSize;
37  static const int kContextOffset = kArgumentOffset + kPointerSize;
38  static const int kHandlerOffset = kContextOffset + kPointerSize;
39  static const int kPromiseOrCapabilityOffset = kHandlerOffset + kPointerSize;
40  static const int kSize = kPromiseOrCapabilityOffset + kPointerSize;
41 
42  // Dispatched behavior.
43  DECL_CAST(PromiseReactionJobTask)
44  DECL_VERIFIER(PromiseReactionJobTask)
45 
46  private:
47  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseReactionJobTask);
48 };
49 
50 // Struct to hold state required for a PromiseReactionJob of type "Fulfill".
52  public:
53  // Dispatched behavior.
55  DECL_PRINTER(PromiseFulfillReactionJobTask)
56  DECL_VERIFIER(PromiseFulfillReactionJobTask)
57 
58  private:
59  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseFulfillReactionJobTask);
60 };
61 
62 // Struct to hold state required for a PromiseReactionJob of type "Reject".
64  public:
65  // Dispatched behavior.
67  DECL_PRINTER(PromiseRejectReactionJobTask)
68  DECL_VERIFIER(PromiseRejectReactionJobTask)
69 
70  private:
71  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseRejectReactionJobTask);
72 };
73 
74 // A container struct to hold state required for PromiseResolveThenableJob.
76  public:
77  DECL_ACCESSORS2(context, Context)
78  DECL_ACCESSORS(promise_to_resolve, JSPromise)
79  DECL_ACCESSORS(then, JSReceiver)
80  DECL_ACCESSORS(thenable, JSReceiver)
81 
82  static const int kContextOffset = Microtask::kHeaderSize;
83  static const int kPromiseToResolveOffset = kContextOffset + kPointerSize;
84  static const int kThenOffset = kPromiseToResolveOffset + kPointerSize;
85  static const int kThenableOffset = kThenOffset + kPointerSize;
86  static const int kSize = kThenableOffset + kPointerSize;
87 
88  // Dispatched behavior.
90  DECL_PRINTER(PromiseResolveThenableJobTask)
91  DECL_VERIFIER(PromiseResolveThenableJobTask)
92 
93  private:
94  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseResolveThenableJobTask);
95 };
96 
97 // Struct to hold the state of a PromiseCapability.
98 class PromiseCapability : public Struct {
99  public:
100  DECL_ACCESSORS(promise, HeapObject)
101  DECL_ACCESSORS(resolve, Object)
102  DECL_ACCESSORS(reject, Object)
103 
104  static const int kPromiseOffset = Struct::kHeaderSize;
105  static const int kResolveOffset = kPromiseOffset + kPointerSize;
106  static const int kRejectOffset = kResolveOffset + kPointerSize;
107  static const int kSize = kRejectOffset + kPointerSize;
108 
109  // Dispatched behavior.
110  DECL_CAST(PromiseCapability)
111  DECL_PRINTER(PromiseCapability)
112  DECL_VERIFIER(PromiseCapability)
113 
114  private:
115  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseCapability);
116 };
117 
118 // A representation of promise reaction. This differs from the specification
119 // in that the PromiseReaction here holds both handlers for the fulfill and
120 // the reject case. When a JSPromise is eventually resolved (either via
121 // fulfilling it or rejecting it), we morph this PromiseReaction object in
122 // memory into a proper PromiseReactionJobTask and schedule it on the queue
123 // of microtasks. So the size of PromiseReaction and the size of the
124 // PromiseReactionJobTask has to be same for this to work.
125 //
126 // The PromiseReaction::promise_or_capability field can either hold a JSPromise
127 // instance (in the fast case of a native promise) or a PromiseCapability in
128 // case of a Promise subclass. In case of await it can also be undefined if
129 // PromiseHooks are disabled (see https://github.com/tc39/ecma262/pull/1146).
130 //
131 // The PromiseReaction objects form a singly-linked list, terminated by
132 // Smi 0. On the JSPromise instance they are linked in reverse order,
133 // and are turned into the proper order again when scheduling them on
134 // the microtask queue.
135 class PromiseReaction : public Struct {
136  public:
137  enum Type { kFulfill, kReject };
138 
139  DECL_ACCESSORS(next, Object)
140  DECL_ACCESSORS(reject_handler, HeapObject)
141  DECL_ACCESSORS(fulfill_handler, HeapObject)
142  // [promise_or_capability]: Either a JSPromise (in case of native promises),
143  // a PromiseCapability (general case), or undefined (in case of await).
144  DECL_ACCESSORS(promise_or_capability, HeapObject)
145 
146  static const int kNextOffset = Struct::kHeaderSize;
147  static const int kRejectHandlerOffset = kNextOffset + kPointerSize;
148  static const int kFulfillHandlerOffset = kRejectHandlerOffset + kPointerSize;
149  static const int kPromiseOrCapabilityOffset =
150  kFulfillHandlerOffset + kPointerSize;
151  static const int kSize = kPromiseOrCapabilityOffset + kPointerSize;
152 
153  // Dispatched behavior.
154  DECL_CAST(PromiseReaction)
155  DECL_PRINTER(PromiseReaction)
156  DECL_VERIFIER(PromiseReaction)
157 
158  private:
159  DISALLOW_IMPLICIT_CONSTRUCTORS(PromiseReaction);
160 };
161 
162 } // namespace internal
163 } // namespace v8
164 
165 #include "src/objects/object-macros-undef.h"
166 
167 #endif // V8_OBJECTS_PROMISE_H_
Definition: libplatform.h:13