• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2024 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/ets_napi_env.h"
18 #include "plugins/ets/runtime/ets_coroutine.h"
19 #include "plugins/ets/runtime/ets_vm.h"
20 #include "plugins/ets/runtime/napi/ets_napi_native_interface.h"
21 
22 namespace ark::ets {
Create(EtsCoroutine * coroutine,mem::InternalAllocatorPtr allocator)23 Expected<std::unique_ptr<PandaEtsNapiEnv>, const char *> PandaEtsNapiEnv::Create(EtsCoroutine *coroutine,
24                                                                                  mem::InternalAllocatorPtr allocator)
25 {
26     auto etsVm = coroutine->GetVM();
27     auto referenceStorage = MakePandaUnique<EtsReferenceStorage>(etsVm->GetGlobalObjectStorage(), allocator, false);
28     if (!referenceStorage || !referenceStorage->GetAsReferenceStorage()->Init()) {
29         return Unexpected("Cannot allocate EtsReferenceStorage");
30     }
31 
32     // Do not use PandaUniquePtr here as the environment could be accessed from daemon threads after destroy of runtime
33     auto etsNapiEnv = std::make_unique<PandaEtsNapiEnv>(coroutine, std::move(referenceStorage));
34     if (etsNapiEnv.get() == nullptr) {
35         return Unexpected("Cannot allocate PandaEtsNapiEnv");
36     }
37 
38     return Expected<std::unique_ptr<PandaEtsNapiEnv>, const char *>(std::move(etsNapiEnv));
39 }
40 
GetCurrent()41 PandaEtsNapiEnv *PandaEtsNapiEnv::GetCurrent()
42 {
43     return EtsCoroutine::GetCurrent()->GetEtsNapiEnv();
44 }
45 
PandaEtsNapiEnv(EtsCoroutine * coroutine,PandaUniquePtr<EtsReferenceStorage> referenceStorage)46 PandaEtsNapiEnv::PandaEtsNapiEnv(EtsCoroutine *coroutine, PandaUniquePtr<EtsReferenceStorage> referenceStorage)
47     : EtsEnv {napi::GetNativeInterface()}, coroutine_(coroutine), referenceStorage_(std::move(referenceStorage))
48 {
49 }
50 
GetEtsVM() const51 PandaEtsVM *PandaEtsNapiEnv::GetEtsVM() const
52 {
53     return coroutine_->GetPandaVM();
54 }
55 
FreeInternalMemory()56 void PandaEtsNapiEnv::FreeInternalMemory()
57 {
58     referenceStorage_.reset();
59 }
60 
SetException(EtsThrowable * thr)61 void PandaEtsNapiEnv::SetException(EtsThrowable *thr)
62 {
63     ASSERT_MANAGED_CODE();
64     coroutine_->SetException(thr->GetCoreType());
65 }
66 
GetThrowable() const67 EtsThrowable *PandaEtsNapiEnv::GetThrowable() const
68 {
69     ASSERT_MANAGED_CODE();
70     return reinterpret_cast<EtsThrowable *>(coroutine_->GetException());
71 }
72 
HasPendingException() const73 bool PandaEtsNapiEnv::HasPendingException() const
74 {
75     return coroutine_->HasPendingException();
76 }
77 
ClearException()78 void PandaEtsNapiEnv::ClearException()
79 {
80     ASSERT_MANAGED_CODE();
81     coroutine_->ClearException();
82 }
83 
84 }  // namespace ark::ets