1 /**
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include "ets_coroutine.h"
19 #include "ets_vm.h"
20
21 #include "plugins/ets/runtime/types/ets_typeapi_field.h"
22 #include "plugins/ets/runtime/types/ets_typeapi_method.h"
23 #include "plugins/ets/runtime/types/ets_typeapi_parameter.h"
24 #include "plugins/ets/runtime/types/ets_typeapi_type.h"
25 #include "tests/runtime/types/ets_test_mirror_classes.h"
26
27 namespace ark::ets::test {
28
29 class EtsTypeAPITest : public testing::Test {
30 public:
EtsTypeAPITest()31 EtsTypeAPITest()
32 {
33 options_.SetShouldLoadBootPandaFiles(true);
34 options_.SetShouldInitializeIntrinsics(false);
35 options_.SetCompilerEnableJit(false);
36 options_.SetGcType("g1-gc");
37 options_.SetLoadRuntimes({"ets"});
38
39 auto stdlib = std::getenv("PANDA_STD_LIB");
40 if (stdlib == nullptr) {
41 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
42 std::abort();
43 }
44 options_.SetBootPandaFiles({stdlib});
45
46 Runtime::Create(options_);
47 }
48
~EtsTypeAPITest()49 ~EtsTypeAPITest() override
50 {
51 Runtime::Destroy();
52 }
53
54 NO_COPY_SEMANTIC(EtsTypeAPITest);
55 NO_MOVE_SEMANTIC(EtsTypeAPITest);
56
SetUp()57 void SetUp() override
58 {
59 coroutine_ = EtsCoroutine::GetCurrent();
60 vm_ = coroutine_->GetPandaVM();
61 coroutine_->ManagedCodeBegin();
62 }
63
TearDown()64 void TearDown() override
65 {
66 coroutine_->ManagedCodeEnd();
67 }
68
GetTypeAPITypeClassMembers()69 static std::vector<MirrorFieldInfo> GetTypeAPITypeClassMembers()
70 {
71 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsTypeAPIType, td_, "td"),
72 MIRROR_FIELD_INFO(EtsTypeAPIType, contextLinker_, "contextLinker")};
73 }
74
GetTypeAPIMethodClassMembers()75 static std::vector<MirrorFieldInfo> GetTypeAPIMethodClassMembers()
76 {
77 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsTypeAPIMethod, methodType_, "methodType"),
78 MIRROR_FIELD_INFO(EtsTypeAPIMethod, name_, "name"),
79 MIRROR_FIELD_INFO(EtsTypeAPIMethod, attr_, "attributes"),
80 MIRROR_FIELD_INFO(EtsTypeAPIMethod, accessMod_, "accessMod")};
81 }
82
GetTypeAPIFieldClassMembers()83 static std::vector<MirrorFieldInfo> GetTypeAPIFieldClassMembers()
84 {
85 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsTypeAPIField, fieldType_, "fieldType"),
86 MIRROR_FIELD_INFO(EtsTypeAPIField, ownerType_, "ownerType"),
87 MIRROR_FIELD_INFO(EtsTypeAPIField, name_, "name"),
88 MIRROR_FIELD_INFO(EtsTypeAPIField, attr_, "attributes"),
89 MIRROR_FIELD_INFO(EtsTypeAPIField, accessMod_, "accessMod")};
90 }
91
GetTypeAPIParameterClassMembers()92 static std::vector<MirrorFieldInfo> GetTypeAPIParameterClassMembers()
93 {
94 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsTypeAPIParameter, paramType_, "paramType"),
95 MIRROR_FIELD_INFO(EtsTypeAPIParameter, name_, "name"),
96 MIRROR_FIELD_INFO(EtsTypeAPIParameter, attr_, "attributes")};
97 }
98
99 protected:
100 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
101
102 private:
103 RuntimeOptions options_;
104 EtsCoroutine *coroutine_ = nullptr;
105 };
106
TEST_F(EtsTypeAPITest,TypeAPITypeMemoryLayout)107 TEST_F(EtsTypeAPITest, TypeAPITypeMemoryLayout)
108 {
109 auto *typeAPITypeClass = vm_->GetClassLinker()->GetClass("Lstd/core/Type;");
110 ASSERT(typeAPITypeClass != nullptr);
111 MirrorFieldInfo::CompareMemberOffsets(typeAPITypeClass, GetTypeAPITypeClassMembers());
112 }
113
TEST_F(EtsTypeAPITest,TypeAPIMethodMemoryLayout)114 TEST_F(EtsTypeAPITest, TypeAPIMethodMemoryLayout)
115 {
116 auto *typeAPIMethodClass = PlatformTypes(vm_)->coreMethod;
117 MirrorFieldInfo::CompareMemberOffsets(typeAPIMethodClass, GetTypeAPIMethodClassMembers());
118 }
119
TEST_F(EtsTypeAPITest,TypeAPIFieldMemoryLayout)120 TEST_F(EtsTypeAPITest, TypeAPIFieldMemoryLayout)
121 {
122 auto *typeAPIFieldClass = PlatformTypes(vm_)->coreField;
123 MirrorFieldInfo::CompareMemberOffsets(typeAPIFieldClass, GetTypeAPIFieldClassMembers());
124 }
125
TEST_F(EtsTypeAPITest,TypeAPIParameterMemoryLayout)126 TEST_F(EtsTypeAPITest, TypeAPIParameterMemoryLayout)
127 {
128 auto *typeAPIParameterClass = PlatformTypes(vm_)->coreParameter;
129 MirrorFieldInfo::CompareMemberOffsets(typeAPIParameterClass, GetTypeAPIParameterClassMembers());
130 }
131
132 } // namespace ark::ets::test
133