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