diff --git a/napi-inl.h b/napi-inl.h index a1b8140d5..17132fa9c 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -796,6 +796,13 @@ inline void Object::Set(uint32_t index, double numberValue) { Set(index, static_cast(Number::New(Env(), numberValue))); } +inline Array Object::GetPropertyNames() { + napi_value result; + napi_status status = napi_get_property_names(_env, _value, &result); + NAPI_THROW_IF_FAILED(_env, status, Array()); + return Array(_env, result); +} + inline void Object::DefineProperty(const PropertyDescriptor& property) { napi_status status = napi_define_properties(_env, _value, 1, reinterpret_cast(&property)); diff --git a/napi.h b/napi.h index 6e9414179..c2b943ca3 100644 --- a/napi.h +++ b/napi.h @@ -523,6 +523,8 @@ namespace Napi { double numberValue ///< Property value ); + Array GetPropertyNames(); ///< Get all property names + /// Defines a property on the object. void DefineProperty( const PropertyDescriptor& property ///< Descriptor for the property to be defined diff --git a/package.json b/package.json index 7b4c2cf83..6c28d04cf 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,5 @@ "pretest": "node-gyp rebuild -C test", "test": "node test" }, - "version": "0.3.3" + "version": "0.3.4" } diff --git a/test/object.cc b/test/object.cc index e51064db8..bfe58b919 100644 --- a/test/object.cc +++ b/test/object.cc @@ -16,6 +16,12 @@ Value TestFunction(const CallbackInfo& info) { return Boolean::New(info.Env(), true); } +Array GetPropertyNames(const CallbackInfo& info) { + Object obj = info[0].As(); + Array arr = obj.GetPropertyNames(); + return arr; +} + void DefineProperties(const CallbackInfo& info) { Object obj = info[0].As(); String nameType = info[1].As(); @@ -87,6 +93,7 @@ void SetProperty(const CallbackInfo& info) { Object InitObject(Env env) { Object exports = Object::New(env); + exports["GetPropertyNames"] = Function::New(env, GetPropertyNames); exports["defineProperties"] = Function::New(env, DefineProperties); exports["defineValueProperty"] = Function::New(env, DefineValueProperty); exports["getProperty"] = Function::New(env, GetProperty); diff --git a/test/object.js b/test/object.js index 855509db5..7728b1a4b 100644 --- a/test/object.js +++ b/test/object.js @@ -82,6 +82,12 @@ function test(binding) { assert.strictEqual(obj.test, 1); } + { + const obj = {'one': 1, 'two': 2, 'three': 3}; + var arr = binding.object.GetPropertyNames(obj); + assert.deepStrictEqual(arr, ['one', 'two', 'three']) + } + assert.throws(() => { binding.object.getProperty(undefined, 'test'); }, /object was expected/);