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/ostreams.h"
11
12 namespace v8 {
13 namespace internal {
14
operator <<(std::ostream & os,const PropertyAttributes & attributes)15 std::ostream& operator<<(std::ostream& os,
16 const PropertyAttributes& attributes) {
17 os << "[";
18 os << (((attributes & READ_ONLY) == 0) ? "W" : "_"); // writable
19 os << (((attributes & DONT_ENUM) == 0) ? "E" : "_"); // enumerable
20 os << (((attributes & DONT_DELETE) == 0) ? "C" : "_"); // configurable
21 os << "]";
22 return os;
23 }
24
DataField(Handle<Name> key,int field_index,PropertyAttributes attributes,Representation representation)25 Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
26 PropertyAttributes attributes,
27 Representation representation) {
28 return DataField(key, field_index, attributes, kMutable, representation,
29 FieldType::Any(key->GetIsolate()));
30 }
31
DataField(Handle<Name> key,int field_index,PropertyAttributes attributes,PropertyConstness constness,Representation representation,Handle<Object> wrapped_field_type)32 Descriptor Descriptor::DataField(Handle<Name> key, int field_index,
33 PropertyAttributes attributes,
34 PropertyConstness constness,
35 Representation representation,
36 Handle<Object> wrapped_field_type) {
37 DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeakCell());
38 PropertyDetails details(kData, attributes, kField, constness, representation,
39 field_index);
40 return Descriptor(key, wrapped_field_type, details);
41 }
42
DataConstant(Handle<Name> key,int field_index,Handle<Object> value,PropertyAttributes attributes)43 Descriptor Descriptor::DataConstant(Handle<Name> key, int field_index,
44 Handle<Object> value,
45 PropertyAttributes attributes) {
46 if (FLAG_track_constant_fields) {
47 Handle<Object> any_type(FieldType::Any(), key->GetIsolate());
48 return DataField(key, field_index, attributes, kConst,
49 Representation::Tagged(), any_type);
50
51 } else {
52 return Descriptor(key, value, kData, attributes, kDescriptor, kConst,
53 value->OptimalRepresentation(), field_index);
54 }
55 }
56
57 // Outputs PropertyDetails as a dictionary details.
PrintAsSlowTo(std::ostream & os)58 void PropertyDetails::PrintAsSlowTo(std::ostream& os) {
59 os << "(";
60 if (constness() == kConst) os << "const ";
61 os << (kind() == kData ? "data" : "accessor");
62 os << ", dictionary_index: " << dictionary_index();
63 os << ", attrs: " << attributes() << ")";
64 }
65
66 // Outputs PropertyDetails as a descriptor array details.
PrintAsFastTo(std::ostream & os,PrintMode mode)67 void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
68 os << "(";
69 if (constness() == kConst) os << "const ";
70 os << (kind() == kData ? "data" : "accessor");
71 if (location() == kField) {
72 os << " field";
73 if (mode & kPrintFieldIndex) {
74 os << " " << field_index();
75 }
76 if (mode & kPrintRepresentation) {
77 os << ":" << representation().Mnemonic();
78 }
79 } else {
80 os << " descriptor";
81 }
82 if (mode & kPrintPointer) {
83 os << ", p: " << pointer();
84 }
85 if (mode & kPrintAttributes) {
86 os << ", attrs: " << attributes();
87 }
88 os << ")";
89 }
90
91 #ifdef OBJECT_PRINT
Print(bool dictionary_mode)92 void PropertyDetails::Print(bool dictionary_mode) {
93 OFStream os(stdout);
94 if (dictionary_mode) {
95 PrintAsSlowTo(os);
96 } else {
97 PrintAsFastTo(os, PrintMode::kPrintFull);
98 }
99 os << "\n" << std::flush;
100 }
101 #endif
102
103 } // namespace internal
104 } // namespace v8
105