• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_BUILTINS_BUILTINS_TYPEDARRAY_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_TYPEDARRAY_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 
21 #define TYPED_ARRAY_TYPES(V)      \
22     V(Int8Array)                  \
23     V(Uint8Array)                 \
24     V(Uint8ClampedArray)          \
25     V(Int16Array)                 \
26     V(Uint16Array)                \
27     V(Int32Array)                 \
28     V(Uint32Array)                \
29     V(Float32Array)               \
30     V(Float64Array)               \
31     V(BigInt64Array)              \
32     V(BigUint64Array)
33 
34 // All types of %TypedArray%.
35 // V(Type, TYPE, bytesPerElement) where JSType::JS_##TYPE is the type index.
36 #define BUILTIN_TYPED_ARRAY_TYPES(V)                \
37     V(Int8Array,         INT8_ARRAY,          1)    \
38     V(Uint8Array,        UINT8_ARRAY,         1)    \
39     V(Uint8ClampedArray, UINT8_CLAMPED_ARRAY, 1)    \
40     V(Int16Array,        INT16_ARRAY,         2)    \
41     V(Uint16Array,       UINT16_ARRAY,        2)    \
42     V(Int32Array,        INT32_ARRAY,         4)    \
43     V(Uint32Array,       UINT32_ARRAY,        4)    \
44     V(Float32Array,      FLOAT32_ARRAY,       4)    \
45     V(Float64Array,      FLOAT64_ARRAY,       8)    \
46     V(BigInt64Array,     BIGINT64_ARRAY,      8)    \
47     V(BigUint64Array,    BIGUINT64_ARRAY,     8)
48 
49 // List of functions in %TypedArray%, excluding the '@@' properties.
50 // V(name, func, length, stubIndex)
51 // where BuiltinsTypedArray::func refers to the native implementation of %TypedArray%[name].
52 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
53 #define BUILTIN_TYPED_ARRAY_FUNCTIONS(V)                            \
54     /* %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ) */    \
55     V("from", From, 1, INVALID)                                     \
56     /* %TypedArray%.of ( ...items ) */                              \
57     V("of",   Of,   0, INVALID)
58 
59 // List of get accessors in %TypedArray%.prototype, excluding the '@@' properties.
60 // V(name, func, stubIndex)
61 // where BuiltinsTypedArray::func refers to the native implementation.
62 #define BUILTIN_TYPED_ARRAY_PROTOTYPE_GETTERS(V)                                            \
63     V("buffer",     GetBuffer,     INVALID) /* get %TypedArray%.prototype.buffer */         \
64     V("byteLength", GetByteLength, INVALID) /* get %TypedArray%.prototype.byteLength */     \
65     V("byteOffset", GetByteOffset, INVALID) /* get %TypedArray%.prototype.byteOffset */     \
66     V("length",     GetLength,     INVALID) /* get %TypedArray%.prototype.length */
67 
68 // List of functions in %TypedArray%.prototype, excluding the constructor and '@@' properties.
69 // V(name, func, length, stubIndex)
70 // where BuiltinsTypedArray::func refers to the native implementation of %TypedArray%.prototype[name].
71 // The following functions are not included:
72 //   - %TypedArray%.prototype.toString ( ), which is strictly equal to Array.prototype.toString
73 #define BUILTIN_TYPED_ARRAY_PROTOTYPE_FUNCTIONS(V)                                      \
74     /* %TypedArray%.prototype.at ( index ) */                                           \
75     V("at",             At,             1, INVALID)                                     \
76     /* %TypedArray%.prototype.copyWithin ( target, start [ , end ] ) */                 \
77     V("copyWithin",     CopyWithin,     2, INVALID)                                     \
78     /* %TypedArray%.prototype.entries ( ) */                                            \
79     V("entries",        Entries,        0, INVALID)                                     \
80     /* %TypedArray%.prototype.every ( callbackfn [ , thisArg ] ) */                     \
81     V("every",          Every,          1, INVALID)                                     \
82     /* %TypedArray%.prototype.fill ( value [ , start [ , end ] ] ) */                   \
83     V("fill",           Fill,           1, INVALID)                                     \
84     /* %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] ) */                    \
85     V("filter",         Filter,         1, INVALID)                                     \
86     /* %TypedArray%.prototype.find ( predicate [ , thisArg ] ) */                       \
87     V("find",           Find,           1, INVALID)                                     \
88     /* %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) */                  \
89     V("findIndex",      FindIndex,      1, INVALID)                                     \
90     /* %TypedArray%.prototype.findLast ( predicate [ , thisArg ] ) */                   \
91     V("findLast",       FindLast,       1, INVALID)                                     \
92     /* %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) */              \
93     V("findLastIndex",  FindLastIndex,  1, INVALID)                                     \
94     /* %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] ) */                   \
95     V("forEach",        ForEach,        1, INVALID)                                     \
96     /* %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] ) */             \
97     V("includes",       Includes,       1, INVALID)                                     \
98     /* %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] ) */              \
99     V("indexOf",        IndexOf,        1, INVALID)                                     \
100     /* %TypedArray%.prototype.join ( separator ) */                                     \
101     V("join",           Join,           1, INVALID)                                     \
102     /* %TypedArray%.prototype.keys ( ) */                                               \
103     V("keys",           Keys,           0, INVALID)                                     \
104     /* %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) */          \
105     V("lastIndexOf",    LastIndexOf,    1, INVALID)                                     \
106     /* %TypedArray%.prototype.map ( callbackfn [ , thisArg ] ) */                       \
107     V("map",            Map,            1, INVALID)                                     \
108     /* %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) */               \
109     V("reduce",         Reduce,         1, INVALID)                                     \
110     /* %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] ) */          \
111     V("reduceRight",    ReduceRight,    1, INVALID)                                     \
112     /* %TypedArray%.prototype.reverse ( ) */                                            \
113     V("reverse",        Reverse,        0, INVALID)                                     \
114     /* %TypedArray%.prototype.set ( source [ , offset ] ) */                            \
115     V("set",            Set,            1, INVALID)                                     \
116     /* %TypedArray%.prototype.slice ( start, end ) */                                   \
117     V("slice",          Slice,          2, INVALID)                                     \
118     /* %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ) */                      \
119     V("some",           Some,           1, INVALID)                                     \
120     /* %TypedArray%.prototype.sort ( comparefn ) */                                     \
121     V("sort",           Sort,           1, INVALID)                                     \
122     /* %TypedArray%.prototype.subarray ( begin, end ) */                                \
123     V("subarray",       Subarray,       2, TypedArraySubArray)                          \
124     /* %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */       \
125     V("toLocaleString", ToLocaleString, 0, INVALID)                                     \
126     /* %TypedArray%.prototype.toReversed ( ) */                                         \
127     V("toReversed",     ToReversed,     0, INVALID)                                     \
128     /* %TypedArray%.prototype.toSorted ( comparefn ) */                                 \
129     V("toSorted",       ToSorted,       1, INVALID)                                     \
130     /* %TypedArray%.prototype.values ( ) */                                             \
131     V("values",         Values,         0, INVALID)                                     \
132     /* %TypedArray%.prototype.with ( index, value ) */                                  \
133     V("with",           With,           2, INVALID)
134 
135 namespace panda::ecmascript::builtins {
136 class BuiltinsTypedArray : public base::BuiltinsBase {
137 public:
138     enum SeparatorFlag : int { MINUS_ONE = -1, MINUS_TWO = -2 };
139     // 22.2.1
140     static JSTaggedValue TypedArrayBaseConstructor(EcmaRuntimeCallInfo *argv);
141     static JSTaggedValue Int8ArrayConstructor(EcmaRuntimeCallInfo *argv);
142     static JSTaggedValue Uint8ArrayConstructor(EcmaRuntimeCallInfo *argv);
143     static JSTaggedValue Uint8ClampedArrayConstructor(EcmaRuntimeCallInfo *argv);
144     static JSTaggedValue Int16ArrayConstructor(EcmaRuntimeCallInfo *argv);
145     static JSTaggedValue Uint16ArrayConstructor(EcmaRuntimeCallInfo *argv);
146     static JSTaggedValue Int32ArrayConstructor(EcmaRuntimeCallInfo *argv);
147     static JSTaggedValue Uint32ArrayConstructor(EcmaRuntimeCallInfo *argv);
148     static JSTaggedValue Float32ArrayConstructor(EcmaRuntimeCallInfo *argv);
149     static JSTaggedValue Float64ArrayConstructor(EcmaRuntimeCallInfo *argv);
150     static JSTaggedValue BigInt64ArrayConstructor(EcmaRuntimeCallInfo *argv);
151     static JSTaggedValue BigUint64ArrayConstructor(EcmaRuntimeCallInfo *argv);
152 
153     // 22.2.1.2.1
154     static JSTaggedValue AllocateTypedArray(EcmaRuntimeCallInfo *argv);
155 
156     // 22.2.2.1
157     static JSTaggedValue From(EcmaRuntimeCallInfo *argv);
158     // 22.2.2.2
159     static JSTaggedValue Of(EcmaRuntimeCallInfo *argv);
160     // 22.2.2.4
161     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
162 
163     // prototype
164     // 22.2.3.1
165     static JSTaggedValue GetBuffer(EcmaRuntimeCallInfo *argv);
166     // 22.2.3.2
167     static JSTaggedValue GetByteLength(EcmaRuntimeCallInfo *argv);
168     // 22.2.3.3
169     static JSTaggedValue GetByteOffset(EcmaRuntimeCallInfo *argv);
170     // 22.2.3.5
171     static JSTaggedValue CopyWithin(EcmaRuntimeCallInfo *argv);
172     // 22.2.3.6
173     static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
174     // 22.2.3.7
175     static JSTaggedValue Every(EcmaRuntimeCallInfo *argv);
176     // 22.2.3.8
177     static JSTaggedValue Fill(EcmaRuntimeCallInfo *argv);
178     // 22.2.3.9
179     static JSTaggedValue Filter(EcmaRuntimeCallInfo *argv);
180     // 22.2.3.10
181     static JSTaggedValue Find(EcmaRuntimeCallInfo *argv);
182     // 22.2.3.11
183     static JSTaggedValue FindIndex(EcmaRuntimeCallInfo *argv);
184     // 22.2.3.12
185     static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
186     // 22.2.3.13
187     static JSTaggedValue IndexOf(EcmaRuntimeCallInfo *argv);
188     // 22.2.3.14
189     static JSTaggedValue Join(EcmaRuntimeCallInfo *argv);
190     // 22.2.3.15
191     static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv);
192     // 22.2.3.16
193     static JSTaggedValue LastIndexOf(EcmaRuntimeCallInfo *argv);
194     // 22.2.3.17
195     static JSTaggedValue GetLength(EcmaRuntimeCallInfo *argv);
196     // 22.2.3.18
197     static JSTaggedValue Map(EcmaRuntimeCallInfo *argv);
198     // 22.2.3.19
199     static JSTaggedValue Reduce(EcmaRuntimeCallInfo *argv);
200     // 22.2.3.20
201     static JSTaggedValue ReduceRight(EcmaRuntimeCallInfo *argv);
202     // 22.2.3.21
203     static JSTaggedValue Reverse(EcmaRuntimeCallInfo *argv);
204     // 22.2.3.22
205     static JSTaggedValue Set(EcmaRuntimeCallInfo *argv);
206     // 22.2.3.23
207     static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv);
208     // 22.2.3.24
209     static JSTaggedValue Some(EcmaRuntimeCallInfo *argv);
210     // 22.2.3.25
211     static JSTaggedValue Sort(EcmaRuntimeCallInfo *argv);
212     // 22.2.3.26
213     static JSTaggedValue Subarray(EcmaRuntimeCallInfo *argv);
214     // 22.2.3.27
215     static JSTaggedValue ToLocaleString(EcmaRuntimeCallInfo *argv);
216     // 22.2.3.28
217     static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv);
218     // 22.2.3.29
219     static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
220     // 22.2.3.31
221     static JSTaggedValue ToStringTag(EcmaRuntimeCallInfo *argv);
222     // es12 23.2.3.13
223     static JSTaggedValue Includes(EcmaRuntimeCallInfo *argv);
224     // 23.2.3.1
225     static JSTaggedValue At(EcmaRuntimeCallInfo *argv);
226     // 23.2.3.32 %TypedArray%.prototype.toReversed ( )
227     static JSTaggedValue ToReversed(EcmaRuntimeCallInfo *argv);
228     // 23.2.3.13
229     static JSTaggedValue FindLast(EcmaRuntimeCallInfo *argv);
230     // 23.2.3.14
231     static JSTaggedValue FindLastIndex(EcmaRuntimeCallInfo *argv);
232     // 23.2.3.33
233     static JSTaggedValue ToSorted(EcmaRuntimeCallInfo *argv);
234     // 23.2.3.36
235     static JSTaggedValue With(EcmaRuntimeCallInfo *argv);
236     static const uint32_t MAX_ARRAY_INDEX = std::numeric_limits<uint32_t>::max();
237 
238     // Excluding the '@@' internal properties
GetTypedArrayFunctions()239     static Span<const base::BuiltinFunctionEntry> GetTypedArrayFunctions()
240     {
241         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_FUNCTIONS);
242     }
243 
244     // Excluding the '@@' internal properties
GetTypedArrayPrototypeAccessors()245     static Span<const base::BuiltinFunctionEntry> GetTypedArrayPrototypeAccessors()
246     {
247         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_PROTOTYPE_ACCESSORS);
248     }
249 
250     // Excluding the constructor and '@@' internal properties.
GetTypedArrayPrototypeFunctions()251     static Span<const base::BuiltinFunctionEntry> GetTypedArrayPrototypeFunctions()
252     {
253         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_PROTOTYPE_FUNCTIONS);
254     }
255 
GetNumPrototypeInlinedProperties()256     static size_t GetNumPrototypeInlinedProperties()
257     {
258         // 4 : 4 more inline properties in %TypedArray%.prototype for the following functions/accessors:
259         //   (1) %TypedArray%.prototype.constructor
260         //   (2) %TypedArray%.prototype.toString, which is strictly equal to Array.prototype.toString
261         //   (3) %TypedArray%.prototype[@@iterator]
262         //   (4) %TypedArray%.prototype[@@toStringTag]
263         return GetTypedArrayPrototypeFunctions().Size() +
264                GetTypedArrayPrototypeAccessors().Size() + 4;
265     }
266 
267 private:
268 #define BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY(name, func, length, id) \
269     base::BuiltinFunctionEntry::Create(name, BuiltinsTypedArray::func, length, kungfu::BuiltinsStubCSigns::id),
270 #define BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY(name, func, id)                          \
271     base::BuiltinFunctionEntry::Create<base::BuiltinFunctionEntry::IsAccessorBit>(  \
272         name, BuiltinsTypedArray::func, 0, kungfu::BuiltinsStubCSigns::id),
273 
274     static constexpr std::array TYPED_ARRAY_FUNCTIONS = {
275         BUILTIN_TYPED_ARRAY_FUNCTIONS(BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY)
276     };
277     static constexpr std::array TYPED_ARRAY_PROTOTYPE_ACCESSORS = {
278         BUILTIN_TYPED_ARRAY_PROTOTYPE_GETTERS(BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY)
279     };
280     static constexpr std::array TYPED_ARRAY_PROTOTYPE_FUNCTIONS = {
281         BUILTIN_TYPED_ARRAY_PROTOTYPE_FUNCTIONS(BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY)
282     };
283 #undef BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY
284 #undef BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY
285 };
286 }  // namespace panda::ecmascript::builtins
287 
288 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_TYPEDARRAY_H
289