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 namespace panda::ecmascript::base { 26 enum ElementSize : uint8_t { ONE = 1, TWO = 2, FOUR = 4, EIGHT = 8 }; 27 class TypedArrayHelper { 28 public: 29 static JSTaggedValue TypedArrayConstructor(EcmaRuntimeCallInfo *argv, 30 const JSHandle<JSTaggedValue> &constructorName, 31 const DataViewType arrayType); 32 static JSHandle<JSObject> AllocateTypedArray(JSThread *thread, 33 const JSHandle<JSTaggedValue> &constructorName, 34 const JSHandle<JSTaggedValue> &newTarget, 35 const DataViewType arrayType); 36 static JSHandle<JSObject> AllocateTypedArray(JSThread *thread, 37 const JSHandle<JSTaggedValue> &constructorName, 38 const JSHandle<JSTaggedValue> &newTarget, uint32_t length, 39 const DataViewType arrayType); 40 static JSHandle<JSObject> TypedArraySpeciesCreate(JSThread *thread, const JSHandle<JSTypedArray> &obj, 41 uint32_t argc, JSTaggedType argv[]); 42 static JSHandle<JSObject> TypedArrayCreate(JSThread *thread, const JSHandle<JSTaggedValue> &constructor, 43 uint32_t argc, const JSTaggedType argv[]); 44 static JSHandle<JSObject> TypedArrayCreateSameType(JSThread *thread, const JSHandle<JSTypedArray> &obj, 45 uint32_t argc, JSTaggedType argv[]); 46 static JSTaggedValue ValidateTypedArray(JSThread *thread, const JSHandle<JSTaggedValue> &value); 47 inline static DataViewType GetType(const JSHandle<JSTypedArray> &obj); 48 inline static DataViewType GetType(JSType type); 49 inline static uint32_t GetElementSize(const JSHandle<JSTypedArray> &obj); 50 inline static uint32_t GetElementSize(JSType type); 51 inline static JSHandle<JSTaggedValue> GetConstructor(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 52 inline static JSHandle<JSFunction> GetConstructorFromType(JSThread *thread, 53 const DataViewType arrayType); 54 inline static uint32_t GetSizeFromType(const DataViewType arrayType); 55 static int32_t SortCompare(JSThread *thread, const JSHandle<JSTaggedValue> &callbackfnHandle, 56 const JSHandle<JSTaggedValue> &buffer, const JSHandle<JSTaggedValue> &firstValue, 57 const JSHandle<JSTaggedValue> &secondValue); 58 59 private: 60 static JSTaggedValue CreateFromOrdinaryObject(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 61 const DataViewType arrayType); 62 static JSTaggedValue CreateFromTypedArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 63 const DataViewType arrayType); 64 static JSTaggedValue CreateFromArrayBuffer(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 65 const DataViewType arrayType); 66 static JSHandle<JSObject> AllocateTypedArrayBuffer(JSThread *thread, const JSHandle<JSObject> &obj, 67 uint64_t length, const DataViewType arrayType); 68 static JSTaggedValue FastCopyElementFromArray(EcmaRuntimeCallInfo *argv, const JSHandle<JSObject> &obj, 69 const DataViewType arrayType); 70 }; 71 } // namespace panda::ecmascript::base 72 73 #endif // ECMASCRIPT_BASE_TYPED_ARRAY_HELPER_H 74