• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "torque-generated/src/objects/promise-tq.inc"
19 
20 // Struct to hold state required for PromiseReactionJob. See the comment on the
21 // PromiseReaction below for details on how this is being managed to reduce the
22 // memory and allocation overhead. This is the base class for the concrete
23 //
24 //   - PromiseFulfillReactionJobTask
25 //   - PromiseRejectReactionJobTask
26 //
27 // classes, which are used to represent either reactions, and we distinguish
28 // them by their instance types.
29 class PromiseReactionJobTask
30     : public TorqueGeneratedPromiseReactionJobTask<PromiseReactionJobTask,
31                                                    Microtask> {
32  public:
33   static const int kSizeOfAllPromiseReactionJobTasks = kHeaderSize;
34   TQ_OBJECT_CONSTRUCTORS(PromiseReactionJobTask)
35 };
36 
37 // Struct to hold state required for a PromiseReactionJob of type "Fulfill".
38 class PromiseFulfillReactionJobTask
39     : public TorqueGeneratedPromiseFulfillReactionJobTask<
40           PromiseFulfillReactionJobTask, PromiseReactionJobTask> {
41  public:
42   // Dispatched behavior.
43   DECL_PRINTER(PromiseFulfillReactionJobTask)
44 
45   STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);
46 
47   TQ_OBJECT_CONSTRUCTORS(PromiseFulfillReactionJobTask)
48 };
49 
50 // Struct to hold state required for a PromiseReactionJob of type "Reject".
51 class PromiseRejectReactionJobTask
52     : public TorqueGeneratedPromiseRejectReactionJobTask<
53           PromiseRejectReactionJobTask, PromiseReactionJobTask> {
54  public:
55   // Dispatched behavior.
56   DECL_PRINTER(PromiseRejectReactionJobTask)
57 
58   STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);
59 
60   TQ_OBJECT_CONSTRUCTORS(PromiseRejectReactionJobTask)
61 };
62 
63 // A container struct to hold state required for PromiseResolveThenableJob.
64 class PromiseResolveThenableJobTask
65     : public TorqueGeneratedPromiseResolveThenableJobTask<
66           PromiseResolveThenableJobTask, Microtask> {
67  public:
68   // Dispatched behavior.
69   DECL_PRINTER(PromiseResolveThenableJobTask)
70 
71   TQ_OBJECT_CONSTRUCTORS(PromiseResolveThenableJobTask)
72 };
73 
74 // Struct to hold the state of a PromiseCapability.
75 class PromiseCapability
76     : public TorqueGeneratedPromiseCapability<PromiseCapability, Struct> {
77  public:
78   // Dispatched behavior.
79   DECL_PRINTER(PromiseCapability)
80 
81   TQ_OBJECT_CONSTRUCTORS(PromiseCapability)
82 };
83 
84 // A representation of promise reaction. This differs from the specification
85 // in that the PromiseReaction here holds both handlers for the fulfill and
86 // the reject case. When a JSPromise is eventually resolved (either via
87 // fulfilling it or rejecting it), we morph this PromiseReaction object in
88 // memory into a proper PromiseReactionJobTask and schedule it on the queue
89 // of microtasks. So the size of PromiseReaction and the size of the
90 // PromiseReactionJobTask has to be same for this to work.
91 //
92 // The PromiseReaction::promise_or_capability field can either hold a JSPromise
93 // instance (in the fast case of a native promise) or a PromiseCapability in
94 // case of a Promise subclass. In case of await it can also be undefined if
95 // PromiseHooks are disabled (see https://github.com/tc39/ecma262/pull/1146).
96 //
97 // The PromiseReaction objects form a singly-linked list, terminated by
98 // Smi 0. On the JSPromise instance they are linked in reverse order,
99 // and are turned into the proper order again when scheduling them on
100 // the microtask queue.
101 class PromiseReaction
102     : public TorqueGeneratedPromiseReaction<PromiseReaction, Struct> {
103  public:
104   enum Type { kFulfill, kReject };
105 
106   // Dispatched behavior.
107   DECL_PRINTER(PromiseReaction)
108 
109   TQ_OBJECT_CONSTRUCTORS(PromiseReaction)
110 };
111 
112 }  // namespace internal
113 }  // namespace v8
114 
115 #include "src/objects/object-macros-undef.h"
116 
117 #endif  // V8_OBJECTS_PROMISE_H_
118