1 /**
2 * Copyright (c) 2022-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/include/managed_thread.h"
17 #include "plugins/ets/runtime/ani/ani_interaction_api.h"
18 #include "plugins/ets/runtime/ets_napi_env.h"
19 #include "plugins/ets/runtime/ets_coroutine.h"
20 #include "plugins/ets/runtime/ets_vm.h"
21 #include "plugins/ets/runtime/napi/ets_napi_native_interface.h"
22
23 namespace ark::ets {
Create(EtsCoroutine * coroutine,mem::InternalAllocatorPtr allocator)24 Expected<PandaEtsNapiEnv *, const char *> PandaEtsNapiEnv::Create(EtsCoroutine *coroutine,
25 mem::InternalAllocatorPtr allocator)
26 {
27 auto etsVm = coroutine->GetVM();
28 auto referenceStorage = MakePandaUnique<EtsReferenceStorage>(etsVm->GetGlobalObjectStorage(), allocator, false);
29 if (!referenceStorage || !referenceStorage->GetAsReferenceStorage()->Init()) {
30 return Unexpected("Cannot allocate EtsReferenceStorage");
31 }
32
33 auto *etsNapiEnv = allocator->New<PandaEtsNapiEnv>(coroutine, std::move(referenceStorage));
34 if (etsNapiEnv == nullptr) {
35 return Unexpected("Cannot allocate PandaEtsNapiEnv");
36 }
37
38 return Expected<PandaEtsNapiEnv *, const char *>(etsNapiEnv);
39 }
40
GetCurrent()41 PandaEtsNapiEnv *PandaEtsNapiEnv::GetCurrent()
42 {
43 auto *coro = EtsCoroutine::GetCurrent();
44 ASSERT(coro != nullptr);
45 return coro->GetEtsNapiEnv();
46 }
47
PandaEtsNapiEnv(EtsCoroutine * coroutine,PandaUniquePtr<EtsReferenceStorage> referenceStorage)48 PandaEtsNapiEnv::PandaEtsNapiEnv(EtsCoroutine *coroutine, PandaUniquePtr<EtsReferenceStorage> referenceStorage)
49 : ani_env {ani::GetInteractionAPI()},
50 EtsEnv {napi::GetNativeInterface()},
51 coroutine_(coroutine),
52 referenceStorage_(std::move(referenceStorage))
53 {
54 }
55
GetEtsVM() const56 PandaEtsVM *PandaEtsNapiEnv::GetEtsVM() const
57 {
58 return coroutine_->GetPandaVM();
59 }
60
FreeInternalMemory()61 void PandaEtsNapiEnv::FreeInternalMemory()
62 {
63 referenceStorage_.reset();
64 }
65
SetException(EtsThrowable * thr)66 void PandaEtsNapiEnv::SetException(EtsThrowable *thr)
67 {
68 ASSERT_MANAGED_CODE();
69 coroutine_->SetException(thr->GetCoreType());
70 }
71
GetThrowable() const72 EtsThrowable *PandaEtsNapiEnv::GetThrowable() const
73 {
74 ASSERT_MANAGED_CODE();
75 return reinterpret_cast<EtsThrowable *>(coroutine_->GetException());
76 }
77
HasPendingException() const78 bool PandaEtsNapiEnv::HasPendingException() const
79 {
80 return coroutine_->HasPendingException();
81 }
82
ClearException()83 void PandaEtsNapiEnv::ClearException()
84 {
85 ASSERT_MANAGED_CODE();
86 coroutine_->ClearException();
87 }
88
89 } // namespace ark::ets
90