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#pragma once
27
28#include "ConcurrentJSLock.h"
29#include "RuntimeType.h"
30#include "StructureSet.h"
31#include <wtf/HashSet.h>
32#include <wtf/JSONValues.h>
33#include <wtf/RefCounted.h>
34#include <wtf/ThreadSafeRefCounted.h>
35#include <wtf/text/WTFString.h>
36#include <wtf/Vector.h>
37
38namespace Inspector {
39namespace Protocol {
40
41namespace Runtime {
42class StructureDescription;
43class TypeSet;
44}
45
46}
47}
48
49namespace JSC {
50
51class StructureShape : public RefCounted<StructureShape> {
52 friend class TypeSet;
53
54public:
55 StructureShape();
56
57 static Ref<StructureShape> create() { return adoptRef(*new StructureShape); }
58 String propertyHash();
59 void markAsFinal();
60 void addProperty(UniquedStringImpl&);
61 String stringRepresentation();
62 String toJSONString() const;
63 Ref<Inspector::Protocol::Runtime::StructureDescription> inspectorRepresentation();
64 void setConstructorName(String name) { m_constructorName = (name.isEmpty() ? "Object"_s : name); }
65 String constructorName() { return m_constructorName; }
66 void setProto(Ref<StructureShape>&& shape) { m_proto = WTFMove(shape); }
67 void enterDictionaryMode();
68
69private:
70 static String leastCommonAncestor(const Vector<Ref<StructureShape>>&);
71 static Ref<StructureShape> merge(Ref<StructureShape>&&, Ref<StructureShape>&&);
72 bool hasSamePrototypeChain(const StructureShape&);
73
74 bool m_final;
75 bool m_isInDictionaryMode;
76 HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> m_fields;
77 HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> m_optionalFields;
78 RefPtr<StructureShape> m_proto;
79 std::unique_ptr<String> m_propertyHash;
80 String m_constructorName;
81};
82
83class TypeSet : public ThreadSafeRefCounted<TypeSet> {
84
85public:
86 static Ref<TypeSet> create() { return adoptRef(*new TypeSet); }
87 TypeSet();
88 void addTypeInformation(RuntimeType, RefPtr<StructureShape>&&, Structure*, bool sawPolyProtoStructure);
89 void invalidateCache(VM&);
90 String dumpTypes() const;
91 String displayName() const;
92 Ref<JSON::ArrayOf<Inspector::Protocol::Runtime::StructureDescription>> allStructureRepresentations() const;
93 String toJSONString() const;
94 bool isOverflown() const { return m_isOverflown; }
95 String leastCommonAncestor() const;
96 Ref<Inspector::Protocol::Runtime::TypeSet> inspectorTypeSet() const;
97 bool isEmpty() const { return m_seenTypes == TypeNothing; }
98 bool doesTypeConformTo(RuntimeTypeMask test) const;
99 RuntimeTypeMask seenTypes() const { return m_seenTypes; }
100 StructureSet structureSet(const ConcurrentJSLocker&) const { return m_structureSet; }
101
102 ConcurrentJSLock m_lock;
103private:
104 bool m_isOverflown;
105 RuntimeTypeMask m_seenTypes;
106 Vector<Ref<StructureShape>> m_structureHistory;
107 StructureSet m_structureSet;
108};
109
110} // namespace JSC
111