• 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/ecma_string.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/js_array_iterator.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_hclass.h"
23 #include "ecmascript/js_iterator.h"
24 #include "ecmascript/js_object-inl.h"
25 #include "ecmascript/js_tagged_value-inl.h"
26 #include "ecmascript/object_factory.h"
27 #include "ecmascript/tests/test_helper.h"
28 
29 using namespace panda::ecmascript;
30 
31 namespace panda::test {
32 class JSArrayTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase()
35     {
36         GTEST_LOG_(INFO) << "SetUpTestCase";
37     }
38 
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         GTEST_LOG_(INFO) << "TearDownCase";
42     }
43 
SetUp()44     void SetUp() override
45     {
46         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
47     }
48 
TearDown()49     void TearDown() override
50     {
51         TestHelper::DestroyEcmaVMWithScope(instance, scope);
52     }
53     EcmaVM *instance {nullptr};
54     EcmaHandleScope *scope {nullptr};
55     JSThread *thread {nullptr};
56 };
57 
HWTEST_F_L0(JSArrayTest,ArrayCreate)58 HWTEST_F_L0(JSArrayTest, ArrayCreate)
59 {
60     JSHandle<JSTaggedValue> lengthKeyHandle(thread->GlobalConstants()->GetHandledLengthString());
61     JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
62     EXPECT_TRUE(arr != nullptr);
63     JSHandle<JSTaggedValue> obj(thread, arr);
64     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 0);
65 
66     JSArray *arr2 = JSArray::ArrayCreate(thread, JSTaggedNumber(10)).GetObject<JSArray>();
67     EXPECT_TRUE(arr2 != nullptr);
68     JSHandle<JSTaggedValue> obj2(thread, arr2);
69     EXPECT_EQ(JSArray::GetProperty(thread, obj2, lengthKeyHandle).GetValue()->GetInt(), 10);
70 }
71 
HWTEST_F_L0(JSArrayTest,ArraySpeciesCreate)72 HWTEST_F_L0(JSArrayTest, ArraySpeciesCreate)
73 {
74     JSHandle<JSTaggedValue> lengthKeyHandle(thread->GlobalConstants()->GetHandledLengthString());
75     JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
76     EXPECT_TRUE(arr != nullptr);
77     JSHandle<JSObject> obj(thread, arr);
78     EXPECT_EQ(JSArray::GetProperty(thread, JSHandle<JSTaggedValue>(obj), lengthKeyHandle).GetValue()->GetInt(), 0);
79 
80     JSArray *arr2 = JSArray::Cast(JSArray::ArraySpeciesCreate(thread, obj, JSTaggedNumber(10)).GetTaggedObject());
81     EXPECT_TRUE(arr2 != nullptr);
82     JSHandle<JSTaggedValue> obj2(thread, arr2);
83     EXPECT_EQ(JSArray::GetProperty(thread, obj2, lengthKeyHandle).GetValue()->GetInt(), 10);
84 }
85 
HWTEST_F_L0(JSArrayTest,DefineOwnProperty)86 HWTEST_F_L0(JSArrayTest, DefineOwnProperty)
87 {
88     auto ecmaVM = thread->GetEcmaVM();
89     auto factory = ecmaVM->GetFactory();
90     JSHandle<JSTaggedValue> lengthKeyHandle(thread->GlobalConstants()->GetHandledLengthString());
91     JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
92     EXPECT_TRUE(arr != nullptr);
93     JSHandle<JSTaggedValue> obj(thread, arr);
94     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 0);
95 
96     PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(100)), true, true, true);
97 
98     EcmaString *string1 = *factory->NewFromASCII("1");
99     JSHandle<JSTaggedValue> key1(thread, static_cast<TaggedObject *>(string1));
100     JSHandle<JSTaggedValue> index1(thread, JSTaggedValue(1));
101     EXPECT_TRUE(JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key1, desc));
102     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 2);
103     JSTaggedValue v = JSArray::GetProperty(thread, obj, key1).GetValue().GetTaggedValue();
104     EXPECT_EQ(v.GetInt(), 100);
105     v = JSArray::GetProperty(thread, obj, index1).GetValue().GetTaggedValue();
106     EXPECT_EQ(v.GetInt(), 100);
107     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 2);
108 
109     EcmaString *string100 = *factory->NewFromASCII("100");
110     JSHandle<JSTaggedValue> key100(thread, static_cast<TaggedObject *>(string100));
111     JSHandle<JSTaggedValue> index100(thread, JSTaggedValue(100));
112 
113     EXPECT_TRUE(JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key100, desc));
114     EXPECT_EQ(JSArray::GetProperty(thread, obj, key100).GetValue()->GetInt(), 100);
115     EXPECT_EQ(JSArray::GetProperty(thread, obj, index100).GetValue()->GetInt(), 100);
116     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 101);
117 
118     EcmaString *stringx = *factory->NewFromASCII("2147483646");
119     JSHandle<JSTaggedValue> keyx(thread, static_cast<TaggedObject *>(stringx));
120     JSHandle<JSTaggedValue> indexx(thread, JSTaggedValue(2147483646U));  // 2147483646U
121 
122     EXPECT_TRUE(JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), keyx, desc));
123     EXPECT_EQ(JSArray::GetProperty(thread, obj, keyx).GetValue()->GetInt(), 100);
124     EXPECT_EQ(JSArray::GetProperty(thread, obj, indexx).GetValue()->GetInt(), 100);
125     EXPECT_EQ(JSArray::GetProperty(thread, obj, lengthKeyHandle).GetValue()->GetInt(), 2147483647);
126 
127     EXPECT_TRUE(JSArray::DeleteProperty(thread, JSHandle<JSObject>(obj), indexx));
128     EXPECT_TRUE(JSArray::GetProperty(thread, obj, keyx).GetValue()->IsUndefined());
129     EXPECT_TRUE(JSArray::GetProperty(thread, obj, indexx).GetValue()->IsUndefined());
130 }
131 
HWTEST_F_L0(JSArrayTest,Next)132 HWTEST_F_L0(JSArrayTest, Next)
133 {
134     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
135     JSHandle<TaggedArray> values(factory->NewTaggedArray(5));
136     for (int i = 0; i < 5; i++) {
137         values->Set(thread, i, JSTaggedValue(i));
138     }
139     JSHandle<JSObject> array(JSArray::CreateArrayFromList(thread, values));
140     JSHandle<JSTaggedValue> iter(factory->NewJSArrayIterator(array, IterationKind::KEY));
141     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
142     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
143     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
144     for (int i = 0; i < 5; i++) {
145         ecmaRuntimeCallInfo->SetThis(iter.GetTaggedValue());
146         JSTaggedValue ret = JSArrayIterator::Next(ecmaRuntimeCallInfo);
147         JSHandle<JSTaggedValue> result(thread, ret);
148         EXPECT_EQ(JSIterator::IteratorValue(thread, result)->GetInt(), i);
149     }
150     TestHelper::TearDownFrame(thread, prev);
151 }
152 
HWTEST_F_L0(JSArrayTest,Iterator)153 HWTEST_F_L0(JSArrayTest, Iterator)
154 {
155     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
156     JSHandle<TaggedArray> values(factory->NewTaggedArray(5));
157     for (int i = 0; i < 5; i++) {
158         values->Set(thread, i, JSTaggedValue(i));
159     }
160     JSHandle<JSObject> array(JSArray::CreateArrayFromList(thread, values));
161     JSHandle<JSTaggedValue> key_iter(factory->NewJSArrayIterator(array, IterationKind::KEY));
162     JSHandle<JSTaggedValue> value_iter(factory->NewJSArrayIterator(array, IterationKind::VALUE));
163     JSHandle<JSTaggedValue> iter(factory->NewJSArrayIterator(array, IterationKind::KEY_AND_VALUE));
164 
165     for (int i = 0; i < 5; i++) {
166         if (i == 2) {
167             JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
168             JSObject::DeleteProperty(thread, JSHandle<JSObject>(array), key);
169         }
170         JSHandle<JSTaggedValue> key_result(JSIterator::IteratorStep(thread, key_iter));
171         JSHandle<JSTaggedValue> value_result(JSIterator::IteratorStep(thread, value_iter));
172         JSHandle<JSTaggedValue> iter_result(JSIterator::IteratorStep(thread, iter));
173         JSHandle<JSTaggedValue> iter_value(JSIterator::IteratorValue(thread, iter_result));
174         JSHandle<JSTaggedValue> index_key(thread, JSTaggedValue(0));
175         JSHandle<JSTaggedValue> element_key(thread, JSTaggedValue(1));
176         if (i == 2) {
177             EXPECT_EQ(static_cast<int>(i), JSIterator::IteratorValue(thread, key_result)->GetInt());
178             EXPECT_EQ(JSTaggedValue::Undefined(), JSIterator::IteratorValue(thread, value_result).GetTaggedValue());
179             EXPECT_EQ(static_cast<int>(i), JSObject::GetProperty(thread, iter_value, index_key).GetValue()->GetInt());
180             EXPECT_EQ(JSTaggedValue::Undefined(),
181                       JSObject::GetProperty(thread, iter_value, element_key).GetValue().GetTaggedValue());
182             continue;
183         }
184         EXPECT_EQ(static_cast<int>(i), JSIterator::IteratorValue(thread, key_result)->GetInt());
185         EXPECT_EQ(static_cast<int>(i), JSIterator::IteratorValue(thread, value_result)->GetInt());
186         EXPECT_EQ(static_cast<int>(i), JSObject::GetProperty(thread, iter_value, index_key).GetValue()->GetInt());
187         EXPECT_EQ(static_cast<int>(i), JSObject::GetProperty(thread, iter_value, element_key).GetValue()->GetInt());
188     }
189 }
190 
HWTEST_F_L0(JSArrayTest,COWArray)191 HWTEST_F_L0(JSArrayTest, COWArray)
192 {
193     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
194     JSHandle<TaggedArray> values(factory->NewTaggedArray(5));
195 
196     for (int i = 0; i < 5; i++) {
197         values->Set(thread, i, JSTaggedValue(i));
198     }
199     JSHandle<JSArray> array(JSArray::CreateArrayFromList(thread, values));
200     JSHandle<JSArray> cloneArray = factory->CloneArrayLiteral(array);
201 
202     for (int i = 0; i < 5; i++) {
203         JSTaggedValue value1 = TaggedArray::Cast(cloneArray->GetElements().GetTaggedObject())->Get(i);
204         JSTaggedValue value2 = TaggedArray::Cast(array->GetElements().GetTaggedObject())->Get(i);
205         EXPECT_EQ(value1.GetRawData(), value2.GetRawData());
206     }
207 
208 #if defined ENABLE_COW_ARRAY
209     // Elements array is shared to use the same array.
210     EXPECT_EQ(cloneArray->GetElements().GetTaggedObject(), array->GetElements().GetTaggedObject());
211 #endif
212     JSHandle<JSTaggedValue> lengthKeyHandle(thread->GlobalConstants()->GetHandledLengthString());
213 
214     // Change the value and the elements will not be shared.
215     JSArray::FastSetPropertyByValue(thread, JSHandle<JSTaggedValue>(cloneArray), 0, lengthKeyHandle);
216 
217     EXPECT_TRUE(TaggedArray::Cast(cloneArray->GetElements().GetTaggedObject())->Get(0) !=
218         TaggedArray::Cast(array->GetElements().GetTaggedObject())->Get(0));
219 
220 #if defined ENABLE_COW_ARRAY
221     EXPECT_TRUE(cloneArray->GetElements().GetTaggedObject() != array->GetElements().GetTaggedObject());
222 #endif
223 
224     for (int i = 1; i < 5; i++) {
225         JSTaggedValue value1 = TaggedArray::Cast(cloneArray->GetElements().GetTaggedObject())->Get(i);
226         JSTaggedValue value2 = TaggedArray::Cast(array->GetElements().GetTaggedObject())->Get(i);
227         EXPECT_EQ(value1.GetRawData(), value2.GetRawData());
228     }
229 }
230 }  // namespace panda::test
231