forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapObject.cpp
More file actions
225 lines (203 loc) · 7.3 KB
/
MapObject.cpp
File metadata and controls
225 lines (203 loc) · 7.3 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
/*
* Copyright (c) 2018-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 "runtime/MapObject.h"
#include "runtime/ArrayObject.h"
#include "runtime/Context.h"
namespace Escargot {
MapObject::MapObject(ExecutionState& state)
: MapObject(state, state.context()->globalObject()->mapPrototype())
{
}
MapObject::MapObject(ExecutionState& state, Object* proto)
: DerivedObject(state, proto)
{
}
void* MapObject::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(MapObject)] = { 0 };
Object::fillGCDescriptor(obj_bitmap);
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(MapObject, m_storage));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(MapObject));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void MapObject::clear(ExecutionState& state)
{
for (size_t i = 0; i < m_storage.size(); i++) {
m_storage[i] = std::make_pair(Value(Value::EmptyValue), Value(Value::EmptyValue));
}
}
size_t MapObject::size(ExecutionState& state)
{
size_t siz = 0;
for (size_t i = 0; i < m_storage.size(); i++) {
Value existingKey = m_storage[i].first;
if (existingKey.isEmpty()) {
continue;
}
siz++;
}
return siz;
}
bool MapObject::deleteOperation(ExecutionState& state, const Value& key)
{
for (size_t i = 0; i < m_storage.size(); i++) {
Value existingKey = m_storage[i].first;
if (existingKey.isEmpty()) {
continue;
}
if (existingKey.equalsToByTheSameValueZeroAlgorithm(key)) {
m_storage[i] = std::make_pair(Value(Value::EmptyValue), Value(Value::EmptyValue));
return true;
}
}
return false;
}
Value MapObject::get(ExecutionState& state, const Value& key)
{
for (size_t i = 0; i < m_storage.size(); i++) {
Value existingKey = m_storage[i].first;
if (existingKey.isEmpty()) {
continue;
}
if (existingKey.equalsToByTheSameValueZeroAlgorithm(key)) {
return m_storage[i].second;
}
}
return Value();
}
bool MapObject::has(ExecutionState& state, const Value& key)
{
for (size_t i = 0; i < m_storage.size(); i++) {
Value existingKey = m_storage[i].first;
if (existingKey.isEmpty()) {
continue;
}
if (existingKey.equalsToByTheSameValueZeroAlgorithm(key)) {
return true;
}
}
return false;
}
void MapObject::set(ExecutionState& state, const Value& key, const Value& value)
{
for (size_t i = 0; i < m_storage.size(); i++) {
Value existingKey = m_storage[i].first;
if (existingKey.isEmpty()) {
continue;
}
if (existingKey.equalsToByTheSameValueZeroAlgorithm(key)) {
m_storage[i].second = value;
return;
}
}
// If key is -0, let key be +0.
if (key.isNumber() && key.asNumber() == 0 && std::signbit(key.asNumber())) {
m_storage.pushBack(std::make_pair(Value(0), value));
} else {
m_storage.pushBack(std::make_pair(key, value));
}
}
IteratorObject* MapObject::values(ExecutionState& state)
{
return new MapIteratorObject(state, this, MapIteratorObject::TypeValue);
}
IteratorObject* MapObject::keys(ExecutionState& state)
{
return new MapIteratorObject(state, this, MapIteratorObject::TypeKey);
}
IteratorObject* MapObject::entries(ExecutionState& state)
{
return new MapIteratorObject(state, this, MapIteratorObject::TypeKeyValue);
}
MapIteratorObject::MapIteratorObject(ExecutionState& state, MapObject* map, Type type)
: IteratorObject(state, state.context()->globalObject()->mapIteratorPrototype())
, m_map(map)
, m_iteratorIndex(0)
, m_type(type)
{
}
void* MapIteratorObject::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(MapIteratorObject)] = { 0 };
Object::fillGCDescriptor(obj_bitmap);
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(MapIteratorObject, m_map));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(MapIteratorObject));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
std::pair<Value, bool> MapIteratorObject::advance(ExecutionState& state)
{
// Let m be the value of the [[Map]] internal slot of O.
// Let index be the value of the [[MapNextIndex]] internal slot of O.
// Let itemKind be the value of the [[MapIterationKind]] internal slot of O.
MapObject* m = m_map;
size_t index = m_iteratorIndex;
Type itemKind = m_type;
// If m is undefined, return CreateIterResultObject(undefined, true).
if (m == nullptr) {
return std::make_pair(Value(), true);
}
// Let entries be the List that is the value of the [[MapData]] internal slot of m.
// Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
while (index < m->m_storage.size()) {
// Let e be the Record {[[Key]], [[Value]]} that is the value of entries[index].
auto e = m->m_storage[index];
// Set index to index+1.
index++;
// Set the [[MapNextIndex]] internal slot of O to index.
m_iteratorIndex = index;
if (e.first.isEmpty()) {
continue;
}
// If e.[[Key]] is not empty, then
// If itemKind is "key", let result be e.[[Key]].
// Else if itemKind is "value", let result be e.[[Value]].
// Else,
// Assert: itemKind is "key+value".
// Let result be CreateArrayFromList(« e.[[Key]], e.[[Value]] »).
// Return CreateIterResultObject(result, false).
Value result;
if (itemKind == Type::TypeKey) {
result = e.first;
} else if (itemKind == Type::TypeValue) {
result = e.second;
} else if (itemKind == Type::TypeKeyValue) {
ArrayObject* arr = new ArrayObject(state, 2, false);
arr->defineOwnIndexedPropertyWithoutExpanding(state, 0, e.first);
arr->defineOwnIndexedPropertyWithoutExpanding(state, 1, e.second);
result = arr;
}
return std::make_pair(result, false);
}
// Set the [[Map]] internal slot of O to undefined.
m_map = nullptr;
// Return CreateIterResultObject(undefined, true).
return std::make_pair(Value(), true);
}
} // namespace Escargot