1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_map.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/js_map.h"
22 #include "ecmascript/js_map_iterator.h"
23 #include "ecmascript/linked_hash_table.h"
24 #include "ecmascript/object_factory.h"
25
26 namespace panda::ecmascript::builtins {
MapConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsMap::MapConstructor(EcmaRuntimeCallInfo *argv)
28 {
29 BUILTINS_API_TRACE(argv->GetThread(), Map, Constructor);
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope handleScope(thread);
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 // 1.If NewTarget is undefined, throw a TypeError exception
34 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
35 if (newTarget->IsUndefined()) {
36 // throw type error
37 THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
38 }
39 // 2.Let Map be OrdinaryCreateFromConstructor(NewTarget, "%MapPrototype%", «[[MapData]]» ).
40 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
41 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
42 // 3.returnIfAbrupt()
43 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
44 JSHandle<JSMap> map = JSHandle<JSMap>::Cast(obj);
45
46 // 4.Set map’s [[MapData]] internal slot to a new empty List.
47 JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
48 map->SetLinkedMap(thread, linkedMap);
49 // add data into set from iterable
50 // 5.If iterable is not present, let iterable be undefined.
51 // 6.If iterable is either undefined or null, let iter be undefined.
52 JSHandle<JSTaggedValue> iterable = GetCallArg(argv, 0);
53 // 8.If iter is undefined, return set
54 if (iterable->IsUndefined() || iterable->IsNull()) {
55 return map.GetTaggedValue();
56 }
57 if (!iterable->IsECMAObject()) {
58 THROW_TYPE_ERROR_AND_RETURN(thread, "iterable is not object", JSTaggedValue::Exception());
59 }
60 // Let adder be Get(map, "set").
61 JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledSetString();
62 JSHandle<JSTaggedValue> adder = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(map), adderKey).GetValue();
63 // ReturnIfAbrupt(adder).
64 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
65 return AddEntriesFromIterable(thread, obj, iterable, adder, factory);
66 }
67
Set(EcmaRuntimeCallInfo * argv)68 JSTaggedValue BuiltinsMap::Set(EcmaRuntimeCallInfo *argv)
69 {
70 BUILTINS_API_TRACE(argv->GetThread(), Map, Set);
71 JSThread *thread = argv->GetThread();
72 [[maybe_unused]] EcmaHandleScope handleScope(thread);
73 JSHandle<JSTaggedValue> self = GetThis(argv);
74
75 // 2.If Type(S) is not Object, throw a TypeError exception.
76 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
77 if (!self->IsJSMap()) {
78 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
79 }
80
81 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
82 JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
83
84 JSHandle<JSMap> map(self);
85 JSMap::Set(thread, map, key, value);
86 return map.GetTaggedValue();
87 }
88
Clear(EcmaRuntimeCallInfo * argv)89 JSTaggedValue BuiltinsMap::Clear(EcmaRuntimeCallInfo *argv)
90 {
91 BUILTINS_API_TRACE(argv->GetThread(), Map, Clear);
92 JSThread *thread = argv->GetThread();
93 [[maybe_unused]] EcmaHandleScope handleScope(thread);
94
95 JSHandle<JSTaggedValue> self = GetThis(argv);
96
97 // 2.If Type(S) is not Object, throw a TypeError exception.
98 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
99 if (!self->IsJSMap()) {
100 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
101 }
102 JSHandle<JSMap> map(self);
103 JSMap::Clear(thread, map);
104 return JSTaggedValue::Undefined();
105 }
106
Delete(EcmaRuntimeCallInfo * argv)107 JSTaggedValue BuiltinsMap::Delete(EcmaRuntimeCallInfo *argv)
108 {
109 BUILTINS_API_TRACE(argv->GetThread(), Map, Delete);
110 JSThread *thread = argv->GetThread();
111 [[maybe_unused]] EcmaHandleScope handleScope(thread);
112 JSHandle<JSTaggedValue> self = GetThis(argv);
113 // 2.If Type(S) is not Object, throw a TypeError exception.
114 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
115 if (!self->IsJSMap()) {
116 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
117 }
118
119 JSHandle<JSMap> map(self);
120 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
121 bool flag = JSMap::Delete(thread, map, key);
122 return GetTaggedBoolean(flag);
123 }
124
Has(EcmaRuntimeCallInfo * argv)125 JSTaggedValue BuiltinsMap::Has(EcmaRuntimeCallInfo *argv)
126 {
127 BUILTINS_API_TRACE(argv->GetThread(), Map, Has);
128 JSThread *thread = argv->GetThread();
129 [[maybe_unused]] EcmaHandleScope handleScope(thread);
130 JSHandle<JSTaggedValue> self(GetThis(argv));
131 // 2.If Type(S) is not Object, throw a TypeError exception.
132 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
133 if (!self->IsJSMap()) {
134 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
135 }
136 JSMap *jsMap = JSMap::Cast(*JSTaggedValue::ToObject(thread, self));
137 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
138 bool flag = jsMap->Has(key.GetTaggedValue());
139 return GetTaggedBoolean(flag);
140 }
141
Get(EcmaRuntimeCallInfo * argv)142 JSTaggedValue BuiltinsMap::Get(EcmaRuntimeCallInfo *argv)
143 {
144 BUILTINS_API_TRACE(argv->GetThread(), Map, Get);
145 JSThread *thread = argv->GetThread();
146 [[maybe_unused]] EcmaHandleScope handleScope(thread);
147 JSHandle<JSTaggedValue> self(GetThis(argv));
148 // 2.If Type(S) is not Object, throw a TypeError exception.
149 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
150 if (!self->IsJSMap()) {
151 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
152 }
153 JSMap *jsMap = JSMap::Cast(*JSTaggedValue::ToObject(thread, self));
154 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
155 JSTaggedValue value = jsMap->Get(key.GetTaggedValue());
156 return value;
157 }
158
ForEach(EcmaRuntimeCallInfo * argv)159 JSTaggedValue BuiltinsMap::ForEach([[maybe_unused]] EcmaRuntimeCallInfo *argv)
160 {
161 JSThread *thread = argv->GetThread();
162 BUILTINS_API_TRACE(thread, Map, ForEach);
163 [[maybe_unused]] EcmaHandleScope handleScope(thread);
164 JSHandle<JSTaggedValue> self = GetThis(argv);
165 // 2.If Type(S) is not Object, throw a TypeError exception.
166 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
167 if (!self->IsJSMap()) {
168 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
169 }
170 JSHandle<JSMap> map(self);
171
172 // 4.If IsCallable(callbackfn) is false, throw a TypeError exception.
173 JSHandle<JSTaggedValue> func(GetCallArg(argv, 0));
174 if (!func->IsCallable()) {
175 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not Callable", JSTaggedValue::Exception());
176 }
177 // 5.If thisArg was supplied, let T be thisArg; else let T be undefined.
178 JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1);
179
180 JSMutableHandle<LinkedHashMap> hashMap(thread, map->GetLinkedMap());
181 const int32_t argsLength = 3;
182 int index = 0;
183 int totalElements = hashMap->NumberOfElements() + hashMap->NumberOfDeletedElements();
184 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
185 // 7.Repeat for each e that is an element of entries, in original insertion order
186 while (index < totalElements) {
187 JSHandle<JSTaggedValue> key(thread, hashMap->GetKey(index++));
188 // a. If e is not empty, then
189 if (!key->IsHole()) {
190 JSHandle<JSTaggedValue> value(thread, hashMap->GetValue(index - 1));
191 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(
192 thread, func, thisArg, undefined, argsLength);
193 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
194 info->SetCallArg(value.GetTaggedValue(), key.GetTaggedValue(), map.GetTaggedValue());
195 // i. Let funcResult be Call(callbackfn, T, «e, e, S»).
196 JSTaggedValue ret = JSFunction::Call(info); // 3: three args
197 // ii. ReturnIfAbrupt(funcResult).
198 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
199 // Maybe add or delete
200 JSTaggedValue nextTable = hashMap->GetNextTable();
201 while (!nextTable.IsHole()) {
202 index -= hashMap->GetDeletedElementsAt(index);
203 hashMap.Update(nextTable);
204 nextTable = hashMap->GetNextTable();
205 }
206 totalElements = hashMap->NumberOfElements() + hashMap->NumberOfDeletedElements();
207 }
208 }
209
210 return JSTaggedValue::Undefined();
211 }
212
Species(EcmaRuntimeCallInfo * argv)213 JSTaggedValue BuiltinsMap::Species([[maybe_unused]] EcmaRuntimeCallInfo *argv)
214 {
215 return GetThis(argv).GetTaggedValue();
216 }
217
GetSize(EcmaRuntimeCallInfo * argv)218 JSTaggedValue BuiltinsMap::GetSize(EcmaRuntimeCallInfo *argv)
219 {
220 BUILTINS_API_TRACE(argv->GetThread(), Map, GetSize);
221 JSThread *thread = argv->GetThread();
222 [[maybe_unused]] EcmaHandleScope handleScope(thread);
223 JSHandle<JSTaggedValue> self(GetThis(argv));
224 // 2.If Type(S) is not Object, throw a TypeError exception.
225 // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
226 if (!self->IsJSMap()) {
227 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
228 }
229 JSMap *jsMap = JSMap::Cast(*JSTaggedValue::ToObject(thread, self));
230 int count = jsMap->GetSize();
231 return JSTaggedValue(count);
232 }
233
Entries(EcmaRuntimeCallInfo * argv)234 JSTaggedValue BuiltinsMap::Entries(EcmaRuntimeCallInfo *argv)
235 {
236 BUILTINS_API_TRACE(argv->GetThread(), Map, Entries);
237 JSThread *thread = argv->GetThread();
238 [[maybe_unused]] EcmaHandleScope handleScope(thread);
239 JSHandle<JSTaggedValue> self = GetThis(argv);
240 JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY_AND_VALUE);
241 return iter.GetTaggedValue();
242 }
243
Keys(EcmaRuntimeCallInfo * argv)244 JSTaggedValue BuiltinsMap::Keys(EcmaRuntimeCallInfo *argv)
245 {
246 BUILTINS_API_TRACE(argv->GetThread(), Map, Keys);
247 JSThread *thread = argv->GetThread();
248 [[maybe_unused]] EcmaHandleScope handleScope(thread);
249 JSHandle<JSTaggedValue> self = GetThis(argv);
250 JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY);
251 return iter.GetTaggedValue();
252 }
253
Values(EcmaRuntimeCallInfo * argv)254 JSTaggedValue BuiltinsMap::Values(EcmaRuntimeCallInfo *argv)
255 {
256 BUILTINS_API_TRACE(argv->GetThread(), Map, Values);
257 JSThread *thread = argv->GetThread();
258 [[maybe_unused]] EcmaHandleScope handleScope(thread);
259 JSHandle<JSTaggedValue> self = GetThis(argv);
260 JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::VALUE);
261 return iter.GetTaggedValue();
262 }
263
AddEntriesFromIterable(JSThread * thread,const JSHandle<JSObject> & target,const JSHandle<JSTaggedValue> & iterable,const JSHandle<JSTaggedValue> & adder,ObjectFactory * factory)264 JSTaggedValue BuiltinsMap::AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
265 const JSHandle<JSTaggedValue> &iterable,
266 const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory)
267 {
268 // If IsCallable(adder) is false, throw a TypeError exception
269 if (!adder->IsCallable()) {
270 THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
271 }
272 // Let iter be GetIterator(iterable).
273 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
274 // ReturnIfAbrupt(iter).
275 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue());
276 JSHandle<JSTaggedValue> keyIndex(thread, JSTaggedValue(0));
277 JSHandle<JSTaggedValue> valueIndex(thread, JSTaggedValue(1));
278 JSHandle<JSTaggedValue> next = JSIterator::IteratorStep(thread, iter);
279 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
280 while (!next->IsFalse()) {
281 // Let nextValue be IteratorValue(next).
282 JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
283 // ReturnIfAbrupt(nextValue).
284 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
285
286 // If Type(nextItem) is not Object
287 if (!nextValue->IsECMAObject()) {
288 JSHandle<JSObject> typeError = factory->GetJSError(ErrorType::TYPE_ERROR, "nextItem is not Object");
289 JSHandle<JSTaggedValue> record(
290 factory->NewCompletionRecord(CompletionRecordType::THROW, JSHandle<JSTaggedValue>(typeError)));
291 JSTaggedValue ret = JSIterator::IteratorClose(thread, iter, record).GetTaggedValue();
292 if (!thread->HasPendingException()) {
293 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, typeError.GetTaggedValue(), ret);
294 }
295 return ret;
296 }
297 // Let k be Get(nextItem, "0").
298 JSHandle<JSTaggedValue> key = JSObject::GetProperty(thread, nextValue, keyIndex).GetValue();
299 // If k is an abrupt completion, return IteratorClose(iter, k).
300 if (thread->HasPendingException()) {
301 return JSIterator::IteratorCloseAndReturn(thread, iter);
302 }
303 // Let v be Get(nextItem, "1").
304 JSHandle<JSTaggedValue> value = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
305 // If v is an abrupt completion, return IteratorClose(iter, v).
306 if (thread->HasPendingException()) {
307 return JSIterator::IteratorCloseAndReturn(thread, iter);
308 }
309 const int32_t argsLength = 2; // 2: key and value pair
310 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
311 EcmaRuntimeCallInfo *info =
312 EcmaInterpreter::NewRuntimeCallInfo(thread, adder, JSHandle<JSTaggedValue>(target), undefined, argsLength);
313 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
314 info->SetCallArg(key.GetTaggedValue(), value.GetTaggedValue());
315 JSFunction::Call(info);
316 // If status is an abrupt completion, return IteratorClose(iter, status).
317 if (thread->HasPendingException()) {
318 return JSIterator::IteratorCloseAndReturn(thread, iter);
319 }
320 // Let next be IteratorStep(iter).
321 next = JSIterator::IteratorStep(thread, iter);
322 // ReturnIfAbrupt(next).
323 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
324 }
325 return target.GetTaggedValue();
326 }
327 } // namespace panda::ecmascript::builtins
328