Skip to content

Commit a8fb4ee

Browse files
zherczegdbatyai
authored andcommitted
Fix three minor issues. (#3165)
1) Remove an unnecessary check. 2) Check that comma is not allowed between question mark and colon 3) Create a hex decoding variant which does not throw error. These are quality improvements, they have no negative impact. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 711b06d commit a8fb4ee

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

jerry-core/parser/js/js-lexer.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@ lexer_hex_to_character (parser_context_t *context_p, /**< context */
9090
return (ecma_char_t) result;
9191
} /* lexer_hex_to_character */
9292

93+
/**
94+
* Parse hexadecimal character sequence
95+
*
96+
* @return character value
97+
*/
98+
static ecma_char_t
99+
lexer_unchecked_hex_to_character (const uint8_t *source_p, /**< current source position */
100+
int length) /**< source length */
101+
{
102+
uint32_t result = 0;
103+
104+
do
105+
{
106+
uint32_t byte = *source_p++;
107+
108+
result <<= 4;
109+
110+
if (byte >= LIT_CHAR_0 && byte <= LIT_CHAR_9)
111+
{
112+
result += byte - LIT_CHAR_0;
113+
}
114+
else
115+
{
116+
JERRY_ASSERT ((byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
117+
|| (byte >= LIT_CHAR_UPPERCASE_A && byte <= LIT_CHAR_UPPERCASE_F));
118+
119+
result += LEXER_TO_ASCII_LOWERCASE (byte) - (LIT_CHAR_LOWERCASE_A - 10);
120+
}
121+
}
122+
while (--length > 0);
123+
124+
return (ecma_char_t) result;
125+
} /* lexer_unchecked_hex_to_character */
126+
93127
/**
94128
* Skip space mode
95129
*/
@@ -1513,7 +1547,7 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */
15131547
if (*source_p == LIT_CHAR_BACKSLASH)
15141548
{
15151549
destination_p += lit_char_to_utf8_bytes (destination_p,
1516-
lexer_hex_to_character (context_p, source_p + 2, 4));
1550+
lexer_unchecked_hex_to_character (source_p + 2, 4));
15171551
source_p += 6;
15181552
continue;
15191553
}
@@ -1628,9 +1662,8 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */
16281662
JERRY_ASSERT (source_p + 1 + hex_part_length <= context_p->source_end_p);
16291663

16301664
destination_p += lit_char_to_utf8_bytes (destination_p,
1631-
lexer_hex_to_character (context_p,
1632-
source_p + 1,
1633-
hex_part_length));
1665+
lexer_unchecked_hex_to_character (source_p + 1,
1666+
hex_part_length));
16341667
source_p += hex_part_length + 1;
16351668
continue;
16361669
}
@@ -1731,11 +1764,12 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */
17311764
literal_type,
17321765
literal_p->has_escape);
17331766

1767+
JERRY_ASSERT (literal_type == context_p->lit_object.literal_p->type);
1768+
17341769
context_p->lit_object.type = LEXER_LITERAL_OBJECT_ANY;
17351770

17361771
if (literal_type == LEXER_IDENT_LITERAL
1737-
&& (context_p->status_flags & PARSER_INSIDE_WITH)
1738-
&& context_p->lit_object.literal_p->type == LEXER_IDENT_LITERAL)
1772+
&& (context_p->status_flags & PARSER_INSIDE_WITH))
17391773
{
17401774
context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_NO_REG_STORE;
17411775
}
@@ -2535,9 +2569,9 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context *
25352569

25362570
if (*left_p == LIT_CHAR_BACKSLASH && *right_p == LIT_CHAR_BACKSLASH)
25372571
{
2538-
uint16_t left_chr = lexer_hex_to_character (context_p, left_p + 2, 4);
2572+
uint16_t left_chr = lexer_unchecked_hex_to_character (left_p + 2, 4);
25392573

2540-
if (left_chr != lexer_hex_to_character (context_p, right_p + 2, 4))
2574+
if (left_chr != lexer_unchecked_hex_to_character (right_p + 2, 4))
25412575
{
25422576
return false;
25432577
}
@@ -2557,7 +2591,7 @@ lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context *
25572591
right_p = swap_p;
25582592
}
25592593

2560-
utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_hex_to_character (context_p, left_p + 2, 4));
2594+
utf8_len = lit_char_to_utf8_bytes (utf8_buf, lexer_unchecked_hex_to_character (left_p + 2, 4));
25612595
JERRY_ASSERT (utf8_len > 0);
25622596
count -= utf8_len;
25632597
offset = 0;

jerry-core/parser/js/js-scanner.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context *
434434
scanner_context_p->mode = SCAN_MODE_VAR_STATEMENT;
435435
return SCAN_NEXT_TOKEN;
436436
}
437+
case SCAN_STACK_COLON_EXPRESSION:
438+
{
439+
scanner_raise_error (context_p);
440+
break;
441+
}
437442
#if ENABLED (JERRY_ES2015_ARROW_FUNCTION)
438443
case SCAN_STACK_ARROW_EXPRESSION:
439444
{

0 commit comments

Comments
 (0)