Skip to content

Commit 384a4a2

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 384a4a2

File tree

5 files changed

+267
-10
lines changed

5 files changed

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

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)