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

Feature/value type checks #227

Closed
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ inline bool Value::IsBuffer() const {
return result;
}

inline bool Value::IsExternal() const {
return Type() == napi_external;
}

template <typename T>
inline T Value::As() const {
return T(_env, _value);
Expand Down
1 change: 1 addition & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ namespace Napi {
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.

/// Casts to another type of `Napi::Value`, when the actual type is known or assumed.
///
Expand Down
18 changes: 18 additions & 0 deletions test/basic_types/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

using namespace Napi;

namespace {

int testData = 1;

// Helpers for testing non-Javascript values.
Value CreateExternal(const CallbackInfo& info) {
return External<int>::New(info.Env(), &testData);
}

} // end anonymous namespace

static Value IsEmpty(const CallbackInfo& info) {
Value value;
return Boolean::New(info.Env(), value.IsEmpty());
Expand Down Expand Up @@ -59,6 +70,10 @@ static Value IsDataView(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].IsDataView());
}

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

static Value ToBoolean(const CallbackInfo& info) {
return info[0].ToBoolean();
}
Expand Down Expand Up @@ -92,10 +107,13 @@ Object InitBasicTypesValue(Env env) {
exports["isFunction"] = Function::New(env, IsFunction);
exports["isPromise"] = Function::New(env, IsPromise);
exports["isDataView"] = Function::New(env, IsDataView);
exports["isExternal"] = Function::New(env, IsExternal);
exports["toBoolean"] = Function::New(env, ToBoolean);
exports["toNumber"] = Function::New(env, ToNumber);
exports["toString"] = Function::New(env, ToString);
exports["toObject"] = Function::New(env, ToObject);

exports["createExternal"] = Function::New(env, CreateExternal);

return exports;
}
12 changes: 10 additions & 2 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));

function test(binding) {
const externalValue = binding.basic_types_value.createExternal();

function isObject(value) {
return typeof value === 'object' || typeof value === 'function';
return (typeof value === 'object' && value !== externalValue) ||
(typeof value === 'function');
}

function detailedTypeOf(value) {
Expand All @@ -22,6 +25,9 @@ function test(binding) {
if (Array.isArray(value))
return 'array';

if (value === externalValue)
return 'external';

if (!value.constructor)
return type;

Expand Down Expand Up @@ -56,7 +62,8 @@ function test(binding) {
{},
function() {},
new Promise((resolve, reject) => {}),
new DataView(new ArrayBuffer(12))
new DataView(new ArrayBuffer(12)),
externalValue
];

testValueList.forEach((testValue) => {
Expand Down Expand Up @@ -110,6 +117,7 @@ function test(binding) {
typeCheckerTest(value.isFunction, 'function');
typeCheckerTest(value.isPromise, 'promise');
typeCheckerTest(value.isDataView, 'dataview');
typeCheckerTest(value.isExternal, 'external');

typeConverterTest(value.toBoolean, Boolean);
assert.strictEqual(value.toBoolean(undefined), false);
Expand Down