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