1/*
2 * Copyright (c) 2021-2024 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_MESSAGE_STRING_H
17#define ECMASCRIPT_MESSAGE_STRING_H
18
19#include <string>
20
21#include "ecmascript/compiler/common_stub_csigns.h"
22#include "ecmascript/compiler/interpreter_stub.h"
23
24namespace panda::ecmascript {
25// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
26#define COMMON_MESSAGE_STRING_LIST(V)                                                        \
27    V(SetReadOnlyProperty, "Cannot assign to read only property")                            \
28    V(SetTypeMismatchedSharedProperty, "Cannot set sendable property with mismatched type")  \
29    V(CreateObjectWithSendableProto, "Cannot create object with sendable proto")             \
30    V(UpdateSendableAttributes, "Cannot update sendable object's attributes")                \
31    V(SetProtoWithSendable, "Cannot set proto with sendable object")                         \
32    V(ClassNotDerivedFromShared, "Class not derived from a sendable object")                 \
33    V(NotSendableSubClass, "The subclass of sendable class must be a sendable class")        \
34    V(FunctionCallNotConstructor, "class constructor cannot call")                           \
35    V(SetPropertyWhenNotExtensible, "Cannot add property in prevent extensions")             \
36    V(GetPropertyOutOfBounds, "Get Property index out-of-bounds")                            \
37    V(CanNotSetPropertyOnContainer, "Cannot set property on Container")                      \
38    V(NonCallable, "CallObj is NonCallable")                                                 \
39    V(ASM_INTERPRETER_STUB_NAME, "ASM_INTERPRETER stub name: ")                              \
40    V(OPCODE_OVERFLOW, "opcode overflow!")                                                   \
41    V(INT32_VALUE, "value: %ld")                                                             \
42    V(TargetTypeNotObject, "Type of target is not Object")                                   \
43    V(TargetTypeNotTypedArray, "The O is not a TypedArray.")                                 \
44    V(CanNotGetNotEcmaObject, "Can not get Prototype on non ECMA Object")                    \
45    V(SendableArrayForJson, "Array not supported for SENDABLE_JSON")                         \
46    V(InstanceOfErrorTargetNotCallable, "InstanceOf error when target is not Callable")      \
47    V(ApplyTargetNotCallable, "apply target is not callable")                                \
48    V(TargetNotStableJSArray, "target not stable JSArray")                                   \
49    V(LenGreaterThanMax, "len is bigger than 2^32 - 1")                                      \
50    V(ElementTypeNoElementTypes, "CreateListFromArrayLike: not an element of elementTypes")  \
51    V(TargetIsDetachedBuffer, "Is Detached Buffer")                                          \
52    V(ThisBranchIsUnreachable, "this branch is unreachable")                                 \
53    V(CanNotConvertNotUndefinedObject, "Cannot convert a UNDEFINED value to a JSObject")     \
54    V(CanNotConvertNotNullObject, "Cannot convert a NULL value to a JSObject")               \
55    V(CanNotConvertNotHoleObject, "Cannot convert a HOLE value to a JSObject")               \
56    V(CanNotConvertUnknowObject, "Cannot convert a Unknown object value to a JSObject")      \
57    V(CanNotConvertNotValidObject, "Obj is not a valid object")                              \
58    V(CanNotConvertContainerObject, "Can not delete property in Container Object")           \
59    V(InvalidStringLength, "Invalid string length")                                          \
60    V(InvalidNewTarget, "new.target is not an object")                                       \
61    V(ObjIsNotCallable, "obj is not Callable")                                               \
62    V(SharedObjectRefersLocalObject, "shared object refers a local object")                  \
63    V(InvalidRadixLength, "radix must be 2 to 36")                                           \
64    V(SetPrototypeOfFailed, "SetPrototypeOf: prototype set failed")                          \
65    V(ReviverOnlySupportUndefined, "reviver only supports undefined for SENDABLE_JSON")      \
66    V(DefineFieldField, "DefineField: obj is not Object")                                    \
67    V(IsNotPropertyKey, "key is not a property key")                                         \
68    V(CreateDataPropertyFailed, "failed to create data property")                            \
69    V(ValueIsNonSObject, "value is not a shared object")
70
71#define DEBUG_CHECK_MESSAGE_STRING_LIST(V)                                                   \
72    V(IsCallable)                                                                            \
73    V(LoadHClass)                                                                            \
74    V(IsDictionaryMode)                                                                      \
75    V(IsClassConstructor)                                                                    \
76    V(IsClassPrototype)                                                                      \
77    V(IsExtensible)                                                                          \
78    V(IsEcmaObject)                                                                          \
79    V(IsJSObject)                                                                            \
80    V(IsString)                                                                              \
81    V(IsJSHClass)                                                                            \
82    V(IsNotDictionaryMode)                                                                   \
83    V(InitializeWithSpeicalValue)                                                            \
84    V(IsSendableFunctionModule)
85
86class MessageString {
87public:
88    enum MessageId {
89    // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
90#define DEF_MESSAGE_ID(name, string) Message_##name,
91        COMMON_MESSAGE_STRING_LIST(DEF_MESSAGE_ID)
92#undef DEF_MESSAGE_ID
93#define DEF_MESSAGE_ID(name) Message_##name,
94        ASM_INTERPRETER_BC_STUB_LIST(DEF_MESSAGE_ID, DEF_MESSAGE_ID, DEF_MESSAGE_ID)
95        ASM_INTERPRETER_SECOND_BC_STUB_ID_LIST(DEF_MESSAGE_ID)
96        ASM_INTERPRETER_BC_HELPER_STUB_LIST(DEF_MESSAGE_ID)
97#define DEF_MESSAGE_ID_DYN(name, ...) DEF_MESSAGE_ID(name)
98        ASM_INTERPRETER_BC_PROFILER_STUB_LIST(DEF_MESSAGE_ID_DYN)
99#undef DEF_MESSAGE_ID_DYN
100#define DEF_BUILTINS_STUB_MESSAGE_ID(name, type, ...) Message_##type##name,
101        BUILTINS_STUB_LIST(DEF_MESSAGE_ID, DEF_BUILTINS_STUB_MESSAGE_ID, DEF_MESSAGE_ID)
102#undef DEF_BUILTINS_STUB_MESSAGE_ID
103        RUNTIME_ASM_STUB_LIST(DEF_MESSAGE_ID)
104        DEBUG_CHECK_MESSAGE_STRING_LIST(DEF_MESSAGE_ID)
105#undef DEF_MESSAGE_ID
106        MAX_MESSAGE_COUNT,
107        ASM_INTERPRETER_START = Message_INT32_VALUE + 1,
108        BUILTINS_STUB_START = Message_StringCharCodeAt,
109        BUILTINS_STUB_LAST = Message_ArrayConstructor,
110    };
111    static const std::string& PUBLIC_API GetMessageString(int id);
112
113    static bool IsBuiltinsStubMessageString(int id)
114    {
115        return (id >= BUILTINS_STUB_START) && (id <= BUILTINS_STUB_LAST);
116    }
117};
118
119// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
120#define GET_MESSAGE_STRING_ID(name) static_cast<int>((MessageString::MessageId::Message_##name))
121#define GET_MESSAGE_STRING(name)  MessageString::GetMessageString(GET_MESSAGE_STRING_ID(name)).c_str()
122}  // namespace panda::ecmascript
123#endif  // ECMASCRIPT_MESSAGE_STRING_H
124