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_DATAVIEW_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_DATAVIEW_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/js_dataview.h"
21 
22 // List of functions in DataView, excluding the constructor and '@@' properties.
23 // V(name, func, length, stubIndex)
24 // where BuiltinsDataView::func refers to the native implementation of DataView.prototype[name].
25 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
26 #define BUILTIN_DATA_VIEW_PROTOTYPE_FUNCTIONS(V)                                    \
27     /* For %Type% of 1 byte: */                                                     \
28     /*   DataView.prototype.get%Type% ( byteOffset ) */                             \
29     /* For %Type% of 2 or more bytes: */                                            \
30     /*   DataView.prototype.get%Type% ( byteOffset [ , littleEndian ] ) */          \
31     V("getFloat32",     GetFloat32,     1, DataViewGetFloat32  )                    \
32     V("getFloat64",     GetFloat64,     1, DataViewGetFloat64  )                    \
33     V("getInt8",        GetInt8,        1, DataViewGetInt8     )                    \
34     V("getInt16",       GetInt16,       1, DataViewGetInt16    )                    \
35     V("getInt32",       GetInt32,       1, DataViewGetInt32    )                    \
36     V("getBigInt64",    GetBigInt64,    1, INVALID             )                    \
37     V("getUint16",      GetUint16,      1, DataViewGetUint16   )                    \
38     V("getUint32",      GetUint32,      1, DataViewGetUint32   )                    \
39     V("getUint8",       GetUint8,       1, DataViewGetUint8    )                    \
40     V("getBigUint64",   GetBigUint64,   1, INVALID             )                    \
41     /* For %Type% of 1 bytes: */                                                    \
42     /*   DataView.prototype.setInt8 ( byteOffset, value ) */                        \
43     /* For %Type% of 2 or more bytes: */                                            \
44     /*   DataView.prototype.setInt16 ( byteOffset, value [ , littleEndian ] ) */    \
45     V("setFloat32",     SetFloat32,     2, DataViewSetFloat32)                      \
46     V("setFloat64",     SetFloat64,     2, DataViewSetFloat64)                      \
47     V("setInt8",        SetInt8,        2, DataViewSetInt8)                         \
48     V("setInt16",       SetInt16,       2, DataViewSetInt16)                        \
49     V("setInt32",       SetInt32,       2, DataViewSetInt32)                        \
50     V("setBigInt64",    SetBigInt64,    2, INVALID)                                 \
51     V("setUint8",       SetUint8,       2, DataViewSetUint8)                        \
52     V("setUint16",      SetUint16,      2, DataViewSetUint16)                       \
53     V("setUint32",      SetUint32,      2, DataViewSetUint32)                       \
54     V("setBigUint64",   SetBigUint64,   2, INVALID)
55 
56 namespace panda::ecmascript::builtins {
57 using DataViewType = ecmascript::DataViewType;
58 class BuiltinsDataView : public base::BuiltinsBase {
59 public:
60     // 24.2.2.1 DataView (buffer [ , byteOffset [ , byteLength ] ] )
61     static JSTaggedValue DataViewConstructor(EcmaRuntimeCallInfo *argv);
62     // 24.2.4.1 get DataView.prototype.buffer
63     static JSTaggedValue GetBuffer(EcmaRuntimeCallInfo *argv);
64     // 24.2.4.2 get DataView.prototype.byteLength
65     static JSTaggedValue GetByteLength(EcmaRuntimeCallInfo *argv);
66     // 24.2.4.3 get DataView.prototype.byteOffset
67     static JSTaggedValue GetOffset(EcmaRuntimeCallInfo *argv);
68     // 24.2.4.5 DataView.prototype.getFloat32 ( byteOffset [ , littleEndian ] )
69     static JSTaggedValue GetFloat32(EcmaRuntimeCallInfo *argv);
70     // 24.2.4.6 DataView.prototype.getFloat64 ( byteOffset [ , littleEndian ] )
71     static JSTaggedValue GetFloat64(EcmaRuntimeCallInfo *argv);
72     // 24.2.4.7 DataView.prototype.getInt8 ( byteOffset )
73     static JSTaggedValue GetInt8(EcmaRuntimeCallInfo *argv);
74     // 24.2.4.8 DataView.prototype.getInt16 ( byteOffset [ , littleEndian ] )
75     static JSTaggedValue GetInt16(EcmaRuntimeCallInfo *argv);
76     // 24.2.4.9 DataView.prototype.getInt32 ( byteOffset [ , littleEndian ] )
77     static JSTaggedValue GetInt32(EcmaRuntimeCallInfo *argv);
78     // 24.2.4.10 DataView.prototype.getUint8 ( byteOffset )
79     static JSTaggedValue GetUint8(EcmaRuntimeCallInfo *argv);
80     // 24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
81     static JSTaggedValue GetUint16(EcmaRuntimeCallInfo *argv);
82     // 24.2.4.12 DataView.prototype.getUint32 ( byteOffset [ , littleEndian ] )
83     static JSTaggedValue GetUint32(EcmaRuntimeCallInfo *argv);
84     // 25.3.4.5 DataView.prototype.getBigInt64 ( byteOffset [ , littleEndian ] )
85     static JSTaggedValue GetBigInt64(EcmaRuntimeCallInfo *argv);
86     // 25.3.4.6 DataView.prototype.getBigUint64 ( byteOffset [ , littleEndian ] )
87     static JSTaggedValue GetBigUint64(EcmaRuntimeCallInfo *argv);
88     // 24.2.4.13 DataView.prototype.setFloat32 ( byteOffset, value [ , littleEndian ] )
89     static JSTaggedValue SetFloat32(EcmaRuntimeCallInfo *argv);
90     // 24.2.4.14 DataView.prototype.setFloat64 ( byteOffset, value [ , littleEndian ] )
91     static JSTaggedValue SetFloat64(EcmaRuntimeCallInfo *argv);
92     // 24.2.4.15 DataView.prototype.setInt8 ( byteOffset, value )
93     static JSTaggedValue SetInt8(EcmaRuntimeCallInfo *argv);
94     // 24.2.4.16 DataView.prototype.setInt16 ( byteOffset, value [ , littleEndian ] )
95     static JSTaggedValue SetInt16(EcmaRuntimeCallInfo *argv);
96     // 24.2.4.17 DataView.prototype.setInt32 ( byteOffset, value [ , littleEndian ] )
97     static JSTaggedValue SetInt32(EcmaRuntimeCallInfo *argv);
98     // 24.2.4.18 DataView.prototype.setUint8 ( byteOffset, value )
99     static JSTaggedValue SetUint8(EcmaRuntimeCallInfo *argv);
100     // 24.2.4.19 DataView.prototype.setUint16( byteOffset, value [ , littleEndian ] )
101     static JSTaggedValue SetUint16(EcmaRuntimeCallInfo *argv);
102     // 24.2.4.20 DataView.prototype.setUint32 ( byteOffset, value [ , littleEndian ] )
103     static JSTaggedValue SetUint32(EcmaRuntimeCallInfo *argv);
104     // 25.3.4.15 DataView.prototype.setBigInt64 ( byteOffset, value [ , littleEndian ] )
105     static JSTaggedValue SetBigInt64(EcmaRuntimeCallInfo *argv);
106     // 25.3.4.16 DataView.prototype.setBigUint64 ( byteOffset, value [ , littleEndian ] )
107     static JSTaggedValue SetBigUint64(EcmaRuntimeCallInfo *argv);
108 
109     // Excluding the '@@' internal properties.
GetDataViewPrototypeFunctions()110     static Span<const base::BuiltinFunctionEntry> GetDataViewPrototypeFunctions()
111     {
112         return Span<const base::BuiltinFunctionEntry>(DATA_VIEW_PROTOTYPE_FUNCTIONS);
113     }
114 
GetNumPrototypeInlinedProperties()115     static size_t GetNumPrototypeInlinedProperties()
116     {
117         // 5 : 5 more inline properties in DataView.prototype:
118         //   (1) DataView.prototype.constructor
119         //   (2) DataView.prototype [ @@toStringTag ]
120         //   (3) get buffer
121         //   (4) get byteLength
122         //   (5) get byteOffset
123         return GetDataViewPrototypeFunctions().Size() + 5;
124     }
125 
126 private:
127 #define BUILTIN_DATA_VIEW_FUNCTION_ENTRY(name, func, length, id) \
128     base::BuiltinFunctionEntry::Create(name, BuiltinsDataView::func, length, kungfu::BuiltinsStubCSigns::id),
129 
130     static constexpr std::array DATA_VIEW_PROTOTYPE_FUNCTIONS = {
131         BUILTIN_DATA_VIEW_PROTOTYPE_FUNCTIONS(BUILTIN_DATA_VIEW_FUNCTION_ENTRY)
132     };
133 
134 #undef BUILTIN_DATA_VIEW_FUNCTION_ENTRY
135 
136     // 24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
137     static JSTaggedValue GetViewValue(JSThread *thread, const JSHandle<JSTaggedValue> &view,
138                                       const JSHandle<JSTaggedValue> &requestIndex,
139                                       const JSHandle<JSTaggedValue> &littleEndian,
140                                       DataViewType type);
141     static JSTaggedValue SetViewValue(JSThread *thread, const JSHandle<JSTaggedValue> &view,
142                                       const JSHandle<JSTaggedValue> &requestIndex,
143                                       const JSHandle<JSTaggedValue> &littleEndian,
144                                       DataViewType type, const JSHandle<JSTaggedValue> &value);
145 
146     static JSTaggedValue GetTypedValue(EcmaRuntimeCallInfo *argv, DataViewType type);
147     static JSTaggedValue SetTypedValue(EcmaRuntimeCallInfo *argv, DataViewType type);
148 };
149 }  // namespace panda::ecmascript::builtins
150 
151 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_DATAVIEW_H
152