Skip to content

Commit 4f06255

Browse files
committed
Modified ecma string to utf8 string conversion to reduce binary size.
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 6fce323 commit 4f06255

File tree

2 files changed

+47
-59
lines changed

2 files changed

+47
-59
lines changed

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,18 +1310,17 @@ ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number i
13101310
* Returns with the cesu8 character array of a string.
13111311
*
13121312
* Note:
1313-
* This function returns with NULL for uint32 strings.
1314-
* The buffer size is rounded up to 8 in this case.
1313+
* This function returns with a newly allocated buffer for uint32 strings,
1314+
* which must be freed.
13151315
*
1316-
* @return NULL - for uint32 strings
1317-
* start of cesu8 characters - otherwise
1316+
* @return start of cesu8 characters
13181317
*/
13191318
const lit_utf8_byte_t *
13201319
ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13211320
lit_utf8_size_t *size_p, /**< [out] size of the ecma string */
1322-
bool *is_ascii_p) /**< [out] true, if the string is an ascii
1323-
* character sequence (size == length)
1324-
* false, otherwise */
1321+
uint8_t *flags_p) /**< [out] flags: 0 - empty,
1322+
1 - is ascii,
1323+
2 - must be freed */
13251324
{
13261325
ecma_length_t length;
13271326
lit_utf8_size_t size;
@@ -1351,8 +1350,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13511350
/* All numbers must be ascii strings. */
13521351
JERRY_ASSERT (ecma_string_get_length (string_p) == size);
13531352

1354-
length = size;
1355-
result_p = NULL;
1353+
result_p = (const lit_utf8_byte_t *) jmem_heap_alloc_block (size);
1354+
length = ecma_uint32_to_utf8_string (string_p->u.uint32_number, (lit_utf8_byte_t *) result_p, size);
1355+
JERRY_ASSERT (length == size);
1356+
*flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED;
13561357
break;
13571358
}
13581359
case ECMA_STRING_CONTAINER_MAGIC_STRING:
@@ -1371,13 +1372,8 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13711372
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX);
13721373

13731374
size = lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id);
1374-
length = 0;
1375-
1376-
if (is_ascii_p != NULL)
1377-
{
1378-
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1379-
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1380-
}
1375+
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1376+
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
13811377

13821378
result_p = lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id);
13831379
break;
@@ -1386,9 +1382,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13861382

13871383
*size_p = size;
13881384

1389-
if (is_ascii_p != NULL)
1385+
if (length == size)
13901386
{
1391-
*is_ascii_p = (length == size);
1387+
*flags_p |= ECMA_STRING_FLAG_IS_ASCII;
13921388
}
13931389
return result_p;
13941390
} /* ecma_string_get_chars */
@@ -1979,32 +1975,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
19791975
JERRY_ASSERT (index < ecma_string_get_length (string_p));
19801976

19811977
lit_utf8_size_t buffer_size;
1982-
bool is_ascii;
1983-
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &is_ascii);
1978+
uint8_t flags = ECMA_STRING_FLAG_EMPTY;
1979+
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &flags);
19841980

1985-
if (chars_p != NULL)
1981+
ecma_char_t ch;
1982+
if (flags & ECMA_STRING_FLAG_IS_ASCII)
19861983
{
1987-
if (is_ascii)
1988-
{
1989-
return chars_p[index];
1990-
}
1991-
1992-
return lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
1984+
ch = chars_p[index];
1985+
}
1986+
else
1987+
{
1988+
ch = lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
19931989
}
19941990

1995-
ecma_char_t ch;
1996-
1997-
JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
1998-
1999-
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
2000-
2001-
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
2002-
/* Uint32 must be an ascii string. */
2003-
JERRY_ASSERT (is_ascii);
2004-
2005-
ch = utf8_str_p[index];
2006-
2007-
JMEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);
1991+
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
1992+
{
1993+
jmem_heap_free_block ((void *) chars_p, buffer_size);
1994+
}
20081995

20091996
return ch;
20101997
} /* ecma_string_get_char_at_pos */

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,33 @@
5050
*/
5151
#define ECMA_SET_POINTER(field, non_compressed_pointer) JMEM_CP_SET_POINTER (field, non_compressed_pointer)
5252

53+
bool ecma_string_to_utf8_string (const ecma_string_t *ecma_str_ptr,
54+
const lit_utf8_byte_t **utf8_ptr,
55+
lit_utf8_size_t *utf8_str_size);
56+
57+
#define ECMA_STRING_FLAG_EMPTY 0u
58+
#define ECMA_STRING_FLAG_IS_ASCII 1u
59+
#define ECMA_STRING_FLAG_MUST_BE_FREED 2u
60+
5361
/**
5462
* Convert ecma-string's contents to a cesu-8 string and put it into a buffer.
5563
*/
5664
#define ECMA_STRING_TO_UTF8_STRING(ecma_str_ptr, /**< ecma string pointer */ \
5765
utf8_ptr, /**< [out] output buffer pointer */ \
5866
utf8_str_size) /**< [out] output buffer size */ \
5967
lit_utf8_size_t utf8_str_size; \
60-
const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, &utf8_str_size, NULL); \
61-
bool utf8_ptr ## must_be_freed = false; \
62-
\
63-
if (utf8_ptr == NULL) \
68+
uint8_t utf8_ptr ## flags = ECMA_STRING_FLAG_EMPTY; \
69+
const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, &utf8_str_size, &utf8_ptr ## flags);
70+
71+
/**
72+
* Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
73+
*/
74+
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
75+
utf8_str_size) /**< buffer size */ \
76+
if (utf8_ptr ## flags & ECMA_STRING_FLAG_MUST_BE_FREED) \
6477
{ \
65-
utf8_ptr = (const lit_utf8_byte_t *) jmem_heap_alloc_block (utf8_str_size); \
66-
ecma_string_to_utf8_bytes (ecma_str_ptr, (lit_utf8_byte_t *) utf8_ptr, utf8_str_size); \
67-
utf8_ptr ## must_be_freed = true; \
78+
JERRY_ASSERT (utf8_ptr != NULL); \
79+
jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
6880
}
6981

7082
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
@@ -121,17 +133,6 @@
121133

122134
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
123135

124-
/**
125-
* Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
126-
*/
127-
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
128-
utf8_str_size) /**< buffer size */ \
129-
if (utf8_ptr ## must_be_freed) \
130-
{ \
131-
JERRY_ASSERT (utf8_ptr != NULL); \
132-
jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
133-
}
134-
135136
/**
136137
* Convert boolean to bitfield value.
137138
*/
@@ -232,7 +233,7 @@ ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
232233
lit_utf8_size_t buffer_size);
233234
void ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p,
234235
lit_utf8_size_t buffer_size);
235-
const lit_utf8_byte_t *ecma_string_get_chars (const ecma_string_t *string_p, lit_utf8_size_t *size_p, bool *is_ascii_p);
236+
const lit_utf8_byte_t *ecma_string_get_chars (const ecma_string_t *string_p, lit_utf8_size_t *size_p, uint8_t *flags_p);
236237
void ecma_init_ecma_string_from_uint32 (ecma_string_t *string_desc_p, uint32_t uint32_number);
237238
void ecma_init_ecma_magic_string (ecma_string_t *string_desc_p, lit_magic_string_id_t id);
238239
bool ecma_compare_ecma_string_to_magic_id (const ecma_string_t *string_p, lit_magic_string_id_t id);

0 commit comments

Comments
 (0)