• 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/base/builtins_base.h"
19 #include "ecmascript/ecma_runtime_call_info.h"
20 #include "ecmascript/ecma_string.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_array.h"
24 #include "ecmascript/js_handle.h"
25 #include "ecmascript/js_hclass.h"
26 #include "ecmascript/js_map.h"
27 #include "ecmascript/js_map_iterator.h"
28 #include "ecmascript/js_object-inl.h"
29 #include "ecmascript/js_tagged_value.h"
30 #include "ecmascript/js_thread.h"
31 #include "ecmascript/object_factory.h"
32 #include "ecmascript/tagged_array-inl.h"
33 #include "ecmascript/tests/test_helper.h"
34 
35 using namespace panda::ecmascript;
36 using namespace panda::ecmascript::builtins;
37 
38 namespace panda::test {
39 using BuiltinsMap = ecmascript::builtins::BuiltinsMap;
40 using JSMap = ecmascript::JSMap;
41 
42 class BuiltinsMapTest : public testing::Test {
43 public:
SetUpTestCase()44     static void SetUpTestCase()
45     {
46         GTEST_LOG_(INFO) << "SetUpTestCase";
47     }
48 
TearDownTestCase()49     static void TearDownTestCase()
50     {
51         GTEST_LOG_(INFO) << "TearDownCase";
52     }
53 
SetUp()54     void SetUp() override
55     {
56         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
57     }
58 
TearDown()59     void TearDown() override
60     {
61         TestHelper::DestroyEcmaVMWithScope(instance, scope);
62     }
63 
64     EcmaVM *instance {nullptr};
65     EcmaHandleScope *scope {nullptr};
66     JSThread *thread {nullptr};
67 
68     class TestClass : public base::BuiltinsBase {
69     public:
TestFunc(EcmaRuntimeCallInfo * argv)70         static JSTaggedValue TestFunc(EcmaRuntimeCallInfo *argv)
71         {
72             int num = GetCallArg(argv, 0)->GetInt();
73             JSArray *jsArray = JSArray::Cast(GetThis(argv)->GetTaggedObject());
74             int length = jsArray->GetArrayLength() + num;
75             jsArray->SetArrayLength(argv->GetThread(), length);
76             return JSTaggedValue::Undefined();
77         }
78     };
79 };
80 
CreateBuiltinsMap(JSThread * thread)81 JSMap *CreateBuiltinsMap(JSThread *thread)
82 {
83     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
84     JSHandle<JSFunction> newTarget(env->GetBuiltinsMapFunction());
85     // 4 : test case
86     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 4);
87     ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
88     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
89 
90     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
91     JSTaggedValue result = BuiltinsMap::MapConstructor(ecmaRuntimeCallInfo);
92     TestHelper::TearDownFrame(thread, prev);
93 
94     EXPECT_TRUE(result.IsECMAObject());
95     JSMap *jsMap = JSMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()));
96     return jsMap;
97 }
98 // new Map("abrupt").toString()
HWTEST_F_L0(BuiltinsMapTest,CreateAndGetSize)99 HWTEST_F_L0(BuiltinsMapTest, CreateAndGetSize)
100 {
101     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
102     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
103     JSHandle<JSFunction> newTarget(env->GetBuiltinsMapFunction());
104     JSHandle<JSMap> map(thread, CreateBuiltinsMap(thread));
105 
106     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
107     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
108     ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue());
109     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
110 
111     {
112         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
113         JSTaggedValue result = BuiltinsMap::GetSize(ecmaRuntimeCallInfo);
114         TestHelper::TearDownFrame(thread, prev);
115 
116         EXPECT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData());
117     }
118     JSHandle<TaggedArray> array(factory->NewTaggedArray(5));
119     for (int i = 0; i < 5; i++) {
120         JSHandle<TaggedArray> internalArray(factory->NewTaggedArray(2));
121         internalArray->Set(thread, 0, JSTaggedValue(i));
122         internalArray->Set(thread, 1, JSTaggedValue(i));
123         auto arr = JSArray::CreateArrayFromList(thread, internalArray);
124         array->Set(thread, i, arr);
125     }
126     JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array);
127     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
128     ecmaRuntimeCallInfo1->SetFunction(newTarget.GetTaggedValue());
129     ecmaRuntimeCallInfo1->SetThis(map.GetTaggedValue());
130     ecmaRuntimeCallInfo1->SetCallArg(0, values.GetTaggedValue());
131     ecmaRuntimeCallInfo1->SetNewTarget(newTarget.GetTaggedValue());
132     {
133         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
134         JSTaggedValue result1 = BuiltinsMap::MapConstructor(ecmaRuntimeCallInfo1);
135         TestHelper::TearDownFrame(thread, prev);
136 
137         EXPECT_EQ(JSMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))->GetSize(), 5);
138     }
139 }
140 
HWTEST_F_L0(BuiltinsMapTest,SetAndHas)141 HWTEST_F_L0(BuiltinsMapTest, SetAndHas)
142 {
143     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
144     // create jsMap
145     JSHandle<JSMap> map(thread, CreateBuiltinsMap(thread));
146     JSHandle<JSTaggedValue> key(factory->NewFromASCII("key"));
147 
148     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
149     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
150     ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue());
151     ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
152     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1)));
153 
154     JSMap *jsMap;
155     {
156         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
157         JSTaggedValue result1 = BuiltinsMap::Has(ecmaRuntimeCallInfo);
158 
159         EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData());
160 
161         // test Set()
162         JSTaggedValue result2 = BuiltinsMap::Set(ecmaRuntimeCallInfo);
163 
164         EXPECT_TRUE(result2.IsECMAObject());
165         jsMap = JSMap::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData()));
166         EXPECT_EQ(jsMap->GetSize(), 1);
167     }
168 
169     // test Has()
170     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
171     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
172     ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(jsMap));
173     ecmaRuntimeCallInfo1->SetCallArg(0, key.GetTaggedValue());
174     ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1)));
175     {
176         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
177         JSTaggedValue result3 = BuiltinsMap::Has(ecmaRuntimeCallInfo1);
178 
179         EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
180     }
181 }
182 
HWTEST_F_L0(BuiltinsMapTest,ForEach)183 HWTEST_F_L0(BuiltinsMapTest, ForEach)
184 {
185     // generate a map has 5 entries{key1:0,key2:1,key3:2,key4:3,key5:4}
186     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
187     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
188     JSHandle<JSMap> map(thread, CreateBuiltinsMap(thread));
189     char keyArray[] = "key0";
190     for (uint32_t i = 0; i < 5; i++) {
191         keyArray[3] = '1' + i;
192         JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray));
193         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
194         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
195         ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue());
196         ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
197         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(i)));
198 
199         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
200         JSTaggedValue result1 = BuiltinsMap::Set(ecmaRuntimeCallInfo);
201         TestHelper::TearDownFrame(thread, prev);
202         EXPECT_TRUE(result1.IsECMAObject());
203         JSMap *jsMap = JSMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()));
204         EXPECT_EQ(jsMap->GetSize(), static_cast<int>(i) + 1);
205     }
206     // test foreach;
207     JSHandle<JSArray> jsArray(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
208     JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestFunc));
209 
210     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
211     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
212     ecmaRuntimeCallInfo1->SetThis(map.GetTaggedValue());
213     ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue());
214     ecmaRuntimeCallInfo1->SetCallArg(1, jsArray.GetTaggedValue());
215 
216     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
217     JSTaggedValue result2 = BuiltinsMap::ForEach(ecmaRuntimeCallInfo1);
218     TestHelper::TearDownFrame(thread, prev);
219 
220     EXPECT_EQ(result2.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
221     EXPECT_EQ(jsArray->GetArrayLength(), 10U);
222 }
223 
HWTEST_F_L0(BuiltinsMapTest,DeleteAndRemove)224 HWTEST_F_L0(BuiltinsMapTest, DeleteAndRemove)
225 {
226     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
227     // create jsMap
228     JSHandle<JSMap> map(thread, CreateBuiltinsMap(thread));
229 
230     // add 40 keys
231     char keyArray[] = "key0";
232     for (uint32_t i = 0; i < 40; i++) {
233         keyArray[3] = '1' + i;
234         JSHandle<JSTaggedValue> key(thread, factory->NewFromASCII(keyArray).GetTaggedValue());
235         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
236         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
237         ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue());
238         ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
239         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(i)));
240 
241         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
242         JSTaggedValue result1 = BuiltinsMap::Set(ecmaRuntimeCallInfo);
243         TestHelper::TearDownFrame(thread, prev);
244 
245         EXPECT_TRUE(result1.IsECMAObject());
246         JSMap *jsMap = JSMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()));
247         EXPECT_EQ(jsMap->GetSize(), static_cast<int>(i) + 1);
248     }
249     // whether jsMap has delete key
250     keyArray[3] = '1' + 8;
251     JSHandle<JSTaggedValue> deleteKey(factory->NewFromASCII(keyArray));
252 
253     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
254     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
255     ecmaRuntimeCallInfo1->SetThis(map.GetTaggedValue());
256     ecmaRuntimeCallInfo1->SetCallArg(0, deleteKey.GetTaggedValue());
257 
258     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
259     JSTaggedValue result2 = BuiltinsMap::Has(ecmaRuntimeCallInfo1);
260     TestHelper::TearDownFrame(thread, prev);
261 
262     EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData());
263 
264     // delete
265     [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
266     JSTaggedValue result3 = BuiltinsMap::Delete(ecmaRuntimeCallInfo1);
267     TestHelper::TearDownFrame(thread, prev1);
268     EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
269 
270     // check deleteKey is deleted
271     [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
272     JSTaggedValue result4 = BuiltinsMap::Has(ecmaRuntimeCallInfo1);
273     TestHelper::TearDownFrame(thread, prev2);
274     EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData());
275     [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
276     JSTaggedValue result5 = BuiltinsMap::GetSize(ecmaRuntimeCallInfo1);
277     TestHelper::TearDownFrame(thread, prev3);
278     EXPECT_EQ(result5.GetRawData(), JSTaggedValue(39).GetRawData());
279 
280     // clear
281     [[maybe_unused]] auto prev4 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
282     JSTaggedValue result6 = BuiltinsMap::Clear(ecmaRuntimeCallInfo1);
283     TestHelper::TearDownFrame(thread, prev4);
284     EXPECT_EQ(result6.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
285     EXPECT_EQ(map->GetSize(), 0);
286 }
287 
HWTEST_F_L0(BuiltinsMapTest,Species)288 HWTEST_F_L0(BuiltinsMapTest, Species)
289 {
290     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
291     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
292     JSHandle<JSTaggedValue> map(thread, CreateBuiltinsMap(thread));
293 
294     // test species
295     JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
296     EXPECT_TRUE(!speciesSymbol.GetTaggedValue().IsUndefined());
297 
298     JSHandle<JSFunction> newTarget(env->GetBuiltinsMapFunction());
299 
300     JSTaggedValue value =
301         JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(newTarget), speciesSymbol).GetValue().GetTaggedValue();
302     JSHandle<JSTaggedValue> valueHandle(thread, value);
303     EXPECT_EQ(value, newTarget.GetTaggedValue());
304 
305     // to string tag
306     JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol();
307     JSHandle<EcmaString> stringTag(JSObject::GetProperty(thread, map, toStringTagSymbol).GetValue());
308     JSHandle<EcmaString> str = factory->NewFromASCII("Map");
309     EXPECT_TRUE(!stringTag.GetTaggedValue().IsUndefined());
310     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*str, *stringTag));
311 
312     JSHandle<JSFunction> constructor = JSHandle<JSFunction>::Cast(JSTaggedValue::ToObject(thread, valueHandle));
313     EXPECT_EQ(JSTaggedValue::GetPrototype(thread, map), constructor->GetFunctionPrototype());
314 
315     JSHandle<JSTaggedValue> key1(factory->NewFromASCII("set"));
316     JSTaggedValue value1 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
317     EXPECT_FALSE(value1.IsUndefined());
318 
319     JSHandle<JSTaggedValue> key2(factory->NewFromASCII("has"));
320     JSTaggedValue value2 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
321     EXPECT_FALSE(value2.IsUndefined());
322 
323     JSHandle<JSTaggedValue> key3(factory->NewFromASCII("clear"));
324     JSTaggedValue value3 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
325     EXPECT_FALSE(value3.IsUndefined());
326 
327     JSHandle<JSTaggedValue> key4(factory->NewFromASCII("size"));
328     JSTaggedValue value4 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
329     EXPECT_FALSE(value4.IsUndefined());
330 
331     JSHandle<JSTaggedValue> key5(factory->NewFromASCII("delete"));
332     JSTaggedValue value5 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
333     EXPECT_FALSE(value5.IsUndefined());
334 
335     JSHandle<JSTaggedValue> key6(factory->NewFromASCII("forEach"));
336     JSTaggedValue value6 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
337     EXPECT_FALSE(value6.IsUndefined());
338 
339     JSHandle<JSTaggedValue> key7(factory->NewFromASCII("get"));
340     JSTaggedValue value7 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue();
341     EXPECT_FALSE(value7.IsUndefined());
342 }
343 
HWTEST_F_L0(BuiltinsMapTest,GetIterator)344 HWTEST_F_L0(BuiltinsMapTest, GetIterator)
345 {
346     JSHandle<JSTaggedValue> map(thread, CreateBuiltinsMap(thread));
347     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
348     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
349     ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue());
350     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
351 
352     // test Values()
353     JSTaggedValue result = BuiltinsMap::Values(ecmaRuntimeCallInfo);
354     JSHandle<JSMapIterator> iter(thread, result);
355     EXPECT_TRUE(iter->IsJSMapIterator());
356     EXPECT_EQ(IterationKind::VALUE, iter->GetIterationKind());
357     EXPECT_EQ(JSMap::Cast(map.GetTaggedValue().GetTaggedObject())->GetLinkedMap(), iter->GetIteratedMap());
358 
359     // test Keys()
360     JSTaggedValue result1 = BuiltinsMap::Keys(ecmaRuntimeCallInfo);
361     JSHandle<JSMapIterator> iter1(thread, result1);
362     EXPECT_TRUE(iter1->IsJSMapIterator());
363     EXPECT_EQ(IterationKind::KEY, iter1->GetIterationKind());
364 
365     // test entries()
366     JSTaggedValue result2 = BuiltinsMap::Entries(ecmaRuntimeCallInfo);
367     JSHandle<JSMapIterator> iter2(thread, result2);
368     EXPECT_TRUE(iter2->IsJSMapIterator());
369     EXPECT_EQ(IterationKind::KEY_AND_VALUE, iter2->GetIterationKind());
370     TestHelper::TearDownFrame(thread, prev);
371 }
372 }  // namespace panda::test
373