js-weak-refs.h 5.16 KB
Newer Older
1 2 3 4 5 6 7 8
// 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_WEAK_REFS_H_
#define V8_OBJECTS_JS_WEAK_REFS_H_

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

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

namespace v8 {
namespace internal {

17
class NativeContext;
18
class WeakCell;
19

20
// FinalizationGroup object from the JS Weak Refs spec proposal:
21
// https://github.com/tc39/proposal-weakrefs
22
class JSFinalizationGroup : public JSObject {
23
 public:
24
  DECL_PRINTER(JSFinalizationGroup)
25
  EXPORT_DECL_VERIFIER(JSFinalizationGroup)
26
  DECL_CAST(JSFinalizationGroup)
27

28
  DECL_ACCESSORS(native_context, NativeContext)
29
  DECL_ACCESSORS(cleanup, Object)
30

31 32
  DECL_ACCESSORS(active_cells, Object)
  DECL_ACCESSORS(cleared_cells, Object)
33
  DECL_ACCESSORS(key_map, Object)
34

35
  // For storing a list of JSFinalizationGroup objects in NativeContext.
36 37
  DECL_ACCESSORS(next, Object)

38 39
  DECL_INT_ACCESSORS(flags)

40 41 42 43
  inline static void Register(Handle<JSFinalizationGroup> finalization_group,
                              Handle<JSReceiver> target,
                              Handle<Object> holdings, Handle<Object> key,
                              Isolate* isolate);
44 45 46
  inline static bool Unregister(Handle<JSFinalizationGroup> finalization_group,
                                Handle<JSReceiver> unregister_token,
                                Isolate* isolate);
47

48 49 50
  // Returns true if the cleared_cells list is non-empty.
  inline bool NeedsCleanup() const;

51 52 53
  inline bool scheduled_for_cleanup() const;
  inline void set_scheduled_for_cleanup(bool scheduled_for_cleanup);

54 55 56 57
  // Remove the first cleared WeakCell from the cleared_cells
  // list (assumes there is one) and return its holdings.
  inline static Object PopClearedCellHoldings(
      Handle<JSFinalizationGroup> finalization_group, Isolate* isolate);
58

59 60
  // Constructs an iterator for the WeakCells in the cleared_cells list and
  // calls the user's cleanup function.
61 62 63 64 65
  //
  // Returns Nothing<bool> if exception occurs, otherwise returns Just(true).
  static V8_WARN_UNUSED_RESULT Maybe<bool> Cleanup(
      Isolate* isolate, Handle<JSFinalizationGroup> finalization_group,
      Handle<Object> callback);
66

67
  // Layout description.
68
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
69
                                TORQUE_GENERATED_JSFINALIZATION_GROUP_FIELDS)
70 71

  // Bitfields in flags.
72
  using ScheduledForCleanupField = BitField<bool, 0, 1>;
73

74
  OBJECT_CONSTRUCTORS(JSFinalizationGroup, JSObject);
75 76
};

77 78
// Internal object for storing weak references in JSFinalizationGroup.
class WeakCell : public HeapObject {
79
 public:
80
  DECL_PRINTER(WeakCell)
81
  EXPORT_DECL_VERIFIER(WeakCell)
82
  DECL_CAST(WeakCell)
83

84
  DECL_ACCESSORS(finalization_group, Object)
85
  DECL_ACCESSORS(target, HeapObject)
86
  DECL_ACCESSORS(holdings, Object)
87

88 89
  // For storing doubly linked lists of WeakCells in JSFinalizationGroup's
  // "active_cells" and "cleared_cells" lists.
90 91 92
  DECL_ACCESSORS(prev, Object)
  DECL_ACCESSORS(next, Object)

93 94 95 96 97 98 99 100
  // For storing doubly linked lists of WeakCells per key in
  // JSFinalizationGroup's key-based hashmap. WeakCell also needs to know its
  // key, so that we can remove the key from the key_map when we remove the last
  // WeakCell associated with it.
  DECL_ACCESSORS(key, Object)
  DECL_ACCESSORS(key_list_prev, Object)
  DECL_ACCESSORS(key_list_next, Object)

101 102 103
  // Layout description.
  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
                                TORQUE_GENERATED_WEAK_CELL_FIELDS)
104 105 106

  class BodyDescriptor;

107 108 109 110
  // Nullify is called during GC and it modifies the pointers in WeakCell and
  // JSFinalizationGroup. Thus we need to tell the GC about the modified slots
  // via the gc_notify_updated_slot function. The normal write barrier is not
  // enough, since it's disabled before GC.
111 112
  inline void Nullify(
      Isolate* isolate,
113
      std::function<void(HeapObject object, ObjectSlot slot, Object target)>
114 115
          gc_notify_updated_slot);

116
  inline void RemoveFromFinalizationGroupCells(Isolate* isolate);
117

118
  OBJECT_CONSTRUCTORS(WeakCell, HeapObject);
119 120
};

121
class JSWeakRef : public JSObject {
122
 public:
123
  DECL_PRINTER(JSWeakRef)
124
  EXPORT_DECL_VERIFIER(JSWeakRef)
125
  DECL_CAST(JSWeakRef)
126

127
  DECL_ACCESSORS(target, HeapObject)
128

129 130 131
  // Layout description.
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
                                TORQUE_GENERATED_JSWEAK_REF_FIELDS)
132 133 134 135

  class BodyDescriptor;

  OBJECT_CONSTRUCTORS(JSWeakRef, JSObject);
136 137
};

138
class JSFinalizationGroupCleanupIterator : public JSObject {
139
 public:
140 141 142
  DECL_PRINTER(JSFinalizationGroupCleanupIterator)
  DECL_VERIFIER(JSFinalizationGroupCleanupIterator)
  DECL_CAST(JSFinalizationGroupCleanupIterator)
143

144
  DECL_ACCESSORS(finalization_group, JSFinalizationGroup)
145

146 147 148 149
  // Layout description.
  DEFINE_FIELD_OFFSET_CONSTANTS(
    JSObject::kHeaderSize,
    TORQUE_GENERATED_JSFINALIZATION_GROUP_CLEANUP_ITERATOR_FIELDS)
150

151
  OBJECT_CONSTRUCTORS(JSFinalizationGroupCleanupIterator, JSObject);
152 153 154 155 156 157 158 159
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_JS_WEAK_REFS_H_