Skip to content

Commit 908240b

Browse files
authored
Improve property key query for Proxy objects (#3797)
Property key query for Proxy objects always returned all keys even if no symbols were requested symbols were present in the resulting array. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent dd6d148 commit 908240b

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

jerry-core/ecma/operations/ecma-objects.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,37 @@ ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */
20412041
#if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
20422042
if (ECMA_OBJECT_IS_PROXY (obj_p))
20432043
{
2044-
return ecma_proxy_object_own_property_keys (obj_p);
2044+
/* Integrated a part of ECMA 262 v6 7.3.21 EnumerableOwnNames operation. */
2045+
ecma_collection_t *proxy_keys = ecma_proxy_object_own_property_keys (obj_p);
2046+
if (JERRY_UNLIKELY (proxy_keys == NULL))
2047+
{
2048+
return proxy_keys;
2049+
}
2050+
ecma_collection_t *return_keys = ecma_new_collection ();
2051+
2052+
/* Move valid elements to the output collection */
2053+
for (uint32_t i = 0; i < proxy_keys->item_count; i++)
2054+
{
2055+
ecma_value_t entry = proxy_keys->buffer_p[i];
2056+
ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (entry);
2057+
bool prop_is_symbol = ecma_prop_name_is_symbol (prop_name_p);
2058+
2059+
if (prop_is_symbol && ((opts & (ECMA_LIST_SYMBOLS | ECMA_LIST_SYMBOLS_ONLY)) != 0))
2060+
{
2061+
ecma_collection_push_back (return_keys, entry);
2062+
}
2063+
else if (!prop_is_symbol && (opts & ECMA_LIST_SYMBOLS_ONLY) == 0)
2064+
{
2065+
ecma_collection_push_back (return_keys, entry);
2066+
}
2067+
else
2068+
{
2069+
ecma_free_value (entry);
2070+
}
2071+
}
2072+
2073+
ecma_collection_destroy (proxy_keys);
2074+
return return_keys;
20452075
}
20462076
#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
20472077

tests/jerry/es2015/proxy_own_keys.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
// Use of this source code is governed by a BSD-style license that can be
1717
// found in the LICENSE file.
1818

19+
function array_check(result_array, expected_array) {
20+
assert(result_array instanceof Array);
21+
assert(result_array.length === expected_array.length);
22+
for (var idx = 0; idx < expected_array.length; idx++) {
23+
assert(result_array[idx] === expected_array[idx]);
24+
}
25+
}
26+
1927
var target = {};
2028
var handler = { ownKeys (target) {
2129
throw 42;
@@ -56,26 +64,29 @@ try {
5664
}
5765

5866
// test basic functionality
67+
var symA = Symbol("smA");
68+
var symB = Symbol("smB");
5969
var target = { prop1: "prop1", prop2: "prop2"};
70+
target[symB] = "s3";
6071
var handler = {
6172
ownKeys: function(target) {
62-
return ["foo", "bar"];
73+
return ["foo", "bar", symA];
6374
}
6475
}
6576

6677
var proxy = new Proxy(target, handler);
6778

68-
assert(JSON.stringify(Reflect.ownKeys(proxy)) === '["foo","bar"]');
69-
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["foo","bar"]');
70-
assert(JSON.stringify(Object.keys(proxy)) === '["foo","bar"]');
71-
assert(JSON.stringify(Object.getOwnPropertySymbols(proxy)) === '["foo","bar"]');
79+
array_check(Reflect.ownKeys(proxy), ["foo", "bar", symA]);
80+
array_check(Object.getOwnPropertyNames(proxy), ["foo", "bar"]);
81+
array_check(Object.keys(proxy), ["foo", "bar"]);
82+
array_check(Object.getOwnPropertySymbols(proxy), [symA]);
7283

7384
handler.ownKeys = function(target) {return Object.getOwnPropertyNames(target);};
7485

75-
assert(JSON.stringify(Reflect.ownKeys(proxy)) === '["prop1","prop2"]');
76-
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["prop1","prop2"]');
77-
assert(JSON.stringify(Object.keys(proxy)) === '["prop1","prop2"]');
78-
assert(JSON.stringify(Object.getOwnPropertySymbols(proxy)) === '["prop1","prop2"]');
86+
array_check(Reflect.ownKeys(proxy), ["prop1", "prop2"]);
87+
array_check(Object.getOwnPropertyNames(proxy), ["prop1", "prop2"]);
88+
array_check(Object.keys(proxy), ["prop1", "prop2"]);
89+
array_check(Object.getOwnPropertySymbols(proxy), []);
7990

8091
// test with no trap
8192
var target = { prop1: "prop1", prop2: "prop2"};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var symbol = Symbol("s");
16+
var obj = {demo: "3"};
17+
obj[symbol] = 3;
18+
19+
var proxy = new Proxy(obj, []);
20+
var str = JSON.stringify(proxy);
21+
22+
assert(str === '{"demo":"3"}');

0 commit comments

Comments
 (0)