]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/SparseArrayValueMap.cpp
JavaScriptCore-7601.1.46.3.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 "JSCInlines.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, 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(CellType, StructureFlags), 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 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
84 // https://bugs.webkit.org/show_bug.cgi?id=142595
85 Heap::heap(array)->deprecatedReportExtraMemory((capacity - m_reportedCapacity) * (sizeof(unsigned) + sizeof(WriteBarrier<Unknown>)));
86 m_reportedCapacity = capacity;
87 }
88 return result;
89 }
90
91 void SparseArrayValueMap::putEntry(ExecState* exec, JSObject* array, unsigned i, JSValue value, bool shouldThrow)
92 {
93 AddResult result = add(array, i);
94 SparseArrayEntry& entry = result.iterator->value;
95
96 // To save a separate find & add, we first always add to the sparse map.
97 // In the uncommon case that this is a new property, and the array is not
98 // extensible, this is not the right thing to have done - so remove again.
99 if (result.isNewEntry && !array->isExtensible()) {
100 remove(result.iterator);
101 if (shouldThrow)
102 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
103 return;
104 }
105
106 entry.put(exec, array, this, value, shouldThrow);
107 }
108
109 bool SparseArrayValueMap::putDirect(ExecState* exec, JSObject* array, unsigned i, JSValue value, unsigned attributes, PutDirectIndexMode mode)
110 {
111 AddResult result = add(array, i);
112 SparseArrayEntry& entry = result.iterator->value;
113
114 // To save a separate find & add, we first always add to the sparse map.
115 // In the uncommon case that this is a new property, and the array is not
116 // extensible, this is not the right thing to have done - so remove again.
117 if (mode != PutDirectIndexLikePutDirect && result.isNewEntry && !array->isExtensible()) {
118 remove(result.iterator);
119 return reject(exec, mode == PutDirectIndexShouldThrow, "Attempting to define property on object that is not extensible.");
120 }
121
122 entry.attributes = attributes;
123 entry.set(exec->vm(), this, value);
124 return true;
125 }
126
127 void SparseArrayEntry::get(JSObject* thisObject, PropertySlot& slot) const
128 {
129 JSValue value = Base::get();
130 ASSERT(value);
131
132 if (LIKELY(!value.isGetterSetter())) {
133 slot.setValue(thisObject, attributes, value);
134 return;
135 }
136
137 slot.setGetterSlot(thisObject, attributes, jsCast<GetterSetter*>(value));
138 }
139
140 void SparseArrayEntry::get(PropertyDescriptor& descriptor) const
141 {
142 descriptor.setDescriptor(Base::get(), attributes);
143 }
144
145 JSValue SparseArrayEntry::get(ExecState* exec, JSObject* array) const
146 {
147 JSValue value = Base::get();
148 ASSERT(value);
149
150 if (LIKELY(!value.isGetterSetter()))
151 return value;
152
153 return callGetter(exec, array, jsCast<GetterSetter*>(value));
154 }
155
156 void SparseArrayEntry::put(ExecState* exec, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow)
157 {
158 if (!(attributes & Accessor)) {
159 if (attributes & ReadOnly) {
160 if (shouldThrow)
161 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
162 return;
163 }
164
165 set(exec->vm(), map, value);
166 return;
167 }
168
169 callSetter(exec, thisValue, Base::get(), value, shouldThrow ? StrictMode : NotStrictMode);
170 }
171
172 JSValue SparseArrayEntry::getNonSparseMode() const
173 {
174 ASSERT(!attributes);
175 return Base::get();
176 }
177
178 void SparseArrayValueMap::visitChildren(JSCell* thisObject, SlotVisitor& visitor)
179 {
180 Base::visitChildren(thisObject, visitor);
181
182 SparseArrayValueMap* thisMap = jsCast<SparseArrayValueMap*>(thisObject);
183 iterator end = thisMap->m_map.end();
184 for (iterator it = thisMap->m_map.begin(); it != end; ++it)
185 visitor.append(&it->value);
186 }
187
188 } // namespace JSC
189