Skip to content

Commit fbcda55

Browse files
committed
Move 'nums_with_ascending_length' to be a global, non variable length array.
VLAs cannot be initialized by any form of initialization syntax with C99 standard. Also did some refactor around 'ecma_string_get_length' and 'ecma_string_get_size'. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 0219f37 commit fbcda55

File tree

1 file changed

+90
-90
lines changed

1 file changed

+90
-90
lines changed

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

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,63 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12851285
return is_first_less_than_second;
12861286
} /* ecma_compare_ecma_strings_relational */
12871287

1288+
/**
1289+
* Lengths for numeric string values
1290+
*/
1291+
static const uint32_t nums_with_ascending_length[] =
1292+
{
1293+
1u,
1294+
10u,
1295+
100u,
1296+
1000u,
1297+
10000u,
1298+
100000u,
1299+
1000000u,
1300+
10000000u,
1301+
100000000u,
1302+
1000000000u
1303+
};
1304+
1305+
/**
1306+
* Maximum length of numeric strings
1307+
*/
1308+
static const uint32_t max_uint32_len = sizeof (nums_with_ascending_length) / sizeof (uint32_t);
1309+
1310+
/**
1311+
* Get size of the number stored locally in the string's descriptor
1312+
*
1313+
* Note: the represented number size and length are equal
1314+
*
1315+
* @return size in bytes
1316+
*/
1317+
static ecma_length_t __attr_always_inline___
1318+
ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number in the string-descriptor */
1319+
{
1320+
ecma_length_t size = 1;
1321+
1322+
while (size < max_uint32_len && uint32_number >= nums_with_ascending_length[size])
1323+
{
1324+
size++;
1325+
}
1326+
return size;
1327+
} /* ecma_string_get_number_in_desc_size */
1328+
1329+
/**
1330+
* Get size of container heap number of ecma-string
1331+
*
1332+
* Note: the number size and length are equal
1333+
*
1334+
* @return number of bytes in the buffer
1335+
*/
1336+
static lit_utf8_size_t __attr_always_inline___
1337+
ecma_string_get_heap_number_size (mem_cpointer_t number_cp) /**< Compressed pointer to an ecma_number_t */
1338+
{
1339+
const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t, number_cp);
1340+
lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1341+
1342+
return ecma_number_to_utf8_string (*num_p, buffer, sizeof (buffer));
1343+
} /* ecma_string_get_heap_number_size */
1344+
12881345
/**
12891346
* Get length of ecma-string
12901347
*
@@ -1293,75 +1350,46 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12931350
ecma_length_t
12941351
ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */
12951352
{
1296-
ecma_string_container_t container = (ecma_string_container_t) string_p->container;
1297-
1298-
if (container == ECMA_STRING_CONTAINER_LIT_TABLE)
1299-
{
1300-
lit_literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp);
1301-
JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit));
1302-
return lit_charset_literal_get_length (lit);
1303-
}
1304-
else if (container == ECMA_STRING_CONTAINER_MAGIC_STRING)
1305-
{
1306-
TODO ("Cache magic string lengths")
1307-
return lit_utf8_string_length (lit_get_magic_string_utf8 (string_p->u.magic_string_id),
1308-
lit_get_magic_string_size (string_p->u.magic_string_id));
1309-
}
1310-
else if (container == ECMA_STRING_CONTAINER_MAGIC_STRING_EX)
1311-
{
1312-
TODO ("Cache magic string lengths")
1313-
return lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1314-
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1315-
}
1316-
else if (container == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
1353+
switch ((ecma_string_container_t) string_p->container)
13171354
{
1318-
const uint32_t uint32_number = string_p->u.uint32_number;
1319-
const uint32_t max_uint32_len = 10;
1320-
const uint32_t nums_with_ascending_length[10] =
1355+
case ECMA_STRING_CONTAINER_LIT_TABLE:
13211356
{
1322-
1u,
1323-
10u,
1324-
100u,
1325-
1000u,
1326-
10000u,
1327-
100000u,
1328-
1000000u,
1329-
10000000u,
1330-
100000000u,
1331-
1000000000u
1332-
};
1333-
1334-
ecma_length_t length = 1;
1335-
1336-
while (length < max_uint32_len
1337-
&& uint32_number >= nums_with_ascending_length[length])
1357+
lit_literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp);
1358+
JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit));
1359+
return lit_charset_literal_get_length (lit);
1360+
}
1361+
case ECMA_STRING_CONTAINER_MAGIC_STRING:
13381362
{
1339-
length++;
1363+
TODO ("Cache magic string lengths")
1364+
return lit_utf8_string_length (lit_get_magic_string_utf8 (string_p->u.magic_string_id),
1365+
lit_get_magic_string_size (string_p->u.magic_string_id));
13401366
}
1367+
case ECMA_STRING_CONTAINER_MAGIC_STRING_EX:
1368+
{
1369+
TODO ("Cache magic string lengths")
1370+
return lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1371+
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1372+
}
1373+
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
1374+
{
1375+
return ecma_string_get_number_in_desc_size (string_p->u.uint32_number);
1376+
}
1377+
case ECMA_STRING_CONTAINER_HEAP_NUMBER:
1378+
{
1379+
return (ecma_length_t) ecma_string_get_heap_number_size (string_p->u.number_cp);
1380+
}
1381+
default:
1382+
{
1383+
JERRY_ASSERT ((ecma_string_container_t) string_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS);
13411384

1342-
return length;
1343-
}
1344-
else if (container == ECMA_STRING_CONTAINER_HEAP_NUMBER)
1345-
{
1346-
const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
1347-
string_p->u.number_cp);
1348-
1349-
lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1350-
1351-
return (ecma_length_t) ecma_number_to_utf8_string (*num_p, buffer, sizeof (buffer));
1352-
}
1353-
else
1354-
{
1355-
JERRY_ASSERT (container == ECMA_STRING_CONTAINER_HEAP_CHUNKS);
1356-
1357-
const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t,
1358-
string_p->u.collection_cp);
1385+
const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t,
1386+
string_p->u.collection_cp);
13591387

1360-
return ecma_get_chars_collection_length (collection_header_p);
1388+
return ecma_get_chars_collection_length (collection_header_p);
1389+
}
13611390
}
13621391
} /* ecma_string_get_length */
13631392

1364-
13651393
/**
13661394
* Get size of ecma-string
13671395
*
@@ -1389,39 +1417,11 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */
13891417
}
13901418
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
13911419
{
1392-
const uint32_t uint32_number = string_p->u.uint32_number;
1393-
const int32_t max_uint32_len = 10;
1394-
const uint32_t nums_with_ascending_length[max_uint32_len] =
1395-
{
1396-
1u,
1397-
10u,
1398-
100u,
1399-
1000u,
1400-
10000u,
1401-
100000u,
1402-
1000000u,
1403-
10000000u,
1404-
100000000u,
1405-
1000000000u
1406-
};
1407-
1408-
int32_t size = 1;
1409-
while (size < max_uint32_len
1410-
&& uint32_number >= nums_with_ascending_length[size])
1411-
{
1412-
size++;
1413-
}
1414-
return (lit_utf8_size_t) size;
1420+
return (lit_utf8_size_t) ecma_string_get_number_in_desc_size (string_p->u.uint32_number);
14151421
}
14161422
case ECMA_STRING_CONTAINER_HEAP_NUMBER:
14171423
{
1418-
const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
1419-
string_p->u.number_cp);
1420-
lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1421-
1422-
return ecma_number_to_utf8_string (*num_p,
1423-
buffer,
1424-
sizeof (buffer));
1424+
return ecma_string_get_heap_number_size (string_p->u.number_cp);
14251425
}
14261426
default:
14271427
{

0 commit comments

Comments
 (0)