Skip to content

Commit

Permalink
Enable DataView feature by default
Browse files Browse the repository at this point in the history
This patch contains the following things:
  - Add a document for `DataView` feature
  - Remove NAPI_DATA_VIEW_FEATURE

Refs: #196
  • Loading branch information
romandev committed Sep 6, 2018
1 parent b0ecd38 commit 384a4a2
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ still a work in progress as its not yet complete).
- [ArrayBuffer](doc/array_buffer.md)
- [TypedArray](doc/typed_array.md)
- [TypedArrayOf](doc/typed_array_of.md)
- [DataView](doc/dataview.md)
- [Memory Management](doc/memory_management.md)
- [Async Operations](doc/async_operations.md)
- [AsyncWorker](doc/async_worker.md)
Expand Down
264 changes: 264 additions & 0 deletions doc/dataview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
# DataView

The `TypedArray` class corresponds to the
[JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
class.

float GetFloat32(size_t byteOffset) const;
double GetFloat64(size_t byteOffset) const;
int8_t GetInt8(size_t byteOffset) const;
int16_t GetInt16(size_t byteOffset) const;
int32_t GetInt32(size_t byteOffset) const;
uint8_t GetUint8(size_t byteOffset) const;
uint16_t GetUint16(size_t byteOffset) const;
uint32_t GetUint32(size_t byteOffset) const;

void SetFloat32(size_t byteOffset, float value) const;
void SetFloat64(size_t byteOffset, double value) const;
void SetInt8(size_t byteOffset, int8_t value) const;
void SetInt16(size_t byteOffset, int16_t value) const;
void SetInt32(size_t byteOffset, int32_t value) const;
void SetUint8(size_t byteOffset, uint8_t value) const;
void SetUint16(size_t byteOffset, uint16_t value) const;
void SetUint32(size_t byteOffset, uint32_t value) const;


## Methods

### New

Allocates a new `DataView` instance with a given ArrayBuffer.

```cpp
static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer);
```
- `[in] env`: The environment in which to create the `DataView` instance.
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
Returns a new `DataView` instance.
### New
Allocates a new `DataView` instance with a given ArrayBuffer.
```cpp
static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer, size_t byteOffset);
```

- `[in] env`: The environment in which to create the `DataView` instance.
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
- `[in] byteOffset` : The byte offset within the `ArrayBuffer` from which to start projecting the `DataView`.

Returns a new `DataView` instance.

### New

Allocates a new `DataView` instance with a given ArrayBuffer.

```cpp
static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer, size_t byteOffset, size_t byteLength);
```
- `[in] env`: The environment in which to create the `DataView` instance.
- `[in] arrayBuffer` : `ArrayBuffer` underlying the `DataView`.
- `[in] byteOffset` : The byte offset within the `ArrayBuffer` from which to start projecting the `DataView`.
- `[in] byteLength` : Number of elements in the `DataView`.
Returns a new `DataView` instance.
### Constructor
Initializes an empty instance of the `DataView` class.
```cpp
DataView();
```

### Constructor

Initializes a wrapper instance of an existing `DataView` instance.

```cpp
DataView(napi_env env, napi_value value);
```
- `[in] env`: The environment in which to create the `DataView` instance.
- `[in] value`: The `DataView` reference to wrap.
### ArrayBuffer
```cpp
Napi::ArrayBuffer ArrayBuffer() const;
```

Returns the backing array buffer.

### ByteOffset

```cpp
size_t ByteOffset() const;
```

Returns the offset into the `DataView` where the array starts, in bytes.

### ByteLength

```cpp
size_t ByteLength() const;
```

Returns the length of the array, in bytes.

### GetFloat32

```cpp
float GetFloat32(size_t byteOffset) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
Returns a signed 32-bit float (float) at the specified byte offset from the start of the `DataView`.
### GetFloat64
```cpp
double GetFloat64(size_t byteOffset) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.

Returns a signed 64-bit float (double) at the specified byte offset from the start of the `DataView`.

### GetInt8

```cpp
int8_t GetInt8(size_t byteOffset) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
Returns a signed 8-bit integer (byte) at the specified byte offset from the start of the `DataView`.
### GetInt16
```cpp
int16_t GetInt16(size_t byteOffset) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.

Returns a signed 16-bit integer (short) at the specified byte offset from the start of the `DataView`.

### GetInt32

```cpp
int32_t GetInt32(size_t byteOffset) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
Returns a signed 32-bit integer (long) at the specified byte offset from the start of the `DataView`.
### GetUint8
```cpp
uint8_t GetUint8(size_t byteOffset) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.

Returns a unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the `DataView`.

### GetUint16

```cpp
uint16_t GetUint16(size_t byteOffset) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
Returns a unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the `DataView`.
### GetUint32
```cpp
uint32_t GetUint32(size_t byteOffset) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.

Returns a unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the `DataView`.

### SetFloat32

```cpp
void SetFloat32(size_t byteOffset, float value) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.
### SetFloat64
```cpp
void SetFloat64(size_t byteOffset, double value) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.

### SetInt8

```cpp
void SetInt8(size_t byteOffset, int8_t value) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.
### SetInt16
```cpp
void SetInt16(size_t byteOffset, int16_t value) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.

### SetInt32

```cpp
void SetInt32(size_t byteOffset, int32_t value) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.
### SetUint8
```cpp
void SetUint8(size_t byteOffset, uint8_t value) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.

### SetUint16

```cpp
void SetUint16(size_t byteOffset, uint16_t value) const;
```
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.
### SetUint32
```cpp
void SetUint32(size_t byteOffset, uint32_t value) const;
```

- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
- `[in] value`: The value to set.

4 changes: 0 additions & 4 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ inline bool Value::IsPromise() const {
return result;
}

#if NAPI_DATA_VIEW_FEATURE
inline bool Value::IsDataView() const {
if (_value == nullptr) {
return false;
Expand All @@ -358,7 +357,6 @@ inline bool Value::IsDataView() const {
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
#endif

inline bool Value::IsBuffer() const {
if (_value == nullptr) {
Expand Down Expand Up @@ -1175,7 +1173,6 @@ inline void ArrayBuffer::EnsureInfo() const {
}
}

#if NAPI_DATA_VIEW_FEATURE
////////////////////////////////////////////////////////////////////////////////
// DataView class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1346,7 +1343,6 @@ inline void DataView::WriteData(size_t byteOffset, T value) const {

*reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
}
#endif

////////////////////////////////////////////////////////////////////////////////
// TypedArray class
Expand Down
4 changes: 0 additions & 4 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ namespace Napi {
bool IsObject() const; ///< Tests if a value is a JavaScript object.
bool IsFunction() const; ///< Tests if a value is a JavaScript function.
bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
#if NAPI_DATA_VIEW_FEATURE
bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
#endif
bool IsBuffer() const; ///< Tests if a value is a Node buffer.
bool IsExternal() const; ///< Tests if a value is a pointer to external data.

Expand Down Expand Up @@ -781,7 +779,6 @@ namespace Napi {
T* data);
};

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

class Function : public Object {
public:
Expand Down
4 changes: 2 additions & 2 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'targets': [
{
'target_name': 'binding',
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'msvs_settings': {
Expand All @@ -49,7 +49,7 @@
},
{
'target_name': 'binding_noexcept',
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
'cflags': [ '-fno-exceptions' ],
'cflags_cc': [ '-fno-exceptions' ],
'msvs_settings': {
Expand Down

0 comments on commit 384a4a2

Please sign in to comment.