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 "Operations.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
), &s_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(PropertySlot
& slot
) const
127 JSValue value
= Base::get();
130 if (LIKELY(!value
.isGetterSetter())) {
131 slot
.setValue(value
);
135 JSObject
* getter
= asGetterSetter(value
)->getter();
141 slot
.setGetterSlot(getter
);
144 void SparseArrayEntry::get(PropertyDescriptor
& descriptor
) const
146 descriptor
.setDescriptor(Base::get(), attributes
);
149 JSValue
SparseArrayEntry::get(ExecState
* exec
, JSObject
* array
) const
151 JSValue result
= Base::get();
154 if (LIKELY(!result
.isGetterSetter()))
157 JSObject
* getter
= asGetterSetter(result
)->getter();
159 return jsUndefined();
162 CallType callType
= getter
->methodTable()->getCallData(getter
, callData
);
163 return call(exec
, getter
, callType
, callData
, array
->methodTable()->toThisObject(array
, exec
), exec
->emptyList());
166 void SparseArrayEntry::put(ExecState
* exec
, JSValue thisValue
, SparseArrayValueMap
* map
, JSValue value
, bool shouldThrow
)
168 if (!(attributes
& Accessor
)) {
169 if (attributes
& ReadOnly
) {
171 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
175 set(exec
->vm(), map
, value
);
179 JSValue accessor
= Base::get();
180 ASSERT(accessor
.isGetterSetter());
181 JSObject
* setter
= asGetterSetter(accessor
)->setter();
185 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
190 CallType callType
= setter
->methodTable()->getCallData(setter
, callData
);
191 MarkedArgumentBuffer args
;
193 if (thisValue
.isObject())
194 thisValue
= asObject(thisValue
)->methodTable()->toThisObject(asObject(thisValue
), exec
);
195 call(exec
, setter
, callType
, callData
, thisValue
, args
);
198 JSValue
SparseArrayEntry::getNonSparseMode() const
204 void SparseArrayValueMap::visitChildren(JSCell
* thisObject
, SlotVisitor
& visitor
)
206 Base::visitChildren(thisObject
, visitor
);
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
);