• 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 "containers_arraylist.h"
17 #include "ecmascript/base/array_helper.h"
18 #include "ecmascript/base/number_helper.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/internal_call_params.h"
21 #include "ecmascript/js_api_arraylist.h"
22 #include "ecmascript/js_api_arraylist_iterator.h"
23 #include "ecmascript/js_array.h"
24 #include "ecmascript/js_function.h"
25 #include "ecmascript/js_iterator.h"
26 #include "ecmascript/object_factory.h"
27 #include "ecmascript/tagged_array-inl.h"
28 
29 namespace panda::ecmascript::containers {
ArrayListConstructor(EcmaRuntimeCallInfo * argv)30 JSTaggedValue ContainersArrayList::ArrayListConstructor(EcmaRuntimeCallInfo *argv)
31 {
32     ASSERT(argv);
33     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Constructor);
34     JSThread *thread = argv->GetThread();
35     [[maybe_unused]] EcmaHandleScope handleScope(thread);
36     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
37     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
38     if (newTarget->IsUndefined()) {
39         THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
40     }
41     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
42     JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
43     JSHandle<TaggedArray> newTaggedArray = factory->NewTaggedArray(JSAPIArrayList::DEFAULT_CAPACITY_LENGTH);
44     obj->SetElements(thread, newTaggedArray);
45     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
46 
47     return obj.GetTaggedValue();
48 }
49 
Add(EcmaRuntimeCallInfo * argv)50 JSTaggedValue ContainersArrayList::Add(EcmaRuntimeCallInfo *argv)
51 {
52     ASSERT(argv);
53     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Add);
54     JSThread *thread = argv->GetThread();
55     [[maybe_unused]] EcmaHandleScope handleScope(thread);
56     JSHandle<JSTaggedValue> self = GetThis(argv);
57 
58     if (!self->IsJSAPIArrayList()) {
59         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
60     }
61 
62     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
63     return GetTaggedBoolean(JSAPIArrayList::Add(thread, JSHandle<JSAPIArrayList>::Cast(self), value));
64 }
65 
Insert(EcmaRuntimeCallInfo * argv)66 JSTaggedValue ContainersArrayList::Insert(EcmaRuntimeCallInfo *argv)
67 {
68     ASSERT(argv);
69     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Insert);
70     JSThread *thread = argv->GetThread();
71     [[maybe_unused]] EcmaHandleScope handleScope(thread);
72     JSHandle<JSTaggedValue> self = GetThis(argv);
73 
74     if (!self->IsJSAPIArrayList()) {
75         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
76     }
77 
78     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
79     JSHandle<JSTaggedValue> index = GetCallArg(argv, 1);
80     if (!index->IsNumber()) {
81         THROW_TYPE_ERROR_AND_RETURN(thread, "index is not Integer", JSTaggedValue::Exception());
82     }
83     JSAPIArrayList::Insert(thread, JSHandle<JSAPIArrayList>::Cast(self), value, JSTaggedValue::ToUint32(thread, index));
84 
85     return JSTaggedValue::Undefined();
86 }
87 
Clear(EcmaRuntimeCallInfo * argv)88 JSTaggedValue ContainersArrayList::Clear(EcmaRuntimeCallInfo *argv)
89 {
90     ASSERT(argv);
91     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Clear);
92     JSThread *thread = argv->GetThread();
93     [[maybe_unused]] EcmaHandleScope handleScope(thread);
94     JSHandle<JSTaggedValue> self = GetThis(argv);
95 
96     if (!self->IsJSAPIArrayList()) {
97         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
98     }
99 
100     JSAPIArrayList::Clear(thread, JSHandle<JSAPIArrayList>::Cast(self));
101 
102     return JSTaggedValue::True();
103 }
104 
Clone(EcmaRuntimeCallInfo * argv)105 JSTaggedValue ContainersArrayList::Clone(EcmaRuntimeCallInfo *argv)
106 {
107     ASSERT(argv);
108     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Clone);
109     JSThread *thread = argv->GetThread();
110     [[maybe_unused]] EcmaHandleScope handleScope(thread);
111     JSHandle<JSTaggedValue> self = GetThis(argv);
112 
113     if (!self->IsJSAPIArrayList()) {
114         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
115     }
116 
117     JSHandle<JSAPIArrayList> newArrayList = JSAPIArrayList::Clone(thread, JSHandle<JSAPIArrayList>::Cast(self));
118 
119     return newArrayList.GetTaggedValue();
120 }
121 
Has(EcmaRuntimeCallInfo * argv)122 JSTaggedValue ContainersArrayList::Has(EcmaRuntimeCallInfo *argv)
123 {
124     ASSERT(argv);
125     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Has);
126     JSThread *thread = argv->GetThread();
127     [[maybe_unused]] EcmaHandleScope handleScope(thread);
128     JSHandle<JSTaggedValue> self = GetThis(argv);
129 
130     if (!self->IsJSAPIArrayList()) {
131         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
132     }
133 
134     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
135     bool isHas = JSHandle<JSAPIArrayList>::Cast(self)->Has(value.GetTaggedValue());
136 
137     return GetTaggedBoolean(isHas);
138 }
139 
GetCapacity(EcmaRuntimeCallInfo * argv)140 JSTaggedValue ContainersArrayList::GetCapacity(EcmaRuntimeCallInfo *argv)
141 {
142     ASSERT(argv);
143 
144     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, GetCapacity);
145     JSThread *thread = argv->GetThread();
146 
147     JSHandle<JSTaggedValue> self = GetThis(argv);
148     if (!self->IsJSAPIArrayList()) {
149         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
150     }
151 
152     uint32_t capacity = JSAPIArrayList::GetCapacity(thread, JSHandle<JSAPIArrayList>::Cast(self));
153 
154     return JSTaggedValue(capacity);
155 }
156 
IncreaseCapacityTo(EcmaRuntimeCallInfo * argv)157 JSTaggedValue ContainersArrayList::IncreaseCapacityTo(EcmaRuntimeCallInfo *argv)
158 {
159     ASSERT(argv);
160     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, IncreaseCapacityTo);
161     JSThread *thread = argv->GetThread();
162     [[maybe_unused]] EcmaHandleScope handleScope(thread);
163     JSHandle<JSTaggedValue> self = GetThis(argv);
164 
165     if (!self->IsJSAPIArrayList()) {
166         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
167     }
168 
169     JSHandle<JSTaggedValue> newCapacity = GetCallArg(argv, 0);
170     if (!newCapacity->IsNumber()) {
171         THROW_TYPE_ERROR_AND_RETURN(thread, "newCapacity is not Integer", JSTaggedValue::Exception());
172     }
173 
174     JSAPIArrayList::IncreaseCapacityTo(thread, JSHandle<JSAPIArrayList>::Cast(self),
175                                        JSTaggedValue::ToUint32(thread, newCapacity));
176 
177     return JSTaggedValue::True();
178 }
179 
TrimToCurrentLength(EcmaRuntimeCallInfo * argv)180 JSTaggedValue ContainersArrayList::TrimToCurrentLength(EcmaRuntimeCallInfo *argv)
181 {
182     ASSERT(argv);
183     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, TrimToCurrentLength);
184     JSThread *thread = argv->GetThread();
185     [[maybe_unused]] EcmaHandleScope handleScope(thread);
186     JSHandle<JSTaggedValue> self = GetThis(argv);
187 
188     if (!self->IsJSAPIArrayList()) {
189         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
190     }
191 
192     JSAPIArrayList::TrimToCurrentLength(thread, JSHandle<JSAPIArrayList>::Cast(self));
193 
194     return JSTaggedValue::True();
195 }
196 
Get(EcmaRuntimeCallInfo * argv)197 JSTaggedValue ContainersArrayList::Get(EcmaRuntimeCallInfo *argv)
198 {
199     ASSERT(argv);
200     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Get);
201     JSThread *thread = argv->GetThread();
202     [[maybe_unused]] EcmaHandleScope handleScope(thread);
203     JSHandle<JSTaggedValue> self = GetThis(argv);
204 
205     if (!self->IsJSAPIArrayList()) {
206         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
207     }
208 
209     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
210 
211     JSTaggedValue element = JSHandle<JSAPIArrayList>::Cast(self)->Get(thread, JSTaggedValue::ToUint32(thread, value));
212 
213     return element;
214 }
215 
GetIndexOf(EcmaRuntimeCallInfo * argv)216 JSTaggedValue ContainersArrayList::GetIndexOf(EcmaRuntimeCallInfo *argv)
217 {
218     ASSERT(argv);
219     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, GetIndexOf);
220     JSThread *thread = argv->GetThread();
221     [[maybe_unused]] EcmaHandleScope handleScope(thread);
222     JSHandle<JSTaggedValue> self = GetThis(argv);
223 
224     if (!self->IsJSAPIArrayList()) {
225         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
226     }
227 
228     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
229 
230     return JSTaggedValue(JSAPIArrayList::GetIndexOf(thread, JSHandle<JSAPIArrayList>::Cast(self), value));
231 }
232 
IsEmpty(EcmaRuntimeCallInfo * argv)233 JSTaggedValue ContainersArrayList::IsEmpty(EcmaRuntimeCallInfo *argv)
234 {
235     ASSERT(argv);
236     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, IsEmpty);
237     JSThread *thread = argv->GetThread();
238     [[maybe_unused]] EcmaHandleScope handleScope(thread);
239     JSHandle<JSTaggedValue> self = GetThis(argv);
240 
241     if (!self->IsJSAPIArrayList()) {
242         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
243     }
244 
245     return JSTaggedValue(JSAPIArrayList::IsEmpty(JSHandle<JSAPIArrayList>::Cast(self)));
246 }
247 
GetLastIndexOf(EcmaRuntimeCallInfo * argv)248 JSTaggedValue ContainersArrayList::GetLastIndexOf(EcmaRuntimeCallInfo *argv)
249 {
250     ASSERT(argv);
251     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, GetLastIndexOf);
252     JSThread *thread = argv->GetThread();
253     [[maybe_unused]] EcmaHandleScope handleScope(thread);
254     JSHandle<JSTaggedValue> self = GetThis(argv);
255 
256     if (!self->IsJSAPIArrayList()) {
257         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
258     }
259 
260     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
261 
262     return JSTaggedValue(JSAPIArrayList::GetLastIndexOf(thread, JSHandle<JSAPIArrayList>::Cast(self), value));
263 }
264 
RemoveByIndex(EcmaRuntimeCallInfo * argv)265 JSTaggedValue ContainersArrayList::RemoveByIndex(EcmaRuntimeCallInfo *argv)
266 {
267     ASSERT(argv);
268     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, RemoveByIndex);
269     JSThread *thread = argv->GetThread();
270     [[maybe_unused]] EcmaHandleScope handleScope(thread);
271     JSHandle<JSTaggedValue> self = GetThis(argv);
272 
273     if (!self->IsJSAPIArrayList()) {
274         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
275     }
276 
277     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
278     if (!value->IsNumber()) {
279         THROW_TYPE_ERROR_AND_RETURN(thread, "index is not Integer", JSTaggedValue::Exception());
280     }
281 
282     JSAPIArrayList::RemoveByIndex(thread, JSHandle<JSAPIArrayList>::Cast(self), JSTaggedValue::ToUint32(thread, value));
283 
284     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
285     return JSTaggedValue::True();
286 }
287 
Remove(EcmaRuntimeCallInfo * argv)288 JSTaggedValue ContainersArrayList::Remove(EcmaRuntimeCallInfo *argv)
289 {
290     ASSERT(argv);
291     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Remove);
292     JSThread *thread = argv->GetThread();
293     [[maybe_unused]] EcmaHandleScope handleScope(thread);
294     JSHandle<JSTaggedValue> self = GetThis(argv);
295 
296     if (!self->IsJSAPIArrayList()) {
297         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
298     }
299 
300     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
301 
302     bool isRemove = JSAPIArrayList::Remove(thread, JSHandle<JSAPIArrayList>::Cast(self), value);
303     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
304 
305     return GetTaggedBoolean(isRemove);
306 }
307 
RemoveByRange(EcmaRuntimeCallInfo * argv)308 JSTaggedValue ContainersArrayList::RemoveByRange(EcmaRuntimeCallInfo *argv)
309 {
310     ASSERT(argv);
311     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, RemoveByRange);
312     JSThread *thread = argv->GetThread();
313     [[maybe_unused]] EcmaHandleScope handleScope(thread);
314     JSHandle<JSTaggedValue> self = GetThis(argv);
315 
316     if (!self->IsJSAPIArrayList()) {
317         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
318     }
319 
320     JSHandle<JSTaggedValue> startIndex = GetCallArg(argv, 0);
321     JSHandle<JSTaggedValue> endIndex = GetCallArg(argv, 1);
322     if (!startIndex->IsNumber() || !endIndex->IsNumber()) {
323         THROW_TYPE_ERROR_AND_RETURN(thread, "startIndex or endIndex is not Integer", JSTaggedValue::Exception());
324     }
325     JSAPIArrayList::RemoveByRange(thread, JSHandle<JSAPIArrayList>::Cast(self), startIndex, endIndex);
326 
327     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
328     return JSTaggedValue::True();
329 }
330 
ReplaceAllElements(EcmaRuntimeCallInfo * argv)331 JSTaggedValue ContainersArrayList::ReplaceAllElements(EcmaRuntimeCallInfo *argv)
332 {
333     ASSERT(argv);
334     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, ReplaceAllElements);
335     JSThread *thread = argv->GetThread();
336     [[maybe_unused]] EcmaHandleScope handleScope(thread);
337     JSHandle<JSTaggedValue> self = GetThis(argv);
338 
339     if (!self->IsJSAPIArrayList()) {
340         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
341     }
342 
343     JSHandle<JSTaggedValue> callbackFnHandle = GetCallArg(argv, 0);
344     if (!callbackFnHandle->IsCallable()) {
345         THROW_TYPE_ERROR_AND_RETURN(thread, "the callbackfun is not callable.", JSTaggedValue::Exception());
346     }
347     JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1);
348 
349     return JSAPIArrayList::ReplaceAllElements(thread, self, callbackFnHandle, thisArgHandle);
350 }
351 
Set(EcmaRuntimeCallInfo * argv)352 JSTaggedValue ContainersArrayList::Set(EcmaRuntimeCallInfo *argv)
353 {
354     ASSERT(argv);
355     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, Set);
356     JSThread *thread = argv->GetThread();
357     [[maybe_unused]] EcmaHandleScope handleScope(thread);
358     JSHandle<JSTaggedValue> self = GetThis(argv);
359 
360     if (!self->IsJSAPIArrayList()) {
361         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
362     }
363 
364     JSHandle<JSTaggedValue> index = GetCallArg(argv, 0);
365     JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
366     JSHandle<JSAPIArrayList>::Cast(self)->Set(thread, JSTaggedValue::ToUint32(thread, index), value.GetTaggedValue());
367 
368     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
369     return JSTaggedValue::Undefined();
370 }
371 
SubArrayList(EcmaRuntimeCallInfo * argv)372 JSTaggedValue ContainersArrayList::SubArrayList(EcmaRuntimeCallInfo *argv)
373 {
374     ASSERT(argv);
375     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, SubArrayList);
376     JSThread *thread = argv->GetThread();
377     [[maybe_unused]] EcmaHandleScope handleScope(thread);
378     JSHandle<JSTaggedValue> self = GetThis(argv);
379 
380     if (!self->IsJSAPIArrayList()) {
381         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
382     }
383 
384     JSHandle<JSTaggedValue> value1 = GetCallArg(argv, 0);
385     JSHandle<JSTaggedValue> value2 = GetCallArg(argv, 1);
386     if (!value1->IsNumber() || !value2->IsNumber()) {
387         THROW_TYPE_ERROR_AND_RETURN(thread, "startIndex or endIndex is not Integer", JSTaggedValue::Exception());
388     }
389     JSHandle<JSAPIArrayList> newArrayList =
390         JSAPIArrayList::SubArrayList(thread, JSHandle<JSAPIArrayList>::Cast(self), value1, value2);
391 
392     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
393     return newArrayList.GetTaggedValue();
394 }
395 
Sort(EcmaRuntimeCallInfo * argv)396 JSTaggedValue ContainersArrayList::Sort(EcmaRuntimeCallInfo *argv)
397 {
398     ASSERT(argv);
399     BUILTINS_API_TRACE(argv->GetThread(), Array, Sort);
400     JSThread *thread = argv->GetThread();
401     [[maybe_unused]] EcmaHandleScope handleScope(thread);
402 
403     JSHandle<JSTaggedValue> self = GetThis(argv);
404     if (!self->IsJSAPIArrayList()) {
405         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
406     }
407 
408     JSHandle<JSTaggedValue> callbackFnHandle = GetCallArg(argv, 0);
409     if (callbackFnHandle->IsUndefined() || !callbackFnHandle->IsCallable()) {
410         THROW_TYPE_ERROR_AND_RETURN(thread, "Callable is false", JSTaggedValue::Exception());
411     }
412 
413     JSHandle<TaggedArray> elements(thread, JSHandle<JSAPIArrayList>::Cast(self)->GetElements());
414     JSMutableHandle<JSTaggedValue> presentValue(thread, JSTaggedValue::Undefined());
415     JSMutableHandle<JSTaggedValue> middleValue(thread, JSTaggedValue::Undefined());
416     JSMutableHandle<JSTaggedValue> previousValue(thread, JSTaggedValue::Undefined());
417     uint32_t length = JSHandle<JSAPIArrayList>::Cast(self)->GetLength().GetArrayLength();
418 
419     for (uint32_t i = 1; i < length; i++) {
420         uint32_t beginIndex = 0;
421         uint32_t endIndex = i;
422         presentValue.Update(elements->Get(i));
423         while (beginIndex < endIndex) {
424             uint32_t middleIndex = (beginIndex + endIndex) / 2; // 2 : half
425             middleValue.Update(elements->Get(middleIndex));
426             int32_t compareResult = base::ArrayHelper::SortCompare(thread, callbackFnHandle,
427                                                                    middleValue, presentValue);
428             RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
429             if (compareResult > 0) {
430                 endIndex = middleIndex;
431             } else {
432                 beginIndex = middleIndex + 1;
433             }
434         }
435 
436         if (endIndex >= 0 && endIndex < i) {
437             for (uint32_t j = i; j > endIndex; j--) {
438                 previousValue.Update(elements->Get(j - 1));
439                 elements->Set(thread, j, previousValue.GetTaggedValue());
440             }
441             elements->Set(thread, endIndex, presentValue.GetTaggedValue());
442         }
443     }
444 
445     return JSTaggedValue::True();
446 }
447 
GetSize(EcmaRuntimeCallInfo * argv)448 JSTaggedValue ContainersArrayList::GetSize(EcmaRuntimeCallInfo *argv)
449 {
450     ASSERT(argv);
451     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, GetSize);
452     JSThread *thread = argv->GetThread();
453     [[maybe_unused]] EcmaHandleScope handleScope(thread);
454     JSHandle<JSTaggedValue> self = GetThis(argv);
455 
456     if (!self->IsJSAPIArrayList()) {
457         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
458     }
459 
460     return JSTaggedValue(JSHandle<JSAPIArrayList>::Cast(self)->GetSize());
461 }
462 
ConvertToArray(EcmaRuntimeCallInfo * argv)463 JSTaggedValue ContainersArrayList::ConvertToArray(EcmaRuntimeCallInfo *argv)
464 {
465     ASSERT(argv);
466     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, ConvertToArray);
467     JSThread *thread = argv->GetThread();
468     [[maybe_unused]] EcmaHandleScope handleScope(thread);
469     JSHandle<JSTaggedValue> self = GetThis(argv);
470 
471     if (!self->IsJSAPIArrayList()) {
472         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
473     }
474 
475     JSHandle<JSAPIArrayList> arrayList = JSHandle<JSAPIArrayList>::Cast(self);
476     uint32_t length = arrayList->GetLength().GetArrayLength();
477     JSHandle<JSArray> array = thread->GetEcmaVM()->GetFactory()->NewJSArray();
478     array->SetArrayLength(thread, length);
479     JSHandle<TaggedArray> arrayListElements(thread, arrayList->GetElements());
480 
481     uint32_t arrayListCapacity = arrayListElements->GetLength();
482 
483     JSHandle<TaggedArray> newElements =
484         thread->GetEcmaVM()->GetFactory()->CopyArray(arrayListElements, arrayListCapacity, arrayListCapacity);
485     array->SetElements(thread, newElements);
486     return array.GetTaggedValue();
487 }
488 
ForEach(EcmaRuntimeCallInfo * argv)489 JSTaggedValue ContainersArrayList::ForEach(EcmaRuntimeCallInfo *argv)
490 {
491     ASSERT(argv);
492     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, ForEach);
493     JSThread *thread = argv->GetThread();
494     [[maybe_unused]] EcmaHandleScope handleScope(thread);
495     JSHandle<JSTaggedValue> self = GetThis(argv);
496 
497     if (!self->IsJSAPIArrayList()) {
498         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
499     }
500 
501     JSHandle<JSTaggedValue> callbackFnHandle = GetCallArg(argv, 0);
502     if (!callbackFnHandle->IsCallable()) {
503         THROW_TYPE_ERROR_AND_RETURN(thread, "the callbackfun is not callable.", JSTaggedValue::Exception());
504     }
505 
506     JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1);
507 
508     return JSAPIArrayList::ForEach(thread, self, callbackFnHandle, thisArgHandle);
509 }
510 
GetIteratorObj(EcmaRuntimeCallInfo * argv)511 JSTaggedValue ContainersArrayList::GetIteratorObj(EcmaRuntimeCallInfo *argv)
512 {
513     ASSERT(argv);
514     BUILTINS_API_TRACE(argv->GetThread(), ArrayList, GetIteratorObj);
515     JSThread *thread = argv->GetThread();
516     [[maybe_unused]] EcmaHandleScope handleScope(thread);
517 
518     JSHandle<JSTaggedValue> self = GetThis(argv);
519 
520     if (!self->IsJSAPIArrayList()) {
521         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSAPIArrayList", JSTaggedValue::Exception());
522     }
523 
524     JSTaggedValue values = JSAPIArrayList::GetIteratorObj(thread, JSHandle<JSAPIArrayList>::Cast(self));
525 
526     return values;
527 }
528 }  // namespace panda::ecmascript::containers
529