1 /* 2 * Copyright (c) 2022-2023 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_JS_API_JS_API_VECTOR_H 17 #define ECMASCRIPT_JS_API_JS_API_VECTOR_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 22 namespace panda::ecmascript { 23 class JSAPIVector : public JSObject { 24 public: 25 static constexpr int32_t DEFAULT_CAPACITY_LENGTH = 10; Cast(TaggedObject * object)26 static JSAPIVector *Cast(TaggedObject *object) 27 { 28 ASSERT(JSTaggedValue(object).IsJSAPIVector()); 29 return static_cast<JSAPIVector *>(object); 30 } 31 32 static bool Add(JSThread *thread, const JSHandle<JSAPIVector> &vector, const JSHandle<JSTaggedValue> &value); 33 34 static void Insert(JSThread *thread, const JSHandle<JSAPIVector> &vector, 35 const JSHandle<JSTaggedValue> &value, int32_t index); 36 37 static void SetLength(JSThread *thread, const JSHandle<JSAPIVector> &vector, uint32_t newSize); 38 39 uint32_t GetCapacity(); 40 41 static void IncreaseCapacityTo(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t newCapacity); 42 43 static int32_t GetIndexOf(JSThread *thread, const JSHandle<JSAPIVector> &vector, 44 const JSHandle<JSTaggedValue> &obj); 45 46 static int32_t GetIndexFrom(JSThread *thread, const JSHandle<JSAPIVector> &vector, 47 const JSHandle<JSTaggedValue> &obj, int32_t index); 48 49 bool IsEmpty() const; 50 51 JSTaggedValue GetLastElement(); 52 53 static int32_t GetLastIndexOf(JSThread *thread, const JSHandle<JSAPIVector> &vector, 54 const JSHandle<JSTaggedValue> &obj); 55 56 static int32_t GetLastIndexFrom(JSThread *thread, const JSHandle<JSAPIVector> &vector, 57 const JSHandle<JSTaggedValue> &obj, int32_t index); 58 59 static bool Remove(JSThread *thread, const JSHandle<JSAPIVector> &vector, const JSHandle<JSTaggedValue> &obj); 60 61 static JSTaggedValue RemoveByIndex(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t index); 62 63 static JSTaggedValue RemoveByRange(JSThread *thread, const JSHandle<JSAPIVector> &vector, 64 int32_t fromIndex, int32_t toIndex); 65 66 static JSHandle<JSAPIVector> SubVector(JSThread *thread, const JSHandle<JSAPIVector> &vector, 67 int32_t fromIndex, int32_t toIndex); 68 69 static JSTaggedValue ToString(JSThread *thread, const JSHandle<JSAPIVector> &vector); 70 71 static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 72 const JSHandle<JSTaggedValue> &callbackFn, 73 const JSHandle<JSTaggedValue> &thisArg); 74 75 static JSTaggedValue ReplaceAllElements(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 76 const JSHandle<JSTaggedValue> &callbackFn, 77 const JSHandle<JSTaggedValue> &thisArg); 78 79 static JSTaggedValue Get(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t index); 80 81 JSTaggedValue Set(JSThread *thread, int32_t index, const JSTaggedValue &value); 82 83 bool Has(const JSTaggedValue &value) const; 84 85 static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIVector> &obj); 86 static JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIVector> &obj); 87 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, const JSHandle<JSTaggedValue> &key); 88 89 static void TrimToCurrentLength(JSThread *thread, const JSHandle<JSAPIVector> &vector); 90 91 static void Clear(JSThread *thread, const JSHandle<JSAPIVector> &vector); 92 93 static JSHandle<JSAPIVector> Clone(JSThread *thread, const JSHandle<JSAPIVector> &vector); 94 95 static JSTaggedValue GetFirstElement(const JSHandle<JSAPIVector> &vector); 96 97 static JSTaggedValue GetIteratorObj(JSThread *thread, const JSHandle<JSAPIVector> &obj); 98 99 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, 100 const JSHandle<JSTaggedValue> &key); 101 102 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, 103 const JSHandle<JSTaggedValue> &key, 104 const JSHandle<JSTaggedValue> &value); 105 GetSize()106 inline uint32_t GetSize() const 107 { 108 return GetLength(); 109 } 110 111 static constexpr size_t ELEMENT_COUNT_OFFSET = JSObject::SIZE; 112 ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, ELEMENT_COUNT_OFFSET, ENDL_OFFSET) 113 DEFINE_ALIGN_SIZE(ENDL_OFFSET); 114 115 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ELEMENT_COUNT_OFFSET, ELEMENT_COUNT_OFFSET) 116 DECL_DUMP() 117 private: 118 static void GrowCapacity(JSThread *thread, const JSHandle<JSAPIVector> &vector, uint32_t minCapacity); 119 }; 120 } // namespace panda::ecmascript 121 122 #endif // ECMASCRIPT_JS_API_JS_API_VECTOR_H 123