1/*
2 * Copyright (C) 2014-2018 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)
38{
39 if (!vm.emptyPropertyNameEnumerator.get()) {
40 PropertyNameArray propertyNames(&vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
41 vm.emptyPropertyNameEnumerator = Strong<JSCell>(vm, create(vm, 0, 0, 0, WTFMove(propertyNames)));
42 }
43 return jsCast<JSPropertyNameEnumerator*>(vm.emptyPropertyNameEnumerator.get());
44}
45
46JSPropertyNameEnumerator* JSPropertyNameEnumerator::create(VM& vm, Structure* structure, uint32_t indexedLength, uint32_t numberStructureProperties, PropertyNameArray&& propertyNames)
47{
48 StructureID structureID = structure ? structure->id() : 0;
49 uint32_t inlineCapacity = structure ? structure->inlineCapacity() : 0;
50 JSPropertyNameEnumerator* enumerator = new (NotNull,
51 allocateCell<JSPropertyNameEnumerator>(vm.heap)) JSPropertyNameEnumerator(vm, structureID, inlineCapacity);
52 enumerator->finishCreation(vm, indexedLength, numberStructureProperties, propertyNames.releaseData());
53 return enumerator;
54}
55
56JSPropertyNameEnumerator::JSPropertyNameEnumerator(VM& vm, StructureID structureID, uint32_t inlineCapacity)
57 : JSCell(vm, vm.propertyNameEnumeratorStructure.get())
58 , m_cachedStructureID(structureID)
59 , m_cachedInlineCapacity(inlineCapacity)
60{
61}
62
63void JSPropertyNameEnumerator::finishCreation(VM& vm, uint32_t indexedLength, uint32_t endStructurePropertyIndex, RefPtr<PropertyNameArrayData>&& identifiers)
64{
65 Base::finishCreation(vm);
66
67 PropertyNameArrayData::PropertyNameVector& vector = identifiers->propertyNameVector();
68
69 m_indexedLength = indexedLength;
70 m_endStructurePropertyIndex = endStructurePropertyIndex;
71 m_endGenericPropertyIndex = vector.size();
72
73 {
74 auto locker = lockDuringMarking(vm.heap, cellLock());
75 m_propertyNames.resizeToFit(vector.size());
76 }
77 for (unsigned i = 0; i < vector.size(); ++i) {
78 const Identifier& identifier = vector[i];
79 m_propertyNames[i].set(vm, this, jsString(&vm, identifier.string()));
80 }
81}
82
83void JSPropertyNameEnumerator::destroy(JSCell* cell)
84{
85 static_cast<JSPropertyNameEnumerator*>(cell)->JSPropertyNameEnumerator::~JSPropertyNameEnumerator();
86}
87
88void JSPropertyNameEnumerator::visitChildren(JSCell* cell, SlotVisitor& visitor)
89{
90 Base::visitChildren(cell, visitor);
91 JSPropertyNameEnumerator* thisObject = jsCast<JSPropertyNameEnumerator*>(cell);
92 auto locker = holdLock(thisObject->cellLock());
93 for (auto& propertyName : thisObject->m_propertyNames)
94 visitor.append(propertyName);
95 visitor.append(thisObject->m_prototypeChain);
96
97 if (thisObject->cachedStructureID()) {
98 VM& vm = visitor.vm();
99 visitor.appendUnbarriered(vm.getStructure(thisObject->cachedStructureID()));
100 }
101}
102
103} // namespace JSC
104