Skip to content

Commit

Permalink
Implement Value.isDataView()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
romandev authored and mhdawson committed Dec 14, 2017
1 parent ccfcd55 commit b47cce8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
13 changes: 13 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,19 @@ inline bool Value::IsPromise() const {
return result;
}

#if NAPI_DATA_VIEW_FEATURE
inline bool Value::IsDataView() const {
if (_value == nullptr) {
return false;
}

bool result;
napi_status status = napi_is_dataview(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
#endif

inline bool Value::IsBuffer() const {
if (_value == nullptr) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ 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.

/// Casts to another type of `Napi::Value`, when the actual type is known or assumed.
Expand Down
5 changes: 5 additions & 0 deletions test/basic_types/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ static Value IsPromise(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].IsPromise());
}

static Value IsDataView(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].IsDataView());
}

static Value ToBoolean(const CallbackInfo& info) {
return info[0].ToBoolean();
}
Expand Down Expand Up @@ -87,6 +91,7 @@ Object InitBasicTypesValue(Env env) {
exports["isObject"] = Function::New(env, IsObject);
exports["isFunction"] = Function::New(env, IsFunction);
exports["isPromise"] = Function::New(env, IsPromise);
exports["isDataView"] = Function::New(env, IsDataView);
exports["toBoolean"] = Function::New(env, ToBoolean);
exports["toNumber"] = Function::New(env, ToNumber);
exports["toString"] = Function::New(env, ToString);
Expand Down
13 changes: 10 additions & 3 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ function test(binding) {
if (value instanceof ArrayBuffer)
return 'arraybuffer';

if (ArrayBuffer.isView(value))
return 'typedarray';
if (ArrayBuffer.isView(value)) {
if (value instanceof DataView) {
return 'dataview';
} else {
return 'typedarray';
}
}

if (value instanceof Promise)
return 'promise';
Expand All @@ -46,7 +51,8 @@ function test(binding) {
new Int32Array(new ArrayBuffer(12)),
{},
function() {},
new Promise((resolve, reject) => {})
new Promise((resolve, reject) => {}),
new DataView(new ArrayBuffer(12))
];

testValueList.forEach((testValue) => {
Expand Down Expand Up @@ -99,6 +105,7 @@ function test(binding) {
typeCheckerTest(value.isObject, 'object');
typeCheckerTest(value.isFunction, 'function');
typeCheckerTest(value.isPromise, 'promise');
typeCheckerTest(value.isDataView, 'dataview');

typeConverterTest(value.toBoolean, Boolean);
assert.strictEqual(value.toBoolean(undefined), false);
Expand Down
4 changes: 2 additions & 2 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'targets': [
{
'target_name': 'binding',
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'msvs_settings': {
Expand All @@ -41,7 +41,7 @@
},
{
'target_name': 'binding_noexcept',
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NAPI_DATA_VIEW_FEATURE' ],
'cflags': [ '-fno-exceptions' ],
'cflags_cc': [ '-fno-exceptions' ],
'msvs_settings': {
Expand Down

0 comments on commit b47cce8

Please sign in to comment.