11cb0ef41Sopenharmony_ci// Copyright 2014 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#include "src/objects/property.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/handles/handles-inl.h"
81cb0ef41Sopenharmony_ci#include "src/objects/field-type.h"
91cb0ef41Sopenharmony_ci#include "src/objects/name-inl.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci#include "src/objects/smi.h"
121cb0ef41Sopenharmony_ci#include "src/utils/ostreams.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& os,
181cb0ef41Sopenharmony_ci                         const Representation& representation) {
191cb0ef41Sopenharmony_ci  switch (representation.kind()) {
201cb0ef41Sopenharmony_ci    case Representation::kNone:
211cb0ef41Sopenharmony_ci      return os << "none";
221cb0ef41Sopenharmony_ci    case Representation::kSmi:
231cb0ef41Sopenharmony_ci      return os << "smi";
241cb0ef41Sopenharmony_ci    case Representation::kDouble:
251cb0ef41Sopenharmony_ci      return os << "double";
261cb0ef41Sopenharmony_ci    case Representation::kHeapObject:
271cb0ef41Sopenharmony_ci      return os << "heap-object";
281cb0ef41Sopenharmony_ci    case Representation::kTagged:
291cb0ef41Sopenharmony_ci      return os << "tagged";
301cb0ef41Sopenharmony_ci    case Representation::kWasmValue:
311cb0ef41Sopenharmony_ci      return os << "wasm-value";
321cb0ef41Sopenharmony_ci    case Representation::kNumRepresentations:
331cb0ef41Sopenharmony_ci      UNREACHABLE();
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci  UNREACHABLE();
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& os,
391cb0ef41Sopenharmony_ci                         const PropertyAttributes& attributes) {
401cb0ef41Sopenharmony_ci  os << "[";
411cb0ef41Sopenharmony_ci  os << (((attributes & READ_ONLY) == 0) ? "W" : "_");    // writable
421cb0ef41Sopenharmony_ci  os << (((attributes & DONT_ENUM) == 0) ? "E" : "_");    // enumerable
431cb0ef41Sopenharmony_ci  os << (((attributes & DONT_DELETE) == 0) ? "C" : "_");  // configurable
441cb0ef41Sopenharmony_ci  os << "]";
451cb0ef41Sopenharmony_ci  return os;
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& os, PropertyConstness constness) {
491cb0ef41Sopenharmony_ci  switch (constness) {
501cb0ef41Sopenharmony_ci    case PropertyConstness::kMutable:
511cb0ef41Sopenharmony_ci      return os << "mutable";
521cb0ef41Sopenharmony_ci    case PropertyConstness::kConst:
531cb0ef41Sopenharmony_ci      return os << "const";
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci  UNREACHABLE();
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciDescriptor::Descriptor() : details_(Smi::zero()) {}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciDescriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
611cb0ef41Sopenharmony_ci                       PropertyKind kind, PropertyAttributes attributes,
621cb0ef41Sopenharmony_ci                       PropertyLocation location, PropertyConstness constness,
631cb0ef41Sopenharmony_ci                       Representation representation, int field_index)
641cb0ef41Sopenharmony_ci    : key_(key),
651cb0ef41Sopenharmony_ci      value_(value),
661cb0ef41Sopenharmony_ci      details_(kind, attributes, location, constness, representation,
671cb0ef41Sopenharmony_ci               field_index) {
681cb0ef41Sopenharmony_ci  DCHECK(key->IsUniqueName());
691cb0ef41Sopenharmony_ci  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciDescriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
731cb0ef41Sopenharmony_ci                       PropertyDetails details)
741cb0ef41Sopenharmony_ci    : key_(key), value_(value), details_(details) {
751cb0ef41Sopenharmony_ci  DCHECK(key->IsUniqueName());
761cb0ef41Sopenharmony_ci  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciDescriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
801cb0ef41Sopenharmony_ci                                 int field_index, PropertyAttributes attributes,
811cb0ef41Sopenharmony_ci                                 Representation representation) {
821cb0ef41Sopenharmony_ci  return DataField(key, field_index, attributes, PropertyConstness::kMutable,
831cb0ef41Sopenharmony_ci                   representation, MaybeObjectHandle(FieldType::Any(isolate)));
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciDescriptor Descriptor::DataField(Handle<Name> key, int field_index,
871cb0ef41Sopenharmony_ci                                 PropertyAttributes attributes,
881cb0ef41Sopenharmony_ci                                 PropertyConstness constness,
891cb0ef41Sopenharmony_ci                                 Representation representation,
901cb0ef41Sopenharmony_ci                                 const MaybeObjectHandle& wrapped_field_type) {
911cb0ef41Sopenharmony_ci  DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeak());
921cb0ef41Sopenharmony_ci  PropertyDetails details(PropertyKind::kData, attributes,
931cb0ef41Sopenharmony_ci                          PropertyLocation::kField, constness, representation,
941cb0ef41Sopenharmony_ci                          field_index);
951cb0ef41Sopenharmony_ci  return Descriptor(key, wrapped_field_type, details);
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciDescriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
991cb0ef41Sopenharmony_ci                                    PropertyAttributes attributes) {
1001cb0ef41Sopenharmony_ci  PtrComprCageBase cage_base = GetPtrComprCageBase(*key);
1011cb0ef41Sopenharmony_ci  return Descriptor(key, MaybeObjectHandle(value), PropertyKind::kData,
1021cb0ef41Sopenharmony_ci                    attributes, PropertyLocation::kDescriptor,
1031cb0ef41Sopenharmony_ci                    PropertyConstness::kConst,
1041cb0ef41Sopenharmony_ci                    value->OptimalRepresentation(cage_base), 0);
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciDescriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
1081cb0ef41Sopenharmony_ci                                    int field_index, Handle<Object> value,
1091cb0ef41Sopenharmony_ci                                    PropertyAttributes attributes) {
1101cb0ef41Sopenharmony_ci  MaybeObjectHandle any_type(FieldType::Any(), isolate);
1111cb0ef41Sopenharmony_ci  return DataField(key, field_index, attributes, PropertyConstness::kConst,
1121cb0ef41Sopenharmony_ci                   Representation::Tagged(), any_type);
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ciDescriptor Descriptor::AccessorConstant(Handle<Name> key,
1161cb0ef41Sopenharmony_ci                                        Handle<Object> foreign,
1171cb0ef41Sopenharmony_ci                                        PropertyAttributes attributes) {
1181cb0ef41Sopenharmony_ci  return Descriptor(key, MaybeObjectHandle(foreign), PropertyKind::kAccessor,
1191cb0ef41Sopenharmony_ci                    attributes, PropertyLocation::kDescriptor,
1201cb0ef41Sopenharmony_ci                    PropertyConstness::kConst, Representation::Tagged(), 0);
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci// Outputs PropertyDetails as a dictionary details.
1241cb0ef41Sopenharmony_civoid PropertyDetails::PrintAsSlowTo(std::ostream& os, bool print_dict_index) {
1251cb0ef41Sopenharmony_ci  os << "(";
1261cb0ef41Sopenharmony_ci  if (constness() == PropertyConstness::kConst) os << "const ";
1271cb0ef41Sopenharmony_ci  os << (kind() == PropertyKind::kData ? "data" : "accessor");
1281cb0ef41Sopenharmony_ci  if (print_dict_index) {
1291cb0ef41Sopenharmony_ci    os << ", dict_index: " << dictionary_index();
1301cb0ef41Sopenharmony_ci  }
1311cb0ef41Sopenharmony_ci  os << ", attrs: " << attributes() << ")";
1321cb0ef41Sopenharmony_ci}
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci// Outputs PropertyDetails as a descriptor array details.
1351cb0ef41Sopenharmony_civoid PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
1361cb0ef41Sopenharmony_ci  os << "(";
1371cb0ef41Sopenharmony_ci  if (constness() == PropertyConstness::kConst) os << "const ";
1381cb0ef41Sopenharmony_ci  os << (kind() == PropertyKind::kData ? "data" : "accessor");
1391cb0ef41Sopenharmony_ci  if (location() == PropertyLocation::kField) {
1401cb0ef41Sopenharmony_ci    os << " field";
1411cb0ef41Sopenharmony_ci    if (mode & kPrintFieldIndex) {
1421cb0ef41Sopenharmony_ci      os << " " << field_index();
1431cb0ef41Sopenharmony_ci    }
1441cb0ef41Sopenharmony_ci    if (mode & kPrintRepresentation) {
1451cb0ef41Sopenharmony_ci      os << ":" << representation().Mnemonic();
1461cb0ef41Sopenharmony_ci    }
1471cb0ef41Sopenharmony_ci  } else {
1481cb0ef41Sopenharmony_ci    os << " descriptor";
1491cb0ef41Sopenharmony_ci  }
1501cb0ef41Sopenharmony_ci  if (mode & kPrintPointer) {
1511cb0ef41Sopenharmony_ci    os << ", p: " << pointer();
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci  if (mode & kPrintAttributes) {
1541cb0ef41Sopenharmony_ci    os << ", attrs: " << attributes();
1551cb0ef41Sopenharmony_ci  }
1561cb0ef41Sopenharmony_ci  os << ")";
1571cb0ef41Sopenharmony_ci}
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci#ifdef OBJECT_PRINT
1601cb0ef41Sopenharmony_civoid PropertyDetails::Print(bool dictionary_mode) {
1611cb0ef41Sopenharmony_ci  StdoutStream os;
1621cb0ef41Sopenharmony_ci  if (dictionary_mode) {
1631cb0ef41Sopenharmony_ci    PrintAsSlowTo(os, true);
1641cb0ef41Sopenharmony_ci  } else {
1651cb0ef41Sopenharmony_ci    PrintAsFastTo(os, PrintMode::kPrintFull);
1661cb0ef41Sopenharmony_ci  }
1671cb0ef41Sopenharmony_ci  os << "\n" << std::flush;
1681cb0ef41Sopenharmony_ci}
1691cb0ef41Sopenharmony_ci#endif
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci}  // namespace internal
1721cb0ef41Sopenharmony_ci}  // namespace v8
173