Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
n-api: add napi_get_own_property_names
  • Loading branch information
himself65 committed Dec 10, 2019
commit bc0d5e2f59adb312c10f6f22cfead61ee9bd9711
3 changes: 3 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ napi_get_all_property_names(napi_env env,
napi_key_filter key_filter,
napi_key_conversion key_conversion,
napi_value* result);
NAPI_EXTERN napi_status napi_get_own_property_names(napi_env env,
napi_value object,
napi_value* result);

// Instance data
NAPI_EXTERN napi_status napi_set_instance_data(napi_env env,
Expand Down
2 changes: 1 addition & 1 deletion src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ napi_status napi_get_all_property_names(napi_env env,
case napi_key_include_prototypes:
collection_mode = v8::KeyCollectionMode::kIncludePrototypes;
break;
case napi_key_own_only:
case napi_key_include_own_properties:
collection_mode = v8::KeyCollectionMode::kOwnOnly;
break;
default:
Expand Down
28 changes: 19 additions & 9 deletions test/js-native-api/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert');
const test_object = require(`./build/${common.buildType}/test_object`);


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

{
// Verify that napi_get_property_names gets the right set of property names,
// i.e.: includes prototypes, only enumerable properties, skips symbols,
// and includes indices and converts them to strings.

const object = Object.create({
inherited: 1
});
object = Object.create({
inherited: 1
});

const fooSymbol = Symbol('foo');

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

{
// Verify that napi_get_property_names gets the right set of property names,
// i.e.: includes prototypes, only enumerable properties, skips symbols,
// and includes indices and converts them to strings.

assert.deepStrictEqual(test_object.GetPropertyNames(object),
['5', 'normal', 'inherited']);

assert.deepStrictEqual(test_object.GetSymbolNames(object),
[fooSymbol]);
}

{
// Verify that napi_get_own_property_names gets
// the right set of property names,
// i.e.: skips symbols and prototypes,
// and includes indices and converts them to strings.

assert.deepStrictEqual(test_object.GetOwnPropertyNames(object),
['5', 'normal', 'unenumerable']);
}

// Verify that passing NULL to napi_set_property() results in the correct
// error.
assert.deepStrictEqual(test_object.TestSetProperty(), {
Expand Down
21 changes: 21 additions & 0 deletions test/js-native-api/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ static napi_value GetSymbolNames(napi_env env, napi_callback_info info) {
return output;
}

static napi_value GetOwnPropertyNames(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype value_type0;
NAPI_CALL(env, napi_typeof(env, args[0], &value_type0));

NAPI_ASSERT(env,
value_type0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_value output;
NAPI_CALL(env, napi_get_own_property_names(env, args[0], &output));

return output;
}

static napi_value Set(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
Expand Down Expand Up @@ -479,6 +499,7 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_PROPERTY("GetNamed", GetNamed),
DECLARE_NAPI_PROPERTY("GetPropertyNames", GetPropertyNames),
DECLARE_NAPI_PROPERTY("GetSymbolNames", GetSymbolNames),
DECLARE_NAPI_PROPERTY("GetOwnPropertyNames", GetOwnPropertyNames),
DECLARE_NAPI_PROPERTY("Set", Set),
DECLARE_NAPI_PROPERTY("SetNamed", SetNamed),
DECLARE_NAPI_PROPERTY("Has", Has),
Expand Down