• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 "include/object_header.h"
17 #include "intrinsics.h"
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/handle_scope-inl.h"
20 #include "plugins/ets/runtime/ets_coroutine.h"
21 #include "plugins/ets/runtime/ets_exceptions.h"
22 #include "plugins/ets/runtime/types/ets_method.h"
23 #include "plugins/ets/runtime/ets_class_linker_extension.h"
24 #include "plugins/ets/runtime/types/ets_string.h"
25 #include "plugins/ets/runtime/ets_stubs-inl.h"
26 #include "runtime/mem/local_object_handle.h"
27 
28 namespace ark::ets::intrinsics {
29 
StdCoreClassGetNameInternal(EtsClass * cls)30 EtsString *StdCoreClassGetNameInternal(EtsClass *cls)
31 {
32     return cls->GetName();
33 }
34 
StdCoreClassOf(EtsObject * obj)35 EtsClass *StdCoreClassOf(EtsObject *obj)
36 {
37     ASSERT(obj != nullptr);
38     return obj->GetClass();
39 }
40 
StdCoreClassOfCaller()41 EtsClass *StdCoreClassOfCaller()
42 {
43     return GetMethodOwnerClassInFrames(EtsCoroutine::GetCurrent(), 0);
44 }
45 
StdCoreClassInitialize(EtsClass * cls)46 void StdCoreClassInitialize(EtsClass *cls)
47 {
48     ASSERT(cls != nullptr);
49 
50     if (UNLIKELY(!cls->IsInitialized())) {
51         auto coro = EtsCoroutine::GetCurrent();
52         EtsClassLinker *linker = coro->GetPandaVM()->GetClassLinker();
53         linker->InitializeClass(coro, cls);
54     }
55 }
56 
StdCoreClassGetDescriptor(EtsClass * cls)57 EtsString *StdCoreClassGetDescriptor(EtsClass *cls)
58 {
59     ASSERT(cls != nullptr);
60     return EtsString::CreateFromMUtf8(cls->GetDescriptor());
61 }
62 
StdCoreClassCreateInstance(EtsClass * cls)63 EtsObject *StdCoreClassCreateInstance(EtsClass *cls)
64 {
65     auto coro = EtsCoroutine::GetCurrent();
66     const auto throwCreateInstanceErr = [coro, cls](std::string_view msg) {
67         ets::ThrowEtsException(coro, panda_file_items::class_descriptors::ERROR,
68                                PandaString(msg) + " " + cls->GetDescriptor());
69     };
70 
71     if (UNLIKELY(!cls->GetRuntimeClass()->IsInstantiable() || cls->IsArrayClass())) {
72         throwCreateInstanceErr("Cannot instantiate");
73         return nullptr;
74     }
75 
76     EtsMethod *ctor = cls->GetDirectMethod(panda_file_items::CTOR.data(), ":V");
77     if (UNLIKELY(ctor == nullptr)) {
78         throwCreateInstanceErr("No default constructor in");
79         return nullptr;
80     }
81 
82     EtsClassLinker *linker = coro->GetPandaVM()->GetClassLinker();
83     if (UNLIKELY(!cls->IsInitialized() && !linker->InitializeClass(coro, cls))) {
84         return nullptr;
85     }
86     EtsObject *obj = EtsObject::Create(cls);
87     if (UNLIKELY(obj == nullptr)) {
88         return nullptr;
89     }
90 
91     LocalObjectHandle objHandle(coro, obj);
92     std::array<Value, 1> args {Value(obj->GetCoreType())};
93     ctor->GetPandaMethod()->Invoke(coro, args.data());
94     if (UNLIKELY(coro->HasPendingException())) {
95         return nullptr;
96     }
97     return objHandle.GetPtr();
98 }
99 
StdCoreRuntimeLinkerLoadClassInternal(EtsClass * ctxClassObj,EtsString * clsName,uint8_t init)100 EtsClass *StdCoreRuntimeLinkerLoadClassInternal(EtsClass *ctxClassObj, EtsString *clsName, uint8_t init)
101 {
102     EtsCoroutine *coro = EtsCoroutine::GetCurrent();
103     EtsClassLinker *linker = coro->GetPandaVM()->GetClassLinker();
104     ClassLinkerContext *ctx = ctxClassObj->GetRuntimeClass()->GetLoadContext();
105 
106     PandaString name = clsName->GetMutf8();
107     PandaString descriptor;
108     ClassHelper::GetDescriptor(utf::CStringAsMutf8(name.c_str()), &descriptor);
109 
110     EtsClass *klass = linker->GetClass(descriptor.c_str(), false, ctx);
111     if (UNLIKELY(klass == nullptr)) {
112         ASSERT(coro->HasPendingException());
113         return nullptr;
114     }
115 
116     if (UNLIKELY(init != 0 && !klass->IsInitialized())) {
117         if (UNLIKELY(!coro->GetPandaVM()->GetClassLinker()->InitializeClass(coro, klass))) {
118             ASSERT(coro->HasPendingException());
119             return nullptr;
120         }
121     }
122     return klass;
123 }
124 
125 }  // namespace ark::ets::intrinsics
126