promise.h 3.99 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_PROMISE_H_
#define V8_OBJECTS_PROMISE_H_

8
#include "src/objects/microtask.h"
9 10 11 12 13 14 15

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

namespace v8 {
namespace internal {

16 17
class JSPromise;

18 19 20 21 22 23 24 25 26
// Struct to hold state required for PromiseReactionJob. See the comment on the
// PromiseReaction below for details on how this is being managed to reduce the
// memory and allocation overhead. This is the base class for the concrete
//
//   - PromiseFulfillReactionJobTask
//   - PromiseRejectReactionJobTask
//
// classes, which are used to represent either reactions, and we distinguish
// them by their instance types.
27 28 29
class PromiseReactionJobTask
    : public TorqueGeneratedPromiseReactionJobTask<PromiseReactionJobTask,
                                                   Microtask> {
30
 public:
31
  static const int kSizeOfAllPromiseReactionJobTasks = kHeaderSize;
32
  TQ_OBJECT_CONSTRUCTORS(PromiseReactionJobTask)
33 34 35
};

// Struct to hold state required for a PromiseReactionJob of type "Fulfill".
36 37 38
class PromiseFulfillReactionJobTask
    : public TorqueGeneratedPromiseFulfillReactionJobTask<
          PromiseFulfillReactionJobTask, PromiseReactionJobTask> {
39 40 41 42
 public:
  // Dispatched behavior.
  DECL_PRINTER(PromiseFulfillReactionJobTask)

43 44
  STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);

45
  TQ_OBJECT_CONSTRUCTORS(PromiseFulfillReactionJobTask)
46 47 48
};

// Struct to hold state required for a PromiseReactionJob of type "Reject".
49 50 51
class PromiseRejectReactionJobTask
    : public TorqueGeneratedPromiseRejectReactionJobTask<
          PromiseRejectReactionJobTask, PromiseReactionJobTask> {
52 53 54 55
 public:
  // Dispatched behavior.
  DECL_PRINTER(PromiseRejectReactionJobTask)

56 57
  STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);

58
  TQ_OBJECT_CONSTRUCTORS(PromiseRejectReactionJobTask)
59 60 61
};

// A container struct to hold state required for PromiseResolveThenableJob.
62 63 64
class PromiseResolveThenableJobTask
    : public TorqueGeneratedPromiseResolveThenableJobTask<
          PromiseResolveThenableJobTask, Microtask> {
65 66 67 68
 public:
  // Dispatched behavior.
  DECL_PRINTER(PromiseResolveThenableJobTask)

69
  TQ_OBJECT_CONSTRUCTORS(PromiseResolveThenableJobTask)
70 71 72
};

// Struct to hold the state of a PromiseCapability.
73 74
class PromiseCapability
    : public TorqueGeneratedPromiseCapability<PromiseCapability, Struct> {
75 76 77 78
 public:
  // Dispatched behavior.
  DECL_PRINTER(PromiseCapability)

79
  TQ_OBJECT_CONSTRUCTORS(PromiseCapability)
80 81 82 83 84 85 86 87 88 89
};

// A representation of promise reaction. This differs from the specification
// in that the PromiseReaction here holds both handlers for the fulfill and
// the reject case. When a JSPromise is eventually resolved (either via
// fulfilling it or rejecting it), we morph this PromiseReaction object in
// memory into a proper PromiseReactionJobTask and schedule it on the queue
// of microtasks. So the size of PromiseReaction and the size of the
// PromiseReactionJobTask has to be same for this to work.
//
90 91
// The PromiseReaction::promise_or_capability field can either hold a JSPromise
// instance (in the fast case of a native promise) or a PromiseCapability in
92 93
// case of a Promise subclass. In case of await it can also be undefined if
// PromiseHooks are disabled (see https://github.com/tc39/ecma262/pull/1146).
94 95 96 97 98
//
// The PromiseReaction objects form a singly-linked list, terminated by
// Smi 0. On the JSPromise instance they are linked in reverse order,
// and are turned into the proper order again when scheduling them on
// the microtask queue.
99 100
class PromiseReaction
    : public TorqueGeneratedPromiseReaction<PromiseReaction, Struct> {
101 102 103 104 105 106
 public:
  enum Type { kFulfill, kReject };

  // Dispatched behavior.
  DECL_PRINTER(PromiseReaction)

107
  TQ_OBJECT_CONSTRUCTORS(PromiseReaction)
108 109 110 111 112 113 114 115
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_PROMISE_H_