Skip to content

Commit ec90860

Browse files
committed
Enable DataView feature by default
This patch contains the following things: - Add a document for `DataView` feature - Remove NAPI_DATA_VIEW_FEATURE Refs: #196
1 parent b0ecd38 commit ec90860

File tree

5 files changed

+248
-10
lines changed

5 files changed

+248
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ still a work in progress as its not yet complete).
9090
- [ArrayBuffer](doc/array_buffer.md)
9191
- [TypedArray](doc/typed_array.md)
9292
- [TypedArrayOf](doc/typed_array_of.md)
93+
- [DataView](doc/dataview.md)
9394
- [Memory Management](doc/memory_management.md)
9495
- [Async Operations](doc/async_operations.md)
9596
- [AsyncWorker](doc/async_worker.md)

doc/dataview.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# DataView
2+
3+
The `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 `DataView` instance with a given ArrayBuffer.
12+
13+
```cpp
14+
static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer);
15+
```
16+
17+
- `[in] env`: The environment in which to create the `DataView` instance.
18+
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
19+
20+
Returns a new `DataView` instance.
21+
22+
### New
23+
24+
Allocates a new `DataView` instance with a given ArrayBuffer.
25+
26+
```cpp
27+
static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer, size_t byteOffset);
28+
```
29+
30+
- `[in] env`: The environment in which to create the `DataView` instance.
31+
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
32+
- `[in] byteOffset` : The byte offset within the `ArrayBuffer` from which to start projecting the `DataView`.
33+
34+
Returns a new `DataView` instance.
35+
36+
### New
37+
38+
Allocates a new `DataView` instance with a given ArrayBuffer.
39+
40+
```cpp
41+
static 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 `DataView` instance.
45+
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
46+
- `[in] byteOffset` : The byte offset within the `ArrayBuffer` from which to start projecting the `DataView`.
47+
- `[in] byteLength` : Number of elements in the `DataView`.
48+
49+
Returns a new `DataView` instance.
50+
51+
### Constructor
52+
53+
Initializes an empty instance of the `DataView` class.
54+
55+
```cpp
56+
DataView();
57+
```
58+
59+
### Constructor
60+
61+
Initializes a wrapper instance of an existing `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 `DataView` instance.
68+
- `[in] value`: The `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 `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 `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 `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 `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 `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 `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 `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 `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.
245+

napi-inl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ inline bool Value::IsPromise() const {
347347
return result;
348348
}
349349

350-
#if NAPI_DATA_VIEW_FEATURE
351350
inline bool Value::IsDataView() const {
352351
if (_value == nullptr) {
353352
return false;
@@ -358,7 +357,6 @@ inline bool Value::IsDataView() const {
358357
NAPI_THROW_IF_FAILED(_env, status, false);
359358
return result;
360359
}
361-
#endif
362360

363361
inline bool Value::IsBuffer() const {
364362
if (_value == nullptr) {
@@ -1175,7 +1173,6 @@ inline void ArrayBuffer::EnsureInfo() const {
11751173
}
11761174
}
11771175

1178-
#if NAPI_DATA_VIEW_FEATURE
11791176
////////////////////////////////////////////////////////////////////////////////
11801177
// DataView class
11811178
////////////////////////////////////////////////////////////////////////////////
@@ -1346,7 +1343,6 @@ inline void DataView::WriteData(size_t byteOffset, T value) const {
13461343

13471344
*reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
13481345
}
1349-
#endif
13501346

13511347
////////////////////////////////////////////////////////////////////////////////
13521348
// TypedArray class

napi.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ namespace Napi {
179179
bool IsObject() const; ///< Tests if a value is a JavaScript object.
180180
bool IsFunction() const; ///< Tests if a value is a JavaScript function.
181181
bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
182-
#if NAPI_DATA_VIEW_FEATURE
183182
bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
184-
#endif
185183
bool IsBuffer() const; ///< Tests if a value is a Node buffer.
186184
bool IsExternal() const; ///< Tests if a value is a pointer to external data.
187185

@@ -781,7 +779,6 @@ namespace Napi {
781779
T* data);
782780
};
783781

784-
#if NAPI_DATA_VIEW_FEATURE
785782
/// The DataView provides a low-level interface for reading/writing multiple
786783
/// number types in an ArrayBuffer irrespective of the platform's endianness.
787784
class DataView : public Object {
@@ -833,7 +830,6 @@ namespace Napi {
833830
void* _data;
834831
size_t _length;
835832
};
836-
#endif
837833

838834
class Function : public Object {
839835
public:

test/binding.gyp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
'targets': [
3333
{
3434
'target_name': 'binding',
35-
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
35+
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
3636
'cflags!': [ '-fno-exceptions' ],
3737
'cflags_cc!': [ '-fno-exceptions' ],
3838
'msvs_settings': {
@@ -49,7 +49,7 @@
4949
},
5050
{
5151
'target_name': 'binding_noexcept',
52-
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
52+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
5353
'cflags': [ '-fno-exceptions' ],
5454
'cflags_cc': [ '-fno-exceptions' ],
5555
'msvs_settings': {

0 commit comments

Comments
 (0)