1 /* 2 * Copyright (c) 2021 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_RUNTIME_INCLUDE_HCLASS_H_ 17 #define PANDA_RUNTIME_INCLUDE_HCLASS_H_ 18 19 #include "runtime/include/class.h" 20 21 namespace panda { 22 23 namespace coretypes { 24 class DynClass; 25 } // namespace coretypes 26 27 // Class for objects in DYNAMIC_CLASS languages like JavaScript 28 class HClass : public BaseClass { 29 public: 30 static constexpr uint32_t HCLASS = 1U << 1U; 31 static constexpr uint32_t STRING = 1U << 2U; 32 static constexpr uint32_t ARRAY = 1U << 3U; 33 static constexpr uint32_t NATIVE_POINTER = 1U << 4U; 34 static constexpr uint32_t IS_DICTIONARY_ARRAY = 1U << 5U; 35 static constexpr uint32_t IS_BUILTINS_CTOR = 1U << 6U; 36 static constexpr uint32_t IS_CALLABLE = 1U << 7U; 37 38 static constexpr uint32_t BITS_SIZE = 8; 39 40 public: HClass(uint32_t flags,panda_file::SourceLang lang)41 HClass(uint32_t flags, panda_file::SourceLang lang) : BaseClass(lang) 42 { 43 SetFlags(flags | BaseClass::DYNAMIC_CLASS); 44 } 45 SetFlags(uint32_t flags)46 void SetFlags(uint32_t flags) 47 { 48 ASSERT(flags & BaseClass::DYNAMIC_CLASS); 49 BaseClass::SetFlags(flags); 50 } 51 IsNativePointer()52 inline bool IsNativePointer() const 53 { 54 return (GetFlags() & NATIVE_POINTER) != 0; 55 } 56 IsArray()57 inline bool IsArray() const 58 { 59 return (GetFlags() & ARRAY) != 0; 60 } 61 IsString()62 inline bool IsString() const 63 { 64 return (GetFlags() & STRING) != 0; 65 } 66 IsHClass()67 inline bool IsHClass() const 68 { 69 return (GetFlags() & HCLASS) != 0; 70 } 71 SetDictionary()72 void SetDictionary() 73 { 74 uint32_t flags = BaseClass::GetFlags() | IS_DICTIONARY_ARRAY; 75 ASSERT(flags & IS_DICTIONARY_ARRAY); 76 BaseClass::SetFlags(flags); 77 } 78 IsDictionary()79 bool IsDictionary() const 80 { 81 return (BaseClass::GetFlags() & IS_DICTIONARY_ARRAY) != 0U; 82 } 83 SetBuiltinsCtorMode()84 void SetBuiltinsCtorMode() 85 { 86 uint32_t flags = BaseClass::GetFlags() | IS_BUILTINS_CTOR; 87 ASSERT(flags & IS_BUILTINS_CTOR); 88 BaseClass::SetFlags(flags); 89 } 90 IsBuiltinsConstructor()91 bool IsBuiltinsConstructor() const 92 { 93 return (BaseClass::GetFlags() & IS_BUILTINS_CTOR) != 0U; 94 } 95 SetCallable(bool flag)96 void SetCallable(bool flag) 97 { 98 if (flag) { 99 uint32_t flags = BaseClass::GetFlags() | IS_CALLABLE; 100 ASSERT(flags & IS_CALLABLE); 101 BaseClass::SetFlags(flags); 102 } else { 103 uint32_t flags = BaseClass::GetFlags() & (~(IS_CALLABLE)); 104 ASSERT(!(flags & IS_CALLABLE)); 105 BaseClass::SetFlags(flags); 106 } 107 } 108 IsCallable()109 bool IsCallable() const 110 { 111 return (BaseClass::GetFlags() & IS_CALLABLE) != 0U; 112 } 113 114 ~HClass() = default; 115 116 DEFAULT_COPY_SEMANTIC(HClass); 117 DEFAULT_MOVE_SEMANTIC(HClass); 118 119 private: 120 friend class coretypes::DynClass; 121 }; 122 123 } // namespace panda 124 125 #endif // PANDA_RUNTIME_INCLUDE_HCLASS_H_ 126