File tree Expand file tree Collapse file tree 4 files changed +64
-0
lines changed Expand file tree Collapse file tree 4 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -796,6 +796,31 @@ inline bool Object::Has(const std::string& utf8name) const {
796
796
return Has (utf8name.c_str ());
797
797
}
798
798
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
+
799
824
inline Value Object::Get (napi_value key) const {
800
825
napi_value result;
801
826
napi_status status = napi_get_property (_env, _value, key, &result);
Original file line number Diff line number Diff line change @@ -428,6 +428,26 @@ namespace Napi {
428
428
const std::string& utf8name // /< UTF-8 encoded property name
429
429
) const ;
430
430
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
+
431
451
// / Gets a property.
432
452
Value Get (
433
453
napi_value key // /< Property key primitive
Original file line number Diff line number Diff line change @@ -108,6 +108,12 @@ Value DeleteProperty(const CallbackInfo& info) {
108
108
return Boolean::New (info.Env (), obj.Delete (name));
109
109
}
110
110
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
+
111
117
Value CreateObjectUsingMagic (const CallbackInfo& info) {
112
118
Env env = info.Env ();
113
119
Object obj = Object::New (env);
@@ -139,6 +145,7 @@ Object InitObject(Env env) {
139
145
exports[" getProperty" ] = Function::New (env, GetProperty);
140
146
exports[" setProperty" ] = Function::New (env, SetProperty);
141
147
exports[" deleteProperty" ] = Function::New (env, DeleteProperty);
148
+ exports[" hasOwnPropertyFromNative" ] = Function::New (env, HasOwnProperty);
142
149
exports[" createObjectUsingMagic" ] = Function::New (env, CreateObjectUsingMagic);
143
150
144
151
return exports;
Original file line number Diff line number Diff line change @@ -94,6 +94,15 @@ function test(binding) {
94
94
assert . deepStrictEqual ( obj , { two : 2 } ) ;
95
95
}
96
96
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
+
97
106
{
98
107
const obj = { 'one' : 1 , 'two' : 2 , 'three' : 3 } ;
99
108
var arr = binding . object . GetPropertyNames ( obj ) ;
@@ -109,6 +118,9 @@ function test(binding) {
109
118
assert . throws ( ( ) => {
110
119
binding . object . deleteProperty ( undefined , 'test' ) ;
111
120
} , / o b j e c t w a s e x p e c t e d / ) ;
121
+ assert . throws ( ( ) => {
122
+ binding . object . hasOwnPropertyFromNative ( undefined , 'test' ) ;
123
+ } , / o b j e c t w a s e x p e c t e d / ) ;
112
124
113
125
{
114
126
const magicObject = binding . object . createObjectUsingMagic ( ) ;
You can’t perform that action at this time.
0 commit comments