Skip to content

Commit b47cce8

Browse files
romandevmhdawson
authored andcommitted
Implement Value.isDataView()
The method is to check if the given value is a data view object. This ia an initial implementation of DataView feature(#196). This change also adds the NAPI_DATA_VIEW_FEATURE flag to expose only to test modules until all features are implemented. PR-URL: #202 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Hitesh Kanwathirtha <[email protected]>
1 parent ccfcd55 commit b47cce8

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

napi-inl.h

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

350+
#if NAPI_DATA_VIEW_FEATURE
351+
inline bool Value::IsDataView() const {
352+
if (_value == nullptr) {
353+
return false;
354+
}
355+
356+
bool result;
357+
napi_status status = napi_is_dataview(_env, _value, &result);
358+
NAPI_THROW_IF_FAILED(_env, status, false);
359+
return result;
360+
}
361+
#endif
362+
350363
inline bool Value::IsBuffer() const {
351364
if (_value == nullptr) {
352365
return false;

napi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ namespace Napi {
177177
bool IsObject() const; ///< Tests if a value is a JavaScript object.
178178
bool IsFunction() const; ///< Tests if a value is a JavaScript function.
179179
bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
180+
#if NAPI_DATA_VIEW_FEATURE
181+
bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
182+
#endif
180183
bool IsBuffer() const; ///< Tests if a value is a Node buffer.
181184

182185
/// Casts to another type of `Napi::Value`, when the actual type is known or assumed.

test/basic_types/value.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ static Value IsPromise(const CallbackInfo& info) {
5555
return Boolean::New(info.Env(), info[0].IsPromise());
5656
}
5757

58+
static Value IsDataView(const CallbackInfo& info) {
59+
return Boolean::New(info.Env(), info[0].IsDataView());
60+
}
61+
5862
static Value ToBoolean(const CallbackInfo& info) {
5963
return info[0].ToBoolean();
6064
}
@@ -87,6 +91,7 @@ Object InitBasicTypesValue(Env env) {
8791
exports["isObject"] = Function::New(env, IsObject);
8892
exports["isFunction"] = Function::New(env, IsFunction);
8993
exports["isPromise"] = Function::New(env, IsPromise);
94+
exports["isDataView"] = Function::New(env, IsDataView);
9095
exports["toBoolean"] = Function::New(env, ToBoolean);
9196
exports["toNumber"] = Function::New(env, ToNumber);
9297
exports["toString"] = Function::New(env, ToString);

test/basic_types/value.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ function test(binding) {
2424
if (value instanceof ArrayBuffer)
2525
return 'arraybuffer';
2626

27-
if (ArrayBuffer.isView(value))
28-
return 'typedarray';
27+
if (ArrayBuffer.isView(value)) {
28+
if (value instanceof DataView) {
29+
return 'dataview';
30+
} else {
31+
return 'typedarray';
32+
}
33+
}
2934

3035
if (value instanceof Promise)
3136
return 'promise';
@@ -46,7 +51,8 @@ function test(binding) {
4651
new Int32Array(new ArrayBuffer(12)),
4752
{},
4853
function() {},
49-
new Promise((resolve, reject) => {})
54+
new Promise((resolve, reject) => {}),
55+
new DataView(new ArrayBuffer(12))
5056
];
5157

5258
testValueList.forEach((testValue) => {
@@ -99,6 +105,7 @@ function test(binding) {
99105
typeCheckerTest(value.isObject, 'object');
100106
typeCheckerTest(value.isFunction, 'function');
101107
typeCheckerTest(value.isPromise, 'promise');
108+
typeCheckerTest(value.isDataView, 'dataview');
102109

103110
typeConverterTest(value.toBoolean, Boolean);
104111
assert.strictEqual(value.toBoolean(undefined), false);

test/binding.gyp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
'targets': [
2828
{
2929
'target_name': 'binding',
30-
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
30+
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
3131
'cflags!': [ '-fno-exceptions' ],
3232
'cflags_cc!': [ '-fno-exceptions' ],
3333
'msvs_settings': {
@@ -41,7 +41,7 @@
4141
},
4242
{
4343
'target_name': 'binding_noexcept',
44-
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
44+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
4545
'cflags': [ '-fno-exceptions' ],
4646
'cflags_cc': [ '-fno-exceptions' ],
4747
'msvs_settings': {

0 commit comments

Comments
 (0)