Skip to content

Commit c9a674f

Browse files
romandevaddaleax
authored andcommitted
Implement Object::HasOwnProperty() method
Need HasOwnProperty() that is a wrapper for napi_has_own_property(). This change fixes #175 issue. Fixes: #175 PR-URL: #177 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5a5920a commit c9a674f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

napi-inl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,31 @@ inline bool Object::Has(const std::string& utf8name) const {
796796
return Has(utf8name.c_str());
797797
}
798798

799+
inline bool Object::HasOwnProperty(napi_value key) const {
800+
bool result;
801+
napi_status status = napi_has_own_property(_env, _value, key, &result);
802+
NAPI_THROW_IF_FAILED(_env, status, false);
803+
return result;
804+
}
805+
806+
inline bool Object::HasOwnProperty(Value key) const {
807+
bool result;
808+
napi_status status = napi_has_own_property(_env, _value, key, &result);
809+
NAPI_THROW_IF_FAILED(_env, status, false);
810+
return result;
811+
}
812+
813+
inline bool Object::HasOwnProperty(const char* utf8name) const {
814+
napi_value key;
815+
napi_status status = napi_create_string_utf8(_env, utf8name, std::strlen(utf8name), &key);
816+
NAPI_THROW_IF_FAILED(_env, status, false);
817+
return HasOwnProperty(key);
818+
}
819+
820+
inline bool Object::HasOwnProperty(const std::string& utf8name) const {
821+
return HasOwnProperty(utf8name.c_str());
822+
}
823+
799824
inline Value Object::Get(napi_value key) const {
800825
napi_value result;
801826
napi_status status = napi_get_property(_env, _value, key, &result);

napi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,26 @@ namespace Napi {
428428
const std::string& utf8name ///< UTF-8 encoded property name
429429
) const;
430430

431+
/// Checks whether a own property is present.
432+
bool HasOwnProperty(
433+
napi_value key ///< Property key primitive
434+
) const;
435+
436+
/// Checks whether a own property is present.
437+
bool HasOwnProperty(
438+
Value key ///< Property key
439+
) const;
440+
441+
/// Checks whether a own property is present.
442+
bool HasOwnProperty(
443+
const char* utf8name ///< UTF-8 encoded null-terminated property name
444+
) const;
445+
446+
/// Checks whether a own property is present.
447+
bool HasOwnProperty(
448+
const std::string& utf8name ///< UTF-8 encoded property name
449+
) const;
450+
431451
/// Gets a property.
432452
Value Get(
433453
napi_value key ///< Property key primitive

test/object.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ Value DeleteProperty(const CallbackInfo& info) {
108108
return Boolean::New(info.Env(), obj.Delete(name));
109109
}
110110

111+
Value HasOwnProperty(const CallbackInfo& info) {
112+
Object obj = info[0].As<Object>();
113+
Name name = info[1].As<Name>();
114+
return Boolean::New(info.Env(), obj.HasOwnProperty(name));
115+
}
116+
111117
Value CreateObjectUsingMagic(const CallbackInfo& info) {
112118
Env env = info.Env();
113119
Object obj = Object::New(env);
@@ -139,6 +145,7 @@ Object InitObject(Env env) {
139145
exports["getProperty"] = Function::New(env, GetProperty);
140146
exports["setProperty"] = Function::New(env, SetProperty);
141147
exports["deleteProperty"] = Function::New(env, DeleteProperty);
148+
exports["hasOwnPropertyFromNative"] = Function::New(env, HasOwnProperty);
142149
exports["createObjectUsingMagic"] = Function::New(env, CreateObjectUsingMagic);
143150

144151
return exports;

test/object.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ function test(binding) {
9494
assert.deepStrictEqual(obj, { two: 2 });
9595
}
9696

97+
{
98+
const obj = { one: 1 };
99+
Object.defineProperty(obj, 'two', { value: 2 });
100+
assert.strictEqual(binding.object.hasOwnPropertyFromNative(obj, 'one'), true);
101+
assert.strictEqual(binding.object.hasOwnPropertyFromNative(obj, 'two'), true);
102+
assert.strictEqual('toString' in obj, true);
103+
assert.strictEqual(binding.object.hasOwnPropertyFromNative(obj, 'toString'), false);
104+
}
105+
97106
{
98107
const obj = {'one': 1, 'two': 2, 'three': 3};
99108
var arr = binding.object.GetPropertyNames(obj);
@@ -109,6 +118,9 @@ function test(binding) {
109118
assert.throws(() => {
110119
binding.object.deleteProperty(undefined, 'test');
111120
}, /object was expected/);
121+
assert.throws(() => {
122+
binding.object.hasOwnPropertyFromNative(undefined, 'test');
123+
}, /object was expected/);
112124

113125
{
114126
const magicObject = binding.object.createObjectUsingMagic();

0 commit comments

Comments
 (0)