• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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/builtin_entries.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/global_env_constants.h"
19 #include "ecmascript/global_index_map.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda::ecmascript;
23 
24 namespace panda::test {
25 class GlobalIndexMapTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {
29         GTEST_LOG_(INFO) << "SetUpTestCase";
30     }
31 
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         GTEST_LOG_(INFO) << "TearDownCase";
35     }
36 
SetUp()37     void SetUp() override
38     {
39         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(instance, scope);
45     }
46 
47     EcmaVM *instance {nullptr};
48     EcmaHandleScope *scope {nullptr};
49     JSThread *thread {nullptr};
50 };
51 
52 /**
53  * @tc.name: InitGlobalIndexMap
54  * @tc.desc: Check whether InitGlobalIndexMap can initialize dictionary.
55  * @tc.type: FUNC
56  * @tc.require:
57  */
HWTEST_F_L0(GlobalIndexMapTest,GlobalIndexMap_initGlobalIndexMap)58 HWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalIndexMap)
59 {
60     JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
61         GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
62     EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
63     GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
64     globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
65     EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
66 }
67 
68 /**
69  * @tc.name: InitGlobalConst
70  * @tc.desc: Check whether GlobalConst can be find in the GlobalMap.
71  * @tc.type: FUNC
72  * @tc.require:
73  */
HWTEST_F_L0(GlobalIndexMapTest,GlobalIndexMap_initGlobalConst)74 HWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalConst)
75 {
76     JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
77         GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
78     EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
79     GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
80     GlobalIndexMap::InitGlobalConst(thread, globalIndexMap);
81     globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
82     EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
83     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
84     uint32_t constantCount = globalConst->GetConstantCount();
85     for (uint32_t index = 0; index < constantCount; index++) {
86         JSTaggedValue objectValue = globalConst->GetGlobalConstantObject(index);
87         if (objectValue.IsHeapObject() && !objectValue.IsString()) {
88             GlobalIndex rootIndex;
89             GlobalIndexMap::FindGlobalIndex(globalIndexMap, objectValue, &rootIndex);
90             EXPECT_EQ(static_cast<int>(index), rootIndex.GetGlobalConstId());
91             GlobalIndex globalConstGlobalIndex;
92             globalConstGlobalIndex.UpdateGlobalConstId(index);
93             EXPECT_EQ(globalConstGlobalIndex, rootIndex);
94         }
95     }
96 }
97 
98 /**
99  * @tc.name: InitGlobalEnv
100  * @tc.desc: Check whether GlobalEnv can be find in the GlobalMap.
101  * @tc.type: FUNC
102  * @tc.require:
103  */
HWTEST_F_L0(GlobalIndexMapTest,GlobalIndexMap_initGlobalEnv)104 HWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalEnv)
105 {
106     JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
107         GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
108     EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
109     GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
110     GlobalIndexMap::InitGlobalEnv(thread, globalIndexMap);
111     globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
112     EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
113     auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
114     uint32_t globalEnvFieldSize = globalEnv->GetGlobalEnvFieldSize();
115     for (uint32_t index = 0; index < globalEnvFieldSize; index++) {
116         JSTaggedValue objectValue = globalEnv->GetGlobalEnvObjectByIndex(index).GetTaggedValue();
117         if (objectValue.IsHeapObject()) {
118             GlobalIndex rootIndex;
119             GlobalIndexMap::FindGlobalIndex(globalIndexMap, objectValue, &rootIndex);
120             EXPECT_EQ(static_cast<int>(index), rootIndex.GetGlobalEnvId());
121             GlobalIndex globalEnvGlobalIndex;
122             globalEnvGlobalIndex.UpdateGlobalEnvId(index);
123             EXPECT_EQ(globalEnvGlobalIndex, rootIndex);
124         }
125     }
126 }
127 } // namespace panda::test