• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 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 
27 namespace panda::ets {
28 
GetExceptionClass(EtsCoroutine * coroutine,const char * classDescriptor)29 static EtsClass *GetExceptionClass(EtsCoroutine *coroutine, const char *classDescriptor)
30 {
31     ASSERT(coroutine != nullptr);
32     ASSERT(classDescriptor != nullptr);
33 
34     EtsClassLinker *classLinker = coroutine->GetPandaVM()->GetClassLinker();
35     EtsClass *cls = classLinker->GetClass(classDescriptor, true);
36     if (cls == nullptr) {
37         LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " not found";
38         return nullptr;
39     }
40 
41     if (!classLinker->InitializeClass(coroutine, cls)) {
42         LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " cannot be initialized";
43         return nullptr;
44     }
45     return cls;
46 }
47 
ThrowEtsException(EtsCoroutine * coroutine,const char * classDescriptor,const char * msg)48 void ThrowEtsException(EtsCoroutine *coroutine, const char *classDescriptor, const char *msg)
49 {
50     ASSERT(coroutine != nullptr);
51     ASSERT(coroutine == EtsCoroutine::GetCurrent());
52 
53     if (coroutine->IsUsePreAllocObj()) {
54         coroutine->SetUsePreAllocObj(false);
55         coroutine->SetException(coroutine->GetVM()->GetOOMErrorObject());
56         return;
57     }
58 
59     [[maybe_unused]] EtsHandleScope scope(coroutine);
60     EtsHandle<EtsObject> cause(coroutine, EtsObject::FromCoreType(coroutine->GetException()));
61     coroutine->ClearException();
62 
63     EtsClass *cls = GetExceptionClass(coroutine, classDescriptor);
64     if (cls == nullptr) {
65         return;
66     }
67 
68     EtsString *etsMsg = nullptr;
69     if (msg != nullptr) {
70         etsMsg = EtsString::CreateFromMUtf8(msg);
71     } else {
72         etsMsg = EtsString::CreateNewEmptyString();
73     }
74     if (UNLIKELY(etsMsg == nullptr)) {
75         // OOM happened during msg allocation
76         ASSERT(coroutine->HasPendingException());
77         return;
78     }
79 
80     Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID),
81                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE),
82                                                      panda_file::Type(panda_file::Type::TypeId::REFERENCE)},
83                         Method::Proto::RefTypeVector {panda_file_items::class_descriptors::STRING,
84                                                       panda_file_items::class_descriptors::OBJECT});
85     EtsMethod *ctor = cls->GetDirectMethod(panda_file_items::CTOR.data(), proto);
86     if (ctor == nullptr) {
87         LOG(FATAL, RUNTIME) << "No method " << panda_file_items::CTOR << " in class " << classDescriptor;
88         return;
89     }
90 
91     EtsHandle<EtsString> msgHandle(coroutine, etsMsg);
92     EtsHandle<EtsObject> excHandle(coroutine, EtsObject::Create(cls));
93     // clang-format off
94     std::array args {
95         Value(excHandle.GetPtr()->GetCoreType()),
96         Value(msgHandle.GetPtr()->GetCoreType()),
97         Value(cause.GetPtr()->GetCoreType())
98     };
99     // clang-format on
100 
101     EtsMethod::ToRuntimeMethod(ctor)->InvokeVoid(coroutine, args.data());
102     if (LIKELY(!coroutine->HasPendingException())) {
103         coroutine->SetException(excHandle.GetPtr()->GetCoreType());
104     }
105 }
106 
107 }  // namespace panda::ets
108