Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Value.isDataView() #202

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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