• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_JSPANDAFILE_CLASS_INFO_EXTRACTOR_H
17 #define ECMASCRIPT_JSPANDAFILE_CLASS_INFO_EXTRACTOR_H
18 #include <vector>
19 #include "ecmascript/js_tagged_value-inl.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/js_tagged_value_internals.h"
22 #include "ecmascript/jspandafile/method_literal.h"
23 #include "ecmascript/property_attributes.h"
24 
25 namespace panda::ecmascript {
26 // ClassInfoExtractor will analyze and extract the contents from class literal to keys, properties and elements(both
27 // non-static and static), later generate the complete hclass (both prototype and constructor) based on keys.
28 // Attention: keys accessor stores the property key and properties accessor stores the property value, but elements
29 // accessor stores the key-value pair abuttally.
30 using EntityId = panda_file::File::EntityId;
31 enum class FieldType {
32     NONE = 0,
33     NUMBER = (1 << 0),
34     STRING = (1 << 1),
35     BOOLEAN = (1 << 2),
36     TS_TYPE_REF = (1 << 3),
37     BIG_INT = (1 << 4),
38     GENERIC = (1 << 5),
39     NULL_TYPE = (1 << 6),
40     UNDEFINED = (1 << 7),
41 };
42 class ClassInfoExtractor : public TaggedObject {
43 public:
44     static constexpr uint8_t NON_STATIC_RESERVED_LENGTH = 1;
45     static constexpr uint8_t STATIC_RESERVED_LENGTH = 3;
46     static constexpr uint8_t SENDABLE_STATIC_RESERVED_LENGTH = 4; // add elements for sendable class
47 
48     static constexpr uint8_t CONSTRUCTOR_INDEX = 0;
49     static constexpr uint8_t LENGTH_INDEX = 0;
50     static constexpr uint8_t NAME_INDEX = 1;
51     static constexpr uint8_t PROTOTYPE_INDEX = 2;
52     static constexpr uint8_t SENDABLE_ELEMENTS_INDEX = 3;
53 
54     struct ExtractContentsDetail {
55         uint32_t extractBegin;
56         uint32_t extractEnd;
57         uint8_t fillStartLoc;
58         MethodLiteral *methodLiteral;
59     };
60 
61     CAST_CHECK(ClassInfoExtractor, IsClassInfoExtractor);
62 
63     static void BuildClassInfoExtractorFromLiteral(JSThread *thread, JSHandle<ClassInfoExtractor> &extractor,
64                                                    const JSHandle<TaggedArray> &literal,
65                                                    uint32_t length,
66                                                    ClassKind kind = ClassKind::NON_SENDABLE,
67                                                    uint32_t implementLength = 0);
68 
69     static JSHandle<JSHClass> CreatePrototypeHClass(JSThread *thread,
70                                                     JSHandle<TaggedArray> &keys,
71                                                     JSHandle<TaggedArray> &properties);
72 
73     static JSHandle<JSHClass> CreateConstructorHClass(JSThread *thread, const JSHandle<JSTaggedValue> &base,
74                                                       JSHandle<TaggedArray> &keys,
75                                                       JSHandle<TaggedArray> &properties);
76     static JSHandle<JSHClass> CreateSendableHClass(JSThread *thread, JSHandle<TaggedArray> &keys,
77                                                    JSHandle<TaggedArray> &properties, bool isProtoClass,
78                                                    uint32_t extraLength = 0);
79     static void CorrectConstructorHClass(JSThread *thread,
80                                          JSHandle<TaggedArray> &properties,
81                                          JSHClass *constructorHClass);
82 
83     static constexpr size_t PROTOTYPE_HCLASS_OFFSET = TaggedObjectSize();
84     ACCESSORS(NonStaticKeys, PROTOTYPE_HCLASS_OFFSET, NON_STATIC_PROPERTIES_OFFSET)
85     ACCESSORS(NonStaticProperties, NON_STATIC_PROPERTIES_OFFSET, NON_STATIC_ELEMENTS_OFFSET)
86     ACCESSORS(NonStaticElements, NON_STATIC_ELEMENTS_OFFSET, CONSTRUCTOR_HCLASS_OFFSET)
87     ACCESSORS(StaticKeys, CONSTRUCTOR_HCLASS_OFFSET, STATIC_PROPERTIES_OFFSET)
88     ACCESSORS(StaticProperties, STATIC_PROPERTIES_OFFSET, STATIC_ELEMENTS_OFFSET)
89     ACCESSORS(StaticElements, STATIC_ELEMENTS_OFFSET, CONSTRUCTOR_METHOD_OFFSET)
90     ACCESSORS(ConstructorMethod, CONSTRUCTOR_METHOD_OFFSET, BIT_FIELD_OFFSET)
91     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
92     DEFINE_ALIGN_SIZE(LAST_OFFSET);
93 
94     // define BitField
95     static constexpr size_t NON_STATIC_BITS = 1;
96     static constexpr size_t STATIC_BITS = 1;
97     FIRST_BIT_FIELD(BitField, NonStaticWithElements, bool, NON_STATIC_BITS)
98     NEXT_BIT_FIELD(BitField, StaticWithElements, bool, STATIC_BITS, NonStaticWithElements)
99 
100     DECL_VISIT_OBJECT(PROTOTYPE_HCLASS_OFFSET, BIT_FIELD_OFFSET)
101 
102     DECL_DUMP()
103 
104 private:
105     static bool ExtractAndReturnWhetherWithElements(JSThread *thread, const JSHandle<TaggedArray> &literal,
106                                                     const ExtractContentsDetail &detail,
107                                                     JSHandle<TaggedArray> &keys, JSHandle<TaggedArray> &properties,
108                                                     JSHandle<TaggedArray> &elements,
109                                                     const JSPandaFile *jsPandaFile);
110 };
111 
112 enum class ClassPropertyType : uint8_t { NON_STATIC = 0, STATIC };
113 
114 class ClassHelper {
115 public:
116     static JSHandle<JSFunction> DefineClassFromExtractor(JSThread *thread, const JSHandle<JSTaggedValue> &base,
117                                                          JSHandle<ClassInfoExtractor> &extractor,
118                                                          const JSHandle<JSTaggedValue> &lexenv);
119 
120     static JSHandle<JSFunction> DefineClassWithIHClass(JSThread *thread, const JSHandle<JSTaggedValue> &base,
121                                                        JSHandle<ClassInfoExtractor> &extractor,
122                                                        const JSHandle<JSTaggedValue> &lexenv,
123                                                        const JSHandle<JSTaggedValue> &prototypeOrHClassVal,
124                                                        const JSHandle<JSTaggedValue> &constructorHClassVal);
125 
126     static bool PUBLIC_API MatchFieldType(SharedFieldType fieldType, JSTaggedValue value);
127     static CString StaticFieldTypeToString(uint32_t fieldType);
128 private:
129     static JSHandle<NameDictionary> BuildDictionaryProperties(JSThread *thread, const JSHandle<JSObject> &object,
130                                                               JSHandle<TaggedArray> &keys,
131                                                               JSHandle<TaggedArray> &properties, ClassPropertyType type,
132                                                               const JSHandle<JSTaggedValue> &lexenv);
133 
134     static JSHandle<JSFunction> CreateJSFunctionFromTemplate(JSThread *thread,
135                                                              const JSHandle<FunctionTemplate> &funcTemp,
136                                                              const JSHandle<JSObject> &homeObject,
137                                                              const JSHandle<JSTaggedValue> &lexenv);
138 
139     static void HandleElementsProperties(JSThread *thread, const JSHandle<JSObject> &object,
140                                          const JSHandle<JSTaggedValue> &lexenv, JSHandle<TaggedArray> &elements);
141 };
142 
143 class SendableClassDefiner : public ClassHelper {
144 public:
145     static JSHandle<JSFunction> DefineSendableClassFromExtractor(JSThread *thread,
146                                                                  JSHandle<ClassInfoExtractor> &extractor,
147                                                                  const JSHandle<TaggedArray> &fieldTypeArray);
148 
149     static void DefineSendableInstanceHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
150                                              uint32_t length,
151                                              const JSHandle<JSFunction> &ctor, const JSHandle<JSTaggedValue> &base);
152 
153     static JSHandle<TaggedArray> ExtractStaticFieldTypeArray(JSThread *thread,
154                                                              const JSHandle<TaggedArray> &fieldTypeArray);
155 
156     static void FilterDuplicatedKeys(JSThread *thread, const JSHandle<TaggedArray> &keys,
157                                      const JSHandle<TaggedArray> &properties);
158 
FromTaggedValue(JSTaggedValue value)159     static SharedFieldType FromTaggedValue(JSTaggedValue value)
160     {
161         if (value.IsNull()) {
162             return SharedFieldType::NONE;
163         } else if (value.IsNumber()) {
164             return SharedFieldType::NUMBER;
165         } else if (value.IsString()) {
166             return SharedFieldType::STRING;
167         } else if (value.IsBoolean()) {
168             return SharedFieldType::BOOLEAN;
169         } else if (value.IsJSSharedObject()) {
170             return SharedFieldType::SENDABLE;
171         } else {
172             return SharedFieldType::NONE;
173         }
174     }
175 
176     static void PUBLIC_API AddFieldTypeToHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
177                                                 uint32_t length, const JSHandle<NameDictionary> &nameDict,
178                                                 const JSHandle<JSHClass> &hclass,
179                                                 JSHandle<NumberDictionary> &elementsDic, bool &hasElement);
180 
181     static void PUBLIC_API AddFieldTypeToHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
182         uint32_t length, const JSHandle<LayoutInfo> &layout, const JSHandle<JSHClass> &hclass,
183         JSHandle<NumberDictionary> &elementsDic, bool &hasElement, size_t start,
184         std::vector<JSHandle<JSTaggedValue>> &&propertyList = std::vector<JSHandle<JSTaggedValue>>());
185 private:
FromFieldType(FieldType type)186     static SharedFieldType FromFieldType(FieldType type)
187     {
188         return SharedFieldType(static_cast<uint32_t>(type));
189     }
190 
191     static JSHandle<NameDictionary> BuildSendableDictionaryProperties(JSThread *thread,
192                                                                       const JSHandle<JSObject> &object,
193                                                                       JSHandle<TaggedArray> &keys,
194                                                                       JSHandle<TaggedArray> &properties,
195                                                                       ClassPropertyType type,
196                                                                       const JSHandle<JSFunction> &ctor);
197 
198     static JSHandle<JSFunction> CreateSFunctionFromTemplate(JSThread *thread,
199                                                             const JSHandle<FunctionTemplate> &funcTemp,
200                                                             const JSHandle<JSObject> &homeObject,
201                                                             const JSHandle<JSFunction> &ctor);
202 
203     static void UpdateAccessorFunction(JSThread *thread, const JSMutableHandle<JSTaggedValue> &value,
204                                        const JSHandle<JSTaggedValue> &homeObject, const JSHandle<JSFunction> &ctor);
205 
206     static void AddFieldTypeToDict(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
207                                    uint32_t length, JSMutableHandle<NameDictionary> &dict,
208                                    JSHandle<NumberDictionary> &elementsDic, bool &hasElement,
209                                    PropertyAttributes attributes = PropertyAttributes::Default(true, true, false));
210 
211     static bool TryUpdateExistValue(JSThread *thread, JSMutableHandle<JSTaggedValue> &existValue,
212                                     JSMutableHandle<JSTaggedValue> &value);
213 
214     static void TryUpdateValue(JSThread *thread, JSMutableHandle<JSTaggedValue> &value);
215 
216     static void UpdateValueToAccessor(JSThread *thread, JSMutableHandle<JSTaggedValue> &value,
217                                       JSHandle<AccessorData> &accessor);
218     static std::pair<uint32_t, uint32_t> GetSizeAndMaxInlineByType(JSType type);
219 };
220 }  // namespace panda::ecmascript
221 #endif  // ECMASCRIPT_JSPANDAFILE_CLASS_INFO_EXTRACTOR_H
222