js-promise.h 3.84 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_JS_PROMISE_H_
#define V8_OBJECTS_JS_PROMISE_H_

8
#include "src/objects/js-objects.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "src/objects/promise.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

// Representation of promise objects in the specification. Our layout of
// JSPromise differs a bit from the layout in the specification, for example
// there's only a single list of PromiseReaction objects, instead of separate
// lists for fulfill and reject reactions. The PromiseReaction carries both
// callbacks from the start, and is eventually morphed into the proper kind of
// PromiseReactionJobTask when the JSPromise is settled.
//
// We also overlay the result and reactions fields on the JSPromise, since
// the reactions are only necessary for pending promises, whereas the result
// is only meaningful for settled promises.
class JSPromise : public JSObject {
 public:
  // [reactions_or_result]: Smi 0 terminated list of PromiseReaction objects
  // in case the JSPromise was not settled yet, otherwise the result.
  DECL_ACCESSORS(reactions_or_result, Object)

  // [result]: Checks that the promise is settled and returns the result.
34
  inline Object result() const;
35 36

  // [reactions]: Checks that the promise is pending and returns the reactions.
37
  inline Object reactions() const;
38 39 40 41 42 43 44 45 46 47

  DECL_INT_ACCESSORS(flags)

  // [has_handler]: Whether this promise has a reject handler or not.
  DECL_BOOLEAN_ACCESSORS(has_handler)

  // [handled_hint]: Whether this promise will be handled by a catch
  // block in an async function.
  DECL_BOOLEAN_ACCESSORS(handled_hint)

48 49 50
  int async_task_id() const;
  void set_async_task_id(int id);

51
  static const char* Status(Promise::PromiseState status);
52
  V8_EXPORT_PRIVATE Promise::PromiseState status() const;
53 54 55
  void set_status(Promise::PromiseState status);

  // ES section #sec-fulfillpromise
56 57
  V8_EXPORT_PRIVATE static Handle<Object> Fulfill(Handle<JSPromise> promise,
                                                  Handle<Object> value);
58 59 60 61
  // ES section #sec-rejectpromise
  static Handle<Object> Reject(Handle<JSPromise> promise, Handle<Object> reason,
                               bool debug_event = true);
  // ES section #sec-promise-resolve-functions
62 63
  V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Resolve(
      Handle<JSPromise> promise, Handle<Object> resolution);
64

65
  DECL_CAST(JSPromise)
66 67

  // Dispatched behavior.
68
  DECL_PRINTER(JSPromise)
69 70
  DECL_VERIFIER(JSPromise)

71 72
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
                                TORQUE_GENERATED_JSPROMISE_FIELDS)
73

74
  static const int kSizeWithEmbedderFields =
75
      kSize + v8::Promise::kEmbedderFieldCount * kEmbedderDataSlotSize;
76 77 78 79 80 81

  // Flags layout.
  // The first two bits store the v8::Promise::PromiseState.
  static const int kStatusBits = 2;
  static const int kHasHandlerBit = 2;
  static const int kHandledHintBit = 3;
82
  class AsyncTaskIdField : public BitField<int, kHandledHintBit + 1, 22> {};
83 84 85 86 87 88 89 90 91 92 93 94 95

  static const int kStatusShift = 0;
  static const int kStatusMask = 0x3;
  STATIC_ASSERT(v8::Promise::kPending == 0);
  STATIC_ASSERT(v8::Promise::kFulfilled == 1);
  STATIC_ASSERT(v8::Promise::kRejected == 2);

 private:
  // ES section #sec-triggerpromisereactions
  static Handle<Object> TriggerPromiseReactions(Isolate* isolate,
                                                Handle<Object> reactions,
                                                Handle<Object> argument,
                                                PromiseReaction::Type type);
96

97
  OBJECT_CONSTRUCTORS(JSPromise, JSObject);
98 99 100 101 102 103 104 105
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_PROMISE_H_