1 /* 2 * Copyright (c) 2021 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/tagged_array.h" 23 24 namespace panda::ecmascript { 25 enum class ArrayMode : uint8_t { UNDEFINED = 0, DICTIONARY, LITERAL }; 26 // ecma6 9.4.2 Array Exotic Object 27 class JSArray : public JSObject { 28 public: 29 static constexpr int LENGTH_INLINE_PROPERTY_INDEX = 0; 30 31 CAST_CHECK(JSArray, IsJSArray); 32 33 static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 34 ArrayMode mode = ArrayMode::UNDEFINED); 35 static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length, 36 const JSHandle<JSTaggedValue> &newTarget, 37 ArrayMode mode = ArrayMode::UNDEFINED); 38 static JSTaggedValue ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObject> &originalArray, 39 JSTaggedNumber length); 40 static bool ArraySetLength(JSThread *thread, const JSHandle<JSObject> &array, const PropertyDescriptor &desc); 41 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, const JSHandle<JSTaggedValue> &key, 42 const PropertyDescriptor &desc); 43 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, uint32_t index, 44 const PropertyDescriptor &desc); 45 46 static bool IsLengthString(JSThread *thread, const JSHandle<JSTaggedValue> &key); 47 // ecma6 7.3 Operations on Objects 48 static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<TaggedArray> &elements); 49 // use first inlined property slot for array length GetArrayLength()50 inline uint32_t GetArrayLength() const 51 { 52 return GetLength(); 53 } 54 SetArrayLength(const JSThread * thread,uint32_t length)55 inline void SetArrayLength([[maybe_unused]]const JSThread *thread, uint32_t length) 56 { 57 SetLength(length); 58 } 59 60 static constexpr size_t LENGTH_OFFSET = JSObject::SIZE; 61 ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, TRACE_INDEX_OFFSET) 62 ACCESSORS_PRIMITIVE_FIELD(TraceIndex, uint32_t, TRACE_INDEX_OFFSET, SIZE) 63 64 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, SIZE, SIZE) 65 66 static const uint32_t MAX_ARRAY_INDEX = MAX_ELEMENT_INDEX; DECL_DUMP()67 DECL_DUMP() 68 69 static int32_t GetArrayLengthOffset() 70 { 71 return LENGTH_OFFSET; 72 } 73 74 static bool PropertyKeyToArrayIndex(JSThread *thread, const JSHandle<JSTaggedValue> &key, uint32_t *output); 75 76 static JSTaggedValue LengthGetter(JSThread *thread, const JSHandle<JSObject> &self); 77 78 static bool LengthSetter(JSThread *thread, const JSHandle<JSObject> &self, const JSHandle<JSTaggedValue> &value, 79 bool mayThrow = false); 80 81 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 82 uint32_t index); 83 84 static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 85 const JSHandle<JSTaggedValue> &key); 86 87 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, uint32_t index, 88 const JSHandle<JSTaggedValue> &value); 89 90 static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 91 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value); 92 93 static void Sort(JSThread *thread, const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &fn); 94 static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 95 const JSHandle<JSTaggedValue> &value); 96 static JSHandle<TaggedArray> ToTaggedArray(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 97 static void CheckAndCopyArray(const JSThread *thread, JSHandle<JSArray> obj); 98 static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen); 99 }; 100 } // namespace panda::ecmascript 101 102 #endif // ECMASCRIPT_JSARRAY_H 103