]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/PropertyTable.cpp
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / runtime / PropertyTable.cpp
1 /*
2 * Copyright (C) 2013 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 "PropertyMapHashTable.h"
28
29 #include "JSCJSValueInlines.h"
30 #include "JSCellInlines.h"
31 #include "SlotVisitorInlines.h"
32 #include "StructureInlines.h"
33
34 #include <wtf/CryptographicallyRandomNumber.h>
35
36 namespace JSC {
37
38 const ClassInfo PropertyTable::s_info = { "PropertyTable", 0, 0, 0, CREATE_METHOD_TABLE(PropertyTable) };
39
40 PropertyTable* PropertyTable::create(VM& vm, unsigned initialCapacity)
41 {
42 PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, initialCapacity);
43 table->finishCreation(vm);
44 return table;
45 }
46
47 PropertyTable* PropertyTable::clone(VM& vm, const PropertyTable& other)
48 {
49 PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, other);
50 table->finishCreation(vm);
51 return table;
52 }
53
54 PropertyTable* PropertyTable::clone(VM& vm, unsigned initialCapacity, const PropertyTable& other)
55 {
56 PropertyTable* table = new (NotNull, allocateCell<PropertyTable>(vm.heap)) PropertyTable(vm, initialCapacity, other);
57 table->finishCreation(vm);
58 return table;
59 }
60
61 PropertyTable::PropertyTable(VM& vm, unsigned initialCapacity)
62 : JSCell(vm, vm.propertyTableStructure.get())
63 , m_indexSize(sizeForCapacity(initialCapacity))
64 , m_indexMask(m_indexSize - 1)
65 , m_index(static_cast<unsigned*>(fastZeroedMalloc(dataSize())))
66 , m_keyCount(0)
67 , m_deletedCount(0)
68 {
69 ASSERT(isPowerOf2(m_indexSize));
70 }
71
72 PropertyTable::PropertyTable(VM& vm, const PropertyTable& other)
73 : JSCell(vm, vm.propertyTableStructure.get())
74 , m_indexSize(other.m_indexSize)
75 , m_indexMask(other.m_indexMask)
76 , m_index(static_cast<unsigned*>(fastMalloc(dataSize())))
77 , m_keyCount(other.m_keyCount)
78 , m_deletedCount(other.m_deletedCount)
79 {
80 ASSERT(isPowerOf2(m_indexSize));
81
82 memcpy(m_index, other.m_index, dataSize());
83
84 iterator end = this->end();
85 for (iterator iter = begin(); iter != end; ++iter) {
86 iter->key->ref();
87 vm.heap.writeBarrier(this, iter->specificValue.get());
88 }
89
90 // Copy the m_deletedOffsets vector.
91 Vector<PropertyOffset>* otherDeletedOffsets = other.m_deletedOffsets.get();
92 if (otherDeletedOffsets)
93 m_deletedOffsets = adoptPtr(new Vector<PropertyOffset>(*otherDeletedOffsets));
94 }
95
96 PropertyTable::PropertyTable(VM& vm, unsigned initialCapacity, const PropertyTable& other)
97 : JSCell(vm, vm.propertyTableStructure.get())
98 , m_indexSize(sizeForCapacity(initialCapacity))
99 , m_indexMask(m_indexSize - 1)
100 , m_index(static_cast<unsigned*>(fastZeroedMalloc(dataSize())))
101 , m_keyCount(0)
102 , m_deletedCount(0)
103 {
104 ASSERT(isPowerOf2(m_indexSize));
105 ASSERT(initialCapacity >= other.m_keyCount);
106
107 const_iterator end = other.end();
108 for (const_iterator iter = other.begin(); iter != end; ++iter) {
109 ASSERT(canInsert());
110 reinsert(*iter);
111 iter->key->ref();
112 vm.heap.writeBarrier(this, iter->specificValue.get());
113 }
114
115 // Copy the m_deletedOffsets vector.
116 Vector<PropertyOffset>* otherDeletedOffsets = other.m_deletedOffsets.get();
117 if (otherDeletedOffsets)
118 m_deletedOffsets = adoptPtr(new Vector<PropertyOffset>(*otherDeletedOffsets));
119 }
120
121 void PropertyTable::destroy(JSCell* cell)
122 {
123 static_cast<PropertyTable*>(cell)->PropertyTable::~PropertyTable();
124 }
125
126 PropertyTable::~PropertyTable()
127 {
128 iterator end = this->end();
129 for (iterator iter = begin(); iter != end; ++iter)
130 iter->key->deref();
131
132 fastFree(m_index);
133 }
134
135 void PropertyTable::visitChildren(JSCell* cell, SlotVisitor& visitor)
136 {
137 PropertyTable* thisObject = jsCast<PropertyTable*>(cell);
138 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
139 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
140
141 JSCell::visitChildren(thisObject, visitor);
142
143 PropertyTable::iterator end = thisObject->end();
144 for (PropertyTable::iterator ptr = thisObject->begin(); ptr != end; ++ptr)
145 visitor.append(&ptr->specificValue);
146 }
147
148 }