• 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 "runtime/coroutines/coroutine_manager.h"
17 #include "runtime/coroutines/coroutine_events.h"
18 #include "plugins/ets/runtime/types/ets_promise.h"
19 #include "plugins/ets/runtime/ets_coroutine.h"
20 #include "plugins/ets/runtime/ets_platform_types.h"
21 #include "plugins/ets/runtime/ets_vm.h"
22 #include "plugins/ets/runtime/types/ets_method.h"
23 
24 namespace ark::ets {
25 
26 /*static*/
Create(EtsCoroutine * coro)27 EtsPromise *EtsPromise::Create(EtsCoroutine *coro)
28 {
29     [[maybe_unused]] EtsHandleScope scope(coro);
30     auto *klass = PlatformTypes(coro)->corePromise;
31     auto hPromise = EtsHandle<EtsPromise>(coro, EtsPromise::FromEtsObject(EtsObject::Create(coro, klass)));
32     ASSERT(hPromise.GetPtr() != nullptr);
33     auto *mutex = EtsMutex::Create(coro);
34     hPromise->SetMutex(coro, mutex);
35     auto *event = EtsEvent::Create(coro);
36     hPromise->SetEvent(coro, event);
37     return hPromise.GetPtr();
38 }
39 
OnPromiseCompletion(EtsCoroutine * coro)40 void EtsPromise::OnPromiseCompletion(EtsCoroutine *coro)
41 {
42     auto *cbQueue = GetCallbackQueue(coro);
43     auto *launchModeQueue = GetLaunchModeQueue(coro);
44     auto queueSize = GetQueueSize();
45     ASSERT(queueSize == 0 || cbQueue != nullptr);
46     ASSERT(queueSize == 0 || launchModeQueue != nullptr);
47 
48     if (Runtime::GetOptions().IsListUnhandledOnExitPromises(plugins::LangToRuntimeType(panda_file::SourceLang::ETS)) &&
49         state_ == STATE_REJECTED && queueSize == 0) {
50         coro->GetPandaVM()->AddUnhandledRejectedPromise(this);
51     }
52 
53     // Unblock awaitee coros
54     GetEvent(coro)->Fire();
55 
56     for (int idx = 0; idx < queueSize; ++idx) {
57         auto *thenCallback = cbQueue->Get(idx);
58         auto launchMode = static_cast<CoroutineLaunchMode>(launchModeQueue->Get(idx));
59         EtsPromise::LaunchCallback(coro, thenCallback, launchMode);
60     }
61     ClearQueues(coro);
62 }
63 
64 /* static */
LaunchCallback(EtsCoroutine * coro,EtsObject * callback,CoroutineLaunchMode launchMode)65 void EtsPromise::LaunchCallback(EtsCoroutine *coro, EtsObject *callback, CoroutineLaunchMode launchMode)
66 {
67     // Launch callback in its own coroutine
68     auto *coroManager = coro->GetCoroutineManager();
69     auto *event = Runtime::GetCurrent()->GetInternalAllocator()->New<CompletionEvent>(nullptr, coroManager);
70 
71     EtsMethod *etsmethod = callback->GetClass()->GetInstanceMethod(INVOKE_METHOD_NAME, nullptr);
72     auto *method = EtsMethod::ToRuntimeMethod(etsmethod);
73     ASSERT(method != nullptr);
74     auto args = PandaVector<Value> {Value(callback->GetCoreType())};
75     [[maybe_unused]] bool launchResult =
76         coroManager->Launch(event, method, std::move(args), launchMode, EtsCoroutine::PROMISE_CALLBACK, false);
77     ASSERT(launchResult);
78 }
79 
80 }  // namespace ark::ets
81