14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 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_STABLE_ARRAY_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_STABLE_ARRAY_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/typed_array_helper.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_dataview.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_typed_array.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript {
274514f5e3Sopenharmony_ciclass JSStableArray {
284514f5e3Sopenharmony_cipublic:
294514f5e3Sopenharmony_ci    enum SeparatorFlag : int { MINUS_ONE = -1, MINUS_TWO = -2 };
304514f5e3Sopenharmony_ci    static JSTaggedValue Push(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
314514f5e3Sopenharmony_ci    static JSTaggedValue Push(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
324514f5e3Sopenharmony_ci    static JSTaggedValue Pop(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
334514f5e3Sopenharmony_ci    static JSTaggedValue Pop(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
344514f5e3Sopenharmony_ci    static JSTaggedValue Splice(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv, uint32_t start,
354514f5e3Sopenharmony_ci                                uint32_t insertCount, uint32_t actualDeleteCount,
364514f5e3Sopenharmony_ci                                JSHandle<JSObject> newArrayHandle, uint32_t len);
374514f5e3Sopenharmony_ci    static JSTaggedValue Splice(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv, uint32_t start,
384514f5e3Sopenharmony_ci                                uint32_t insertCount, uint32_t actualDeleteCount,
394514f5e3Sopenharmony_ci                                JSHandle<JSObject> newArrayHandle, uint32_t len);
404514f5e3Sopenharmony_ci    static JSTaggedValue Shift(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
414514f5e3Sopenharmony_ci    static JSTaggedValue Shift(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
424514f5e3Sopenharmony_ci    static JSTaggedValue Join(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
434514f5e3Sopenharmony_ci    static JSTaggedValue HandleFindIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
444514f5e3Sopenharmony_ci                                                 JSHandle<JSTaggedValue> callbackFnHandle,
454514f5e3Sopenharmony_ci                                                 JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
464514f5e3Sopenharmony_ci    static JSTaggedValue HandleFindLastIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
474514f5e3Sopenharmony_ci                                                     JSHandle<JSTaggedValue> callbackFnHandle,
484514f5e3Sopenharmony_ci                                                     JSHandle<JSTaggedValue> thisArgHandle, int64_t &k);
494514f5e3Sopenharmony_ci    static JSTaggedValue HandleEveryOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
504514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> callbackFnHandle,
514514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
524514f5e3Sopenharmony_ci    static JSTaggedValue HandleSomeOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
534514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> callbackFnHandle,
544514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
554514f5e3Sopenharmony_ci    static JSTaggedValue HandleforEachOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
564514f5e3Sopenharmony_ci                                               JSHandle<JSTaggedValue> callbackFnHandle,
574514f5e3Sopenharmony_ci                                               JSHandle<JSTaggedValue> thisArgHandle, uint32_t len, uint32_t &k);
584514f5e3Sopenharmony_ci    static JSTaggedValue IndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver,
594514f5e3Sopenharmony_ci                                 JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len);
604514f5e3Sopenharmony_ci    static JSTaggedValue LastIndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver,
614514f5e3Sopenharmony_ci                                 JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len);
624514f5e3Sopenharmony_ci    static JSTaggedValue Filter(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle,
634514f5e3Sopenharmony_ci                                 EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t &toIndex);
644514f5e3Sopenharmony_ci    static JSTaggedValue Map(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle,
654514f5e3Sopenharmony_ci                                 EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t len);
664514f5e3Sopenharmony_ci    static JSTaggedValue Reverse(JSThread *thread, JSHandle<JSObject> thisObjHandle,
674514f5e3Sopenharmony_ci                                 int64_t &lower, uint32_t len);
684514f5e3Sopenharmony_ci    static JSTaggedValue FastReverse(JSThread *thread, JSHandle<TaggedArray> elements,
694514f5e3Sopenharmony_ci                                     int64_t &lower, uint32_t len, ElementsKind kind);
704514f5e3Sopenharmony_ci    static JSTaggedValue Concat(JSThread *thread, JSHandle<JSObject> newArrayHandle,
714514f5e3Sopenharmony_ci                                JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &n);
724514f5e3Sopenharmony_ci    template<base::TypedArrayKind typedArrayKind = base::TypedArrayKind::NON_SHARED>
734514f5e3Sopenharmony_ci    static JSTaggedValue FastCopyFromArrayToTypedArray(JSThread *thread, JSHandle<JSTypedArray> &target,
744514f5e3Sopenharmony_ci                                                       DataViewType targetType, uint64_t targetOffset,
754514f5e3Sopenharmony_ci                                                       uint32_t srcLength, JSHandle<JSObject> &obj);
764514f5e3Sopenharmony_ci    static JSTaggedValue At(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
774514f5e3Sopenharmony_ci    static JSTaggedValue At(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
784514f5e3Sopenharmony_ci    static JSTaggedValue With(JSThread *thread, JSHandle<JSArray> receiver,
794514f5e3Sopenharmony_ci                              int64_t insertCount, int64_t index, JSHandle<JSTaggedValue> value);
804514f5e3Sopenharmony_ci    static JSTaggedValue ToSpliced(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv,
814514f5e3Sopenharmony_ci                                   int64_t argc, int64_t actualStart, int64_t actualSkipCount, int64_t insertCount);
824514f5e3Sopenharmony_ci    static JSTaggedValue ToReversed(JSThread *thread, JSHandle<JSArray> receiver, int64_t insertCount);
834514f5e3Sopenharmony_ci    static JSTaggedValue Reduce(JSThread *thread, JSHandle<JSObject> thisObjHandle,
844514f5e3Sopenharmony_ci                                JSHandle<JSTaggedValue> callbackFnHandle,
854514f5e3Sopenharmony_ci                                JSMutableHandle<JSTaggedValue> accumulator, int64_t &k, int64_t &len);
864514f5e3Sopenharmony_ci    static JSTaggedValue Slice(JSThread *thread, JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &count);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSObject> &thisObj,
894514f5e3Sopenharmony_ci                              const JSHandle<JSTaggedValue> &callbackFnHandle);
904514f5e3Sopenharmony_ci    static JSTaggedValue Fill(JSThread *thread, const JSHandle<JSObject> &thisObj,
914514f5e3Sopenharmony_ci                              const JSHandle<JSTaggedValue> &value,
924514f5e3Sopenharmony_ci                              int64_t start, int64_t end, int64_t len);
934514f5e3Sopenharmony_ci    static JSTaggedValue HandleFindLastOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
944514f5e3Sopenharmony_ci                                                JSHandle<JSTaggedValue> callbackFnHandle,
954514f5e3Sopenharmony_ci                                                JSHandle<JSTaggedValue> thisArgHandle,
964514f5e3Sopenharmony_ci                                                JSMutableHandle<JSTaggedValue> &kValue, int64_t &k);
974514f5e3Sopenharmony_ci    static JSTaggedValue HandleReduceRightOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
984514f5e3Sopenharmony_ci                                                   JSHandle<JSTaggedValue> callbackFnHandle,
994514f5e3Sopenharmony_ci                                                   JSMutableHandle<JSTaggedValue> &accumulator,
1004514f5e3Sopenharmony_ci                                                   JSHandle<JSTaggedValue> thisArgHandle, int64_t &k);
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ciprivate:
1034514f5e3Sopenharmony_ci    static void SetSepValue(JSHandle<EcmaString> sepStringHandle, int &sep, uint32_t &sepLength);
1044514f5e3Sopenharmony_ci    enum class IndexOfType {
1054514f5e3Sopenharmony_ci        IndexOf,
1064514f5e3Sopenharmony_ci        LastIndexOf
1074514f5e3Sopenharmony_ci    };
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    struct IndexOfContext {
1104514f5e3Sopenharmony_ci        JSThread *thread;
1114514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> receiver;
1124514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> searchElement;
1134514f5e3Sopenharmony_ci        uint32_t fromIndex;
1144514f5e3Sopenharmony_ci        uint32_t length;
1154514f5e3Sopenharmony_ci    };
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci    template <class Predicate>
1184514f5e3Sopenharmony_ci    static JSTaggedValue FindRawData(IndexOfContext &ctx, Predicate &&predicate);
1194514f5e3Sopenharmony_ci    template <class Predicate>
1204514f5e3Sopenharmony_ci    static JSTaggedValue FindLastRawData(IndexOfContext &ctx, Predicate &&predicate);
1214514f5e3Sopenharmony_ci    template <class Predicate>
1224514f5e3Sopenharmony_ci    static JSTaggedValue FindRawDataDispatch(IndexOfType type, IndexOfContext &ctx, Predicate &&predicate);
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfZero(IndexOfType type, IndexOfContext &ctx);
1254514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfInt32(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1264514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfDouble(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1274514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfObjectAddress(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1284514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfString(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1294514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfBigInt(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1304514f5e3Sopenharmony_ci    static JSTaggedValue IndexOfDispatch(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
1314514f5e3Sopenharmony_ci    static JSTaggedValue UpdateArrayCapacity(JSHandle<JSObject> &thisObjHandle, uint32_t &len,
1324514f5e3Sopenharmony_ci                                             uint32_t &insertCount, uint32_t &actualDeleteCount,
1334514f5e3Sopenharmony_ci                                             JSHandle<JSArray> &receiver, uint32_t &start,
1344514f5e3Sopenharmony_ci                                             JSThread *thread, bool &needTransition,
1354514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> &holeHandle,
1364514f5e3Sopenharmony_ci                                             EcmaRuntimeCallInfo *argv, JSHandle<JSTaggedValue> &thisObjVal,
1374514f5e3Sopenharmony_ci                                             JSHandle<JSTaggedValue> &lengthKey);
1384514f5e3Sopenharmony_ci    static void HandleArray(JSHandle<JSObject> &newArrayHandle, uint32_t &actualDeleteCount,
1394514f5e3Sopenharmony_ci                            JSThread *thread, uint32_t &start, JSHandle<JSObject> &thisObjHandle,
1404514f5e3Sopenharmony_ci                            JSHandle<JSTaggedValue> &holeHandle);
1414514f5e3Sopenharmony_ci    static JSTaggedValue JoinUseTreeString(const JSThread* thread, JSHandle<JSTaggedValue> receiverValue,
1424514f5e3Sopenharmony_ci                                       JSHandle<EcmaString> sepStringHandle, int sep,
1434514f5e3Sopenharmony_ci                                       CVector<JSHandle<EcmaString>>& vec);
1444514f5e3Sopenharmony_ci    inline static bool WorthUseTreeString(int sep, size_t allocateLength, uint32_t len);
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ci    // Allocate object larger than 256 need liner search in the free object list,
1474514f5e3Sopenharmony_ci    // so try to use tree string when the join result is larger than 256.
1484514f5e3Sopenharmony_ci    static constexpr size_t TREE_STRING_THRESHOLD = 256;
1494514f5e3Sopenharmony_ci    static constexpr size_t NUM_2 = 2;
1504514f5e3Sopenharmony_ci};
1514514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1524514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_STABLE_ARRAY_H
153