• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
GetClass(const Method & caller,panda_file::File::EntityId id,ClassLinkerErrorHandler * errorHandler)26 inline Class *ClassLinker::GetClass(const Method &caller, panda_file::File::EntityId id,
27                                     ClassLinkerErrorHandler *errorHandler /* = nullptr */)
28 {
29     ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()) ||
30            !PandaVM::GetCurrent()->GetGC()->IsGCRunning() || PandaVM::GetCurrent()->GetMutatorLock()->HasLock());
31 
32     Class *klass = caller.GetPandaFile()->GetPandaCache()->GetClassFromCache(id);
33     if (klass != nullptr) {
34         return klass;
35     }
36     LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(caller);
37     auto *ext = GetExtension(ctx);
38     ASSERT(ext != nullptr);
39     klass = ext->GetClass(*caller.GetPandaFile(), id, caller.GetClass()->GetLoadContext(),
40                           (errorHandler == nullptr) ? ext->GetErrorHandler() : errorHandler);
41     if (LIKELY(klass != nullptr)) {
42         caller.GetPandaFile()->GetPandaCache()->SetClassCache(id, klass);
43     }
44     return klass;
45 }
46 
AddClassRoot(ClassRoot root,Class * klass)47 inline void ClassLinker::AddClassRoot(ClassRoot root, Class *klass)
48 {
49     LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(*klass);
50     auto *ext = GetExtension(ctx);
51     ASSERT(ext != nullptr);
52     ASSERT(klass != nullptr);
53     ext->SetClassRoot(root, klass);
54 
55     RemoveCreatedClassInExtension(klass);
56 }
57 
GetLoadedClass(const panda_file::File & pf,panda_file::File::EntityId id,ClassLinkerContext * context)58 inline Class *ClassLinker::GetLoadedClass(const panda_file::File &pf, panda_file::File::EntityId id,
59                                           ClassLinkerContext *context)
60 {
61     ASSERT(context != nullptr);
62     ASSERT(!MTManagedThread::ThreadIsMTManagedThread(Thread::GetCurrent()));
63 
64     Class *cls = pf.GetPandaCache()->GetClassFromCache(id);
65     if (LIKELY(cls != nullptr)) {
66         return cls;
67     }
68 
69     cls = context->FindClass(pf.GetStringData(id).data);
70     if (LIKELY(cls != nullptr)) {
71         pf.GetPandaCache()->SetClassCache(id, cls);
72         return cls;
73     }
74     return nullptr;
75 }
76 
77 }  // namespace ark
78 
79 #endif  // PANDA_RUNTIME_CLASS_LINKER_INL_H_
80