• 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> AllocateTypedArray(JSThread *thread,
49                                                  const JSHandle<JSTaggedValue> &constructorName,
50                                                  const JSHandle<JSTaggedValue> &newTarget,
51                                                  const DataViewType arrayType);
52     static JSHandle<JSObject> AllocateTypedArray(JSThread *thread,
53                                                  const JSHandle<JSTaggedValue> &constructorName,
54                                                  const JSHandle<JSTaggedValue> &newTarget, uint32_t length,
55                                                  const DataViewType arrayType);
56     static JSHandle<JSObject> AllocateSharedTypedArray(JSThread *thread,
57                                                        const JSHandle<JSTaggedValue> &constructorName,
58                                                        const JSHandle<JSTaggedValue> &newTarget,
59                                                        const DataViewType arrayType);
60     static JSHandle<JSObject> AllocateSharedTypedArray(JSThread *thread,
61                                                        const JSHandle<JSTaggedValue> &constructorName,
62                                                        const JSHandle<JSTaggedValue> &newTarget, uint32_t length,
63                                                        const DataViewType arrayType);
64     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
65     static JSHandle<JSObject> TypedArraySpeciesCreate(JSThread *thread, const JSHandle<JSTypedArray> &obj,
66                                                       uint32_t argc, JSTaggedType argv[]);
67     static JSHandle<JSObject> TypedArrayCreate(JSThread *thread, const JSHandle<JSTaggedValue> &constructor,
68                                                uint32_t argc, const JSTaggedType argv[]);
69     static JSHandle<JSObject> TypedArrayCreateSameType(JSThread *thread, const JSHandle<JSTypedArray> &obj,
70                                                        uint32_t argc, JSTaggedType argv[]);
71     static JSTaggedValue ValidateTypedArray(JSThread *thread, const JSHandle<JSTaggedValue> &value);
72     inline static DataViewType GetType(const JSHandle<JSTypedArray> &obj);
73     inline static DataViewType GetType(JSType type);
74     inline static uint32_t GetElementSize(const JSHandle<JSTypedArray> &obj);
75     inline static uint32_t GetElementSize(JSType type);
76     inline static JSHandle<JSTaggedValue> GetConstructor(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
77     inline static JSHandle<JSFunction> GetConstructorFromType(JSThread *thread,
78                                                               const DataViewType arrayType);
79     inline static JSHandle<JSFunction> GetSharedConstructorFromType(JSThread *thread,
80                                                                     const DataViewType arrayType);
81     inline static JSHandle<JSTaggedValue> GetConstructorNameFromType(JSThread *thread,
82                                                                      const DataViewType arrayType);
83     inline static JSHandle<JSTaggedValue> GetSharedConstructorNameFromType(JSThread *thread,
84                                                                            const DataViewType arrayType);
85     inline static JSHandle<JSHClass> GetOnHeapHclassFromType(
86         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
87     inline static JSHandle<JSHClass> GetNotOnHeapHclassFromType(
88         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
89     inline static JSHandle<JSHClass> GetSharedOnHeapHclassFromType(
90         JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType);
91     inline static JSHandle<JSHClass> GetSharedNotOnHeapHclassFromType(
92         JSThread *thread, const JSHandle<JSSharedTypedArray> &obj, const DataViewType arrayType);
93     inline static uint32_t GetSizeFromType(const DataViewType arrayType);
94     inline static bool IsAccessorHasChanged(const JSHandle<JSTaggedValue> &obj);
95     static int32_t SortCompare(JSThread *thread, const JSHandle<JSTaggedValue> &callbackfnHandle,
96                                const JSHandle<JSTaggedValue> &buffer, const JSHandle<JSTaggedValue> &firstValue,
97                                const JSHandle<JSTaggedValue> &secondValue);
98     static bool IsNativeArrayIterator(JSThread *thread,
99         const JSHandle<JSTaggedValue> &obj, JSHandle<JSTaggedValue> &iterMethod);
100     static JSTaggedValue someCommon(EcmaRuntimeCallInfo *argv, JSHandle<JSTaggedValue> thisObjVal, int64_t len);
101 
102     #define DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE(Type)                                                          \
103         inline static JSHandle<JSHClass> GetOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass);
104     TYPED_ARRAY_TYPES(DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE)
105     SHARED_TYPED_ARRAY_TYPES(DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE)
106     #undef DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE
107 
108     #define DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE(Type)                                                          \
109         inline static JSHandle<JSHClass> GetNotOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass);
110     TYPED_ARRAY_TYPES(DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE)
111     SHARED_TYPED_ARRAY_TYPES(DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE)
112     #undef DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE
113 
114 private:
115     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
116     static JSTaggedValue CreateFromOrdinaryObject(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
117                                                   const DataViewType arrayType);
118     static JSTaggedValue CreateFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
119                                               const DataViewType arrayType);
120     static JSTaggedValue CreateSharedFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
121                                               const DataViewType arrayType);
122     static JSTaggedValue CreateFromArrayBuffer(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
123                                                const DataViewType arrayType);
124     static JSTaggedValue CreateFromSendableArrayBuffer(EcmaRuntimeCallInfo *argv,
125                                                        const JSHandle<JSObject> &obj,
126                                                        const DataViewType arrayType);
127     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
128     static JSHandle<JSObject> AllocateTypedArrayBuffer(JSThread *thread, const JSHandle<JSObject> &obj,
129                                                        uint64_t length, const DataViewType arrayType);
130     template <TypedArrayKind typedArrayKind = TypedArrayKind::NON_SHARED>
131     static JSTaggedValue FastCopyElementFromArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj,
132                                                          const DataViewType arrayType);
133 };
134 }  // namespace panda::ecmascript::base
135 
136 #endif  // ECMASCRIPT_BASE_TYPED_ARRAY_HELPER_H
137