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