property.cc 6.08 KB
Newer Older
1 2 3
// Copyright 2014 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.
4

5
#include "src/objects/property.h"
6

7
#include "src/handles/handles-inl.h"
8
#include "src/objects/field-type.h"
9
#include "src/objects/name-inl.h"
10
#include "src/objects/objects-inl.h"
11
#include "src/objects/smi.h"
12
#include "src/utils/ostreams.h"
13

14 15
namespace v8 {
namespace internal {
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
std::ostream& operator<<(std::ostream& os,
                         const Representation& representation) {
  switch (representation.kind()) {
    case Representation::kNone:
      return os << "none";
    case Representation::kSmi:
      return os << "smi";
    case Representation::kDouble:
      return os << "double";
    case Representation::kHeapObject:
      return os << "heap-object";
    case Representation::kTagged:
      return os << "tagged";
    case Representation::kWasmValue:
      return os << "wasm-value";
    case Representation::kNumRepresentations:
      UNREACHABLE();
  }
  UNREACHABLE();
}

38 39 40 41 42 43 44 45 46 47
std::ostream& operator<<(std::ostream& os,
                         const PropertyAttributes& attributes) {
  os << "[";
  os << (((attributes & READ_ONLY) == 0) ? "W" : "_");    // writable
  os << (((attributes & DONT_ENUM) == 0) ? "E" : "_");    // enumerable
  os << (((attributes & DONT_DELETE) == 0) ? "C" : "_");  // configurable
  os << "]";
  return os;
}

48 49 50 51 52 53 54 55 56 57
std::ostream& operator<<(std::ostream& os, PropertyConstness constness) {
  switch (constness) {
    case PropertyConstness::kMutable:
      return os << "mutable";
    case PropertyConstness::kConst:
      return os << "const";
  }
  UNREACHABLE();
}

58
Descriptor::Descriptor() : details_(Smi::zero()) {}
59

60
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
61 62 63 64 65 66 67 68 69 70 71
                       PropertyKind kind, PropertyAttributes attributes,
                       PropertyLocation location, PropertyConstness constness,
                       Representation representation, int field_index)
    : key_(key),
      value_(value),
      details_(kind, attributes, location, constness, representation,
               field_index) {
  DCHECK(key->IsUniqueName());
  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
}

72
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
73 74 75 76 77 78
                       PropertyDetails details)
    : key_(key), value_(value), details_(details) {
  DCHECK(key->IsUniqueName());
  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
}

79 80
Descriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
                                 int field_index, PropertyAttributes attributes,
81
                                 Representation representation) {
82
  return DataField(key, field_index, attributes, PropertyConstness::kMutable,
83
                   representation, MaybeObjectHandle(FieldType::Any(isolate)));
84 85 86 87 88 89
}

Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
                                 PropertyAttributes attributes,
                                 PropertyConstness constness,
                                 Representation representation,
90
                                 const MaybeObjectHandle& wrapped_field_type) {
91
  DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeak());
92 93 94
  PropertyDetails details(PropertyKind::kData, attributes,
                          PropertyLocation::kField, constness, representation,
                          field_index);
95
  return Descriptor(key, wrapped_field_type, details);
96
}
97

98 99
Descriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
                                    PropertyAttributes attributes) {
100
  PtrComprCageBase cage_base = GetPtrComprCageBase(*key);
101 102 103
  return Descriptor(key, MaybeObjectHandle(value), PropertyKind::kData,
                    attributes, PropertyLocation::kDescriptor,
                    PropertyConstness::kConst,
104
                    value->OptimalRepresentation(cage_base), 0);
105 106
}

107 108
Descriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
                                    int field_index, Handle<Object> value,
109
                                    PropertyAttributes attributes) {
110 111 112
  MaybeObjectHandle any_type(FieldType::Any(), isolate);
  return DataField(key, field_index, attributes, PropertyConstness::kConst,
                   Representation::Tagged(), any_type);
113 114
}

115 116 117
Descriptor Descriptor::AccessorConstant(Handle<Name> key,
                                        Handle<Object> foreign,
                                        PropertyAttributes attributes) {
118 119 120
  return Descriptor(key, MaybeObjectHandle(foreign), PropertyKind::kAccessor,
                    attributes, PropertyLocation::kDescriptor,
                    PropertyConstness::kConst, Representation::Tagged(), 0);
121 122
}

123
// Outputs PropertyDetails as a dictionary details.
124
void PropertyDetails::PrintAsSlowTo(std::ostream& os, bool print_dict_index) {
125
  os << "(";
126
  if (constness() == PropertyConstness::kConst) os << "const ";
127
  os << (kind() == PropertyKind::kData ? "data" : "accessor");
128 129 130
  if (print_dict_index) {
    os << ", dict_index: " << dictionary_index();
  }
131
  os << ", attrs: " << attributes() << ")";
132 133 134
}

// Outputs PropertyDetails as a descriptor array details.
135
void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
136
  os << "(";
137
  if (constness() == PropertyConstness::kConst) os << "const ";
138
  os << (kind() == PropertyKind::kData ? "data" : "accessor");
139
  if (location() == PropertyLocation::kField) {
140 141 142 143 144 145 146 147 148 149 150 151
    os << " field";
    if (mode & kPrintFieldIndex) {
      os << " " << field_index();
    }
    if (mode & kPrintRepresentation) {
      os << ":" << representation().Mnemonic();
    }
  } else {
    os << " descriptor";
  }
  if (mode & kPrintPointer) {
    os << ", p: " << pointer();
152
  }
153 154
  if (mode & kPrintAttributes) {
    os << ", attrs: " << attributes();
155
  }
156
  os << ")";
157 158 159 160
}

#ifdef OBJECT_PRINT
void PropertyDetails::Print(bool dictionary_mode) {
161
  StdoutStream os;
162
  if (dictionary_mode) {
163
    PrintAsSlowTo(os, true);
164
  } else {
165
    PrintAsFastTo(os, PrintMode::kPrintFull);
166 167
  }
  os << "\n" << std::flush;
168
}
169
#endif
170

171 172
}  // namespace internal
}  // namespace v8