1 /**
2 * Copyright (c) 2024-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 "include/object_header.h"
17 #include "intrinsics.h"
18 #include "libpandabase/utils/logger.h"
19 #include "plugins/ets/runtime/ets_class_linker_context.h"
20 #include "plugins/ets/runtime/ets_class_linker_extension.h"
21 #include "plugins/ets/runtime/ets_coroutine.h"
22 #include "plugins/ets/runtime/ets_stubs-inl.h"
23 #include "plugins/ets/runtime/ets_vm.h"
24 #include "plugins/ets/runtime/types/ets_field.h"
25 #include "plugins/ets/runtime/types/ets_method.h"
26 #include "plugins/ets/runtime/types/ets_primitives.h"
27 #include "plugins/ets/runtime/types/ets_runtime_linker.h"
28 #include "plugins/ets/runtime/types/ets_string.h"
29 #include "runtime/handle_scope-inl.h"
30
31 #ifdef PANDA_ETS_INTEROP_JS
32 #include "plugins/ets/runtime/interop_js/interop_context.h"
33 #endif /* PANDA_ETS_INTEROP_JS */
34
35 namespace ark::ets::intrinsics {
36
StdCoreClassGetNameInternal(EtsClass * cls)37 EtsString *StdCoreClassGetNameInternal(EtsClass *cls)
38 {
39 return cls->GetName();
40 }
41
StdCoreClassGetLinker(EtsClass * cls)42 EtsRuntimeLinker *StdCoreClassGetLinker(EtsClass *cls)
43 {
44 return EtsClassLinkerExtension::GetOrCreateEtsRuntimeLinker(cls->GetLoadContext());
45 }
46
StdCoreClassOf(EtsObject * obj)47 EtsClass *StdCoreClassOf(EtsObject *obj)
48 {
49 ASSERT(obj != nullptr);
50 return obj->GetClass();
51 }
52
StdCoreClassCurrent()53 EtsClass *StdCoreClassCurrent()
54 {
55 return GetMethodOwnerClassInFrames(EtsCoroutine::GetCurrent(), 0);
56 }
57
StdCoreClassOfCaller()58 EtsClass *StdCoreClassOfCaller()
59 {
60 return GetMethodOwnerClassInFrames(EtsCoroutine::GetCurrent(), 1);
61 }
62
StdCoreClassInitialize(EtsClass * cls)63 void StdCoreClassInitialize(EtsClass *cls)
64 {
65 ASSERT(cls != nullptr);
66
67 if (UNLIKELY(!cls->IsInitialized())) {
68 auto coro = EtsCoroutine::GetCurrent();
69 ASSERT(coro != nullptr);
70 EtsClassLinker *linker = coro->GetPandaVM()->GetClassLinker();
71 linker->InitializeClass(coro, cls);
72 }
73 }
74
StdCoreClassGetDescriptor(EtsClass * cls)75 EtsString *StdCoreClassGetDescriptor(EtsClass *cls)
76 {
77 ASSERT(cls != nullptr);
78 return EtsString::CreateFromMUtf8(cls->GetDescriptor());
79 }
80
StdCoreClassCreateInstance(EtsClass * cls)81 EtsObject *StdCoreClassCreateInstance(EtsClass *cls)
82 {
83 ASSERT(cls != nullptr);
84 return cls->CreateInstance();
85 }
86
EtsRuntimeLinkerFindLoadedClass(EtsRuntimeLinker * runtimeLinker,EtsString * clsName)87 EtsClass *EtsRuntimeLinkerFindLoadedClass(EtsRuntimeLinker *runtimeLinker, EtsString *clsName)
88 {
89 const auto name = clsName->GetMutf8();
90 PandaString descriptor;
91 const auto *classDescriptor = ClassHelper::GetDescriptor(utf::CStringAsMutf8(name.c_str()), &descriptor);
92 return runtimeLinker->FindLoadedClass(classDescriptor);
93 }
94
EtsRuntimeLinkerInitializeContext(EtsRuntimeLinker * runtimeLinker)95 void EtsRuntimeLinkerInitializeContext(EtsRuntimeLinker *runtimeLinker)
96 {
97 auto *ext = PandaEtsVM::GetCurrent()->GetEtsClassLinkerExtension();
98 ext->RegisterContext([ext, runtimeLinker]() {
99 ASSERT(runtimeLinker->GetClassLinkerContext() == nullptr);
100 auto allocator = ext->GetClassLinker()->GetAllocator();
101 auto *ctx = allocator->New<EtsClassLinkerContext>(runtimeLinker);
102 runtimeLinker->SetClassLinkerContext(ctx);
103 return ctx;
104 });
105 #ifdef PANDA_ETS_INTEROP_JS
106 // At first call to ets from js, no available class linker context on the stack
107 // Thus, register the first available application class linker context as default class linker context for interop
108 // Tracked in #24199
109 interop::js::InteropCtx::InitializeDefaultLinkerCtxIfNeeded(runtimeLinker);
110 #endif /* PANDA_ETS_INTEROP_JS */
111 }
112
EtsBootRuntimeLinkerFindAndLoadClass(ObjectHeader * runtimeLinker,EtsString * clsName,EtsBoolean init)113 EtsClass *EtsBootRuntimeLinkerFindAndLoadClass(ObjectHeader *runtimeLinker, EtsString *clsName, EtsBoolean init)
114 {
115 const auto name = clsName->GetMutf8();
116 PandaString descriptor;
117 auto *classDescriptor = ClassHelper::GetDescriptor(utf::CStringAsMutf8(name.c_str()), &descriptor);
118
119 auto *coro = EtsCoroutine::GetCurrent();
120 // Use core ClassLinker in order to pass nullptr as error handler,
121 // as exception is thrown in managed RuntimeLinker.loadClass
122 auto *linker = Runtime::GetCurrent()->GetClassLinker();
123 auto *ctx = EtsRuntimeLinker::FromCoreType(runtimeLinker)->GetClassLinkerContext();
124 ASSERT(ctx->IsBootContext());
125 auto *klass = linker->GetClass(classDescriptor, true, ctx, nullptr);
126 if (klass == nullptr) {
127 return nullptr;
128 }
129
130 if (UNLIKELY(init != 0 && !klass->IsInitialized())) {
131 if (UNLIKELY(!linker->InitializeClass(coro, klass))) {
132 ASSERT(coro->HasPendingException());
133 return nullptr;
134 }
135 }
136 return EtsClass::FromRuntimeClass(klass);
137 }
138
EtsGetNearestNonBootRuntimeLinker()139 EtsRuntimeLinker *EtsGetNearestNonBootRuntimeLinker()
140 {
141 auto *coro = EtsCoroutine::GetCurrent();
142 for (auto stack = StackWalker::Create(coro); stack.HasFrame(); stack.NextFrame()) {
143 auto *method = stack.GetMethod();
144 if (LIKELY(method != nullptr)) {
145 auto *cls = method->GetClass();
146 ASSERT(cls != nullptr);
147 auto *ctx = cls->GetLoadContext();
148 ASSERT(ctx != nullptr);
149 if (!ctx->IsBootContext()) {
150 return reinterpret_cast<EtsClassLinkerContext *>(ctx)->GetRuntimeLinker();
151 }
152 }
153 }
154 return nullptr;
155 }
156
157 } // namespace ark::ets::intrinsics
158