Skip to content

Commit 1f0ed97

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'. VLAs cannot be initialized by any form of initialization syntax with C99 standard. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
1 parent 0219f37 commit 1f0ed97

File tree

1 file changed

+77
-81
lines changed

1 file changed

+77
-81
lines changed

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

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,42 @@ 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+
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+
const uint32_t max_uint32_len = sizeof (nums_with_ascending_length) / sizeof (uint32_t);
1309+
1310+
/**
1311+
* Get size of container heap number of ecma-string
1312+
*
1313+
* @return number of bytes in the buffer
1314+
*/
1315+
static lit_utf8_size_t
1316+
ecma_string_get_heap_number_size (mem_cpointer_t number_cp) /**< ecma-string */
1317+
{
1318+
const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t, number_cp);
1319+
lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1320+
1321+
return ecma_number_to_utf8_string (*num_p, buffer, sizeof (buffer));
1322+
} /* ecma_string_get_heap_number_size */
1323+
12881324
/**
12891325
* Get length of ecma-string
12901326
*
@@ -1293,75 +1329,55 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12931329
ecma_length_t
12941330
ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */
12951331
{
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)
1332+
switch ((ecma_string_container_t) string_p->container)
13171333
{
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] =
1334+
case ECMA_STRING_CONTAINER_LIT_TABLE:
13211335
{
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])
1336+
lit_literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp);
1337+
JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit));
1338+
return lit_charset_literal_get_length (lit);
1339+
}
1340+
case ECMA_STRING_CONTAINER_MAGIC_STRING:
13381341
{
1339-
length++;
1342+
TODO ("Cache magic string lengths")
1343+
return lit_utf8_string_length (lit_get_magic_string_utf8 (string_p->u.magic_string_id),
1344+
lit_get_magic_string_size (string_p->u.magic_string_id));
13401345
}
1346+
case ECMA_STRING_CONTAINER_MAGIC_STRING_EX:
1347+
{
1348+
TODO ("Cache magic string lengths")
1349+
return lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1350+
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1351+
}
1352+
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
1353+
{
1354+
const uint32_t uint32_number = string_p->u.uint32_number;
1355+
ecma_length_t length = 1;
13411356

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];
1357+
while (length < max_uint32_len
1358+
&& uint32_number >= nums_with_ascending_length[length])
1359+
{
1360+
length++;
1361+
}
13501362

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);
1363+
return length;
1364+
}
1365+
case ECMA_STRING_CONTAINER_HEAP_NUMBER:
1366+
{
1367+
return (ecma_length_t) ecma_string_get_heap_number_size (string_p->u.number_cp);
1368+
}
1369+
default:
1370+
{
1371+
JERRY_ASSERT ((ecma_string_container_t) string_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS);
13561372

1357-
const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t,
1358-
string_p->u.collection_cp);
1373+
const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t,
1374+
string_p->u.collection_cp);
13591375

1360-
return ecma_get_chars_collection_length (collection_header_p);
1376+
return ecma_get_chars_collection_length (collection_header_p);
1377+
}
13611378
}
13621379
} /* ecma_string_get_length */
13631380

1364-
13651381
/**
13661382
* Get size of ecma-string
13671383
*
@@ -1390,22 +1406,8 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */
13901406
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
13911407
{
13921408
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+
1410+
uint32_t size = 1;
14091411
while (size < max_uint32_len
14101412
&& uint32_number >= nums_with_ascending_length[size])
14111413
{
@@ -1415,13 +1417,7 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */
14151417
}
14161418
case ECMA_STRING_CONTAINER_HEAP_NUMBER:
14171419
{
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));
1420+
return ecma_string_get_heap_number_size (string_p->u.number_cp);
14251421
}
14261422
default:
14271423
{

0 commit comments

Comments
 (0)