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"
35 #include "SlotVisitor.h"
36 #include "Structure.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);
106 throwTypeError(exec, scope, ReadonlyPropertyWriteError);
110 return entry.put(exec, array, this, value, shouldThrow);
113 bool SparseArrayValueMap::putDirect(ExecState* exec, JSObject* array, unsigned i, JSValue value, unsigned attributes, PutDirectIndexMode mode)
117 AddResult result = add(array, i);
118 SparseArrayEntry& entry = result.iterator->value;
120 // To save a separate find & add, we first always add to the sparse map.
121 // In the uncommon case that this is a new property, and the array is not
122 // extensible, this is not the right thing to have done - so remove again.
123 if (mode != PutDirectIndexLikePutDirect && result.isNewEntry && !array->isStructureExtensible()) {
124 remove(result.iterator);
125 return reject(exec, mode == PutDirectIndexShouldThrow, "Attempting to define property on object that is not extensible.");
128 entry.attributes = attributes;
129 entry.set(exec->vm(), this, value);
133 void SparseArrayEntry::get(JSObject* thisObject, PropertySlot& slot) const
135 JSValue value = Base::get();
138 if (LIKELY(!value.isGetterSetter())) {
139 slot.setValue(thisObject, attributes, value);
143 slot.setGetterSlot(thisObject, attributes, jsCast<GetterSetter*>(value));
146 void SparseArrayEntry::get(PropertyDescriptor& descriptor) const
148 descriptor.setDescriptor(Base::get(), attributes);
151 bool SparseArrayEntry::put(ExecState* exec, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow)
154 auto scope = DECLARE_THROW_SCOPE(vm);
156 if (!(attributes & Accessor)) {
157 if (attributes & ReadOnly) {
159 throwTypeError(exec, scope, ReadonlyPropertyWriteError);
167 return callSetter(exec, thisValue, Base::get(), value, shouldThrow ? StrictMode : NotStrictMode);
170 JSValue SparseArrayEntry::getNonSparseMode() const
176 void SparseArrayValueMap::visitChildren(JSCell* thisObject, SlotVisitor& visitor)
178 Base::visitChildren(thisObject, visitor);
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);