From bc0d5e2f59adb312c10f6f22cfead61ee9bd9711 Mon Sep 17 00:00:00 2001 From: himself65 Date: Sun, 17 Nov 2019 19:05:52 +0800 Subject: [PATCH] n-api: add napi_get_own_property_names --- src/js_native_api.h | 3 +++ src/js_native_api_v8.cc | 2 +- test/js-native-api/test_object/test.js | 28 +++++++++++++------- test/js-native-api/test_object/test_object.c | 21 +++++++++++++++ 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index 4cb88f4c40cfc0..854ab7bc068492 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -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, diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 360e0c9e2aab31..5afea6293f312d 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -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: diff --git a/test/js-native-api/test_object/test.js b/test/js-native-api/test_object/test.js index 2cd65af6b34de6..a23ec7ffe42f5b 100644 --- a/test/js-native-api/test_object/test.js +++ b/test/js-native-api/test_object/test.js @@ -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' } @@ -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'); @@ -224,6 +219,11 @@ 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']); @@ -231,6 +231,16 @@ assert.strictEqual(newObject.test_string, 'test string'); [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(), { diff --git a/test/js-native-api/test_object/test_object.c b/test/js-native-api/test_object/test_object.c index bcb295197f506c..79b73ea3db5984 100644 --- a/test/js-native-api/test_object/test_object.c +++ b/test/js-native-api/test_object/test_object.c @@ -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]; @@ -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),