11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_OBJECTS_JS_PROMISE_H_ 61cb0ef41Sopenharmony_ci#define V8_OBJECTS_JS_PROMISE_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "include/v8-promise.h" 91cb0ef41Sopenharmony_ci#include "src/objects/js-objects.h" 101cb0ef41Sopenharmony_ci#include "src/objects/promise.h" 111cb0ef41Sopenharmony_ci#include "torque-generated/bit-fields.h" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci// Has to be the last include (doesn't have include guards): 141cb0ef41Sopenharmony_ci#include "src/objects/object-macros.h" 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_cinamespace v8 { 171cb0ef41Sopenharmony_cinamespace internal { 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci#include "torque-generated/src/objects/js-promise-tq.inc" 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// Representation of promise objects in the specification. Our layout of 221cb0ef41Sopenharmony_ci// JSPromise differs a bit from the layout in the specification, for example 231cb0ef41Sopenharmony_ci// there's only a single list of PromiseReaction objects, instead of separate 241cb0ef41Sopenharmony_ci// lists for fulfill and reject reactions. The PromiseReaction carries both 251cb0ef41Sopenharmony_ci// callbacks from the start, and is eventually morphed into the proper kind of 261cb0ef41Sopenharmony_ci// PromiseReactionJobTask when the JSPromise is settled. 271cb0ef41Sopenharmony_ci// 281cb0ef41Sopenharmony_ci// We also overlay the result and reactions fields on the JSPromise, since 291cb0ef41Sopenharmony_ci// the reactions are only necessary for pending promises, whereas the result 301cb0ef41Sopenharmony_ci// is only meaningful for settled promises. 311cb0ef41Sopenharmony_ciclass JSPromise 321cb0ef41Sopenharmony_ci : public TorqueGeneratedJSPromise<JSPromise, JSObjectWithEmbedderSlots> { 331cb0ef41Sopenharmony_ci public: 341cb0ef41Sopenharmony_ci // [result]: Checks that the promise is settled and returns the result. 351cb0ef41Sopenharmony_ci inline Object result() const; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci // [reactions]: Checks that the promise is pending and returns the reactions. 381cb0ef41Sopenharmony_ci inline Object reactions() const; 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci // [has_handler]: Whether this promise has a reject handler or not. 411cb0ef41Sopenharmony_ci DECL_BOOLEAN_ACCESSORS(has_handler) 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci // [handled_hint]: Whether this promise will be handled by a catch 441cb0ef41Sopenharmony_ci // block in an async function. 451cb0ef41Sopenharmony_ci DECL_BOOLEAN_ACCESSORS(handled_hint) 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci // [is_silent]: Whether this promise should cause the debugger to pause when 481cb0ef41Sopenharmony_ci // rejected. 491cb0ef41Sopenharmony_ci DECL_BOOLEAN_ACCESSORS(is_silent) 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci int async_task_id() const; 521cb0ef41Sopenharmony_ci void set_async_task_id(int id); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci static const char* Status(Promise::PromiseState status); 551cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE Promise::PromiseState status() const; 561cb0ef41Sopenharmony_ci void set_status(Promise::PromiseState status); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci // ES section #sec-fulfillpromise 591cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static Handle<Object> Fulfill(Handle<JSPromise> promise, 601cb0ef41Sopenharmony_ci Handle<Object> value); 611cb0ef41Sopenharmony_ci // ES section #sec-rejectpromise 621cb0ef41Sopenharmony_ci static Handle<Object> Reject(Handle<JSPromise> promise, Handle<Object> reason, 631cb0ef41Sopenharmony_ci bool debug_event = true); 641cb0ef41Sopenharmony_ci // ES section #sec-promise-resolve-functions 651cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Resolve( 661cb0ef41Sopenharmony_ci Handle<JSPromise> promise, Handle<Object> resolution); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci // Dispatched behavior. 691cb0ef41Sopenharmony_ci DECL_PRINTER(JSPromise) 701cb0ef41Sopenharmony_ci DECL_VERIFIER(JSPromise) 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci static const int kSizeWithEmbedderFields = 731cb0ef41Sopenharmony_ci kHeaderSize + v8::Promise::kEmbedderFieldCount * kEmbedderDataSlotSize; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci // Flags layout. 761cb0ef41Sopenharmony_ci DEFINE_TORQUE_GENERATED_JS_PROMISE_FLAGS() 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci STATIC_ASSERT(v8::Promise::kPending == 0); 791cb0ef41Sopenharmony_ci STATIC_ASSERT(v8::Promise::kFulfilled == 1); 801cb0ef41Sopenharmony_ci STATIC_ASSERT(v8::Promise::kRejected == 2); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci private: 831cb0ef41Sopenharmony_ci // ES section #sec-triggerpromisereactions 841cb0ef41Sopenharmony_ci static Handle<Object> TriggerPromiseReactions(Isolate* isolate, 851cb0ef41Sopenharmony_ci Handle<Object> reactions, 861cb0ef41Sopenharmony_ci Handle<Object> argument, 871cb0ef41Sopenharmony_ci PromiseReaction::Type type); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci TQ_OBJECT_CONSTRUCTORS(JSPromise) 901cb0ef41Sopenharmony_ci}; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci} // namespace internal 931cb0ef41Sopenharmony_ci} // namespace v8 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci#include "src/objects/object-macros-undef.h" 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci#endif // V8_OBJECTS_JS_PROMISE_H_ 98