2 * Copyright (C) 2011-2012, 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "SparseArrayValueMap.h"
29 #include "ClassInfo.h"
30 #include "GetterSetter.h"
32 #include "JSCInlines.h"
33 #include "PropertySlot.h"
34 #include "SlotVisitor.h"
35 #include "Structure.h"
36 #include "TypeError.h"
40 const ClassInfo SparseArrayValueMap::s_info = { "SparseArrayValueMap", 0, 0, CREATE_METHOD_TABLE(SparseArrayValueMap) };
42 SparseArrayValueMap::SparseArrayValueMap(VM& vm)
43 : Base(vm, vm.sparseArrayValueMapStructure.get())
45 , m_reportedCapacity(0)
49 SparseArrayValueMap::~SparseArrayValueMap()
53 void SparseArrayValueMap::finishCreation(VM& vm)
55 Base::finishCreation(vm);
58 SparseArrayValueMap* SparseArrayValueMap::create(VM& vm)
60 SparseArrayValueMap* result = new (NotNull, allocateCell<SparseArrayValueMap>(vm.heap)) SparseArrayValueMap(vm);
61 result->finishCreation(vm);
65 void SparseArrayValueMap::destroy(JSCell* cell)
67 static_cast<SparseArrayValueMap*>(cell)->SparseArrayValueMap::~SparseArrayValueMap();
70 Structure* SparseArrayValueMap::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
72 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
75 SparseArrayValueMap::AddResult SparseArrayValueMap::add(JSObject* array, unsigned i)
77 SparseArrayEntry entry;
78 entry.setWithoutWriteBarrier(jsUndefined());
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;
91 bool SparseArrayValueMap::putEntry(ExecState* exec, JSObject* array, unsigned i, JSValue value, bool shouldThrow)
94 auto scope = DECLARE_THROW_SCOPE(vm);
97 AddResult result = add(array, i);
98 SparseArrayEntry& entry = result.iterator->value;
100 // To save a separate find & add, we first always add to the sparse map.
101 // In the uncommon case that this is a new property, and the array is not
102 // extensible, this is not the right thing to have done - so remove again.
103 if (result.isNewEntry && !array->isStructureExtensible()) {
104 remove(result.iterator);
105 return typeError(exec, scope, shouldThrow, ASCIILiteral(ReadonlyPropertyWriteError));
108 return entry.put(exec, array, this, value, shouldThrow);
111 bool SparseArrayValueMap::putDirect(ExecState* exec, JSObject* array, unsigned i, JSValue value, unsigned attributes, PutDirectIndexMode mode)
114 auto scope = DECLARE_THROW_SCOPE(vm);
117 bool shouldThrow = (mode == PutDirectIndexShouldThrow);
119 AddResult result = add(array, i);
120 SparseArrayEntry& entry = result.iterator->value;
122 if (mode != PutDirectIndexLikePutDirect && !array->isStructureExtensible()) {
123 // To save a separate find & add, we first always add to the sparse map.
124 // In the uncommon case that this is a new property, and the array is not
125 // extensible, this is not the right thing to have done - so remove again.
126 if (result.isNewEntry) {
127 remove(result.iterator);
128 return typeError(exec, scope, shouldThrow, ASCIILiteral(NonExtensibleObjectPropertyDefineError));
130 if (entry.attributes & ReadOnly)
131 return typeError(exec, scope, shouldThrow, ASCIILiteral(ReadonlyPropertyWriteError));
133 entry.attributes = attributes;
134 entry.set(vm, this, value);
138 void SparseArrayEntry::get(JSObject* thisObject, PropertySlot& slot) const
140 JSValue value = Base::get();
143 if (LIKELY(!value.isGetterSetter())) {
144 slot.setValue(thisObject, attributes, value);
148 slot.setGetterSlot(thisObject, attributes, jsCast<GetterSetter*>(value));
151 void SparseArrayEntry::get(PropertyDescriptor& descriptor) const
153 descriptor.setDescriptor(Base::get(), attributes);
156 bool SparseArrayEntry::put(ExecState* exec, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow)
159 auto scope = DECLARE_THROW_SCOPE(vm);
161 if (!(attributes & Accessor)) {
162 if (attributes & ReadOnly)
163 return typeError(exec, scope, shouldThrow, ASCIILiteral(ReadonlyPropertyWriteError));
169 return callSetter(exec, thisValue, Base::get(), value, shouldThrow ? StrictMode : NotStrictMode);
172 JSValue SparseArrayEntry::getNonSparseMode() const
178 void SparseArrayValueMap::visitChildren(JSCell* thisObject, SlotVisitor& visitor)
180 Base::visitChildren(thisObject, visitor);
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);