• 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         // prototypeHClass = HClass of X.prototype
44         JSHClass *prototypeHClass = nullptr;
45     };
46     Entry entries[N_ENTRIES];
47 
GetEntryIndexBuiltinHClassEntries48     static size_t GetEntryIndex(BuiltinTypeId type)
49     {
50         size_t res = static_cast<size_t>(type) - INDEX_OFFSET;
51         ASSERT(res < N_ENTRIES);
52         return res;
53     }
54 
EntryIsValidBuiltinHClassEntries55     size_t EntryIsValid(BuiltinTypeId type) const
56     {
57         size_t index = GetEntryIndex(type);
58         return entries[index].builtinHClass != nullptr && entries[index].prototypeHClass != nullptr;
59     }
60 
GetBuiltinHClassOffsetBuiltinHClassEntries61     static size_t GetBuiltinHClassOffset(BuiltinTypeId type)
62     {
63         size_t index = GetEntryIndex(type);
64         return sizeof(Entry) * index + MEMBER_OFFSET(Entry, builtinHClass);
65     }
66 
GetPrototypeHClassOffsetBuiltinHClassEntries67     static size_t GetPrototypeHClassOffset(BuiltinTypeId type)
68     {
69         size_t index = GetEntryIndex(type);
70         return sizeof(Entry) * index + MEMBER_OFFSET(Entry, prototypeHClass);
71     }
72 
73     static constexpr size_t SizeArch32 = sizeof(entries);
74     static constexpr size_t SizeArch64 = sizeof(entries);
75 
76 private:
77     static constexpr size_t INDEX_OFFSET = static_cast<size_t>(BuiltinTypeId::BUILTIN_OFFSET) + 1;
78 };
79 STATIC_ASSERT_EQ_ARCH(sizeof(BuiltinHClassEntries),
80     BuiltinHClassEntries::SizeArch32,
81     BuiltinHClassEntries::SizeArch64);
82 } // namespace panda::ecmascript
83 
84 #endif // ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H
85