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