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_MAYBE_OBJECT_INL_H_
61cb0ef41Sopenharmony_ci#define V8_OBJECTS_MAYBE_OBJECT_INL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/common/ptr-compr-inl.h"
91cb0ef41Sopenharmony_ci#include "src/objects/maybe-object.h"
101cb0ef41Sopenharmony_ci#include "src/objects/smi-inl.h"
111cb0ef41Sopenharmony_ci#include "src/objects/tagged-impl-inl.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci//
171cb0ef41Sopenharmony_ci// MaybeObject implementation.
181cb0ef41Sopenharmony_ci//
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// static
211cb0ef41Sopenharmony_ciMaybeObject MaybeObject::FromSmi(Smi smi) {
221cb0ef41Sopenharmony_ci  DCHECK(HAS_SMI_TAG(smi.ptr()));
231cb0ef41Sopenharmony_ci  return MaybeObject(smi.ptr());
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// static
271cb0ef41Sopenharmony_ciMaybeObject MaybeObject::FromObject(Object object) {
281cb0ef41Sopenharmony_ci  DCHECK(!HAS_WEAK_HEAP_OBJECT_TAG(object.ptr()));
291cb0ef41Sopenharmony_ci  return MaybeObject(object.ptr());
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciMaybeObject MaybeObject::MakeWeak(MaybeObject object) {
331cb0ef41Sopenharmony_ci  DCHECK(object.IsStrongOrWeak());
341cb0ef41Sopenharmony_ci  return MaybeObject(object.ptr() | kWeakHeapObjectMask);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// static
381cb0ef41Sopenharmony_ciMaybeObject MaybeObject::Create(MaybeObject o) { return o; }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// static
411cb0ef41Sopenharmony_ciMaybeObject MaybeObject::Create(Object o) { return FromObject(o); }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// static
441cb0ef41Sopenharmony_ciMaybeObject MaybeObject::Create(Smi smi) { return FromSmi(smi); }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci//
471cb0ef41Sopenharmony_ci// HeapObjectReference implementation.
481cb0ef41Sopenharmony_ci//
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciHeapObjectReference::HeapObjectReference(Object object)
511cb0ef41Sopenharmony_ci    : MaybeObject(object.ptr()) {}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// static
541cb0ef41Sopenharmony_ciHeapObjectReference HeapObjectReference::Strong(Object object) {
551cb0ef41Sopenharmony_ci  DCHECK(!object.IsSmi());
561cb0ef41Sopenharmony_ci  DCHECK(!HasWeakHeapObjectTag(object));
571cb0ef41Sopenharmony_ci  return HeapObjectReference(object);
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci// static
611cb0ef41Sopenharmony_ciHeapObjectReference HeapObjectReference::Weak(Object object) {
621cb0ef41Sopenharmony_ci  DCHECK(!object.IsSmi());
631cb0ef41Sopenharmony_ci  DCHECK(!HasWeakHeapObjectTag(object));
641cb0ef41Sopenharmony_ci  return HeapObjectReference(object.ptr() | kWeakHeapObjectMask);
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// static
681cb0ef41Sopenharmony_ciHeapObjectReference HeapObjectReference::From(Object object,
691cb0ef41Sopenharmony_ci                                              HeapObjectReferenceType type) {
701cb0ef41Sopenharmony_ci  DCHECK(!object.IsSmi());
711cb0ef41Sopenharmony_ci  DCHECK(!HasWeakHeapObjectTag(object));
721cb0ef41Sopenharmony_ci  switch (type) {
731cb0ef41Sopenharmony_ci    case HeapObjectReferenceType::STRONG:
741cb0ef41Sopenharmony_ci      return HeapObjectReference::Strong(object);
751cb0ef41Sopenharmony_ci    case HeapObjectReferenceType::WEAK:
761cb0ef41Sopenharmony_ci      return HeapObjectReference::Weak(object);
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// static
811cb0ef41Sopenharmony_ciHeapObjectReference HeapObjectReference::ClearedValue(
821cb0ef41Sopenharmony_ci    PtrComprCageBase cage_base) {
831cb0ef41Sopenharmony_ci  // Construct cleared weak ref value.
841cb0ef41Sopenharmony_ci#ifdef V8_COMPRESS_POINTERS
851cb0ef41Sopenharmony_ci  // This is necessary to make pointer decompression computation also
861cb0ef41Sopenharmony_ci  // suitable for cleared weak references.
871cb0ef41Sopenharmony_ci  Address raw_value =
881cb0ef41Sopenharmony_ci      DecompressTaggedPointer(cage_base, kClearedWeakHeapObjectLower32);
891cb0ef41Sopenharmony_ci#else
901cb0ef41Sopenharmony_ci  Address raw_value = kClearedWeakHeapObjectLower32;
911cb0ef41Sopenharmony_ci#endif
921cb0ef41Sopenharmony_ci  // The rest of the code will check only the lower 32-bits.
931cb0ef41Sopenharmony_ci  DCHECK_EQ(kClearedWeakHeapObjectLower32, static_cast<uint32_t>(raw_value));
941cb0ef41Sopenharmony_ci  return HeapObjectReference(raw_value);
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_citemplate <typename THeapObjectSlot>
981cb0ef41Sopenharmony_civoid HeapObjectReference::Update(THeapObjectSlot slot, HeapObject value) {
991cb0ef41Sopenharmony_ci  static_assert(std::is_same<THeapObjectSlot, FullHeapObjectSlot>::value ||
1001cb0ef41Sopenharmony_ci                    std::is_same<THeapObjectSlot, HeapObjectSlot>::value,
1011cb0ef41Sopenharmony_ci                "Only FullHeapObjectSlot and HeapObjectSlot are expected here");
1021cb0ef41Sopenharmony_ci  Address old_value = (*slot).ptr();
1031cb0ef41Sopenharmony_ci  DCHECK(!HAS_SMI_TAG(old_value));
1041cb0ef41Sopenharmony_ci  Address new_value = value.ptr();
1051cb0ef41Sopenharmony_ci  DCHECK(Internals::HasHeapObjectTag(new_value));
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci#ifdef DEBUG
1081cb0ef41Sopenharmony_ci  bool weak_before = HAS_WEAK_HEAP_OBJECT_TAG(old_value);
1091cb0ef41Sopenharmony_ci#endif
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  slot.store(
1121cb0ef41Sopenharmony_ci      HeapObjectReference(new_value | (old_value & kWeakHeapObjectMask)));
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci#ifdef DEBUG
1151cb0ef41Sopenharmony_ci  bool weak_after = HAS_WEAK_HEAP_OBJECT_TAG((*slot).ptr());
1161cb0ef41Sopenharmony_ci  DCHECK_EQ(weak_before, weak_after);
1171cb0ef41Sopenharmony_ci#endif
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci}  // namespace internal
1211cb0ef41Sopenharmony_ci}  // namespace v8
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci#endif  // V8_OBJECTS_MAYBE_OBJECT_INL_H_
124