current commit
261015.17 · deps: update V8 to 15.6.75.13
// Copyright 2015 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_CONTEXTS_INL_H_
#define V8_OBJECTS_CONTEXTS_INL_H_

#include "src/objects/contexts.h"
// Include the non-inl header before the rest of the headers.

#include "src/common/globals.h"
#include "src/heap/heap-write-barrier-inl.h"
#include "src/objects/casting.h"
#include "src/objects/fixed-array-inl.h"
#include "src/objects/heap-object-field-inl.h"
#include "src/objects/js-function-inl.h"
#include "src/objects/js-objects-inl.h"
#include "src/objects/map-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/ordered-hash-table-inl.h"
#include "src/objects/regexp-match-info.h"
#include "src/objects/scope-info.h"
#include "src/sandbox/cppheap-pointer-inl.h"

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

namespace v8 {
namespace internal {

SafeHeapObjectSize ScriptContextTable::length(AcquireLoadTag) const {
  return SafeHeapObjectSize(base::AsAtomic32::Acquire_Load(&length_));
}
void ScriptContextTable::set_length(uint32_t value, ReleaseStoreTag) {
  base::AsAtomic32::Release_Store(&length_, value);
}

Tagged<NameToIndexHashTable> ScriptContextTable::names_to_context_index()
    const {
  return names_to_context_index_.load();
}
void ScriptContextTable::set_names_to_context_index(
    Tagged<NameToIndexHashTable> value, WriteBarrierMode mode) {
  names_to_context_index_.store(this, value, mode);
}

Tagged<Context> ScriptContextTable::get(uint32_t i) const {
  DCHECK_LT(i, length(kAcquireLoad).value());
  return Super::get(i);
}

Tagged<Context> ScriptContextTable::get(uint32_t i, AcquireLoadTag tag) const {
  DCHECK_LT(i, length(tag).value());
  return Super::get(i, tag);
}

int Context::AllocatedSize() const { return SizeFor(length(kRelaxedLoad)); }

int Context::length() const { return length_.load().value(); }
void Context::set_length(int value) {
  length_.store_no_write_barrier(Smi::FromInt(value));
}
int Context::length(RelaxedLoadTag) const {
  return length_.Relaxed_Load().value();
}
void Context::set_length(int value, RelaxedStoreTag) {
  length_.Relaxed_Store_no_write_barrier(Smi::FromInt(value));
}

bool Context::IsElementTdzHole(int index) {
  Tagged<Object> value = get(index, kRelaxedLoad);
#ifdef V8_ENABLE_TDZ_HOLE
  DCHECK(!IsTheHole(value));
#endif
  return IsTdzHole(value);
}

template <typename MemoryTag>
Tagged<Object> Context::GetNoCell(int index, MemoryTag tag) {
  Tagged<Object> value = get(index, tag);
  DCHECK(!Is<ContextCell>(value));
#ifdef V8_ENABLE_TDZ_HOLE
  DCHECK(!IsTheHole(value));
#endif
  return value;
}

Tagged<Object> Context::GetNoCell(int index) {
  return GetNoCell(index, kRelaxedLoad);
}

template <typename MemoryTag>
void Context::SetNoCell(int index, Tagged<Object> value, MemoryTag tag,
                        WriteBarrierMode mode) {
  DCHECK(!Is<ContextCell>(get(index, kRelaxedLoad)));
#ifdef V8_ENABLE_TDZ_HOLE
  DCHECK(!IsTheHole(value));
#endif
  set(index, value, mode, tag);
}

void Context::SetNoCell(int index, Tagged<Object> value,
                        WriteBarrierMode mode) {
  SetNoCell(index, value, kRelaxedStore, mode);
}

Tagged<Object> Context::get(int index, RelaxedLoadTag) const {
  DCHECK_LT(static_cast<unsigned int>(index),
            static_cast<unsigned int>(length(kRelaxedLoad)));
  return elements()[index].Relaxed_Load();
}

Tagged<Object> Context::get(int index, AcquireLoadTag) const {
  DCHECK_LT(static_cast<unsigned int>(index),
            static_cast<unsigned int>(length(kRelaxedLoad)));
  return elements()[index].Acquire_Load();
}

void Context::set(int index, Tagged<Object> value, WriteBarrierMode mode) {
  set(index, value, mode, kRelaxedStore);
}

void Context::set(int index, Tagged<Object> value, WriteBarrierMode mode,
                  RelaxedStoreTag) {
  DCHECK_LT(static_cast<unsigned int>(index),
            static_cast<unsigned int>(length(kRelaxedLoad)));
  elements()[index].Relaxed_Store(this, value, mode);
}

void Context::set(int index, Tagged<Object> value, WriteBarrierMode mode,
                  ReleaseStoreTag) {
  DCHECK_LT(static_cast<unsigned int>(index),
            static_cast<unsigned int>(length(kRelaxedLoad)));
  elements()[index].Release_Store(this, value, mode);
}

void NativeContext::set(int index, Tagged<Object> value, WriteBarrierMode mode,
                        ReleaseStoreTag tag) {
  Context::set(index, value, mode, tag);
}

Tagged<ScopeInfo> Context::scope_info() const {
  return Cast<ScopeInfo>(elements()[SCOPE_INFO_INDEX].load());
}
void Context::set_scope_info(Tagged<ScopeInfo> value, WriteBarrierMode mode) {
  elements()[SCOPE_INFO_INDEX].store(this, value, mode);
}

Tagged<Object> Context::unchecked_previous() const {
  return get(PREVIOUS_INDEX, kRelaxedLoad);
}

Tagged<Context> Context::previous() const {
  Tagged<Object> result = get(PREVIOUS_INDEX, kRelaxedLoad);
  DCHECK(IsBootstrappingOrValidParentContext(result, this));
  return UncheckedCast<Context>(result);
}
void Context::set_previous(Tagged<Context> context, WriteBarrierMode mode) {
  set(PREVIOUS_INDEX, context, mode);
}

Tagged<Object> Context::next_context_link() const {
  return get(Context::NEXT_CONTEXT_LINK, kRelaxedLoad);
}
void Context::set_next_context_link(Tagged<Object> object,
                                    WriteBarrierMode mode) {
  set(Context::NEXT_CONTEXT_LINK, object, mode);
}

bool Context::has_extension() const {
  return scope_info()->HasContextExtensionSlot() && !IsUndefined(extension());
}

Tagged<HeapObject> Context::extension() const {
  DCHECK(scope_info()->HasContextExtensionSlot());
  return Cast<HeapObject>(get(EXTENSION_INDEX, kRelaxedLoad));
}

Tagged<NativeContext> Context::native_context() const {
  return this->map()->native_context();
}

Tagged<JSGlobalObject> Context::global_object() const {
  return native_context()->global_object();
}

Tagged<JSGlobalProxy> Context::global_proxy() const {
  return native_context()->global_proxy();
}

bool Context::IsFunctionContext() const {
  return map()->instance_type() == FUNCTION_CONTEXT_TYPE;
}

bool Context::IsCatchContext() const {
  return map()->instance_type() == CATCH_CONTEXT_TYPE;
}

bool Context::IsWithContext() const {
  return map()->instance_type() == WITH_CONTEXT_TYPE;
}

bool Context::IsDebugEvaluateContext() const {
  return map()->instance_type() == DEBUG_EVALUATE_CONTEXT_TYPE;
}

bool Context::IsAwaitContext() const {
  return map()->instance_type() == AWAIT_CONTEXT_TYPE;
}

bool Context::IsBlockContext() const {
  return map()->instance_type() == BLOCK_CONTEXT_TYPE;
}

bool Context::IsModuleContext() const {
  return map()->instance_type() == MODULE_CONTEXT_TYPE;
}

bool Context::IsEvalContext() const {
  return map()->instance_type() == EVAL_CONTEXT_TYPE;
}

bool Context::IsScriptContext() const {
  return map()->instance_type() == SCRIPT_CONTEXT_TYPE;
}

inline bool Context::HasContextCells() const {
  return scope_info()->HasContextCells();
}

#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name)          \
  void Context::set_##name(Tagged<UNPAREN(type)> value) {          \
    DCHECK(Is<NativeContext>(this));                               \
    set(index, value, UPDATE_WRITE_BARRIER, kReleaseStore);        \
  }                                                                \
  bool Context::is_##name(Tagged<UNPAREN(type)> value) const {     \
    DCHECK(Is<NativeContext>(this));                               \
    return Cast<UNPAREN(type)>(get(index, kRelaxedLoad)) == value; \
  }                                                                \
  Tagged<UNPAREN(type)> Context::name() const {                    \
    DCHECK(Is<NativeContext>(this));                               \
    return Cast<UNPAREN(type)>(get(index, kRelaxedLoad));          \
  }                                                                \
  Tagged<UNPAREN(type)> Context::name(AcquireLoadTag tag) const {  \
    DCHECK(Is<NativeContext>(this));                               \
    return Cast<UNPAREN(type)>(get(index, tag));                   \
  }
NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
#undef NATIVE_CONTEXT_FIELD_ACCESSORS

Tagged<Map> Context::GetInitialJSArrayMap(ElementsKind kind) const {
  DCHECK(Is<NativeContext>(this));
  if (!IsFastElementsKind(kind)) return {};
  DisallowGarbageCollection no_gc;
  Tagged<Object> const initial_js_array_map =
      get(Context::ArrayMapIndex(kind), kRelaxedLoad);
  DCHECK(!IsUndefined(initial_js_array_map));
  return Cast<Map>(initial_js_array_map);
}

MicrotaskQueue* NativeContext::microtask_queue() const {
  i::IsolateForPointerCompression isolate = Isolate::Current();
  return microtask_queue(isolate);
}

MicrotaskQueue* NativeContext::microtask_queue(
    IsolateForPointerCompression isolate) const {
  return reinterpret_cast<MicrotaskQueue*>(
      ReadCppHeapPointerField<kMicrotaskQueueTag, kMicrotaskQueueTag>(
          kMicrotaskQueueOffset, isolate));
}

void NativeContext::set_microtask_queue(IsolateForPointerCompression isolate,
                                        MicrotaskQueue* queue) {
  WriteLazilyInitializedCppHeapPointerField(kMicrotaskQueueOffset, isolate,
                                            reinterpret_cast<Address>(queue),
                                            kMicrotaskQueueTag);
  WriteBarrier::ForCppHeapPointer(Tagged<NativeContext>(this),
                                  RawCppHeapPointerField(kMicrotaskQueueOffset),
                                  queue);
}

void NativeContext::init_microtask_queue(IsolateForPointerCompression isolate,
                                         MicrotaskQueue* queue) {
  SetupLazilyInitializedCppHeapPointerField(kMicrotaskQueueOffset);
  if (queue) {
    set_microtask_queue(isolate, queue);
  }
}

bool NativeContext::HasSameSecurityTokenAs(Tagged<NativeContext> that) const {
  return this == that || this->security_token() == that->security_token();
}

Tagged<JSGlobalProxy> NativeContext::global_proxy() const {
  return global_proxy_object();
}

bool NativeContext::IsDetached() const { return global_object()->IsDetached(); }

Tagged<JSGlobalObject> NativeContext::global_object() const {
  return Cast<JSGlobalObject>(extension());
}

// Caution, hack: this getter ignores the AcquireLoadTag. The global_object
// slot is safe to read concurrently since it is immutable after
// initialization.  This function should *not* be used from anywhere other
// than heap-refs.cc.
// TODO(jgruber): Remove this function after NativeContextRef is actually
// never serialized and BROKER_NATIVE_CONTEXT_FIELDS is removed.
Tagged<JSGlobalObject> NativeContext::global_object(AcquireLoadTag) const {
  return global_object();
}

void NativeContext::synchronized_set_script_context_table(
    Tagged<ScriptContextTable> script_context_table) {
  set(SCRIPT_CONTEXT_TABLE_INDEX, script_context_table, UPDATE_WRITE_BARRIER,
      kReleaseStore);
}

Tagged<ScriptContextTable> NativeContext::synchronized_script_context_table()
    const {
  return Cast<ScriptContextTable>(
      get(SCRIPT_CONTEXT_TABLE_INDEX, kAcquireLoad));
}

Tagged<Map> NativeContext::TypedArrayElementsKindToCtorMap(
    ElementsKind element_kind) const {
  int ctor_index = Context::FIRST_FIXED_TYPED_ARRAY_FUN_INDEX + element_kind -
                   ElementsKind::FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND;
  Tagged<Map> map =
      Cast<Map>(Cast<JSFunction>(get(ctor_index, kRelaxedLoad))->initial_map());
  DCHECK_EQ(map->elements_kind(), element_kind);
  DCHECK(InstanceTypeChecker::IsJSTypedArray(map));
  return map;
}

Tagged<Map> NativeContext::TypedArrayElementsKindToRabGsabCtorMap(
    ElementsKind element_kind) const {
  int ctor_index = Context::FIRST_RAB_GSAB_TYPED_ARRAY_MAP_INDEX +
                   element_kind -
                   ElementsKind::FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND;
  Tagged<Map> map = Cast<Map>(get(ctor_index, kRelaxedLoad));
  DCHECK_EQ(map->elements_kind(),
            GetCorrespondingRabGsabElementsKind(element_kind));
  DCHECK(InstanceTypeChecker::IsJSTypedArray(map));
  return map;
}

inline std::ostream& operator<<(std::ostream& os, ContextCell::State state) {
  switch (state) {
    case ContextCell::kConst:
      os << "const";
      break;
    case ContextCell::kSmi:
      os << "smi";
      break;
    case ContextCell::kInt32:
      os << "int32";
      break;
    case ContextCell::kFloat64:
      os << "float64";
      break;
    case ContextCell::kDetached:
      os << "detached";
      break;
  }
  return os;
}

ContextCell::State ContextCell::state() const {
  return state_.load(std::memory_order_acquire);
}

void ContextCell::set_state(ContextCell::State state) {
  return state_.store(state, std::memory_order_release);
}

Tagged<DependentCode> ContextCell::dependent_code() const {
  return dependent_code_.load();
}

void ContextCell::set_dependent_code(Tagged<DependentCode> value,
                                     WriteBarrierMode mode) {
  dependent_code_.store(this, value, mode);
}

Tagged<JSAny> ContextCell::tagged_value() const {
  return tagged_value_.Relaxed_Load();
}

void ContextCell::set_tagged_value(Tagged<JSAny> value, WriteBarrierMode mode) {
  tagged_value_.Relaxed_Store(this, value, mode);
}

void ContextCell::set_smi_value(Tagged<Smi> value) {
  tagged_value_.Relaxed_Store(this, value);
}

void ContextCell::clear_tagged_value() {
  set_tagged_value(Smi::FromInt(0), SKIP_WRITE_BARRIER);
}

void ContextCell::clear_padding() {
#if TAGGED_SIZE_8_BYTES
  optional_padding_ = 0;
#endif  // TAGGED_SIZE_8_BYTES
}

double ContextCell::float64_value() const {
  DCHECK_EQ(state(), kFloat64);
  return double_value_.value();
}

void ContextCell::set_float64_value(double value) {
  double_value_.set_value(value);
}

int32_t ContextCell::int32_value() const {
  DCHECK_EQ(state(), kInt32);
  return *reinterpret_cast<const int32_t*>(&double_value_);
}

void ContextCell::set_int32_value(int32_t value) {
  *reinterpret_cast<int32_t*>(&double_value_) = value;
}

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_CONTEXTS_INL_H_