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 ECMASCRIPT_GLOBAL_ENV_CONSTANTS_INL_H
17#define ECMASCRIPT_GLOBAL_ENV_CONSTANTS_INL_H
18
19#include "ecmascript/global_env_constants.h"
20
21#include "ecmascript/ecma_macros.h"
22#include "ecmascript/js_handle.h"
23
24namespace panda::ecmascript {
25inline const JSTaggedValue *GlobalEnvConstants::BeginSlot() const
26{
27    return &constants_[static_cast<int>(ConstantIndex::CONSTANT_BEGIN)];
28}
29
30inline const JSTaggedValue *GlobalEnvConstants::EndSlot() const
31{
32    return &constants_[static_cast<int>(ConstantIndex::CONSTANT_END)];
33}
34
35inline void GlobalEnvConstants::SetConstant(ConstantIndex index, JSTaggedValue value)
36{
37    DASSERT_PRINT(index >= ConstantIndex::CONSTANT_BEGIN && index < ConstantIndex::CONSTANT_END,
38                  "Root Index out of bound");
39    constants_[static_cast<int>(index)] = value;
40}
41
42template<typename T>
43inline void GlobalEnvConstants::SetConstant(ConstantIndex index, JSHandle<T> value)
44{
45    DASSERT_PRINT(index >= ConstantIndex::CONSTANT_BEGIN && index < ConstantIndex::CONSTANT_END,
46                  "Root Index out of bound");
47    constants_[static_cast<int>(index)] = value.GetTaggedValue();
48}
49
50inline uintptr_t GlobalEnvConstants::GetGlobalConstantAddr(ConstantIndex index) const
51{
52    return ToUintPtr(this) + sizeof(JSTaggedValue) * static_cast<uint32_t>(index);
53}
54
55// clang-format off
56// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
57#define DECL_GET_IMPL_COMMON(Type, Name, Index)                              \
58    inline const Type GlobalEnvConstants::Get##Name() const                  \
59    {                                                                        \
60        return constants_[static_cast<int>(ConstantIndex::Index)];           \
61    }                                                                        \
62    inline const JSHandle<Type> GlobalEnvConstants::GetHandled##Name() const \
63    {                                                                        \
64        return JSHandle<Type>(reinterpret_cast<uintptr_t>(                   \
65            &constants_[static_cast<int>(ConstantIndex::Index)]));           \
66    }                                                                        \
67    inline size_t GlobalEnvConstants::GetOffsetOf##Name()                    \
68    {                                                                        \
69        return sizeof(JSTaggedValue)                                         \
70            * static_cast<int>(ConstantIndex::Index);                        \
71    }
72
73#define DECL_GET_IMPL_STRING(Name, Index, Token) DECL_GET_IMPL_COMMON(JSTaggedValue, Name, Index)
74#define DECL_GET_IMPL_WITH_TYPE(Type, Name, Index, Desc) DECL_GET_IMPL_COMMON(Type, Name, Index)
75    SHARED_GLOBAL_ENV_CONSTANT_CLASS(DECL_GET_IMPL_WITH_TYPE)     // NOLINT(readability-const-return-type)
76    SHARED_GLOBAL_ENV_CONSTANT_STRING(DECL_GET_IMPL_STRING)       // NOLINT(readability-const-return-type)
77    SHARED_GLOBAL_ENV_CONSTANT_ACCESSOR(DECL_GET_IMPL_WITH_TYPE)  // NOLINT(readability-const-return-type)
78    SHARED_GLOBAL_ENV_CONSTANT_SPECIAL(DECL_GET_IMPL_WITH_TYPE);  // NOLINT(readability-const-return-type)
79    GLOBAL_ENV_CONSTANT_CLASS(DECL_GET_IMPL_WITH_TYPE)            // NOLINT(readability-const-return-type)
80    GLOBAL_ENV_CONSTANT_SPECIAL(DECL_GET_IMPL_WITH_TYPE)          // NOLINT(readability-const-return-type)
81    GLOBAL_ENV_INLINED_BUILTINS(DECL_GET_IMPL_WITH_TYPE)          // NOLINT(readability-const-return-type)
82    GLOBAL_ENV_CONSTANT_CONSTANT(DECL_GET_IMPL_WITH_TYPE)         // NOLINT(readability-const-return-type)
83    GLOBAL_ENV_CACHES(DECL_GET_IMPL_WITH_TYPE)                    // NOLINT(readability-const-return-type)
84#undef DECL_GET_IMPL_WITH_TYPE
85#undef DECL_GET_IMPL_STRING
86#undef DECL_GET_IMPL_COMMON
87// clang-format on
88}  // namespace panda::ecmascript
89#endif  // ECMASCRIPT_GLOBAL_ENV_CONSTANTS_INL_H
90