property.cc 5.14 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/property.h"
6

7
#include "src/field-type.h"
8
#include "src/handles-inl.h"
9
#include "src/objects-inl.h"
10
#include "src/objects/name-inl.h"
11
#include "src/objects/smi.h"
12
#include "src/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
Descriptor::Descriptor() : details_(Smi::zero()) {}
28

29
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
30 31 32 33 34 35 36 37 38 39 40
                       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());
}

41
Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
42 43 44 45 46 47
                       PropertyDetails details)
    : key_(key), value_(value), details_(details) {
  DCHECK(key->IsUniqueName());
  DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
}

48 49
Descriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
                                 int field_index, PropertyAttributes attributes,
50
                                 Representation representation) {
51
  return DataField(key, field_index, attributes, PropertyConstness::kMutable,
52
                   representation, MaybeObjectHandle(FieldType::Any(isolate)));
53 54 55 56 57 58
}

Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
                                 PropertyAttributes attributes,
                                 PropertyConstness constness,
                                 Representation representation,
59
                                 const MaybeObjectHandle& wrapped_field_type) {
60
  DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeak());
61 62 63
  PropertyDetails details(kData, attributes, kField, constness, representation,
                          field_index);
  return Descriptor(key, wrapped_field_type, details);
64
}
65

66 67 68 69 70 71 72
Descriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
                                    PropertyAttributes attributes) {
  return Descriptor(key, MaybeObjectHandle(value), kData, attributes,
                    kDescriptor, PropertyConstness::kConst,
                    value->OptimalRepresentation(), 0);
}

73 74
Descriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
                                    int field_index, Handle<Object> value,
75 76
                                    PropertyAttributes attributes) {
  if (FLAG_track_constant_fields) {
77
    MaybeObjectHandle any_type(FieldType::Any(), isolate);
78
    return DataField(key, field_index, attributes, PropertyConstness::kConst,
79 80 81
                     Representation::Tagged(), any_type);

  } else {
82 83 84
    return Descriptor(key, MaybeObjectHandle(value), kData, attributes,
                      kDescriptor, PropertyConstness::kConst,
                      value->OptimalRepresentation(), field_index);
85 86 87
  }
}

88 89 90 91 92 93 94 95
Descriptor Descriptor::AccessorConstant(Handle<Name> key,
                                        Handle<Object> foreign,
                                        PropertyAttributes attributes) {
  return Descriptor(key, MaybeObjectHandle(foreign), kAccessor, attributes,
                    kDescriptor, PropertyConstness::kConst,
                    Representation::Tagged(), 0);
}

96
// Outputs PropertyDetails as a dictionary details.
97
void PropertyDetails::PrintAsSlowTo(std::ostream& os) {
98
  os << "(";
99
  if (constness() == PropertyConstness::kConst) os << "const ";
100
  os << (kind() == kData ? "data" : "accessor");
101
  os << ", dict_index: " << dictionary_index();
102
  os << ", attrs: " << attributes() << ")";
103 104 105
}

// Outputs PropertyDetails as a descriptor array details.
106
void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
107
  os << "(";
108
  if (constness() == PropertyConstness::kConst) os << "const ";
109 110 111 112 113 114 115 116 117 118 119 120 121 122
  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();
123
  }
124 125
  if (mode & kPrintAttributes) {
    os << ", attrs: " << attributes();
126
  }
127
  os << ")";
128 129 130 131
}

#ifdef OBJECT_PRINT
void PropertyDetails::Print(bool dictionary_mode) {
132
  StdoutStream os;
133
  if (dictionary_mode) {
134
    PrintAsSlowTo(os);
135
  } else {
136
    PrintAsFastTo(os, PrintMode::kPrintFull);
137 138
  }
  os << "\n" << std::flush;
139
}
140
#endif
141

142 143
}  // namespace internal
}  // namespace v8