• 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/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 
26 using namespace panda::ecmascript;
27 using namespace panda::ecmascript::base;
28 
29 namespace panda::test {
30 class GlueRegsTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         GTEST_LOG_(INFO) << "SetUpTestCase";
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         GTEST_LOG_(INFO) << "TearDownCase";
40     }
41 
SetUp()42     void SetUp() override
43     {
44         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
45     }
46 
TearDown()47     void TearDown() override
48     {
49         TestHelper::DestroyEcmaVMWithScope(instance, scope);
50     }
51 
52     EcmaVM *instance {nullptr};
53     ecmascript::EcmaHandleScope *scope {nullptr};
54     JSThread *thread {nullptr};
55 };
56 
HWTEST_F_L0(GlueRegsTest,ConstantClassTest)57 HWTEST_F_L0(GlueRegsTest, ConstantClassTest)
58 {
59     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
60     ASSERT_NE(globalConst, nullptr);
61 
62     const JSTaggedValue *address = globalConst->BeginSlot();
63     while (address < globalConst->EndSlot()) {
64         EXPECT_TRUE(!(*address).IsHole());  // Visit barely
65         address += 1;
66     }
67 }
68 
HWTEST_F_L0(GlueRegsTest,ConstantSpecialTest)69 HWTEST_F_L0(GlueRegsTest, ConstantSpecialTest)
70 {
71     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
72     ASSERT_NE(globalConst, nullptr);
73 
74     EXPECT_TRUE(globalConst->GetUndefined().IsUndefined());
75     EXPECT_TRUE(globalConst->GetHandledUndefined()->IsUndefined());
76     EXPECT_TRUE(globalConst->GetNull().IsNull());
77     EXPECT_TRUE(globalConst->GetHandledNull()->IsNull());
78     EXPECT_TRUE(globalConst->GetEmptyString().IsString());
79     EXPECT_TRUE(globalConst->GetHandledEmptyString()->IsString());
80 }
81 
HWTEST_F_L0(GlueRegsTest,ConstantStringTest)82 HWTEST_F_L0(GlueRegsTest, ConstantStringTest)
83 {
84     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
85     ASSERT_NE(globalConst, nullptr);
86 
87 #define CONSTANT_STRING_ITERATOR(Type, Name, Index, Desc)                \
88     Type Name##value = globalConst->Get##Name();                         \
89     EXPECT_TRUE(!Name##value.IsNull());                                  \
90     JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
91     EXPECT_TRUE(!Name##handledValue->IsNull());
92     GLOBAL_ENV_CONSTANT_CONSTANT(CONSTANT_STRING_ITERATOR)
93 #undef CONSTANT_STRING_ITERATOR
94 }
95 
HWTEST_F_L0(GlueRegsTest,ConstantAccessorTest)96 HWTEST_F_L0(GlueRegsTest, ConstantAccessorTest)
97 {
98     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
99     ASSERT_NE(globalConst, nullptr);
100 
101 #define CONSTANT_ACCESSOR_ITERATOR(Type, Name, Index, Desc)              \
102     Type Name##value = globalConst->Get##Name();                         \
103     EXPECT_TRUE(!Name##value.IsNull());                                  \
104     JSHandle<Type> Name##handledValue = globalConst->GetHandled##Name(); \
105     EXPECT_TRUE(!Name##handledValue->IsNull());
106     GLOBAL_ENV_CONSTANT_ACCESSOR(CONSTANT_ACCESSOR_ITERATOR)
107 #undef CONSTANT_ACCESSOR_ITERATOR
108 }
109 }  // namespace panda::test