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_PLAIN_ARRAY_H 17 #define ECMASCRIPT_JS_API_JS_API_PLAIN_ARRAY_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 22 namespace panda::ecmascript { 23 class JSAPIPlainArray : public JSObject { 24 public: 25 static constexpr int DEFAULT_CAPACITY_LENGTH = 8; Cast(TaggedObject * object)26 static JSAPIPlainArray *Cast(TaggedObject *object) 27 { 28 ASSERT(JSTaggedValue(object).IsJSAPIPlainArray()); 29 return static_cast<JSAPIPlainArray *>(object); 30 } 31 static void Add(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, JSHandle<JSTaggedValue> key, 32 JSHandle<JSTaggedValue> value); 33 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, 34 const JSHandle<JSTaggedValue> &key); 35 static JSHandle<TaggedArray> CreateSlot(const JSThread *thread, const uint32_t capacity); 36 static JSHandle<JSAPIPlainArray> Clone(JSThread *thread, const JSHandle<JSAPIPlainArray> &plainArray); 37 static JSHandle<JSTaggedValue> GetIteratorObj(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, 38 IterationKind kind); 39 static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 40 const JSHandle<JSTaggedValue> &callbackFn, const JSHandle<JSTaggedValue> &thisArg); 41 static JSTaggedValue ToString(JSThread *thread, const JSHandle<JSAPIPlainArray> &plainarray); 42 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, 43 const JSHandle<JSTaggedValue> &key); 44 static bool SetProperty(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, 45 const JSHandle<JSTaggedValue> &key, 46 const JSHandle<JSTaggedValue> &value); 47 static JSTaggedValue Set(JSThread *thread, const JSHandle<JSAPIPlainArray> &obj, 48 const uint32_t index, JSTaggedValue value); 49 JSTaggedValue RemoveAt(JSThread *thread, JSTaggedValue index); 50 JSTaggedValue GetIndexOfKey(int32_t key); 51 JSTaggedValue GetIndexOfValue(JSTaggedValue value); 52 JSTaggedValue GetKeyAt(int32_t index); 53 JSTaggedValue GetValueAt(JSThread *thread, int32_t index); 54 JSTaggedValue Get(const JSTaggedValue key); 55 JSTaggedValue Remove(JSThread *thread, JSTaggedValue key); 56 JSTaggedValue RemoveRangeFrom(JSThread *thread, int32_t index, int32_t batchSize); 57 bool SetValueAt(JSThread *thread, JSTaggedValue index, JSTaggedValue value); 58 bool Has(const int32_t key); 59 bool IsEmpty(); 60 bool AdjustForward(JSThread *thread, int32_t index, int32_t forwardSize); 61 int32_t BinarySearch(TaggedArray *array, int32_t fromIndex, int32_t toIndex, int32_t key); 62 void Clear(JSThread *thread); 63 void AdjustArray(JSThread *thread, TaggedArray *srcArray, int32_t fromIndex, int32_t toIndex, 64 bool direction); GetSize()65 inline uint32_t GetSize() const 66 { 67 return GetLength(); 68 } 69 static constexpr size_t KEYS_OFFSET = JSObject::SIZE; 70 ACCESSORS(Keys, KEYS_OFFSET, VALUES_OFFSET); 71 ACCESSORS(Values, VALUES_OFFSET, LENGTH_OFFSET); 72 ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, LAST_OFFSET); 73 DEFINE_ALIGN_SIZE(LAST_OFFSET); 74 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,KEYS_OFFSET,LENGTH_OFFSET)75 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, KEYS_OFFSET, LENGTH_OFFSET) 76 DECL_DUMP() 77 private: 78 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 79 { 80 uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U); 81 return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH; 82 } 83 }; 84 } // namespace panda::ecmascript 85 #endif // ECMASCRIPT_JS_API_JS_API_PLAIN_ARRAY_H 86