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