Skip to content

Commit e2462a2

Browse files
vmorozBethGriggs
authored andcommitted
node-api: fix napi_get_all_property_names
PR-URL: #42463 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ef177da commit e2462a2

File tree

3 files changed

+166
-5
lines changed

3 files changed

+166
-5
lines changed

src/js_native_api_v8.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ napi_status napi_get_all_property_names(napi_env env,
936936
filter | v8::PropertyFilter::ONLY_ENUMERABLE);
937937
}
938938
if (key_filter & napi_key_configurable) {
939-
filter = static_cast<v8::PropertyFilter>(filter |
940-
v8::PropertyFilter::ONLY_WRITABLE);
939+
filter = static_cast<v8::PropertyFilter>(
940+
filter | v8::PropertyFilter::ONLY_CONFIGURABLE);
941941
}
942942
if (key_filter & napi_key_skip_strings) {
943943
filter = static_cast<v8::PropertyFilter>(filter |

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const object = {
1919
assert.strictEqual(test_object.Get(object, 'hello'), 'world');
2020
assert.strictEqual(test_object.GetNamed(object, 'hello'), 'world');
2121
assert.deepStrictEqual(test_object.Get(object, 'array'),
22-
[ 1, 94, 'str', 12.321, { test: 'obj in arr' } ]);
22+
[1, 94, 'str', 12.321, { test: 'obj in arr' }]);
2323
assert.deepStrictEqual(test_object.Get(object, 'newObject'),
2424
{ test: 'obj in obj' });
2525

@@ -54,7 +54,7 @@ assert.strictEqual(newObject.test_string, 'test string');
5454

5555
{
5656
// Verify that napi_has_own_property() fails if property is not a name.
57-
[true, false, null, undefined, {}, [], 0, 1, () => {}].forEach((value) => {
57+
[true, false, null, undefined, {}, [], 0, 1, () => { }].forEach((value) => {
5858
assert.throws(() => {
5959
test_object.HasOwn({}, value);
6060
}, /^Error: A string or symbol was expected$/);
@@ -240,13 +240,57 @@ assert.strictEqual(newObject.test_string, 'test string');
240240
writable: true,
241241
configurable: true
242242
});
243+
Object.defineProperty(object, 'writable', {
244+
value: 4,
245+
enumerable: true,
246+
writable: true,
247+
configurable: false
248+
});
249+
Object.defineProperty(object, 'configurable', {
250+
value: 4,
251+
enumerable: true,
252+
writable: false,
253+
configurable: true
254+
});
243255
object[5] = 5;
244256

245257
assert.deepStrictEqual(test_object.GetPropertyNames(object),
246-
['5', 'normal', 'inherited']);
258+
['5',
259+
'normal',
260+
'writable',
261+
'configurable',
262+
'inherited']);
247263

248264
assert.deepStrictEqual(test_object.GetSymbolNames(object),
249265
[fooSymbol]);
266+
267+
assert.deepStrictEqual(test_object.GetEnumerableWritableNames(object),
268+
['5',
269+
'normal',
270+
'writable',
271+
fooSymbol,
272+
'inherited']);
273+
274+
assert.deepStrictEqual(test_object.GetOwnWritableNames(object),
275+
['5',
276+
'normal',
277+
'unenumerable',
278+
'writable',
279+
fooSymbol]);
280+
281+
assert.deepStrictEqual(test_object.GetEnumerableConfigurableNames(object),
282+
['5',
283+
'normal',
284+
'configurable',
285+
fooSymbol,
286+
'inherited']);
287+
288+
assert.deepStrictEqual(test_object.GetOwnConfigurableNames(object),
289+
['5',
290+
'normal',
291+
'unenumerable',
292+
'configurable',
293+
fooSymbol]);
250294
}
251295

252296
// Verify that passing NULL to napi_set_property() results in the correct

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

+117
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,119 @@ static napi_value GetSymbolNames(napi_env env, napi_callback_info info) {
106106
return output;
107107
}
108108

109+
static napi_value GetEnumerableWritableNames(napi_env env,
110+
napi_callback_info info) {
111+
size_t argc = 1;
112+
napi_value args[1];
113+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
114+
115+
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
116+
117+
napi_valuetype value_type0;
118+
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));
119+
120+
NODE_API_ASSERT(
121+
env,
122+
value_type0 == napi_object,
123+
"Wrong type of arguments. Expects an object as first argument.");
124+
125+
napi_value output;
126+
NODE_API_CALL(
127+
env,
128+
napi_get_all_property_names(env,
129+
args[0],
130+
napi_key_include_prototypes,
131+
napi_key_enumerable | napi_key_writable,
132+
napi_key_numbers_to_strings,
133+
&output));
134+
135+
return output;
136+
}
137+
138+
static napi_value GetOwnWritableNames(napi_env env, napi_callback_info info) {
139+
size_t argc = 1;
140+
napi_value args[1];
141+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
142+
143+
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
144+
145+
napi_valuetype value_type0;
146+
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));
147+
148+
NODE_API_ASSERT(
149+
env,
150+
value_type0 == napi_object,
151+
"Wrong type of arguments. Expects an object as first argument.");
152+
153+
napi_value output;
154+
NODE_API_CALL(env,
155+
napi_get_all_property_names(env,
156+
args[0],
157+
napi_key_own_only,
158+
napi_key_writable,
159+
napi_key_numbers_to_strings,
160+
&output));
161+
162+
return output;
163+
}
164+
165+
static napi_value GetEnumerableConfigurableNames(napi_env env,
166+
napi_callback_info info) {
167+
size_t argc = 1;
168+
napi_value args[1];
169+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
170+
171+
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
172+
173+
napi_valuetype value_type0;
174+
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));
175+
176+
NODE_API_ASSERT(
177+
env,
178+
value_type0 == napi_object,
179+
"Wrong type of arguments. Expects an object as first argument.");
180+
181+
napi_value output;
182+
NODE_API_CALL(
183+
env,
184+
napi_get_all_property_names(env,
185+
args[0],
186+
napi_key_include_prototypes,
187+
napi_key_enumerable | napi_key_configurable,
188+
napi_key_numbers_to_strings,
189+
&output));
190+
191+
return output;
192+
}
193+
194+
static napi_value GetOwnConfigurableNames(napi_env env,
195+
napi_callback_info info) {
196+
size_t argc = 1;
197+
napi_value args[1];
198+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
199+
200+
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
201+
202+
napi_valuetype value_type0;
203+
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));
204+
205+
NODE_API_ASSERT(
206+
env,
207+
value_type0 == napi_object,
208+
"Wrong type of arguments. Expects an object as first argument.");
209+
210+
napi_value output;
211+
NODE_API_CALL(env,
212+
napi_get_all_property_names(env,
213+
args[0],
214+
napi_key_own_only,
215+
napi_key_configurable,
216+
napi_key_numbers_to_strings,
217+
&output));
218+
219+
return output;
220+
}
221+
109222
static napi_value Set(napi_env env, napi_callback_info info) {
110223
size_t argc = 3;
111224
napi_value args[3];
@@ -536,6 +649,10 @@ napi_value Init(napi_env env, napi_value exports) {
536649
DECLARE_NODE_API_PROPERTY("GetNamed", GetNamed),
537650
DECLARE_NODE_API_PROPERTY("GetPropertyNames", GetPropertyNames),
538651
DECLARE_NODE_API_PROPERTY("GetSymbolNames", GetSymbolNames),
652+
DECLARE_NODE_API_PROPERTY("GetEnumerableWritableNames", GetEnumerableWritableNames),
653+
DECLARE_NODE_API_PROPERTY("GetOwnWritableNames", GetOwnWritableNames),
654+
DECLARE_NODE_API_PROPERTY("GetEnumerableConfigurableNames", GetEnumerableConfigurableNames),
655+
DECLARE_NODE_API_PROPERTY("GetOwnConfigurableNames", GetOwnConfigurableNames),
539656
DECLARE_NODE_API_PROPERTY("Set", Set),
540657
DECLARE_NODE_API_PROPERTY("SetNamed", SetNamed),
541658
DECLARE_NODE_API_PROPERTY("Has", Has),

0 commit comments

Comments
 (0)