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