• 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_BASE_TYPED_ARRAY_HELPER_H
17 #define ECMASCRIPT_BASE_TYPED_ARRAY_HELPER_H
18 
19 #include <string>
20 
21 #include "ecmascript/base/builtins_base.h"
22 #include "ecmascript/js_dataview.h"
23 #include "ecmascript/js_typed_array.h"
24 #include "ecmascript/shared_objects/js_shared_typed_array.h"
25 
26 #include "ecmascript/builtins/builtins_shared_typedarray.h"
27 #include "ecmascript/builtins/builtins_typedarray.h"
28 
29 namespace panda::ecmascript::base {
30 enum ElementSize : uint8_t { ONE = 1, TWO = 2, FOUR = 4, EIGHT = 8 };
31 
32 enum class TypedArrayKind : uint8_t {
33     NON_SHARED = 0,
34     SHARED = 1
35 };
36 
37 template <TypedArrayKind typedArrayKind>
38 struct BuiltinsArrayBufferType {};
39 
40 class TypedArrayHelper {
41 public:
42     static JSTaggedValue TypedArrayConstructor(EcmaRuntimeCallInfo *argv,
43                                                const JSHandle<JSTaggedValue> &constructorName,
44                                                const DataViewType arrayType);
45     static JSTaggedValue SharedTypedArrayConstructor(EcmaRuntimeCallInfo *argv,
46                                                      const JSHandle<JSTaggedValue> &constructorName,
47                                                      const DataViewType arrayType);
48     static JSHandle<JSObject> FastCreateTypedArray(JSThread *thread,
49                                                    const JSHandle<JSTaggedValue> &constructorName,
50                                                    uint32_t length,
51                                                    const DataViewType arrayType);
52     static JSHandle<JSObject> AllocateTypedArray(JSThread *thread,
53                                                  const JSHandle<JSTaggedValue> &constructorName,
54                                                  const JSHandle<JSTaggedValue> &newTarget,
55                                                  const DataViewType arrayType);
56     static JSHandle<JSObject> AllocateTypedArray(JSThread *thread,
57                                                  const JSHandle<JSTaggedValue> &constructorName,
58                                                  const JSHandle<JSTaggedValue> &newTarget, uint32_t length,
59                                                  const DataViewType arrayType);
60     static JSHandle<JSObject> AllocateSharedTypedArray(JSThread *thread,
61                                                        const JSHandle<JSTaggedValue> &constructorName,
62                                                        const JSHandle<JSTaggedValue> &newTarget,
63                                                        const DataViewType arrayType);
64     static JSHandle<JSObject> AllocateSharedTypedArray(JSThread *thread,
65                                                        const JSHandle<JSTaggedValue> &constructorName,
66                                                        const JSHandle<JSTaggedValue> &newTarget, uint32_t length,
67                                                        const DataViewType arrayType);
68     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
69     static JSHandle<JSObject> TypedArraySpeciesCreate(JSThread *thread, const JSHandle<JSTypedArray> &obj,
70                                                       uint32_t argc, JSTaggedType argv[]);
71     static JSHandle<JSObject> TypedArrayCreate(JSThread *thread, const JSHandle<JSTaggedValue> &constructor,
72                                                uint32_t argc, const JSTaggedType argv[]);
73     static JSHandle<JSObject> TypedArrayCreateSameType(JSThread *thread, const JSHandle<JSTypedArray> &obj,
74                                                        uint32_t argc, JSTaggedType argv[]);
75     static JSTaggedValue ValidateTypedArray(JSThread *thread, const JSHandle<JSTaggedValue> &value);
76     inline static DataViewType GetType(const JSHandle<JSTypedArray> &obj);
77     inline static DataViewType GetType(JSType type);
78     inline static uint32_t GetElementSize(const JSHandle<JSTypedArray> &obj);
79     inline static uint32_t GetElementSize(JSType type);
80     inline static JSHandle<JSTaggedValue> GetConstructor(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
81     inline static JSHandle<JSFunction> GetConstructorFromType(JSThread *thread,
82                                                               const DataViewType arrayType);
83     inline static JSHandle<JSFunction> GetSharedConstructorFromType(JSThread *thread,
84                                                                     const DataViewType arrayType);
85     inline static JSHandle<JSTaggedValue> GetConstructorNameFromType(JSThread *thread,
86                                                                      const DataViewType arrayType);
87     inline static JSHandle<JSTaggedValue> GetSharedConstructorNameFromType(JSThread *thread,
88                                                                            const DataViewType arrayType);
89     inline static JSHandle<JSHClass> GetOnHeapHclassFromType(
90         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
91     inline static JSHandle<JSHClass> GetNotOnHeapHclassFromType(
92         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
93     inline static JSHandle<JSHClass> GetSharedOnHeapHclassFromType(
94         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
95     inline static JSHandle<JSHClass> GetSharedNotOnHeapHclassFromType(
96         JSThread *thread, const JSHandle<JSSharedTypedArray> &obj, const DataViewType arrayType);
97     inline static uint32_t GetSizeFromType(const DataViewType arrayType);
98     inline static bool IsAccessorHasChanged(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
99     static int32_t SortCompare(JSThread *thread, const JSHandle<JSTaggedValue> &callbackfnHandle,
100                                const JSHandle<JSTaggedValue> &buffer, const JSHandle<JSTaggedValue> &firstValue,
101                                const JSHandle<JSTaggedValue> &secondValue);
102     static bool IsNativeArrayIterator(JSThread *thread,
103         const JSHandle<JSTaggedValue> &obj, JSHandle<JSTaggedValue> &iterMethod);
104     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
105     static JSHandle<JSObject> AllocateTypedArrayBuffer(JSThread *thread, const JSHandle<JSObject> &obj,
106                                                        uint64_t length, const DataViewType arrayType);
107 
108     #define DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE(Type)                                                          \
109         inline static JSHandle<JSHClass> GetOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass);
110     TYPED_ARRAY_TYPES(DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE)
111     SHARED_TYPED_ARRAY_TYPES(DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE)
112     #undef DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE
113 
114     #define DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE(Type)                                                          \
115         inline static JSHandle<JSHClass> GetNotOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass);
116     TYPED_ARRAY_TYPES(DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE)
117     SHARED_TYPED_ARRAY_TYPES(DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE)
118     #undef DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE
119 
120 private:
121     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
122     static JSTaggedValue CreateFromOrdinaryObject(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
123                                                   const DataViewType arrayType);
124     static JSTaggedValue CreateFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
125                                               const DataViewType arrayType);
126     static JSTaggedValue CreateSharedFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
127                                               const DataViewType arrayType);
128     static JSTaggedValue CreateFromArrayBuffer(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
129                                                const DataViewType arrayType);
130     static JSTaggedValue CreateFromSendableArrayBuffer(EcmaRuntimeCallInfo *argv,
131                                                        const JSHandle<JSObject> &obj,
132                                                        const DataViewType arrayType);
133     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
134     static JSTaggedValue FastCopyElementFromArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
135                                                          const DataViewType arrayType);
136 };
137 }  // namespace panda::ecmascript::base
138 
139 #endif  // ECMASCRIPT_BASE_TYPED_ARRAY_HELPER_H
140