Skip to content

Commit 4afcc70

Browse files
authored
Add fast array support for property name enumeration (#3053)
Since fast access mode arrays can be part of the prototype chain these objects must be handed separately. This patch fixes #3050. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 24196b6 commit 4afcc70

File tree

2 files changed

+144
-62
lines changed

2 files changed

+144
-62
lines changed

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

Lines changed: 117 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,104 +1812,159 @@ ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */
18121812

18131813
jmem_cpointer_t prop_iter_cp = prototype_chain_iter_p->u1.property_list_cp;
18141814

1815-
#if ENABLED (JERRY_PROPRETY_HASHMAP)
1816-
if (prop_iter_cp != JMEM_CP_NULL)
1815+
if (ecma_get_object_type (prototype_chain_iter_p) == ECMA_OBJECT_TYPE_ARRAY
1816+
&& ((ecma_extended_object_t *) prototype_chain_iter_p)->u.array.is_fast_mode
1817+
&& prop_iter_cp != JMEM_CP_NULL)
18171818
{
1818-
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
1819+
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) prototype_chain_iter_p;
18191820

1820-
if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
1821-
{
1822-
prop_iter_cp = prop_iter_p->next_property_cp;
1823-
}
1824-
}
1825-
#endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */
1821+
uint32_t length = ext_obj_p->u.array.length;
18261822

1827-
while (prop_iter_cp != JMEM_CP_NULL)
1828-
{
1829-
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
1830-
JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
1823+
ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, prop_iter_cp);
18311824

1832-
for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++)
1825+
for (uint32_t i = 0; i < length; i++)
18331826
{
1834-
ecma_property_t *property_p = prop_iter_p->types + i;
1827+
if (ecma_is_value_array_hole (values_p[i]))
1828+
{
1829+
continue;
1830+
}
1831+
1832+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i);
1833+
1834+
uint8_t hash = (uint8_t) ecma_string_hash (index_str_p);
1835+
uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
1836+
uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
1837+
1838+
bool is_add = true;
18351839

1836-
if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA
1837-
|| ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
1840+
if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0)
18381841
{
1839-
ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p;
1842+
ecma_value_p = ecma_collection_iterator_init (prop_names_p);
18401843

1841-
if (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
1842-
&& prop_pair_p->names_cp[i] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT
1843-
&& prop_pair_p->names_cp[i] < LIT_MAGIC_STRING__COUNT)
1844+
while (ecma_value_p != NULL)
18441845
{
1845-
/* Internal properties are never enumerated. */
1846-
continue;
1846+
ecma_string_t *current_name_p = ecma_get_prop_name_from_value (*ecma_value_p);
1847+
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
1848+
1849+
if (ecma_compare_ecma_strings (index_str_p, current_name_p))
1850+
{
1851+
is_add = false;
1852+
break;
1853+
}
18471854
}
1855+
}
18481856

1849-
ecma_string_t *name_p = ecma_string_from_property_name (*property_p,
1850-
prop_pair_p->names_cp[i]);
1857+
if (is_add)
1858+
{
1859+
own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
18511860

1852-
if (!(is_enumerable_only && !ecma_is_property_enumerable (*property_p)))
1861+
ecma_append_to_values_collection (prop_names_p,
1862+
ecma_make_string_value (index_str_p),
1863+
ECMA_COLLECTION_NO_COPY);
1864+
}
1865+
}
1866+
}
1867+
else
1868+
{
1869+
#if ENABLED (JERRY_PROPRETY_HASHMAP)
1870+
if (prop_iter_cp != JMEM_CP_NULL)
1871+
{
1872+
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
1873+
1874+
if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
1875+
{
1876+
prop_iter_cp = prop_iter_p->next_property_cp;
1877+
}
1878+
}
1879+
#endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */
1880+
1881+
while (prop_iter_cp != JMEM_CP_NULL)
1882+
{
1883+
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
1884+
JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
1885+
1886+
for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++)
1887+
{
1888+
ecma_property_t *property_p = prop_iter_p->types + i;
1889+
1890+
if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA
1891+
|| ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
18531892
{
1854-
#if ENABLED (JERRY_ES2015_BUILTIN_SYMBOL)
1855-
/* If is_symbols_only is false and prop_name is symbol
1856-
we should skip the current property e.g. for-in.
1893+
ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p;
18571894

1858-
Also if is_symbols_only is true and prop_name is not symbol
1859-
we should skip the current property e.g. Object.getOwnPropertySymbols. */
1860-
if (JERRY_UNLIKELY (is_symbols_only != ecma_prop_name_is_symbol (name_p)))
1895+
if (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
1896+
&& prop_pair_p->names_cp[i] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT
1897+
&& prop_pair_p->names_cp[i] < LIT_MAGIC_STRING__COUNT)
18611898
{
1862-
ecma_deref_ecma_string (name_p);
1899+
/* Internal properties are never enumerated. */
18631900
continue;
18641901
}
1865-
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SYMBOL) */
18661902

1867-
uint8_t hash = (uint8_t) ecma_string_hash (name_p);
1868-
uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
1869-
uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
1903+
ecma_string_t *name_p = ecma_string_from_property_name (*property_p,
1904+
prop_pair_p->names_cp[i]);
18701905

1871-
bool is_add = true;
1872-
1873-
if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0)
1906+
if (!(is_enumerable_only && !ecma_is_property_enumerable (*property_p)))
18741907
{
1875-
ecma_value_p = ecma_collection_iterator_init (prop_names_p);
1908+
#if ENABLED (JERRY_ES2015_BUILTIN_SYMBOL)
1909+
/* If is_symbols_only is false and prop_name is symbol
1910+
we should skip the current property e.g. for-in.
1911+
1912+
Also if is_symbols_only is true and prop_name is not symbol
1913+
we should skip the current property e.g. Object.getOwnPropertySymbols. */
1914+
if (JERRY_UNLIKELY (is_symbols_only != ecma_prop_name_is_symbol (name_p)))
1915+
{
1916+
ecma_deref_ecma_string (name_p);
1917+
continue;
1918+
}
1919+
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SYMBOL) */
1920+
1921+
uint8_t hash = (uint8_t) ecma_string_hash (name_p);
1922+
uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
1923+
uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
1924+
1925+
bool is_add = true;
18761926

1877-
while (ecma_value_p != NULL)
1927+
if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0)
18781928
{
1879-
ecma_string_t *current_name_p = ecma_get_prop_name_from_value (*ecma_value_p);
1880-
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
1929+
ecma_value_p = ecma_collection_iterator_init (prop_names_p);
18811930

1882-
if (ecma_compare_ecma_strings (name_p, current_name_p))
1931+
while (ecma_value_p != NULL)
18831932
{
1884-
is_add = false;
1885-
break;
1933+
ecma_string_t *current_name_p = ecma_get_prop_name_from_value (*ecma_value_p);
1934+
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
1935+
1936+
if (ecma_compare_ecma_strings (name_p, current_name_p))
1937+
{
1938+
is_add = false;
1939+
break;
1940+
}
18861941
}
18871942
}
1888-
}
18891943

1890-
if (is_add)
1944+
if (is_add)
1945+
{
1946+
own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
1947+
1948+
ecma_append_to_values_collection (prop_names_p,
1949+
ecma_make_prop_name_value (name_p),
1950+
0);
1951+
}
1952+
}
1953+
else
18911954
{
1892-
own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
1955+
JERRY_ASSERT (is_enumerable_only && !ecma_is_property_enumerable (*property_p));
18931956

1894-
ecma_append_to_values_collection (prop_names_p,
1957+
ecma_append_to_values_collection (skipped_non_enumerable_p,
18951958
ecma_make_prop_name_value (name_p),
18961959
0);
18971960
}
1898-
}
1899-
else
1900-
{
1901-
JERRY_ASSERT (is_enumerable_only && !ecma_is_property_enumerable (*property_p));
19021961

1903-
ecma_append_to_values_collection (skipped_non_enumerable_p,
1904-
ecma_make_prop_name_value (name_p),
1905-
0);
1962+
ecma_deref_ecma_string (name_p);
19061963
}
1907-
1908-
ecma_deref_ecma_string (name_p);
19091964
}
1910-
}
19111965

1912-
prop_iter_cp = prop_iter_p->next_property_cp;
1966+
prop_iter_cp = prop_iter_p->next_property_cp;
1967+
}
19131968
}
19141969

19151970
ecma_value_p = ecma_collection_iterator_init (prop_names_p);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 counter = 0;
16+
var expected = [0];
17+
18+
var b = [$];
19+
function dConstr () { }
20+
dConstr.prototype = b;
21+
var d = new dConstr()
22+
for (var $ in d) {
23+
counter++;
24+
assert($ in expected);
25+
}
26+
27+
assert(counter === 1);

0 commit comments

Comments
 (0)