1 /**
2 * Copyright (c) 2021-2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include <cmath>
20 #include <cstdint>
21 #include <sstream>
22
23 #include "assembly-parser.h"
24 #include "libpandabase/mem/pool_manager.h"
25 #include "libpandabase/utils/utf.h"
26 #include "libpandafile/bytecode_emitter.h"
27 #include "libpandafile/file.h"
28 #include "libpandafile/file_items.h"
29 #include "libpandafile/value.h"
30 #include "runtime/include/class_linker.h"
31 #include "runtime/include/method.h"
32 #include "runtime/include/runtime.h"
33 #include "runtime/include/runtime_options.h"
34
35 namespace ark::test {
36
37 class GetMethodTest : public testing::Test {
38 public:
GetMethodTest()39 GetMethodTest()
40 {
41 RuntimeOptions options;
42 options.SetShouldLoadBootPandaFiles(false);
43 options.SetShouldInitializeIntrinsics(false);
44 options.SetRunGcInPlace(true);
45 options.SetVerifyCallStack(false);
46 options.SetGcType("epsilon");
47 Runtime::Create(options);
48 thread_ = ark::MTManagedThread::GetCurrent();
49 thread_->ManagedCodeBegin();
50 }
51
~GetMethodTest()52 ~GetMethodTest() override
53 {
54 thread_->ManagedCodeEnd();
55 Runtime::Destroy();
56 }
57
58 NO_COPY_SEMANTIC(GetMethodTest);
59 NO_MOVE_SEMANTIC(GetMethodTest);
60
61 private:
62 ark::MTManagedThread *thread_ = nullptr;
63 };
64
TEST_F(GetMethodTest,GetMethod)65 TEST_F(GetMethodTest, GetMethod)
66 {
67 pandasm::Parser p;
68 PandaStringStream ss;
69 ss << ".record R1 {}" << std::endl;
70
71 // define some methods unsorted
72 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
73 std::string methodsName[] = {"ab", "hello", "f1", "say", "world", "k0", "a"};
74 size_t methodsNum = sizeof(methodsName) / sizeof(methodsName[0]);
75 for (size_t i = 0; i < methodsNum; i++) {
76 ss << ".function void R1." << methodsName[i] << "() {" << std::endl;
77 ss << " return.void" << std::endl;
78 ss << "}" << std::endl;
79 }
80
81 auto source = ss.str();
82 // NOLINTNEXTLINE(readability-redundant-string-cstr)
83 auto res = p.Parse(source.c_str());
84 ASSERT_TRUE(res) << res.Error().message;
85
86 auto pf = pandasm::AsmEmitter::Emit(res.Value());
87 ASSERT_NE(pf, nullptr) << pandasm::AsmEmitter::GetLastError();
88
89 ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
90 classLinker->AddPandaFile(std::move(pf));
91
92 PandaString descriptor;
93 auto *ext = classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
94 Class *klass = ext->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R1"), &descriptor));
95 ASSERT_NE(klass, nullptr);
96
97 // check if methods sorted by id and name
98 auto methods = klass->GetMethods();
99 ASSERT_EQ(methods.size(), methodsNum);
100 for (size_t i = 0; i < methodsNum; i++) {
101 for (size_t j = i + 1; j < methodsNum; j++) {
102 ASSERT_TRUE(methods[i].GetFileId().GetOffset() < methods[j].GetFileId().GetOffset());
103 ASSERT_TRUE(methods[i].GetName() < methods[j].GetName());
104 }
105 }
106
107 // get each method by id and name
108 Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID)},
109 Method::Proto::RefTypeVector {});
110 for (size_t i = 0; i < methodsNum; i++) {
111 Method *method = klass->GetClassMethod(utf::CStringAsMutf8(methodsName[i].c_str()), proto);
112 ASSERT_NE(method, nullptr);
113 ASSERT_EQ(method, klass->GetStaticClassMethod(method->GetFileId()));
114 }
115 }
116
117 } // namespace ark::test
118