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/base/builtins_base.h"
17#include "ecmascript/builtins/builtins.h"
18#include "ecmascript/ecma_vm.h"
19#include "ecmascript/global_env.h"
20#include "ecmascript/global_env_constants-inl.h"
21#include "ecmascript/js_handle.h"
22#include "ecmascript/js_object-inl.h"
23#include "ecmascript/js_thread.h"
24#include "ecmascript/tests/test_helper.h"
25
26using namespace panda::ecmascript;
27using namespace panda::ecmascript::base;
28
29namespace panda::test {
30class GlueRegsTest :  public BaseTestWithScope<false> {
31};
32
33HWTEST_F_L0(GlueRegsTest, ConstantClassTest)
34{
35    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
36    ASSERT_NE(globalConst, nullptr);
37
38    const JSTaggedValue *address = globalConst->BeginSlot();
39    size_t curIndex = static_cast<size_t>(ConstantIndex::CONSTANT_BEGIN);
40    size_t holeIndex = static_cast<size_t>(ConstantIndex::HOLE_INDEX);
41    while (address < globalConst->EndSlot()) {
42        if (curIndex != holeIndex) {
43            EXPECT_TRUE(!(*address).IsHole());  // Visit barely
44        }
45        address += 1;
46        curIndex += 1;
47    }
48}
49
50HWTEST_F_L0(GlueRegsTest, ConstantSpecialTest)
51{
52    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
53    ASSERT_NE(globalConst, nullptr);
54
55    EXPECT_TRUE(globalConst->GetUndefined().IsUndefined());
56    EXPECT_TRUE(globalConst->GetHandledUndefined()->IsUndefined());
57    EXPECT_TRUE(globalConst->GetNull().IsNull());
58    EXPECT_TRUE(globalConst->GetHandledNull()->IsNull());
59    EXPECT_TRUE(globalConst->GetEmptyString().IsString());
60    EXPECT_TRUE(globalConst->GetHandledEmptyString()->IsString());
61}
62
63HWTEST_F_L0(GlueRegsTest, ConstantConstantTest)
64{
65    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
66    ASSERT_NE(globalConst, nullptr);
67
68#define CONSTANT_CONSTANT_ITERATOR(Type, Name, Index, Desc)              \
69    Type Name##value = globalConst->Get##Name();                         \
70    EXPECT_TRUE(!Name##value.IsNull());                                  \
71    JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
72    EXPECT_TRUE(!Name##handledValue->IsNull());
73    GLOBAL_ENV_CONSTANT_CONSTANT(CONSTANT_CONSTANT_ITERATOR)
74#undef CONSTANT_CONSTANT_ITERATOR
75}
76
77HWTEST_F_L0(GlueRegsTest, ConstantStringTest)
78{
79    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
80    ASSERT_NE(globalConst, nullptr);
81
82#define CONSTANT_STRING_ITERATOR(Name, Index, Desc)                                     \
83    JSTaggedValue Name##value = globalConst->Get##Name();                               \
84    EXPECT_TRUE(Name##value.IsString());                                                \
85    Region *Name##region = Region::ObjectAddressToRange(Name##value.GetTaggedObject()); \
86    EXPECT_TRUE(Name##region->InSharedReadOnlySpace());
87    SHARED_GLOBAL_ENV_CONSTANT_STRING(CONSTANT_STRING_ITERATOR)
88#undef CONSTANT_STRING_ITERATOR
89}
90
91HWTEST_F_L0(GlueRegsTest, ConstantAccessorTest)
92{
93    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
94    ASSERT_NE(globalConst, nullptr);
95
96#define CONSTANT_ACCESSOR_ITERATOR(Type, Name, Index, Desc)              \
97    Type Name##value = globalConst->Get##Name();                         \
98    EXPECT_TRUE(!Name##value.IsNull());                                  \
99    JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
100    EXPECT_TRUE(!Name##handledValue->IsNull());
101    SHARED_GLOBAL_ENV_CONSTANT_ACCESSOR(CONSTANT_ACCESSOR_ITERATOR)
102#undef CONSTANT_ACCESSOR_ITERATOR
103}
104}  // namespace panda::test