Skip to content

Commit 3f47e1b

Browse files
rerobikadbatyai
andcommitted
Container object's internal object must be allocated firstly (#2961)
This patch slightly reworks the container objects internal objects allocation. This rework allows the same lifetime of the objects without the manual allocation/deallocation of the internal object. This patch also fixes #2951. Co-authored-by: Dániel Bátyai <dbatyai@inf.u-szeged.hu> JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent a3cd2d7 commit 3f47e1b

File tree

4 files changed

+28
-73
lines changed

4 files changed

+28
-73
lines changed

jerry-core/ecma/base/ecma-gc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ ecma_gc_mark_container_object (ecma_object_t *object_p) /**< object */
254254
ecma_map_object_t *map_object_p = (ecma_map_object_t *) object_p;
255255
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
256256

257+
ecma_gc_set_object_visited (internal_obj_p);
258+
257259
ecma_property_header_t *prop_iter_p = ecma_get_property_list (internal_obj_p);
258260

259261
if (prop_iter_p != NULL && prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
@@ -719,15 +721,13 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
719721
#if ENABLED (JERRY_ES2015_BUILTIN_SET)
720722
case LIT_MAGIC_STRING_SET_UL:
721723
{
722-
ecma_op_container_clear_map ((ecma_map_object_t *) object_p);
723724
ecma_dealloc_extended_object (object_p, sizeof (ecma_map_object_t));
724725
return;
725726
}
726727
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
727728
#if ENABLED (JERRY_ES2015_BUILTIN_MAP)
728729
case LIT_MAGIC_STRING_MAP_UL:
729730
{
730-
ecma_op_container_clear_map ((ecma_map_object_t *) object_p);
731731
ecma_dealloc_extended_object (object_p, sizeof (ecma_map_object_t));
732732
return;
733733
}

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

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,6 @@
3333
* @{
3434
*/
3535

36-
/**
37-
* Creates an empty object for the map/set object's internal slot.
38-
*
39-
* Note: The created object is not registered to the GC.
40-
*
41-
* @return ecma value of the created object
42-
*/
43-
static ecma_value_t
44-
ecma_op_container_create_internal_object (void)
45-
{
46-
ecma_object_t *internal_object_p = ecma_alloc_object ();
47-
internal_object_p->type_flags_refs = (ECMA_OBJECT_TYPE_GENERAL | ECMA_OBJECT_FLAG_EXTENSIBLE | ECMA_OBJECT_REF_ONE);
48-
internal_object_p->property_list_or_bound_object_cp = JMEM_CP_NULL;
49-
internal_object_p->prototype_or_outer_reference_cp = JMEM_CP_NULL;
50-
51-
return ecma_make_object_value (internal_object_p);
52-
} /* ecma_op_container_create_internal_object */
53-
5436
/**
5537
* Handle calling [[Construct]] of built-in map/set like objects
5638
*
@@ -64,15 +46,19 @@ ecma_op_container_create (const ecma_value_t *arguments_list_p, /**< arguments l
6446
{
6547
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
6648

49+
ecma_object_t *internal_object_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
50+
6751
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (proto_id),
6852
sizeof (ecma_map_object_t),
6953
ECMA_OBJECT_TYPE_CLASS);
7054

7155
ecma_map_object_t *map_obj_p = (ecma_map_object_t *) object_p;
7256
map_obj_p->header.u.class_prop.class_id = (uint16_t) lit_id;
73-
map_obj_p->header.u.class_prop.u.value = ecma_op_container_create_internal_object ();
57+
map_obj_p->header.u.class_prop.u.value = ecma_make_object_value (internal_object_p);
7458
map_obj_p->size = 0;
7559

60+
ecma_deref_object (internal_object_p);
61+
7662
ecma_value_t set_value = ecma_make_object_value (object_p);
7763

7864
#if ENABLED (JERRY_ES2015_BUILTIN_SYMBOL)
@@ -433,54 +419,6 @@ ecma_op_container_set (ecma_value_t this_arg, /**< this argument */
433419
return this_arg;
434420
} /* ecma_op_container_set */
435421

436-
/**
437-
* Low-level function to clear all items from a map/set
438-
*/
439-
void
440-
ecma_op_container_clear_map (ecma_map_object_t *map_object_p) /**< map object */
441-
{
442-
ecma_object_t *object_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
443-
444-
JERRY_ASSERT (object_p->type_flags_refs >= ECMA_OBJECT_REF_ONE);
445-
446-
ecma_property_header_t *prop_iter_p = ecma_get_property_list (object_p);
447-
448-
if (prop_iter_p != NULL && prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
449-
{
450-
ecma_property_hashmap_free (object_p);
451-
prop_iter_p = ecma_get_property_list (object_p);
452-
}
453-
454-
while (prop_iter_p != NULL)
455-
{
456-
JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
457-
458-
/* Both cannot be deleted. */
459-
JERRY_ASSERT (prop_iter_p->types[0] != ECMA_PROPERTY_TYPE_DELETED
460-
|| prop_iter_p->types[1] != ECMA_PROPERTY_TYPE_DELETED);
461-
462-
ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p;
463-
464-
for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++)
465-
{
466-
ecma_property_t *property_p = (ecma_property_t *) (prop_iter_p->types + i);
467-
jmem_cpointer_t name_cp = prop_pair_p->names_cp[i];
468-
469-
if (prop_iter_p->types[i] != ECMA_PROPERTY_TYPE_DELETED)
470-
{
471-
ecma_free_property (object_p, name_cp, property_p);
472-
}
473-
}
474-
475-
prop_iter_p = ECMA_GET_POINTER (ecma_property_header_t,
476-
prop_iter_p->next_property_cp);
477-
478-
ecma_dealloc_property_pair (prop_pair_p);
479-
}
480-
481-
ecma_dealloc_object (object_p);
482-
} /* ecma_op_container_clear_map */
483-
484422
/**
485423
* The generic map/set prototype object's 'forEach' routine
486424
*
@@ -592,11 +530,12 @@ ecma_op_container_clear (ecma_value_t this_arg, /**< this argument */
592530
return ECMA_VALUE_ERROR;
593531
}
594532

595-
ecma_op_container_clear_map (map_object_p);
596-
597-
map_object_p->header.u.class_prop.u.value = ecma_op_container_create_internal_object ();
533+
ecma_object_t *internal_object_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
534+
map_object_p->header.u.class_prop.u.value = ecma_make_object_value (internal_object_p);
598535
map_object_p->size = 0;
599536

537+
ecma_deref_object (internal_object_p);
538+
600539
return ECMA_VALUE_UNDEFINED;
601540
} /* ecma_op_container_clear */
602541

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ecma_value_t ecma_op_container_foreach (ecma_value_t this_arg, ecma_value_t pred
3737
ecma_value_t ecma_op_container_has (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
3838
ecma_value_t ecma_op_container_set (ecma_value_t this_arg, ecma_value_t key_arg, ecma_value_t value_arg,
3939
lit_magic_string_id_t lit_id);
40-
void ecma_op_container_clear_map (ecma_map_object_t *map_object_p);
40+
4141
ecma_value_t ecma_op_container_clear (ecma_value_t this_arg, lit_magic_string_id_t lit_id);
4242
ecma_value_t ecma_op_container_delete (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
4343
ecma_value_t ecma_op_container_create_iterator (ecma_value_t this_arg, uint8_t type, lit_magic_string_id_t lit_id,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// To trigger the assertion the engine must be compiled with --system allocator=ON and --mem-stress-test=ON
16+
m = new Map([]);

0 commit comments

Comments
 (0)