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