1/*
2 * Copyright (C) 2014-2019 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#pragma once
27
28#include "JSCast.h"
29#include "Operations.h"
30#include "PropertyNameArray.h"
31#include "Structure.h"
32
33namespace JSC {
34
35class JSPropertyNameEnumerator final : public JSCell {
36public:
37 using Base = JSCell;
38 static constexpr unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
39
40 static JSPropertyNameEnumerator* create(VM&, Structure*, uint32_t, uint32_t, PropertyNameArray&&);
41
42 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
43 {
44 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
45 }
46
47 DECLARE_EXPORT_INFO;
48
49 JSString* propertyNameAtIndex(uint32_t index)
50 {
51 if (index >= sizeOfPropertyNames())
52 return nullptr;
53 return m_propertyNames.get()[index].get();
54 }
55
56 StructureChain* cachedPrototypeChain() const { return m_prototypeChain.get(); }
57 void setCachedPrototypeChain(VM& vm, StructureChain* prototypeChain) { return m_prototypeChain.set(vm, this, prototypeChain); }
58
59 Structure* cachedStructure(VM& vm) const
60 {
61 if (!m_cachedStructureID)
62 return nullptr;
63 return vm.heap.structureIDTable().get(m_cachedStructureID);
64 }
65 StructureID cachedStructureID() const { return m_cachedStructureID; }
66 uint32_t indexedLength() const { return m_indexedLength; }
67 uint32_t endStructurePropertyIndex() const { return m_endStructurePropertyIndex; }
68 uint32_t endGenericPropertyIndex() const { return m_endGenericPropertyIndex; }
69 uint32_t cachedInlineCapacity() const { return m_cachedInlineCapacity; }
70 uint32_t sizeOfPropertyNames() const { return endGenericPropertyIndex(); }
71 static ptrdiff_t cachedStructureIDOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_cachedStructureID); }
72 static ptrdiff_t indexedLengthOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_indexedLength); }
73 static ptrdiff_t endStructurePropertyIndexOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_endStructurePropertyIndex); }
74 static ptrdiff_t endGenericPropertyIndexOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_endGenericPropertyIndex); }
75 static ptrdiff_t cachedInlineCapacityOffset() { return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_cachedInlineCapacity); }
76 static ptrdiff_t cachedPropertyNamesVectorOffset()
77 {
78 return OBJECT_OFFSETOF(JSPropertyNameEnumerator, m_propertyNames);
79 }
80
81 static void visitChildren(JSCell*, SlotVisitor&);
82
83private:
84 JSPropertyNameEnumerator(VM&, Structure*, uint32_t, uint32_t, WriteBarrier<JSString>*, unsigned);
85 void finishCreation(VM&, RefPtr<PropertyNameArrayData>&&);
86
87 AuxiliaryBarrier<WriteBarrier<JSString>*> m_propertyNames;
88 WriteBarrier<StructureChain> m_prototypeChain;
89 StructureID m_cachedStructureID;
90 uint32_t m_indexedLength;
91 uint32_t m_endStructurePropertyIndex;
92 uint32_t m_endGenericPropertyIndex;
93 uint32_t m_cachedInlineCapacity;
94};
95
96inline JSPropertyNameEnumerator* propertyNameEnumerator(JSGlobalObject* globalObject, JSObject* base)
97{
98 VM& vm = getVM(globalObject);
99 auto scope = DECLARE_THROW_SCOPE(vm);
100
101 uint32_t indexedLength = base->methodTable(vm)->getEnumerableLength(globalObject, base);
102
103 JSPropertyNameEnumerator* enumerator = nullptr;
104
105 Structure* structure = base->structure(vm);
106 if (!indexedLength
107 && (enumerator = structure->cachedPropertyNameEnumerator())
108 && enumerator->cachedPrototypeChain() == structure->prototypeChain(globalObject, base))
109 return enumerator;
110
111 uint32_t numberStructureProperties = 0;
112
113 PropertyNameArray propertyNames(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
114
115 if (structure->canAccessPropertiesQuicklyForEnumeration() && indexedLength == base->getArrayLength()) {
116 base->methodTable(vm)->getStructurePropertyNames(base, globalObject, propertyNames, EnumerationMode());
117 scope.assertNoException();
118
119 numberStructureProperties = propertyNames.size();
120
121 base->methodTable(vm)->getGenericPropertyNames(base, globalObject, propertyNames, EnumerationMode());
122 } else {
123 // Generic property names vector contains all indexed property names.
124 // So disable indexed property enumeration phase by setting |indexedLength| to 0.
125 indexedLength = 0;
126 base->methodTable(vm)->getPropertyNames(base, globalObject, propertyNames, EnumerationMode());
127 }
128 RETURN_IF_EXCEPTION(scope, nullptr);
129
130 ASSERT(propertyNames.size() < UINT32_MAX);
131
132 bool sawPolyProto;
133 bool successfullyNormalizedChain = normalizePrototypeChain(globalObject, base, sawPolyProto) != InvalidPrototypeChain;
134
135 Structure* structureAfterGettingPropertyNames = base->structure(vm);
136 enumerator = JSPropertyNameEnumerator::create(vm, structureAfterGettingPropertyNames, indexedLength, numberStructureProperties, WTFMove(propertyNames));
137 if (!indexedLength && successfullyNormalizedChain && structureAfterGettingPropertyNames == structure) {
138 enumerator->setCachedPrototypeChain(vm, structure->prototypeChain(globalObject, base));
139 if (structure->canCachePropertyNameEnumerator())
140 structure->setCachedPropertyNameEnumerator(vm, enumerator);
141 }
142 return enumerator;
143}
144
145} // namespace JSC
146