2 * Copyright (C) 2011, 2012 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, 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(CompoundType
, 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 Heap::heap(array
)->reportExtraMemoryCost((capacity
- m_reportedCapacity
) * (sizeof(unsigned) + sizeof(WriteBarrier
<Unknown
>)));
84 m_reportedCapacity
= capacity
;
89 void SparseArrayValueMap::putEntry(ExecState
* exec
, JSObject
* array
, unsigned i
, JSValue value
, bool shouldThrow
)
91 AddResult result
= add(array
, i
);
92 SparseArrayEntry
& entry
= result
.iterator
->value
;
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
);
100 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
104 entry
.put(exec
, array
, this, value
, shouldThrow
);
107 bool SparseArrayValueMap::putDirect(ExecState
* exec
, JSObject
* array
, unsigned i
, JSValue value
, unsigned attributes
, PutDirectIndexMode mode
)
109 AddResult result
= add(array
, i
);
110 SparseArrayEntry
& entry
= result
.iterator
->value
;
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.");
120 entry
.attributes
= attributes
;
121 entry
.set(exec
->vm(), this, value
);
125 void SparseArrayEntry::get(JSObject
* thisObject
, PropertySlot
& slot
) const
127 JSValue value
= Base::get();
130 if (LIKELY(!value
.isGetterSetter())) {
131 slot
.setValue(thisObject
, attributes
, value
);
135 slot
.setGetterSlot(thisObject
, attributes
, jsCast
<GetterSetter
*>(value
));
138 void SparseArrayEntry::get(PropertyDescriptor
& descriptor
) const
140 descriptor
.setDescriptor(Base::get(), attributes
);
143 JSValue
SparseArrayEntry::get(ExecState
* exec
, JSObject
* array
) const
145 JSValue value
= Base::get();
148 if (LIKELY(!value
.isGetterSetter()))
151 return callGetter(exec
, array
, jsCast
<GetterSetter
*>(value
));
154 void SparseArrayEntry::put(ExecState
* exec
, JSValue thisValue
, SparseArrayValueMap
* map
, JSValue value
, bool shouldThrow
)
156 if (!(attributes
& Accessor
)) {
157 if (attributes
& ReadOnly
) {
159 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
163 set(exec
->vm(), map
, value
);
167 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
);