1 /** 2 * Copyright (c) 2023-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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ETS_HANDLE_H_ 17 #define PANDA_PLUGINS_ETS_RUNTIME_ETS_HANDLE_H_ 18 19 #include "plugins/ets/runtime/ets_coroutine.h" 20 #include "plugins/ets/runtime/types/ets_object.h" 21 #include "runtime/mem/vm_handle.h" 22 #include "runtime/handle_scope-inl.h" 23 24 namespace ark::ets { 25 26 template <typename T> 27 class EtsHandle : public VMHandle<T> { 28 public: EtsHandle()29 inline explicit EtsHandle() : VMHandle<T>() {} EtsHandle(EtsCoroutine * coroutine,T * etsObj)30 explicit EtsHandle(EtsCoroutine *coroutine, T *etsObj) 31 : VMHandle<T>(ManagedThread::CastFromThread(coroutine), GetObjectHeader(etsObj)) 32 { 33 } 34 35 template <typename P> EtsHandle(const VMHandle<P> & other)36 inline explicit EtsHandle(const VMHandle<P> &other) : VMHandle<T>(other) 37 { 38 } 39 40 ~EtsHandle() = default; 41 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(EtsHandle); 42 DEFAULT_COPY_SEMANTIC(EtsHandle); 43 44 private: GetObjectHeader(T * etsObj)45 static constexpr ObjectHeader *GetObjectHeader(T *etsObj) 46 { 47 EtsObject *object = nullptr; 48 if constexpr (std::is_same_v<T, EtsObject>) { 49 object = etsObj; 50 } else { 51 object = etsObj != nullptr ? etsObj->AsObject() : nullptr; 52 } 53 return object != nullptr ? object->GetCoreType() : nullptr; 54 } 55 }; 56 57 } // namespace ark::ets 58 59 #endif // PANDA_PLUGINS_ETS_RUNTIME_ETS_HANDLE_H_ 60