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 25namespace panda::ecmascript { 26enum class ArrayMode : uint8_t { UNDEFINED = 0, DICTIONARY, LITERAL }; 27// ecma6 9.4.2 Array Exotic Object 28class JSArray : public JSObject { 29public: 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 53 inline uint32_t GetArrayLength() const 54 { 55 return GetLength(); 56 } 57 58 inline void SetArrayLength([[maybe_unused]]const JSThread *thread, uint32_t length) 59 { 60 SetLength(length); 61 } 62 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; 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 bool TryFastCreateDataProperty(JSThread *thread, const JSHandle<JSObject> &obj, uint32_t index, 108 const JSHandle<JSTaggedValue> &value, 109 SCheckMode sCheckMode = SCheckMode::CHECK); 110 111 static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &fn); 112 static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 113 const JSHandle<JSTaggedValue> &value); 114 static JSHandle<TaggedArray> ToTaggedArray(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 115 static void PUBLIC_API CheckAndCopyArray(const JSThread *thread, JSHandle<JSArray> obj); 116 static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen, 117 bool isNew = false); 118 static void SortElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 119 const JSHandle<JSTaggedValue> &fn); 120 static void SortElementsByObject(JSThread *thread, const JSHandle<JSObject> &thisObjHandle, 121 const JSHandle<JSTaggedValue> &fn); 122 static void SortElementsByInsertionSort(JSThread *thread, const JSHandle<TaggedArray> &elements, uint32_t len, 123 const JSHandle<JSTaggedValue> &fn); 124 static void SortElementsByMergeSort(JSThread *thread, const JSHandle<TaggedArray> &elements, 125 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t endIdx); 126 static void MergeSortedElements(JSThread *thread, const JSHandle<TaggedArray> &elements, 127 const JSHandle<JSTaggedValue> &fn, int64_t startIdx, int64_t middleIdx, int64_t endIdx); 128 129 template <class Callback> 130 static JSTaggedValue ArrayCreateWithInit(JSThread *thread, uint32_t length, const Callback &cb) 131 { 132 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 133 JSHandle<TaggedArray> newElements(factory->NewTaggedArray(length)); 134 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 135 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, newElements)); 136 cb(newElements, length); 137 return array.GetTaggedValue(); 138 } 139}; 140 141class TrackInfo : public TaggedObject { 142public: 143 static TrackInfo *Cast(TaggedObject *object) 144 { 145 ASSERT(JSTaggedValue(object).IsTrackInfoObject()); 146 return static_cast<TrackInfo *>(object); 147 } 148 149 static constexpr size_t CACHED_HCLASS_OFFSET = TaggedObjectSize(); 150 ACCESSORS(CachedHClass, CACHED_HCLASS_OFFSET, CACHED_FUNC_OFFSET); 151 ACCESSORS(CachedFunc, CACHED_FUNC_OFFSET, BIT_FIELD_OFFSET); 152 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, ARRAY_LENGTH_OFFSET); 153 ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, LAST_OFFSET) 154 DEFINE_ALIGN_SIZE(LAST_OFFSET); 155 156 // define BitField 157 static constexpr size_t ELEMENTS_KIND_BITS = 8; 158 static constexpr size_t SPACE_FALG_BITS = 8; 159 FIRST_BIT_FIELD(BitField, ElementsKind, ElementsKind, ELEMENTS_KIND_BITS); 160 NEXT_BIT_FIELD(BitField, SpaceFlag, RegionSpaceFlag, SPACE_FALG_BITS, ElementsKind); 161 162 DECL_DUMP() 163 164 DECL_VISIT_OBJECT(CACHED_HCLASS_OFFSET, BIT_FIELD_OFFSET); 165}; 166} // namespace panda::ecmascript 167 168#endif // ECMASCRIPT_JSARRAY_H 169