• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 panda::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_ = panda::MTManagedThread::GetCurrent();
49         thread_->ManagedCodeBegin();
50     }
51 
~GetMethodTest()52     ~GetMethodTest()
53     {
54         thread_->ManagedCodeEnd();
55         Runtime::Destroy();
56     }
57 
58 protected:
59     panda::MTManagedThread *thread_ = nullptr;
60 };
61 
TEST_F(GetMethodTest,GetMethod)62 TEST_F(GetMethodTest, GetMethod)
63 {
64     pandasm::Parser p;
65     PandaStringStream ss;
66     ss << ".record R1 {}" << std::endl;
67 
68     // define some methods unsorted
69     std::string methods_name[] = {"ab", "hello", "f1", "say", "world", "k0", "a"};
70     size_t methods_num = sizeof(methods_name) / sizeof(methods_name[0]);
71     for (size_t i = 0; i < methods_num; i++) {
72         ss << ".function void R1." << methods_name[i] << "() {" << std::endl;
73         ss << "    return.void" << std::endl;
74         ss << "}" << std::endl;
75     }
76 
77     auto source = ss.str();
78     auto res = p.Parse(source.c_str());
79     ASSERT_TRUE(res) << res.Error().message;
80 
81     auto pf = pandasm::AsmEmitter::Emit(res.Value());
82     ASSERT_NE(pf, nullptr) << pandasm::AsmEmitter::GetLastError();
83 
84     ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
85     class_linker->AddPandaFile(std::move(pf));
86 
87     PandaString descriptor;
88     auto *ext = class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
89     Class *klass = ext->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R1"), &descriptor));
90     ASSERT_NE(klass, nullptr);
91 
92     // check if methods sorted by id and name
93     auto methods = klass->GetMethods();
94     ASSERT_EQ(methods.size(), methods_num);
95     for (size_t i = 0; i < methods_num; i++) {
96         for (size_t j = i + 1; j < methods_num; j++) {
97             ASSERT_TRUE(methods[i].GetFileId().GetOffset() < methods[j].GetFileId().GetOffset());
98             ASSERT_TRUE(methods[i].GetName() < methods[j].GetName());
99         }
100     }
101 
102     // get each method by id and name
103     Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID)},
104                         Method::Proto::RefTypeVector {});
105     for (size_t i = 0; i < methods_num; i++) {
106         Method *method = klass->GetClassMethod(utf::CStringAsMutf8(methods_name[i].c_str()), proto);
107         ASSERT_NE(method, nullptr);
108         ASSERT_EQ(method, klass->GetStaticClassMethod(method->GetFileId()));
109     }
110 }
111 
112 }  // namespace panda::test
113