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 
47     static constexpr uint8_t CONSTRUCTOR_INDEX = 0;
48     static constexpr uint8_t LENGTH_INDEX = 0;
49     static constexpr uint8_t NAME_INDEX = 1;
50     static constexpr uint8_t PROTOTYPE_INDEX = 2;
51 
52     struct ExtractContentsDetail {
53         uint32_t extractBegin;
54         uint32_t extractEnd;
55         uint8_t fillStartLoc;
56         MethodLiteral *methodLiteral;
57     };
58 
59     CAST_CHECK(ClassInfoExtractor, IsClassInfoExtractor);
60 
61     static void BuildClassInfoExtractorFromLiteral(JSThread *thread, JSHandle<ClassInfoExtractor> &extractor,
62                                                    const JSHandle<TaggedArray> &literal,
63                                                    uint32_t length,
64                                                    ClassKind kind = ClassKind::NON_SENDABLE);
65 
66     static JSHandle<JSHClass> CreatePrototypeHClass(JSThread *thread,
67                                                     JSHandle<TaggedArray> &keys,
68                                                     JSHandle<TaggedArray> &properties);
69 
70     static JSHandle<JSHClass> CreateConstructorHClass(JSThread *thread, const JSHandle<JSTaggedValue> &base,
71                                                       JSHandle<TaggedArray> &keys,
72                                                       JSHandle<TaggedArray> &properties);
73     static JSHandle<JSHClass> CreateSendableHClass(JSThread *thread, JSHandle<TaggedArray> &keys,
74                                                    JSHandle<TaggedArray> &properties, bool isProtoClass,
75                                                    uint32_t extraLength = 0);
76     static void CorrectConstructorHClass(JSThread *thread,
77                                          JSHandle<TaggedArray> &properties,
78                                          JSHClass *constructorHClass);
79 
80     static constexpr size_t PROTOTYPE_HCLASS_OFFSET = TaggedObjectSize();
81     ACCESSORS(NonStaticKeys, PROTOTYPE_HCLASS_OFFSET, NON_STATIC_PROPERTIES_OFFSET)
82     ACCESSORS(NonStaticProperties, NON_STATIC_PROPERTIES_OFFSET, NON_STATIC_ELEMENTS_OFFSET)
83     ACCESSORS(NonStaticElements, NON_STATIC_ELEMENTS_OFFSET, CONSTRUCTOR_HCLASS_OFFSET)
84     ACCESSORS(StaticKeys, CONSTRUCTOR_HCLASS_OFFSET, STATIC_PROPERTIES_OFFSET)
85     ACCESSORS(StaticProperties, STATIC_PROPERTIES_OFFSET, STATIC_ELEMENTS_OFFSET)
86     ACCESSORS(StaticElements, STATIC_ELEMENTS_OFFSET, CONSTRUCTOR_METHOD_OFFSET)
87     ACCESSORS(ConstructorMethod, CONSTRUCTOR_METHOD_OFFSET, BIT_FIELD_OFFSET)
88     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
89     DEFINE_ALIGN_SIZE(LAST_OFFSET);
90 
91     // define BitField
92     static constexpr size_t NON_STATIC_BITS = 1;
93     static constexpr size_t STATIC_BITS = 1;
94     FIRST_BIT_FIELD(BitField, NonStaticWithElements, bool, NON_STATIC_BITS)
95     NEXT_BIT_FIELD(BitField, StaticWithElements, bool, STATIC_BITS, NonStaticWithElements)
96 
97     DECL_VISIT_OBJECT(PROTOTYPE_HCLASS_OFFSET, BIT_FIELD_OFFSET)
98 
99     DECL_DUMP()
100 
101 private:
102     static bool ExtractAndReturnWhetherWithElements(JSThread *thread, const JSHandle<TaggedArray> &literal,
103                                                     const ExtractContentsDetail &detail,
104                                                     JSHandle<TaggedArray> &keys, JSHandle<TaggedArray> &properties,
105                                                     JSHandle<TaggedArray> &elements,
106                                                     const JSPandaFile *jsPandaFile);
107 };
108 
109 enum class ClassPropertyType : uint8_t { NON_STATIC = 0, STATIC };
110 
111 class ClassHelper {
112 public:
113     static JSHandle<JSFunction> DefineClassFromExtractor(JSThread *thread, const JSHandle<JSTaggedValue> &base,
114                                                          JSHandle<ClassInfoExtractor> &extractor,
115                                                          const JSHandle<JSTaggedValue> &lexenv);
116 
117     static JSHandle<JSFunction> DefineClassWithIHClass(JSThread *thread, const JSHandle<JSTaggedValue> &base,
118                                                        JSHandle<ClassInfoExtractor> &extractor,
119                                                        const JSHandle<JSTaggedValue> &lexenv,
120                                                        const JSHandle<JSTaggedValue> &prototypeOrHClassVal,
121                                                        const JSHandle<JSTaggedValue> &constructorHClassVal);
122 
123     static bool PUBLIC_API MatchFieldType(SharedFieldType fieldType, JSTaggedValue value);
124     static CString StaticFieldTypeToString(uint32_t fieldType);
125 private:
126     static JSHandle<NameDictionary> BuildDictionaryProperties(JSThread *thread, const JSHandle<JSObject> &object,
127                                                               JSHandle<TaggedArray> &keys,
128                                                               JSHandle<TaggedArray> &properties, ClassPropertyType type,
129                                                               const JSHandle<JSTaggedValue> &lexenv);
130 
131     static JSHandle<JSFunction> CreateJSFunctionFromTemplate(JSThread *thread,
132                                                              const JSHandle<FunctionTemplate> &funcTemp,
133                                                              const JSHandle<JSObject> &homeObject,
134                                                              const JSHandle<JSTaggedValue> &lexenv);
135 
136     static void HandleElementsProperties(JSThread *thread, const JSHandle<JSObject> &object,
137                                          const JSHandle<JSTaggedValue> &lexenv, JSHandle<TaggedArray> &elements);
138 };
139 
140 class SendableClassDefiner : public ClassHelper {
141 public:
142     static JSHandle<JSFunction> DefineSendableClassFromExtractor(JSThread *thread,
143                                                                  JSHandle<ClassInfoExtractor> &extractor,
144                                                                  const JSHandle<TaggedArray> &fieldTypeArray);
145 
146     static void DefineSendableInstanceHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
147                                              uint32_t length,
148                                              const JSHandle<JSFunction> &ctor, const JSHandle<JSTaggedValue> &base);
149 
150     static JSHandle<TaggedArray> ExtractStaticFieldTypeArray(JSThread *thread,
151                                                              const JSHandle<TaggedArray> &fieldTypeArray);
152 
153     static void FilterDuplicatedKeys(JSThread *thread, const JSHandle<TaggedArray> &keys,
154                                      const JSHandle<TaggedArray> &properties);
155 
FromTaggedValue(JSTaggedValue value)156     static SharedFieldType FromTaggedValue(JSTaggedValue value)
157     {
158         if (value.IsNull()) {
159             return SharedFieldType::NONE;
160         } else if (value.IsNumber()) {
161             return SharedFieldType::NUMBER;
162         } else if (value.IsString()) {
163             return SharedFieldType::STRING;
164         } else if (value.IsBoolean()) {
165             return SharedFieldType::BOOLEAN;
166         } else if (value.IsJSSharedObject()) {
167             return SharedFieldType::SENDABLE;
168         } else {
169             return SharedFieldType::NONE;
170         }
171     }
172 
173     static void PUBLIC_API AddFieldTypeToHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
174                                                 uint32_t length, const JSHandle<NameDictionary> &nameDict,
175                                                 const JSHandle<JSHClass> &hclass);
176 
177     static void PUBLIC_API AddFieldTypeToHClass(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray,
178         uint32_t length, const JSHandle<LayoutInfo> &layout, const JSHandle<JSHClass> &hclass,
179         size_t start, const JSHandle<NumberDictionary> &elementsDic = JSHandle<NumberDictionary>(),
180         std::vector<JSHandle<JSTaggedValue>> &&propertyList = std::vector<JSHandle<JSTaggedValue>>());
181 private:
FromFieldType(FieldType type)182     static SharedFieldType FromFieldType(FieldType type)
183     {
184         return SharedFieldType(static_cast<uint32_t>(type));
185     }
186 
187     static JSHandle<NameDictionary> BuildSendableDictionaryProperties(JSThread *thread,
188                                                                       const JSHandle<JSObject> &object,
189                                                                       JSHandle<TaggedArray> &keys,
190                                                                       JSHandle<TaggedArray> &properties,
191                                                                       ClassPropertyType type,
192                                                                       const JSHandle<JSFunction> &ctor);
193 
194     static JSHandle<JSFunction> CreateSFunctionFromTemplate(JSThread *thread,
195                                                             const JSHandle<FunctionTemplate> &funcTemp,
196                                                             const JSHandle<JSObject> &homeObject,
197                                                             const JSHandle<JSTaggedValue> &lexenv);
198 
199     static void UpdateAccessorFunction(JSThread *thread, const JSMutableHandle<JSTaggedValue> &value,
200                                        const JSHandle<JSTaggedValue> &homeObject, const JSHandle<JSFunction> &ctor);
201 
202     static void AddFieldTypeToDict(JSThread *thread, const JSHandle<TaggedArray> &fieldTypeArray, uint32_t length,
203                                    JSMutableHandle<NameDictionary> &dict,
204                                    PropertyAttributes attributes = PropertyAttributes::Default(true, true, false));
205 
206     static bool TryUpdateExistValue(JSThread *thread, JSMutableHandle<JSTaggedValue> &existValue,
207                                     JSMutableHandle<JSTaggedValue> &value);
208 
209     static void TryUpdateValue(JSThread *thread, JSMutableHandle<JSTaggedValue> &value);
210 
211     static void UpdateValueToAccessor(JSThread *thread, JSMutableHandle<JSTaggedValue> &value,
212                                       JSHandle<AccessorData> &accessor);
213     static std::pair<uint32_t, uint32_t> GetSizeAndMaxInlineByType(JSType type);
214 };
215 }  // namespace panda::ecmascript
216 #endif  // ECMASCRIPT_JSPANDAFILE_CLASS_INFO_EXTRACTOR_H
217