blob: d73f748e3af574e12bffa13d27a3a066a9769fa9 [file] [log] [blame]
oliver@apple.com98fb6bf2013-07-25 03:59:44 +00001/*
fpizlo@apple.com1b84a422014-02-08 05:06:33 +00002 * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
oliver@apple.com98fb6bf2013-07-25 03:59:44 +00003 *
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 *
mjs@apple.com92047332014-03-15 04:08:27 +000013 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mjs@apple.com92047332014-03-15 04:08:27 +000016 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000017 * 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#include "config.h"
27#include "IntendedStructureChain.h"
28
29#include "CodeBlock.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000030#include "JSCInlines.h"
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000031#include "StructureChain.h"
fpizlo@apple.com51614cc2014-02-17 06:35:32 +000032#include <wtf/CommaPrinter.h>
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000033
34namespace JSC {
35
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000036IntendedStructureChain::IntendedStructureChain(JSGlobalObject* globalObject, JSValue prototype)
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000037 : m_globalObject(globalObject)
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000038 , m_prototype(prototype)
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000039{
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000040 ASSERT(m_prototype.isNull() || m_prototype.isObject());
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000041 if (prototype.isNull())
42 return;
43 for (Structure* current = asObject(prototype)->structure(); current; current = current->storedPrototypeStructure())
44 m_vector.append(current);
45}
46
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000047IntendedStructureChain::IntendedStructureChain(JSGlobalObject* globalObject, Structure* head)
48 : m_globalObject(globalObject)
49 , m_prototype(head->prototypeForLookup(m_globalObject))
50{
51 if (m_prototype.isNull())
52 return;
53 for (Structure* current = asObject(m_prototype)->structure(); current; current = current->storedPrototypeStructure())
54 m_vector.append(current);
55}
56
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000057IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, Structure* prototypeStructure)
58 : m_globalObject(codeBlock->globalObject())
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000059 , m_prototype(head->prototypeForLookup(m_globalObject))
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000060{
61 m_vector.append(prototypeStructure);
62}
63
64IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, StructureChain* chain)
65 : m_globalObject(codeBlock->globalObject())
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000066 , m_prototype(head->prototypeForLookup(m_globalObject))
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000067{
68 for (unsigned i = 0; chain->head()[i]; ++i)
69 m_vector.append(chain->head()[i].get());
70}
71
72IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, StructureChain* chain, unsigned count)
73 : m_globalObject(codeBlock->globalObject())
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000074 , m_prototype(head->prototypeForLookup(m_globalObject))
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000075{
76 for (unsigned i = 0; i < count; ++i)
77 m_vector.append(chain->head()[i].get());
78}
79
80IntendedStructureChain::~IntendedStructureChain()
81{
82}
83
84bool IntendedStructureChain::isStillValid() const
85{
fpizlo@apple.comb41e6822014-07-25 20:55:17 +000086 JSValue currentPrototype = m_prototype;
oliver@apple.com98fb6bf2013-07-25 03:59:44 +000087 for (unsigned i = 0; i < m_vector.size(); ++i) {
88 if (asObject(currentPrototype)->structure() != m_vector[i])
89 return false;
90 currentPrototype = m_vector[i]->storedPrototype();
91 }
92 return true;
93}
94
95bool IntendedStructureChain::matches(StructureChain* chain) const
96{
97 for (unsigned i = 0; i < m_vector.size(); ++i) {
98 if (m_vector[i] != chain->head()[i].get())
99 return false;
100 }
101 if (chain->head()[m_vector.size()])
102 return false;
103 return true;
104}
105
utatane.tea@gmail.com8268d392015-05-23 18:41:53 +0000106bool IntendedStructureChain::mayInterceptStoreTo(UniquedStringImpl* uid)
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000107{
108 for (unsigned i = 0; i < m_vector.size(); ++i) {
109 unsigned attributes;
fpizlo@apple.com15ec1b22014-09-21 19:18:40 +0000110 PropertyOffset offset = m_vector[i]->getConcurrently(uid, attributes);
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000111 if (!isValidOffset(offset))
112 continue;
113 if (attributes & (ReadOnly | Accessor))
114 return true;
115 return false;
116 }
117 return false;
118}
119
120bool IntendedStructureChain::isNormalized()
121{
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000122 for (unsigned i = 0; i < m_vector.size(); ++i) {
123 Structure* structure = m_vector[i];
mhahnenberg@apple.com890aa1b2014-04-29 22:21:04 +0000124 if (structure->isProxy())
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000125 return false;
126 if (structure->isDictionary())
127 return false;
128 }
129 return true;
130}
131
fpizlo@apple.com2c4a7e92014-08-06 05:27:46 +0000132bool IntendedStructureChain::takesSlowPathInDFGForImpureProperty()
133{
134 for (size_t i = 0; i < size(); ++i) {
135 if (at(i)->takesSlowPathInDFGForImpureProperty())
136 return true;
137 }
138 return false;
139}
140
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000141JSObject* IntendedStructureChain::terminalPrototype() const
142{
143 ASSERT(!m_vector.isEmpty());
144 if (m_vector.size() == 1)
fpizlo@apple.comb41e6822014-07-25 20:55:17 +0000145 return asObject(m_prototype);
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000146 return asObject(m_vector[m_vector.size() - 2]->storedPrototype());
147}
148
fpizlo@apple.comb41e6822014-07-25 20:55:17 +0000149bool IntendedStructureChain::operator==(const IntendedStructureChain& other) const
150{
151 return m_globalObject == other.m_globalObject
152 && m_prototype == other.m_prototype
153 && m_vector == other.m_vector;
154}
155
156void IntendedStructureChain::gatherChecks(ConstantStructureCheckVector& vector) const
157{
158 JSValue currentPrototype = m_prototype;
159 for (unsigned i = 0; i < size(); ++i) {
160 JSObject* currentObject = asObject(currentPrototype);
161 Structure* currentStructure = at(i);
162 vector.append(ConstantStructureCheck(currentObject, currentStructure));
163 currentPrototype = currentStructure->prototypeForLookup(m_globalObject);
164 }
165}
166
fpizlo@apple.com1b84a422014-02-08 05:06:33 +0000167void IntendedStructureChain::visitChildren(SlotVisitor& visitor)
168{
169 visitor.appendUnbarrieredPointer(&m_globalObject);
fpizlo@apple.comb41e6822014-07-25 20:55:17 +0000170 visitor.appendUnbarrieredValue(&m_prototype);
fpizlo@apple.com1b84a422014-02-08 05:06:33 +0000171 for (unsigned i = m_vector.size(); i--;)
172 visitor.appendUnbarrieredPointer(&m_vector[i]);
173}
174
fpizlo@apple.com51614cc2014-02-17 06:35:32 +0000175void IntendedStructureChain::dump(PrintStream& out) const
176{
177 dumpInContext(out, 0);
178}
179
180void IntendedStructureChain::dumpInContext(PrintStream& out, DumpContext* context) const
181{
182 out.print(
183 "(global = ", RawPointer(m_globalObject), ", head = ",
fpizlo@apple.comb41e6822014-07-25 20:55:17 +0000184 inContext(m_prototype, context), ", vector = [");
fpizlo@apple.com51614cc2014-02-17 06:35:32 +0000185 CommaPrinter comma;
186 for (unsigned i = 0; i < m_vector.size(); ++i)
187 out.print(comma, pointerDumpInContext(m_vector[i], context));
188 out.print("])");
189}
190
oliver@apple.com98fb6bf2013-07-25 03:59:44 +0000191} // namespace JSC
192