• 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_BUILTINS_BUILTINS_ARRAYBUFFER_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_ARRAYBUFFER_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/base/number_helper.h"
21 #include "ecmascript/js_dataview.h"
22 
23 namespace panda::ecmascript::builtins {
24 static constexpr double NUMBER_HALF = 0.5;
25 static constexpr uint32_t BITS_EIGHT = 8;
26 static constexpr uint32_t BITS_TWENTY_FOUR = 24;
27 static constexpr uint32_t BITS_FORTY = 40;
28 static constexpr uint32_t BITS_FIFTY_SIX = 56;
29 using DataViewType = ecmascript::DataViewType;
30 union UnionType32 {
31     uint32_t uValue;
32     float value;
33 };
34 union UnionType64 {
35     uint64_t uValue;
36     double value;
37 };
38 class BuiltinsArrayBuffer : public base::BuiltinsBase {
39 public:
40     enum NumberSize : uint8_t { UINT16 = 2, INT16 = 2, UINT32 = 4, INT32 = 4, FLOAT32 = 4, FLOAT64 = 8 };
41 
42     // 24.1.2.1 ArrayBuffer(length)
43     static JSTaggedValue ArrayBufferConstructor(EcmaRuntimeCallInfo *argv);
44     // 24.1.3.1 ArrayBuffer.isView(arg)
45     static JSTaggedValue IsView(EcmaRuntimeCallInfo *argv);
46     // 24.1.3.3 get ArrayBuffer[@@species]
47     static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
48     // 24.1.4.1 get ArrayBuffer.prototype.byteLength
49     static JSTaggedValue GetByteLength(EcmaRuntimeCallInfo *argv);
50     // 24.1.4.3 ArrayBuffer.prototype.slice()
51     static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv);
52     // 24.1.1.2 IsDetachedBuffer(arrayBuffer)
53     static bool IsDetachedBuffer(JSTaggedValue arrayBuffer);
54     // 24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isLittleEndian )
55     static JSTaggedValue GetValueFromBuffer(JSTaggedValue arrBuf, uint32_t byteIndex, DataViewType type,
56                                             bool littleEndian);
57     // 24.1.1.6 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isLittleEndian )
58     static JSTaggedValue SetValueInBuffer(JSTaggedValue arrBuf, uint32_t byteIndex, DataViewType type,
59                                           JSTaggedNumber value, bool littleEndian);
60     // 24.1.1.4 CloneArrayBuffer( srcBuffer, srcByteOffset [, cloneConstructor] )
61     static JSTaggedValue CloneArrayBuffer(JSThread *thread, const JSHandle<JSTaggedValue> &srcBuffer,
62                                           uint32_t srcByteOffset, JSHandle<JSTaggedValue> constructor);
63     // 24.1.1.1 AllocateArrayBuffer(constructor, byteLength)
64     static JSTaggedValue AllocateArrayBuffer(JSThread *thread, const JSHandle<JSTaggedValue> &newTarget,
65                                              double byteLength);
66 
67 private:
68     template <typename T>
69     static T LittleEndianToBigEndian(T liValue);
70 
71     static uint64_t LittleEndianToBigEndianUint64(uint64_t liValue);
72 
73     template<typename T>
74     static void SetTypeData(uint8_t *block, T value, uint32_t index);
75 
76     template<typename T, NumberSize size>
77     static JSTaggedValue GetValueFromBufferForInteger(uint8_t *block, uint32_t byteIndex, bool littleEndian);
78 
79     template<typename T, typename UnionType, NumberSize size>
80     static JSTaggedValue GetValueFromBufferForFloat(uint8_t *block, uint32_t byteIndex, bool littleEndian);
81 
82     template<typename T>
83     static void SetValueInBufferForByte(double val, uint8_t *block, uint32_t byteIndex);
84 
85     static void SetValueInBufferForUint8Clamped(double val, uint8_t *block, uint32_t byteIndex);
86 
87     template<typename T>
88     static void SetValueInBufferForInteger(double val, uint8_t *block, uint32_t byteIndex, bool littleEndian);
89 
90     template<typename T>
91     static void SetValueInBufferForFloat(double val, uint8_t *block, uint32_t byteIndex, bool littleEndian);
92 };
93 }  // namespace panda::ecmascript::builtins
94 
95 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_ARRAYBUFFER_H
96