1 /* 2 * Copyright (c) 2021-2024 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_JSARRAY_H 17 #define ECMASCRIPT_JSARRAY_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 #include "ecmascript/js_tagged_value.h" 22 #include "ecmascript/js_thread.h" 23 #include "ecmascript/tagged_array.h" 24 25 namespace panda::ecmascript { 26 enum class ArrayMode : uint8_t { UNDEFINED = 0, DICTIONARY, LITERAL }; 27 // ecma6 9.4.2 Array Exotic Object 28 class JSArray final : public JSObject { 29 public: 30 // array instance property: 31 static constexpr int LENGTH_INLINE_PROPERTY_INDEX = 0; 32 // array prototype property: 33 static constexpr int CONSTRUCTOR_INLINE_PROPERTY_INDEX = 1; 34 // array constructor property: 35 static constexpr int ARRAY_FUNCTION_INLINE_PROPERTY_NUM = 7; 36 static constexpr int ARRAY_FUNCTION_SPECIES_INDEX = 0; 37 CAST_CHECK(JSArray, IsJSArray); 38 39 static JSTaggedValue CheckStableArrayAndGet(JSThread *thread, const JSHandle<JSObject> &thisObjHandle, 40 uint32_t index); 41 static void CheckStableArrayAndSet(JSThread *thread, const JSHandle<JSObject> &thisObjHandle, 42 uint32_t index, JSMutableHandle<JSTaggedValue> &value); 43 static PUBLIC_API JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 44 ArrayMode mode = ArrayMode::UNDEFINED); 45 static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 46 const JSHandle<JSTaggedValue> &newTarget, 47 ArrayMode mode = ArrayMode::UNDEFINED); 48 static JSTaggedValue GetConstructorOrSpeciesInlinedProp(JSThread *thread, JSTaggedValue object, 49 uint32_t inlinePropIndex); 50 static JSTaggedValue ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObject> &originalArray, 51 JSTaggedNumber length); 52 static bool ArraySetLength(JSThread *thread, const JSHandle<JSObject> &array, const PropertyDescriptor &desc); 53 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, const JSHandle<JSTaggedValue> &key, 54 const PropertyDescriptor &desc); 55 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, uint32_t index, 56 const PropertyDescriptor &desc); 57 58 static bool IsLengthString(JSThread *thread, const JSHandle<JSTaggedValue> &key); 59 static bool IsProtoNotChangeJSArray(JSThread *thread, const JSHandle<JSObject> &obj); 60 static bool IsProtoNotModifiedDictionaryJSArray(JSThread *thread, const JSHandle<JSObject> &obj); 61 // ecma6 7.3 Operations on Objects 62 static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<TaggedArray> &elements); 63 static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<JSTaggedValue> &newtarget, 64 const JSHandle<TaggedArray> &elements); 65 static JSTaggedValue FastConcatDictionaryArray(JSThread *thread, JSHandle<JSObject> obj, 66 JSHandle<JSObject> &newArrayHandle, JSMutableHandle<JSTaggedValue> &fromValHandle, 67 JSMutableHandle<JSTaggedValue> &toKey, int64_t &n); 68 69 // use first inlined property slot for array length GetArrayLength()70 inline uint32_t GetArrayLength() const 71 { 72 return GetLength(); 73 } 74 SetArrayLength(const JSThread * thread,uint32_t length)75 inline void SetArrayLength([[maybe_unused]]const JSThread *thread, uint32_t length) 76 { 77 SetLength(length); 78 } 79 GetHintLength(const JSThread * thread)80 inline uint32_t GetHintLength(const JSThread *thread) const 81 { 82 auto trackInfo = GetTrackInfo(thread); 83 if (trackInfo.IsInt()) { 84 int hint = trackInfo.GetInt(); 85 return hint > 0 ? hint : 0; 86 } 87 return 0; 88 } 89 90 static constexpr size_t LENGTH_OFFSET = JSObject::SIZE; 91 ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, TRACE_INDEX_OFFSET) 92 ACCESSORS_PRIMITIVE_FIELD(TraceIndex, uint32_t, TRACE_INDEX_OFFSET, TRACK_INFO_OFFSET) 93 ACCESSORS(TrackInfo, TRACK_INFO_OFFSET, SIZE) 94 95 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, TRACK_INFO_OFFSET, SIZE) 96 97 static const uint32_t MAX_ARRAY_INDEX = MAX_ELEMENT_INDEX; DECL_DUMP()98 DECL_DUMP() 99 100 static uint32_t GetInlinedPropertyOffset(uint32_t index) 101 { 102 return JSArray::SIZE + index * JSTaggedValue::TaggedTypeSize(); 103 } 104 GetArrayLengthOffset()105 static int32_t GetArrayLengthOffset() 106 { 107 return LENGTH_OFFSET; 108 } 109 110 static bool PropertyKeyToArrayIndex(JSThread *thread, const JSHandle<JSTaggedValue> &key, uint32_t *output); 111 112 static JSTaggedValue LengthGetter(JSThread *thread, const JSHandle<JSObject> &self); 113 114 static bool LengthSetter(JSThread *thread, const JSHandle<JSObject> &self, const JSHandle<JSTaggedValue> &value, 115 bool mayThrow = false); 116 117 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 118 uint32_t index); 119 120 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 121 const JSHandle<JSTaggedValue> &key); 122 123 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, uint32_t index, 124 const JSHandle<JSTaggedValue> &value); 125 126 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 127 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value); 128 129 static bool TryFastCreateDataProperty(JSThread *thread, const JSHandle<JSObject> &obj, uint32_t index, 130 const JSHandle<JSTaggedValue> &value, 131 SCheckMode sCheckMode = SCheckMode::CHECK); 132 133 static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &fn); 134 static JSTaggedValue CopySortedListToReceiver(JSThread *thread, const JSHandle<JSTaggedValue> &thisObjVal, 135 JSHandle<TaggedArray> sortedList, uint32_t len); 136 static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 137 const JSHandle<JSTaggedValue> &value); 138 static JSHandle<TaggedArray> ToTaggedArray(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 139 static void PUBLIC_API CheckAndCopyArray(const JSThread *thread, JSHandle<JSArray> obj); 140 static void CheckAndSetPrototypeModified(JSThread *thread, const JSHandle<JSObject> &newArrayHandle); 141 static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen, 142 bool isNew = false); 143 static void TransformElementsKindAfterSetCapacity(JSThread *thread, const JSHandle<JSObject> &array, 144 [[maybe_unused]] uint32_t oldLen, uint32_t newLen, 145 [[maybe_unused]] bool isNew); 146 static void SortElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 147 const JSHandle<JSTaggedValue> &fn); 148 static void SortElementsByObject(JSThread *thread, const JSHandle<JSObject> &thisObjHandle, 149 const JSHandle<JSTaggedValue> &fn); 150 static void SortElementsByInsertionSort(JSThread *thread, const JSHandle<TaggedArray> &elements, uint32_t len, 151 const JSHandle<JSTaggedValue> &fn); 152 static void SortElementsByMergeSort(JSThread *thread, const JSHandle<TaggedArray> &elements, 153 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t endIdx); 154 static void MergeSortedElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 155 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t middleIdx, int64_t endIdx); 156 157 static JSHandle<JSHClass> CreateJSArrayPrototypeClass(const JSThread *thread, ObjectFactory *factory, 158 JSHandle<JSTaggedValue> proto, uint32_t inlinedProps); 159 160 static JSHandle<JSHClass> CreateJSArrayFunctionClass(const JSThread *thread, ObjectFactory *factory, 161 const JSHandle<GlobalEnv> &env); 162 163 template <class Callback> ArrayCreateWithInit(JSThread * thread,uint32_t length,const Callback & cb)164 static JSTaggedValue ArrayCreateWithInit(JSThread *thread, uint32_t length, const Callback &cb) 165 { 166 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 167 JSHandle<TaggedArray> newElements(factory->NewTaggedArray(length)); 168 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 169 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, newElements)); 170 cb(newElements, length); 171 return array.GetTaggedValue(); 172 } 173 174 void UpdateTrackInfo(const JSThread *thread); 175 }; 176 177 class ArrayJoinStack { 178 public: 179 static bool Push(const JSThread *thread, const JSHandle<JSTaggedValue> receiver); 180 static void Pop(const JSThread *thread, const JSHandle<JSTaggedValue> receiver); 181 182 static constexpr uint32_t MIN_JOIN_STACK_SIZE = 2; 183 }; 184 185 class TrackInfo : public TaggedObject { 186 public: Cast(TaggedObject * object)187 static TrackInfo *Cast(TaggedObject *object) 188 { 189 ASSERT(JSTaggedValue(object).IsTrackInfoObject()); 190 return static_cast<TrackInfo *>(object); 191 } 192 193 static constexpr size_t CACHED_HCLASS_OFFSET = TaggedObjectSize(); 194 ACCESSORS(CachedHClass, CACHED_HCLASS_OFFSET, CACHED_FUNC_OFFSET); 195 ACCESSORS(CachedFunc, CACHED_FUNC_OFFSET, BIT_FIELD_OFFSET); 196 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, ARRAY_LENGTH_OFFSET); 197 ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, LAST_OFFSET) 198 DEFINE_ALIGN_SIZE(LAST_OFFSET); 199 200 // define BitField 201 static constexpr size_t ELEMENTS_KIND_BITS = 8; 202 static constexpr size_t SPACE_FALG_BITS = 8; 203 FIRST_BIT_FIELD(BitField, ElementsKind, ElementsKind, ELEMENTS_KIND_BITS); 204 NEXT_BIT_FIELD(BitField, SpaceFlag, RegionSpaceFlag, SPACE_FALG_BITS, ElementsKind); 205 206 DECL_DUMP() 207 208 DECL_VISIT_OBJECT(CACHED_HCLASS_OFFSET, BIT_FIELD_OFFSET); 209 }; 210 } // namespace panda::ecmascript 211 212 #endif // ECMASCRIPT_JSARRAY_H 213