• 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_ELEMENTS_H
17 #define ECMASCRIPT_ELEMENTS_H
18 
19 #include "ecmascript/global_env_constants.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/mem/c_containers.h"
22 
23 namespace panda::ecmascript {
24 enum class ElementsKind : uint8_t {
25     NONE = 0x00UL,
26     HOLE = 0x01UL,
27     INT = 0x1UL << 1,      // 2
28     NUMBER = (0x1UL << 2) | INT, // 6
29     STRING = 0x1UL << 3,   // 8
30     OBJECT = 0x1UL << 4,   // 16
31     TAGGED = 0x1EUL,       // 30
32     HOLE_INT = HOLE | INT,
33     HOLE_NUMBER = HOLE | NUMBER,
34     HOLE_STRING = HOLE | STRING,
35     HOLE_OBJECT = HOLE | OBJECT,
36     HOLE_TAGGED = HOLE | TAGGED,
37     GENERIC = HOLE_TAGGED,
38     DICTIONARY = HOLE_TAGGED,
39 };
40 
41 class Elements {
42 public:
43     static CMap<ElementsKind, ConstantIndex> InitializeHClassMap();
44 
45     static std::string GetString(ElementsKind kind);
46     static bool IsInt(ElementsKind kind);
47     static bool IsNumber(ElementsKind kind);
48     static bool IsTagged(ElementsKind kind);
49     static bool IsObject(ElementsKind kind);
50     static bool IsHole(ElementsKind kind);
IsGeneric(ElementsKind kind)51     static bool IsGeneric(ElementsKind kind)
52     {
53         return kind == ElementsKind::GENERIC;
54     }
55 
IsNone(ElementsKind kind)56     static bool IsNone(ElementsKind kind)
57     {
58         return kind == ElementsKind::NONE;
59     }
60 
IsComplex(ElementsKind kind)61     static bool IsComplex(ElementsKind kind)
62     {
63         return IsNumber(kind) || IsTagged(kind);
64     }
65 
IsInNumbers(ElementsKind kind)66     static bool IsInNumbers(ElementsKind kind)
67     {
68         return (static_cast<uint32_t>(kind) >= static_cast<uint32_t>(ElementsKind::HOLE) &&
69                 static_cast<uint32_t>(kind) < static_cast<uint32_t>(ElementsKind::STRING));
70     }
71 
72     static ElementsKind MergeElementsKind(ElementsKind curKind, ElementsKind newKind);
73     static ElementsKind FixElementsKind(ElementsKind oldKind);
74     static ElementsKind ToElementsKind(JSTaggedValue value, ElementsKind kind);
75     static void MigrateArrayWithKind(const JSThread *thread, const JSHandle<JSObject> &object,
76                                      const ElementsKind oldKind, const ElementsKind newKind);
77 };
78 }  // namespace panda::ecmascript
79 #endif // ECMASCRIPT_ELEMENTS_H
80