1 /* 2 * Copyright (c) 2022 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_CONTAINERS_CONTAINERS_ARRAYLIST_H 17 #define ECMASCRIPT_CONTAINERS_CONTAINERS_ARRAYLIST_H 18 19 #include "ecmascript/base/builtins_base.h" 20 #include "ecmascript/ecma_runtime_call_info.h" 21 22 // List of functions in ArrayList.prototype, excluding the constructor and '@@' properties. 23 // V(name, func, length, stubIndex) 24 // where ContainersArrayList::func refers to the native implementation of ArrayList.prototype[name]. 25 #define CONTAINER_ARRAYLIST_PROTOTYPE_FUNCTIONS(V) \ 26 V("add", Add, 1, INVALID) \ 27 V("insert", Insert, 2, INVALID) \ 28 V("clear", Clear, 0, INVALID) \ 29 V("clone", Clone, 0, INVALID) \ 30 V("has", Has, 1, INVALID) \ 31 V("getCapacity", GetCapacity, 0, INVALID) \ 32 V("increaseCapacityTo", IncreaseCapacityTo, 1, INVALID) \ 33 V("trimToCurrentLength", TrimToCurrentLength, 0, INVALID) \ 34 V("getIndexOf", GetIndexOf, 1, INVALID) \ 35 V("isEmpty", IsEmpty, 0, INVALID) \ 36 V("getLastIndexOf", GetLastIndexOf, 1, INVALID) \ 37 V("removeByIndex", RemoveByIndex, 1, INVALID) \ 38 V("remove", Remove, 1, INVALID) \ 39 V("removeByRange", RemoveByRange, 2, INVALID) \ 40 V("replaceAllElements", ReplaceAllElements, 2, ArrayListReplaceAllElements) \ 41 V("sort", Sort, 1, INVALID) \ 42 V("subArrayList", SubArrayList, 2, INVALID) \ 43 V("convertToArray", ConvertToArray, 0, INVALID) \ 44 V("forEach", ForEach, 2, ArrayListForEach) 45 46 namespace panda::ecmascript::containers { 47 /** 48 * High performance container interface in jsapi. 49 * */ 50 class ContainersArrayList : public base::BuiltinsBase { 51 public: 52 static JSTaggedValue ArrayListConstructor(EcmaRuntimeCallInfo *argv); 53 54 static JSTaggedValue Add(EcmaRuntimeCallInfo *argv); 55 static JSTaggedValue Insert(EcmaRuntimeCallInfo *argv); 56 static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv); 57 static JSTaggedValue Clone(EcmaRuntimeCallInfo *argv); 58 static JSTaggedValue Has(EcmaRuntimeCallInfo *argv); 59 static JSTaggedValue GetCapacity(EcmaRuntimeCallInfo *argv); 60 static JSTaggedValue IncreaseCapacityTo(EcmaRuntimeCallInfo *argv); 61 static JSTaggedValue TrimToCurrentLength(EcmaRuntimeCallInfo *argv); 62 static JSTaggedValue GetIndexOf(EcmaRuntimeCallInfo *argv); 63 static JSTaggedValue IsEmpty(EcmaRuntimeCallInfo *argv); 64 static JSTaggedValue GetLastIndexOf(EcmaRuntimeCallInfo *argv); 65 static JSTaggedValue RemoveByIndex(EcmaRuntimeCallInfo *argv); 66 static JSTaggedValue Remove(EcmaRuntimeCallInfo *argv); 67 static JSTaggedValue RemoveByRange(EcmaRuntimeCallInfo *argv); 68 static JSTaggedValue ReplaceAllElements(EcmaRuntimeCallInfo *argv); 69 static JSTaggedValue Sort(EcmaRuntimeCallInfo *argv); 70 static JSTaggedValue SubArrayList(EcmaRuntimeCallInfo *argv); 71 static JSTaggedValue ConvertToArray(EcmaRuntimeCallInfo *argv); 72 static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv); 73 static JSTaggedValue GetIteratorObj(EcmaRuntimeCallInfo *argv); 74 static JSTaggedValue Get(EcmaRuntimeCallInfo *argv); 75 static JSTaggedValue Set(EcmaRuntimeCallInfo *argv); 76 static JSTaggedValue GetSize(EcmaRuntimeCallInfo *argv); 77 78 // Excluding the constructor and '@@' internal properties. GetArrayListPrototypeFunctions()79 static Span<const base::BuiltinFunctionEntry> GetArrayListPrototypeFunctions() 80 { 81 return Span<const base::BuiltinFunctionEntry>(ARRAYLIST_PROTOTYPE_FUNCTIONS); 82 } 83 84 private: 85 #define CONTAINER_ARRAYLIST_FUNCTION_ENTRY(name, method, length, id) \ 86 base::BuiltinFunctionEntry::Create(name, ContainersArrayList::method, length, BUILTINS_STUB_ID(id)), 87 88 static constexpr std::array ARRAYLIST_PROTOTYPE_FUNCTIONS = { 89 CONTAINER_ARRAYLIST_PROTOTYPE_FUNCTIONS(CONTAINER_ARRAYLIST_FUNCTION_ENTRY) 90 }; 91 #undef CONTAINER_ARRAYLIST_FUNCTION_ENTRY 92 }; 93 } // namespace panda::ecmascript::containers 94 #endif // ECMASCRIPT_CONTAINERS_CONTAINERS_ARRAYLIST_H 95