Skip to content

Commit f86d745

Browse files
zherczegrobertsipka
authored andcommitted
Fixes for ES2015 classes. (#2424)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 0ca0437 commit f86d745

File tree

7 files changed

+70
-72
lines changed

7 files changed

+70
-72
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,10 @@ lexer_skip_empty_statements (parser_context_t *context_p) /**< context */
286286
{
287287
lexer_skip_spaces (context_p);
288288

289-
while (*context_p->source_p == LIT_CHAR_SEMICOLON)
289+
while (context_p->source_p < context_p->source_end_p
290+
&& *context_p->source_p == LIT_CHAR_SEMICOLON)
290291
{
291-
lexer_next_token (context_p);
292+
context_p->source_p++;
292293
lexer_skip_spaces (context_p);
293294
}
294295
} /* lexer_skip_empty_statements */
@@ -2268,7 +2269,9 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
22682269
lexer_skip_spaces (context_p);
22692270

22702271
#ifndef CONFIG_DISABLE_ES2015_CLASS
2271-
bool is_static_method = (context_p->token.type == LEXER_KEYW_STATIC);
2272+
int is_class_method = ((context_p->status_flags & PARSER_IS_CLASS)
2273+
&& !must_be_identifier
2274+
&& (context_p->token.type != LEXER_KEYW_STATIC));
22722275
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
22732276

22742277
context_p->token.line = context_p->line;
@@ -2302,11 +2305,9 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
23022305
}
23032306
}
23042307
}
2308+
23052309
#ifndef CONFIG_DISABLE_ES2015_CLASS
2306-
if ((context_p->status_flags & PARSER_IS_CLASS)
2307-
&& !must_be_identifier
2308-
&& !is_static_method
2309-
&& context_p->token.lit_location.length == 6
2310+
if (is_class_method
23102311
&& lexer_compare_raw_identifier_to_current (context_p, "static", 6))
23112312
{
23122313
context_p->token.type = LEXER_KEYW_STATIC;
@@ -2349,9 +2350,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
23492350
}
23502351

23512352
#ifndef CONFIG_DISABLE_ES2015_CLASS
2352-
if ((context_p->status_flags & PARSER_IS_CLASS)
2353-
&& !must_be_identifier
2354-
&& !is_static_method
2353+
if (is_class_method
23552354
&& lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
23562355
{
23572356
context_p->token.type = LEXER_CLASS_CONSTRUCTOR;
@@ -2431,28 +2430,28 @@ lexer_scan_identifier (parser_context_t *context_p, /**< context */
24312430
*/
24322431
bool
24332432
lexer_compare_identifier_to_current (parser_context_t *context_p, /**< context */
2434-
const lexer_lit_location_t *right) /**< identifier */
2433+
const lexer_lit_location_t *right_ident_p) /**< identifier */
24352434
{
2436-
lexer_lit_location_t *left = &context_p->token.lit_location;
2435+
lexer_lit_location_t *left_ident_p = &context_p->token.lit_location;
24372436
const uint8_t *left_p;
24382437
const uint8_t *right_p;
24392438
size_t count;
24402439

2441-
JERRY_ASSERT (left->length > 0 && right->length > 0);
2440+
JERRY_ASSERT (left_ident_p->length > 0 && right_ident_p->length > 0);
24422441

2443-
if (left->length != right->length)
2442+
if (left_ident_p->length != right_ident_p->length)
24442443
{
24452444
return 0;
24462445
}
24472446

2448-
if (!left->has_escape && !right->has_escape)
2447+
if (!left_ident_p->has_escape && !right_ident_p->has_escape)
24492448
{
2450-
return memcmp (left->char_p, right->char_p, left->length) == 0;
2449+
return memcmp (left_ident_p->char_p, right_ident_p->char_p, left_ident_p->length) == 0;
24512450
}
24522451

2453-
left_p = left->char_p;
2454-
right_p = right->char_p;
2455-
count = left->length;
2452+
left_p = left_ident_p->char_p;
2453+
right_p = right_ident_p->char_p;
2454+
count = left_ident_p->length;
24562455

24572456
do
24582457
{
@@ -2529,14 +2528,14 @@ lexer_compare_raw_identifier_to_current (parser_context_t *context_p, /**< conte
25292528
const char *right_ident_p, /**< identifier */
25302529
size_t right_ident_length) /**< identifier length */
25312530
{
2532-
lexer_lit_location_t *left = &context_p->token.lit_location;
2531+
lexer_lit_location_t *left_ident_p = &context_p->token.lit_location;
25332532

2534-
if (left->length != right_ident_length || left->has_escape)
2533+
if (left_ident_p->length != right_ident_length || left_ident_p->has_escape)
25352534
{
25362535
return 0;
25372536
}
25382537

2539-
return memcmp (left->char_p, right_ident_p, right_ident_length) == 0;
2538+
return memcmp (left_ident_p->char_p, right_ident_p, right_ident_length) == 0;
25402539
} /* lexer_compare_raw_identifier_to_current */
25412540

25422541
/**

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
429429
if (constructor_literal_p->type == LEXER_FUNCTION_LITERAL)
430430
{
431431
/* 14.5.1 */
432-
parser_raise_error (context_p, PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR);
432+
parser_raise_error (context_p, PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS);
433433
}
434434

435435
parser_flush_cbc (context_p);
@@ -445,7 +445,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
445445
{
446446
if (is_static && lexer_compare_raw_identifier_to_current (context_p, "prototype", 9))
447447
{
448-
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE);
448+
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
449449
}
450450

451451
if (!lexer_check_left_paren (context_p))
@@ -1102,13 +1102,6 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
11021102
PARSER_IS_FUNCTION | PARSER_IS_FUNC_EXPRESSION | PARSER_IS_CLOSURE);
11031103
break;
11041104
}
1105-
#ifndef CONFIG_DISABLE_ES2015_CLASS
1106-
case LEXER_KEYW_CLASS:
1107-
{
1108-
parser_parse_class (context_p, false);
1109-
return;
1110-
}
1111-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
11121105
case LEXER_LEFT_BRACE:
11131106
{
11141107
parser_parse_object_literal (context_p);
@@ -1165,6 +1158,13 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
11651158
parser_emit_cbc (context_p, CBC_PUSH_NULL);
11661159
break;
11671160
}
1161+
#ifndef CONFIG_DISABLE_ES2015_CLASS
1162+
case LEXER_KEYW_CLASS:
1163+
{
1164+
parser_parse_class (context_p, false);
1165+
return;
1166+
}
1167+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
11681168
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
11691169
case LEXER_RIGHT_PAREN:
11701170
{

jerry-core/parser/js/js-parser-internal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ bool lexer_construct_number_object (parser_context_t *context_p, bool is_expr, b
454454
void lexer_convert_push_number_to_push_literal (parser_context_t *context_p);
455455
uint16_t lexer_construct_function_object (parser_context_t *context_p, uint32_t extra_status_flags);
456456
void lexer_construct_regexp_object (parser_context_t *context_p, bool parse_only);
457-
bool lexer_compare_identifier_to_current (parser_context_t *context_p, const lexer_lit_location_t *right);
457+
bool lexer_compare_identifier_to_current (parser_context_t *context_p, const lexer_lit_location_t *right_ident_p);
458458
bool lexer_compare_raw_identifier_to_current (parser_context_t *context_p, const char *right_ident_p,
459459
size_t right_ident_length);
460460

@@ -468,6 +468,9 @@ bool lexer_compare_raw_identifier_to_current (parser_context_t *context_p, const
468468
/* Parser functions. */
469469

470470
void parser_parse_expression (parser_context_t *context_p, int options);
471+
#ifndef CONFIG_DISABLE_ES2015_CLASS
472+
void parser_parse_class (parser_context_t *context_p, bool is_statement);
473+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
471474

472475
/**
473476
* @}
@@ -486,9 +489,6 @@ void parser_scan_until (parser_context_t *context_p, lexer_range_t *range_p, lex
486489
*/
487490

488491
void parser_parse_statements (parser_context_t *context_p);
489-
#ifndef CONFIG_DISABLE_ES2015_CLASS
490-
void parser_parse_class (parser_context_t *context_p, bool is_statement);
491-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
492492
void parser_free_jumps (parser_stack_iterator_t iterator);
493493

494494
/**

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ typedef enum
4141
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
4242
SCAN_MODE_ARROW_FUNCTION, /**< arrow function might follows */
4343
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
44-
#ifndef CONFIG_DISABLE_ES2015_CLASS
45-
SCAN_MODE_CLASS_DECLARATION, /**< scanning class declaration */
46-
SCAN_MODE_CLASS_METHOD, /**< scanning class method */
47-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
4844
SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */
4945
SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning primary expression end */
5046
SCAN_MODE_STATEMENT, /**< scanning statement */
5147
SCAN_MODE_FUNCTION_ARGUMENTS, /**< scanning function arguments */
5248
SCAN_MODE_PROPERTY_NAME, /**< scanning property name */
49+
#ifndef CONFIG_DISABLE_ES2015_CLASS
50+
SCAN_MODE_CLASS_DECLARATION, /**< scanning class declaration */
51+
SCAN_MODE_CLASS_METHOD, /**< scanning class method */
52+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
5353
} scan_modes_t;
5454

5555
/**
@@ -71,7 +71,7 @@ typedef enum
7171
SCAN_STACK_TEMPLATE_STRING, /**< template string */
7272
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
7373
#ifndef CONFIG_DISABLE_ES2015_CLASS
74-
SCAN_STACK_CLASS, /**< class language element */
74+
SCAN_STACK_CLASS, /**< class language element */
7575
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
7676
} scan_stack_modes_t;
7777

@@ -106,14 +106,6 @@ parser_scan_primary_expression (parser_context_t *context_p, /**< context */
106106
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
107107
break;
108108
}
109-
#ifndef CONFIG_DISABLE_ES2015_CLASS
110-
case LEXER_KEYW_CLASS:
111-
{
112-
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_EXPRESSION);
113-
*mode = SCAN_MODE_CLASS_DECLARATION;
114-
break;
115-
}
116-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
117109
case LEXER_LEFT_PAREN:
118110
{
119111
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
@@ -164,6 +156,14 @@ parser_scan_primary_expression (parser_context_t *context_p, /**< context */
164156
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
165157
break;
166158
}
159+
#ifndef CONFIG_DISABLE_ES2015_CLASS
160+
case LEXER_KEYW_CLASS:
161+
{
162+
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_EXPRESSION);
163+
*mode = SCAN_MODE_CLASS_DECLARATION;
164+
break;
165+
}
166+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
167167
case LEXER_RIGHT_SQUARE:
168168
{
169169
if (stack_top != SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
@@ -512,7 +512,7 @@ parser_scan_statement (parser_context_t *context_p, /**< context */
512512
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
513513
}
514514
#ifndef CONFIG_DISABLE_ES2015_CLASS
515-
if (stack_top == SCAN_STACK_CLASS)
515+
else if (stack_top == SCAN_STACK_CLASS)
516516
{
517517
*mode = SCAN_MODE_CLASS_METHOD;
518518
}
@@ -767,7 +767,7 @@ parser_scan_until (parser_context_t *context_p, /**< context */
767767
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
768768
|| stack_top == SCAN_STACK_CLASS
769769
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);
770-
#else
770+
#else /* CONFIG_DISABLE_ES2015_CLASS */
771771
JERRY_ASSERT (stack_top == SCAN_STACK_BLOCK_STATEMENT
772772
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
773773
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,20 @@ parser_error_to_string (parser_error_t error) /**< error code */
898898
{
899899
return "Case statement must be in a switch block.";
900900
}
901+
#ifndef CONFIG_DISABLE_ES2015_CLASS
902+
case PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS:
903+
{
904+
return "Multiple constructors are not allowed.";
905+
}
906+
case PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR:
907+
{
908+
return "Class constructor may not be an accessor.";
909+
}
910+
case PARSER_ERR_CLASS_STATIC_PROTOTYPE:
911+
{
912+
return "Classes may not have a static property called 'prototype'.";
913+
}
914+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
901915
case PARSER_ERR_LEFT_PAREN_EXPECTED:
902916
{
903917
return "Expected '(' token.";
@@ -920,20 +934,6 @@ parser_error_to_string (parser_error_t error) /**< error code */
920934
return "Expected '}' token.";
921935
}
922936
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
923-
#ifndef CONFIG_DISABLE_ES2015_CLASS
924-
case PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR:
925-
{
926-
return "Multiple constructors are not allowed.";
927-
}
928-
case PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR:
929-
{
930-
return "Class constructor may not be an accessor.";
931-
}
932-
case PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE:
933-
{
934-
return "Classes may not have a static property called 'prototype'.";
935-
}
936-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
937937
case PARSER_ERR_COLON_EXPECTED:
938938
{
939939
return "Expected ':' token.";

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,8 +2541,7 @@ parser_parse_function (parser_context_t *context_p, /**< context */
25412541
bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR;
25422542
JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing start ---\n\n"
25432543
: "\n--- Function parsing start ---\n\n");
2544-
2545-
#else
2544+
#else /* CONFIG_DISABLE_ES2015_CLASS */
25462545
JERRY_DEBUG_MSG ("\n--- Function parsing start ---\n\n");
25472546
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
25482547
}
@@ -2646,7 +2645,7 @@ parser_parse_function (parser_context_t *context_p, /**< context */
26462645
bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR;
26472646
JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing end ---\n\n"
26482647
: "\n--- Function parsing end ---\n\n");
2649-
#else
2648+
#else /* CONFIG_DISABLE_ES2015_CLASS */
26502649
JERRY_DEBUG_MSG ("\n--- Function parsing end ---\n\n");
26512650
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
26522651
}

jerry-core/parser/js/js-parser.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ typedef enum
7979
PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED, /**< multiple default cases are not allowed */
8080
PARSER_ERR_DEFAULT_NOT_IN_SWITCH, /**< default statement is not in switch block */
8181
PARSER_ERR_CASE_NOT_IN_SWITCH, /**< case statement is not in switch block */
82+
#ifndef CONFIG_DISABLE_ES2015_CLASS
83+
PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS, /**< multiple class constructor */
84+
PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR, /**< class constructor cannot be an accessor */
85+
PARSER_ERR_CLASS_STATIC_PROTOTYPE, /**< static method name 'prototype' is not allowed */
86+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
8287

8388
PARSER_ERR_LEFT_PAREN_EXPECTED, /**< left paren expected */
8489
PARSER_ERR_LEFT_BRACE_EXPECTED, /**< left brace expected */
@@ -87,11 +92,6 @@ typedef enum
8792
#ifndef CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
8893
PARSER_ERR_RIGHT_BRACE_EXPECTED, /**< right brace expected */
8994
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
90-
#ifndef CONFIG_DISABLE_ES2015_CLASS
91-
PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR, /**< multiple class constructor */
92-
PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR, /**< class constructor cannot be an accessor */
93-
PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE, /**< static method name 'prototype' is not allowed */
94-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
9595
PARSER_ERR_COLON_EXPECTED, /**< colon expected */
9696
PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED, /**< colon expected for conditional expression */
9797
PARSER_ERR_SEMICOLON_EXPECTED, /**< semicolon expected */

0 commit comments

Comments
 (0)