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 25 #include "ecmascript/builtins/builtins_typedarray.h" 26 27 namespace panda::ecmascript::base { 28 enum ElementSize : uint8_t { ONE = 1, TWO = 2, FOUR = 4, EIGHT = 8 }; 29 class TypedArrayHelper { 30 public: 31 static JSTaggedValue TypedArrayConstructor(EcmaRuntimeCallInfo *argv, 32 const JSHandle<JSTaggedValue> &constructorName, 33 const DataViewType arrayType); 34 static JSHandle<JSObject> AllocateTypedArray(JSThread *thread, 35 const JSHandle<JSTaggedValue> &constructorName, 36 const JSHandle<JSTaggedValue> &newTarget, 37 const DataViewType arrayType); 38 static JSHandle<JSObject> AllocateTypedArray(JSThread *thread, 39 const JSHandle<JSTaggedValue> &constructorName, 40 const JSHandle<JSTaggedValue> &newTarget, uint32_t length, 41 const DataViewType arrayType); 42 static JSHandle<JSObject> TypedArraySpeciesCreate(JSThread *thread, const JSHandle<JSTypedArray> &obj, 43 uint32_t argc, JSTaggedType argv[]); 44 static JSHandle<JSObject> TypedArrayCreate(JSThread *thread, const JSHandle<JSTaggedValue> &constructor, 45 uint32_t argc, const JSTaggedType argv[]); 46 static JSHandle<JSObject> TypedArrayCreateSameType(JSThread *thread, const JSHandle<JSTypedArray> &obj, 47 uint32_t argc, JSTaggedType argv[]); 48 static JSTaggedValue ValidateTypedArray(JSThread *thread, const JSHandle<JSTaggedValue> &value); 49 inline static DataViewType GetType(const JSHandle<JSTypedArray> &obj); 50 inline static DataViewType GetType(JSType type); 51 inline static uint32_t GetElementSize(const JSHandle<JSTypedArray> &obj); 52 inline static uint32_t GetElementSize(JSType type); 53 inline static JSHandle<JSTaggedValue> GetConstructor(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 54 inline static JSHandle<JSFunction> GetConstructorFromType(JSThread *thread, 55 const DataViewType arrayType); 56 inline static JSHandle<JSTaggedValue> GetConstructorNameFromType(JSThread *thread, 57 const DataViewType arrayType); 58 inline static JSHandle<JSHClass> GetOnHeapHclassFromType( 59 JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType); 60 inline static JSHandle<JSHClass> GetNotOnHeapHclassFromType( 61 JSThread *thread, const JSHandle<JSTypedArray> &obj, const DataViewType arrayType); 62 inline static uint32_t GetSizeFromType(const DataViewType arrayType); 63 inline static bool IsAccessorHasChanged(const JSHandle<JSTaggedValue> &obj); 64 static int32_t SortCompare(JSThread *thread, const JSHandle<JSTaggedValue> &callbackfnHandle, 65 const JSHandle<JSTaggedValue> &buffer, const JSHandle<JSTaggedValue> &firstValue, 66 const JSHandle<JSTaggedValue> &secondValue); 67 68 #define DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE(Type) \ 69 inline static JSHandle<JSHClass> GetOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass); 70 TYPED_ARRAY_TYPES(DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE) 71 #undef DEFINE_GET_ONHEAP_HCLASS_FROM_TYPE 72 73 #define DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE(Type) \ 74 inline static JSHandle<JSHClass> GetNotOnHeapHclass##Type(JSThread *thread, JSHClass* objHclass); 75 TYPED_ARRAY_TYPES(DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE) 76 #undef DEFINE_GET_NOT_ONHEAP_HCLASS_FROM_TYPE 77 78 private: 79 static JSTaggedValue CreateFromOrdinaryObject(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 80 const DataViewType arrayType); 81 static JSTaggedValue CreateFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 82 const DataViewType arrayType); 83 static JSTaggedValue CreateFromArrayBuffer(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 84 const DataViewType arrayType); 85 static JSHandle<JSObject> AllocateTypedArrayBuffer(JSThread *thread, const JSHandle<JSObject> &obj, 86 uint64_t length, const DataViewType arrayType); 87 static JSTaggedValue FastCopyElementFromArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 88 const DataViewType arrayType); 89 }; 90 } // namespace panda::ecmascript::base 91 92 #endif // ECMASCRIPT_BASE_TYPED_ARRAY_HELPER_H 93