promise.h 4.04 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
#include "torque-generated/src/objects/promise-tq.inc"

20 21 22 23 24 25 26 27 28
// 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.
29 30 31
class PromiseReactionJobTask
    : public TorqueGeneratedPromiseReactionJobTask<PromiseReactionJobTask,
                                                   Microtask> {
32
 public:
33
  static const int kSizeOfAllPromiseReactionJobTasks = kHeaderSize;
34
  TQ_OBJECT_CONSTRUCTORS(PromiseReactionJobTask)
35 36 37
};

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

45 46
  STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);

47
  TQ_OBJECT_CONSTRUCTORS(PromiseFulfillReactionJobTask)
48 49 50
};

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

58 59
  STATIC_ASSERT(kSize == kSizeOfAllPromiseReactionJobTasks);

60
  TQ_OBJECT_CONSTRUCTORS(PromiseRejectReactionJobTask)
61 62 63
};

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

71
  TQ_OBJECT_CONSTRUCTORS(PromiseResolveThenableJobTask)
72 73 74
};

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

81
  TQ_OBJECT_CONSTRUCTORS(PromiseCapability)
82 83 84 85 86 87 88 89 90 91
};

// 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.
//
92 93
// The PromiseReaction::promise_or_capability field can either hold a JSPromise
// instance (in the fast case of a native promise) or a PromiseCapability in
94 95
// 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).
96 97 98 99 100
//
// 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.
101 102
class PromiseReaction
    : public TorqueGeneratedPromiseReaction<PromiseReaction, Struct> {
103 104 105 106 107 108
 public:
  enum Type { kFulfill, kReject };

  // Dispatched behavior.
  DECL_PRINTER(PromiseReaction)

109
  TQ_OBJECT_CONSTRUCTORS(PromiseReaction)
110 111 112 113 114 115 116 117
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_PROMISE_H_