1 /** 2 * Copyright (c) 2024-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_STUBS_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_STUBS_H 18 19 #include <cstdint> 20 #include "plugins/ets/runtime/types/ets_bigint.h" 21 22 namespace ark::ets { 23 24 constexpr static uint64_t METHOD_FLAG_MASK = 0x00000001; 25 26 class EtsCoroutine; 27 class EtsObject; 28 class EtsClass; 29 class EtsString; 30 31 // Generic comparator for ets reference types 32 template <bool IS_STRICT = false> 33 inline bool EtsReferenceEquals(EtsCoroutine *coro, EtsObject *ref1, EtsObject *ref2); 34 35 inline bool EtsReferenceNullish(EtsCoroutine *coro, EtsObject *ref); 36 37 // Comparison slowpath for value-typed references 38 bool EtsValueTypedEquals(EtsCoroutine *coro, EtsObject *obj1, EtsObject *obj2); 39 40 bool EtsBigIntEquality(EtsBigInt *obj1, EtsBigInt *obj2); 41 42 // Obtain owner class of method in ets frames 43 inline EtsClass *GetMethodOwnerClassInFrames(EtsCoroutine *coro, uint32_t depth); 44 45 // Compute typeof 46 EtsString *EtsGetTypeof(EtsCoroutine *coro, EtsObject *obj); 47 48 bool EtsGetIstrue(EtsCoroutine *coro, EtsObject *obj); 49 50 template <bool IS_GETTER> 51 inline void LookUpException(ark::Class *klass, Field *rawField); 52 53 inline void LookUpException(ark::Class *klass, ark::Method *rawMethod); 54 55 template <bool IS_GETTER = true> 56 inline Field *GetFieldByName(InterpreterCache::Entry *entry, ark::Method *method, Field *rawField, 57 const uint8_t *address, ark::Class *klass); 58 59 template <panda_file::Type::TypeId FIELD_TYPE, bool IS_GETTER> 60 inline ark::Method *GetAccessorByName(InterpreterCache::Entry *entry, ark::Method *method, Field *rawField, 61 const uint8_t *address, ark::Class *klass); 62 63 inline Field *LookupFieldByName(panda_file::File::StringData name, const ark::Class *klass); 64 65 template <panda_file::Type::TypeId FIELD_TYPE> 66 inline ark::Method *LookupGetterByName(panda_file::File::StringData name, const ark::Class *klass); 67 68 template <panda_file::Type::TypeId FIELD_TYPE> 69 inline ark::Method *LookupSetterByName(panda_file::File::StringData name, const ark::Class *klass); 70 71 struct ETSStubCacheInfo { 72 public: ETSStubCacheInfoETSStubCacheInfo73 ETSStubCacheInfo(InterpreterCache::Entry *entry, Method *instMethod, const uint8_t *instAddress) 74 : entry_(entry), instMethod_(instMethod), instAddress_(instAddress) 75 { 76 } 77 GetItemETSStubCacheInfo78 void *GetItem() const 79 { 80 return entry_->item; 81 } 82 UpdateItemETSStubCacheInfo83 void UpdateItem(void *value) const 84 { 85 auto mUint = reinterpret_cast<uint64_t>(value); 86 *entry_ = {instAddress_, instMethod_, reinterpret_cast<Method *>(mUint | METHOD_FLAG_MASK)}; 87 } 88 89 private: 90 InterpreterCache::Entry *entry_ {}; 91 Method *instMethod_ {}; 92 const uint8_t *instAddress_ {}; 93 }; 94 95 Method *GetMethodByName(EtsCoroutine *coro, ETSStubCacheInfo const &cache, Method *rawMethod, ark::Class *klass); 96 } // namespace ark::ets 97 98 #endif // PANDA_PLUGINS_ETS_RUNTIME_STUBS_H 99