14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/builtin_entries.h"
174514f5e3Sopenharmony_ci#include "ecmascript/ecma_context.h"
184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
194514f5e3Sopenharmony_ci#include "ecmascript/global_env_constants.h"
204514f5e3Sopenharmony_ci#include "ecmascript/global_index.h"
214514f5e3Sopenharmony_ci#include "ecmascript/global_index_map.h"
224514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::test {
274514f5e3Sopenharmony_ciclass GlobalIndexMapTest :  public BaseTestWithScope<false> {
284514f5e3Sopenharmony_ci};
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci/**
314514f5e3Sopenharmony_ci * @tc.name: InitGlobalIndexMap
324514f5e3Sopenharmony_ci * @tc.desc: Check whether InitGlobalIndexMap can initialize dictionary.
334514f5e3Sopenharmony_ci * @tc.type: FUNC
344514f5e3Sopenharmony_ci * @tc.require:
354514f5e3Sopenharmony_ci */
364514f5e3Sopenharmony_ciHWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalIndexMap)
374514f5e3Sopenharmony_ci{
384514f5e3Sopenharmony_ci    JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
394514f5e3Sopenharmony_ci        GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
404514f5e3Sopenharmony_ci    EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
414514f5e3Sopenharmony_ci    GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
424514f5e3Sopenharmony_ci    globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
434514f5e3Sopenharmony_ci    EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
444514f5e3Sopenharmony_ci}
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci/**
474514f5e3Sopenharmony_ci * @tc.name: InitGlobalConst
484514f5e3Sopenharmony_ci * @tc.desc: Check whether GlobalConst can be find in the GlobalMap.
494514f5e3Sopenharmony_ci * @tc.type: FUNC
504514f5e3Sopenharmony_ci * @tc.require:
514514f5e3Sopenharmony_ci */
524514f5e3Sopenharmony_ciHWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalConst)
534514f5e3Sopenharmony_ci{
544514f5e3Sopenharmony_ci    JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
554514f5e3Sopenharmony_ci        GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
564514f5e3Sopenharmony_ci    EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
574514f5e3Sopenharmony_ci    GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
584514f5e3Sopenharmony_ci    GlobalIndexMap::InitGlobalConst(thread, globalIndexMap);
594514f5e3Sopenharmony_ci    globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
604514f5e3Sopenharmony_ci    EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
614514f5e3Sopenharmony_ci    auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
624514f5e3Sopenharmony_ci    uint32_t constantCount = globalConst->GetConstantCount();
634514f5e3Sopenharmony_ci    for (uint32_t index = 0; index < constantCount; index++) {
644514f5e3Sopenharmony_ci        JSTaggedValue objectValue = globalConst->GetGlobalConstantObject(index);
654514f5e3Sopenharmony_ci        if (objectValue.IsHeapObject() && !objectValue.IsString()) {
664514f5e3Sopenharmony_ci            GlobalIndex rootIndex;
674514f5e3Sopenharmony_ci            GlobalIndexMap::FindGlobalIndex(globalIndexMap, objectValue, &rootIndex);
684514f5e3Sopenharmony_ci            EXPECT_EQ(static_cast<int>(index), rootIndex.GetGlobalConstId());
694514f5e3Sopenharmony_ci            GlobalIndex globalConstGlobalIndex;
704514f5e3Sopenharmony_ci            globalConstGlobalIndex.UpdateGlobalConstId(index);
714514f5e3Sopenharmony_ci            EXPECT_EQ(globalConstGlobalIndex, rootIndex);
724514f5e3Sopenharmony_ci        }
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci/**
774514f5e3Sopenharmony_ci * @tc.name: InitGlobalEnv
784514f5e3Sopenharmony_ci * @tc.desc: Check whether GlobalEnv can be find in the GlobalMap.
794514f5e3Sopenharmony_ci * @tc.type: FUNC
804514f5e3Sopenharmony_ci * @tc.require:
814514f5e3Sopenharmony_ci */
824514f5e3Sopenharmony_ciHWTEST_F_L0(GlobalIndexMapTest, GlobalIndexMap_initGlobalEnv)
834514f5e3Sopenharmony_ci{
844514f5e3Sopenharmony_ci    JSMutableHandle<PointerToIndexDictionary> globalIndexMap(
854514f5e3Sopenharmony_ci        GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()).GetAddress());
864514f5e3Sopenharmony_ci    EXPECT_NE(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
874514f5e3Sopenharmony_ci    GlobalIndexMap::InitGlobalIndexMap(thread, globalIndexMap);
884514f5e3Sopenharmony_ci    GlobalIndexMap::InitGlobalEnv(thread, globalIndexMap);
894514f5e3Sopenharmony_ci    globalIndexMap.Update(GlobalIndexMap::GetGlobalIndexMap(thread->GetCurrentEcmaContext()));
904514f5e3Sopenharmony_ci    EXPECT_EQ(globalIndexMap.GetTaggedValue().IsHeapObject(), true);
914514f5e3Sopenharmony_ci    auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
924514f5e3Sopenharmony_ci    uint32_t globalEnvFieldSize = globalEnv->GetGlobalEnvFieldSize();
934514f5e3Sopenharmony_ci    for (uint32_t index = 0; index < globalEnvFieldSize; index++) {
944514f5e3Sopenharmony_ci        JSTaggedValue objectValue = globalEnv->GetGlobalEnvObjectByIndex(index).GetTaggedValue();
954514f5e3Sopenharmony_ci        if (objectValue.IsHeapObject()) {
964514f5e3Sopenharmony_ci            GlobalIndex rootIndex;
974514f5e3Sopenharmony_ci            GlobalIndexMap::FindGlobalIndex(globalIndexMap, objectValue, &rootIndex);
984514f5e3Sopenharmony_ci            EXPECT_EQ(static_cast<int>(index), rootIndex.GetGlobalEnvId());
994514f5e3Sopenharmony_ci            if (index != rootIndex.GetGlobalEnvId()) {
1004514f5e3Sopenharmony_ci                objectValue.D();
1014514f5e3Sopenharmony_ci            }
1024514f5e3Sopenharmony_ci            GlobalIndex globalEnvGlobalIndex;
1034514f5e3Sopenharmony_ci            globalEnvGlobalIndex.UpdateGlobalEnvId(index);
1044514f5e3Sopenharmony_ci            EXPECT_EQ(globalEnvGlobalIndex, rootIndex);
1054514f5e3Sopenharmony_ci        }
1064514f5e3Sopenharmony_ci    }
1074514f5e3Sopenharmony_ci}
1084514f5e3Sopenharmony_ci} // namespace panda::test