• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  // Copyright 2014 the V8 project authors. All rights reserved.
2  // Use of this source code is governed by a BSD-style license that can be
3  // 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/ostreams.h"
12  
13  namespace v8 {
14  namespace internal {
15  
operator <<(std::ostream & os,const PropertyAttributes & attributes)16  std::ostream& operator<<(std::ostream& os,
17                           const PropertyAttributes& attributes) {
18    os << "[";
19    os << (((attributes & READ_ONLY) == 0) ? "W" : "_");    // writable
20    os << (((attributes & DONT_ENUM) == 0) ? "E" : "_");    // enumerable
21    os << (((attributes & DONT_DELETE) == 0) ? "C" : "_");  // configurable
22    os << "]";
23    return os;
24  }
25  
Descriptor()26  Descriptor::Descriptor() : details_(Smi::kZero) {}
27  
Descriptor(Handle<Name> key,MaybeObjectHandle value,PropertyKind kind,PropertyAttributes attributes,PropertyLocation location,PropertyConstness constness,Representation representation,int field_index)28  Descriptor::Descriptor(Handle<Name> key, MaybeObjectHandle value,
29                         PropertyKind kind, PropertyAttributes attributes,
30                         PropertyLocation location, PropertyConstness constness,
31                         Representation representation, int field_index)
32      : key_(key),
33        value_(value),
34        details_(kind, attributes, location, constness, representation,
35                 field_index) {
36    DCHECK(key->IsUniqueName());
37    DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
38  }
39  
Descriptor(Handle<Name> key,MaybeObjectHandle value,PropertyDetails details)40  Descriptor::Descriptor(Handle<Name> key, MaybeObjectHandle value,
41                         PropertyDetails details)
42      : key_(key), value_(value), details_(details) {
43    DCHECK(key->IsUniqueName());
44    DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
45  }
46  
DataField(Isolate * isolate,Handle<Name> key,int field_index,PropertyAttributes attributes,Representation representation)47  Descriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
48                                   int field_index, PropertyAttributes attributes,
49                                   Representation representation) {
50    return DataField(key, field_index, attributes, PropertyConstness::kMutable,
51                     representation, MaybeObjectHandle(FieldType::Any(isolate)));
52  }
53  
DataField(Handle<Name> key,int field_index,PropertyAttributes attributes,PropertyConstness constness,Representation representation,MaybeObjectHandle wrapped_field_type)54  Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
55                                   PropertyAttributes attributes,
56                                   PropertyConstness constness,
57                                   Representation representation,
58                                   MaybeObjectHandle wrapped_field_type) {
59    DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeakHeapObject());
60    PropertyDetails details(kData, attributes, kField, constness, representation,
61                            field_index);
62    return Descriptor(key, wrapped_field_type, details);
63  }
64  
DataConstant(Handle<Name> key,Handle<Object> value,PropertyAttributes attributes)65  Descriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
66                                      PropertyAttributes attributes) {
67    return Descriptor(key, MaybeObjectHandle(value), kData, attributes,
68                      kDescriptor, PropertyConstness::kConst,
69                      value->OptimalRepresentation(), 0);
70  }
71  
DataConstant(Isolate * isolate,Handle<Name> key,int field_index,Handle<Object> value,PropertyAttributes attributes)72  Descriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
73                                      int field_index, Handle<Object> value,
74                                      PropertyAttributes attributes) {
75    if (FLAG_track_constant_fields) {
76      MaybeObjectHandle any_type(FieldType::Any(), isolate);
77      return DataField(key, field_index, attributes, PropertyConstness::kConst,
78                       Representation::Tagged(), any_type);
79  
80    } else {
81      return Descriptor(key, MaybeObjectHandle(value), kData, attributes,
82                        kDescriptor, PropertyConstness::kConst,
83                        value->OptimalRepresentation(), field_index);
84    }
85  }
86  
AccessorConstant(Handle<Name> key,Handle<Object> foreign,PropertyAttributes attributes)87  Descriptor Descriptor::AccessorConstant(Handle<Name> key,
88                                          Handle<Object> foreign,
89                                          PropertyAttributes attributes) {
90    return Descriptor(key, MaybeObjectHandle(foreign), kAccessor, attributes,
91                      kDescriptor, PropertyConstness::kConst,
92                      Representation::Tagged(), 0);
93  }
94  
95  // Outputs PropertyDetails as a dictionary details.
PrintAsSlowTo(std::ostream & os)96  void PropertyDetails::PrintAsSlowTo(std::ostream& os) {
97    os << "(";
98    if (constness() == PropertyConstness::kConst) os << "const ";
99    os << (kind() == kData ? "data" : "accessor");
100    os << ", dict_index: " << dictionary_index();
101    os << ", attrs: " << attributes() << ")";
102  }
103  
104  // Outputs PropertyDetails as a descriptor array details.
PrintAsFastTo(std::ostream & os,PrintMode mode)105  void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
106    os << "(";
107    if (constness() == PropertyConstness::kConst) os << "const ";
108    os << (kind() == kData ? "data" : "accessor");
109    if (location() == kField) {
110      os << " field";
111      if (mode & kPrintFieldIndex) {
112        os << " " << field_index();
113      }
114      if (mode & kPrintRepresentation) {
115        os << ":" << representation().Mnemonic();
116      }
117    } else {
118      os << " descriptor";
119    }
120    if (mode & kPrintPointer) {
121      os << ", p: " << pointer();
122    }
123    if (mode & kPrintAttributes) {
124      os << ", attrs: " << attributes();
125    }
126    os << ")";
127  }
128  
129  #ifdef OBJECT_PRINT
Print(bool dictionary_mode)130  void PropertyDetails::Print(bool dictionary_mode) {
131    StdoutStream os;
132    if (dictionary_mode) {
133      PrintAsSlowTo(os);
134    } else {
135      PrintAsFastTo(os, PrintMode::kPrintFull);
136    }
137    os << "\n" << std::flush;
138  }
139  #endif
140  
141  }  // namespace internal
142  }  // namespace v8
143