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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ETS_NAPI_ENV_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_ETS_NAPI_ENV_H 18 19 #include "plugins/ets/runtime/ani/ani.h" 20 #include "plugins/ets/runtime/mem/ets_reference.h" 21 #include "plugins/ets/runtime/napi/ets_napi.h" 22 23 namespace ark::ets { 24 25 class EtsCoroutine; 26 class PandaEtsVM; 27 using EtsThrowable = EtsObject; 28 29 class PandaEtsNapiEnv : public ani_env, public EtsEnv { // NOLINT(fuchsia-multiple-inheritance) 30 public: 31 static Expected<PandaEtsNapiEnv *, const char *> Create(EtsCoroutine *coroutine, 32 mem::InternalAllocatorPtr allocator); 33 PANDA_PUBLIC_API static PandaEtsNapiEnv *GetCurrent(); 34 35 PandaEtsNapiEnv(EtsCoroutine *coroutine, PandaUniquePtr<EtsReferenceStorage> referenceStorage); 36 ~PandaEtsNapiEnv() = default; 37 GetEtsCoroutine()38 EtsCoroutine *GetEtsCoroutine() const 39 { 40 return coroutine_; 41 } 42 43 PandaEtsVM *GetEtsVM() const; 44 GetEtsReferenceStorage()45 EtsReferenceStorage *GetEtsReferenceStorage() const 46 { 47 return referenceStorage_.get(); 48 } 49 ToPandaEtsEnv(EtsEnv * env)50 static PandaEtsNapiEnv *ToPandaEtsEnv(EtsEnv *env) 51 { 52 return static_cast<PandaEtsNapiEnv *>(env); 53 } 54 FromEtsEnv(EtsEnv * env)55 static PandaEtsNapiEnv *FromEtsEnv(EtsEnv *env) 56 { 57 return static_cast<PandaEtsNapiEnv *>(env); 58 } 59 FromAniEnv(ani_env * env)60 static PandaEtsNapiEnv *FromAniEnv(ani_env *env) 61 { 62 return static_cast<PandaEtsNapiEnv *>(env); 63 } 64 65 void SetException(EtsThrowable *thr); 66 EtsThrowable *GetThrowable() const; 67 bool HasPendingException() const; 68 void ClearException(); 69 void FreeInternalMemory(); 70 71 NO_COPY_SEMANTIC(PandaEtsNapiEnv); 72 NO_MOVE_SEMANTIC(PandaEtsNapiEnv); 73 74 private: 75 EtsCoroutine *coroutine_; 76 PandaUniquePtr<EtsReferenceStorage> referenceStorage_; 77 }; 78 79 using PandaEnv = PandaEtsNapiEnv; 80 81 } // namespace ark::ets 82 83 #endif // PANDA_PLUGINS_ETS_RUNTIME_ETS_NAPI_ENV_H 84