property.cc 5.28 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
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;
}

27 28 29 30 31 32 33 34 35 36
std::ostream& operator<<(std::ostream& os, PropertyConstness constness) {
  switch (constness) {
    case PropertyConstness::kMutable:
      return os << "mutable";
    case PropertyConstness::kConst:
      return os << "const";
  }
  UNREACHABLE();
}

37
Descriptor::Descriptor() : details_(Smi::zero()) {}
38

39
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
40 41 42 43 44 45 46 47 48 49 50
                       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());
}

51
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
52 53 54 55 56 57
                       PropertyDetails details)
    : key_(key), value_(value), details_(details) {
  DCHECK(key->IsUniqueName());
  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
}

58 59
Descriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
                                 int field_index, PropertyAttributes attributes,
60
                                 Representation representation) {
61
  return DataField(key, field_index, attributes, PropertyConstness::kMutable,
62
                   representation, MaybeObjectHandle(FieldType::Any(isolate)));
63 64 65 66 67 68
}

Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
                                 PropertyAttributes attributes,
                                 PropertyConstness constness,
                                 Representation representation,
69
                                 const MaybeObjectHandle& wrapped_field_type) {
70
  DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeak());
71 72 73
  PropertyDetails details(kData, attributes, kField, constness, representation,
                          field_index);
  return Descriptor(key, wrapped_field_type, details);
74
}
75

76 77
Descriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
                                    PropertyAttributes attributes) {
78
  IsolateRoot isolate = GetIsolateForPtrCompr(*key);
79 80
  return Descriptor(key, MaybeObjectHandle(value), kData, attributes,
                    kDescriptor, PropertyConstness::kConst,
81
                    value->OptimalRepresentation(isolate), 0);
82 83
}

84 85
Descriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
                                    int field_index, Handle<Object> value,
86
                                    PropertyAttributes attributes) {
87 88 89
  MaybeObjectHandle any_type(FieldType::Any(), isolate);
  return DataField(key, field_index, attributes, PropertyConstness::kConst,
                   Representation::Tagged(), any_type);
90 91
}

92 93 94 95 96 97 98 99
Descriptor Descriptor::AccessorConstant(Handle<Name> key,
                                        Handle<Object> foreign,
                                        PropertyAttributes attributes) {
  return Descriptor(key, MaybeObjectHandle(foreign), kAccessor, attributes,
                    kDescriptor, PropertyConstness::kConst,
                    Representation::Tagged(), 0);
}

100
// Outputs PropertyDetails as a dictionary details.
101
void PropertyDetails::PrintAsSlowTo(std::ostream& os, bool print_dict_index) {
102
  os << "(";
103
  if (constness() == PropertyConstness::kConst) os << "const ";
104
  os << (kind() == kData ? "data" : "accessor");
105 106 107
  if (print_dict_index) {
    os << ", dict_index: " << dictionary_index();
  }
108
  os << ", attrs: " << attributes() << ")";
109 110 111
}

// Outputs PropertyDetails as a descriptor array details.
112
void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
113
  os << "(";
114
  if (constness() == PropertyConstness::kConst) os << "const ";
115 116 117 118 119 120 121 122 123 124 125 126 127 128
  os << (kind() == kData ? "data" : "accessor");
  if (location() == kField) {
    os << " field";
    if (mode & kPrintFieldIndex) {
      os << " " << field_index();
    }
    if (mode & kPrintRepresentation) {
      os << ":" << representation().Mnemonic();
    }
  } else {
    os << " descriptor";
  }
  if (mode & kPrintPointer) {
    os << ", p: " << pointer();
129
  }
130 131
  if (mode & kPrintAttributes) {
    os << ", attrs: " << attributes();
132
  }
133
  os << ")";
134 135 136 137
}

#ifdef OBJECT_PRINT
void PropertyDetails::Print(bool dictionary_mode) {
138
  StdoutStream os;
139
  if (dictionary_mode) {
140
    PrintAsSlowTo(os, true);
141
  } else {
142
    PrintAsFastTo(os, PrintMode::kPrintFull);
143 144
  }
  os << "\n" << std::flush;
145
}
146
#endif
147

148 149
}  // namespace internal
}  // namespace v8