1 /**
2 * Copyright (c) 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 "intrinsics.h"
17 #include "plugins/ets/runtime/ets_coroutine.h"
18 #include "plugins/ets/runtime/types/ets_promise.h"
19 #include "plugins/ets/runtime/types/ets_job.h"
20 #include "plugins/ets/runtime/ets_class_linker.h"
21 #include "plugins/ets/runtime/ets_handle_scope.h"
22 #include "plugins/ets/runtime/ets_handle.h"
23 #include "plugins/ets/runtime/ets_panda_file_items.h"
24 #include "runtime/include/object_header.h"
25 #include "types/ets_object.h"
26
27 namespace ark::ets::intrinsics {
28 extern "C" {
EtsAwaitJob(EtsJob * job)29 EtsObject *EtsAwaitJob(EtsJob *job)
30 {
31 EtsCoroutine *currentCoro = EtsCoroutine::GetCurrent();
32 if (job == nullptr) {
33 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ETS);
34 ThrowNullPointerException(ctx, currentCoro);
35 return nullptr;
36 }
37 if (currentCoro->GetCoroutineManager()->IsCoroutineSwitchDisabled()) {
38 ThrowEtsException(currentCoro, panda_file_items::class_descriptors::INVALID_COROUTINE_OPERATION_ERROR,
39 "Cannot await in the current context!");
40 return nullptr;
41 }
42 [[maybe_unused]] EtsHandleScope scope(currentCoro);
43 EtsHandle<EtsJob> jobHandle(currentCoro, job);
44
45 LOG(DEBUG, COROUTINES) << "Job::await: starting await() for a job...";
46 jobHandle->Wait();
47 ASSERT(!jobHandle->IsRunning());
48 LOG(DEBUG, COROUTINES) << "Job::await: await() finished.";
49
50 if (jobHandle->IsFinished()) {
51 LOG(DEBUG, COROUTINES) << "Job::await: job is already successfully finished!";
52 return jobHandle->GetValue(currentCoro);
53 }
54 LOG(DEBUG, COROUTINES) << "Job::await: job is failed!";
55
56 if (Runtime::GetOptions().IsListUnhandledOnExitJobs(plugins::LangToRuntimeType(panda_file::SourceLang::ETS))) {
57 currentCoro->GetPandaVM()->RemoveUnhandledFailedJob(jobHandle.GetPtr());
58 }
59
60 auto *exc = jobHandle->GetValue(currentCoro);
61 currentCoro->SetException(exc->GetCoreType());
62 return nullptr;
63 }
64
EtsFinishJob(EtsJob * job,EtsObject * value)65 void EtsFinishJob(EtsJob *job, EtsObject *value)
66 {
67 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
68 if (job == nullptr) {
69 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ETS);
70 ThrowNullPointerException(ctx, coro);
71 return;
72 }
73 [[maybe_unused]] EtsHandleScope scope(coro);
74 EtsHandle<EtsJob> hjob(coro, job);
75 EtsHandle<EtsObject> hvalue(coro, value);
76 EtsMutex::LockHolder lh(hjob);
77 if (!hjob->IsRunning()) {
78 return;
79 }
80 hjob->Finish(coro, hvalue.GetPtr());
81 }
82
EtsFailJob(EtsJob * job,EtsObject * error)83 void EtsFailJob(EtsJob *job, EtsObject *error)
84 {
85 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
86 if (job == nullptr) {
87 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ETS);
88 ThrowNullPointerException(ctx, coro);
89 return;
90 }
91 [[maybe_unused]] EtsHandleScope scope(coro);
92 EtsHandle<EtsJob> hjob(coro, job);
93 ASSERT(error != nullptr);
94 EtsHandle<EtsObject> herror(coro, error);
95 EtsMutex::LockHolder lh(hjob);
96
97 EtsClassLinker *classLinker = coro->GetPandaVM()->GetClassLinker();
98 auto *errorClass = classLinker->GetClass(panda_file_items::class_descriptors::ERROR.data());
99 if (!herror->IsInstanceOf(errorClass)) {
100 ThrowRuntimeException("fail() argument is not an Error object");
101 return;
102 }
103
104 if (!hjob->IsRunning()) {
105 return;
106 }
107 hjob->Fail(coro, herror.GetPtr());
108 }
109 }
110 } // namespace ark::ets::intrinsics
111