• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(self.GetTaggedValue().GetTaggedObject());
137     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
138     bool flag = jsMap->Has(thread, 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(self.GetTaggedValue().GetTaggedObject());
154     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
155     JSTaggedValue value = jsMap->Get(thread, key.GetTaggedValue());
156     return value;
157 }
158 
ForEach(EcmaRuntimeCallInfo * argv)159 JSTaggedValue BuiltinsMap::ForEach(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 uint32_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(EcmaRuntimeCallInfo *argv)
214 {
215     BUILTINS_API_TRACE(argv->GetThread(), Map, Species);
216     return GetThis(argv).GetTaggedValue();
217 }
218 
GetSize(EcmaRuntimeCallInfo * argv)219 JSTaggedValue BuiltinsMap::GetSize(EcmaRuntimeCallInfo *argv)
220 {
221     BUILTINS_API_TRACE(argv->GetThread(), Map, GetSize);
222     JSThread *thread = argv->GetThread();
223     [[maybe_unused]] EcmaHandleScope handleScope(thread);
224     JSHandle<JSTaggedValue> self(GetThis(argv));
225     // 2.If Type(S) is not Object, throw a TypeError exception.
226     // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
227     if (!self->IsJSMap()) {
228         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
229     }
230     JSMap *jsMap = JSMap::Cast(self.GetTaggedValue().GetTaggedObject());
231     uint32_t count = jsMap->GetSize();
232     return JSTaggedValue(count);
233 }
234 
Entries(EcmaRuntimeCallInfo * argv)235 JSTaggedValue BuiltinsMap::Entries(EcmaRuntimeCallInfo *argv)
236 {
237     BUILTINS_API_TRACE(argv->GetThread(), Map, Entries);
238     JSThread *thread = argv->GetThread();
239     [[maybe_unused]] EcmaHandleScope handleScope(thread);
240     JSHandle<JSTaggedValue> self = GetThis(argv);
241     JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY_AND_VALUE);
242     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
243     return iter.GetTaggedValue();
244 }
245 
Keys(EcmaRuntimeCallInfo * argv)246 JSTaggedValue BuiltinsMap::Keys(EcmaRuntimeCallInfo *argv)
247 {
248     BUILTINS_API_TRACE(argv->GetThread(), Map, Keys);
249     JSThread *thread = argv->GetThread();
250     [[maybe_unused]] EcmaHandleScope handleScope(thread);
251     JSHandle<JSTaggedValue> self = GetThis(argv);
252     JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY);
253     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
254     return iter.GetTaggedValue();
255 }
256 
Values(EcmaRuntimeCallInfo * argv)257 JSTaggedValue BuiltinsMap::Values(EcmaRuntimeCallInfo *argv)
258 {
259     BUILTINS_API_TRACE(argv->GetThread(), Map, Values);
260     JSThread *thread = argv->GetThread();
261     [[maybe_unused]] EcmaHandleScope handleScope(thread);
262     JSHandle<JSTaggedValue> self = GetThis(argv);
263     JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::VALUE);
264     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
265     return iter.GetTaggedValue();
266 }
267 
AddEntriesFromIterable(JSThread * thread,const JSHandle<JSObject> & target,const JSHandle<JSTaggedValue> & iterable,const JSHandle<JSTaggedValue> & adder,ObjectFactory * factory)268 JSTaggedValue BuiltinsMap::AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
269                                                   const JSHandle<JSTaggedValue> &iterable,
270                                                   const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory)
271 {
272     BUILTINS_API_TRACE(thread, Map, AddEntriesFromIterable);
273     // If IsCallable(adder) is false, throw a TypeError exception
274     if (!adder->IsCallable()) {
275         THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
276     }
277     // Let iter be GetIterator(iterable).
278     JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
279     // ReturnIfAbrupt(iter).
280     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue());
281     JSHandle<JSTaggedValue> keyIndex(thread, JSTaggedValue(0));
282     JSHandle<JSTaggedValue> valueIndex(thread, JSTaggedValue(1));
283     JSHandle<JSTaggedValue> next = JSIterator::IteratorStep(thread, iter);
284     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
285     while (!next->IsFalse()) {
286         // Let nextValue be IteratorValue(next).
287         JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
288         // ReturnIfAbrupt(nextValue).
289         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
290 
291         // If Type(nextItem) is not Object
292         if (!nextValue->IsECMAObject()) {
293             JSHandle<JSObject> typeError = factory->GetJSError(ErrorType::TYPE_ERROR, "nextItem is not Object");
294             JSHandle<JSTaggedValue> record(
295                 factory->NewCompletionRecord(CompletionRecordType::THROW, JSHandle<JSTaggedValue>(typeError)));
296             JSTaggedValue ret = JSIterator::IteratorClose(thread, iter, record).GetTaggedValue();
297             if (!thread->HasPendingException()) {
298                 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, typeError.GetTaggedValue(), ret);
299             }
300             return ret;
301         }
302         // Let k be Get(nextItem, "0").
303         JSHandle<JSTaggedValue> key = JSTaggedValue::GetProperty(thread, nextValue, keyIndex).GetValue();
304         // If k is an abrupt completion, return IteratorClose(iter, k).
305         if (thread->HasPendingException()) {
306             return JSIterator::IteratorCloseAndReturn(thread, iter);
307         }
308         // Let v be Get(nextItem, "1").
309         JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, nextValue, valueIndex).GetValue();
310         // If v is an abrupt completion, return IteratorClose(iter, v).
311         if (thread->HasPendingException()) {
312             return JSIterator::IteratorCloseAndReturn(thread, iter);
313         }
314         const uint32_t argsLength = 2;  // 2: key and value pair
315         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
316         EcmaRuntimeCallInfo *info =
317             EcmaInterpreter::NewRuntimeCallInfo(thread, adder, JSHandle<JSTaggedValue>(target), undefined, argsLength);
318         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
319         info->SetCallArg(key.GetTaggedValue(), value.GetTaggedValue());
320         JSFunction::Call(info);
321         // If status is an abrupt completion, return IteratorClose(iter, status).
322         if (thread->HasPendingException()) {
323             return JSIterator::IteratorCloseAndReturn(thread, iter);
324         }
325         // Let next be IteratorStep(iter).
326         next = JSIterator::IteratorStep(thread, iter);
327         // ReturnIfAbrupt(next).
328         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
329     }
330     return target.GetTaggedValue();
331 }
332 }  // namespace panda::ecmascript::builtins
333