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_JS_PROMISE_H_ 6 #define V8_OBJECTS_JS_PROMISE_H_ 7 8 #include "src/objects/js-objects.h" 9 #include "src/objects/promise.h" 10 #include "torque-generated/bit-fields.h" 11 12 // Has to be the last include (doesn't have include guards): 13 #include "src/objects/object-macros.h" 14 15 namespace v8 { 16 namespace internal { 17 18 #include "torque-generated/src/objects/js-promise-tq.inc" 19 20 // Representation of promise objects in the specification. Our layout of 21 // JSPromise differs a bit from the layout in the specification, for example 22 // there's only a single list of PromiseReaction objects, instead of separate 23 // lists for fulfill and reject reactions. The PromiseReaction carries both 24 // callbacks from the start, and is eventually morphed into the proper kind of 25 // PromiseReactionJobTask when the JSPromise is settled. 26 // 27 // We also overlay the result and reactions fields on the JSPromise, since 28 // the reactions are only necessary for pending promises, whereas the result 29 // is only meaningful for settled promises. 30 class JSPromise : public TorqueGeneratedJSPromise<JSPromise, JSObject> { 31 public: 32 // [result]: Checks that the promise is settled and returns the result. 33 inline Object result() const; 34 35 // [reactions]: Checks that the promise is pending and returns the reactions. 36 inline Object reactions() const; 37 38 // [has_handler]: Whether this promise has a reject handler or not. 39 DECL_BOOLEAN_ACCESSORS(has_handler) 40 41 // [handled_hint]: Whether this promise will be handled by a catch 42 // block in an async function. 43 DECL_BOOLEAN_ACCESSORS(handled_hint) 44 45 int async_task_id() const; 46 void set_async_task_id(int id); 47 48 static const char* Status(Promise::PromiseState status); 49 V8_EXPORT_PRIVATE Promise::PromiseState status() const; 50 void set_status(Promise::PromiseState status); 51 52 // ES section #sec-fulfillpromise 53 V8_EXPORT_PRIVATE static Handle<Object> Fulfill(Handle<JSPromise> promise, 54 Handle<Object> value); 55 // ES section #sec-rejectpromise 56 static Handle<Object> Reject(Handle<JSPromise> promise, Handle<Object> reason, 57 bool debug_event = true); 58 // ES section #sec-promise-resolve-functions 59 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Resolve( 60 Handle<JSPromise> promise, Handle<Object> resolution); 61 62 // Dispatched behavior. 63 DECL_PRINTER(JSPromise) 64 DECL_VERIFIER(JSPromise) 65 66 static const int kSizeWithEmbedderFields = 67 kHeaderSize + v8::Promise::kEmbedderFieldCount * kEmbedderDataSlotSize; 68 69 // Flags layout. 70 DEFINE_TORQUE_GENERATED_JS_PROMISE_FLAGS() 71 72 STATIC_ASSERT(v8::Promise::kPending == 0); 73 STATIC_ASSERT(v8::Promise::kFulfilled == 1); 74 STATIC_ASSERT(v8::Promise::kRejected == 2); 75 76 private: 77 // ES section #sec-triggerpromisereactions 78 static Handle<Object> TriggerPromiseReactions(Isolate* isolate, 79 Handle<Object> reactions, 80 Handle<Object> argument, 81 PromiseReaction::Type type); 82 83 TQ_OBJECT_CONSTRUCTORS(JSPromise) 84 }; 85 86 } // namespace internal 87 } // namespace v8 88 89 #include "src/objects/object-macros-undef.h" 90 91 #endif // V8_OBJECTS_JS_PROMISE_H_ 92