14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_API_JS_API_VECTOR_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_API_JS_API_VECTOR_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/js_object.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_cinamespace panda::ecmascript { 234514f5e3Sopenharmony_ciclass JSAPIVector : public JSObject { 244514f5e3Sopenharmony_cipublic: 254514f5e3Sopenharmony_ci static constexpr int32_t DEFAULT_CAPACITY_LENGTH = 10; 264514f5e3Sopenharmony_ci static JSAPIVector *Cast(TaggedObject *object) 274514f5e3Sopenharmony_ci { 284514f5e3Sopenharmony_ci ASSERT(JSTaggedValue(object).IsJSAPIVector()); 294514f5e3Sopenharmony_ci return static_cast<JSAPIVector *>(object); 304514f5e3Sopenharmony_ci } 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_ci static bool Add(JSThread *thread, const JSHandle<JSAPIVector> &vector, const JSHandle<JSTaggedValue> &value); 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci static void Insert(JSThread *thread, const JSHandle<JSAPIVector> &vector, 354514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &value, int32_t index); 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci static void SetLength(JSThread *thread, const JSHandle<JSAPIVector> &vector, uint32_t newSize); 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ci uint32_t GetCapacity(); 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ci static void IncreaseCapacityTo(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t newCapacity); 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci static int32_t GetIndexOf(JSThread *thread, const JSHandle<JSAPIVector> &vector, 444514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &obj); 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_ci static int32_t GetIndexFrom(JSThread *thread, const JSHandle<JSAPIVector> &vector, 474514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &obj, int32_t index); 484514f5e3Sopenharmony_ci 494514f5e3Sopenharmony_ci bool IsEmpty() const; 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci JSTaggedValue GetLastElement(); 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ci static int32_t GetLastIndexOf(JSThread *thread, const JSHandle<JSAPIVector> &vector, 544514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &obj); 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_ci static int32_t GetLastIndexFrom(JSThread *thread, const JSHandle<JSAPIVector> &vector, 574514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &obj, int32_t index); 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ci static bool Remove(JSThread *thread, const JSHandle<JSAPIVector> &vector, const JSHandle<JSTaggedValue> &obj); 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci static JSTaggedValue RemoveByIndex(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t index); 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_ci static JSTaggedValue RemoveByRange(JSThread *thread, const JSHandle<JSAPIVector> &vector, 644514f5e3Sopenharmony_ci int32_t fromIndex, int32_t toIndex); 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci static JSHandle<JSAPIVector> SubVector(JSThread *thread, const JSHandle<JSAPIVector> &vector, 674514f5e3Sopenharmony_ci int32_t fromIndex, int32_t toIndex); 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ci static JSTaggedValue ToString(JSThread *thread, const JSHandle<JSAPIVector> &vector); 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 724514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &callbackFn, 734514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &thisArg); 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ci static JSTaggedValue ReplaceAllElements(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle, 764514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &callbackFn, 774514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &thisArg); 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_ci static JSTaggedValue Get(JSThread *thread, const JSHandle<JSAPIVector> &vector, int32_t index); 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci JSTaggedValue PUBLIC_API Set(JSThread *thread, int32_t index, const JSTaggedValue &value); 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ci bool Has(const JSTaggedValue &value) const; 844514f5e3Sopenharmony_ci 854514f5e3Sopenharmony_ci static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIVector> &obj); 864514f5e3Sopenharmony_ci static JSHandle<TaggedArray> OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIVector> &obj); 874514f5e3Sopenharmony_ci static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, const JSHandle<JSTaggedValue> &key); 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci static void TrimToCurrentLength(JSThread *thread, const JSHandle<JSAPIVector> &vector); 904514f5e3Sopenharmony_ci 914514f5e3Sopenharmony_ci static void Clear(JSThread *thread, const JSHandle<JSAPIVector> &vector); 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci static JSHandle<JSAPIVector> Clone(JSThread *thread, const JSHandle<JSAPIVector> &vector); 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_ci static JSTaggedValue GetFirstElement(const JSHandle<JSAPIVector> &vector); 964514f5e3Sopenharmony_ci 974514f5e3Sopenharmony_ci static JSTaggedValue GetIteratorObj(JSThread *thread, const JSHandle<JSAPIVector> &obj); 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_ci static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, 1004514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &key); 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ci static bool SetProperty(JSThread *thread, const JSHandle<JSAPIVector> &obj, 1034514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &key, 1044514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &value); 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci inline uint32_t GetSize() const 1074514f5e3Sopenharmony_ci { 1084514f5e3Sopenharmony_ci return GetLength(); 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci static constexpr size_t ELEMENT_COUNT_OFFSET = JSObject::SIZE; 1124514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, ELEMENT_COUNT_OFFSET, ENDL_OFFSET) 1134514f5e3Sopenharmony_ci DEFINE_ALIGN_SIZE(ENDL_OFFSET); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ELEMENT_COUNT_OFFSET, ELEMENT_COUNT_OFFSET) 1164514f5e3Sopenharmony_ci DECL_DUMP() 1174514f5e3Sopenharmony_ciprivate: 1184514f5e3Sopenharmony_ci static void GrowCapacity(JSThread *thread, const JSHandle<JSAPIVector> &vector, uint32_t minCapacity); 1194514f5e3Sopenharmony_ci}; 1204514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci#endif // ECMASCRIPT_JS_API_JS_API_VECTOR_H 123