14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <string_view>
204514f5e3Sopenharmony_ci#include "ecmascript/common.h"
214514f5e3Sopenharmony_ci#include "ecmascript/ts_types/builtin_type_id.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::ecmascript {
244514f5e3Sopenharmony_ciclass GlobalEnv;
254514f5e3Sopenharmony_ciclass JSHClass;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ci// Records initial HClass of builtin objects.
284514f5e3Sopenharmony_ci// Let X be the builtin object (e.g. X = Array, object, etc.),
294514f5e3Sopenharmony_ci// HClass address mismatch happens if properties in X or X.prototype are modified (with HClass change),
304514f5e3Sopenharmony_ci// thus the record entries are useful to detect builtin object modification.
314514f5e3Sopenharmony_cistruct BuiltinHClassEntries {
324514f5e3Sopenharmony_ci#define TS_BUILTIN_TYPE_ITEM(TYPE) BuiltinTypeId::TYPE,
334514f5e3Sopenharmony_ci    static constexpr std::array BUILTIN_TYPES = {
344514f5e3Sopenharmony_ci        TS_BUILTIN_TYPE_LIST(TS_BUILTIN_TYPE_ITEM)
354514f5e3Sopenharmony_ci    };
364514f5e3Sopenharmony_ci    static constexpr size_t N_ENTRIES = static_cast<size_t>(BuiltinTypeId::NUM_OF_BUILTIN_TYPES);
374514f5e3Sopenharmony_ci#undef TS_BUILTIN_TYPE_ITEM
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ci    struct Entry {
404514f5e3Sopenharmony_ci        // Let X be the builtin object (e.g. X = Array, Object, etc.)
414514f5e3Sopenharmony_ci        // builtinHClass = HClass of X
424514f5e3Sopenharmony_ci        JSHClass *builtinHClass = nullptr;
434514f5e3Sopenharmony_ci        // Let x be the builtin instance object (e.g. x = new Array(), new Object(), etc.)
444514f5e3Sopenharmony_ci        // instanceHClass = HClass of x
454514f5e3Sopenharmony_ci        JSHClass *instanceHClass = nullptr;
464514f5e3Sopenharmony_ci        // prototypeHClass = HClass of X.prototype
474514f5e3Sopenharmony_ci        JSHClass *prototypeHClass = nullptr;
484514f5e3Sopenharmony_ci        // prototypeOfPrototypeHClass = HClass of X.prototype.prototype
494514f5e3Sopenharmony_ci        JSHClass *prototypeOfPrototypeHClass = nullptr;
504514f5e3Sopenharmony_ci        //extraHClass . In typedArray means instanceHClassOnHeap
514514f5e3Sopenharmony_ci        JSHClass *extraHClass = nullptr;
524514f5e3Sopenharmony_ci    };
534514f5e3Sopenharmony_ci    Entry entries[N_ENTRIES];
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    static size_t GetEntryIndex(BuiltinTypeId type)
564514f5e3Sopenharmony_ci    {
574514f5e3Sopenharmony_ci        size_t res = static_cast<size_t>(type) - INDEX_OFFSET;
584514f5e3Sopenharmony_ci        ASSERT(res < N_ENTRIES);
594514f5e3Sopenharmony_ci        return res;
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    size_t EntryIsValid(BuiltinTypeId type) const
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
654514f5e3Sopenharmony_ci        return entries[index].builtinHClass != nullptr && entries[index].prototypeHClass != nullptr;
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    static size_t GetBuiltinHClassOffset(BuiltinTypeId type)
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
714514f5e3Sopenharmony_ci        return sizeof(Entry) * index + MEMBER_OFFSET(Entry, builtinHClass);
724514f5e3Sopenharmony_ci    }
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    static size_t GetInstanceHClassOffset(BuiltinTypeId type)
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
774514f5e3Sopenharmony_ci        return sizeof(Entry) * index + MEMBER_OFFSET(Entry, instanceHClass);
784514f5e3Sopenharmony_ci    }
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    static size_t GetExtraHClassOffset(BuiltinTypeId type)
814514f5e3Sopenharmony_ci    {
824514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
834514f5e3Sopenharmony_ci        return sizeof(Entry) * index + MEMBER_OFFSET(Entry, extraHClass);
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    static size_t GetPrototypeHClassOffset(BuiltinTypeId type)
874514f5e3Sopenharmony_ci    {
884514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
894514f5e3Sopenharmony_ci        return sizeof(Entry) * index + MEMBER_OFFSET(Entry, prototypeHClass);
904514f5e3Sopenharmony_ci    }
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ci    static size_t GetPrototypeOfPrototypeHClassOffset(BuiltinTypeId type)
934514f5e3Sopenharmony_ci    {
944514f5e3Sopenharmony_ci        size_t index = GetEntryIndex(type);
954514f5e3Sopenharmony_ci        return sizeof(Entry) * index + MEMBER_OFFSET(Entry, prototypeOfPrototypeHClass);
964514f5e3Sopenharmony_ci    }
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci    static constexpr size_t SizeArch32 = sizeof(entries);
994514f5e3Sopenharmony_ci    static constexpr size_t SizeArch64 = sizeof(entries);
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ciprivate:
1024514f5e3Sopenharmony_ci    static constexpr size_t INDEX_OFFSET = static_cast<size_t>(BuiltinTypeId::BUILTIN_OFFSET) + 1;
1034514f5e3Sopenharmony_ci};
1044514f5e3Sopenharmony_ciSTATIC_ASSERT_EQ_ARCH(sizeof(BuiltinHClassEntries),
1054514f5e3Sopenharmony_ci    BuiltinHClassEntries::SizeArch32,
1064514f5e3Sopenharmony_ci    BuiltinHClassEntries::SizeArch64);
1074514f5e3Sopenharmony_ci} // namespace panda::ecmascript
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci#endif // ECMASCRIPT_JS_THREAD_HCLASS_ENTRIES_H
110