Introduce ECMA_STRING_TO_UTF8_STRING and ECMA_FINALIZE_UTF8_STRING

Benefits:
 * Better readability and maintenance
 * Better heap consumption on 'date-format-xparb.js' test of SunSpider

JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
This commit is contained in:
László Langó
2016-03-29 09:23:23 +02:00
parent 9ab0998ae6
commit 9dad7dbfba
13 changed files with 136 additions and 195 deletions
@@ -205,14 +205,10 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
ecma_string_t *date_str_p = ecma_get_string_from_value (date_str_value);
lit_utf8_size_t date_str_size = ecma_string_get_size (date_str_p);
MEM_DEFINE_LOCAL_ARRAY (date_start_p, date_str_size, lit_utf8_byte_t);
ECMA_STRING_TO_UTF8_STRING (date_str_p, date_start_p, date_start_size);
lit_utf8_size_t sz = ecma_string_to_utf8_string (date_str_p, date_start_p, date_str_size);
JERRY_ASSERT (sz == date_str_size);
lit_utf8_byte_t *date_str_curr_p = date_start_p;
const lit_utf8_byte_t *date_str_end_p = date_start_p + date_str_size;
lit_utf8_byte_t *date_str_curr_p = (lit_utf8_byte_t *) date_start_p;
const lit_utf8_byte_t *date_str_end_p = date_start_p + date_start_size;
/* 1. read year */
ecma_number_t year = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 4);
@@ -396,7 +392,7 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
ret_value = ecma_make_number_value (date_num_p);
MEM_FINALIZE_LOCAL_ARRAY (date_start_p);
ECMA_FINALIZE_UTF8_STRING (date_start_p, date_start_size);
ECMA_FINALIZE (date_str_value);
return ret_value;
@@ -203,23 +203,17 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
ECMA_TRY_CATCH (string_var, ecma_op_to_string (string), ret_value);
ecma_string_t *number_str_p = ecma_get_string_from_value (string_var);
lit_utf8_size_t str_size = ecma_string_get_size (number_str_p);
ECMA_STRING_TO_UTF8_STRING (number_str_p, string_buff, string_buff_size);
if (str_size > 0)
if (string_buff_size > 0)
{
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
string_buff,
str_size);
JERRY_ASSERT (bytes_copied == str_size);
lit_utf8_byte_t *string_curr_p = string_buff;
lit_utf8_byte_t *string_end_p = string_buff + str_size;
lit_utf8_byte_t *string_curr_p = (lit_utf8_byte_t *) string_buff;
const lit_utf8_byte_t *string_end_p = string_buff + string_buff_size;
/* 2. Remove leading whitespace. */
lit_utf8_byte_t *start_p = string_end_p;
lit_utf8_byte_t *end_p = string_end_p;
lit_utf8_byte_t *start_p = (lit_utf8_byte_t *) string_end_p;
lit_utf8_byte_t *end_p = start_p;
while (string_curr_p < string_end_p)
{
@@ -396,7 +390,6 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
ret_value = ecma_make_number_value (ret_num_p);
}
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
}
else
{
@@ -405,6 +398,7 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
ret_value = ecma_make_number_value (ret_num_p);
}
ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
ECMA_FINALIZE (string_var);
return ret_value;
} /* ecma_builtin_global_object_parse_int */
@@ -428,22 +422,15 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
ECMA_TRY_CATCH (string_var, ecma_op_to_string (string), ret_value);
ecma_string_t *number_str_p = ecma_get_string_from_value (string_var);
lit_utf8_size_t str_size = ecma_string_get_size (number_str_p);
ECMA_STRING_TO_UTF8_STRING (number_str_p, string_buff, string_buff_size);
if (str_size > 0)
if (string_buff_size > 0)
{
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
lit_utf8_byte_t *str_curr_p = (lit_utf8_byte_t *) string_buff;
const lit_utf8_byte_t *str_end_p = string_buff + string_buff_size;
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
string_buff,
str_size);
JERRY_ASSERT (bytes_copied == str_size);
lit_utf8_byte_t *str_curr_p = string_buff;
lit_utf8_byte_t *str_end_p = string_buff + str_size;
lit_utf8_byte_t *start_p = str_end_p;
lit_utf8_byte_t *end_p = str_end_p;
lit_utf8_byte_t *start_p = (lit_utf8_byte_t *) str_end_p;
lit_utf8_byte_t *end_p = (lit_utf8_byte_t *) str_end_p;
/* 2. Find first non whitespace char and set starting position. */
while (str_curr_p < str_end_p)
@@ -626,7 +613,6 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_number_value (ret_num_p);
}
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
}
/* String length is zero. */
else
@@ -636,6 +622,7 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
ret_value = ecma_make_number_value (ret_num_p);
}
ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
ECMA_FINALIZE (string_var);
return ret_value;
@@ -565,12 +565,8 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
ecma_length_t *ret_index_p) /**< position found in original string */
{
bool match_found = false;
const ecma_length_t original_len = ecma_string_get_length (original_str_p);
const lit_utf8_size_t original_size = ecma_string_get_size (original_str_p);
const ecma_length_t search_len = ecma_string_get_length (search_str_p);
const lit_utf8_size_t search_size = ecma_string_get_size (search_str_p);
if (search_len <= original_len)
{
@@ -582,34 +578,20 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
else
{
/* create utf8 string from original string and advance to position */
MEM_DEFINE_LOCAL_ARRAY (original_str_utf8_p,
original_size,
lit_utf8_byte_t);
lit_utf8_size_t sz = ecma_string_to_utf8_string (original_str_p,
original_str_utf8_p,
original_size);
JERRY_ASSERT (sz == original_size);
ECMA_STRING_TO_UTF8_STRING (original_str_p, original_str_utf8_p, original_str_size);
ecma_length_t index = start_pos;
lit_utf8_byte_t *original_str_curr_p = original_str_utf8_p;
lit_utf8_byte_t *original_str_curr_p = (lit_utf8_byte_t *) original_str_utf8_p;
for (ecma_length_t idx = 0; idx < index; idx++)
{
lit_utf8_incr (&original_str_curr_p);
}
/* create utf8 string from search string */
MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p,
search_size,
lit_utf8_byte_t);
ECMA_STRING_TO_UTF8_STRING (search_str_p, search_str_utf8_p, search_str_size);
lit_utf8_size_t sz = ecma_string_to_utf8_string (search_str_p,
search_str_utf8_p,
search_size);
JERRY_ASSERT (sz == search_size);
lit_utf8_byte_t *search_str_curr_p = search_str_utf8_p;
lit_utf8_byte_t *search_str_curr_p = (lit_utf8_byte_t *) search_str_utf8_p;
/* iterate original string and try to match at each position */
bool searching = true;
@@ -667,8 +649,8 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
}
}
MEM_FINALIZE_LOCAL_ARRAY (search_str_utf8_p);
MEM_FINALIZE_LOCAL_ARRAY (original_str_utf8_p);
ECMA_FINALIZE_UTF8_STRING (search_str_utf8_p, search_str_size);
ECMA_FINALIZE_UTF8_STRING (original_str_utf8_p, original_str_size);
}
}
@@ -76,12 +76,12 @@ typedef struct
{
ecma_json_token_type_t type; /**< type of the current token */
lit_utf8_byte_t *current_p; /**< current position of the string processed by the parser */
lit_utf8_byte_t *end_p; /**< end of the string processed by the parser */
const lit_utf8_byte_t *end_p; /**< end of the string processed by the parser */
union
{
struct
{
lit_utf8_byte_t *start_p; /**< when type is string_token, it contains the start of the string */
const lit_utf8_byte_t *start_p; /**< when type is string_token, it contains the start of the string */
lit_utf8_size_t size; /**< when type is string_token, it contains the size of the string */
} string;
ecma_number_t number; /**< when type is number_token, it contains the value of the number */
@@ -286,11 +286,11 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
lit_utf8_byte_t *current_p = token_p->current_p;
token_p->type = invalid_token;
/*
* No need for end check since the string is zero terminated.
*/
while (*current_p == LIT_CHAR_SP || *current_p == LIT_CHAR_CR
|| *current_p == LIT_CHAR_LF || *current_p == LIT_CHAR_TAB)
while (current_p < token_p->end_p
&& (*current_p == LIT_CHAR_SP
|| *current_p == LIT_CHAR_CR
|| *current_p == LIT_CHAR_LF
|| *current_p == LIT_CHAR_TAB))
{
current_p++;
}
@@ -500,7 +500,7 @@ ecma_builtin_json_parse_value (ecma_json_token_t *token_p) /**< token argument *
break;
}
lit_utf8_byte_t *string_start_p = token_p->u.string.start_p;
const lit_utf8_byte_t *string_start_p = token_p->u.string.start_p;
lit_utf8_size_t string_size = token_p->u.string.size;
ecma_builtin_json_parse_next_token (token_p);
@@ -709,19 +709,12 @@ ecma_builtin_json_parse (ecma_value_t this_arg __attr_unused___, /**< 'this' arg
ret_value);
ecma_string_t *string_p = ecma_get_string_from_value (string);
ecma_length_t string_size = (uint32_t) ecma_string_get_size (string_p);
lit_utf8_size_t buffer_size = sizeof (lit_utf8_byte_t) * (string_size + 1);
MEM_DEFINE_LOCAL_ARRAY (str_start_p, buffer_size, lit_utf8_byte_t);
lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, str_start_p, buffer_size);
JERRY_ASSERT (sz == string_size);
str_start_p[string_size] = LIT_BYTE_NULL;
ECMA_STRING_TO_UTF8_STRING (string_p, str_start_p, str_start_size);
ecma_json_token_t token;
token.current_p = str_start_p;
token.end_p = str_start_p + string_size;
token.current_p = (lit_utf8_byte_t *) str_start_p;
token.end_p = str_start_p + str_start_size;
ecma_value_t final_result = ecma_builtin_json_parse_value (&token);
@@ -767,7 +760,7 @@ ecma_builtin_json_parse (ecma_value_t this_arg __attr_unused___, /**< 'this' arg
}
}
MEM_FINALIZE_LOCAL_ARRAY (str_start_p);
ECMA_FINALIZE_UTF8_STRING (str_start_p, str_start_size);
ECMA_FINALIZE (string);
return ret_value;
@@ -1064,18 +1057,10 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
ecma_string_t *product_str_p = ecma_copy_or_ref_ecma_string (quote_str_p);
ecma_string_t *tmp_str_p;
ecma_length_t string_size = ecma_string_get_size (string_p);
ECMA_STRING_TO_UTF8_STRING (string_p, string_buff, string_buff_size);
MEM_DEFINE_LOCAL_ARRAY (string_buff, string_size, lit_utf8_byte_t);
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string_p,
string_buff,
string_size);
JERRY_ASSERT (bytes_copied == string_size);
lit_utf8_byte_t *str_p = string_buff;
const lit_utf8_byte_t *str_end_p = str_p + string_size;
lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) string_buff;
const lit_utf8_byte_t *str_end_p = string_buff + string_buff_size;
while (str_p < str_end_p)
{
@@ -1196,7 +1181,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
}
}
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
/* 3. */
tmp_str_p = ecma_concat_ecma_strings (product_str_p, quote_str_p);
@@ -1198,23 +1198,15 @@ ecma_builtin_string_prototype_object_replace_main (ecma_builtin_replace_search_c
ret_value);
ecma_string_t *replace_string_p = ecma_get_string_from_value (to_string_replace_val);
lit_utf8_size_t replace_size = ecma_string_get_size (replace_string_p);
MEM_DEFINE_LOCAL_ARRAY (replace_start_p,
replace_size,
lit_utf8_byte_t);
lit_utf8_size_t sz = ecma_string_to_utf8_string (replace_string_p,
replace_start_p,
replace_size);
JERRY_ASSERT (sz == replace_size);
ECMA_STRING_TO_UTF8_STRING (replace_string_p, replace_start_p, replace_start_size);
context_p->replace_string_p = replace_string_p;
context_p->replace_str_curr_p = replace_start_p;
context_p->replace_str_curr_p = (lit_utf8_byte_t *) replace_start_p;
ret_value = ecma_builtin_string_prototype_object_replace_loop (context_p);
MEM_FINALIZE_LOCAL_ARRAY (replace_start_p);
ECMA_FINALIZE_UTF8_STRING (replace_start_p, replace_start_size);
ECMA_FINALIZE (to_string_replace_val);
}
@@ -2073,16 +2065,8 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
/* 3. */
ecma_string_t *input_string_p = ecma_get_string_from_value (to_string_val);
lit_utf8_size_t input_size = ecma_string_get_size (input_string_p);
MEM_DEFINE_LOCAL_ARRAY (input_start_p,
input_size,
lit_utf8_byte_t);
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
input_start_p,
input_size);
JERRY_ASSERT (sz == input_size);
ECMA_STRING_TO_UTF8_STRING (input_string_p, input_start_p, input_start_size);
/*
* The URI encoding has two major phases: first we compute
@@ -2090,8 +2074,8 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
*/
lit_utf8_size_t output_length = 0;
lit_utf8_byte_t *input_str_curr_p = input_start_p;
const lit_utf8_byte_t *input_str_end_p = input_start_p + input_size;
lit_utf8_byte_t *input_str_curr_p = (lit_utf8_byte_t *) input_start_p;
const lit_utf8_byte_t *input_str_end_p = input_start_p + input_start_size;
while (input_str_curr_p < input_str_end_p)
{
@@ -2130,7 +2114,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
lit_utf8_byte_t *output_char_p = output_start_p;
/* Encoding the output. */
input_str_curr_p = input_start_p;
input_str_curr_p = (lit_utf8_byte_t *) input_start_p;
while (input_str_curr_p < input_str_end_p)
{
@@ -2166,7 +2150,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
ret_value = ecma_make_string_value (output_string_p);
MEM_FINALIZE_LOCAL_ARRAY (output_start_p);
MEM_FINALIZE_LOCAL_ARRAY (input_start_p);
ECMA_FINALIZE_UTF8_STRING (input_start_p, input_start_size);
ECMA_FINALIZE (to_string_val);
ECMA_FINALIZE (check_coercible_val);