• 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 "builtin_test_util.h"
17 #include "ecmascript/builtins/builtins_typedarray.h"
18 
19 #include "ecmascript/base/typed_array_helper-inl.h"
20 #include "ecmascript/base/typed_array_helper.h"
21 #include "ecmascript/builtins/builtins_array.h"
22 #include "ecmascript/builtins/builtins_object.h"
23 #include "ecmascript/ecma_runtime_call_info.h"
24 #include "ecmascript/ecma_string.h"
25 #include "ecmascript/ecma_vm.h"
26 #include "ecmascript/global_env.h"
27 #include "ecmascript/js_array.h"
28 #include "ecmascript/js_array_iterator.h"
29 #include "ecmascript/js_handle.h"
30 #include "ecmascript/js_hclass.h"
31 #include "ecmascript/js_object-inl.h"
32 #include "ecmascript/js_tagged_value-inl.h"
33 #include "ecmascript/js_tagged_value.h"
34 #include "ecmascript/js_thread.h"
35 #include "ecmascript/js_typed_array.h"
36 #include "ecmascript/object_factory.h"
37 #include "ecmascript/object_operator.h"
38 #include "ecmascript/tests/test_helper.h"
39 
40 using namespace panda::ecmascript;
41 using namespace panda::ecmascript::builtins;
42 using namespace panda::ecmascript::base;
43 
44 namespace panda::test {
45 using Array = ecmascript::builtins::BuiltinsArray;
46 using TypedArray = ecmascript::builtins::BuiltinsTypedArray;
47 using TypedArrayHelper = ecmascript::base::TypedArrayHelper;
48 constexpr uint32_t ECMA_RUNTIME_CALL_INFO_4 = 4;
49 constexpr uint32_t ECMA_RUNTIME_CALL_INFO_6 = 6;
50 
51 enum class TypeArrayIndex {
52     TYPED_ARRAY_INDEX_0,
53     TYPED_ARRAY_INDEX_1,
54     TYPED_ARRAY_INDEX_2,
55     TYPED_ARRAY_INDEX_3
56 };
57 constexpr uint32_t TYPED_ARRAY_LENGTH_3 = 3;
58 constexpr int32_t INT_VALUE_0 = 0;
59 constexpr int32_t INT_VALUE_2 = 2;
60 constexpr int32_t INT_VALUE_4 = 4;
61 constexpr int32_t INT_VALUE_9 = 9;
62 
63 class BuiltinsTypedArrayTest : public BaseTestWithScope<false> {
64 protected:
65     class TestClass : public base::BuiltinsBase {
66     public:
TestForEachFunc(EcmaRuntimeCallInfo * argv)67         static JSTaggedValue TestForEachFunc(EcmaRuntimeCallInfo *argv)
68         {
69             JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
70             if (key->IsUndefined()) {
71                 return JSTaggedValue::Undefined();
72             }
73             JSArray *array = JSArray::Cast(GetThis(argv)->GetTaggedObject());
74             uint32_t len = array->GetArrayLength() + 1U;
75             array->SetArrayLength(argv->GetThread(), len);
76             return JSTaggedValue::Undefined();
77         }
78 
TestEveryFunc(EcmaRuntimeCallInfo * argv)79         static JSTaggedValue TestEveryFunc(EcmaRuntimeCallInfo *argv)
80         {
81             uint32_t argc = argv->GetArgsNumber();
82             if (argc > 0) {
83                 [[maybe_unused]] int aaa = GetCallArg(argv, 0)->GetInt();
84                 //  10 : test case
85                 if (GetCallArg(argv, 0)->GetInt() > 10) {
86                     return GetTaggedBoolean(true);
87                 }
88             }
89             return GetTaggedBoolean(false);
90         }
91 
TestFilterFunc(EcmaRuntimeCallInfo * argv)92         static JSTaggedValue TestFilterFunc(EcmaRuntimeCallInfo *argv)
93         {
94             ASSERT(argv);
95             uint32_t argc = argv->GetArgsNumber();
96             if (argc > 0) {
97                 // 10 : test case
98                 if (GetCallArg(argv, 0)->GetInt() > 10) {
99                     return GetTaggedBoolean(true);
100                 }
101             }
102             return GetTaggedBoolean(false);
103         }
104 
TestMapFunc(EcmaRuntimeCallInfo * argv)105         static JSTaggedValue TestMapFunc(EcmaRuntimeCallInfo *argv)
106         {
107             int accumulator = GetCallArg(argv, 0)->GetInt();
108             accumulator = accumulator * 2; // 2 : mapped to 2 times the original value
109             return BuiltinsBase::GetTaggedInt(accumulator);
110         }
111 
TestFindFunc(EcmaRuntimeCallInfo * argv)112         static JSTaggedValue TestFindFunc(EcmaRuntimeCallInfo *argv)
113         {
114             uint32_t argc = argv->GetArgsNumber();
115             if (argc > 0) {
116                 // 10 : test case
117                 if (GetCallArg(argv, 0)->GetInt() > 10) {
118                     return GetTaggedBoolean(true);
119                 }
120             }
121             return GetTaggedBoolean(false);
122         }
123 
TestFindIndexFunc(EcmaRuntimeCallInfo * argv)124         static JSTaggedValue TestFindIndexFunc(EcmaRuntimeCallInfo *argv)
125         {
126             uint32_t argc = argv->GetArgsNumber();
127             if (argc > 0) {
128                 //  10 : test case
129                 if (GetCallArg(argv, 0)->GetInt() > 10) {
130                     return GetTaggedBoolean(true);
131                 }
132             }
133             return GetTaggedBoolean(false);
134         }
135 
TestToSortedFunc(EcmaRuntimeCallInfo * argv)136         static JSTaggedValue TestToSortedFunc(EcmaRuntimeCallInfo *argv)
137         {
138             uint32_t argc = argv->GetArgsNumber();
139             if (argc > 1) {
140                 // x < y
141                 if (GetCallArg(argv, 0)->GetInt() < GetCallArg(argv, 1)->GetInt()) {
142                     return GetTaggedBoolean(true);
143                 }
144             }
145             return GetTaggedBoolean(false);
146         }
147 
TestFindLastIndexFunc(EcmaRuntimeCallInfo * argv)148         static JSTaggedValue TestFindLastIndexFunc(EcmaRuntimeCallInfo *argv)
149         {
150             uint32_t argc = argv->GetArgsNumber();
151             if (argc > 0) {
152                 // 20 : test case
153                 if (GetCallArg(argv, 0)->GetInt() > 20) {
154                     return GetTaggedBoolean(true);
155                 }
156             }
157             return GetTaggedBoolean(false);
158         }
159 
TestFindLastFunc(EcmaRuntimeCallInfo * argv)160         static JSTaggedValue TestFindLastFunc(EcmaRuntimeCallInfo *argv)
161         {
162             uint32_t argc = argv->GetArgsNumber();
163             if (argc > 0) {
164                 // 20 : test case
165                 if (GetCallArg(argv, 0)->GetInt() > 20) {
166                     return GetTaggedBoolean(true);
167                 }
168             }
169             return GetTaggedBoolean(false);
170         }
171 
TestSomeFunc(EcmaRuntimeCallInfo * argv)172         static JSTaggedValue TestSomeFunc(EcmaRuntimeCallInfo *argv)
173         {
174             uint32_t argc = argv->GetArgsNumber();
175             if (argc > 0) {
176                 //  10 : test case
177                 if (GetCallArg(argv, 0)->GetInt() > 10) {
178                     return GetTaggedBoolean(true);
179                 }
180             }
181             return GetTaggedBoolean(false);
182         }
183 
TestReduceFunc(EcmaRuntimeCallInfo * argv)184         static JSTaggedValue TestReduceFunc(EcmaRuntimeCallInfo *argv)
185         {
186             int accumulator = GetCallArg(argv, 0)->GetInt();
187             accumulator = accumulator + GetCallArg(argv, 1)->GetInt();
188             return BuiltinsBase::GetTaggedInt(accumulator);
189         }
190 
TestReduceRightFunc(EcmaRuntimeCallInfo * argv)191         static JSTaggedValue TestReduceRightFunc(EcmaRuntimeCallInfo *argv)
192         {
193             int accumulator = GetCallArg(argv, 0)->GetInt();
194             accumulator = accumulator + GetCallArg(argv, 1)->GetInt();
195             return BuiltinsBase::GetTaggedInt(accumulator);
196         }
197     };
198 };
199 
CreateBuiltinsTypeArrayJSObject(JSThread * thread,const CString keyCStr)200 JSTaggedValue CreateBuiltinsTypeArrayJSObject(JSThread *thread, const CString keyCStr)
201 {
202     auto ecmaVM = thread->GetEcmaVM();
203     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
204     JSHandle<JSTaggedValue> hclass = env->GetObjectFunction();
205     ObjectFactory *factory = ecmaVM->GetFactory();
206 
207     JSHandle<JSTaggedValue> obj(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
208     JSHandle<JSTaggedValue> key(factory->NewFromASCII(&keyCStr[0]));
209     JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
210     JSObject::SetProperty(thread, obj, key, value);
211     return obj.GetTaggedValue();
212 }
213 
HWTEST_F_L0(BuiltinsTypedArrayTest,Species)214 HWTEST_F_L0(BuiltinsTypedArrayTest, Species)
215 {
216     auto ecmaVM = thread->GetEcmaVM();
217     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
218     JSHandle<JSFunction> array(env->GetArrayFunction());
219     JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
220 
221     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
222     ecmaRuntimeCallInfo1->SetFunction(array.GetTaggedValue());
223     ecmaRuntimeCallInfo1->SetThis(globalObject.GetTaggedValue());
224 
225     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
226     JSTaggedValue result = TypedArray::Species(ecmaRuntimeCallInfo1);
227     ASSERT_TRUE(result.IsECMAObject());
228 }
229 
HWTEST_F_L0(BuiltinsTypedArrayTest,Includes)230 HWTEST_F_L0(BuiltinsTypedArrayTest, Includes)
231 {
232     ASSERT_NE(thread, nullptr);
233     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
234     [[maybe_unused]] JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
235     array->Set(thread, 0, JSTaggedValue(2));
236     array->Set(thread, 1, JSTaggedValue(3));
237     array->Set(thread, 2, JSTaggedValue(4));
238 
239     [[maybe_unused]] JSHandle<JSTaggedValue> obj =
240         JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
241     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
242     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
243     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
244     ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(2)));
245 
246     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
247     [[maybe_unused]] JSTaggedValue result = TypedArray::Includes(ecmaRuntimeCallInfo1);
248     TestHelper::TearDownFrame(thread, prev);
249 
250     ASSERT_TRUE(result.JSTaggedValue::ToBoolean()); // new Int8Array[2,3,4].includes(2)
251 
252     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
253     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
254     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
255     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(1)));
256 
257     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
258     result = TypedArray::Includes(ecmaRuntimeCallInfo2);
259     TestHelper::TearDownFrame(thread, prev);
260 
261     ASSERT_TRUE(!result.JSTaggedValue::ToBoolean()); // new Int8Array[2,3,4].includes(1)
262 
263     auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
264     ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
265     ecmaRuntimeCallInfo3->SetThis(obj.GetTaggedValue());
266     ecmaRuntimeCallInfo3->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(3)));
267     ecmaRuntimeCallInfo3->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1)));
268 
269     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
270     result = TypedArray::Includes(ecmaRuntimeCallInfo3);
271     TestHelper::TearDownFrame(thread, prev);
272 
273     ASSERT_TRUE(result.JSTaggedValue::ToBoolean()); // new Int8Array[2,3,4].includes(3, 1)
274 
275     auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
276     ecmaRuntimeCallInfo4->SetFunction(JSTaggedValue::Undefined());
277     ecmaRuntimeCallInfo4->SetThis(obj.GetTaggedValue());
278     ecmaRuntimeCallInfo4->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(2)));
279     ecmaRuntimeCallInfo4->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
280 
281     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4);
282     result = Array::Includes(ecmaRuntimeCallInfo4);
283     TestHelper::TearDownFrame(thread, prev);
284 
285     ASSERT_TRUE(!result.JSTaggedValue::ToBoolean()); // new Int8Array[2,3,4].includes(2, 5)
286 
287     auto ecmaRuntimeCallInfo5 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
288     ecmaRuntimeCallInfo5->SetFunction(JSTaggedValue::Undefined());
289     ecmaRuntimeCallInfo5->SetThis(obj.GetTaggedValue());
290     ecmaRuntimeCallInfo5->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(2)));
291     ecmaRuntimeCallInfo5->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(-2)));
292 
293     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo5);
294     result = Array::Includes(ecmaRuntimeCallInfo5);
295     TestHelper::TearDownFrame(thread, prev);
296 
297     ASSERT_TRUE(!result.JSTaggedValue::ToBoolean()); // new Int8Array[2,3,4].includes(2, -2)
298 }
299 
HWTEST_F_L0(BuiltinsTypedArrayTest,At)300 HWTEST_F_L0(BuiltinsTypedArrayTest, At)
301 {
302     ASSERT_NE(thread, nullptr);
303     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
304     [[maybe_unused]] JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
305     array->Set(thread, 0, JSTaggedValue(2));
306     array->Set(thread, 1, JSTaggedValue(3));
307     array->Set(thread, 2, JSTaggedValue(4));
308 
309     [[maybe_unused]] JSHandle<JSTaggedValue> obj =
310         JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
311     std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<int32_t>(2))};
312     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6, obj.GetTaggedValue());
313 
314     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
315     [[maybe_unused]] JSTaggedValue result = TypedArray::At(ecmaRuntimeCallInfo1);
316     TestHelper::TearDownFrame(thread, prev);
317 
318     ASSERT_EQ(result, JSTaggedValue(4));
319 
320     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
321     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
322     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
323     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(-2)));
324 
325     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
326     result = TypedArray::At(ecmaRuntimeCallInfo2);
327     TestHelper::TearDownFrame(thread, prev);
328 
329     ASSERT_EQ(result, JSTaggedValue(3));
330 
331     auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
332     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
333     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
334     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(-4)));
335 
336     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
337     result = TypedArray::At(ecmaRuntimeCallInfo3);
338     TestHelper::TearDownFrame(thread, prev);
339 
340     ASSERT_TRUE(result.IsUndefined());
341 
342     auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
343     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
344     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
345     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(3)));
346 
347     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4);
348     result = TypedArray::At(ecmaRuntimeCallInfo4);
349     TestHelper::TearDownFrame(thread, prev);
350 
351     ASSERT_TRUE(result.IsUndefined());
352 }
353 
HWTEST_F_L0(BuiltinsTypedArrayTest,ToReversed)354 HWTEST_F_L0(BuiltinsTypedArrayTest, ToReversed)
355 {
356     ASSERT_NE(thread, nullptr);
357     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
358     [[maybe_unused]] JSHandle<TaggedArray> array(factory->NewTaggedArray(TYPED_ARRAY_LENGTH_3));
359     array->Set(thread, static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_0), JSTaggedValue(INT_VALUE_0));
360     array->Set(thread, static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_1), JSTaggedValue(INT_VALUE_4));
361     array->Set(thread, static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_2), JSTaggedValue(INT_VALUE_9));
362 
363     [[maybe_unused]] JSHandle<JSTaggedValue> obj =
364         JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
365     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
366                                                                       ECMA_RUNTIME_CALL_INFO_4);
367     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
368     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
369 
370     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
371     [[maybe_unused]] JSTaggedValue result = TypedArray::ToReversed(ecmaRuntimeCallInfo1);
372     TestHelper::TearDownFrame(thread, prev);
373 
374     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread,
375                                                                       JSTaggedValue::Undefined(),
376                                                                       ECMA_RUNTIME_CALL_INFO_6);
377     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
378     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
379     ecmaRuntimeCallInfo2->SetCallArg(static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_0),
380                                      JSTaggedValue(INT_VALUE_0));
381     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
382     JSTaggedValue value = TypedArray::At(ecmaRuntimeCallInfo2);
383     TestHelper::TearDownFrame(thread, prev);
384     ASSERT_EQ(value, JSTaggedValue(INT_VALUE_0));
385 
386     auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread,
387                                                                       JSTaggedValue::Undefined(),
388                                                                       ECMA_RUNTIME_CALL_INFO_6);
389     ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
390     ecmaRuntimeCallInfo3->SetThis(obj.GetTaggedValue());
391     ecmaRuntimeCallInfo3->SetCallArg(static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_0),
392                                      JSTaggedValue(INT_VALUE_2));
393     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
394     value = TypedArray::At(ecmaRuntimeCallInfo3);
395     TestHelper::TearDownFrame(thread, prev);
396     ASSERT_EQ(value, JSTaggedValue(INT_VALUE_9));
397 
398     auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread,
399                                                                       JSTaggedValue::Undefined(),
400                                                                       ECMA_RUNTIME_CALL_INFO_6);
401     ecmaRuntimeCallInfo4->SetFunction(JSTaggedValue::Undefined());
402     ecmaRuntimeCallInfo4->SetThis(result);
403     ecmaRuntimeCallInfo4->SetCallArg(static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_0),
404                                      JSTaggedValue(INT_VALUE_0));
405     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4);
406     value = TypedArray::At(ecmaRuntimeCallInfo4);
407     TestHelper::TearDownFrame(thread, prev);
408     ASSERT_EQ(value, JSTaggedValue(INT_VALUE_9));
409     auto ecmaRuntimeCallInfo5 = TestHelper::CreateEcmaRuntimeCallInfo(thread,
410                                                                       JSTaggedValue::Undefined(),
411                                                                       ECMA_RUNTIME_CALL_INFO_6);
412     ecmaRuntimeCallInfo5->SetFunction(JSTaggedValue::Undefined());
413     ecmaRuntimeCallInfo5->SetThis(result);
414     ecmaRuntimeCallInfo5->SetCallArg(static_cast<uint32_t>(TypeArrayIndex::TYPED_ARRAY_INDEX_0),
415                                      JSTaggedValue(INT_VALUE_2));
416     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo5);
417     value = TypedArray::At(ecmaRuntimeCallInfo5);
418     TestHelper::TearDownFrame(thread, prev);
419     ASSERT_EQ(value, JSTaggedValue(INT_VALUE_0));
420 }
421 
HWTEST_F_L0(BuiltinsTypedArrayTest,ToSorted)422 HWTEST_F_L0(BuiltinsTypedArrayTest, ToSorted)
423 {
424     ASSERT_NE(thread, nullptr);
425     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
426     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
427     JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
428     // array [10, 8, 30]
429     array->Set(thread, 0, JSTaggedValue(10));
430     array->Set(thread, 1, JSTaggedValue(8));
431     array->Set(thread, 2, JSTaggedValue(30));
432 
433     JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
434     JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestToSortedFunc));
435     auto ecmaRuntimeCallInfo1 =
436         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call arg
437     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
438     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
439     ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue());
440 
441     [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
442     JSTaggedValue result1 = TypedArray::ToSorted(ecmaRuntimeCallInfo1);
443     TestHelper::TearDownFrame(thread, prev1);
444 
445     EXPECT_TRUE(result1.IsTypedArray());
446     JSHandle<JSTaggedValue> resultArr1 = JSHandle<JSTaggedValue>(thread, result1);
447     // [30, 10, 8]
448     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 0).GetValue()->GetInt(), 30);
449     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 1).GetValue()->GetInt(), 10);
450     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 2).GetValue()->GetInt(), 8);
451 
452     auto ecmaRuntimeCallInfo2 =
453         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); // 4 means 0 call arg
454     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
455     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
456 
457     [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
458     JSTaggedValue result2 = TypedArray::ToSorted(ecmaRuntimeCallInfo2);
459     TestHelper::TearDownFrame(thread, prev2);
460 
461     EXPECT_TRUE(result2.IsTypedArray());
462     JSHandle<JSTaggedValue> resultArr2 = JSHandle<JSTaggedValue>(thread, result2);
463     // [8, 10 ,30]
464     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 0).GetValue()->GetInt(), 8);
465     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 1).GetValue()->GetInt(), 10);
466     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 2).GetValue()->GetInt(), 30);
467 }
468 
HWTEST_F_L0(BuiltinsTypedArrayTest,With)469 HWTEST_F_L0(BuiltinsTypedArrayTest, With)
470 {
471     ASSERT_NE(thread, nullptr);
472     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
473     JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
474     // array [1, 2, 3]
475     array->Set(thread, 0, JSTaggedValue(1));
476     array->Set(thread, 1, JSTaggedValue(2));
477     array->Set(thread, 2, JSTaggedValue(3));
478 
479     JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
480     auto ecmaRuntimeCallInfo1 =
481         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); // 8 means 2 call args
482     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
483     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
484     ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(-1)));
485     ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(30))); // with(-1, 30)
486 
487     [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
488     JSTaggedValue result1 = TypedArray::With(ecmaRuntimeCallInfo1);
489     TestHelper::TearDownFrame(thread, prev1);
490 
491     EXPECT_TRUE(result1.IsTypedArray());
492     JSHandle<JSTaggedValue> resultArr1 = JSHandle<JSTaggedValue>(thread, result1);
493     // [1, 2, 30]
494     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 0).GetValue()->GetInt(), 1);
495     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 1).GetValue()->GetInt(), 2);
496     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr1, 2).GetValue()->GetInt(), 30);
497 
498     auto ecmaRuntimeCallInfo2 =
499         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); // 8 means 2 call args
500     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
501     ecmaRuntimeCallInfo2->SetThis(obj.GetTaggedValue());
502     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(1)));
503     ecmaRuntimeCallInfo2->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(-100))); // with(1, -100)
504 
505     [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
506     JSTaggedValue result2 = TypedArray::With(ecmaRuntimeCallInfo2);
507     TestHelper::TearDownFrame(thread, prev2);
508 
509     EXPECT_TRUE(result2.IsTypedArray());
510     JSHandle<JSTaggedValue> resultArr2 = JSHandle<JSTaggedValue>(thread, result2);
511     // [1, -100, 3]
512     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 0).GetValue()->GetInt(), 1);
513     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 1).GetValue()->GetInt(), -100);
514     EXPECT_EQ(JSTypedArray::GetProperty(thread, resultArr2, 2).GetValue()->GetInt(), 3);
515 }
516 
HWTEST_F_L0(BuiltinsTypedArrayTest,FindLast)517 HWTEST_F_L0(BuiltinsTypedArrayTest, FindLast)
518 {
519     ASSERT_NE(thread, nullptr);
520     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
521     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
522     JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
523     // array [50, 40, 2]
524     array->Set(thread, 0, JSTaggedValue(50));
525     array->Set(thread, 1, JSTaggedValue(40));
526     array->Set(thread, 2, JSTaggedValue(2));
527 
528     JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
529     JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestFindLastFunc));
530     auto ecmaRuntimeCallInfo1 =
531         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call arg
532     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
533     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
534     ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue());
535 
536     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
537     JSTaggedValue result = TypedArray::FindLast(ecmaRuntimeCallInfo1);
538     TestHelper::TearDownFrame(thread, prev);
539 
540     EXPECT_EQ(result.GetRawData(), JSTaggedValue(40).GetRawData());
541 }
542 
HWTEST_F_L0(BuiltinsTypedArrayTest,FindLastIndex)543 HWTEST_F_L0(BuiltinsTypedArrayTest, FindLastIndex)
544 {
545     ASSERT_NE(thread, nullptr);
546     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
547     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
548     JSHandle<TaggedArray> array(factory->NewTaggedArray(3));
549     // array [50, 40, 30]
550     array->Set(thread, 0, JSTaggedValue(50));
551     array->Set(thread, 1, JSTaggedValue(40));
552     array->Set(thread, 2, JSTaggedValue(30));
553 
554     JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, BuiltTestUtil::CreateTypedArray(thread, array));
555     JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestFindLastFunc));
556     auto ecmaRuntimeCallInfo1 =
557         TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call arg
558     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
559     ecmaRuntimeCallInfo1->SetThis(obj.GetTaggedValue());
560     ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue());
561 
562     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
563     JSTaggedValue result = TypedArray::FindLastIndex(ecmaRuntimeCallInfo1);
564     TestHelper::TearDownFrame(thread, prev);
565 
566     EXPECT_EQ(result.GetRawData(), JSTaggedValue(static_cast<double>(2)).GetRawData());
567 }
568 }  // namespace panda::test
569