Skip to content

Commit c79659d

Browse files
authored
Fix array initialization with array holes (#3076)
Fast mode access arrays must be converted back to normal if the array hole count reaches the limit during the initializtaion. This patch fixes #3075. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 3111d0e commit c79659d

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

jerry-core/ecma/operations/ecma-array-object.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,6 @@ ecma_fast_array_convert_to_normal (ecma_object_t *object_p) /**< fast access mod
186186
ecma_deref_object (object_p);
187187
} /* ecma_fast_array_convert_to_normal */
188188

189-
/**
190-
* Maximum number of array holes in a fast mode access array.
191-
* If the number of holes exceeds this limit, the array is converted back
192-
* to normal property list based array.
193-
*/
194-
#define ECMA_FAST_ARRAY_MAX_HOLE_COUNT 32
195-
196189
#if ENABLED (JERRY_SYSTEM_ALLOCATOR)
197190
/**
198191
* Maximum length of the array length to allocate fast mode access for it

jerry-core/ecma/operations/ecma-array-object.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
* @{
2626
*/
2727

28+
/**
29+
* Maximum number of array holes in a fast mode access array.
30+
* If the number of holes exceeds this limit, the array is converted back
31+
* to normal property list based array.
32+
*/
33+
#define ECMA_FAST_ARRAY_MAX_HOLE_COUNT 32
34+
2835
/**
2936
* Flags for ecma_op_array_object_set_length
3037
*/

jerry-core/vm/vm.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,23 +1668,58 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
16681668
ecma_extended_object_t *ext_array_obj_p = (ecma_extended_object_t *) array_obj_p;
16691669
uint32_t old_length = ext_array_obj_p->u.array.length;
16701670

1671-
JERRY_ASSERT (ext_array_obj_p->u.array.is_fast_mode);
1671+
if (JERRY_LIKELY (ext_array_obj_p->u.array.is_fast_mode))
1672+
{
1673+
ecma_value_t *values_p = ecma_fast_array_extend (array_obj_p, old_length + values_length);
16721674

1673-
ecma_value_t *values_p = ecma_fast_array_extend (array_obj_p, old_length + values_length);
1675+
for (uint32_t i = 0; i < values_length; i++)
1676+
{
1677+
values_p[old_length + i] = stack_top_p[i];
16741678

1675-
for (uint32_t i = 0; i < values_length; i++)
1676-
{
1677-
values_p[old_length + i] = stack_top_p[i];
1679+
if (JERRY_UNLIKELY (ecma_is_value_array_hole (stack_top_p[i])))
1680+
{
1681+
ext_array_obj_p->u.array.hole_count++;
1682+
}
1683+
else if (ecma_is_value_object (stack_top_p[i]))
1684+
{
1685+
ecma_deref_object (ecma_get_object_from_value (stack_top_p[i]));
1686+
}
1687+
}
16781688

1679-
if (JERRY_UNLIKELY (ecma_is_value_array_hole (stack_top_p[i])))
1689+
if (JERRY_UNLIKELY (ext_array_obj_p->u.array.length > ECMA_FAST_ARRAY_MAX_HOLE_COUNT))
16801690
{
1681-
ext_array_obj_p->u.array.hole_count++;
1691+
ecma_fast_array_convert_to_normal (array_obj_p);
16821692
}
1683-
else if (ecma_is_value_object (stack_top_p[i]))
1693+
}
1694+
else
1695+
{
1696+
for (uint32_t i = 0; i < values_length; i++)
16841697
{
1685-
ecma_deref_object (ecma_get_object_from_value (stack_top_p[i]));
1698+
if (!ecma_is_value_array_hole (stack_top_p[i]))
1699+
{
1700+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (old_length + i);
1701+
1702+
ecma_property_value_t *prop_value_p;
1703+
1704+
prop_value_p = ecma_create_named_data_property (array_obj_p,
1705+
index_str_p,
1706+
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
1707+
NULL);
1708+
1709+
ecma_deref_ecma_string (index_str_p);
1710+
prop_value_p->value = stack_top_p[i];
1711+
1712+
if (ecma_is_value_object (stack_top_p[i]))
1713+
{
1714+
ecma_free_value (stack_top_p[i]);
1715+
}
1716+
1717+
}
16861718
}
1719+
1720+
ext_array_obj_p->u.array.length = old_length + values_length;
16871721
}
1722+
16881723
continue;
16891724
}
16901725
case VM_OC_PUSH_UNDEFINED_BASE:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 arr = [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ];
16+
arr [4294967294] = 0
17+
assert (arr.length === 4294967295);
18+
assert (arr[4294967294] === 0);

0 commit comments

Comments
 (0)