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_JS_STABLE_ARRAY_H 17 #define ECMASCRIPT_JS_STABLE_ARRAY_H 18 19 #include "ecmascript/js_array.h" 20 #include "ecmascript/js_dataview.h" 21 #include "ecmascript/js_typed_array.h" 22 #include "ecmascript/js_tagged_value.h" 23 24 namespace panda::ecmascript { 25 class JSStableArray { 26 public: 27 enum SeparatorFlag : int { MINUS_ONE = -1, MINUS_TWO = -2 }; 28 static JSTaggedValue Push(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv); 29 static JSTaggedValue Pop(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv); 30 static JSTaggedValue Splice(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv, uint32_t start, 31 uint32_t insertCount, uint32_t actualDeleteCount, 32 JSHandle<JSObject> newArrayHandle, uint32_t len); 33 static JSTaggedValue Shift(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv); 34 static JSTaggedValue Join(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv); 35 static JSTaggedValue HandleFindIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle, 36 JSHandle<JSTaggedValue> callbackFnHandle, 37 JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k); 38 static JSTaggedValue HandleFindLastIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle, 39 JSHandle<JSTaggedValue> callbackFnHandle, 40 JSHandle<JSTaggedValue> thisArgHandle, int64_t &k); 41 static JSTaggedValue HandleEveryOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle, 42 JSHandle<JSTaggedValue> callbackFnHandle, 43 JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k); 44 static JSTaggedValue HandleforEachOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle, 45 JSHandle<JSTaggedValue> callbackFnHandle, 46 JSHandle<JSTaggedValue> thisArgHandle, uint32_t len, uint32_t &k); 47 static JSTaggedValue IndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver, 48 JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len); 49 static JSTaggedValue LastIndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver, 50 JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len); 51 static JSTaggedValue Filter(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle, 52 EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t &toIndex); 53 static JSTaggedValue Map(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle, 54 EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t len); 55 static JSTaggedValue Reverse(JSThread *thread, JSHandle<JSObject> thisObjHandle, 56 int64_t &lower, uint32_t len); 57 static JSTaggedValue Concat(JSThread *thread, JSHandle<JSObject> newArrayHandle, 58 JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &n); 59 static JSTaggedValue FastCopyFromArrayToTypedArray(JSThread *thread, JSHandle<JSTypedArray> &target, 60 DataViewType targetType, uint64_t targetOffset, 61 uint32_t srcLength, JSHandle<JSObject> &obj); 62 static JSTaggedValue At(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv); 63 static JSTaggedValue With(JSThread *thread, JSHandle<JSArray> receiver, 64 int64_t insertCount, int64_t index, JSHandle<JSTaggedValue> value); 65 static JSTaggedValue ToSpliced(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv, 66 int64_t argc, int64_t actualStart, int64_t actualSkipCount, int64_t insertCount); 67 static JSTaggedValue ToReversed(JSThread *thread, JSHandle<JSArray> receiver, int64_t insertCount); 68 static JSTaggedValue Reduce(JSThread *thread, JSHandle<JSObject> thisObjHandle, 69 JSHandle<JSTaggedValue> callbackFnHandle, 70 JSMutableHandle<JSTaggedValue> accumulator, int64_t &k, int64_t &len); 71 static JSTaggedValue Slice(JSThread *thread, JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &count); 72 73 static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSObject> &thisObj, 74 const JSHandle<JSTaggedValue> &callbackFnHandle); 75 static JSTaggedValue Fill(JSThread *thread, const JSHandle<JSObject> &thisObj, 76 const JSHandle<JSTaggedValue> &value, 77 int64_t start, int64_t end, int64_t len); 78 79 private: 80 static void SetSepValue(JSHandle<EcmaString> sepStringHandle, int &sep, uint32_t &sepLength); 81 enum class IndexOfType { 82 IndexOf, 83 LastIndexOf 84 }; 85 86 struct IndexOfContext { 87 JSThread *thread; 88 JSHandle<JSTaggedValue> receiver; 89 JSHandle<JSTaggedValue> searchElement; 90 uint32_t fromIndex; 91 uint32_t length; 92 }; 93 94 template <class Predicate> 95 static JSTaggedValue FindRawData(IndexOfContext &ctx, Predicate &&predicate); 96 template <class Predicate> 97 static JSTaggedValue FindLastRawData(IndexOfContext &ctx, Predicate &&predicate); 98 template <class Predicate> 99 static JSTaggedValue FindRawDataDispatch(IndexOfType type, IndexOfContext &ctx, Predicate &&predicate); 100 101 static JSTaggedValue IndexOfZero(IndexOfType type, IndexOfContext &ctx); 102 static JSTaggedValue IndexOfInt32(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 103 static JSTaggedValue IndexOfDouble(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 104 static JSTaggedValue IndexOfObjectAddress(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 105 static JSTaggedValue IndexOfString(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 106 static JSTaggedValue IndexOfBigInt(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 107 static JSTaggedValue IndexOfDispatch(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement); 108 }; 109 } // namespace panda::ecmascript 110 #endif // ECMASCRIPT_JS_STABLE_ARRAY_H 111