diff --git a/test/js-native-api/test_object/test.js b/test/js-native-api/test_object/test.js index 9a66febf7e2323..021f45b55f407a 100644 --- a/test/js-native-api/test_object/test.js +++ b/test/js-native-api/test_object/test.js @@ -19,7 +19,7 @@ const object = { assert.strictEqual(test_object.Get(object, 'hello'), 'world'); assert.strictEqual(test_object.GetNamed(object, 'hello'), 'world'); assert.deepStrictEqual(test_object.Get(object, 'array'), - [ 1, 94, 'str', 12.321, { test: 'obj in arr' } ]); + [1, 94, 'str', 12.321, { test: 'obj in arr' }]); assert.deepStrictEqual(test_object.Get(object, 'newObject'), { test: 'obj in obj' }); @@ -54,7 +54,7 @@ assert.strictEqual(newObject.test_string, 'test string'); { // Verify that napi_has_own_property() fails if property is not a name. - [true, false, null, undefined, {}, [], 0, 1, () => {}].forEach((value) => { + [true, false, null, undefined, {}, [], 0, 1, () => { }].forEach((value) => { assert.throws(() => { test_object.HasOwn({}, value); }, /^Error: A string or symbol was expected$/); @@ -238,7 +238,7 @@ assert.strictEqual(newObject.test_string, 'test string'); value: 4, enumerable: false, writable: true, - configurable: false + configurable: true }); Object.defineProperty(object, 'writable', { value: 4, @@ -264,19 +264,33 @@ assert.strictEqual(newObject.test_string, 'test string'); assert.deepStrictEqual(test_object.GetSymbolNames(object), [fooSymbol]); - assert.deepStrictEqual(test_object.GetWritableNames(object), + assert.deepStrictEqual(test_object.GetEnumerableWritableNames(object), ['5', 'normal', 'writable', fooSymbol, 'inherited']); - assert.deepStrictEqual(test_object.GetConfigurableNames(object), + assert.deepStrictEqual(test_object.GetOwnWritableNames(object), + ['5', + 'normal', + 'unenumerable', + 'writable', + fooSymbol]); + + assert.deepStrictEqual(test_object.GetEnumerableConfigurableNames(object), ['5', 'normal', 'configurable', fooSymbol, 'inherited']); + + assert.deepStrictEqual(test_object.GetOwnConfigurableNames(object), + ['5', + 'normal', + 'unenumerable', + 'configurable', + fooSymbol]); } // Verify that passing NULL to napi_set_property() results in the correct diff --git a/test/js-native-api/test_object/test_object.c b/test/js-native-api/test_object/test_object.c index 0594f3cc8792b0..46a99a1e9ee1ed 100644 --- a/test/js-native-api/test_object/test_object.c +++ b/test/js-native-api/test_object/test_object.c @@ -106,7 +106,8 @@ static napi_value GetSymbolNames(napi_env env, napi_callback_info info) { return output; } -static napi_value GetWritableNames(napi_env env, napi_callback_info info) { +static napi_value GetEnumerableWritableNames(napi_env env, + napi_callback_info info) { size_t argc = 1; napi_value args[1]; NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -134,7 +135,35 @@ static napi_value GetWritableNames(napi_env env, napi_callback_info info) { return output; } -static napi_value GetConfigurableNames(napi_env env, napi_callback_info info) { +static napi_value GetOwnWritableNames(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); + + napi_valuetype value_type0; + NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0)); + + NODE_API_ASSERT( + env, + value_type0 == napi_object, + "Wrong type of arguments. Expects an object as first argument."); + + napi_value output; + NODE_API_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_own_only, + napi_key_writable, + napi_key_numbers_to_strings, + &output)); + + return output; +} + +static napi_value GetEnumerableConfigurableNames(napi_env env, + napi_callback_info info) { size_t argc = 1; napi_value args[1]; NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -162,6 +191,34 @@ static napi_value GetConfigurableNames(napi_env env, napi_callback_info info) { return output; } +static napi_value GetOwnConfigurableNames(napi_env env, + napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); + + napi_valuetype value_type0; + NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0)); + + NODE_API_ASSERT( + env, + value_type0 == napi_object, + "Wrong type of arguments. Expects an object as first argument."); + + napi_value output; + NODE_API_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_own_only, + napi_key_configurable, + napi_key_numbers_to_strings, + &output)); + + return output; +} + static napi_value Set(napi_env env, napi_callback_info info) { size_t argc = 3; napi_value args[3]; @@ -592,8 +649,10 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NODE_API_PROPERTY("GetNamed", GetNamed), DECLARE_NODE_API_PROPERTY("GetPropertyNames", GetPropertyNames), DECLARE_NODE_API_PROPERTY("GetSymbolNames", GetSymbolNames), - DECLARE_NODE_API_PROPERTY("GetWritableNames", GetWritableNames), - DECLARE_NODE_API_PROPERTY("GetConfigurableNames", GetConfigurableNames), + DECLARE_NODE_API_PROPERTY("GetEnumerableWritableNames", GetEnumerableWritableNames), + DECLARE_NODE_API_PROPERTY("GetOwnWritableNames", GetOwnWritableNames), + DECLARE_NODE_API_PROPERTY("GetEnumerableConfigurableNames", GetEnumerableConfigurableNames), + DECLARE_NODE_API_PROPERTY("GetOwnConfigurableNames", GetOwnConfigurableNames), DECLARE_NODE_API_PROPERTY("Set", Set), DECLARE_NODE_API_PROPERTY("SetNamed", SetNamed), DECLARE_NODE_API_PROPERTY("Has", Has),