• Home
  • 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/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 namespace v8 {
15 namespace internal {
16 
operator <<(std::ostream & os,const Representation & representation)17 std::ostream& operator<<(std::ostream& os,
18                          const Representation& representation) {
19   switch (representation.kind()) {
20     case Representation::kNone:
21       return os << "none";
22     case Representation::kSmi:
23       return os << "smi";
24     case Representation::kDouble:
25       return os << "double";
26     case Representation::kHeapObject:
27       return os << "heap-object";
28     case Representation::kTagged:
29       return os << "tagged";
30     case Representation::kWasmValue:
31       return os << "wasm-value";
32     case Representation::kNumRepresentations:
33       UNREACHABLE();
34   }
35   UNREACHABLE();
36 }
37 
operator <<(std::ostream & os,const PropertyAttributes & attributes)38 std::ostream& operator<<(std::ostream& os,
39                          const PropertyAttributes& attributes) {
40   os << "[";
41   os << (((attributes & READ_ONLY) == 0) ? "W" : "_");    // writable
42   os << (((attributes & DONT_ENUM) == 0) ? "E" : "_");    // enumerable
43   os << (((attributes & DONT_DELETE) == 0) ? "C" : "_");  // configurable
44   os << "]";
45   return os;
46 }
47 
operator <<(std::ostream & os,PropertyConstness constness)48 std::ostream& operator<<(std::ostream& os, PropertyConstness constness) {
49   switch (constness) {
50     case PropertyConstness::kMutable:
51       return os << "mutable";
52     case PropertyConstness::kConst:
53       return os << "const";
54   }
55   UNREACHABLE();
56 }
57 
Descriptor()58 Descriptor::Descriptor() : details_(Smi::zero()) {}
59 
Descriptor(Handle<Name> key,const MaybeObjectHandle & value,PropertyKind kind,PropertyAttributes attributes,PropertyLocation location,PropertyConstness constness,Representation representation,int field_index)60 Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
61                        PropertyKind kind, PropertyAttributes attributes,
62                        PropertyLocation location, PropertyConstness constness,
63                        Representation representation, int field_index)
64     : key_(key),
65       value_(value),
66       details_(kind, attributes, location, constness, representation,
67                field_index) {
68   DCHECK(key->IsUniqueName());
69   DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
70 }
71 
Descriptor(Handle<Name> key,const MaybeObjectHandle & value,PropertyDetails details)72 Descriptor::Descriptor(Handle<Name> key, const MaybeObjectHandle& value,
73                        PropertyDetails details)
74     : key_(key), value_(value), details_(details) {
75   DCHECK(key->IsUniqueName());
76   DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
77 }
78 
DataField(Isolate * isolate,Handle<Name> key,int field_index,PropertyAttributes attributes,Representation representation)79 Descriptor Descriptor::DataField(Isolate* isolate, Handle<Name> key,
80                                  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 
DataField(Handle<Name> key,int field_index,PropertyAttributes attributes,PropertyConstness constness,Representation representation,const MaybeObjectHandle & wrapped_field_type)86 Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
87                                  PropertyAttributes attributes,
88                                  PropertyConstness constness,
89                                  Representation representation,
90                                  const MaybeObjectHandle& wrapped_field_type) {
91   DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeak());
92   PropertyDetails details(PropertyKind::kData, attributes,
93                           PropertyLocation::kField, constness, representation,
94                           field_index);
95   return Descriptor(key, wrapped_field_type, details);
96 }
97 
DataConstant(Handle<Name> key,Handle<Object> value,PropertyAttributes attributes)98 Descriptor Descriptor::DataConstant(Handle<Name> key, Handle<Object> value,
99                                     PropertyAttributes attributes) {
100   PtrComprCageBase cage_base = GetPtrComprCageBase(*key);
101   return Descriptor(key, MaybeObjectHandle(value), PropertyKind::kData,
102                     attributes, PropertyLocation::kDescriptor,
103                     PropertyConstness::kConst,
104                     value->OptimalRepresentation(cage_base), 0);
105 }
106 
DataConstant(Isolate * isolate,Handle<Name> key,int field_index,Handle<Object> value,PropertyAttributes attributes)107 Descriptor Descriptor::DataConstant(Isolate* isolate, Handle<Name> key,
108                                     int field_index, Handle<Object> value,
109                                     PropertyAttributes attributes) {
110   MaybeObjectHandle any_type(FieldType::Any(), isolate);
111   return DataField(key, field_index, attributes, PropertyConstness::kConst,
112                    Representation::Tagged(), any_type);
113 }
114 
AccessorConstant(Handle<Name> key,Handle<Object> foreign,PropertyAttributes attributes)115 Descriptor Descriptor::AccessorConstant(Handle<Name> key,
116                                         Handle<Object> foreign,
117                                         PropertyAttributes attributes) {
118   return Descriptor(key, MaybeObjectHandle(foreign), PropertyKind::kAccessor,
119                     attributes, PropertyLocation::kDescriptor,
120                     PropertyConstness::kConst, Representation::Tagged(), 0);
121 }
122 
123 // Outputs PropertyDetails as a dictionary details.
PrintAsSlowTo(std::ostream & os,bool print_dict_index)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   if (print_dict_index) {
129     os << ", dict_index: " << dictionary_index();
130   }
131   os << ", attrs: " << attributes() << ")";
132 }
133 
134 // Outputs PropertyDetails as a descriptor array details.
PrintAsFastTo(std::ostream & os,PrintMode mode)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     os << " field";
141     if (mode & kPrintFieldIndex) {
142       os << " " << field_index();
143     }
144     if (mode & kPrintRepresentation) {
145       os << ":" << representation().Mnemonic();
146     }
147   } else {
148     os << " descriptor";
149   }
150   if (mode & kPrintPointer) {
151     os << ", p: " << pointer();
152   }
153   if (mode & kPrintAttributes) {
154     os << ", attrs: " << attributes();
155   }
156   os << ")";
157 }
158 
159 #ifdef OBJECT_PRINT
Print(bool dictionary_mode)160 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 }  // namespace internal
172 }  // namespace v8
173