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_WEAK_REFS_H_
61cb0ef41Sopenharmony_ci#define V8_OBJECTS_JS_WEAK_REFS_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/objects/js-objects.h"
91cb0ef41Sopenharmony_ci#include "torque-generated/bit-fields.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Has to be the last include (doesn't have include guards):
121cb0ef41Sopenharmony_ci#include "src/objects/object-macros.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciclass NativeContext;
181cb0ef41Sopenharmony_ciclass WeakCell;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci#include "torque-generated/src/objects/js-weak-refs-tq.inc"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// FinalizationRegistry object from the JS Weak Refs spec proposal:
231cb0ef41Sopenharmony_ci// https://github.com/tc39/proposal-weakrefs
241cb0ef41Sopenharmony_ciclass JSFinalizationRegistry
251cb0ef41Sopenharmony_ci    : public TorqueGeneratedJSFinalizationRegistry<JSFinalizationRegistry,
261cb0ef41Sopenharmony_ci                                                   JSObject> {
271cb0ef41Sopenharmony_ci public:
281cb0ef41Sopenharmony_ci  DECL_PRINTER(JSFinalizationRegistry)
291cb0ef41Sopenharmony_ci  EXPORT_DECL_VERIFIER(JSFinalizationRegistry)
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  DECL_BOOLEAN_ACCESSORS(scheduled_for_cleanup)
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  class BodyDescriptor;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  inline static void RegisterWeakCellWithUnregisterToken(
361cb0ef41Sopenharmony_ci      Handle<JSFinalizationRegistry> finalization_registry,
371cb0ef41Sopenharmony_ci      Handle<WeakCell> weak_cell, Isolate* isolate);
381cb0ef41Sopenharmony_ci  inline static bool Unregister(
391cb0ef41Sopenharmony_ci      Handle<JSFinalizationRegistry> finalization_registry,
401cb0ef41Sopenharmony_ci      Handle<HeapObject> unregister_token, Isolate* isolate);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // RemoveUnregisterToken is called from both Unregister and during GC. Since
431cb0ef41Sopenharmony_ci  // it modifies slots in key_map and WeakCells and the normal write barrier is
441cb0ef41Sopenharmony_ci  // disabled during GC, we need to tell the GC about the modified slots via the
451cb0ef41Sopenharmony_ci  // gc_notify_updated_slot function.
461cb0ef41Sopenharmony_ci  enum RemoveUnregisterTokenMode {
471cb0ef41Sopenharmony_ci    kRemoveMatchedCellsFromRegistry,
481cb0ef41Sopenharmony_ci    kKeepMatchedCellsInRegistry
491cb0ef41Sopenharmony_ci  };
501cb0ef41Sopenharmony_ci  template <typename GCNotifyUpdatedSlotCallback>
511cb0ef41Sopenharmony_ci  inline bool RemoveUnregisterToken(
521cb0ef41Sopenharmony_ci      HeapObject unregister_token, Isolate* isolate,
531cb0ef41Sopenharmony_ci      RemoveUnregisterTokenMode removal_mode,
541cb0ef41Sopenharmony_ci      GCNotifyUpdatedSlotCallback gc_notify_updated_slot);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  // Returns true if the cleared_cells list is non-empty.
571cb0ef41Sopenharmony_ci  inline bool NeedsCleanup() const;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  // Remove the already-popped weak_cell from its unregister token linked list,
601cb0ef41Sopenharmony_ci  // as well as removing the entry from the key map if it is the only WeakCell
611cb0ef41Sopenharmony_ci  // with its unregister token. This method cannot GC and does not shrink the
621cb0ef41Sopenharmony_ci  // key map. Asserts that weak_cell has a non-undefined unregister token.
631cb0ef41Sopenharmony_ci  //
641cb0ef41Sopenharmony_ci  // It takes raw Addresses because it is called from CSA and Torque.
651cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE static void RemoveCellFromUnregisterTokenMap(
661cb0ef41Sopenharmony_ci      Isolate* isolate, Address raw_finalization_registry,
671cb0ef41Sopenharmony_ci      Address raw_weak_cell);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  // Bitfields in flags.
701cb0ef41Sopenharmony_ci  DEFINE_TORQUE_GENERATED_FINALIZATION_REGISTRY_FLAGS()
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  TQ_OBJECT_CONSTRUCTORS(JSFinalizationRegistry)
731cb0ef41Sopenharmony_ci};
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci// Internal object for storing weak references in JSFinalizationRegistry.
761cb0ef41Sopenharmony_ciclass WeakCell : public TorqueGeneratedWeakCell<WeakCell, HeapObject> {
771cb0ef41Sopenharmony_ci public:
781cb0ef41Sopenharmony_ci  EXPORT_DECL_VERIFIER(WeakCell)
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  class BodyDescriptor;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  // Provide relaxed load access to target field.
831cb0ef41Sopenharmony_ci  inline HeapObject relaxed_target() const;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  // Provide relaxed load access to the unregister token field.
861cb0ef41Sopenharmony_ci  inline HeapObject relaxed_unregister_token() const;
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  // Nullify is called during GC and it modifies the pointers in WeakCell and
891cb0ef41Sopenharmony_ci  // JSFinalizationRegistry. Thus we need to tell the GC about the modified
901cb0ef41Sopenharmony_ci  // slots via the gc_notify_updated_slot function. The normal write barrier is
911cb0ef41Sopenharmony_ci  // not enough, since it's disabled before GC.
921cb0ef41Sopenharmony_ci  template <typename GCNotifyUpdatedSlotCallback>
931cb0ef41Sopenharmony_ci  inline void Nullify(Isolate* isolate,
941cb0ef41Sopenharmony_ci                      GCNotifyUpdatedSlotCallback gc_notify_updated_slot);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  inline void RemoveFromFinalizationRegistryCells(Isolate* isolate);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  TQ_OBJECT_CONSTRUCTORS(WeakCell)
991cb0ef41Sopenharmony_ci};
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciclass JSWeakRef : public TorqueGeneratedJSWeakRef<JSWeakRef, JSObject> {
1021cb0ef41Sopenharmony_ci public:
1031cb0ef41Sopenharmony_ci  DECL_PRINTER(JSWeakRef)
1041cb0ef41Sopenharmony_ci  EXPORT_DECL_VERIFIER(JSWeakRef)
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  class BodyDescriptor;
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  TQ_OBJECT_CONSTRUCTORS(JSWeakRef)
1091cb0ef41Sopenharmony_ci};
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci}  // namespace internal
1121cb0ef41Sopenharmony_ci}  // namespace v8
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci#include "src/objects/object-macros-undef.h"
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci#endif  // V8_OBJECTS_JS_WEAK_REFS_H_
117