• 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, TypedArrayGetByteLength) /* get %TypedArray%.prototype.byteLength */     \
66     V("byteOffset", GetByteOffset, TypedArrayGetByteOffset) /* 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.join ( separator ) */                                     \
96     V("join",           Join,           1, INVALID)                                     \
97     /* %TypedArray%.prototype.keys ( ) */                                               \
98     V("keys",           Keys,           0, INVALID)                                     \
99     /* %TypedArray%.prototype.map ( callbackfn [ , thisArg ] ) */                       \
100     V("map",            Map,            1, INVALID)                                     \
101     /* %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) */               \
102     V("reduce",         Reduce,         1, INVALID)                                     \
103     /* %TypedArray%.prototype.reverse ( ) */                                            \
104     V("reverse",        Reverse,        0, INVALID)                                     \
105     /* %TypedArray%.prototype.set ( source [ , offset ] ) */                            \
106     V("set",            Set,            1, INVALID)                                     \
107     /* %TypedArray%.prototype.slice ( start, end ) */                                   \
108     V("slice",          Slice,          2, INVALID)                                     \
109     /* %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ) */                      \
110     V("some",           Some,           1, INVALID)                                     \
111     /* %TypedArray%.prototype.sort ( comparefn ) */                                     \
112     V("sort",           Sort,           1, INVALID)                                     \
113     /* %TypedArray%.prototype.subarray ( begin, end ) */                                \
114     V("subarray",       Subarray,       2, INVALID)                                     \
115     /* %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */       \
116     V("toLocaleString", ToLocaleString, 0, INVALID)                                     \
117     /* %TypedArray%.prototype.toString ( ) */                                           \
118     V("toString",      ToString,        0, INVALID)                                     \
119     /* %TypedArray%.prototype.values ( ) */                                             \
120     V("values",         Values,         0, INVALID)
121 
122 namespace panda::ecmascript::builtins {
123 class BuiltinsSharedTypedArray : public base::BuiltinsBase {
124 public:
125     enum SeparatorFlag : int { MINUS_ONE = -1, MINUS_TWO = -2 };
126     static JSTaggedValue TypedArrayBaseConstructor(EcmaRuntimeCallInfo *argv);
127     static JSTaggedValue Int8ArrayConstructor(EcmaRuntimeCallInfo *argv);
128     static JSTaggedValue Uint8ArrayConstructor(EcmaRuntimeCallInfo *argv);
129     static JSTaggedValue Uint8ClampedArrayConstructor(EcmaRuntimeCallInfo *argv);
130     static JSTaggedValue Int16ArrayConstructor(EcmaRuntimeCallInfo *argv);
131     static JSTaggedValue Uint16ArrayConstructor(EcmaRuntimeCallInfo *argv);
132     static JSTaggedValue Int32ArrayConstructor(EcmaRuntimeCallInfo *argv);
133     static JSTaggedValue Uint32ArrayConstructor(EcmaRuntimeCallInfo *argv);
134     static JSTaggedValue Float32ArrayConstructor(EcmaRuntimeCallInfo *argv);
135     static JSTaggedValue Float64ArrayConstructor(EcmaRuntimeCallInfo *argv);
136     static JSTaggedValue BigInt64ArrayConstructor(EcmaRuntimeCallInfo *argv);
137     static JSTaggedValue BigUint64ArrayConstructor(EcmaRuntimeCallInfo *argv);
138 
139     static JSTaggedValue AllocateTypedArray(EcmaRuntimeCallInfo *argv);
140     static JSTaggedValue From(EcmaRuntimeCallInfo *argv);
141     static JSTaggedValue Of(EcmaRuntimeCallInfo *argv);
142     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
143 
144     // prototype
145     static JSTaggedValue GetBuffer(EcmaRuntimeCallInfo *argv);
146     static JSTaggedValue GetByteLength(EcmaRuntimeCallInfo *argv);
147     static JSTaggedValue GetByteOffset(EcmaRuntimeCallInfo *argv);
148     static JSTaggedValue CopyWithin(EcmaRuntimeCallInfo *argv);
149     static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv);
150     static JSTaggedValue Every(EcmaRuntimeCallInfo *argv);
151     static JSTaggedValue Fill(EcmaRuntimeCallInfo *argv);
152     static JSTaggedValue Filter(EcmaRuntimeCallInfo *argv);
153     static JSTaggedValue Find(EcmaRuntimeCallInfo *argv);
154     static JSTaggedValue FindIndex(EcmaRuntimeCallInfo *argv);
155     static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
156     static JSTaggedValue IndexOf(EcmaRuntimeCallInfo *argv);
157     static JSTaggedValue Join(EcmaRuntimeCallInfo *argv);
158     static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv);
159     static JSTaggedValue GetLength(EcmaRuntimeCallInfo *argv);
160     static JSTaggedValue Map(EcmaRuntimeCallInfo *argv);
161     static JSTaggedValue Reduce(EcmaRuntimeCallInfo *argv);
162     static JSTaggedValue Reverse(EcmaRuntimeCallInfo *argv);
163     static JSTaggedValue Set(EcmaRuntimeCallInfo *argv);
164     static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv);
165     static JSTaggedValue Some(EcmaRuntimeCallInfo *argv);
166     static JSTaggedValue Sort(EcmaRuntimeCallInfo *argv);
167     static JSTaggedValue Subarray(EcmaRuntimeCallInfo *argv);
168     static JSTaggedValue ToLocaleString(EcmaRuntimeCallInfo *argv);
169     static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv);
170     static JSTaggedValue Values(EcmaRuntimeCallInfo *argv);
171     static JSTaggedValue ToStringTag(EcmaRuntimeCallInfo *argv);
172     static JSTaggedValue Includes(EcmaRuntimeCallInfo *argv);
173     static JSTaggedValue At(EcmaRuntimeCallInfo *argv);
174     static const uint32_t MAX_ARRAY_INDEX = std::numeric_limits<uint32_t>::max();
175 
176     // Excluding the '@@' internal properties
GetTypedArrayFunctions()177     static Span<const base::BuiltinFunctionEntry> GetTypedArrayFunctions()
178     {
179         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_FUNCTIONS);
180     }
181 
182     // Excluding the '@@' internal properties
GetTypedArrayPrototypeAccessors()183     static Span<const base::BuiltinFunctionEntry> GetTypedArrayPrototypeAccessors()
184     {
185         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_PROTOTYPE_ACCESSORS);
186     }
187 
188     // Excluding the constructor and '@@' internal properties.
GetTypedArrayPrototypeFunctions()189     static Span<const base::BuiltinFunctionEntry> GetTypedArrayPrototypeFunctions()
190     {
191         return Span<const base::BuiltinFunctionEntry>(TYPED_ARRAY_PROTOTYPE_FUNCTIONS);
192     }
193 
GetNumPrototypeInlinedProperties()194     static size_t GetNumPrototypeInlinedProperties()
195     {
196         // 3 : 3 more inline properties in %TypedArray%.prototype for the following functions/accessors:
197         //   (1) %TypedArray%.prototype.constructor
198         //   (2) %TypedArray%.prototype[@@iterator]
199         //   (3) %TypedArray%.prototype[@@toStringTag]
200         return GetTypedArrayPrototypeFunctions().Size() +
201                GetTypedArrayPrototypeAccessors().Size() + 3;
202     }
203 
GetPrototypeProperties()204     static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties()
205     {
206         return Span<const std::pair<std::string_view, bool>>(TYPED_ARRAY_PROTOTYPE_PROPERTIES);
207     }
208 
GetFunctionProperties()209     static Span<const std::pair<std::string_view, bool>> GetFunctionProperties()
210     {
211         return Span<const std::pair<std::string_view, bool>>(TYPED_ARRAY_FUNCTION_PROPERTIES);
212     }
213 
GetSpecificFunctionProperties()214     static Span<const std::pair<std::string_view, bool>> GetSpecificFunctionProperties()
215     {
216         return Span<const std::pair<std::string_view, bool>>(SPECIFIC_TYPED_ARRAY_FUNCTION_PROPERTIES);
217     }
218 
GetSpecificArrayPrototypeProperties()219     static Span<const std::pair<std::string_view, bool>> GetSpecificArrayPrototypeProperties()
220     {
221         return Span<const std::pair<std::string_view, bool>>(SPECIFIC_TYPED_ARRAY_PROTOTYPE_PROPERTIES);
222     }
223 
224 private:
225 #define BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY(name, func, length, id) \
226     base::BuiltinFunctionEntry::Create(name, BuiltinsSharedTypedArray::func, length, kungfu::BuiltinsStubCSigns::id),
227 #define BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY(name, func, id)                          \
228     base::BuiltinFunctionEntry::Create<base::BuiltinFunctionEntry::IsAccessorBit>(  \
229         name, BuiltinsSharedTypedArray::func, 0, kungfu::BuiltinsStubCSigns::id),
230 
231     static constexpr std::array TYPED_ARRAY_FUNCTIONS = {
232         BUILTIN_SHARED_TYPED_ARRAY_FUNCTIONS(BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY)
233     };
234     static constexpr std::array TYPED_ARRAY_PROTOTYPE_ACCESSORS = {
235         BUILTIN_SHARED_TYPED_ARRAY_PROTOTYPE_GETTERS(BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY)
236     };
237     static constexpr std::array TYPED_ARRAY_PROTOTYPE_FUNCTIONS = {
238         BUILTIN_SHARED_TYPED_ARRAY_PROTOTYPE_FUNCTIONS(BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY)
239     };
240 
241 #define TYPED_ARRAY_PROPERTIES_PAIR(name, func, length, id) \
242     std::pair<std::string_view, bool>(name, false),
243 
244     static constexpr std::array TYPED_ARRAY_PROTOTYPE_PROPERTIES = {
245         std::pair<std::string_view, bool>("constructor", false),
246         BUILTIN_SHARED_TYPED_ARRAY_PROTOTYPE_FUNCTIONS(TYPED_ARRAY_PROPERTIES_PAIR)
247         std::pair<std::string_view, bool>("buffer", true),
248         std::pair<std::string_view, bool>("byteLength", true),
249         std::pair<std::string_view, bool>("byteOffset", true),
250         std::pair<std::string_view, bool>("length", true),
251         std::pair<std::string_view, bool>("[Symbol.iterator]", false),
252         std::pair<std::string_view, bool>("[Symbol.toStringTag]", true),
253     };
254 
255     static constexpr std::array TYPED_ARRAY_FUNCTION_PROPERTIES = {
256         std::pair<std::string_view, bool>("length", false),
257         std::pair<std::string_view, bool>("name", false),
258         std::pair<std::string_view, bool>("prototype", false),
259         BUILTIN_SHARED_TYPED_ARRAY_FUNCTIONS(TYPED_ARRAY_PROPERTIES_PAIR)
260         std::pair<std::string_view, bool>("[Symbol.species]", true),
261     };
262 
263     static constexpr std::array SPECIFIC_TYPED_ARRAY_FUNCTION_PROPERTIES = {
264         std::pair<std::string_view, bool>("length", false),
265         std::pair<std::string_view, bool>("name", false),
266         std::pair<std::string_view, bool>("prototype", false),
267         std::pair<std::string_view, bool>("BYTES_PER_ELEMENT", false),
268     };
269 
270     static constexpr std::array SPECIFIC_TYPED_ARRAY_PROTOTYPE_PROPERTIES = {
271         std::pair<std::string_view, bool>("constructor", false),
272         std::pair<std::string_view, bool>("BYTES_PER_ELEMENT", false),
273     };
274 #undef TYPED_ARRAY_PROPERTIES_PAIR
275 
276 #undef BUILTIN_TYPED_ARRAY_FUNCTION_ENTRY
277 #undef BUILTIN_TYPED_ARRAY_ACCESSOR_ENTRY
278 };
279 }  // namespace panda::ecmascript::builtins
280 
281 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_TYPEDARRAY_H
282