Optimize string concatenation. (#2141)

This patch adds two new string concatenation functions:
ecma_append_chars_to_string and ecma_append_magic_string_to_string

The former appends a cesu8 byte array and the latter appends a magic string
to the end of an ecma-string. These two free (dereference) their ecma-string
argument, and this change is also applied to the original ecma_concat_ecma_strings
function which simplifies string handling in most cases.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2017-12-19 13:45:48 +01:00
committed by GitHub
parent bd574956e3
commit a2d3ea61eb
16 changed files with 369 additions and 555 deletions
@@ -151,8 +151,6 @@ ecma_builtin_array_prototype_object_to_locale_string (const ecma_value_t this_ar
uint32_t length = ecma_number_to_uint32 (length_number);
/* 4. Implementation-defined: set the separator to a single comma character */
ecma_string_t *separator_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
/* 5. */
if (length == 0)
@@ -173,21 +171,18 @@ ecma_builtin_array_prototype_object_to_locale_string (const ecma_value_t this_ar
/* 9-10. */
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
{
ecma_string_t *part_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p);
/* 4. Implementation-defined: set the separator to a single comma character. */
return_string_p = ecma_append_magic_string_to_string (return_string_p,
LIT_MAGIC_STRING_COMMA_CHAR);
ECMA_TRY_CATCH (next_string_value,
ecma_builtin_helper_get_to_locale_string_at_index (obj_p, k),
ret_value);
ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value);
ecma_deref_ecma_string (return_string_p);
return_string_p = ecma_concat_ecma_strings (part_string_p, next_string_p);
return_string_p = ecma_concat_ecma_strings (return_string_p, next_string_p);
ECMA_FINALIZE (next_string_value);
ecma_deref_ecma_string (part_string_p);
}
if (ecma_is_value_empty (ret_value))
@@ -202,8 +197,6 @@ ecma_builtin_array_prototype_object_to_locale_string (const ecma_value_t this_ar
ECMA_FINALIZE (first_value);
}
ecma_deref_ecma_string (separator_string_p);
ECMA_OP_TO_NUMBER_FINALIZE (length_number);
ECMA_FINALIZE (length_value);
@@ -397,23 +390,18 @@ ecma_builtin_array_prototype_join (const ecma_value_t this_arg, /**< this argume
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
{
/* 10.a */
ecma_string_t *part_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p);
return_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p);
/* 10.b, 10.c */
ECMA_TRY_CATCH (next_string_value,
ecma_op_array_get_to_string_at_index (obj_p, k),
ret_value);
ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value);
ecma_deref_ecma_string (return_string_p);
/* 10.d */
return_string_p = ecma_concat_ecma_strings (part_string_p, next_string_p);
ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value);
return_string_p = ecma_concat_ecma_strings (return_string_p, next_string_p);
ECMA_FINALIZE (next_string_value);
ecma_deref_ecma_string (part_string_p);
}
if (ecma_is_value_empty (ret_value))
@@ -82,8 +82,6 @@ ecma_builtin_function_helper_get_function_arguments (const ecma_value_t *argumen
return final_str;
}
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
for (ecma_length_t idx = 1; idx < arguments_list_len - 1; idx++)
{
ecma_value_t new_str = ecma_op_to_string (arguments_list_p[idx]);
@@ -98,18 +96,16 @@ ecma_builtin_function_helper_get_function_arguments (const ecma_value_t *argumen
}
ecma_string_t *final_str_p = ecma_get_string_from_value (final_str);
ecma_string_t *fragment_str_p = ecma_concat_ecma_strings (final_str_p, comma_str_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = ecma_append_magic_string_to_string (final_str_p,
LIT_MAGIC_STRING_COMMA_CHAR);
ecma_string_t *new_str_p = ecma_get_string_from_value (new_str);
final_str_p = ecma_concat_ecma_strings (fragment_str_p, new_str_p);
final_str_p = ecma_concat_ecma_strings (final_str_p, new_str_p);
ecma_deref_ecma_string (new_str_p);
ecma_deref_ecma_string (fragment_str_p);
final_str = ecma_make_string_value (final_str_p);
}
ecma_deref_ecma_string (comma_str_p);
return final_str;
} /* ecma_builtin_function_helper_get_function_arguments */
@@ -110,35 +110,24 @@ ecma_builtin_helper_json_create_separated_properties (ecma_collection_header_t *
ecma_string_t *separator_p) /**< separator*/
{
ecma_string_t *properties_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
ecma_string_t *tmp_str_p;
ecma_collection_iterator_t iterator;
ecma_collection_iterator_init (&iterator, partial_p);
uint32_t index = 0;
bool first = true;
while (ecma_collection_iterator_next (&iterator))
{
ecma_value_t name_value = *iterator.current_value_p;
ecma_string_t *current_p = ecma_get_string_from_value (name_value);
if (index == 0)
if (likely (!first))
{
index++;
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, current_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
continue;
properties_str_p = ecma_concat_ecma_strings (properties_str_p, separator_p);
}
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, separator_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, current_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
properties_str_p = ecma_concat_ecma_strings (properties_str_p, current_p);
first = false;
}
return properties_str_p;
@@ -158,58 +147,43 @@ ecma_builtin_helper_json_create_separated_properties (ecma_collection_header_t *
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/
ecma_string_t *right_bracket_p, /**< right bracket*/
ecma_builtin_helper_json_create_formatted_json (lit_utf8_byte_t left_bracket, /**< left bracket character */
lit_utf8_byte_t right_bracket, /**< right bracket character */
ecma_string_t *stepback_p, /**< stepback*/
ecma_collection_header_t *partial_p, /**< key-value pairs*/
ecma_json_stringify_context_t *context_p) /**< context*/
{
/* 10.b */
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
ecma_string_t *line_feed_p = ecma_get_magic_string (LIT_MAGIC_STRING_NEW_LINE_CHAR);
ecma_string_t *properties_str_p;
ecma_string_t *separator_p;
JERRY_ASSERT (left_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX
&& right_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX);
/* 10.b.i */
ecma_string_t *tmp_str_p = ecma_concat_ecma_strings (comma_str_p, line_feed_p);
ecma_deref_ecma_string (comma_str_p);
separator_p = tmp_str_p;
lit_utf8_byte_t chars[2] = { LIT_CHAR_COMMA, LIT_CHAR_LF };
tmp_str_p = ecma_concat_ecma_strings (separator_p, context_p->indent_str_p);
ecma_deref_ecma_string (separator_p);
separator_p = tmp_str_p;
ecma_string_t *separator_p = ecma_new_ecma_string_from_utf8 (chars, 2);
separator_p = ecma_concat_ecma_strings (separator_p, context_p->indent_str_p);
/* 10.b.ii */
properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, separator_p);
ecma_string_t *properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, separator_p);
ecma_deref_ecma_string (separator_p);
/* 10.b.iii */
ecma_string_t *final_str_p;
chars[0] = left_bracket;
tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, line_feed_p);
final_str_p = tmp_str_p;
ecma_string_t *final_str_p = ecma_new_ecma_string_from_utf8 (chars, 2);
tmp_str_p = ecma_concat_ecma_strings (final_str_p, context_p->indent_str_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
final_str_p = ecma_concat_ecma_strings (final_str_p, context_p->indent_str_p);
tmp_str_p = ecma_concat_ecma_strings (final_str_p, properties_str_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = ecma_concat_ecma_strings (final_str_p, properties_str_p);
ecma_deref_ecma_string (properties_str_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, line_feed_p);
ecma_deref_ecma_string (line_feed_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
chars[0] = LIT_CHAR_LF;
final_str_p = ecma_append_chars_to_string (final_str_p, chars, 1, 1);
tmp_str_p = ecma_concat_ecma_strings (final_str_p, stepback_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
final_str_p = ecma_concat_ecma_strings (final_str_p, stepback_p);
tmp_str_p = ecma_concat_ecma_strings (final_str_p, right_bracket_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
chars[0] = right_bracket;
final_str_p = ecma_append_chars_to_string (final_str_p, chars, 1, 1);
return ecma_make_string_value (final_str_p);
} /* ecma_builtin_helper_json_create_formatted_json */
@@ -228,81 +202,34 @@ ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p, /
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_helper_json_create_non_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/
ecma_string_t *right_bracket_p, /**< right bracket*/
ecma_collection_header_t *partial_p) /**< key-value pairs*/
ecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, /**< left bracket character */
lit_utf8_byte_t right_bracket, /**< right bracket character */
ecma_collection_header_t *partial_p) /**< key-value pairs */
{
JERRY_ASSERT (left_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX
&& right_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX);
/* 10.a */
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
ecma_string_t *properties_str_p;
ecma_string_t *tmp_str_p;
/* 10.a.i */
properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, comma_str_p);
ecma_deref_ecma_string (comma_str_p);
/* 10.a.ii */
tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, properties_str_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
ecma_string_t *result_str_p = ecma_new_ecma_string_from_code_unit (left_bracket);
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, right_bracket_p);
result_str_p = ecma_concat_ecma_strings (result_str_p, properties_str_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
return ecma_make_string_value (properties_str_p);
lit_utf8_byte_t chars[1] = { right_bracket };
result_str_p = ecma_append_chars_to_string (result_str_p, chars, 1, 1);
return ecma_make_string_value (result_str_p);
} /* ecma_builtin_helper_json_create_non_formatted_json */
/**
* Convert decimal value to 4 digit hexadecimal string value.
*
* See also:
* ECMA-262 v5, 15.12.3
*
* Used by:
* - ecma_builtin_json_quote step 2.c.iii
*
* @return pointer to ecma-string
* Returned value must be freed with ecma_deref_ecma_string.
*/
ecma_string_t *
ecma_builtin_helper_json_create_hex_digit_ecma_string (uint8_t value) /**< value in decimal*/
{
/* 2.c.iii */
ecma_string_t *hex_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
JMEM_DEFINE_LOCAL_ARRAY (hex_buff, 4, lit_utf8_byte_t);
for (uint32_t i = 0; i < 4; i++)
{
uint8_t remainder = value % 16;
lit_utf8_byte_t ch = ' ';
if (remainder < 10)
{
ch = (lit_utf8_byte_t) (LIT_CHAR_0 + remainder);
}
else
{
uint8_t a = (uint8_t) (remainder - 10);
ch = (lit_utf8_byte_t) (LIT_CHAR_LOWERCASE_A + a);
}
hex_buff[3 - i] = ch;
value = value / 16;
}
ecma_deref_ecma_string (hex_str_p);
hex_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) hex_buff, 4);
JMEM_FINALIZE_LOCAL_ARRAY (hex_buff);
JERRY_ASSERT (ecma_string_get_length (hex_str_p));
return hex_str_p;
} /* ecma_builtin_helper_json_create_hex_digit_ecma_string */
/**
* @}
* @}
@@ -146,16 +146,14 @@ typedef struct
bool ecma_has_object_value_in_collection (ecma_collection_header_t *collection_p, ecma_value_t object_value);
bool ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, ecma_value_t string_value);
ecma_string_t *
ecma_builtin_helper_json_create_hex_digit_ecma_string (uint8_t value);
ecma_string_t *
ecma_builtin_helper_json_create_separated_properties (ecma_collection_header_t *partial_p, ecma_string_t *separator_p);
ecma_value_t
ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p, ecma_string_t *right_bracket_p,
ecma_builtin_helper_json_create_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket,
ecma_string_t *stepback_p, ecma_collection_header_t *partial_p,
ecma_json_stringify_context_t *context_p);
ecma_value_t
ecma_builtin_helper_json_create_non_formatted_json (ecma_string_t *left_bracket_p, ecma_string_t *right_bracket_p,
ecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket,
ecma_collection_header_t *partial_p);
/* ecma-builtin-helper-error.c */
@@ -1156,10 +1156,7 @@ static ecma_value_t
ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quoted*/
{
/* 1. */
ecma_string_t *quote_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR);
ecma_ref_ecma_string (quote_str_p);
ecma_string_t *product_str_p = quote_str_p;
ecma_string_t *tmp_str_p;
ecma_string_t *product_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR);
ECMA_STRING_TO_UTF8_STRING (string_p, string_buff, string_buff_size);
@@ -1170,42 +1167,16 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
{
ecma_char_t current_char = lit_utf8_read_next (&str_p);
/* 2.a */
if (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE)
/* 2.a, b */
if (current_char == LIT_CHAR_BACKSLASH
|| current_char == LIT_CHAR_DOUBLE_QUOTE
|| current_char == LIT_CHAR_BS
|| current_char == LIT_CHAR_FF
|| current_char == LIT_CHAR_LF
|| current_char == LIT_CHAR_CR
|| current_char == LIT_CHAR_TAB)
{
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
/* 2.a.i */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, backslash_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (backslash_str_p);
product_str_p = tmp_str_p;
/* 2.a.ii */
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (current_char_str_p);
product_str_p = tmp_str_p;
}
/* 2.b */
else if (current_char == LIT_CHAR_BS
|| current_char == LIT_CHAR_FF
|| current_char == LIT_CHAR_LF
|| current_char == LIT_CHAR_CR
|| current_char == LIT_CHAR_TAB)
{
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
/* 2.b.i */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, backslash_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (backslash_str_p);
product_str_p = tmp_str_p;
/* 2.b.ii */
lit_utf8_byte_t abbrev = LIT_CHAR_SP;
lit_utf8_byte_t abbrev = (lit_utf8_byte_t) current_char;
switch (current_char)
{
@@ -1234,64 +1205,54 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
abbrev = LIT_CHAR_LOWERCASE_T;
break;
}
default:
{
JERRY_ASSERT (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE);
break;
}
}
/* 2.b.iii */
ecma_string_t *abbrev_str_p = ecma_new_ecma_string_from_utf8 (&abbrev, 1);
lit_utf8_byte_t chars[2] = { LIT_CHAR_BACKSLASH, abbrev };
tmp_str_p = ecma_concat_ecma_strings (product_str_p, abbrev_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (abbrev_str_p);
product_str_p = tmp_str_p;
product_str_p = ecma_append_chars_to_string (product_str_p, chars, 2, 2);
}
/* 2.c */
else if (current_char < LIT_CHAR_SP)
{
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
lit_utf8_byte_t chars[6] = { LIT_CHAR_BACKSLASH, LIT_CHAR_LOWERCASE_U, LIT_CHAR_0, LIT_CHAR_0 };
/* 2.c.i */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, backslash_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (backslash_str_p);
product_str_p = tmp_str_p;
JERRY_ASSERT (current_char < 0x9f);
/* 2.c.ii */
lit_utf8_byte_t u_ch = LIT_CHAR_LOWERCASE_U;
ecma_string_t *u_ch_str_p = ecma_new_ecma_string_from_utf8 (&u_ch, 1);
chars[4] = (lit_utf8_byte_t) (LIT_CHAR_0 + (current_char >> 4));
tmp_str_p = ecma_concat_ecma_strings (product_str_p, u_ch_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (u_ch_str_p);
product_str_p = tmp_str_p;
int last_char = current_char & 0xf;
last_char += (last_char <= 9) ? LIT_CHAR_0 : (LIT_CHAR_LOWERCASE_A - 10);
/* 2.c.iii */
ecma_string_t *hex_str_p = ecma_builtin_helper_json_create_hex_digit_ecma_string ((uint8_t) current_char);
chars[5] = (lit_utf8_byte_t) last_char;
/* 2.c.iv */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, hex_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (hex_str_p);
product_str_p = tmp_str_p;
product_str_p = ecma_append_chars_to_string (product_str_p, chars, 6, 6);
}
/* 2.d */
else if (current_char < LIT_UTF8_1_BYTE_CODE_POINT_MAX)
{
/* Fast case for ascii characters. */
lit_utf8_byte_t chars[1] = { (lit_utf8_byte_t) current_char };
product_str_p = ecma_append_chars_to_string (product_str_p, chars, 1, 1);
}
else
{
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
ecma_deref_ecma_string (product_str_p);
product_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
ecma_deref_ecma_string (current_char_str_p);
product_str_p = tmp_str_p;
}
}
ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
/* 3. */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, quote_str_p);
ecma_deref_ecma_string (product_str_p);
ecma_deref_ecma_string (quote_str_p);
product_str_p = tmp_str_p;
product_str_p = ecma_append_magic_string_to_string (product_str_p, LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR);
/* 4. */
return ecma_make_string_value (product_str_p);
@@ -1515,7 +1476,8 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
ecma_string_t *stepback_p = context_p->indent_str_p;
/* 4. */
context_p->indent_str_p = ecma_concat_ecma_strings (context_p->indent_str_p, context_p->gap_str_p);
ecma_ref_ecma_string (stepback_p);
context_p->indent_str_p = ecma_concat_ecma_strings (stepback_p, context_p->gap_str_p);
ecma_collection_header_t *property_keys_p;
@@ -1574,9 +1536,7 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
/* 8.b */
if (!ecma_is_value_undefined (str_val))
{
ecma_string_t *colon_p = ecma_get_magic_string (LIT_MAGIC_STRING_COLON_CHAR);
ecma_string_t *value_str_p = ecma_get_string_from_value (str_val);
ecma_string_t *tmp_str_p;
/* 8.b.i */
ecma_value_t str_comp_val = ecma_builtin_json_quote (key_p);
@@ -1585,26 +1545,16 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
ecma_string_t *member_str_p = ecma_get_string_from_value (str_comp_val);
/* 8.b.ii */
tmp_str_p = ecma_concat_ecma_strings (member_str_p, colon_p);
ecma_free_value (str_comp_val);
ecma_deref_ecma_string (colon_p);
member_str_p = tmp_str_p;
member_str_p = ecma_append_magic_string_to_string (member_str_p, LIT_MAGIC_STRING_COLON_CHAR);
/* 8.b.iii */
if (!ecma_string_is_empty (context_p->gap_str_p))
{
ecma_string_t *space_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR);
tmp_str_p = ecma_concat_ecma_strings (member_str_p, space_str_p);
ecma_deref_ecma_string (member_str_p);
ecma_deref_ecma_string (space_str_p);
member_str_p = tmp_str_p;
member_str_p = ecma_append_magic_string_to_string (member_str_p, LIT_MAGIC_STRING_SPACE_CHAR);
}
/* 8.b.iv */
tmp_str_p = ecma_concat_ecma_strings (member_str_p, value_str_p);
ecma_deref_ecma_string (member_str_p);
member_str_p = tmp_str_p;
member_str_p = ecma_concat_ecma_strings (member_str_p, value_str_p);
/* 8.b.v */
ecma_value_t member_value = ecma_make_string_value (member_str_p);
@@ -1630,14 +1580,10 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
/* 9. */
if (partial_p->unit_number == 0)
{
ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR);
ecma_string_t *right_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR);
lit_utf8_byte_t chars[2] = { LIT_CHAR_LEFT_BRACE, LIT_CHAR_RIGHT_BRACE };
ecma_string_t *final_str_p = ecma_concat_ecma_strings (left_brace_str_p, right_brace_str_p);
ecma_string_t *final_str_p = ecma_new_ecma_string_from_utf8 (chars, 2);
ret_value = ecma_make_string_value (final_str_p);
ecma_deref_ecma_string (left_brace_str_p);
ecma_deref_ecma_string (right_brace_str_p);
}
/* 10. */
else
@@ -1645,30 +1591,18 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
/* 10.a */
if (ecma_string_is_empty (context_p->gap_str_p))
{
ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR);
ecma_string_t *right_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR);
ret_value = ecma_builtin_helper_json_create_non_formatted_json (left_brace_str_p,
right_brace_str_p,
ret_value = ecma_builtin_helper_json_create_non_formatted_json (LIT_CHAR_LEFT_BRACE,
LIT_CHAR_RIGHT_BRACE,
partial_p);
ecma_deref_ecma_string (left_brace_str_p);
ecma_deref_ecma_string (right_brace_str_p);
}
/* 10.b */
else
{
ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR);
ecma_string_t *right_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR);
ret_value = ecma_builtin_helper_json_create_formatted_json (left_brace_str_p,
right_brace_str_p,
ret_value = ecma_builtin_helper_json_create_formatted_json (LIT_CHAR_LEFT_BRACE,
LIT_CHAR_RIGHT_BRACE,
stepback_p,
partial_p,
context_p);
ecma_deref_ecma_string (left_brace_str_p);
ecma_deref_ecma_string (right_brace_str_p);
}
}
@@ -1715,7 +1649,8 @@ ecma_builtin_json_array (ecma_object_t *obj_p, /**< the array object*/
ecma_string_t *stepback_p = context_p->indent_str_p;
/* 4. */
context_p->indent_str_p = ecma_concat_ecma_strings (context_p->indent_str_p, context_p->gap_str_p);
ecma_ref_ecma_string (stepback_p);
context_p->indent_str_p = ecma_concat_ecma_strings (stepback_p, context_p->gap_str_p);
/* 5. */
ecma_collection_header_t *partial_p = ecma_new_values_collection (NULL, 0, true);
@@ -1764,14 +1699,10 @@ ecma_builtin_json_array (ecma_object_t *obj_p, /**< the array object*/
/* 9. */
if (partial_p->unit_number == 0)
{
ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR);
ecma_string_t *right_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR);
lit_utf8_byte_t chars[2] = { LIT_CHAR_LEFT_SQUARE, LIT_CHAR_RIGHT_SQUARE };
ecma_string_t *final_str_p = ecma_concat_ecma_strings (left_square_str_p, right_square_str_p);
ecma_string_t *final_str_p = ecma_new_ecma_string_from_utf8 (chars, 2);
ret_value = ecma_make_string_value (final_str_p);
ecma_deref_ecma_string (left_square_str_p);
ecma_deref_ecma_string (right_square_str_p);
}
/* 10. */
else
@@ -1779,30 +1710,18 @@ ecma_builtin_json_array (ecma_object_t *obj_p, /**< the array object*/
/* 10.a */
if (ecma_string_is_empty (context_p->gap_str_p))
{
ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR);
ecma_string_t *right_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR);
ret_value = ecma_builtin_helper_json_create_non_formatted_json (left_square_str_p,
right_square_str_p,
ret_value = ecma_builtin_helper_json_create_non_formatted_json (LIT_CHAR_LEFT_SQUARE,
LIT_CHAR_RIGHT_SQUARE,
partial_p);
ecma_deref_ecma_string (left_square_str_p);
ecma_deref_ecma_string (right_square_str_p);
}
/* 10.b */
else
{
ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR);
ecma_string_t *right_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR);
ret_value = ecma_builtin_helper_json_create_formatted_json (left_square_str_p,
right_square_str_p,
ret_value = ecma_builtin_helper_json_create_formatted_json (LIT_CHAR_LEFT_SQUARE,
LIT_CHAR_RIGHT_SQUARE,
stepback_p,
partial_p,
context_p);
ecma_deref_ecma_string (left_square_str_p);
ecma_deref_ecma_string (right_square_str_p);
}
}
}
@@ -606,20 +606,10 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
/* We handle infinities separately. */
if (ecma_number_is_infinity (this_num))
{
ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
lit_magic_string_id_t id = (is_negative ? LIT_MAGIC_STRING_NEGATIVE_INFINITY_UL
: LIT_MAGIC_STRING_INFINITY_UL);
if (is_negative)
{
ecma_string_t *neg_str_p = ecma_new_ecma_string_from_utf8 ((const lit_utf8_byte_t *) "-", 1);
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
ecma_deref_ecma_string (infinity_str_p);
ecma_deref_ecma_string (neg_str_p);
ret_value = ecma_make_string_value (neg_inf_str_p);
}
else
{
ret_value = ecma_make_string_value (infinity_str_p);
}
ret_value = ecma_make_string_value (ecma_get_magic_string (id));
}
else
{
@@ -747,20 +737,10 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
/* 6. */
if (ecma_number_is_infinity (this_num))
{
ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
lit_magic_string_id_t id = (is_negative ? LIT_MAGIC_STRING_NEGATIVE_INFINITY_UL
: LIT_MAGIC_STRING_INFINITY_UL);
if (is_negative)
{
ecma_string_t *neg_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_MINUS_CHAR);
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
ecma_deref_ecma_string (infinity_str_p);
ecma_deref_ecma_string (neg_str_p);
ret_value = ecma_make_string_value (neg_inf_str_p);
}
else
{
ret_value = ecma_make_string_value (infinity_str_p);
}
ret_value = ecma_make_string_value (ecma_get_magic_string (id));
}
else
{
@@ -894,20 +874,10 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
/* 7. */
if (ecma_number_is_infinity (this_num))
{
ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
lit_magic_string_id_t id = (is_negative ? LIT_MAGIC_STRING_NEGATIVE_INFINITY_UL
: LIT_MAGIC_STRING_INFINITY_UL);
if (is_negative)
{
ecma_string_t *neg_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_MINUS_CHAR);
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
ecma_deref_ecma_string (infinity_str_p);
ecma_deref_ecma_string (neg_str_p);
ret_value = ecma_make_string_value (neg_inf_str_p);
}
else
{
ret_value = ecma_make_string_value (infinity_str_p);
}
ret_value = ecma_make_string_value (ecma_get_magic_string (id));
}
/* 8. */
else if (arg_num < 1.0 || arg_num >= 22.0)
@@ -22,6 +22,7 @@
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-try-catch-macro.h"
#include "lit-char-helpers.h"
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
#include "ecma-regexp-object.h"
@@ -365,15 +366,15 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume
ecma_value_t source_value = ecma_op_object_get_own_data_prop (obj_p, magic_string_p);
ecma_deref_ecma_string (magic_string_p);
ecma_string_t *src_sep_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SLASH_CHAR);
ecma_string_t *output_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SLASH_CHAR);
ecma_string_t *source_str_p = ecma_get_string_from_value (source_value);
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, source_str_p);
output_str_p = ecma_concat_ecma_strings (output_str_p, source_str_p);
ecma_deref_ecma_string (source_str_p);
ecma_string_t *concat_p = ecma_concat_ecma_strings (output_str_p, src_sep_str_p);
ecma_deref_ecma_string (src_sep_str_p);
ecma_deref_ecma_string (output_str_p);
output_str_p = concat_p;
lit_utf8_byte_t flags[4];
lit_utf8_byte_t *flags_p = flags;
*flags_p++ = LIT_CHAR_SLASH;
/* Check the global flag */
magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_GLOBAL);
@@ -384,11 +385,7 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume
if (ecma_is_value_true (global_value))
{
ecma_string_t *g_flag_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_G_CHAR);
concat_p = ecma_concat_ecma_strings (output_str_p, g_flag_str_p);
ecma_deref_ecma_string (output_str_p);
ecma_deref_ecma_string (g_flag_str_p);
output_str_p = concat_p;
*flags_p++ = LIT_CHAR_LOWERCASE_G;
}
/* Check the ignoreCase flag */
@@ -400,11 +397,7 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume
if (ecma_is_value_true (ignore_case_value))
{
ecma_string_t *ic_flag_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_I_CHAR);
concat_p = ecma_concat_ecma_strings (output_str_p, ic_flag_str_p);
ecma_deref_ecma_string (output_str_p);
ecma_deref_ecma_string (ic_flag_str_p);
output_str_p = concat_p;
*flags_p++ = LIT_CHAR_LOWERCASE_I;
}
/* Check the multiline flag */
@@ -416,13 +409,12 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume
if (ecma_is_value_true (multiline_value))
{
ecma_string_t *m_flag_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_M_CHAR);
concat_p = ecma_concat_ecma_strings (output_str_p, m_flag_str_p);
ecma_deref_ecma_string (output_str_p);
ecma_deref_ecma_string (m_flag_str_p);
output_str_p = concat_p;
*flags_p++ = LIT_CHAR_LOWERCASE_M;
}
lit_utf8_size_t size = (lit_utf8_size_t) (flags_p - flags);
output_str_p = ecma_append_chars_to_string (output_str_p, flags, size, size);
ret_value = ecma_make_string_value (output_str_p);
ECMA_FINALIZE (obj_this);
@@ -236,38 +236,41 @@ ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this ar
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
/* 1 */
ECMA_TRY_CATCH (check_coercible_val,
ecma_op_check_object_coercible (this_arg),
ret_value);
ecma_value_t check_coercible_val = ecma_op_check_object_coercible (this_arg);
if (ECMA_IS_VALUE_ERROR (check_coercible_val))
{
return check_coercible_val;
}
JERRY_ASSERT (ecma_is_value_empty (check_coercible_val));
/* 2 */
ECMA_TRY_CATCH (to_string_val,
ecma_op_to_string (this_arg),
ret_value);
ecma_value_t to_string_val = ecma_op_to_string (this_arg);
if (ECMA_IS_VALUE_ERROR (to_string_val))
{
return to_string_val;
}
/* 3 */
/* No copy performed */
/* 4 */
ecma_string_t *string_to_return = ecma_get_string_from_value (to_string_val);
ecma_ref_ecma_string (string_to_return);
/* 5 */
for (uint32_t arg_index = 0;
arg_index < arguments_number && ecma_is_value_empty (ret_value);
++arg_index)
{
/* 5a */
/* 5b */
ecma_string_t *string_temp = string_to_return;
/* 5a, b */
ECMA_TRY_CATCH (get_arg_string,
ecma_op_to_string (argument_list_p[arg_index]),
ret_value);
string_to_return = ecma_concat_ecma_strings (string_to_return, ecma_get_string_from_value (get_arg_string));
ecma_deref_ecma_string (string_temp);
string_to_return = ecma_concat_ecma_strings (string_to_return,
ecma_get_string_from_value (get_arg_string));
ECMA_FINALIZE (get_arg_string);
}
@@ -282,9 +285,6 @@ ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this ar
ecma_deref_ecma_string (string_to_return);
}
ECMA_FINALIZE (to_string_val);
ECMA_FINALIZE (check_coercible_val);
return ret_value;
} /* ecma_builtin_string_prototype_object_concat */
@@ -620,44 +620,27 @@ typedef struct
/**
* Generic helper function to append a substring at the end of a base string
*
* The base string can be kept or freed
*
* @return the constructed string
*/
static ecma_string_t *
ecma_builtin_string_prototype_object_replace_append_substr (ecma_string_t *base_string_p, /**< base string */
ecma_string_t *appended_string_p, /**< appended string */
ecma_length_t start, /**< start position */
ecma_length_t end, /**< end position */
bool free_base_string) /**< free base string or not */
ecma_length_t end) /**< end position */
{
ecma_string_t *ret_string_p;
JERRY_ASSERT (start <= end);
JERRY_ASSERT (end <= ecma_string_get_length (appended_string_p));
if (start < end)
{
ecma_string_t *substring_p = ecma_string_substr (appended_string_p, start, end);
ret_string_p = ecma_concat_ecma_strings (base_string_p, substring_p);
base_string_p = ecma_concat_ecma_strings (base_string_p, substring_p);
ecma_deref_ecma_string (substring_p);
if (free_base_string)
{
ecma_deref_ecma_string (base_string_p);
}
}
else if (free_base_string)
{
ret_string_p = base_string_p;
}
else
{
ret_string_p = base_string_p;
ecma_ref_ecma_string (ret_string_p);
}
return ret_string_p;
return base_string_p;
} /* ecma_builtin_string_prototype_object_replace_append_substr */
/**
@@ -940,8 +923,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
result_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
context_p->replace_string_p,
previous_start,
current_position,
true);
current_position);
replace_str_curr_p++;
current_position++;
@@ -955,8 +937,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
result_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
input_string_p,
0,
context_p->match_start,
true);
context_p->match_start);
}
else if (action == LIT_CHAR_SINGLE_QUOTE)
{
@@ -964,8 +945,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
result_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
input_string_p,
context_p->match_end,
context_p->input_length,
true);
context_p->input_length);
}
else
{
@@ -1008,9 +988,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
JERRY_ASSERT (ecma_is_value_string (submatch_value));
ecma_string_t *submatch_string_p = ecma_get_string_from_value (submatch_value);
ecma_string_t *appended_string_p = ecma_concat_ecma_strings (result_string_p, submatch_string_p);
ecma_deref_ecma_string (result_string_p);
result_string_p = appended_string_p;
result_string_p = ecma_concat_ecma_strings (result_string_p, submatch_string_p);
}
ECMA_FINALIZE (submatch_value);
@@ -1036,8 +1014,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
result_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
context_p->replace_string_p,
previous_start,
current_position,
true);
current_position);
ret_value = ecma_make_string_value (result_string_p);
}
@@ -1077,8 +1054,7 @@ ecma_builtin_string_prototype_object_replace_loop (ecma_builtin_replace_search_c
result_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
input_string_p,
previous_start,
context_p->match_start,
true);
context_p->match_start);
ECMA_TRY_CATCH (string_value,
ecma_builtin_string_prototype_object_replace_get_string (context_p, match_value),
@@ -1086,11 +1062,8 @@ ecma_builtin_string_prototype_object_replace_loop (ecma_builtin_replace_search_c
JERRY_ASSERT (ecma_is_value_string (string_value));
ecma_string_t *appended_string_p = ecma_concat_ecma_strings (result_string_p,
ecma_get_string_from_value (string_value));
ecma_deref_ecma_string (result_string_p);
result_string_p = appended_string_p;
result_string_p = ecma_concat_ecma_strings (result_string_p,
ecma_get_string_from_value (string_value));
ECMA_FINALIZE (string_value);
@@ -1131,11 +1104,13 @@ ecma_builtin_string_prototype_object_replace_loop (ecma_builtin_replace_search_c
{
/* No more matches */
ecma_string_t *appended_string_p;
ecma_ref_ecma_string (result_string_p);
appended_string_p = ecma_builtin_string_prototype_object_replace_append_substr (result_string_p,
input_string_p,
previous_start,
context_p->input_length,
false);
context_p->input_length);
ret_value = ecma_make_string_value (appended_string_p);
}