Skip to content

Commit d567f4b

Browse files
ebicklemhdawson
authored andcommitted
Added Napi::Value::IsExternal()
PR-URL: #227 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Hitesh Kanwathirtha <[email protected]>
1 parent 1b0f0e0 commit d567f4b

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

napi-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ inline bool Value::IsBuffer() const {
371371
return result;
372372
}
373373

374+
inline bool Value::IsExternal() const {
375+
return Type() == napi_external;
376+
}
377+
374378
template <typename T>
375379
inline T Value::As() const {
376380
return T(_env, _value);

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ namespace Napi {
181181
bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
182182
#endif
183183
bool IsBuffer() const; ///< Tests if a value is a Node buffer.
184+
bool IsExternal() const; ///< Tests if a value is a pointer to external data.
184185

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

test/basic_types/value.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
using namespace Napi;
44

5+
namespace {
6+
7+
int testData = 1;
8+
9+
// Helpers for testing non-Javascript values.
10+
Value CreateExternal(const CallbackInfo& info) {
11+
return External<int>::New(info.Env(), &testData);
12+
}
13+
14+
} // end anonymous namespace
15+
516
static Value IsEmpty(const CallbackInfo& info) {
617
Value value;
718
return Boolean::New(info.Env(), value.IsEmpty());
@@ -59,6 +70,10 @@ static Value IsDataView(const CallbackInfo& info) {
5970
return Boolean::New(info.Env(), info[0].IsDataView());
6071
}
6172

73+
static Value IsExternal(const CallbackInfo& info) {
74+
return Boolean::New(info.Env(), info[0].IsExternal());
75+
}
76+
6277
static Value ToBoolean(const CallbackInfo& info) {
6378
return info[0].ToBoolean();
6479
}
@@ -92,10 +107,13 @@ Object InitBasicTypesValue(Env env) {
92107
exports["isFunction"] = Function::New(env, IsFunction);
93108
exports["isPromise"] = Function::New(env, IsPromise);
94109
exports["isDataView"] = Function::New(env, IsDataView);
110+
exports["isExternal"] = Function::New(env, IsExternal);
95111
exports["toBoolean"] = Function::New(env, ToBoolean);
96112
exports["toNumber"] = Function::New(env, ToNumber);
97113
exports["toString"] = Function::New(env, ToString);
98114
exports["toObject"] = Function::New(env, ToObject);
99115

116+
exports["createExternal"] = Function::New(env, CreateExternal);
117+
100118
return exports;
101119
}

test/basic_types/value.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ test(require(`../build/${buildType}/binding.node`));
77
test(require(`../build/${buildType}/binding_noexcept.node`));
88

99
function test(binding) {
10+
const externalValue = binding.basic_types_value.createExternal();
11+
1012
function isObject(value) {
11-
return typeof value === 'object' || typeof value === 'function';
13+
return (typeof value === 'object' && value !== externalValue) ||
14+
(typeof value === 'function');
1215
}
1316

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

28+
if (value === externalValue)
29+
return 'external';
30+
2531
if (!value.constructor)
2632
return type;
2733

@@ -56,7 +62,8 @@ function test(binding) {
5662
{},
5763
function() {},
5864
new Promise((resolve, reject) => {}),
59-
new DataView(new ArrayBuffer(12))
65+
new DataView(new ArrayBuffer(12)),
66+
externalValue
6067
];
6168

6269
testValueList.forEach((testValue) => {
@@ -110,6 +117,7 @@ function test(binding) {
110117
typeCheckerTest(value.isFunction, 'function');
111118
typeCheckerTest(value.isPromise, 'promise');
112119
typeCheckerTest(value.isDataView, 'dataview');
120+
typeCheckerTest(value.isExternal, 'external');
113121

114122
typeConverterTest(value.toBoolean, Boolean);
115123
assert.strictEqual(value.toBoolean(undefined), false);

0 commit comments

Comments
 (0)