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