1 // Copyright 2016 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/field-type.h" 6 7 #include "src/handles/handles-inl.h" 8 #include "src/objects/objects-inl.h" 9 #include "src/objects/smi.h" 10 #include "src/utils/ostreams.h" 11 12 namespace v8 { 13 namespace internal { 14 15 // static None()16FieldType FieldType::None() { return FieldType(Smi::FromInt(2).ptr()); } 17 18 // static Any()19FieldType FieldType::Any() { return FieldType(Smi::FromInt(1).ptr()); } 20 21 // static None(Isolate * isolate)22Handle<FieldType> FieldType::None(Isolate* isolate) { 23 return handle(None(), isolate); 24 } 25 26 // static Any(Isolate * isolate)27Handle<FieldType> FieldType::Any(Isolate* isolate) { 28 return handle(Any(), isolate); 29 } 30 31 // static Class(Map map)32FieldType FieldType::Class(Map map) { return FieldType::cast(map); } 33 34 // static Class(Handle<Map> map,Isolate * isolate)35Handle<FieldType> FieldType::Class(Handle<Map> map, Isolate* isolate) { 36 return handle(Class(*map), isolate); 37 } 38 39 // static cast(Object object)40FieldType FieldType::cast(Object object) { 41 DCHECK(object == None() || object == Any() || object.IsMap()); 42 return FieldType(object.ptr()); 43 } 44 IsClass() const45bool FieldType::IsClass() const { return this->IsMap(); } 46 AsClass() const47Map FieldType::AsClass() const { 48 DCHECK(IsClass()); 49 return Map::cast(*this); 50 } 51 NowStable() const52bool FieldType::NowStable() const { 53 return !this->IsClass() || AsClass().is_stable(); 54 } 55 NowIs(FieldType other) const56bool FieldType::NowIs(FieldType other) const { 57 if (other.IsAny()) return true; 58 if (IsNone()) return true; 59 if (other.IsNone()) return false; 60 if (IsAny()) return false; 61 DCHECK(IsClass()); 62 DCHECK(other.IsClass()); 63 return *this == other; 64 } 65 Equals(FieldType other) const66bool FieldType::Equals(FieldType other) const { 67 if (IsAny() && other.IsAny()) return true; 68 if (IsNone() && other.IsNone()) return true; 69 if (IsClass() && other.IsClass()) { 70 return *this == other; 71 } 72 return false; 73 } 74 NowIs(Handle<FieldType> other) const75bool FieldType::NowIs(Handle<FieldType> other) const { return NowIs(*other); } 76 PrintTo(std::ostream & os) const77void FieldType::PrintTo(std::ostream& os) const { 78 if (IsAny()) { 79 os << "Any"; 80 } else if (IsNone()) { 81 os << "None"; 82 } else { 83 DCHECK(IsClass()); 84 os << "Class(" << reinterpret_cast<void*>(AsClass().ptr()) << ")"; 85 } 86 } 87 NowContains(Object value) const88bool FieldType::NowContains(Object value) const { 89 if (*this == Any()) return true; 90 if (*this == None()) return false; 91 if (!value.IsHeapObject()) return false; 92 return HeapObject::cast(value).map() == Map::cast(*this); 93 } 94 95 } // namespace internal 96 } // namespace v8 97