• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 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_coroutine.h"
17 #include "runtime/include/object_header.h"
18 #include "plugins/ets/runtime/types/ets_promise.h"
19 #include "plugins/ets/runtime/ets_vm.h"
20 #include "runtime/include/panda_vm.h"
21 #include "plugins/ets/runtime/ets_class_linker_extension.h"
22 #include "plugins/ets/runtime/types/ets_object.h"
23 #include "plugins/ets/runtime/types/ets_box_primitive-inl.h"
24 #include "intrinsics.h"
25 
26 namespace panda::ets {
27 
EtsCoroutine(ThreadId id,mem::InternalAllocatorPtr allocator,PandaVM * vm,PandaString name,CoroutineContext * context,std::optional<EntrypointInfo> && epInfo)28 EtsCoroutine::EtsCoroutine(ThreadId id, mem::InternalAllocatorPtr allocator, PandaVM *vm, PandaString name,
29                            CoroutineContext *context, std::optional<EntrypointInfo> &&epInfo)
30     : Coroutine(id, allocator, vm, panda::panda_file::SourceLang::ETS, std::move(name), context, std::move(epInfo))
31 {
32     ASSERT(vm != nullptr);
33 }
34 
GetPandaVM() const35 PandaEtsVM *EtsCoroutine::GetPandaVM() const
36 {
37     return static_cast<PandaEtsVM *>(GetVM());
38 }
39 
GetCoroutineManager() const40 CoroutineManager *EtsCoroutine::GetCoroutineManager() const
41 {
42     return GetPandaVM()->GetCoroutineManager();
43 }
44 
Initialize()45 void EtsCoroutine::Initialize()
46 {
47     auto allocator = GetVM()->GetHeapManager()->GetInternalAllocator();
48     auto etsNapiEnv = PandaEtsNapiEnv::Create(this, allocator);
49     if (!etsNapiEnv) {
50         LOG(FATAL, RUNTIME) << "Cannot create PandaEtsNapiEnv: " << etsNapiEnv.Error();
51     }
52     etsNapiEnv_ = std::move(etsNapiEnv.Value());
53     // Main EtsCoroutine is Initialized before class linker and promise_class_ptr_ will be set after creating the class
54     if (HasManagedEntrypoint()) {
55         promiseClassPtr_ = GetPandaVM()->GetClassLinker()->GetPromiseClass()->GetRuntimeClass();
56         undefinedObj_ = GetPandaVM()->GetUndefinedObject();
57     }
58     ASSERT(promiseClassPtr_ != nullptr || !HasManagedEntrypoint());
59 
60     Coroutine::Initialize();
61 }
62 
FreeInternalMemory()63 void EtsCoroutine::FreeInternalMemory()
64 {
65     etsNapiEnv_->FreeInternalMemory();
66     ManagedThread::FreeInternalMemory();
67 }
68 
RequestCompletion(Value returnValue)69 void EtsCoroutine::RequestCompletion(Value returnValue)
70 {
71     auto promiseRef = GetCompletionEvent()->GetPromise();
72     auto promise = reinterpret_cast<EtsPromise *>(GetVM()->GetGlobalObjectStorage()->Get(promiseRef));
73     if (promise != nullptr) {
74         [[maybe_unused]] EtsHandleScope scope(this);
75         EtsHandle<EtsPromise> hpromise(this, promise);
76         EtsObject *retObject = nullptr;
77         if (!HasPendingException()) {
78             panda_file::Type returnType = GetReturnType();
79             retObject = GetReturnValueAsObject(returnType, returnValue);
80             if (retObject != nullptr) {
81                 if (returnType.IsVoid()) {
82                     LOG(DEBUG, COROUTINES) << "Coroutine " << GetName() << " has completed";
83                 } else if (returnType.IsPrimitive()) {
84                     LOG(DEBUG, COROUTINES) << "Coroutine " << GetName() << " has completed with return value 0x"
85                                            << std::hex << returnValue.GetAs<uint64_t>();
86                 } else {
87                     LOG(DEBUG, COROUTINES)
88                         << "Coroutine " << GetName() << " has completed with return value = ObjectPtr<"
89                         << returnValue.GetAs<ObjectHeader *>() << ">";
90                 }
91             }
92         }
93         if (HasPendingException()) {
94             // An exception may occur while boxin primitive return value in GetReturnValueAsObject
95             auto *exc = GetException();
96             ClearException();
97             LOG(INFO, COROUTINES) << "Coroutine " << GetName()
98                                   << " completed with an exception: " << exc->ClassAddr<Class>()->GetName();
99             intrinsics::EtsPromiseReject(hpromise.GetPtr(), EtsObject::FromCoreType(exc));
100         } else {
101             intrinsics::EtsPromiseResolve(hpromise.GetPtr(), retObject);
102         }
103     } else {
104         LOG(DEBUG, COROUTINES)
105             << "Coroutine " << GetName()
106             << " has completed, but the associated promise has been already collected by the GC. Exception thrown: "
107             << HasPendingException();
108     }
109     Coroutine::RequestCompletion(returnValue);
110 }
111 
GetReturnType()112 panda_file::Type EtsCoroutine::GetReturnType()
113 {
114     Method *entrypoint = GetManagedEntrypoint();
115     ASSERT(entrypoint != nullptr);
116     return entrypoint->GetReturnType();
117 }
118 
GetReturnValueAsObject(panda_file::Type returnType,Value returnValue)119 EtsObject *EtsCoroutine::GetReturnValueAsObject(panda_file::Type returnType, Value returnValue)
120 {
121     switch (returnType.GetId()) {
122         case panda_file::Type::TypeId::VOID:
123             LOG(FATAL, COROUTINES) << "Return type 'void' is not supported yet in 'launch' instruction";
124             break;
125         case panda_file::Type::TypeId::U1:
126             return EtsBoxPrimitive<EtsBoolean>::Create(this, returnValue.GetAs<EtsBoolean>());
127         case panda_file::Type::TypeId::I8:
128             return EtsBoxPrimitive<EtsByte>::Create(this, returnValue.GetAs<EtsByte>());
129         case panda_file::Type::TypeId::I16:
130             return EtsBoxPrimitive<EtsShort>::Create(this, returnValue.GetAs<EtsShort>());
131         case panda_file::Type::TypeId::U16:
132             return EtsBoxPrimitive<EtsChar>::Create(this, returnValue.GetAs<EtsChar>());
133         case panda_file::Type::TypeId::I32:
134             return EtsBoxPrimitive<EtsInt>::Create(this, returnValue.GetAs<EtsInt>());
135         case panda_file::Type::TypeId::F32:
136             return EtsBoxPrimitive<EtsFloat>::Create(this, returnValue.GetAs<EtsFloat>());
137         case panda_file::Type::TypeId::F64:
138             return EtsBoxPrimitive<EtsDouble>::Create(this, returnValue.GetAs<EtsDouble>());
139         case panda_file::Type::TypeId::I64:
140             return EtsBoxPrimitive<EtsLong>::Create(this, returnValue.GetAs<EtsLong>());
141         case panda_file::Type::TypeId::REFERENCE:
142             return EtsObject::FromCoreType(returnValue.GetAs<ObjectHeader *>());
143         default:
144             LOG(FATAL, COROUTINES) << "Unsupported return type: " << returnType;
145             break;
146     }
147     return nullptr;
148 }
149 }  // namespace panda::ets
150