Skip to content

Commit bc0d5e2

Browse files
committed
n-api: add napi_get_own_property_names
1 parent ece3006 commit bc0d5e2

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/js_native_api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ napi_get_all_property_names(napi_env env,
514514
napi_key_filter key_filter,
515515
napi_key_conversion key_conversion,
516516
napi_value* result);
517+
NAPI_EXTERN napi_status napi_get_own_property_names(napi_env env,
518+
napi_value object,
519+
napi_value* result);
517520

518521
// Instance data
519522
NAPI_EXTERN napi_status napi_set_instance_data(napi_env env,

src/js_native_api_v8.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ napi_status napi_get_all_property_names(napi_env env,
922922
case napi_key_include_prototypes:
923923
collection_mode = v8::KeyCollectionMode::kIncludePrototypes;
924924
break;
925-
case napi_key_own_only:
925+
case napi_key_include_own_properties:
926926
collection_mode = v8::KeyCollectionMode::kOwnOnly;
927927
break;
928928
default:

test/js-native-api/test_object/test.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const assert = require('assert');
66
const test_object = require(`./build/${common.buildType}/test_object`);
77

88

9-
const object = {
9+
let object = {
1010
hello: 'world',
1111
array: [
1212
1, 94, 'str', 12.321, { test: 'obj in arr' }
@@ -203,14 +203,9 @@ assert.strictEqual(newObject.test_string, 'test string');
203203
assert.strictEqual(obj.foo, 'baz');
204204
}
205205

206-
{
207-
// Verify that napi_get_property_names gets the right set of property names,
208-
// i.e.: includes prototypes, only enumerable properties, skips symbols,
209-
// and includes indices and converts them to strings.
210-
211-
const object = Object.create({
212-
inherited: 1
213-
});
206+
object = Object.create({
207+
inherited: 1
208+
});
214209

215210
const fooSymbol = Symbol('foo');
216211

@@ -224,13 +219,28 @@ assert.strictEqual(newObject.test_string, 'test string');
224219
});
225220
object[5] = 5;
226221

222+
{
223+
// Verify that napi_get_property_names gets the right set of property names,
224+
// i.e.: includes prototypes, only enumerable properties, skips symbols,
225+
// and includes indices and converts them to strings.
226+
227227
assert.deepStrictEqual(test_object.GetPropertyNames(object),
228228
['5', 'normal', 'inherited']);
229229

230230
assert.deepStrictEqual(test_object.GetSymbolNames(object),
231231
[fooSymbol]);
232232
}
233233

234+
{
235+
// Verify that napi_get_own_property_names gets
236+
// the right set of property names,
237+
// i.e.: skips symbols and prototypes,
238+
// and includes indices and converts them to strings.
239+
240+
assert.deepStrictEqual(test_object.GetOwnPropertyNames(object),
241+
['5', 'normal', 'unenumerable']);
242+
}
243+
234244
// Verify that passing NULL to napi_set_property() results in the correct
235245
// error.
236246
assert.deepStrictEqual(test_object.TestSetProperty(), {

test/js-native-api/test_object/test_object.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ static napi_value GetSymbolNames(napi_env env, napi_callback_info info) {
111111
return output;
112112
}
113113

114+
static napi_value GetOwnPropertyNames(napi_env env, napi_callback_info info) {
115+
size_t argc = 1;
116+
napi_value args[1];
117+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
118+
119+
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
120+
121+
napi_valuetype value_type0;
122+
NAPI_CALL(env, napi_typeof(env, args[0], &value_type0));
123+
124+
NAPI_ASSERT(env,
125+
value_type0 == napi_object,
126+
"Wrong type of arguments. Expects an object as first argument.");
127+
128+
napi_value output;
129+
NAPI_CALL(env, napi_get_own_property_names(env, args[0], &output));
130+
131+
return output;
132+
}
133+
114134
static napi_value Set(napi_env env, napi_callback_info info) {
115135
size_t argc = 3;
116136
napi_value args[3];
@@ -479,6 +499,7 @@ napi_value Init(napi_env env, napi_value exports) {
479499
DECLARE_NAPI_PROPERTY("GetNamed", GetNamed),
480500
DECLARE_NAPI_PROPERTY("GetPropertyNames", GetPropertyNames),
481501
DECLARE_NAPI_PROPERTY("GetSymbolNames", GetSymbolNames),
502+
DECLARE_NAPI_PROPERTY("GetOwnPropertyNames", GetOwnPropertyNames),
482503
DECLARE_NAPI_PROPERTY("Set", Set),
483504
DECLARE_NAPI_PROPERTY("SetNamed", SetNamed),
484505
DECLARE_NAPI_PROPERTY("Has", Has),

0 commit comments

Comments
 (0)