]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSPropertyNameEnumerator.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSPropertyNameEnumerator.h
1 /*
2 * Copyright (C) 2014 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef JSPropertyNameEnumerator_h
27 #define JSPropertyNameEnumerator_h
28
29 #include "JSCell.h"
30 #include "Operations.h"
31 #include "PropertyNameArray.h"
32 #include "Structure.h"
33
34 namespace JSC {
35
36 class Identifier;
37
38 class JSPropertyNameEnumerator final : public JSCell {
39 public:
40 typedef JSCell Base;
41 static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
42
43 static JSPropertyNameEnumerator* create(VM&);
44 static JSPropertyNameEnumerator* create(VM&, Structure*, uint32_t, uint32_t, PropertyNameArray&);
45
46 static const bool needsDestruction = true;
47 static void destroy(JSCell*);
48
49 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
50 {
51 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
52 }
53
54 DECLARE_EXPORT_INFO;
55
56 JSString* propertyNameAtIndex(uint32_t index) const
57 {
58 if (index >= m_propertyNames.size())
59 return nullptr;
60 return m_propertyNames[index].get();
61 }
62
63 StructureChain* cachedPrototypeChain() const { return m_prototypeChain.get(); }
64 void setCachedPrototypeChain(VM& vm, StructureChain* prototypeChain) { return m_prototypeChain.set(vm, this, prototypeChain); }
65
66 Structure* cachedStructure(VM& vm) const
67 {
68 if (!m_cachedStructureID)
69 return nullptr;
70 return vm.heap.structureIDTable().get(m_cachedStructureID);
71 }
72 StructureID cachedStructureID() const { return m_cachedStructureID; }
73 uint32_t indexedLength() const { return m_indexedLength; }
74 uint32_t endStructurePropertyIndex() const { return m_endStructurePropertyIndex; }
75 uint32_t endGenericPropertyIndex() const { return m_endGenericPropertyIndex; }
76 uint32_t cachedInlineCapacity() const { return m_cachedInlineCapacity; }
77 static ptrdiff_t cachedStructureIDOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_cachedStructureID); }
78 static ptrdiff_t indexedLengthOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_indexedLength); }
79 static ptrdiff_t endStructurePropertyIndexOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_endStructurePropertyIndex); }
80 static ptrdiff_t endGenericPropertyIndexOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_endGenericPropertyIndex); }
81 static ptrdiff_t cachedInlineCapacityOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_cachedInlineCapacity); }
82 static ptrdiff_t cachedPropertyNamesVectorOffset()
83 {
84 return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_propertyNames) + Vector<WriteBarrier<JSString>>::dataMemoryOffset();
85 }
86
87 static void visitChildren(JSCell*, SlotVisitor&);
88
89 private:
90 JSPropertyNameEnumerator(VM&, StructureID, uint32_t);
91 void finishCreation(VM&, uint32_t, uint32_t, PassRefPtr<PropertyNameArrayData>);
92
93 Vector<WriteBarrier<JSString>> m_propertyNames;
94 StructureID m_cachedStructureID;
95 WriteBarrier<StructureChain> m_prototypeChain;
96 uint32_t m_indexedLength;
97 uint32_t m_endStructurePropertyIndex;
98 uint32_t m_endGenericPropertyIndex;
99 uint32_t m_cachedInlineCapacity;
100 };
101
102 inline JSPropertyNameEnumerator* propertyNameEnumerator(ExecState* exec, JSObject* base)
103 {
104 VM& vm = exec->vm();
105
106 uint32_t indexedLength = base->methodTable(vm)->getEnumerableLength(exec, base);
107
108 JSPropertyNameEnumerator* enumerator = nullptr;
109
110 Structure* structure = base->structure(vm);
111 if (!indexedLength
112 && (enumerator = structure->cachedPropertyNameEnumerator())
113 && enumerator->cachedPrototypeChain() == structure->prototypeChain(exec))
114 return enumerator;
115
116 uint32_t numberStructureProperties = 0;
117
118 PropertyNameArray propertyNames(exec);
119
120 if (structure->canAccessPropertiesQuickly() && indexedLength == base->getArrayLength()) {
121 base->methodTable(vm)->getStructurePropertyNames(base, exec, propertyNames, EnumerationMode());
122
123 numberStructureProperties = propertyNames.size();
124
125 base->methodTable(vm)->getGenericPropertyNames(base, exec, propertyNames, EnumerationMode());
126 } else
127 base->methodTable(vm)->getPropertyNames(base, exec, propertyNames, EnumerationMode());
128
129 ASSERT(propertyNames.size() < UINT32_MAX);
130
131 normalizePrototypeChain(exec, structure);
132
133 enumerator = JSPropertyNameEnumerator::create(vm, structure, indexedLength, numberStructureProperties, propertyNames);
134 enumerator->setCachedPrototypeChain(vm, structure->prototypeChain(exec));
135 if (!indexedLength && structure->canCachePropertyNameEnumerator())
136 structure->setCachedPropertyNameEnumerator(vm, enumerator);
137 return enumerator;
138 }
139
140 } // namespace JSC
141
142 #endif // JSPropertyNameEnumerator_h