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