Skip to content

Add API for getting object's property names #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,13 @@ inline void Object::Set(uint32_t index, double numberValue) {
Set(index, static_cast<napi_value>(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<const napi_property_descriptor*>(&property));
Expand Down
2 changes: 2 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
"pretest": "node-gyp rebuild -C test",
"test": "node test"
},
"version": "0.3.3"
"version": "0.3.4"
}
7 changes: 7 additions & 0 deletions test/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>();
Array arr = obj.GetPropertyNames();
return arr;
}

void DefineProperties(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String nameType = info[1].As<String>();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down