• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 "plugins/ets/runtime/ets_exceptions.h"
17 
18 #include "plugins/ets/runtime/ets_class_linker_extension.h"
19 #include "plugins/ets/runtime/ets_coroutine.h"
20 #include "plugins/ets/runtime/ets_handle.h"
21 #include "plugins/ets/runtime/ets_handle_scope.h"
22 #include "plugins/ets/runtime/ets_panda_file_items.h"
23 #include "plugins/ets/runtime/ets_vm.h"
24 #include "plugins/ets/runtime/types/ets_method.h"
25 #include "plugins/ets/runtime/types/ets_string.h"
26 #include "plugins/ets/runtime/types/ets_error_options.h"
27 
28 namespace ark::ets {
29 
GetExceptionClass(EtsCoroutine * coro,const char * classDescriptor)30 static EtsClass *GetExceptionClass(EtsCoroutine *coro, const char *classDescriptor)
31 {
32     EtsClassLinker *classLinker = coro->GetPandaVM()->GetClassLinker();
33     EtsClass *cls = classLinker->GetClass(classDescriptor, true);
34     if (cls == nullptr) {
35         LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " not found";
36         return nullptr;
37     }
38 
39     if (!classLinker->InitializeClass(coro, cls)) {
40         LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " cannot be initialized";
41         return nullptr;
42     }
43     return cls;
44 }
45 
CreateExceptionInstance(EtsCoroutine * coro,EtsClass * cls,EtsHandle<EtsString> msg,EtsHandle<EtsObject> pending)46 static EtsObject *CreateExceptionInstance(EtsCoroutine *coro, EtsClass *cls, EtsHandle<EtsString> msg,
47                                           EtsHandle<EtsObject> pending)
48 {
49     EtsHandle<EtsObject> error(coro, EtsObject::Create(cls));
50     if (UNLIKELY(error.GetPtr() == nullptr)) {
51         return nullptr;
52     }
53 
54     Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID),
55                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE),
56                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE)},
57                         Method::Proto::RefTypeVector {panda_file_items::class_descriptors::STRING,
58                                                       panda_file_items::class_descriptors::OBJECT});
59     EtsMethod *ctor = cls->GetDirectMethod(panda_file_items::CTOR.data(), proto);
60     if (ctor == nullptr) {
61         LOG(FATAL, RUNTIME) << "No method " << panda_file_items::CTOR << " in class " << cls->GetDescriptor();
62         return nullptr;
63     }
64 
65     std::array args {Value(error.GetPtr()->GetCoreType()), Value(msg.GetPtr()->GetCoreType()),
66                      Value(pending.GetPtr()->GetCoreType())};
67 
68     EtsMethod::ToRuntimeMethod(ctor)->InvokeVoid(coro, args.data());
69     if (UNLIKELY(coro->HasPendingException())) {
70         return nullptr;
71     }
72     return error.GetPtr();
73 }
74 
CreateErrorInstance(EtsCoroutine * coro,EtsClass * cls,EtsHandle<EtsString> msg,EtsHandle<EtsObject> pending)75 static EtsObject *CreateErrorInstance(EtsCoroutine *coro, EtsClass *cls, EtsHandle<EtsString> msg,
76                                       EtsHandle<EtsObject> pending)
77 {
78     EtsHandle<EtsErrorOptions> errOptions(coro, EtsErrorOptions::Create(coro));
79     if (UNLIKELY(errOptions.GetPtr() == nullptr)) {
80         return nullptr;
81     }
82     errOptions->SetCause(pending.GetPtr());
83 
84     EtsHandle<EtsObject> error(coro, EtsObject::Create(cls));
85     if (UNLIKELY(error.GetPtr() == nullptr)) {
86         return nullptr;
87     }
88 
89     Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID),
90                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE),
91                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE)},
92                         Method::Proto::RefTypeVector {panda_file_items::class_descriptors::STRING,
93                                                       panda_file_items::class_descriptors::ERROR_OPTIONS});
94     EtsMethod *ctor = cls->GetDirectMethod(panda_file_items::CTOR.data(), proto);
95     if (ctor == nullptr) {
96         LOG(FATAL, RUNTIME) << "No method " << panda_file_items::CTOR << " in class " << cls->GetDescriptor();
97         return nullptr;
98     }
99 
100     std::array args {Value(error.GetPtr()->GetCoreType()), Value(msg.GetPtr()->GetCoreType()),
101                      Value(errOptions.GetPtr()->AsObject()->GetCoreType())};
102 
103     EtsMethod::ToRuntimeMethod(ctor)->InvokeVoid(coro, args.data());
104     if (UNLIKELY(coro->HasPendingException())) {
105         return nullptr;
106     }
107     return error.GetPtr();
108 }
109 
SetupEtsException(EtsCoroutine * coro,const char * classDescriptor,const char * msg)110 EtsObject *SetupEtsException(EtsCoroutine *coro, const char *classDescriptor, const char *msg)
111 {
112     [[maybe_unused]] EtsHandleScope scope(coro);
113     EtsHandle<EtsObject> pending(coro, EtsObject::FromCoreType(coro->GetException()));
114     coro->ClearException();
115 
116     EtsHandle<EtsString> etsMsg(coro,
117                                 msg == nullptr ? EtsString::CreateNewEmptyString() : EtsString::CreateFromMUtf8(msg));
118     if (UNLIKELY(etsMsg.GetPtr() == nullptr)) {
119         ASSERT(coro->HasPendingException());
120         return nullptr;
121     }
122 
123     EtsClass *cls = GetExceptionClass(coro, classDescriptor);
124     if (UNLIKELY(cls == nullptr)) {
125         ASSERT(coro->HasPendingException());
126         return nullptr;
127     }
128 
129     if (PlatformTypes(coro)->escompatError->IsAssignableFrom(cls)) {
130         return CreateErrorInstance(coro, cls, etsMsg, pending);
131     }
132     if (PlatformTypes(coro)->coreException->IsAssignableFrom(cls)) {
133         return CreateExceptionInstance(coro, cls, etsMsg, pending);
134     }
135     LOG(FATAL, RUNTIME) << "Class " << cls->GetDescriptor() << " is not compatible with (Error | Exception)";
136     return nullptr;
137 }
138 
ThrowEtsException(EtsCoroutine * coroutine,const char * classDescriptor,const char * msg)139 void ThrowEtsException(EtsCoroutine *coroutine, const char *classDescriptor, const char *msg)
140 {
141     ASSERT(coroutine != nullptr);
142     ASSERT(coroutine == EtsCoroutine::GetCurrent());
143 
144     if (coroutine->IsUsePreAllocObj()) {
145         coroutine->SetUsePreAllocObj(false);
146         coroutine->SetException(coroutine->GetVM()->GetOOMErrorObject());
147         return;
148     }
149 
150     EtsObject *exc = SetupEtsException(coroutine, classDescriptor, msg);
151     if (LIKELY(exc != nullptr)) {
152         coroutine->SetException(exc->GetCoreType());
153     }
154     ASSERT(coroutine->HasPendingException());
155 }
156 
157 }  // namespace ark::ets
158