1/*
2 * Copyright (C) 2013 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 COMPUTER, 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 COMPUTER, 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#include "config.h"
27#include "IntendedStructureChain.h"
28
29#include "CodeBlock.h"
30#include "Operations.h"
31#include "StructureChain.h"
32
33namespace JSC {
34
35IntendedStructureChain::IntendedStructureChain(JSGlobalObject* globalObject, Structure* head)
36 : m_globalObject(globalObject)
37 , m_head(head)
38{
39 for (Structure* current = head; current; current = current->storedPrototypeStructure())
40 m_vector.append(current);
41}
42
43IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, Structure* prototypeStructure)
44 : m_globalObject(codeBlock->globalObject())
45 , m_head(head)
46{
47 m_vector.append(prototypeStructure);
48}
49
50IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, StructureChain* chain)
51 : m_globalObject(codeBlock->globalObject())
52 , m_head(head)
53{
54 for (unsigned i = 0; chain->head()[i]; ++i)
55 m_vector.append(chain->head()[i].get());
56}
57
58IntendedStructureChain::IntendedStructureChain(CodeBlock* codeBlock, Structure* head, StructureChain* chain, unsigned count)
59 : m_globalObject(codeBlock->globalObject())
60 , m_head(head)
61{
62 for (unsigned i = 0; i < count; ++i)
63 m_vector.append(chain->head()[i].get());
64}
65
66IntendedStructureChain::~IntendedStructureChain()
67{
68}
69
70bool IntendedStructureChain::isStillValid() const
71{
72 JSValue currentPrototype = m_head->prototypeForLookup(m_globalObject);
73 for (unsigned i = 0; i < m_vector.size(); ++i) {
74 if (asObject(currentPrototype)->structure() != m_vector[i])
75 return false;
76 currentPrototype = m_vector[i]->storedPrototype();
77 }
78 return true;
79}
80
81bool IntendedStructureChain::matches(StructureChain* chain) const
82{
83 for (unsigned i = 0; i < m_vector.size(); ++i) {
84 if (m_vector[i] != chain->head()[i].get())
85 return false;
86 }
87 if (chain->head()[m_vector.size()])
88 return false;
89 return true;
90}
91
92StructureChain* IntendedStructureChain::chain(VM& vm) const
93{
94 ASSERT(isStillValid());
95 StructureChain* result = StructureChain::create(vm, m_head);
96 ASSERT(matches(result));
97 return result;
98}
99
100bool IntendedStructureChain::mayInterceptStoreTo(VM& vm, StringImpl* uid)
101{
102 for (unsigned i = 0; i < m_vector.size(); ++i) {
103 unsigned attributes;
104 JSCell* specificValue;
105 PropertyOffset offset = m_vector[i]->getConcurrently(vm, uid, attributes, specificValue);
106 if (!isValidOffset(offset))
107 continue;
108 if (attributes & (ReadOnly | Accessor))
109 return true;
110 return false;
111 }
112 return false;
113}
114
115bool IntendedStructureChain::isNormalized()
116{
117 if (m_head->typeInfo().type() == ProxyType)
118 return false;
119 for (unsigned i = 0; i < m_vector.size(); ++i) {
120 Structure* structure = m_vector[i];
121 if (structure->typeInfo().type() == ProxyType)
122 return false;
123 if (structure->isDictionary())
124 return false;
125 }
126 return true;
127}
128
129JSObject* IntendedStructureChain::terminalPrototype() const
130{
131 ASSERT(!m_vector.isEmpty());
132 if (m_vector.size() == 1)
133 return asObject(m_head->prototypeForLookup(m_globalObject));
134 return asObject(m_vector[m_vector.size() - 2]->storedPrototype());
135}
136
137} // namespace JSC
138