1 /**
2 * Copyright (c) 2021-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 #ifndef PANDA_RUNTIME_CLASS_LINKER_INL_H_
16 #define PANDA_RUNTIME_CLASS_LINKER_INL_H_
17
18 #include "libpandafile/panda_cache.h"
19 #include "runtime/include/class_linker.h"
20 #include "runtime/include/panda_vm.h"
21 #include "runtime/include/runtime.h"
22 #include "runtime/include/mtmanaged_thread.h"
23
24 namespace ark {
25
26 // CC-OFFNXT(G.FUD.06) perf critical
GetClass(const Method & caller,panda_file::File::EntityId id,ClassLinkerErrorHandler * errorHandler)27 inline Class *ClassLinker::GetClass(const Method &caller, panda_file::File::EntityId id,
28 ClassLinkerErrorHandler *errorHandler /* = nullptr */)
29 {
30 ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) ||
31 !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || PandaVM::GetCurrent()->GetMutatorLock()->HasLock());
32
33 Class *klass = caller.GetPandaFile()->GetPandaCache()->GetClassFromCache(id);
34 if (klass != nullptr) {
35 return klass;
36 }
37 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(caller);
38 auto *ext = GetExtension(ctx);
39 ASSERT(ext != nullptr);
40 klass = ext->GetClass(*caller.GetPandaFile(), id, caller.GetClass()->GetLoadContext(),
41 (errorHandler == nullptr) ? ext->GetErrorHandler() : errorHandler);
42 if (LIKELY(klass != nullptr)) {
43 caller.GetPandaFile()->GetPandaCache()->SetClassCache(id, klass);
44 }
45 return klass;
46 }
47
AddClassRoot(ClassRoot root,Class * klass)48 inline void ClassLinker::AddClassRoot(ClassRoot root, Class *klass)
49 {
50 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(*klass);
51 auto *ext = GetExtension(ctx);
52 ASSERT(ext != nullptr);
53 ASSERT(klass != nullptr);
54 ext->SetClassRoot(root, klass);
55
56 RemoveCreatedClassInExtension(klass);
57 }
58
59 // CC-OFFNXT(G.FUD.06) perf critical
GetLoadedClass(const panda_file::File & pf,panda_file::File::EntityId id,ClassLinkerContext * context)60 inline Class *ClassLinker::GetLoadedClass(const panda_file::File &pf, panda_file::File::EntityId id,
61 ClassLinkerContext *context)
62 {
63 ASSERT(context != nullptr);
64
65 Class *cls = pf.GetPandaCache()->GetClassFromCache(id);
66 if (LIKELY(cls != nullptr)) {
67 return cls;
68 }
69
70 cls = FindLoadedClass(pf.GetStringData(id).data, context);
71 if (LIKELY(cls != nullptr)) {
72 pf.GetPandaCache()->SetClassCache(id, cls);
73 return cls;
74 }
75 return nullptr;
76 }
77
78 } // namespace ark
79
80 #endif // PANDA_RUNTIME_CLASS_LINKER_INL_H_
81