forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerateObject.cpp
More file actions
338 lines (300 loc) · 13.1 KB
/
EnumerateObject.cpp
File metadata and controls
338 lines (300 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2019-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "EnumerateObject.h"
#include "runtime/EncodedValue.h"
#include "runtime/ArrayObject.h"
#include "runtime/TypedArrayObject.h"
namespace Escargot {
bool EnumerateObject::checkLastEnumerateKey(ExecutionState& state)
{
if (UNLIKELY(checkIfModified(state))) {
update(state);
}
if (m_index < m_keys.size()) {
return false;
}
return true;
}
void EnumerateObject::update(ExecutionState& state)
{
EncodedValueTightVector newKeys;
executeEnumeration(state, newKeys);
VectorWithInlineStorage<32, Value, GCUtil::gc_malloc_allocator<Value>> differenceKeys;
for (size_t i = 0; i < newKeys.size(); i++) {
const auto& key = newKeys[i];
// If a property that has not yet been visited during enumeration is deleted, then it will not be visited.
if (std::find(m_keys.begin(), m_keys.begin() + m_index, key) == m_keys.begin() + m_index && std::find(m_keys.begin() + m_index, m_keys.end(), key) != m_keys.end()) {
// If new properties are added to the object being enumerated during enumeration,
// the newly added properties are not guaranteed to be visited in the active enumeration.
differenceKeys.push_back(key);
}
}
m_index = 0;
m_keys.clear();
m_keys.resizeWithUninitializedValues(differenceKeys.size());
for (size_t i = 0; i < differenceKeys.size(); i++) {
m_keys[i] = differenceKeys[i];
}
}
bool EnumerateObject::checkIfModified(ExecutionState& state)
{
if (m_object->isArrayObject()) {
ArrayObject* obj = m_object->asArrayObject();
if (UNLIKELY(obj->arrayLength(state) != m_arrayLength)) {
return true;
}
if (obj->isFastModeArray() && m_index < m_keys.size()) {
Value currentKey = m_keys[m_index];
auto idx = currentKey.tryToUseAsIndex(state);
if (idx < m_arrayLength) {
if (obj->m_fastModeData[idx].isEmpty()) {
return true;
}
}
}
}
return false;
}
void* EnumerateObjectWithDestruction::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(EnumerateObjectWithDestruction)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithDestruction, m_keys));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithDestruction, m_object));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithDestruction, m_hiddenClass));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(EnumerateObjectWithDestruction));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void* EnumerateObjectWithIteration::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(EnumerateObjectWithIteration)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithIteration, m_keys));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithIteration, m_object));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(EnumerateObjectWithIteration, m_hiddenClassChain));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(EnumerateObjectWithIteration));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void EnumerateObjectWithDestruction::fillRestElement(ExecutionState& state, Object* result)
{
ASSERT(m_index == 0);
Value key, value;
while (m_index < m_keys.size()) {
if (UNLIKELY(checkIfModified(state))) {
update(state);
} else {
key = m_keys[m_index++];
// check unmarked key and put rest properties
if (!key.isEmpty()) {
value = m_object->getIndexedProperty(state, key).value(state, m_object);
result->setIndexedProperty(state, key, value);
}
}
}
}
void EnumerateObjectWithDestruction::executeEnumeration(ExecutionState& state, EncodedValueTightVector& keys)
{
ASSERT(!!m_object);
if (m_object->isArrayObject()) {
m_arrayLength = m_object->asArrayObject()->arrayLength(state);
}
m_hiddenClass = m_object->structure();
struct Properties {
std::multiset<Value::ValueIndex, std::less<Value::ValueIndex>> indexes;
VectorWithInlineStorage<32, EncodedValue, GCUtil::gc_malloc_allocator<EncodedValue>> strings;
VectorWithInlineStorage<4, Value, GCUtil::gc_malloc_allocator<Value>> symbols;
} properties;
m_object->enumeration(state, [](ExecutionState& state, Object* self, const ObjectPropertyName& name, const ObjectStructurePropertyDescriptor& desc, void* data) -> bool {
auto properties = (Properties*)data;
auto value = name.toPlainValue();
if (desc.isEnumerable()) {
Value::ValueIndex nameAsIndexValue;
if (value.isSymbol()) {
properties->symbols.push_back(value);
} else if (name.isIndexString() && (nameAsIndexValue = value.toIndex(state)) != Value::InvalidIndexValue) {
properties->indexes.insert(nameAsIndexValue);
} else {
properties->strings.push_back(value);
}
}
return true;
},
&properties, false);
keys.resizeWithUninitializedValues(properties.indexes.size() + properties.strings.size() + properties.symbols.size());
size_t idx = 0;
for (auto& v : properties.indexes) {
keys[idx++] = Value(v).toString(state);
}
for (auto& v : properties.strings) {
keys[idx++] = v;
}
for (auto& v : properties.symbols) {
keys[idx++] = v;
}
}
bool EnumerateObjectWithDestruction::checkIfModified(ExecutionState& state)
{
if (UNLIKELY(m_hiddenClass != m_object->structure())) {
return true;
}
return EnumerateObject::checkIfModified(state);
}
void EnumerateObjectWithIteration::executeEnumeration(ExecutionState& state, EncodedValueTightVector& keys)
{
ASSERT(!!m_object);
m_hiddenClassChain.clear();
if (UNLIKELY(m_object->isArrayObject())) {
m_arrayLength = m_object->asArrayObject()->arrayLength(state);
} else if (UNLIKELY(m_object->isTypedArrayObject() && m_object->asTypedArrayObject()->buffer()->isDetachedBuffer())) {
return;
}
bool shouldSearchProto = false;
m_hiddenClassChain.push_back(m_object->structure());
std::unordered_set<String*, std::hash<String*>, std::equal_to<String*>, GCUtil::gc_malloc_allocator<String*>> keyStringSet;
Object* proto = m_object->getPrototypeObject(state);
while (proto) {
if (!shouldSearchProto) {
if (proto->hasOwnEnumeration()) {
proto->enumeration(state, [](ExecutionState& state, Object* self, const ObjectPropertyName& name, const ObjectStructurePropertyDescriptor& desc, void* data) -> bool {
if (desc.isEnumerable()) {
bool* shouldSearchProto = (bool*)data;
*shouldSearchProto = true;
return false;
}
return true;
},
&shouldSearchProto);
} else {
shouldSearchProto |= proto->structure()->hasEnumerableProperty();
}
}
m_hiddenClassChain.push_back(proto->structure());
proto = proto->getPrototypeObject(state);
// v8 throw exception when there is too many things on prototype chain
if (UNLIKELY(m_hiddenClassChain.size() > 1024 * 128)) {
ErrorObject::throwBuiltinError(state, ErrorCode::RangeError, "Maximum call stack size exceeded");
}
}
if (shouldSearchProto) {
// TODO sorting properties
struct EData {
std::unordered_set<String*, std::hash<String*>, std::equal_to<String*>, GCUtil::gc_malloc_allocator<String*>>* keyStringSet;
VectorWithInlineStorage<32, EncodedValue, GCUtil::gc_malloc_allocator<EncodedValue>> keys;
Object* obj;
} eData;
eData.keyStringSet = &keyStringSet;
eData.obj = m_object;
Object* target = m_object;
while (target) {
target->enumeration(state, [](ExecutionState& state, Object* self, const ObjectPropertyName& name, const ObjectStructurePropertyDescriptor& desc, void* data) -> bool {
EData* eData = (EData*)data;
if (desc.isEnumerable()) {
String* key = name.toPlainValue().toString(state);
auto iter = eData->keyStringSet->find(key);
if (iter == eData->keyStringSet->end()) {
eData->keyStringSet->insert(key);
eData->keys.pushBack(Value(key));
}
} else if (self == eData->obj) {
// 12.6.4 The values of [[Enumerable]] attributes are not considered
// when determining if a property of a prototype object is shadowed by a previous object on the prototype chain.
String* key = name.toPlainValue().toString(state);
ASSERT(eData->keyStringSet->find(key) == eData->keyStringSet->end());
eData->keyStringSet->insert(key);
}
return true;
},
&eData);
target = target->getPrototypeObject(state);
}
keys.resizeWithUninitializedValues(eData.keys.size());
for (size_t i = 0; i < eData.keys.size(); i++) {
keys[i] = eData.keys[i];
}
} else {
if (m_object->hasOwnEnumeration() || m_object->structure()->hasIndexPropertyName()) {
struct Properties {
std::multiset<Value::ValueIndex, std::less<Value::ValueIndex>> indexes;
VectorWithInlineStorage<32, EncodedValue, GCUtil::gc_malloc_allocator<EncodedValue>> strings;
} properties;
m_object->enumeration(state, [](ExecutionState& state, Object* self, const ObjectPropertyName& name, const ObjectStructurePropertyDescriptor& desc, void* data) -> bool {
auto properties = (Properties*)data;
auto value = name.toPlainValue();
if (desc.isEnumerable()) {
Value::ValueIndex nameAsIndexValue;
if (name.isIndexString() && (nameAsIndexValue = value.toIndex(state)) != Value::InvalidIndexValue) {
properties->indexes.insert(nameAsIndexValue);
} else {
properties->strings.push_back(value);
}
}
return true;
},
&properties);
keys.resizeWithUninitializedValues(properties.indexes.size() + properties.strings.size());
size_t idx = 0;
for (auto& v : properties.indexes) {
keys[idx++] = Value(v).toString(state);
}
for (auto& v : properties.strings) {
keys[idx++] = v;
}
} else {
m_object->enumeration(state, [](ExecutionState& state, Object* self, const ObjectPropertyName& name, const ObjectStructurePropertyDescriptor& desc, void* data) -> bool {
EncodedValueTightVector* keys = reinterpret_cast<EncodedValueTightVector*>(data);
auto value = name.toPlainValue();
if (desc.isEnumerable()) {
ASSERT(!name.isIndexString() || value.toIndex(state) == Value::InvalidIndexValue);
keys->pushBack(value);
}
return true;
},
&keys);
}
}
}
bool EnumerateObjectWithIteration::checkIfModified(ExecutionState& state)
{
Object* obj = m_object;
for (size_t i = 0; i < m_hiddenClassChain.size(); i++) {
auto hc = m_hiddenClassChain[i];
ObjectStructure* structure = obj->structure();
if (UNLIKELY(hc != structure)) {
return true;
}
Object* val = obj->getPrototypeObject(state);
if (val) {
obj = val;
} else {
break;
}
}
return EnumerateObject::checkIfModified(state);
}
} // namespace Escargot
;