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 class StructBodyDescriptor; 18 19 #include "torque-generated/src/objects/promise-tq.inc" 20 21 // Struct to hold state required for PromiseReactionJob. See the comment on the 22 // PromiseReaction below for details on how this is being managed to reduce the 23 // memory and allocation overhead. This is the base class for the concrete 24 // 25 // - PromiseFulfillReactionJobTask 26 // - PromiseRejectReactionJobTask 27 // 28 // classes, which are used to represent either reactions, and we distinguish 29 // them by their instance types. 30 class PromiseReactionJobTask 31 : public TorqueGeneratedPromiseReactionJobTask<PromiseReactionJobTask, 32 Microtask> { 33 public: 34 static const int kSizeOfAllPromiseReactionJobTasks = kHeaderSize; 35 36 using BodyDescriptor = StructBodyDescriptor; 37 38 TQ_OBJECT_CONSTRUCTORS(PromiseReactionJobTask) 39 }; 40 41 // Struct to hold state required for a PromiseReactionJob of type "Fulfill". 42 class PromiseFulfillReactionJobTask 43 : public TorqueGeneratedPromiseFulfillReactionJobTask< 44 PromiseFulfillReactionJobTask, PromiseReactionJobTask> { 45 public: 46 STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks); 47 48 using BodyDescriptor = StructBodyDescriptor; 49 50 TQ_OBJECT_CONSTRUCTORS(PromiseFulfillReactionJobTask) 51 }; 52 53 // Struct to hold state required for a PromiseReactionJob of type "Reject". 54 class PromiseRejectReactionJobTask 55 : public TorqueGeneratedPromiseRejectReactionJobTask< 56 PromiseRejectReactionJobTask, PromiseReactionJobTask> { 57 public: 58 STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks); 59 60 using BodyDescriptor = StructBodyDescriptor; 61 62 TQ_OBJECT_CONSTRUCTORS(PromiseRejectReactionJobTask) 63 }; 64 65 // A container struct to hold state required for PromiseResolveThenableJob. 66 class PromiseResolveThenableJobTask 67 : public TorqueGeneratedPromiseResolveThenableJobTask< 68 PromiseResolveThenableJobTask, Microtask> { 69 public: 70 using BodyDescriptor = StructBodyDescriptor; 71 72 TQ_OBJECT_CONSTRUCTORS(PromiseResolveThenableJobTask) 73 }; 74 75 // Struct to hold the state of a PromiseCapability. 76 class PromiseCapability 77 : public TorqueGeneratedPromiseCapability<PromiseCapability, Struct> { 78 public: 79 using BodyDescriptor = StructBodyDescriptor; 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 using BodyDescriptor = StructBodyDescriptor; 107 108 TQ_OBJECT_CONSTRUCTORS(PromiseReaction) 109 }; 110 111 } // namespace internal 112 } // namespace v8 113 114 #include "src/objects/object-macros-undef.h" 115 116 #endif // V8_OBJECTS_PROMISE_H_ 117