• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/typed_array_helper.h"
20 #include "ecmascript/js_array.h"
21 #include "ecmascript/js_dataview.h"
22 #include "ecmascript/js_hclass.h"
23 #include "ecmascript/js_typed_array.h"
24 #include "ecmascript/js_tagged_value.h"
25 
26 namespace panda::ecmascript {
27 class JSStableArray {
28 public:
29     enum SeparatorFlag : int { MINUS_ONE = -1, MINUS_TWO = -2 };
30     static JSTaggedValue Push(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
31     static JSTaggedValue Push(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
32     static JSTaggedValue Pop(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
33     static JSTaggedValue Pop(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
34     static JSTaggedValue Splice(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv, uint32_t start,
35                                 uint32_t insertCount, uint32_t actualDeleteCount,
36                                 JSHandle<JSObject> newArrayHandle, uint32_t len);
37     static JSTaggedValue Splice(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv, uint32_t start,
38                                 uint32_t insertCount, uint32_t actualDeleteCount,
39                                 JSHandle<JSObject> newArrayHandle, uint32_t len);
40     static JSTaggedValue Shift(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
41     static JSTaggedValue Shift(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
42     static JSTaggedValue Join(JSThread *thread, JSHandle<JSArray> receiver,
43                               JSHandle<EcmaString> sepStringHandle, int64_t length);
44     static JSTaggedValue Join(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
45     static JSTaggedValue HandleFindIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
46                                                  JSHandle<JSTaggedValue> callbackFnHandle,
47                                                  JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
48     static JSTaggedValue HandleFindLastIndexOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
49                                                      JSHandle<JSTaggedValue> callbackFnHandle,
50                                                      JSHandle<JSTaggedValue> thisArgHandle, int64_t &k);
51     static JSTaggedValue HandleEveryOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
52                                              JSHandle<JSTaggedValue> callbackFnHandle,
53                                              JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
54     static JSTaggedValue HandleSomeOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
55                                              JSHandle<JSTaggedValue> callbackFnHandle,
56                                              JSHandle<JSTaggedValue> thisArgHandle, uint32_t &k);
57     static JSTaggedValue HandleforEachOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
58                                                JSHandle<JSTaggedValue> callbackFnHandle,
59                                                JSHandle<JSTaggedValue> thisArgHandle, uint32_t len, uint32_t &k);
60     static JSTaggedValue IndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver,
61                                  JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len);
62     static JSTaggedValue LastIndexOf(JSThread *thread, JSHandle<JSTaggedValue> receiver,
63                                  JSHandle<JSTaggedValue> searchElement, uint32_t from, uint32_t len);
64     static JSTaggedValue Filter(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle,
65                                  EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t &toIndex);
66     static JSTaggedValue Map(JSHandle<JSObject> newArrayHandle, JSHandle<JSObject> thisObjHandle,
67                                  EcmaRuntimeCallInfo *argv, uint32_t &k, uint32_t len);
68     static JSTaggedValue Reverse(JSThread *thread, JSHandle<JSObject> thisObjHandle,
69                                  int64_t &lower, uint32_t len);
70     static JSTaggedValue FastReverse(JSThread *thread, JSHandle<TaggedArray> elements,
71                                      int64_t &lower, uint32_t len, ElementsKind kind);
72     static JSTaggedValue Concat(JSThread *thread, JSHandle<JSObject> newArrayHandle,
73                                 JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &n);
74     template<base::TypedArrayKind typedArrayKind = base::TypedArrayKind::NON_SHARED>
75     static JSTaggedValue FastCopyFromArrayToTypedArray(JSThread *thread, JSHandle<JSTypedArray> &target,
76                                                        DataViewType targetType, uint64_t targetOffset,
77                                                        uint32_t srcLength, JSHandle<JSObject> &obj);
78     static JSTaggedValue At(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv);
79     static JSTaggedValue At(JSHandle<JSSharedArray> receiver, EcmaRuntimeCallInfo *argv);
80     static JSTaggedValue With(JSThread *thread, JSHandle<JSArray> receiver,
81                               int64_t insertCount, int64_t index, JSHandle<JSTaggedValue> value);
82     static JSTaggedValue ToSpliced(JSHandle<JSArray> receiver, EcmaRuntimeCallInfo *argv,
83                                    int64_t argc, int64_t actualStart, int64_t actualSkipCount, int64_t insertCount);
84     static JSTaggedValue ToReversed(JSThread *thread, JSHandle<JSArray> receiver, int64_t insertCount);
85     static JSTaggedValue Reduce(JSThread *thread, JSHandle<JSObject> thisObjHandle,
86                                 JSHandle<JSTaggedValue> callbackFnHandle,
87                                 JSMutableHandle<JSTaggedValue> accumulator, int64_t &k, int64_t &len);
88     static JSTaggedValue Slice(JSThread *thread, JSHandle<JSObject> thisObjHandle, int64_t &k, int64_t &count);
89 
90     static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSObject> &thisObj,
91                               const JSHandle<JSTaggedValue> &callbackFnHandle);
92     static JSTaggedValue Fill(JSThread *thread, const JSHandle<JSObject> &thisObj,
93                               const JSHandle<JSTaggedValue> &value,
94                               int64_t start, int64_t end, int64_t len);
95     static JSTaggedValue HandleFindLastOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
96                                                 JSHandle<JSTaggedValue> callbackFnHandle,
97                                                 JSHandle<JSTaggedValue> thisArgHandle,
98                                                 JSMutableHandle<JSTaggedValue> &kValue, int64_t &k);
99     static JSTaggedValue HandleReduceRightOfStable(JSThread *thread, JSHandle<JSObject> thisObjHandle,
100                                                    JSHandle<JSTaggedValue> callbackFnHandle,
101                                                    JSMutableHandle<JSTaggedValue> &accumulator,
102                                                    JSHandle<JSTaggedValue> thisArgHandle, int64_t &k);
103 
104 private:
105     static void SetSepValue(JSHandle<EcmaString> sepStringHandle, int &sep, uint32_t &sepLength);
106     enum class IndexOfType {
107         IndexOf,
108         LastIndexOf
109     };
110 
111     struct IndexOfContext {
112         JSThread *thread;
113         JSHandle<JSTaggedValue> receiver;
114         JSHandle<JSTaggedValue> searchElement;
115         uint32_t fromIndex;
116         uint32_t length;
117     };
118 
119     template <class Predicate>
120     static JSTaggedValue FindRawData(IndexOfContext &ctx, Predicate &&predicate);
121     template <class Predicate>
122     static JSTaggedValue FindLastRawData(IndexOfContext &ctx, Predicate &&predicate);
123     template <class Predicate>
124     static JSTaggedValue FindRawDataDispatch(IndexOfType type, IndexOfContext &ctx, Predicate &&predicate);
125 
126     static JSTaggedValue IndexOfZero(IndexOfType type, IndexOfContext &ctx);
127     static JSTaggedValue IndexOfInt32(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
128     static JSTaggedValue IndexOfDouble(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
129     static JSTaggedValue IndexOfObjectAddress(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
130     static JSTaggedValue IndexOfString(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
131     static JSTaggedValue IndexOfBigInt(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
132     static JSTaggedValue IndexOfDispatch(IndexOfType type, IndexOfContext &ctx, JSTaggedValue searchElement);
133     static JSTaggedValue UpdateArrayCapacity(JSHandle<JSObject> &thisObjHandle, uint32_t &len,
134                                              uint32_t &insertCount, uint32_t &actualDeleteCount,
135                                              JSHandle<JSArray> &receiver, uint32_t &start,
136                                              JSThread *thread, bool &needTransition,
137                                              JSHandle<JSTaggedValue> &holeHandle,
138                                              EcmaRuntimeCallInfo *argv, JSHandle<JSTaggedValue> &thisObjVal,
139                                              JSHandle<JSTaggedValue> &lengthKey);
140     static void HandleArray(JSHandle<JSObject> &newArrayHandle, uint32_t &actualDeleteCount,
141                             JSThread *thread, uint32_t &start, JSHandle<JSObject> &thisObjHandle,
142                             JSHandle<JSTaggedValue> &holeHandle);
143     static JSTaggedValue JoinUseTreeString(const JSThread* thread, JSHandle<JSTaggedValue> receiverValue,
144                                        JSHandle<EcmaString> sepStringHandle, int sep,
145                                        CVector<JSHandle<EcmaString>>& vec);
146     inline static bool WorthUseTreeString(int sep, size_t allocateLength, uint32_t len);
147 
148     // Allocate object larger than 256 need liner search in the free object list,
149     // so try to use tree string when the join result is larger than 256.
150     static constexpr size_t TREE_STRING_THRESHOLD = 256;
151     static constexpr size_t NUM_2 = 2;
152 };
153 }  // namespace panda::ecmascript
154 #endif  // ECMASCRIPT_JS_STABLE_ARRAY_H
155