Skip to content

Commit e1357b4

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 d990832 commit e1357b4

File tree

2 files changed

+67
-69
lines changed

2 files changed

+67
-69
lines changed

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

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,12 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
11061106
}
11071107
}
11081108

1109-
bool is_ascii;
1110-
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &size, &is_ascii);
1109+
uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
1110+
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &size, &flags);
11111111

11121112
JERRY_ASSERT (chars_p != NULL);
11131113

1114-
if (is_ascii)
1114+
if (flags & ECMA_STRING_FLAG_IS_ASCII)
11151115
{
11161116
JERRY_ASSERT (size <= buffer_size);
11171117
memcpy (buffer_p, chars_p, size);
@@ -1123,6 +1123,11 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
11231123
buffer_p,
11241124
buffer_size);
11251125

1126+
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
1127+
{
1128+
jmem_heap_free_block ((void *) chars_p, size);
1129+
}
1130+
11261131
JERRY_ASSERT (size <= buffer_size);
11271132
return size;
11281133
} /* ecma_string_copy_to_utf8_buffer */
@@ -1392,18 +1397,19 @@ ecma_string_get_uint32_size (const uint32_t uint32_number) /**< number in the st
13921397
* Returns with the cesu8 character array of a string.
13931398
*
13941399
* Note:
1395-
* This function returns with NULL for uint32 strings.
1396-
* The buffer size is rounded up to 8 in this case.
1400+
* - This function returns with a newly allocated buffer for uint32 strings,
1401+
* which must be freed.
1402+
* - The ASCII check only happens if the flags parameter gets
1403+
* 'ECMA_STRING_FLAG_IS_ASCII' as an input.
13971404
*
1398-
* @return NULL - for uint32 strings
1399-
* start of cesu8 characters - otherwise
1405+
* @return start of cesu8 characters
14001406
*/
14011407
const lit_utf8_byte_t *
14021408
ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14031409
lit_utf8_size_t *size_p, /**< [out] size of the ecma string */
1404-
bool *is_ascii_p) /**< [out] true, if the string is an ascii
1405-
* character sequence (size == length)
1406-
* false, otherwise */
1410+
uint8_t *flags_p) /**< [in,out] flags: ECMA_STRING_FLAG_EMPTY,
1411+
ECMA_STRING_FLAG_IS_ASCII,
1412+
ECMA_STRING_FLAG_MUST_BE_FREED */
14071413
{
14081414
ecma_length_t length;
14091415
lit_utf8_size_t size;
@@ -1431,11 +1437,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14311437
uint32_t uint32_number = (uint32_t) ECMA_GET_DIRECT_STRING_VALUE (string_p);
14321438
size = (lit_utf8_size_t) ecma_string_get_uint32_size (uint32_number);
14331439

1434-
/* All numbers must be ascii strings. */
1435-
JERRY_ASSERT (ecma_string_get_length (string_p) == size);
1436-
1437-
length = size;
1438-
result_p = NULL;
1440+
result_p = (const lit_utf8_byte_t *) jmem_heap_alloc_block (size);
1441+
length = ecma_uint32_to_utf8_string (uint32_number, (lit_utf8_byte_t *) result_p, size);
1442+
JERRY_ASSERT (length == size);
1443+
*flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED;
14391444
break;
14401445
}
14411446
default:
@@ -1447,10 +1452,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14471452
size = lit_get_magic_string_ex_size (id);
14481453
length = 0;
14491454

1450-
if (unlikely (is_ascii_p != NULL))
1455+
if (unlikely (*flags_p & ECMA_STRING_FLAG_IS_ASCII))
14511456
{
1452-
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1453-
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1457+
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id), size);
14541458
}
14551459

14561460
result_p = lit_get_magic_string_ex_utf8 (id);
@@ -1483,12 +1487,12 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14831487
{
14841488
size = (lit_utf8_size_t) ecma_string_get_uint32_size (string_p->u.uint32_number);
14851489

1486-
/* All numbers must be ascii strings. */
1487-
JERRY_ASSERT (ecma_string_get_length (string_p) == size);
1488-
1489-
length = size;
1490-
result_p = NULL;
1490+
result_p = (const lit_utf8_byte_t *) jmem_heap_alloc_block (size);
1491+
length = ecma_uint32_to_utf8_string (string_p->u.uint32_number, (lit_utf8_byte_t *) result_p, size);
1492+
JERRY_ASSERT (length == size);
1493+
*flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED;
14911494
break;
1495+
14921496
}
14931497
default:
14941498
{
@@ -1497,10 +1501,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14971501
size = lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id);
14981502
length = 0;
14991503

1500-
if (unlikely (is_ascii_p != NULL))
1504+
if (unlikely (*flags_p & ECMA_STRING_FLAG_IS_ASCII))
15011505
{
1502-
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1503-
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1506+
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id), size);
15041507
}
15051508

15061509
result_p = lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id);
@@ -1510,11 +1513,14 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
15101513
}
15111514

15121515
*size_p = size;
1513-
1514-
if (is_ascii_p != NULL)
1516+
if (*flags_p & ECMA_STRING_FLAG_IS_ASCII)
15151517
{
1516-
*is_ascii_p = (length == size);
1518+
if (length != size)
1519+
{
1520+
*flags_p = (uint8_t) (*flags_p & ~ECMA_STRING_FLAG_IS_ASCII);
1521+
}
15171522
}
1523+
15181524
return result_p;
15191525
} /* ecma_string_get_chars */
15201526

@@ -2126,31 +2132,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
21262132
JERRY_ASSERT (index < ecma_string_get_length (string_p));
21272133

21282134
lit_utf8_size_t buffer_size;
2129-
bool is_ascii;
2130-
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &is_ascii);
2135+
uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
2136+
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &flags);
21312137

2132-
if (chars_p != NULL)
2138+
ecma_char_t ch;
2139+
if (flags & ECMA_STRING_FLAG_IS_ASCII)
21332140
{
2134-
if (is_ascii)
2135-
{
2136-
return chars_p[index];
2137-
}
2138-
2139-
return lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
2141+
ch = chars_p[index];
2142+
}
2143+
else
2144+
{
2145+
ch = lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
21402146
}
21412147

2142-
ecma_char_t ch;
2143-
2144-
JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
2145-
2146-
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
2147-
2148-
/* Uint32 must be an ascii string. */
2149-
JERRY_ASSERT (is_ascii);
2150-
2151-
ch = utf8_str_p[index];
2152-
2153-
JMEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);
2148+
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
2149+
{
2150+
jmem_heap_free_block ((void *) chars_p, buffer_size);
2151+
}
21542152

21552153
return ch;
21562154
} /* ecma_string_get_char_at_pos */

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

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

53+
typedef enum
54+
{
55+
ECMA_STRING_FLAG_EMPTY = 0,
56+
ECMA_STRING_FLAG_IS_ASCII,
57+
ECMA_STRING_FLAG_MUST_BE_FREED
58+
} ecma_string_flag_t;
59+
5360
/**
5461
* Convert ecma-string's contents to a cesu-8 string and put it into a buffer.
5562
*/
5663
#define ECMA_STRING_TO_UTF8_STRING(ecma_str_ptr, /**< ecma string pointer */ \
5764
utf8_ptr, /**< [out] output buffer pointer */ \
5865
utf8_str_size) /**< [out] output buffer size */ \
5966
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) \
67+
uint8_t utf8_ptr ## flags = ECMA_STRING_FLAG_EMPTY; \
68+
const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, &utf8_str_size, &utf8_ptr ## flags);
69+
70+
/**
71+
* Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
72+
*/
73+
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
74+
utf8_str_size) /**< buffer size */ \
75+
if (utf8_ptr ## flags & ECMA_STRING_FLAG_MUST_BE_FREED) \
6476
{ \
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; \
77+
JERRY_ASSERT (utf8_ptr != NULL); \
78+
jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
6879
}
6980

7081
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
@@ -121,17 +132,6 @@
121132

122133
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
123134

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-
135135
/**
136136
* Convert boolean to bitfield value.
137137
*/
@@ -232,7 +232,7 @@ ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
232232
lit_utf8_size_t buffer_size);
233233
void ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p,
234234
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);
235+
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);
236236
bool ecma_compare_ecma_string_to_magic_id (const ecma_string_t *string_p, lit_magic_string_id_t id);
237237
bool ecma_string_is_empty (const ecma_string_t *string_p);
238238
bool ecma_string_is_length (const ecma_string_t *string_p);

0 commit comments

Comments
 (0)