Skip to content

Commit b6e2d92

Browse files
romandevmhdawson
authored andcommitted
src: enable DataView feature by default
This patch contains the following things: - Add a document for `DataView` feature - Remove NAPI_DATA_VIEW_FEATURE Refs: #196 PR-URL: #331 Refs: #196 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
1 parent 0a00e7c commit b6e2d92

File tree

5 files changed

+247
-10
lines changed

5 files changed

+247
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ still a work in progress as its not yet complete).
9797
- [ArrayBuffer](doc/array_buffer.md)
9898
- [TypedArray](doc/typed_array.md)
9999
- [TypedArrayOf](doc/typed_array_of.md)
100+
- [DataView](doc/dataview.md)
100101
- [Memory Management](doc/memory_management.md)
101102
- [Async Operations](doc/async_operations.md)
102103
- [AsyncWorker](doc/async_worker.md)

doc/dataview.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# DataView
2+
3+
The `Napi::DataView` class corresponds to the
4+
[JavaScript `DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
5+
class.
6+
7+
## Methods
8+
9+
### New
10+
11+
Allocates a new `Napi::DataView` instance with a given `Napi::ArrayBuffer`.
12+
13+
```cpp
14+
static Napi::DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer);
15+
```
16+
17+
- `[in] env`: The environment in which to create the `Napi::DataView` instance.
18+
- `[in] arrayBuffer` : `Napi::ArrayBuffer` underlying the `Napi::DataView`.
19+
20+
Returns a new `Napi::DataView` instance.
21+
22+
### New
23+
24+
Allocates a new `Napi::DataView` instance with a given `Napi::ArrayBuffer`.
25+
26+
```cpp
27+
static Napi::DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer, size_t byteOffset);
28+
```
29+
30+
- `[in] env`: The environment in which to create the `Napi::DataView` instance.
31+
- `[in] arrayBuffer` : `Napi::ArrayBuffer` underlying the `Napi::DataView`.
32+
- `[in] byteOffset` : The byte offset within the `Napi::ArrayBuffer` from which to start projecting the `Napi::DataView`.
33+
34+
Returns a new `Napi::DataView` instance.
35+
36+
### New
37+
38+
Allocates a new `Napi::DataView` instance with a given `Napi::ArrayBuffer`.
39+
40+
```cpp
41+
static Napi::DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer, size_t byteOffset, size_t byteLength);
42+
```
43+
44+
- `[in] env`: The environment in which to create the `Napi::DataView` instance.
45+
- `[in] arrayBuffer` : `Napi::ArrayBuffer` underlying the `Napi::DataView`.
46+
- `[in] byteOffset` : The byte offset within the `Napi::ArrayBuffer` from which to start projecting the `Napi::DataView`.
47+
- `[in] byteLength` : Number of elements in the `Napi::DataView`.
48+
49+
Returns a new `Napi::DataView` instance.
50+
51+
### Constructor
52+
53+
Initializes an empty instance of the `Napi::DataView` class.
54+
55+
```cpp
56+
DataView();
57+
```
58+
59+
### Constructor
60+
61+
Initializes a wrapper instance of an existing `Napi::DataView` instance.
62+
63+
```cpp
64+
DataView(napi_env env, napi_value value);
65+
```
66+
67+
- `[in] env`: The environment in which to create the `Napi::DataView` instance.
68+
- `[in] value`: The `Napi::DataView` reference to wrap.
69+
70+
### ArrayBuffer
71+
72+
```cpp
73+
Napi::ArrayBuffer ArrayBuffer() const;
74+
```
75+
76+
Returns the backing array buffer.
77+
78+
### ByteOffset
79+
80+
```cpp
81+
size_t ByteOffset() const;
82+
```
83+
84+
Returns the offset into the `Napi::DataView` where the array starts, in bytes.
85+
86+
### ByteLength
87+
88+
```cpp
89+
size_t ByteLength() const;
90+
```
91+
92+
Returns the length of the array, in bytes.
93+
94+
### GetFloat32
95+
96+
```cpp
97+
float GetFloat32(size_t byteOffset) const;
98+
```
99+
100+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
101+
102+
Returns a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`.
103+
104+
### GetFloat64
105+
106+
```cpp
107+
double GetFloat64(size_t byteOffset) const;
108+
```
109+
110+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
111+
112+
Returns a signed 64-bit float (double) at the specified byte offset from the start of the `Napi::DataView`.
113+
114+
### GetInt8
115+
116+
```cpp
117+
int8_t GetInt8(size_t byteOffset) const;
118+
```
119+
120+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
121+
122+
Returns a signed 8-bit integer (byte) at the specified byte offset from the start of the `Napi::DataView`.
123+
124+
### GetInt16
125+
126+
```cpp
127+
int16_t GetInt16(size_t byteOffset) const;
128+
```
129+
130+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
131+
132+
Returns a signed 16-bit integer (short) at the specified byte offset from the start of the `Napi::DataView`.
133+
134+
### GetInt32
135+
136+
```cpp
137+
int32_t GetInt32(size_t byteOffset) const;
138+
```
139+
140+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
141+
142+
Returns a signed 32-bit integer (long) at the specified byte offset from the start of the `Napi::DataView`.
143+
144+
### GetUint8
145+
146+
```cpp
147+
uint8_t GetUint8(size_t byteOffset) const;
148+
```
149+
150+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
151+
152+
Returns a unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `Napi::DataView`.
153+
154+
### GetUint16
155+
156+
```cpp
157+
uint16_t GetUint16(size_t byteOffset) const;
158+
```
159+
160+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
161+
162+
Returns a unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `Napi::DataView`.
163+
164+
### GetUint32
165+
166+
```cpp
167+
uint32_t GetUint32(size_t byteOffset) const;
168+
```
169+
170+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
171+
172+
Returns a unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `Napi::DataView`.
173+
174+
### SetFloat32
175+
176+
```cpp
177+
void SetFloat32(size_t byteOffset, float value) const;
178+
```
179+
180+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
181+
- `[in] value`: The value to set.
182+
183+
### SetFloat64
184+
185+
```cpp
186+
void SetFloat64(size_t byteOffset, double value) const;
187+
```
188+
189+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
190+
- `[in] value`: The value to set.
191+
192+
### SetInt8
193+
194+
```cpp
195+
void SetInt8(size_t byteOffset, int8_t value) const;
196+
```
197+
198+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
199+
- `[in] value`: The value to set.
200+
201+
### SetInt16
202+
203+
```cpp
204+
void SetInt16(size_t byteOffset, int16_t value) const;
205+
```
206+
207+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
208+
- `[in] value`: The value to set.
209+
210+
### SetInt32
211+
212+
```cpp
213+
void SetInt32(size_t byteOffset, int32_t value) const;
214+
```
215+
216+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
217+
- `[in] value`: The value to set.
218+
219+
### SetUint8
220+
221+
```cpp
222+
void SetUint8(size_t byteOffset, uint8_t value) const;
223+
```
224+
225+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
226+
- `[in] value`: The value to set.
227+
228+
### SetUint16
229+
230+
```cpp
231+
void SetUint16(size_t byteOffset, uint16_t value) const;
232+
```
233+
234+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
235+
- `[in] value`: The value to set.
236+
237+
### SetUint32
238+
239+
```cpp
240+
void SetUint32(size_t byteOffset, uint32_t value) const;
241+
```
242+
243+
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
244+
- `[in] value`: The value to set.

napi-inl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ inline bool Value::IsPromise() const {
364364
return result;
365365
}
366366

367-
#if NAPI_DATA_VIEW_FEATURE
368367
inline bool Value::IsDataView() const {
369368
if (_value == nullptr) {
370369
return false;
@@ -375,7 +374,6 @@ inline bool Value::IsDataView() const {
375374
NAPI_THROW_IF_FAILED(_env, status, false);
376375
return result;
377376
}
378-
#endif
379377

380378
inline bool Value::IsBuffer() const {
381379
if (_value == nullptr) {
@@ -1255,7 +1253,6 @@ inline void ArrayBuffer::EnsureInfo() const {
12551253
}
12561254
}
12571255

1258-
#if NAPI_DATA_VIEW_FEATURE
12591256
////////////////////////////////////////////////////////////////////////////////
12601257
// DataView class
12611258
////////////////////////////////////////////////////////////////////////////////
@@ -1426,7 +1423,6 @@ inline void DataView::WriteData(size_t byteOffset, T value) const {
14261423

14271424
*reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
14281425
}
1429-
#endif
14301426

14311427
////////////////////////////////////////////////////////////////////////////////
14321428
// TypedArray class

napi.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ namespace Napi {
189189
bool IsObject() const; ///< Tests if a value is a JavaScript object.
190190
bool IsFunction() const; ///< Tests if a value is a JavaScript function.
191191
bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
192-
#if NAPI_DATA_VIEW_FEATURE
193192
bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
194-
#endif
195193
bool IsBuffer() const; ///< Tests if a value is a Node buffer.
196194
bool IsExternal() const; ///< Tests if a value is a pointer to external data.
197195

@@ -836,7 +834,6 @@ namespace Napi {
836834
T* data);
837835
};
838836

839-
#if NAPI_DATA_VIEW_FEATURE
840837
/// The DataView provides a low-level interface for reading/writing multiple
841838
/// number types in an ArrayBuffer irrespective of the platform's endianness.
842839
class DataView : public Object {
@@ -888,7 +885,6 @@ namespace Napi {
888885
void* _data;
889886
size_t _length;
890887
};
891-
#endif
892888

893889
class Function : public Object {
894890
public:

test/binding.gyp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'targets': [
3838
{
3939
'target_name': 'binding',
40-
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
40+
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
4141
'cflags!': [ '-fno-exceptions' ],
4242
'cflags_cc!': [ '-fno-exceptions' ],
4343
'msvs_settings': {
@@ -54,7 +54,7 @@
5454
},
5555
{
5656
'target_name': 'binding_noexcept',
57-
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
57+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
5858
'cflags': [ '-fno-exceptions' ],
5959
'cflags_cc': [ '-fno-exceptions' ],
6060
'msvs_settings': {

0 commit comments

Comments
 (0)