]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/SparseArrayValueMap.cpp
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / SparseArrayValueMap.cpp
CommitLineData
93a37866
A
1/*
2 * Copyright (C) 2011, 2012 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#include "config.h"
27#include "SparseArrayValueMap.h"
28
29#include "ClassInfo.h"
30#include "GetterSetter.h"
31#include "JSObject.h"
81345200 32#include "JSCInlines.h"
93a37866
A
33#include "PropertySlot.h"
34#include "Reject.h"
35#include "SlotVisitor.h"
36#include "Structure.h"
37
38namespace JSC {
39
40const ClassInfo SparseArrayValueMap::s_info = { "SparseArrayValueMap", 0, 0, 0, CREATE_METHOD_TABLE(SparseArrayValueMap) };
41
42SparseArrayValueMap::SparseArrayValueMap(VM& vm)
43 : Base(vm, vm.sparseArrayValueMapStructure.get())
44 , m_flags(Normal)
45 , m_reportedCapacity(0)
46{
47}
48
49SparseArrayValueMap::~SparseArrayValueMap()
50{
51}
52
53void SparseArrayValueMap::finishCreation(VM& vm)
54{
55 Base::finishCreation(vm);
56}
57
58SparseArrayValueMap* SparseArrayValueMap::create(VM& vm)
59{
60 SparseArrayValueMap* result = new (NotNull, allocateCell<SparseArrayValueMap>(vm.heap)) SparseArrayValueMap(vm);
61 result->finishCreation(vm);
62 return result;
63}
64
65void SparseArrayValueMap::destroy(JSCell* cell)
66{
67 static_cast<SparseArrayValueMap*>(cell)->SparseArrayValueMap::~SparseArrayValueMap();
68}
69
70Structure* SparseArrayValueMap::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
71{
81345200 72 return Structure::create(vm, globalObject, prototype, TypeInfo(CompoundType, StructureFlags), info());
93a37866
A
73}
74
75SparseArrayValueMap::AddResult SparseArrayValueMap::add(JSObject* array, unsigned i)
76{
77 SparseArrayEntry entry;
78 entry.setWithoutWriteBarrier(jsUndefined());
79
80 AddResult result = m_map.add(i, entry);
81 size_t capacity = m_map.capacity();
82 if (capacity != m_reportedCapacity) {
83 Heap::heap(array)->reportExtraMemoryCost((capacity - m_reportedCapacity) * (sizeof(unsigned) + sizeof(WriteBarrier<Unknown>)));
84 m_reportedCapacity = capacity;
85 }
86 return result;
87}
88
89void SparseArrayValueMap::putEntry(ExecState* exec, JSObject* array, unsigned i, JSValue value, bool shouldThrow)
90{
91 AddResult result = add(array, i);
92 SparseArrayEntry& entry = result.iterator->value;
93
94 // To save a separate find & add, we first always add to the sparse map.
95 // In the uncommon case that this is a new property, and the array is not
96 // extensible, this is not the right thing to have done - so remove again.
97 if (result.isNewEntry && !array->isExtensible()) {
98 remove(result.iterator);
99 if (shouldThrow)
100 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
101 return;
102 }
103
104 entry.put(exec, array, this, value, shouldThrow);
105}
106
107bool SparseArrayValueMap::putDirect(ExecState* exec, JSObject* array, unsigned i, JSValue value, unsigned attributes, PutDirectIndexMode mode)
108{
109 AddResult result = add(array, i);
110 SparseArrayEntry& entry = result.iterator->value;
111
112 // To save a separate find & add, we first always add to the sparse map.
113 // In the uncommon case that this is a new property, and the array is not
114 // extensible, this is not the right thing to have done - so remove again.
115 if (mode != PutDirectIndexLikePutDirect && result.isNewEntry && !array->isExtensible()) {
116 remove(result.iterator);
117 return reject(exec, mode == PutDirectIndexShouldThrow, "Attempting to define property on object that is not extensible.");
118 }
119
120 entry.attributes = attributes;
121 entry.set(exec->vm(), this, value);
122 return true;
123}
124
81345200 125void SparseArrayEntry::get(JSObject* thisObject, PropertySlot& slot) const
93a37866
A
126{
127 JSValue value = Base::get();
128 ASSERT(value);
129
130 if (LIKELY(!value.isGetterSetter())) {
81345200 131 slot.setValue(thisObject, attributes, value);
93a37866
A
132 return;
133 }
134
81345200 135 slot.setGetterSlot(thisObject, attributes, jsCast<GetterSetter*>(value));
93a37866
A
136}
137
138void SparseArrayEntry::get(PropertyDescriptor& descriptor) const
139{
140 descriptor.setDescriptor(Base::get(), attributes);
141}
142
143JSValue SparseArrayEntry::get(ExecState* exec, JSObject* array) const
144{
81345200
A
145 JSValue value = Base::get();
146 ASSERT(value);
93a37866 147
81345200
A
148 if (LIKELY(!value.isGetterSetter()))
149 return value;
93a37866 150
81345200 151 return callGetter(exec, array, jsCast<GetterSetter*>(value));
93a37866
A
152}
153
154void SparseArrayEntry::put(ExecState* exec, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow)
155{
156 if (!(attributes & Accessor)) {
157 if (attributes & ReadOnly) {
158 if (shouldThrow)
159 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
160 return;
161 }
162
163 set(exec->vm(), map, value);
164 return;
165 }
166
81345200 167 callSetter(exec, thisValue, Base::get(), value, shouldThrow ? StrictMode : NotStrictMode);
93a37866
A
168}
169
170JSValue SparseArrayEntry::getNonSparseMode() const
171{
172 ASSERT(!attributes);
173 return Base::get();
174}
175
176void SparseArrayValueMap::visitChildren(JSCell* thisObject, SlotVisitor& visitor)
177{
178 Base::visitChildren(thisObject, visitor);
179
180 SparseArrayValueMap* thisMap = jsCast<SparseArrayValueMap*>(thisObject);
181 iterator end = thisMap->m_map.end();
182 for (iterator it = thisMap->m_map.begin(); it != end; ++it)
183 visitor.append(&it->value);
184}
185
186} // namespace JSC
187