Rework ecma collection. (#2153)

Greatly simplify the iterator part and make it compatible with 32 bit cpointers.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2018-01-10 15:55:56 +01:00
committed by Dániel Bátyai
parent f833da2c13
commit b9560b7c70
23 changed files with 511 additions and 521 deletions
-2
View File
@@ -81,8 +81,6 @@ JERRY_STATIC_ASSERT (sizeof (ecma_extended_object_t) - sizeof (ecma_object_t) <=
DEALLOC (ecma_type)
DECLARE_ROUTINES_FOR (number)
DECLARE_ROUTINES_FOR (collection_header)
DECLARE_ROUTINES_FOR (collection_chunk)
/**
* Allocate memory for ecma-object
-24
View File
@@ -61,30 +61,6 @@ ecma_number_t *ecma_alloc_number (void);
*/
void ecma_dealloc_number (ecma_number_t *number_p);
/**
* Allocate memory for header of a collection
*
* @return pointer to allocated memory
*/
ecma_collection_header_t *ecma_alloc_collection_header (void);
/**
* Dealloc memory from the collection's header
*/
void ecma_dealloc_collection_header (ecma_collection_header_t *collection_header_p);
/**
* Allocate memory for non-first chunk of a collection
*
* @return pointer to allocated memory
*/
ecma_collection_chunk_t *ecma_alloc_collection_chunk (void);
/**
* Dealloc memory from non-first chunk of a collection
*/
void ecma_dealloc_collection_chunk (ecma_collection_chunk_t *collection_chunk_p);
/**
* Allocate memory for ecma-string descriptor
*
+13 -9
View File
@@ -257,19 +257,21 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
}
/* Mark all reactions. */
ecma_collection_iterator_t iter;
ecma_collection_iterator_init (&iter, ((ecma_promise_object_t *) ext_object_p)->fulfill_reactions);
ecma_value_t *ecma_value_p;
ecma_value_p = ecma_collection_iterator_init (((ecma_promise_object_t *) ext_object_p)->fulfill_reactions);
while (ecma_collection_iterator_next (&iter))
while (ecma_value_p != NULL)
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*iter.current_value_p));
ecma_gc_set_object_visited (ecma_get_object_from_value (*ecma_value_p));
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
}
ecma_collection_iterator_init (&iter, ((ecma_promise_object_t *) ext_object_p)->reject_reactions);
ecma_value_p = ecma_collection_iterator_init (((ecma_promise_object_t *) ext_object_p)->reject_reactions);
while (ecma_collection_iterator_next (&iter))
while (ecma_value_p != NULL)
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*iter.current_value_p));
ecma_gc_set_object_visited (ecma_get_object_from_value (*ecma_value_p));
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
}
}
@@ -554,8 +556,10 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
case LIT_MAGIC_STRING_REGEXP_UL:
{
ecma_compiled_code_t *bytecode_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t,
ext_object_p->u.class_prop.u.value);
ecma_compiled_code_t *bytecode_p;
bytecode_p = ECMA_GET_INTERNAL_VALUE_ANY_POINTER (ecma_compiled_code_t,
ext_object_p->u.class_prop.u.value);
if (bytecode_p != NULL)
{
ecma_bytecode_deref (bytecode_p);
+31 -14
View File
@@ -74,6 +74,7 @@ typedef enum
ECMA_TYPE_STRING = 2, /**< pointer to description of a string */
ECMA_TYPE_OBJECT = 3, /**< pointer to description of an object */
ECMA_TYPE_ERROR = 7, /**< pointer to description of an error reference */
ECMA_TYPE_COLLECTION_CHUNK = ECMA_TYPE_ERROR, /**< pointer to description of a collection chunk */
ECMA_TYPE___MAX = ECMA_TYPE_ERROR /** highest value for ecma types */
} ecma_type_t;
@@ -1018,31 +1019,47 @@ typedef double ecma_number_t;
*/
#define ECMA_STRING_NOT_ARRAY_INDEX UINT32_MAX
/*
* Ecma-collection: a growable list of ecma-values. Currently the list is
* a chain list, where appending new items at the end is cheap operation.
*
* Enumerating elements is also cheap, since each page is terminated by a
* special ecma-value: collection-type. This type has a pointer to the next
* chunk. The last chunk is terminated by a NULL pointer. There when the
* next value is requested from the iterator it simply checks the next
* memory location. If it is not a collection-type value, it returns with
* the value. Otherwise it gets the start address of the next chunk, and
* return the value there.
*
* The collection-type value is always the last item of a collection chunk,
* even if the chunk is not completely filled with values (this is only true
* for the last chunk). Each chunk must have at least one non collection-type
* value as well.
*/
/**
* Description of a collection's header.
*/
typedef struct
{
/** Number of elements in the collection */
ecma_length_t unit_number;
/** Compressed pointer to first chunk with collection's data */
jmem_cpointer_t first_chunk_cp;
/** Compressed pointer to last chunk with collection's data */
jmem_cpointer_t last_chunk_cp;
jmem_cpointer_t first_chunk_cp; /**< compressed pointer to first chunk with collection's data */
jmem_cpointer_t last_chunk_cp; /**< compressed pointer to last chunk with collection's data */
ecma_length_t item_count; /**< number of items in the collection */
} ecma_collection_header_t;
/**
* Description of non-first chunk in a collection's chain of chunks
* Maximum number of items stored by a collection chunk (excluding the last collection-type value).
*/
#define ECMA_COLLECTION_CHUNK_ITEMS 5
/**
* Collection chunk item.
*/
typedef struct
{
/** Characters */
lit_utf8_byte_t data[ sizeof (uint64_t) - sizeof (jmem_cpointer_t) ];
/** Compressed pointer to next chunk */
jmem_cpointer_t next_chunk_cp;
ecma_value_t items[ECMA_COLLECTION_CHUNK_ITEMS + 1]; /**< ecma-value list, where the last value is a special
* collection-type value which points to the next chunk,
* so the chunk area is enlarged by one for this value */
} ecma_collection_chunk_t;
/**
+56 -4
View File
@@ -77,6 +77,7 @@ ecma_pointer_to_ecma_value (const void *ptr) /**< pointer */
{
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
JERRY_ASSERT (ptr != NULL);
uintptr_t uint_ptr = (uintptr_t) ptr;
JERRY_ASSERT ((uint_ptr & ECMA_VALUE_TYPE_MASK) == 0);
return (ecma_value_t) uint_ptr;
@@ -99,10 +100,11 @@ static inline void * __attr_pure___ __attr_always_inline___
ecma_get_pointer_from_ecma_value (ecma_value_t value) /**< value */
{
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
return (void *) (uintptr_t) ((value) & ~ECMA_VALUE_TYPE_MASK);
void *ptr = (void *) (uintptr_t) ((value) & ~ECMA_VALUE_TYPE_MASK);
JERRY_ASSERT (ptr != NULL);
return ptr;
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
return ECMA_GET_NON_NULL_POINTER (ecma_number_t,
value >> ECMA_VALUE_SHIFT);
return ECMA_GET_NON_NULL_POINTER (void, value >> ECMA_VALUE_SHIFT);
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
} /* ecma_get_pointer_from_ecma_value */
@@ -319,7 +321,7 @@ ecma_is_value_object (ecma_value_t value) /**< ecma value */
/**
* Check if the value is error reference.
*
* @return true - if the value contains object value,
* @return true - if the value contains an error reference,
* false - otherwise
*/
inline bool __attr_const___ __attr_always_inline___
@@ -328,6 +330,18 @@ ecma_is_value_error_reference (ecma_value_t value) /**< ecma value */
return (ecma_get_value_type_field (value) == ECMA_TYPE_ERROR);
} /* ecma_is_value_error_reference */
/**
* Check if the value is collection chunk.
*
* @return true - if the value contains a collection chunk,
* false - otherwise
*/
inline bool __attr_const___ __attr_always_inline___
ecma_is_value_collection_chunk (ecma_value_t value) /**< ecma value */
{
return (ecma_get_value_type_field (value) == ECMA_TYPE_COLLECTION_CHUNK);
} /* ecma_is_value_collection_chunk */
/**
* Debug assertion that specified value's type is one of ECMA-defined
* script-visible types, i.e.: undefined, null, boolean, number, string, object.
@@ -512,6 +526,27 @@ ecma_make_error_reference_value (const ecma_error_reference_t *error_ref_p) /**<
return ecma_pointer_to_ecma_value (error_ref_p) | ECMA_TYPE_ERROR;
} /* ecma_make_error_reference_value */
/**
* Collection chunk constructor
*/
inline ecma_value_t __attr_pure___ __attr_always_inline___
ecma_make_collection_chunk_value (const ecma_collection_chunk_t *collection_chunk_p) /**< collection chunk */
{
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
uintptr_t uint_ptr = (uintptr_t) collection_chunk_p;
JERRY_ASSERT ((uint_ptr & ECMA_VALUE_TYPE_MASK) == 0);
return ((ecma_value_t) uint_ptr) | ECMA_TYPE_COLLECTION_CHUNK;
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
jmem_cpointer_t ptr_cp;
ECMA_SET_POINTER (ptr_cp, collection_chunk_p);
return (((ecma_value_t) ptr_cp) << ECMA_VALUE_SHIFT) | ECMA_TYPE_COLLECTION_CHUNK;
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
} /* ecma_make_collection_chunk_value */
/**
* Get integer value from an integer ecma value
*
@@ -588,6 +623,23 @@ ecma_get_error_reference_from_value (ecma_value_t value) /**< ecma value */
return (ecma_error_reference_t *) ecma_get_pointer_from_ecma_value (value);
} /* ecma_get_error_reference_from_value */
/**
* Get pointer to collection chunk from ecma value
*
* @return the pointer
*/
inline ecma_collection_chunk_t *__attr_pure___ __attr_always_inline___
ecma_get_collection_chunk_from_value (ecma_value_t value) /**< ecma value */
{
JERRY_ASSERT (ecma_get_value_type_field (value) == ECMA_TYPE_COLLECTION_CHUNK);
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
return (ecma_collection_chunk_t *) (uintptr_t) ((value) & ~ECMA_VALUE_TYPE_MASK);
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
return ECMA_GET_POINTER (ecma_collection_chunk_t, value >> ECMA_VALUE_SHIFT);
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
} /* ecma_get_collection_chunk_from_value */
/**
* Invert a boolean value
*
@@ -25,6 +25,12 @@
* @{
*/
/**
* The type of ecma error and ecma collection chunk must be the same.
*/
JERRY_STATIC_ASSERT (ECMA_TYPE_ERROR == ECMA_TYPE_COLLECTION_CHUNK,
ecma_type_error_must_be_the_same_as_ecma_type_collection_chunk);
/**
* Allocate a collection of ecma values.
*
@@ -38,48 +44,56 @@ ecma_new_values_collection (const ecma_value_t values_buffer[], /**< ecma values
{
JERRY_ASSERT (values_buffer != NULL || values_number == 0);
const size_t values_in_chunk = JERRY_SIZE_OF_STRUCT_MEMBER (ecma_collection_chunk_t, data) / sizeof (ecma_value_t);
ecma_collection_header_t *header_p;
header_p = (ecma_collection_header_t *) jmem_pools_alloc (sizeof (ecma_collection_header_t));
ecma_collection_header_t *header_p = ecma_alloc_collection_header ();
header_p->item_count = values_number;
header_p->first_chunk_cp = ECMA_NULL_POINTER;
header_p->last_chunk_cp = ECMA_NULL_POINTER;
header_p->unit_number = values_number;
if (values_number == 0)
{
return header_p;
}
jmem_cpointer_t *next_chunk_cp_p = &header_p->first_chunk_cp;
ecma_collection_chunk_t *last_chunk_p = NULL;
ecma_value_t *cur_value_buf_iter_p = NULL;
ecma_value_t *cur_value_buf_end_p = NULL;
ecma_collection_chunk_t *current_chunk_p = NULL;
int current_chunk_index = ECMA_COLLECTION_CHUNK_ITEMS;
for (ecma_length_t value_index = 0;
value_index < values_number;
value_index++)
{
if (cur_value_buf_iter_p == cur_value_buf_end_p)
if (unlikely (current_chunk_index >= ECMA_COLLECTION_CHUNK_ITEMS))
{
ecma_collection_chunk_t *chunk_p = ecma_alloc_collection_chunk ();
ECMA_SET_POINTER (*next_chunk_cp_p, chunk_p);
next_chunk_cp_p = &chunk_p->next_chunk_cp;
ecma_collection_chunk_t *next_chunk_p;
next_chunk_p = (ecma_collection_chunk_t *) jmem_pools_alloc (sizeof (ecma_collection_chunk_t));
cur_value_buf_iter_p = (ecma_value_t *) chunk_p->data;
cur_value_buf_end_p = cur_value_buf_iter_p + values_in_chunk;
if (header_p->last_chunk_cp == ECMA_NULL_POINTER)
{
ECMA_SET_POINTER (header_p->first_chunk_cp, next_chunk_p);
header_p->last_chunk_cp = header_p->first_chunk_cp;
}
else
{
current_chunk_p->items[ECMA_COLLECTION_CHUNK_ITEMS] = ecma_make_collection_chunk_value (next_chunk_p);
ECMA_SET_POINTER (header_p->last_chunk_cp, next_chunk_p);
}
last_chunk_p = chunk_p;
current_chunk_p = next_chunk_p;
current_chunk_index = 0;
}
JERRY_ASSERT (cur_value_buf_iter_p + 1 <= cur_value_buf_end_p);
ecma_value_t value = values_buffer[value_index];
if (do_ref_if_object)
if (do_ref_if_object || !ecma_is_value_object (value))
{
*cur_value_buf_iter_p++ = ecma_copy_value (values_buffer[value_index]);
}
else
{
*cur_value_buf_iter_p++ = ecma_copy_value_if_not_object (values_buffer[value_index]);
value = ecma_copy_value (value);
}
current_chunk_p->items[current_chunk_index++] = value;
}
*next_chunk_cp_p = ECMA_NULL_POINTER;
ECMA_SET_POINTER (header_p->last_chunk_cp, last_chunk_p);
current_chunk_p->items[current_chunk_index] = ecma_make_collection_chunk_value (NULL);
return header_p;
} /* ecma_new_values_collection */
@@ -91,46 +105,44 @@ ecma_free_values_collection (ecma_collection_header_t *header_p, /**< collection
bool do_deref_if_object) /**< if the value is object value,
decrement reference counter of the object */
{
JERRY_ASSERT (header_p != NULL);
const size_t values_in_chunk = JERRY_SIZE_OF_STRUCT_MEMBER (ecma_collection_chunk_t, data) / sizeof (ecma_value_t);
ecma_collection_chunk_t *chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t,
header_p->first_chunk_cp);
ecma_length_t value_index = 0;
while (chunk_p != NULL)
jmem_heap_free_block (header_p, sizeof (ecma_collection_header_t));
if (chunk_p == NULL)
{
JERRY_ASSERT (value_index < header_p->unit_number);
return;
}
ecma_value_t *cur_value_buf_iter_p = (ecma_value_t *) chunk_p->data;
ecma_value_t *cur_value_buf_end_p = cur_value_buf_iter_p + values_in_chunk;
do
{
ecma_value_t *item_p = chunk_p->items;
while (cur_value_buf_iter_p != cur_value_buf_end_p
&& value_index < header_p->unit_number)
JERRY_ASSERT (!ecma_is_value_collection_chunk (*item_p));
do
{
JERRY_ASSERT (cur_value_buf_iter_p < cur_value_buf_end_p);
if (do_deref_if_object)
{
ecma_free_value (*cur_value_buf_iter_p);
ecma_free_value (*item_p);
}
else
{
ecma_free_value_if_not_object (*cur_value_buf_iter_p);
ecma_free_value_if_not_object (*item_p);
}
cur_value_buf_iter_p++;
value_index++;
item_p++;
}
while (!ecma_is_value_collection_chunk (*item_p));
ecma_collection_chunk_t *next_chunk_p = ecma_get_collection_chunk_from_value (*item_p);
jmem_heap_free_block (chunk_p, sizeof (ecma_collection_chunk_t));
ecma_collection_chunk_t *next_chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t,
chunk_p->next_chunk_cp);
ecma_dealloc_collection_chunk (chunk_p);
chunk_p = next_chunk_p;
}
ecma_dealloc_collection_header (header_p);
while (chunk_p != NULL);
} /* ecma_free_values_collection */
/**
@@ -138,234 +150,98 @@ ecma_free_values_collection (ecma_collection_header_t *header_p, /**< collection
*/
void
ecma_append_to_values_collection (ecma_collection_header_t *header_p, /**< collection's header */
ecma_value_t v, /**< ecma value to append */
ecma_value_t value, /**< ecma value to append */
bool do_ref_if_object) /**< if the value is object value,
increase reference counter of the object */
{
const size_t values_in_chunk = JERRY_SIZE_OF_STRUCT_MEMBER (ecma_collection_chunk_t, data) / sizeof (ecma_value_t);
ecma_length_t item_index;
ecma_collection_chunk_t *chunk_p;
size_t values_number = header_p->unit_number;
size_t pos_of_new_value_in_chunk = values_number % values_in_chunk;
values_number++;
if ((ecma_length_t) values_number == values_number)
if (unlikely (header_p->item_count == 0))
{
header_p->unit_number = (ecma_length_t) values_number;
item_index = 0;
chunk_p = (ecma_collection_chunk_t *) jmem_heap_alloc_block (sizeof (ecma_collection_chunk_t));
ECMA_SET_POINTER (header_p->first_chunk_cp, chunk_p);
header_p->last_chunk_cp = header_p->first_chunk_cp;
}
else
{
jerry_fatal (ERR_OUT_OF_MEMORY);
}
item_index = header_p->item_count % ECMA_COLLECTION_CHUNK_ITEMS;
ecma_collection_chunk_t *chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t,
header_p->last_chunk_cp);
chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
header_p->last_chunk_cp);
if (pos_of_new_value_in_chunk == 0)
{
/* all chunks are currently filled with values */
chunk_p = ecma_alloc_collection_chunk ();
chunk_p->next_chunk_cp = ECMA_NULL_POINTER;
if (header_p->last_chunk_cp == ECMA_NULL_POINTER)
if (unlikely (item_index == 0))
{
JERRY_ASSERT (header_p->first_chunk_cp == ECMA_NULL_POINTER);
JERRY_ASSERT (ecma_is_value_collection_chunk (chunk_p->items[ECMA_COLLECTION_CHUNK_ITEMS])
&& ecma_get_collection_chunk_from_value (chunk_p->items[ECMA_COLLECTION_CHUNK_ITEMS]) == NULL);
ECMA_SET_NON_NULL_POINTER (header_p->first_chunk_cp, chunk_p);
ecma_collection_chunk_t *next_chunk_p;
next_chunk_p = (ecma_collection_chunk_t *) jmem_heap_alloc_block (sizeof (ecma_collection_chunk_t));
chunk_p->items[ECMA_COLLECTION_CHUNK_ITEMS] = ecma_make_collection_chunk_value (next_chunk_p);
ECMA_SET_POINTER (header_p->last_chunk_cp, next_chunk_p);
chunk_p = next_chunk_p;
}
else
{
ecma_collection_chunk_t *last_chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
header_p->last_chunk_cp);
JERRY_ASSERT (last_chunk_p->next_chunk_cp == ECMA_NULL_POINTER);
ECMA_SET_NON_NULL_POINTER (last_chunk_p->next_chunk_cp, chunk_p);
JERRY_ASSERT (ecma_is_value_collection_chunk (chunk_p->items[item_index])
&& ecma_get_collection_chunk_from_value (chunk_p->items[item_index]) == NULL);
}
ECMA_SET_NON_NULL_POINTER (header_p->last_chunk_cp, chunk_p);
}
else
if (do_ref_if_object || !ecma_is_value_object (value))
{
/* last chunk can be appended with the new value */
JERRY_ASSERT (chunk_p != NULL);
value = ecma_copy_value (value);
}
ecma_value_t *values_p = (ecma_value_t *) chunk_p->data;
JERRY_ASSERT ((uint8_t *) (values_p + pos_of_new_value_in_chunk + 1) <= (uint8_t *) (chunk_p + 1));
if (do_ref_if_object)
{
values_p[pos_of_new_value_in_chunk] = ecma_copy_value (v);
}
else
{
values_p[pos_of_new_value_in_chunk] = ecma_copy_value_if_not_object (v);
}
chunk_p->items[item_index] = value;
chunk_p->items[item_index + 1] = ecma_make_collection_chunk_value (NULL);
header_p->item_count++;
} /* ecma_append_to_values_collection */
/**
* Remove last element of the collection
*
* Warning:
* the function invalidates all iterators that are configured to access the passed collection
*/
void
ecma_remove_last_value_from_values_collection (ecma_collection_header_t *header_p) /**< collection's header */
{
JERRY_ASSERT (header_p != NULL && header_p->unit_number > 0);
const size_t values_in_chunk = JERRY_SIZE_OF_STRUCT_MEMBER (ecma_collection_chunk_t, data) / sizeof (ecma_value_t);
size_t values_number = header_p->unit_number;
size_t pos_of_value_to_remove_in_chunk = (values_number - 1u) % values_in_chunk;
ecma_collection_chunk_t *last_chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
header_p->last_chunk_cp);
ecma_value_t *values_p = (ecma_value_t *) last_chunk_p->data;
JERRY_ASSERT ((uint8_t *) (values_p + pos_of_value_to_remove_in_chunk + 1) <= (uint8_t *) (last_chunk_p + 1));
ecma_value_t value_to_remove = values_p[pos_of_value_to_remove_in_chunk];
ecma_free_value (value_to_remove);
header_p->unit_number--;
if (pos_of_value_to_remove_in_chunk == 0)
{
ecma_collection_chunk_t *chunk_to_remove_p = last_chunk_p;
/* free last chunk */
if (header_p->first_chunk_cp == header_p->last_chunk_cp)
{
header_p->first_chunk_cp = ECMA_NULL_POINTER;
header_p->last_chunk_cp = ECMA_NULL_POINTER;
}
else
{
ecma_collection_chunk_t *chunk_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
header_p->first_chunk_cp);
while (chunk_iter_p->next_chunk_cp != header_p->last_chunk_cp)
{
chunk_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
chunk_iter_p->next_chunk_cp);
}
ecma_collection_chunk_t *new_last_chunk_p = chunk_iter_p;
JERRY_ASSERT (ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
new_last_chunk_p->next_chunk_cp) == chunk_to_remove_p);
ECMA_SET_NON_NULL_POINTER (header_p->last_chunk_cp, new_last_chunk_p);
new_last_chunk_p->next_chunk_cp = ECMA_NULL_POINTER;
}
ecma_dealloc_collection_chunk (chunk_to_remove_p);
}
} /* ecma_remove_last_value_from_values_collection */
/**
* Allocate a collection of ecma-strings.
*
* @return pointer to the collection's header
*/
ecma_collection_header_t *
ecma_new_strings_collection (ecma_string_t *string_ptrs_buffer[], /**< pointers to ecma-strings */
ecma_length_t strings_number) /**< number of ecma-strings */
{
JERRY_ASSERT (string_ptrs_buffer != NULL || strings_number == 0);
ecma_collection_header_t *new_collection_p;
new_collection_p = ecma_new_values_collection (NULL, 0, false);
for (ecma_length_t string_index = 0;
string_index < strings_number;
string_index++)
{
ecma_append_to_values_collection (new_collection_p,
ecma_make_string_value (string_ptrs_buffer[string_index]),
false);
}
return new_collection_p;
} /* ecma_new_strings_collection */
/**
* Initialize new collection iterator for the collection
*
* @return pointer to the first item
*/
void
ecma_collection_iterator_init (ecma_collection_iterator_t *iterator_p, /**< context of iterator */
ecma_collection_header_t *collection_p) /**< header of collection */
ecma_value_t *
ecma_collection_iterator_init (ecma_collection_header_t *header_p) /**< header of collection */
{
iterator_p->header_p = collection_p;
iterator_p->next_chunk_cp = (collection_p != NULL ? collection_p->first_chunk_cp : JMEM_CP_NULL);
iterator_p->current_index = 0;
iterator_p->current_value_p = NULL;
iterator_p->current_chunk_end_p = NULL;
if (unlikely (!header_p || header_p->item_count == 0))
{
return NULL;
}
ecma_collection_chunk_t *chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
header_p->first_chunk_cp);
return chunk_p->items;
} /* ecma_collection_iterator_init */
/**
* Move collection iterator to next element if there is any.
*
* @return true - if iterator moved,
* false - otherwise (current element is last element in the collection)
* @return pointer to the next item
*/
bool
ecma_collection_iterator_next (ecma_collection_iterator_t *iterator_p) /**< context of iterator */
ecma_value_t *
ecma_collection_iterator_next (ecma_value_t *ecma_value_p) /**< current value */
{
if (iterator_p->header_p == NULL
|| unlikely (iterator_p->header_p->unit_number == 0)
|| unlikely (iterator_p->header_p->first_chunk_cp == JMEM_CP_NULL))
JERRY_ASSERT (ecma_value_p != NULL);
ecma_value_p++;
if (unlikely (ecma_is_value_collection_chunk (*ecma_value_p)))
{
return false;
ecma_value_p = ecma_get_collection_chunk_from_value (*ecma_value_p)->items;
JERRY_ASSERT (ecma_value_p == NULL || !ecma_is_value_collection_chunk (*ecma_value_p));
return ecma_value_p;
}
const size_t values_in_chunk = JERRY_SIZE_OF_STRUCT_MEMBER (ecma_collection_chunk_t, data) / sizeof (ecma_value_t);
if (iterator_p->current_value_p == NULL)
{
JERRY_ASSERT (iterator_p->current_index == 0);
ecma_collection_chunk_t *first_chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t,
iterator_p->header_p->first_chunk_cp);
iterator_p->next_chunk_cp = first_chunk_p->next_chunk_cp;
iterator_p->current_value_p = (ecma_value_t *) &first_chunk_p->data;
iterator_p->current_chunk_end_p = (iterator_p->current_value_p + values_in_chunk);
}
else
{
if (iterator_p->current_index + 1 == iterator_p->header_p->unit_number)
{
return false;
}
JERRY_ASSERT (iterator_p->current_index + 1 < iterator_p->header_p->unit_number);
iterator_p->current_index++;
iterator_p->current_value_p++;
}
if (iterator_p->current_value_p == iterator_p->current_chunk_end_p)
{
ecma_collection_chunk_t *next_chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t,
iterator_p->next_chunk_cp);
JERRY_ASSERT (next_chunk_p != NULL);
iterator_p->next_chunk_cp = next_chunk_p->next_chunk_cp;
iterator_p->current_value_p = (ecma_value_t *) &next_chunk_p->data;
iterator_p->current_chunk_end_p = iterator_p->current_value_p + values_in_chunk;
}
else
{
JERRY_ASSERT (iterator_p->current_value_p < iterator_p->current_chunk_end_p);
}
return true;
return ecma_value_p;
} /* ecma_collection_iterator_next */
/**
+37 -27
View File
@@ -71,30 +71,53 @@
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
/**
* Set an internal property value of pointer
* Set an internal property value from pointer.
*/
#define ECMA_SET_INTERNAL_VALUE_POINTER(field, pointer) \
(field) = ((ecma_value_t) pointer)
/**
* Get an internal property value of pointer
* Set an internal property value from pointer. Pointer can be NULL.
*/
#define ECMA_SET_INTERNAL_VALUE_ANY_POINTER(field, pointer) \
(field) = ((ecma_value_t) pointer)
/**
* Convert an internal property value to pointer.
*/
#define ECMA_GET_INTERNAL_VALUE_POINTER(type, field) \
((type *) field)
/**
* Convert an internal property value to pointer. Result can be NULL.
*/
#define ECMA_GET_INTERNAL_VALUE_ANY_POINTER(type, field) \
((type *) field)
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
/**
* Set an internal property value of non-null pointer so that it will correspond
* to specified non_compressed_pointer.
* Set an internal property value from pointer.
*/
#define ECMA_SET_INTERNAL_VALUE_POINTER(field, non_compressed_pointer) \
ECMA_SET_NON_NULL_POINTER (field, non_compressed_pointer)
#define ECMA_SET_INTERNAL_VALUE_POINTER(field, pointer) \
ECMA_SET_NON_NULL_POINTER (field, pointer)
/**
* Get an internal property value of pointer from specified compressed pointer.
* Set an internal property value from pointer. Pointer can be NULL.
*/
#define ECMA_SET_INTERNAL_VALUE_ANY_POINTER(field, pointer) \
ECMA_SET_POINTER (field, pointer)
/**
* Convert an internal property value to pointer.
*/
#define ECMA_GET_INTERNAL_VALUE_POINTER(type, field) \
ECMA_GET_NON_NULL_POINTER (type, field)
/**
* Convert an internal property value to pointer. Result can be NULL.
*/
#define ECMA_GET_INTERNAL_VALUE_ANY_POINTER(type, field) \
ECMA_GET_POINTER (type, field)
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
@@ -134,6 +157,7 @@ bool ecma_is_value_number (ecma_value_t value) __attr_const___;
bool ecma_is_value_string (ecma_value_t value) __attr_const___;
bool ecma_is_value_object (ecma_value_t value) __attr_const___;
bool ecma_is_value_error_reference (ecma_value_t value) __attr_const___;
bool ecma_is_value_collection_chunk (ecma_value_t value) __attr_const___;
void ecma_check_value_type_is_spec_defined (ecma_value_t value);
@@ -146,12 +170,14 @@ ecma_value_t ecma_make_uint32_value (uint32_t uint32_number);
ecma_value_t ecma_make_string_value (const ecma_string_t *ecma_string_p) __attr_pure___;
ecma_value_t ecma_make_object_value (const ecma_object_t *object_p) __attr_pure___;
ecma_value_t ecma_make_error_reference_value (const ecma_error_reference_t *error_ref_p) __attr_pure___;
ecma_value_t ecma_make_collection_chunk_value (const ecma_collection_chunk_t *collection_chunk_p) __attr_pure___;
ecma_integer_value_t ecma_get_integer_from_value (ecma_value_t value) __attr_const___;
ecma_number_t ecma_get_float_from_value (ecma_value_t value) __attr_pure___;
ecma_number_t ecma_get_number_from_value (ecma_value_t value) __attr_pure___;
ecma_string_t *ecma_get_string_from_value (ecma_value_t value) __attr_pure___;
ecma_object_t *ecma_get_object_from_value (ecma_value_t value) __attr_pure___;
ecma_error_reference_t *ecma_get_error_reference_from_value (ecma_value_t value) __attr_pure___;
ecma_collection_chunk_t *ecma_get_collection_chunk_from_value (ecma_value_t value) __attr_pure___;
ecma_value_t ecma_invert_boolean_value (ecma_value_t value) __attr_const___;
ecma_value_t ecma_copy_value (ecma_value_t value);
ecma_value_t ecma_fast_copy_value (ecma_value_t value);
@@ -264,27 +290,11 @@ ecma_collection_header_t *ecma_new_values_collection (const ecma_value_t values_
bool do_ref_if_object);
void ecma_free_values_collection (ecma_collection_header_t *header_p, bool do_deref_if_object);
void ecma_append_to_values_collection (ecma_collection_header_t *header_p, ecma_value_t v, bool do_ref_if_object);
void ecma_remove_last_value_from_values_collection (ecma_collection_header_t *header_p);
ecma_collection_header_t *ecma_new_strings_collection (ecma_string_t *string_ptrs_buffer[],
ecma_length_t strings_number);
/**
* Context of ecma values' collection iterator
*/
typedef struct
{
ecma_collection_header_t *header_p; /**< collection header */
jmem_cpointer_t next_chunk_cp; /**< compressed pointer to next chunk */
ecma_length_t current_index; /**< index of current element */
const ecma_value_t *current_value_p; /**< pointer to current element */
const ecma_value_t *current_chunk_beg_p; /**< pointer to beginning of current chunk's data */
const ecma_value_t *current_chunk_end_p; /**< pointer to place right after the end of current chunk's data */
} ecma_collection_iterator_t;
void
ecma_collection_iterator_init (ecma_collection_iterator_t *iterator_p, ecma_collection_header_t *collection_p);
bool
ecma_collection_iterator_next (ecma_collection_iterator_t *iterator_p);
ecma_value_t *
ecma_collection_iterator_init (ecma_collection_header_t *header_p);
ecma_value_t *
ecma_collection_iterator_next (ecma_value_t *iterator_p);
/* ecma-helpers.c */
ecma_object_t *ecma_create_object (ecma_object_t *prototype_object_p, size_t ext_object_size, ecma_object_type_t type);