forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStructure.cpp
More file actions
432 lines (383 loc) · 18.6 KB
/
ObjectStructure.cpp
File metadata and controls
432 lines (383 loc) · 18.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright (c) 2016-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 "Object.h"
namespace Escargot {
void* ObjectStructureItemVector::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(ObjectStructureItemVector)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureItemVector, m_buffer));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(ObjectStructureItemVector));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void* ObjectStructureWithoutTransition::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(ObjectStructureWithoutTransition)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureWithoutTransition, m_properties));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(ObjectStructureWithoutTransition));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
std::pair<size_t, Optional<const ObjectStructureItem*>> ObjectStructureWithoutTransition::findProperty(const ObjectStructurePropertyName& s)
{
size_t size = m_properties->size();
if (LIKELY(s.hasAtomicString())) {
if (LIKELY(!m_hasNonAtomicPropertyName)) {
for (size_t i = 0; i < size; i++) {
if ((*m_properties)[i].m_propertyName.rawValue() == s.rawValue()) {
return std::make_pair(i, &(*m_properties)[i]);
}
}
} else {
AtomicString as = s.asAtomicString();
for (size_t i = 0; i < size; i++) {
if ((*m_properties)[i].m_propertyName == as) {
return std::make_pair(i, &(*m_properties)[i]);
}
}
}
} else if (s.isSymbol()) {
if (m_hasSymbolPropertyName) {
for (size_t i = 0; i < size; i++) {
if ((*m_properties)[i].m_propertyName == s) {
return std::make_pair(i, &(*m_properties)[i]);
}
}
}
} else {
for (size_t i = 0; i < size; i++) {
if ((*m_properties)[i].m_propertyName == s) {
return std::make_pair(i, &(*m_properties)[i]);
}
}
}
return std::make_pair(SIZE_MAX, Optional<const ObjectStructureItem*>());
}
const ObjectStructureItem& ObjectStructureWithoutTransition::readProperty(size_t idx)
{
return m_properties->at(idx);
}
const ObjectStructureItem* ObjectStructureWithoutTransition::properties() const
{
return m_properties->data();
}
size_t ObjectStructureWithoutTransition::propertyCount() const
{
return m_properties->size();
}
ObjectStructure* ObjectStructureWithoutTransition::addProperty(const ObjectStructurePropertyName& name, const ObjectStructurePropertyDescriptor& desc)
{
ObjectStructureItem newItem(name, desc);
bool nameIsIndexString = m_hasIndexPropertyName ? true : name.isIndexString();
bool nameIsSymbol = m_hasSymbolPropertyName ? true : name.isSymbol();
bool hasNonAtomicName = m_hasNonAtomicPropertyName ? true : !name.hasAtomicString();
bool hasEnumerableProperty = m_hasEnumerableProperty ? true : desc.isEnumerable();
ObjectStructure* newStructure;
m_properties->push_back(newItem);
if (m_properties->size() + 1 > ESCARGOT_OBJECT_STRUCTURE_ACCESS_CACHE_BUILD_MIN_SIZE) {
newStructure = new ObjectStructureWithMap(m_properties, ObjectStructureWithMap::createPropertyNameMap(m_properties), nameIsIndexString, nameIsSymbol, hasEnumerableProperty);
} else {
newStructure = new ObjectStructureWithoutTransition(m_properties, nameIsIndexString, nameIsSymbol, hasNonAtomicName, hasEnumerableProperty);
}
m_properties = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithoutTransition::removeProperty(size_t pIndex)
{
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector();
size_t ps = m_properties->size();
newProperties->resizeWithUninitializedValues(ps - 1);
size_t newIdx = 0;
bool hasIndexString = false;
bool hasSymbol = false;
bool hasNonAtomicName = false;
bool hasEnumerableProperty = false;
for (size_t i = 0; i < ps; i++) {
if (i == pIndex)
continue;
hasIndexString = hasIndexString | (*m_properties)[i].m_propertyName.isIndexString();
hasSymbol = hasSymbol | (*m_properties)[i].m_propertyName.isSymbol();
hasNonAtomicName = hasNonAtomicName | !(*m_properties)[i].m_propertyName.hasAtomicString();
hasEnumerableProperty = hasEnumerableProperty | (*m_properties)[i].m_descriptor.isEnumerable();
(*newProperties)[newIdx].m_propertyName = (*m_properties)[i].m_propertyName;
(*newProperties)[newIdx].m_descriptor = (*m_properties)[i].m_descriptor;
newIdx++;
}
auto newStructure = new ObjectStructureWithoutTransition(newProperties, hasIndexString, hasSymbol, hasNonAtomicName, hasEnumerableProperty);
m_properties = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithoutTransition::replacePropertyDescriptor(size_t idx, const ObjectStructurePropertyDescriptor& newDesc)
{
m_properties->at(idx).m_descriptor = newDesc;
auto newStructure = new ObjectStructureWithoutTransition(m_properties, m_hasIndexPropertyName, m_hasSymbolPropertyName, m_hasNonAtomicPropertyName, m_hasEnumerableProperty);
m_properties = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithoutTransition::convertToNonTransitionStructure()
{
return this;
}
void* ObjectStructureWithTransition::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(ObjectStructureWithTransition)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureWithTransition, m_properties));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureWithTransition, m_transitionTableVectorBuffer));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(ObjectStructureWithTransition));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
std::pair<size_t, Optional<const ObjectStructureItem*>> ObjectStructureWithTransition::findProperty(const ObjectStructurePropertyName& s)
{
size_t size = m_properties.size();
if (LIKELY(s.hasAtomicString())) {
if (LIKELY(!m_hasNonAtomicPropertyName)) {
for (size_t i = 0; i < size; i++) {
if (m_properties[i].m_propertyName.rawValue() == s.rawValue()) {
return std::make_pair(i, &m_properties[i]);
}
}
} else {
AtomicString as = s.asAtomicString();
for (size_t i = 0; i < size; i++) {
if (m_properties[i].m_propertyName == as) {
return std::make_pair(i, &m_properties[i]);
}
}
}
} else if (s.isSymbol()) {
if (m_hasSymbolPropertyName) {
for (size_t i = 0; i < size; i++) {
if (m_properties[i].m_propertyName == s) {
return std::make_pair(i, &m_properties[i]);
}
}
}
} else {
for (size_t i = 0; i < size; i++) {
if (m_properties[i].m_propertyName == s) {
return std::make_pair(i, &m_properties[i]);
}
}
}
return std::make_pair(SIZE_MAX, Optional<const ObjectStructureItem*>());
}
const ObjectStructureItem& ObjectStructureWithTransition::readProperty(size_t idx)
{
return m_properties[idx];
}
const ObjectStructureItem* ObjectStructureWithTransition::properties() const
{
return m_properties.data();
}
size_t ObjectStructureWithTransition::propertyCount() const
{
return m_properties.size();
}
ObjectStructure* ObjectStructureWithTransition::addProperty(const ObjectStructurePropertyName& name, const ObjectStructurePropertyDescriptor& desc)
{
if (m_doesTransitionTableUseMap) {
auto iter = m_transitionTableMap->find(ObjectStructureTransitionMapItem(name, desc));
if (iter != m_transitionTableMap->end()) {
return iter->second;
}
} else {
size_t len = m_transitionTableVectorBufferSize;
for (size_t i = 0; i < len; i++) {
const auto& item = m_transitionTableVectorBuffer[i];
if (item.m_descriptor == desc && item.m_propertyName == name) {
return item.m_structure;
}
}
}
ObjectStructureItem newItem(name, desc);
bool nameIsIndexString = m_hasIndexPropertyName ? true : name.isIndexString();
bool hasSymbol = m_hasSymbolPropertyName ? true : name.isSymbol();
bool hasNonAtomicName = m_hasNonAtomicPropertyName ? true : !name.hasAtomicString();
bool hasEnumerableProperty = m_hasEnumerableProperty ? true : desc.isEnumerable();
ObjectStructure* newObjectStructure;
size_t nextSize = m_properties.size() + 1;
if (nextSize > ESCARGOT_OBJECT_STRUCTURE_ACCESS_CACHE_BUILD_MIN_SIZE) {
newObjectStructure = new ObjectStructureWithMap(nameIsIndexString, hasSymbol, hasEnumerableProperty, m_properties, newItem);
} else if (nextSize > ESCARGOT_OBJECT_STRUCTURE_TRANSITION_MODE_MAX_SIZE) {
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector(m_properties, newItem);
newObjectStructure = new ObjectStructureWithoutTransition(newProperties, nameIsIndexString, hasSymbol, hasNonAtomicName, hasEnumerableProperty);
} else {
ObjectStructureItemTightVector newProperties(m_properties, newItem);
newObjectStructure = new ObjectStructureWithTransition(std::move(newProperties), nameIsIndexString, hasSymbol, hasNonAtomicName, hasEnumerableProperty);
ObjectStructureTransitionVectorItem newTransitionItem(name, desc, newObjectStructure);
if (m_doesTransitionTableUseMap) {
m_transitionTableMap->insert(std::make_pair(ObjectStructureTransitionMapItem(newTransitionItem.m_propertyName, newTransitionItem.m_descriptor),
newTransitionItem.m_structure));
} else {
if (m_transitionTableVectorBufferSize + 1 > ESCARGOT_OBJECT_STRUCTURE_TRANSITION_MAP_MIN_SIZE) {
ObjectStructureTransitionTableMap* transitionTableMap = new (GC) ObjectStructureTransitionTableMap();
for (size_t i = 0; i < m_transitionTableVectorBufferSize; i++) {
transitionTableMap->insert(std::make_pair(ObjectStructureTransitionMapItem(m_transitionTableVectorBuffer[i].m_propertyName, m_transitionTableVectorBuffer[i].m_descriptor),
m_transitionTableVectorBuffer[i].m_structure));
}
transitionTableMap->insert(std::make_pair(ObjectStructureTransitionMapItem(newTransitionItem.m_propertyName, newTransitionItem.m_descriptor),
newTransitionItem.m_structure));
GC_FREE(m_transitionTableVectorBuffer);
m_doesTransitionTableUseMap = true;
m_transitionTableMap = transitionTableMap;
m_transitionTableVectorBufferCapacity = 0;
m_transitionTableVectorBufferSize = 0;
} else {
if (m_transitionTableVectorBufferCapacity <= (size_t)(m_transitionTableVectorBufferSize + 1)) {
m_transitionTableVectorBufferCapacity = std::min(computeVectorAllocateSize(m_transitionTableVectorBufferSize + 1), (size_t)std::numeric_limits<uint8_t>::max());
m_transitionTableVectorBuffer = (ObjectStructureTransitionVectorItem*)GC_REALLOC(m_transitionTableVectorBuffer, sizeof(ObjectStructureTransitionVectorItem) * m_transitionTableVectorBufferCapacity);
}
m_transitionTableVectorBuffer[m_transitionTableVectorBufferSize] = newTransitionItem;
m_transitionTableVectorBufferSize++;
}
}
}
return newObjectStructure;
}
ObjectStructure* ObjectStructureWithTransition::removeProperty(size_t pIndex)
{
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector();
newProperties->resizeWithUninitializedValues(m_properties.size() - 1);
size_t pc = m_properties.size();
size_t newIdx = 0;
bool hasIndexString = false;
bool hasSymbol = false;
bool hasNonAtomicName = false;
bool hasEnumerableProperty = false;
for (size_t i = 0; i < pc; i++) {
if (i == pIndex)
continue;
hasIndexString = hasIndexString | m_properties[i].m_propertyName.isIndexString();
hasSymbol = hasSymbol | m_properties[i].m_propertyName.isSymbol();
hasNonAtomicName = hasNonAtomicName | !m_properties[i].m_propertyName.hasAtomicString();
hasEnumerableProperty = hasEnumerableProperty | m_properties[i].m_descriptor.isEnumerable();
(*newProperties)[newIdx].m_propertyName = m_properties[i].m_propertyName;
(*newProperties)[newIdx].m_descriptor = m_properties[i].m_descriptor;
newIdx++;
}
return new ObjectStructureWithoutTransition(newProperties, hasIndexString, hasSymbol, hasNonAtomicName, hasEnumerableProperty);
}
ObjectStructure* ObjectStructureWithTransition::replacePropertyDescriptor(size_t idx, const ObjectStructurePropertyDescriptor& newDesc)
{
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector(m_properties);
newProperties->at(idx).m_descriptor = newDesc;
return new ObjectStructureWithoutTransition(newProperties, m_hasIndexPropertyName, m_hasSymbolPropertyName, m_hasNonAtomicPropertyName, m_hasEnumerableProperty);
}
ObjectStructure* ObjectStructureWithTransition::convertToNonTransitionStructure()
{
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector(m_properties);
return new ObjectStructureWithoutTransition(newProperties, m_hasIndexPropertyName, m_hasSymbolPropertyName, m_hasNonAtomicPropertyName, m_hasEnumerableProperty);
}
void* ObjectStructureWithMap::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(ObjectStructureWithMap)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureWithMap, m_properties));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(ObjectStructureWithMap, m_propertyNameMap));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(ObjectStructureWithMap));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
std::pair<size_t, Optional<const ObjectStructureItem*>> ObjectStructureWithMap::findProperty(const ObjectStructurePropertyName& s)
{
auto iter = m_propertyNameMap->find(s);
if (iter == m_propertyNameMap->end()) {
return std::make_pair(SIZE_MAX, Optional<const ObjectStructureItem*>());
}
return std::make_pair(iter->second, &(m_properties->data()[iter->second]));
}
const ObjectStructureItem& ObjectStructureWithMap::readProperty(size_t idx)
{
return m_properties->at(idx);
}
const ObjectStructureItem* ObjectStructureWithMap::properties() const
{
return m_properties->data();
}
size_t ObjectStructureWithMap::propertyCount() const
{
return m_properties->size();
}
ObjectStructure* ObjectStructureWithMap::addProperty(const ObjectStructurePropertyName& name, const ObjectStructurePropertyDescriptor& desc)
{
ObjectStructureItem newItem(name, desc);
bool nameIsIndexString = m_hasIndexPropertyName ? true : name.isIndexString();
bool hasSymbol = m_hasSymbolPropertyName ? true : name.isSymbol();
bool hasEnumerableProperty = m_hasEnumerableProperty ? true : desc.isEnumerable();
m_propertyNameMap->insert(std::make_pair(name, m_properties->size()));
m_properties->push_back(newItem);
ObjectStructure* newStructure = new ObjectStructureWithMap(m_properties, m_propertyNameMap, nameIsIndexString, hasSymbol, hasEnumerableProperty);
m_properties = nullptr;
m_propertyNameMap = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithMap::removeProperty(size_t pIndex)
{
ObjectStructureItemVector* newProperties = new ObjectStructureItemVector();
size_t ps = m_properties->size();
newProperties->resizeWithUninitializedValues(ps - 1);
size_t newIdx = 0;
bool hasIndexString = false;
bool hasSymbol = false;
bool hasEnumerableProperty = false;
for (size_t i = 0; i < ps; i++) {
if (i == pIndex)
continue;
hasIndexString = hasIndexString | (*m_properties)[i].m_propertyName.isIndexString();
hasSymbol = hasSymbol | (*m_properties)[i].m_propertyName.isSymbol();
hasEnumerableProperty = hasEnumerableProperty | (*m_properties)[i].m_descriptor.isEnumerable();
(*newProperties)[newIdx].m_propertyName = (*m_properties)[i].m_propertyName;
(*newProperties)[newIdx].m_descriptor = (*m_properties)[i].m_descriptor;
newIdx++;
}
ObjectStructure* newStructure = new ObjectStructureWithMap(newProperties, ObjectStructureWithMap::createPropertyNameMap(newProperties), hasIndexString, hasSymbol, hasEnumerableProperty);
m_properties = nullptr;
m_propertyNameMap = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithMap::replacePropertyDescriptor(size_t idx, const ObjectStructurePropertyDescriptor& newDesc)
{
m_properties->at(idx).m_descriptor = newDesc;
ObjectStructure* newStructure = new ObjectStructureWithMap(m_properties, m_propertyNameMap, m_hasIndexPropertyName, m_hasSymbolPropertyName, m_hasEnumerableProperty);
m_properties = nullptr;
m_propertyNameMap = nullptr;
return newStructure;
}
ObjectStructure* ObjectStructureWithMap::convertToNonTransitionStructure()
{
return this;
}
} // namespace Escargot