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 : public JSObject { 29 public: 30 static constexpr int LENGTH_INLINE_PROPERTY_INDEX = 0; 31 32 CAST_CHECK(JSArray, IsJSArray); 33 34 static PUBLIC_API JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 35 ArrayMode mode = ArrayMode::UNDEFINED); 36 static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 37 const JSHandle<JSTaggedValue> &newTarget, 38 ArrayMode mode = ArrayMode::UNDEFINED); 39 static JSTaggedValue ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObject> &originalArray, 40 JSTaggedNumber length); 41 static bool ArraySetLength(JSThread *thread, const JSHandle<JSObject> &array, const PropertyDescriptor &desc); 42 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, const JSHandle<JSTaggedValue> &key, 43 const PropertyDescriptor &desc); 44 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, uint32_t index, 45 const PropertyDescriptor &desc); 46 47 static bool IsLengthString(JSThread *thread, const JSHandle<JSTaggedValue> &key); 48 // ecma6 7.3 Operations on Objects 49 static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<TaggedArray> &elements); 50 static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<JSTaggedValue> &newtarget, 51 const JSHandle<TaggedArray> &elements); 52 // use first inlined property slot for array length GetArrayLength()53 inline uint32_t GetArrayLength() const 54 { 55 return GetLength(); 56 } 57 SetArrayLength(const JSThread * thread,uint32_t length)58 inline void SetArrayLength([[maybe_unused]]const JSThread *thread, uint32_t length) 59 { 60 SetLength(length); 61 } 62 GetHintLength()63 inline uint32_t GetHintLength() const 64 { 65 auto trackInfo = GetTrackInfo(); 66 if (trackInfo.IsInt()) { 67 int hint = trackInfo.GetInt(); 68 return hint > 0 ? hint : 0; 69 } 70 return 0; 71 } 72 73 static constexpr size_t LENGTH_OFFSET = JSObject::SIZE; 74 ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, TRACE_INDEX_OFFSET) 75 ACCESSORS_PRIMITIVE_FIELD(TraceIndex, uint32_t, TRACE_INDEX_OFFSET, TRACK_INFO_OFFSET) 76 ACCESSORS(TrackInfo, TRACK_INFO_OFFSET, SIZE) 77 78 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, TRACK_INFO_OFFSET, SIZE) 79 80 static const uint32_t MAX_ARRAY_INDEX = MAX_ELEMENT_INDEX; DECL_DUMP()81 DECL_DUMP() 82 83 static int32_t GetArrayLengthOffset() 84 { 85 return LENGTH_OFFSET; 86 } 87 88 static bool PropertyKeyToArrayIndex(JSThread *thread, const JSHandle<JSTaggedValue> &key, uint32_t *output); 89 90 static JSTaggedValue LengthGetter(JSThread *thread, const JSHandle<JSObject> &self); 91 92 static bool LengthSetter(JSThread *thread, const JSHandle<JSObject> &self, const JSHandle<JSTaggedValue> &value, 93 bool mayThrow = false); 94 95 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 96 uint32_t index); 97 98 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 99 const JSHandle<JSTaggedValue> &key); 100 101 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, uint32_t index, 102 const JSHandle<JSTaggedValue> &value); 103 104 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 105 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value); 106 107 static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &fn); 108 static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 109 const JSHandle<JSTaggedValue> &value); 110 static JSHandle<TaggedArray> ToTaggedArray(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 111 static void PUBLIC_API CheckAndCopyArray(const JSThread *thread, JSHandle<JSArray> obj); 112 static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen, 113 bool isNew = false); 114 static void SortElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 115 const JSHandle<JSTaggedValue> &fn); 116 static void SortElementsByObject(JSThread *thread, const JSHandle<JSObject> &thisObjHandle, 117 const JSHandle<JSTaggedValue> &fn); 118 static void SortElementsByInsertionSort(JSThread *thread, const JSHandle<TaggedArray> &elements, uint32_t len, 119 const JSHandle<JSTaggedValue> &fn); 120 static void SortElementsByMergeSort(JSThread *thread, const JSHandle<TaggedArray> &elements, 121 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t endIdx); 122 static void MergeSortedElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 123 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t middleIdx, int64_t endIdx); 124 125 template <class Callback> ArrayCreateWithInit(JSThread * thread,uint32_t length,const Callback & cb)126 static JSTaggedValue ArrayCreateWithInit(JSThread *thread, uint32_t length, const Callback &cb) 127 { 128 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 129 JSHandle<TaggedArray> newElements(factory->NewTaggedArray(length)); 130 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 131 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, newElements)); 132 cb(newElements, length); 133 return array.GetTaggedValue(); 134 } 135 }; 136 137 class TrackInfo : public TaggedObject { 138 public: Cast(TaggedObject * object)139 static TrackInfo *Cast(TaggedObject *object) 140 { 141 ASSERT(JSTaggedValue(object).IsTrackInfoObject()); 142 return static_cast<TrackInfo *>(object); 143 } 144 145 static constexpr size_t CACHED_HCLASS_OFFSET = TaggedObjectSize(); 146 ACCESSORS(CachedHClass, CACHED_HCLASS_OFFSET, CACHED_FUNC_OFFSET); 147 ACCESSORS(CachedFunc, CACHED_FUNC_OFFSET, BIT_FIELD_OFFSET); 148 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, ARRAY_LENGTH_OFFSET); 149 ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, LAST_OFFSET) 150 DEFINE_ALIGN_SIZE(LAST_OFFSET); 151 152 // define BitField 153 static constexpr size_t ELEMENTS_KIND_BITS = 8; 154 static constexpr size_t SPACE_FALG_BITS = 8; 155 FIRST_BIT_FIELD(BitField, ElementsKind, ElementsKind, ELEMENTS_KIND_BITS); 156 NEXT_BIT_FIELD(BitField, SpaceFlag, RegionSpaceFlag, SPACE_FALG_BITS, ElementsKind); 157 158 DECL_DUMP() 159 160 DECL_VISIT_OBJECT(CACHED_HCLASS_OFFSET, BIT_FIELD_OFFSET); 161 }; 162 } // namespace panda::ecmascript 163 164 #endif // ECMASCRIPT_JSARRAY_H 165