1 /** 2 * Copyright (c) 2021-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_TYPEAPI_METHOD_H_ 17 #define PANDA_PLUGINS_ETS_TYPEAPI_METHOD_H_ 18 19 #include "plugins/ets/runtime/types/ets_object.h" 20 #include "plugins/ets/runtime/types/ets_string.h" 21 #include "types/ets_primitives.h" 22 #include "types/ets_typeapi.h" 23 24 namespace ark::ets { 25 26 class EtsCoroutine; 27 28 class EtsTypeAPIMethod : public ObjectHeader { 29 public: 30 EtsTypeAPIMethod() = delete; 31 ~EtsTypeAPIMethod() = delete; 32 33 NO_COPY_SEMANTIC(EtsTypeAPIMethod); 34 NO_MOVE_SEMANTIC(EtsTypeAPIMethod); 35 36 static EtsTypeAPIMethod *Create(EtsCoroutine *etsCoroutine = EtsCoroutine::GetCurrent()); 37 AsObject()38 EtsObject *AsObject() 39 { 40 return EtsObject::FromCoreType(this); 41 } 42 AsObject()43 const EtsObject *AsObject() const 44 { 45 return EtsObject::FromCoreType(this); 46 } 47 FromEtsObject(EtsObject * field)48 static EtsTypeAPIMethod *FromEtsObject(EtsObject *field) 49 { 50 return reinterpret_cast<EtsTypeAPIMethod *>(field); 51 } 52 SetTypeDesc(const char * td)53 void SetTypeDesc(const char *td) 54 { 55 auto coro = EtsCoroutine::GetCurrent(); 56 [[maybe_unused]] HandleScope<ObjectHeader *> scope {coro}; 57 VMHandle<EtsTypeAPIMethod> self {coro, this}; 58 // allocate beforehand to ensure order and keep self/this pointer valid in SetObject 59 auto val = EtsString::CreateFromMUtf8(td)->AsObject()->GetCoreType(); 60 ObjectAccessor::SetObject(self.GetPtr(), MEMBER_OFFSET(EtsTypeAPIMethod, td_), val); 61 } 62 SetName(EtsString * name)63 void SetName(EtsString *name) 64 { 65 ObjectAccessor::SetObject(this, MEMBER_OFFSET(EtsTypeAPIMethod, name_), name->AsObject()->GetCoreType()); 66 } 67 SetAccessMod(EtsTypeAPIAccessModifier accessMod)68 void SetAccessMod(EtsTypeAPIAccessModifier accessMod) 69 { 70 ObjectAccessor::SetPrimitive(this, MEMBER_OFFSET(EtsTypeAPIMethod, accessMod_), accessMod); 71 } 72 SetAttributes(EtsInt attr)73 void SetAttributes(EtsInt attr) 74 { 75 ObjectAccessor::SetPrimitive(this, MEMBER_OFFSET(EtsTypeAPIMethod, attr_), attr); 76 } 77 78 private: 79 ObjectPointer<EtsString> td_; 80 ObjectPointer<EtsString> name_; 81 FIELD_UNUSED EtsInt attr_; // note alignment 82 FIELD_UNUSED EtsByte accessMod_; 83 }; 84 85 } // namespace ark::ets 86 87 #endif // PANDA_PLUGINS_ETS_TYPEAPI_METHOD_H 88