1 /* 2 * Copyright (c) 2023 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 ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H 17 #define ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H 18 19 #include <array> 20 #include <string_view> 21 22 #include "ecmascript/common.h" 23 #include "ecmascript/ts_types/builtin_type_id.h" 24 25 namespace panda::ecmascript { 26 class GlobalEnv; 27 class JSHClass; 28 29 // Records initial HClass of builtin objects. 30 // Let X be the builtin object (e.g. X = Array, object, etc.), 31 // HClass address mismatch happens if properties in X or X.prototype are modified (with HClass change), 32 // thus the record entries are useful to detect builtin object modification. 33 struct BuiltinHClassEntries { 34 #define TS_BUILTIN_TYPE_ITEM(TYPE) BuiltinTypeId::TYPE, 35 static constexpr std::array BUILTIN_TYPES = { 36 TS_BUILTIN_TYPE_LIST(TS_BUILTIN_TYPE_ITEM) 37 }; 38 static constexpr size_t N_ENTRIES = static_cast<size_t>(BuiltinTypeId::NUM_OF_BUILTIN_TYPES); 39 #undef TS_BUILTIN_TYPE_ITEM 40 41 struct Entry { 42 // Let X be the builtin object (e.g. X = Array, Object, etc.) 43 // builtinHClass = HClass of X 44 JSHClass *builtinHClass = nullptr; 45 // Let x be the builtin instance object (e.g. x = new Array(), new Object(), etc.) 46 // instanceHClass = HClass of x 47 JSHClass *instanceHClass = nullptr; 48 // prototypeHClass = HClass of X.prototype 49 JSHClass *prototypeHClass = nullptr; 50 // prototypeOfPrototypeHClass = HClass of X.prototype.prototype 51 JSHClass *prototypeOfPrototypeHClass = nullptr; 52 //extraHClass . In typedArray means instanceHClassOnHeap 53 JSHClass *extraHClass = nullptr; 54 }; 55 Entry entries[N_ENTRIES]; 56 GetEntryIndexBuiltinHClassEntries57 static size_t GetEntryIndex(BuiltinTypeId type) 58 { 59 size_t res = static_cast<size_t>(type) - INDEX_OFFSET; 60 ASSERT(res < N_ENTRIES); 61 return res; 62 } 63 EntryIsValidBuiltinHClassEntries64 size_t EntryIsValid(BuiltinTypeId type) const 65 { 66 size_t index = GetEntryIndex(type); 67 return entries[index].builtinHClass != nullptr && entries[index].prototypeHClass != nullptr; 68 } 69 GetBuiltinHClassOffsetBuiltinHClassEntries70 static size_t GetBuiltinHClassOffset(BuiltinTypeId type) 71 { 72 size_t index = GetEntryIndex(type); 73 return sizeof(Entry) * index + MEMBER_OFFSET(Entry, builtinHClass); 74 } 75 GetInstanceHClassOffsetBuiltinHClassEntries76 static size_t GetInstanceHClassOffset(BuiltinTypeId type) 77 { 78 size_t index = GetEntryIndex(type); 79 return sizeof(Entry) * index + MEMBER_OFFSET(Entry, instanceHClass); 80 } 81 GetExtraHClassOffsetBuiltinHClassEntries82 static size_t GetExtraHClassOffset(BuiltinTypeId type) 83 { 84 size_t index = GetEntryIndex(type); 85 return sizeof(Entry) * index + MEMBER_OFFSET(Entry, extraHClass); 86 } 87 GetPrototypeHClassOffsetBuiltinHClassEntries88 static size_t GetPrototypeHClassOffset(BuiltinTypeId type) 89 { 90 size_t index = GetEntryIndex(type); 91 return sizeof(Entry) * index + MEMBER_OFFSET(Entry, prototypeHClass); 92 } 93 GetPrototypeOfPrototypeHClassOffsetBuiltinHClassEntries94 static size_t GetPrototypeOfPrototypeHClassOffset(BuiltinTypeId type) 95 { 96 size_t index = GetEntryIndex(type); 97 return sizeof(Entry) * index + MEMBER_OFFSET(Entry, prototypeOfPrototypeHClass); 98 } 99 100 static constexpr size_t SizeArch32 = sizeof(entries); 101 static constexpr size_t SizeArch64 = sizeof(entries); 102 103 private: 104 static constexpr size_t INDEX_OFFSET = static_cast<size_t>(BuiltinTypeId::BUILTIN_OFFSET) + 1; 105 }; 106 STATIC_ASSERT_EQ_ARCH(sizeof(BuiltinHClassEntries), 107 BuiltinHClassEntries::SizeArch32, 108 BuiltinHClassEntries::SizeArch64); 109 } // namespace panda::ecmascript 110 111 #endif // ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H 112