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#include "config.h"
27#include "JSPropertyNameEnumerator.h"
28
29#include "JSCInlines.h"
30#include "LockDuringMarking.h"
31#include "StrongInlines.h"
32
33namespace JSC {
34
35const ClassInfo JSPropertyNameEnumerator::s_info = { "JSPropertyNameEnumerator", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSPropertyNameEnumerator) };
36
37JSPropertyNameEnumerator* JSPropertyNameEnumerator::create(VM& vm, Structure* structure, uint32_t indexedLength, uint32_t numberStructureProperties, PropertyNameArray&& propertyNames)
38{
39 unsigned propertyNamesSize = propertyNames.size();
40 unsigned propertyNamesBufferSizeInBytes = (Checked<unsigned>(propertyNamesSize) * sizeof(WriteBarrier<JSString>)).unsafeGet();
41 WriteBarrier<JSString>* propertyNamesBuffer = nullptr;
42 if (propertyNamesBufferSizeInBytes) {
43 propertyNamesBuffer = static_cast<WriteBarrier<JSString>*>(vm.jsValueGigacageAuxiliarySpace.allocateNonVirtual(vm, propertyNamesBufferSizeInBytes, nullptr, AllocationFailureMode::Assert));
44 for (unsigned i = 0; i < propertyNamesSize; ++i)
45 propertyNamesBuffer[i].clear();
46 }
47 JSPropertyNameEnumerator* enumerator = new (NotNull, allocateCell<JSPropertyNameEnumerator>(vm.heap)) JSPropertyNameEnumerator(vm, structure, indexedLength, numberStructureProperties, propertyNamesBuffer, propertyNamesSize);
48 enumerator->finishCreation(vm, propertyNames.releaseData());
49 return enumerator;
50}
51
52JSPropertyNameEnumerator::JSPropertyNameEnumerator(VM& vm, Structure* structure, uint32_t indexedLength, uint32_t numberStructureProperties, WriteBarrier<JSString>* propertyNamesBuffer, unsigned propertyNamesSize)
53 : JSCell(vm, vm.propertyNameEnumeratorStructure.get())
54 , m_propertyNames(vm, this, propertyNamesBuffer)
55 , m_cachedStructureID(structure ? structure->id() : 0)
56 , m_indexedLength(indexedLength)
57 , m_endStructurePropertyIndex(numberStructureProperties)
58 , m_endGenericPropertyIndex(propertyNamesSize)
59 , m_cachedInlineCapacity(structure ? structure->inlineCapacity() : 0)
60{
61}
62
63void JSPropertyNameEnumerator::finishCreation(VM& vm, RefPtr<PropertyNameArrayData>&& identifiers)
64{
65 Base::finishCreation(vm);
66
67 PropertyNameArrayData::PropertyNameVector& vector = identifiers->propertyNameVector();
68 ASSERT(m_endGenericPropertyIndex == vector.size());
69 for (unsigned i = 0; i < vector.size(); ++i) {
70 const Identifier& identifier = vector[i];
71 m_propertyNames.get()[i].set(vm, this, jsString(vm, identifier.string()));
72 }
73}
74
75void JSPropertyNameEnumerator::visitChildren(JSCell* cell, SlotVisitor& visitor)
76{
77 JSPropertyNameEnumerator* thisObject = jsCast<JSPropertyNameEnumerator*>(cell);
78 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
79 Base::visitChildren(cell, visitor);
80 if (auto* propertyNames = thisObject->m_propertyNames.get()) {
81 visitor.markAuxiliary(propertyNames);
82 visitor.append(propertyNames, propertyNames + thisObject->sizeOfPropertyNames());
83 }
84 visitor.append(thisObject->m_prototypeChain);
85
86 if (thisObject->cachedStructureID()) {
87 VM& vm = visitor.vm();
88 visitor.appendUnbarriered(vm.getStructure(thisObject->cachedStructureID()));
89 }
90}
91
92} // namespace JSC
93