Skip to content

Commit

Permalink
Added Napi::Value::IsExternal()
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-addon-api#227
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Hitesh Kanwathirtha <hiteshk@microsoft.com>
  • Loading branch information
kevindavies8 committed Mar 2, 2018
1 parent 7f2644c commit a4d3042
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
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

0 comments on commit a4d3042

Please sign in to comment.