Skip to content

Modify the usage of ecma_string_to_utf8_string() #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions jerry-core/ecma/base/ecma-helpers-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,8 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_HEAP_ASCII_STRING | ECMA_STRING_REF_ONE;
const size_t data_size = new_size;
lit_utf8_byte_t *data_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (data_size);
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, data_p, str1_size);
JERRY_ASSERT (bytes_copied == str1_size);
bytes_copied = ecma_string_to_utf8_string (string2_p, data_p + str1_size, str2_size);
JERRY_ASSERT (bytes_copied == str2_size);
ecma_string_to_utf8_bytes (string1_p, data_p, str1_size);
ecma_string_to_utf8_bytes (string2_p, data_p + str1_size, str2_size);

string_desc_p->u.ascii_string.size = (uint16_t) new_size;

Expand All @@ -411,15 +409,14 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_HEAP_UTF8_STRING | ECMA_STRING_REF_ONE;
const size_t data_size = new_size + sizeof (ecma_string_heap_header_t);
ecma_string_heap_header_t *data_p = (ecma_string_heap_header_t *) jmem_heap_alloc_block (data_size);
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p,
(lit_utf8_byte_t *) (data_p + 1),
str1_size);
JERRY_ASSERT (bytes_copied == str1_size);

bytes_copied = ecma_string_to_utf8_string (string2_p,
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
str2_size);
JERRY_ASSERT (bytes_copied == str2_size);
ecma_string_to_utf8_bytes (string1_p,
(lit_utf8_byte_t *) (data_p + 1),
str1_size);

ecma_string_to_utf8_bytes (string2_p,
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
str2_size);

JERRY_ASSERT (string_length <= UINT16_MAX);

data_p->size = (uint16_t) new_size;
Expand Down Expand Up @@ -675,10 +672,10 @@ ecma_string_get_array_index (const ecma_string_t *str_p, /**< ecma-string */
* @return number of bytes, actually copied to the buffer.
*/
lit_utf8_size_t __attr_return_value_should_be_checked___
ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
JERRY_ASSERT (string_desc_p != NULL);
JERRY_ASSERT (string_desc_p->refs_and_container >= ECMA_STRING_REF_ONE);
Expand Down Expand Up @@ -741,7 +738,22 @@ ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string

JERRY_ASSERT (size <= buffer_size);
return size;
} /* ecma_string_to_utf8_string */
} /* ecma_string_copy_to_utf8_buffer */

/**
* Convert ecma-string's contents to a cesu-8 string and put it to the buffer.
* It is the caller's responsibility to make sure that the string fits in the buffer.
* Check if the size of the string is equal with the size of the buffer.
*/
void __attr_always_inline___
ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
const lit_utf8_size_t size = ecma_string_copy_to_utf8_buffer (string_desc_p, buffer_p, buffer_size);
JERRY_ASSERT (size == buffer_size);
} /* ecma_string_to_utf8_bytes */

/**
* Lengths for numeric string values
Expand Down Expand Up @@ -1018,8 +1030,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
{
utf8_string1_p = (lit_utf8_byte_t *) jmem_heap_alloc_block ((size_t) strings_size);

lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, utf8_string1_p, strings_size);
JERRY_ASSERT (bytes_copied == strings_size);
ecma_string_to_utf8_bytes (string1_p, utf8_string1_p, strings_size);

is_utf8_string1_on_heap = true;
}
Expand Down Expand Up @@ -1049,11 +1060,11 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
{
utf8_string2_p = (lit_utf8_byte_t *) jmem_heap_alloc_block ((size_t) strings_size);

lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string2_p, utf8_string2_p, strings_size);
JERRY_ASSERT (bytes_copied == strings_size);
ecma_string_to_utf8_bytes (string2_p, utf8_string2_p, strings_size);

is_utf8_string2_on_heap = true;
}

const bool is_equal = !strncmp ((char *) utf8_string1_p, (char *) utf8_string2_p, (size_t) strings_size);

if (is_utf8_string1_on_heap)
Expand Down Expand Up @@ -1160,8 +1171,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
utf8_string1_p = utf8_string1_buffer;
}

lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, utf8_string1_p, utf8_string1_size);
JERRY_ASSERT (bytes_copied == utf8_string1_size);
ecma_string_to_utf8_bytes (string1_p, utf8_string1_p, utf8_string1_size);
}

if (ECMA_STRING_GET_CONTAINER (string2_p) == ECMA_STRING_CONTAINER_HEAP_UTF8_STRING)
Expand Down Expand Up @@ -1202,8 +1212,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
utf8_string2_p = utf8_string2_buffer;
}

lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string2_p, utf8_string2_p, utf8_string2_size);
JERRY_ASSERT (bytes_copied == utf8_string2_size);
ecma_string_to_utf8_bytes (string2_p, utf8_string2_p, utf8_string2_size);
}

bool is_first_less_than_second = lit_compare_utf8_strings_relational (utf8_string1_p,
Expand Down Expand Up @@ -1345,8 +1354,7 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */

JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);

lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, buffer_size);
JERRY_ASSERT (sz == buffer_size);
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);

JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
/* Both above must be ascii strings. */
Expand Down Expand Up @@ -1504,8 +1512,7 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin

JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);

lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, buffer_size);
JERRY_ASSERT (sz == buffer_size);
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);

/**
* II. Extract substring
Expand Down
6 changes: 3 additions & 3 deletions jerry-core/ecma/base/ecma-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
if (utf8_ptr == NULL) \
{ \
utf8_ptr = (const lit_utf8_byte_t *) jmem_heap_alloc_block (utf8_str_size); \
lit_utf8_size_t sz = ecma_string_to_utf8_string (ecma_str_ptr, (lit_utf8_byte_t *) utf8_ptr, utf8_str_size); \
JERRY_ASSERT (sz == utf8_str_size); \
ecma_string_to_utf8_bytes (ecma_str_ptr, (lit_utf8_byte_t *) utf8_ptr, utf8_str_size); \
utf8_ptr ## must_be_freed = true; \
}

Expand Down Expand Up @@ -177,7 +176,8 @@ extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);

extern lit_utf8_size_t __attr_return_value_should_be_checked___
ecma_string_to_utf8_string (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
ecma_string_copy_to_utf8_buffer (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
extern void ecma_string_to_utf8_bytes (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
extern const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *, lit_utf8_size_t *, bool *);

extern bool ecma_compare_ecma_strings_equal_hashes (const ecma_string_t *, const ecma_string_t *);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
JMEM_DEFINE_LOCAL_ARRAY (ret_str_buffer, size, lit_utf8_byte_t);
lit_utf8_byte_t *ret_str_buffer_p = ret_str_buffer;

lit_utf8_size_t bytes = ecma_string_to_utf8_string (name_string_p, ret_str_buffer_p, name_size);
lit_utf8_size_t bytes = ecma_string_copy_to_utf8_buffer (name_string_p, ret_str_buffer_p, name_size);
JERRY_ASSERT (bytes == name_size);
ret_str_buffer_p = ret_str_buffer_p + bytes;
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
Expand All @@ -157,7 +157,7 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
space_size);
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);

bytes = ecma_string_to_utf8_string (msg_string_p, ret_str_buffer_p, msg_size);
bytes = ecma_string_copy_to_utf8_buffer (msg_string_p, ret_str_buffer_p, msg_size);
JERRY_ASSERT (bytes == msg_size);
ret_str_buffer_p = ret_str_buffer_p + bytes;
JERRY_ASSERT (ret_str_buffer_p == ret_str_buffer + size);
Expand Down
21 changes: 5 additions & 16 deletions jerry-core/ecma/builtin-objects/ecma-builtin-global.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
utf8_str_size,
lit_utf8_byte_t);

lit_utf8_size_t actual_sz = ecma_string_to_utf8_string (str_p, utf8_str_p, utf8_str_size);
JERRY_ASSERT (actual_sz == utf8_str_size);
ecma_string_to_utf8_bytes (str_p, utf8_str_p, utf8_str_size);

const lit_utf8_byte_t *utf8_str_curr_p = utf8_str_p;
const lit_utf8_byte_t *utf8_str_end_p = utf8_str_p + utf8_str_size;
Expand Down Expand Up @@ -739,10 +738,7 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___,
input_size + 1,
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_bytes (input_string_p, input_start_p, input_size);

input_start_p[input_size] = LIT_BYTE_NULL;

Expand Down Expand Up @@ -1017,10 +1013,7 @@ ecma_builtin_global_object_encode_uri_helper (ecma_value_t uri, /**< uri argumen
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_bytes (input_string_p, input_start_p, input_size);

/*
* The URI encoding has two major phases: first we validate the input,
Expand Down Expand Up @@ -1238,10 +1231,7 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
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_bytes (input_string_p, input_start_p, input_size);

/*
* The escape routine has two major phases: first we compute
Expand Down Expand Up @@ -1358,8 +1348,7 @@ ecma_builtin_global_object_unescape (ecma_value_t this_arg __attr_unused___, /**

/* 3. */
JMEM_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_bytes (input_string_p, input_start_p, input_size);

const lit_utf8_byte_t *input_curr_p = input_start_p;
const lit_utf8_byte_t *input_end_p = input_start_p + input_size;
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/ecma/builtin-objects/ecma-builtin-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ ecma_builtin_json_parse (ecma_value_t this_arg __attr_unused___, /**< 'this' arg

JMEM_DEFINE_LOCAL_ARRAY (str_start_p, buffer_size, lit_utf8_byte_t);

const lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, str_start_p, buffer_size);
const lit_utf8_size_t sz = ecma_string_copy_to_utf8_buffer (string_p, str_start_p, buffer_size);
JERRY_ASSERT (sz == string_size);

str_start_p[string_size] = LIT_BYTE_NULL;
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ jerry_string_to_char_buffer (const jerry_string_t *string_p, /**< string descrip
{
jerry_assert_api_available ();

return ecma_string_to_utf8_string (string_p, (lit_utf8_byte_t *) buffer_p, buffer_size);
return ecma_string_copy_to_utf8_buffer (string_p, (lit_utf8_byte_t *) buffer_p, buffer_size);
} /* jerry_string_to_char_buffer */

/**
Expand Down