@@ -1325,6 +1325,106 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
13251325 return ret_value;
13261326} /* ecma_builtin_global_object_escape */
13271327
1328+ /* *
1329+ * The Global object's 'unescape' routine
1330+ *
1331+ * See also:
1332+ * ECMA-262 v5, B.2.2
1333+ *
1334+ * @return completion value
1335+ * Returned value must be freed with ecma_free_completion_value.
1336+ */
1337+ static ecma_completion_value_t
1338+ ecma_builtin_global_object_unescape (ecma_value_t this_arg __attr_unused___, /* *< this argument */
1339+ ecma_value_t arg) /* *< routine's first argument */
1340+ {
1341+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
1342+
1343+ /* 1. */
1344+ ECMA_TRY_CATCH (string, ecma_op_to_string (arg), ret_value);
1345+ ecma_string_t *input_string_p = ecma_get_string_from_value (string);
1346+ /* 2. */
1347+ lit_utf8_size_t input_size = ecma_string_get_size (input_string_p);
1348+
1349+ /* 3. */
1350+ MEM_DEFINE_LOCAL_ARRAY (input_start_p, input_size, lit_utf8_byte_t );
1351+ ecma_string_to_utf8_string (input_string_p, input_start_p, (ssize_t ) (input_size));
1352+
1353+ lit_utf8_iterator_t iterator = lit_utf8_iterator_create (input_start_p, input_size);
1354+ /* 4. */
1355+ lit_utf8_size_t output_length = 0 ;
1356+ lit_utf8_byte_t *output_char_p = input_start_p;
1357+
1358+ uint8_t status = 0 ;
1359+ lit_code_point_t code_point = 0 ;
1360+ /* 5. */
1361+ while (!lit_utf8_iterator_is_eos (&iterator))
1362+ {
1363+ /* 6. */
1364+ ecma_char_t ch = lit_utf8_iterator_read_next (&iterator);
1365+
1366+ /* 7-8. */
1367+ if (status == 0 && ch == LIT_CHAR_PERCENT)
1368+ {
1369+ /* Found '%' char, start of escape sequence. */
1370+ status = 1 ;
1371+ }
1372+ /* 9-10. */
1373+ else if (status == 1 && ch == LIT_CHAR_LOWERCASE_U)
1374+ {
1375+ /* Found 'u' char after '%'. */
1376+ status = 4 ;
1377+ }
1378+ else if (status > 0 && lit_char_is_hex_digit (ch))
1379+ {
1380+ /* Found hexadecimal digit in escape sequence. */
1381+ code_point = code_point * 16 + lit_char_hex_to_int (ch);
1382+ status++;
1383+ }
1384+
1385+ /* 11-13. */
1386+ if (status == 8 )
1387+ {
1388+ /* Found valid '%uwxyz' escape, decode. */
1389+ output_char_p -= 5 ;
1390+ lit_utf8_size_t lit_size = lit_code_point_to_utf8 (code_point, output_char_p);
1391+ output_char_p += lit_size;
1392+ output_length = output_length + (lit_size - 5 );
1393+ status = 0 ;
1394+ code_point = 0 ;
1395+ }
1396+ /* 14-17. */
1397+ else if (status == 3 )
1398+ {
1399+ /* Found valid '%xy' escape, decode. */
1400+ output_char_p -= 2 ;
1401+ lit_utf8_size_t lit_size = lit_code_point_to_utf8 (code_point, output_char_p);
1402+ output_char_p += lit_size;
1403+ output_length = output_length + (lit_size - 2 );
1404+ status = 0 ;
1405+ code_point = 0 ;
1406+ }
1407+ /* 18. */
1408+ else
1409+ {
1410+ /* Copying unescaped character. */
1411+ *output_char_p = (lit_utf8_byte_t ) ch;
1412+ output_char_p++;
1413+ output_length++;
1414+ }
1415+
1416+ JERRY_ASSERT (output_length <= iterator.buf_pos .offset );
1417+ }
1418+
1419+ ecma_string_t *output_string_p = ecma_new_ecma_string_from_utf8 (input_start_p, output_length);
1420+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_string_p));
1421+
1422+ MEM_FINALIZE_LOCAL_ARRAY (input_start_p);
1423+
1424+ ECMA_FINALIZE (string);
1425+ return ret_value;
1426+ } /* ecma_builtin_global_object_unescape */
1427+
13281428#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */
13291429
13301430/* *
0 commit comments