1/*
2 * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef TypeSet_h
27#define TypeSet_h
28
29#include "RuntimeType.h"
30#include "StructureSet.h"
31#include <wtf/HashSet.h>
32#include <wtf/RefCounted.h>
33#include <wtf/text/WTFString.h>
34#include <wtf/Vector.h>
35
36namespace Inspector {
37namespace Protocol {
38template<typename T> class Array;
39
40namespace Runtime {
41class StructureDescription;
42class TypeSet;
43}
44
45}
46}
47
48namespace JSC {
49
50class StructureShape : public RefCounted<StructureShape> {
51 friend class TypeSet;
52
53public:
54 StructureShape();
55
56 static Ref<StructureShape> create() { return adoptRef(*new StructureShape); }
57 String propertyHash();
58 void markAsFinal();
59 void addProperty(UniquedStringImpl&);
60 String stringRepresentation();
61 String toJSONString() const;
62 Ref<Inspector::Protocol::Runtime::StructureDescription> inspectorRepresentation();
63 void setConstructorName(String name) { m_constructorName = (name.isEmpty() ? "Object" : name); }
64 String constructorName() { return m_constructorName; }
65 void setProto(PassRefPtr<StructureShape> shape) { m_proto = shape; }
66 void enterDictionaryMode();
67
68private:
69 static String leastCommonAncestor(const Vector<RefPtr<StructureShape>>);
70 static PassRefPtr<StructureShape> merge(const PassRefPtr<StructureShape>, const PassRefPtr<StructureShape>);
71 bool hasSamePrototypeChain(PassRefPtr<StructureShape>);
72
73 HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> m_fields;
74 HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> m_optionalFields;
75 RefPtr<StructureShape> m_proto;
76 std::unique_ptr<String> m_propertyHash;
77 String m_constructorName;
78 bool m_final;
79 bool m_isInDictionaryMode;
80};
81
82class TypeSet : public RefCounted<TypeSet> {
83
84public:
85 static Ref<TypeSet> create() { return adoptRef(*new TypeSet); }
86 TypeSet();
87 void addTypeInformation(RuntimeType, PassRefPtr<StructureShape>, Structure*);
88 void invalidateCache();
89 String dumpTypes() const;
90 String displayName() const;
91 Ref<Inspector::Protocol::Array<Inspector::Protocol::Runtime::StructureDescription>> allStructureRepresentations() const;
92 String toJSONString() const;
93 bool isOverflown() const { return m_isOverflown; }
94 String leastCommonAncestor() const;
95 Ref<Inspector::Protocol::Runtime::TypeSet> inspectorTypeSet() const;
96 bool isEmpty() const { return m_seenTypes == TypeNothing; }
97 bool doesTypeConformTo(RuntimeTypeMask test) const;
98 RuntimeTypeMask seenTypes() const { return m_seenTypes; }
99 StructureSet structureSet() const { return m_structureSet; };
100
101private:
102 RuntimeTypeMask m_seenTypes;
103 bool m_isOverflown;
104 Vector<RefPtr<StructureShape>> m_structureHistory;
105 StructureSet m_structureSet;
106};
107
108} //namespace JSC
109
110#endif //TypeSet_h
111