1/*
2 * Copyright (C) 2011-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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "JSCast.h"
29#include "JSTypeInfo.h"
30#include "PropertyDescriptor.h"
31#include "PutDirectIndexMode.h"
32#include "VM.h"
33#include "WriteBarrier.h"
34#include <wtf/HashMap.h>
35
36namespace JSC {
37
38class SparseArrayValueMap;
39
40class SparseArrayEntry : private WriteBarrier<Unknown> {
41 WTF_MAKE_FAST_ALLOCATED;
42public:
43 using Base = WriteBarrier<Unknown>;
44
45 SparseArrayEntry()
46 {
47 Base::setWithoutWriteBarrier(jsUndefined());
48 }
49
50 void get(JSObject*, PropertySlot&) const;
51 void get(PropertyDescriptor&) const;
52 bool put(JSGlobalObject*, JSValue thisValue, SparseArrayValueMap*, JSValue, bool shouldThrow);
53 JSValue getNonSparseMode() const;
54 JSValue getConcurrently() const;
55
56 unsigned attributes() const { return m_attributes; }
57
58 void forceSet(unsigned attributes)
59 {
60 // FIXME: We can expand this for non x86 environments. Currently, loading ReadOnly | DontDelete property
61 // from compiler thread is only supported in X86 architecture because of its TSO nature.
62 // https://bugs.webkit.org/show_bug.cgi?id=134641
63 if (isX86())
64 WTF::storeStoreFence();
65 m_attributes = attributes;
66 }
67
68 void forceSet(VM& vm, JSCell* map, JSValue value, unsigned attributes)
69 {
70 Base::set(vm, map, value);
71 forceSet(attributes);
72 }
73
74 WriteBarrier<Unknown>& asValue() { return *this; }
75
76private:
77 unsigned m_attributes { 0 };
78};
79
80class SparseArrayValueMap final : public JSCell {
81public:
82 typedef JSCell Base;
83 static constexpr unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
84
85private:
86 typedef HashMap<uint64_t, SparseArrayEntry, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> Map;
87
88 enum Flags {
89 Normal = 0,
90 SparseMode = 1,
91 LengthIsReadOnly = 2,
92 };
93
94 SparseArrayValueMap(VM&);
95
96 void finishCreation(VM&);
97
98public:
99 DECLARE_EXPORT_INFO;
100
101 typedef Map::iterator iterator;
102 typedef Map::const_iterator const_iterator;
103 typedef Map::AddResult AddResult;
104
105 static SparseArrayValueMap* create(VM&);
106
107 static constexpr bool needsDestruction = true;
108 static void destroy(JSCell*);
109
110 template<typename CellType, SubspaceAccess>
111 static IsoSubspace* subspaceFor(VM& vm)
112 {
113 return &vm.sparseArrayValueMapSpace;
114 }
115
116 static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
117
118 static void visitChildren(JSCell*, SlotVisitor&);
119
120 bool sparseMode()
121 {
122 return m_flags & SparseMode;
123 }
124
125 void setSparseMode()
126 {
127 m_flags = static_cast<Flags>(m_flags | SparseMode);
128 }
129
130 bool lengthIsReadOnly()
131 {
132 return m_flags & LengthIsReadOnly;
133 }
134
135 void setLengthIsReadOnly()
136 {
137 m_flags = static_cast<Flags>(m_flags | LengthIsReadOnly);
138 }
139
140 // These methods may mutate the contents of the map
141 bool putEntry(JSGlobalObject*, JSObject*, unsigned, JSValue, bool shouldThrow);
142 bool putDirect(JSGlobalObject*, JSObject*, unsigned, JSValue, unsigned attributes, PutDirectIndexMode);
143 AddResult add(JSObject*, unsigned);
144 iterator find(unsigned i) { return m_map.find(i); }
145 // This should ASSERT the remove is valid (check the result of the find).
146 void remove(iterator it);
147 void remove(unsigned i);
148
149 JSValue getConcurrently(unsigned index);
150
151 // These methods do not mutate the contents of the map.
152 iterator notFound() { return m_map.end(); }
153 bool isEmpty() const { return m_map.isEmpty(); }
154 bool contains(unsigned i) const { return m_map.contains(i); }
155 size_t size() const { return m_map.size(); }
156 // Only allow const begin/end iteration.
157 const_iterator begin() const { return m_map.begin(); }
158 const_iterator end() const { return m_map.end(); }
159
160private:
161 Map m_map;
162 Flags m_flags { Normal };
163 size_t m_reportedCapacity { 0 };
164};
165
166} // namespace JSC
167