]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/SparseArrayValueMap.cpp
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / runtime / SparseArrayValueMap.cpp
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"
32 #include "Operations.h"
33 #include "PropertySlot.h"
34 #include "Reject.h"
35 #include "SlotVisitor.h"
36 #include "Structure.h"
37
38 namespace JSC {
39
40 const ClassInfo SparseArrayValueMap::s_info = { "SparseArrayValueMap", 0, 0, 0, CREATE_METHOD_TABLE(SparseArrayValueMap) };
41
42 SparseArrayValueMap::SparseArrayValueMap(VM& vm)
43 : Base(vm, vm.sparseArrayValueMapStructure.get())
44 , m_flags(Normal)
45 , m_reportedCapacity(0)
46 {
47 }
48
49 SparseArrayValueMap::~SparseArrayValueMap()
50 {
51 }
52
53 void SparseArrayValueMap::finishCreation(VM& vm)
54 {
55 Base::finishCreation(vm);
56 }
57
58 SparseArrayValueMap* 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
65 void SparseArrayValueMap::destroy(JSCell* cell)
66 {
67 static_cast<SparseArrayValueMap*>(cell)->SparseArrayValueMap::~SparseArrayValueMap();
68 }
69
70 Structure* SparseArrayValueMap::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
71 {
72 return Structure::create(vm, globalObject, prototype, TypeInfo(CompoundType, StructureFlags), &s_info);
73 }
74
75 SparseArrayValueMap::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
89 void 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
107 bool 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
125 void SparseArrayEntry::get(PropertySlot& slot) const
126 {
127 JSValue value = Base::get();
128 ASSERT(value);
129
130 if (LIKELY(!value.isGetterSetter())) {
131 slot.setValue(value);
132 return;
133 }
134
135 JSObject* getter = asGetterSetter(value)->getter();
136 if (!getter) {
137 slot.setUndefined();
138 return;
139 }
140
141 slot.setGetterSlot(getter);
142 }
143
144 void SparseArrayEntry::get(PropertyDescriptor& descriptor) const
145 {
146 descriptor.setDescriptor(Base::get(), attributes);
147 }
148
149 JSValue SparseArrayEntry::get(ExecState* exec, JSObject* array) const
150 {
151 JSValue result = Base::get();
152 ASSERT(result);
153
154 if (LIKELY(!result.isGetterSetter()))
155 return result;
156
157 JSObject* getter = asGetterSetter(result)->getter();
158 if (!getter)
159 return jsUndefined();
160
161 CallData callData;
162 CallType callType = getter->methodTable()->getCallData(getter, callData);
163 return call(exec, getter, callType, callData, array->methodTable()->toThisObject(array, exec), exec->emptyList());
164 }
165
166 void SparseArrayEntry::put(ExecState* exec, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow)
167 {
168 if (!(attributes & Accessor)) {
169 if (attributes & ReadOnly) {
170 if (shouldThrow)
171 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
172 return;
173 }
174
175 set(exec->vm(), map, value);
176 return;
177 }
178
179 JSValue accessor = Base::get();
180 ASSERT(accessor.isGetterSetter());
181 JSObject* setter = asGetterSetter(accessor)->setter();
182
183 if (!setter) {
184 if (shouldThrow)
185 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
186 return;
187 }
188
189 CallData callData;
190 CallType callType = setter->methodTable()->getCallData(setter, callData);
191 MarkedArgumentBuffer args;
192 args.append(value);
193 if (thisValue.isObject())
194 thisValue = asObject(thisValue)->methodTable()->toThisObject(asObject(thisValue), exec);
195 call(exec, setter, callType, callData, thisValue, args);
196 }
197
198 JSValue SparseArrayEntry::getNonSparseMode() const
199 {
200 ASSERT(!attributes);
201 return Base::get();
202 }
203
204 void SparseArrayValueMap::visitChildren(JSCell* thisObject, SlotVisitor& visitor)
205 {
206 Base::visitChildren(thisObject, visitor);
207
208 SparseArrayValueMap* thisMap = jsCast<SparseArrayValueMap*>(thisObject);
209 iterator end = thisMap->m_map.end();
210 for (iterator it = thisMap->m_map.begin(); it != end; ++it)
211 visitor.append(&it->value);
212 }
213
214 } // namespace JSC
215