1 /** 2 * Copyright (c) 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_TYPES_ETS_MODULE_H_ 17 #define PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_MODULE_H_ 18 19 #include "ani.h" 20 #include "plugins/ets/runtime/types/ets_class.h" 21 #include "plugins/ets/runtime/types/ets_variable.h" 22 23 namespace ark::ets { 24 25 class EtsModule : private EtsClass { 26 public: AsClass()27 EtsClass *AsClass() 28 { 29 return this; 30 } 31 FromClass(EtsClass * cls)32 static EtsModule *FromClass(EtsClass *cls) 33 { 34 // NODE: Check that class is module, when #22400 is resolved 35 return static_cast<EtsModule *>(cls); 36 } 37 GetVariabe(const char * name)38 EtsVariable *GetVariabe(const char *name) 39 { 40 EtsField *field = GetStaticFieldIDByName(name, nullptr); 41 return EtsVariable::FromField(field); 42 } 43 GetFunction(const char * name,const char * signature)44 EtsMethod *GetFunction(const char *name, const char *signature) 45 { 46 return GetStaticMethod(name, signature); 47 } 48 GetModulePrefix(PandaString & prefix)49 ani_status GetModulePrefix(PandaString &prefix) 50 { 51 static constexpr std::string_view SUFFIX = "/ETSGLOBAL;"; 52 const char *moduleDescriptor = AsClass()->GetDescriptor(); 53 size_t len = strlen(moduleDescriptor); 54 if (len < SUFFIX.size()) { 55 return ANI_INVALID_DESCRIPTOR; 56 } 57 std::string_view descriptorView(moduleDescriptor, len); 58 if (!std::equal(SUFFIX.rbegin(), SUFFIX.rend(), descriptorView.rbegin())) { 59 return ANI_INVALID_DESCRIPTOR; 60 } 61 prefix = PandaString(moduleDescriptor, len - SUFFIX.size()); 62 return ANI_OK; 63 } 64 }; 65 66 static_assert(sizeof(EtsModule) == sizeof(EtsClass)); 67 68 } // namespace ark::ets 69 70 #endif // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_MODULE_H_ 71