-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathjs-parser-statm.c
More file actions
3399 lines (2802 loc) · 109 KB
/
js-parser-statm.c
File metadata and controls
3399 lines (2802 loc) · 109 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js-parser-internal.h"
#if JERRY_PARSER
#include "ecma-helpers.h"
#include "jcontext.h"
#include "lit-char-helpers.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_stmt Statement parser
* @{
*/
/**
* Parser statement types.
*
* When a new statement is added, the following
* arrays must be updated as well:
* - statement_lengths[]
* - parser_statement_flags[]
*/
typedef enum
{
PARSER_STATEMENT_START,
PARSER_STATEMENT_BLOCK,
PARSER_STATEMENT_BLOCK_SCOPE,
PARSER_STATEMENT_PRIVATE_SCOPE,
PARSER_STATEMENT_BLOCK_CONTEXT,
PARSER_STATEMENT_PRIVATE_CONTEXT,
PARSER_STATEMENT_LABEL,
PARSER_STATEMENT_IF,
PARSER_STATEMENT_ELSE,
PARSER_STATEMENT_SWITCH,
PARSER_STATEMENT_SWITCH_NO_DEFAULT,
PARSER_STATEMENT_DO_WHILE,
PARSER_STATEMENT_WHILE,
PARSER_STATEMENT_FOR,
PARSER_STATEMENT_FOR_IN,
PARSER_STATEMENT_FOR_OF,
PARSER_STATEMENT_FOR_AWAIT_OF,
PARSER_STATEMENT_WITH,
PARSER_STATEMENT_TRY,
} parser_statement_type_t;
/**
* Parser statement type flags.
*/
typedef enum
{
PARSER_STATM_NO_OPTS = 0, /**< no options */
PARSER_STATM_SINGLE_STATM = (1 << 0), /**< statment can form single statement context */
PARSER_STATM_HAS_BLOCK = (1 << 1), /**< statement always has a code block */
PARSER_STATM_BREAK_TARGET = (1 << 2), /**< break target statement */
PARSER_STATM_CONTINUE_TARGET = (1 << 3), /**< continue target statement */
PARSER_STATM_CONTEXT_BREAK = (1 << 4), /**< uses another instruction form when crosses their borders */
} parser_statement_flags_t;
/**
* Parser statement attributes.
* Note: the order of the attributes must be keep in sync with parser_statement_type_t
*/
static const uint8_t parser_statement_flags[] = {
/* PARSER_STATEMENT_START */
PARSER_STATM_HAS_BLOCK,
/* PARSER_STATEMENT_BLOCK, */
PARSER_STATM_HAS_BLOCK,
/* PARSER_STATEMENT_BLOCK_SCOPE, */
PARSER_STATM_HAS_BLOCK,
/* PARSER_STATEMENT_PRIVATE_SCOPE, */
PARSER_STATM_NO_OPTS,
/* PARSER_STATEMENT_BLOCK_CONTEXT, */
PARSER_STATM_HAS_BLOCK | PARSER_STATM_CONTEXT_BREAK,
/* PARSER_STATEMENT_PRIVATE_CONTEXT, */
PARSER_STATM_CONTEXT_BREAK,
/* PARSER_STATEMENT_LABEL */
PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_IF */
PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_ELSE */
PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_SWITCH */
PARSER_STATM_HAS_BLOCK | PARSER_STATM_BREAK_TARGET,
/* PARSER_STATEMENT_SWITCH_NO_DEFAULT */
PARSER_STATM_HAS_BLOCK | PARSER_STATM_BREAK_TARGET,
/* PARSER_STATEMENT_DO_WHILE */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_WHILE */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_FOR */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_FOR_IN */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM | PARSER_STATM_CONTEXT_BREAK,
/* PARSER_STATEMENT_FOR_OF */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM | PARSER_STATM_CONTEXT_BREAK,
/* PARSER_STATEMENT_FOR_AWAIT_OF */
PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM | PARSER_STATM_CONTEXT_BREAK,
/* PARSER_STATEMENT_WITH */
PARSER_STATM_CONTEXT_BREAK | PARSER_STATM_SINGLE_STATM,
/* PARSER_STATEMENT_TRY */
PARSER_STATM_HAS_BLOCK | PARSER_STATM_CONTEXT_BREAK
};
/**
* Block statement.
*/
typedef struct
{
uint16_t scope_stack_top; /**< preserved top of scope stack */
uint16_t scope_stack_reg_top; /**< preserved top register of scope stack */
} parser_block_statement_t;
/**
* Context of block statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
} parser_block_context_t;
/**
* Loop statement.
*/
typedef struct
{
parser_branch_node_t *branch_list_p; /**< list of breaks and continues targeting this statement */
} parser_loop_statement_t;
/**
* Label statement.
*/
typedef struct
{
lexer_lit_location_t label_ident; /**< name of the label */
parser_branch_node_t *break_list_p; /**< list of breaks targeting this label */
} parser_label_statement_t;
/**
* If/else statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
} parser_if_else_statement_t;
/**
* Switch statement.
*/
typedef struct
{
parser_branch_t default_branch; /**< branch to the default case */
parser_branch_node_t *branch_list_p; /**< branches of case statements */
} parser_switch_statement_t;
/**
* Do-while statement.
*/
typedef struct
{
uint32_t start_offset; /**< start byte code offset */
} parser_do_while_statement_t;
/**
* While statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
scanner_location_t condition_location; /**< condition part */
uint32_t start_offset; /**< start byte code offset */
} parser_while_statement_t;
/**
* For statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
scanner_location_t condition_location; /**< condition part */
scanner_location_t expression_location; /**< expression part */
uint32_t start_offset; /**< start byte code offset */
} parser_for_statement_t;
/**
* For-in statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
uint32_t start_offset; /**< start byte code offset */
} parser_for_in_of_statement_t;
/**
* With statement.
*/
typedef struct
{
parser_branch_t branch; /**< branch to the end */
} parser_with_statement_t;
/**
* Lexer token types.
*/
typedef enum
{
parser_try_block, /**< try block */
parser_catch_block, /**< catch block */
parser_finally_block, /**< finally block */
} parser_try_block_type_t;
/**
* Try statement.
*/
typedef struct
{
parser_try_block_type_t type; /**< current block type */
uint16_t scope_stack_top; /**< current top of scope stack */
uint16_t scope_stack_reg_top; /**< current top register of scope stack */
parser_branch_t branch; /**< branch to the end of the current block */
} parser_try_statement_t;
/**
* Returns the data consumed by a statement. It can be used
* to skip undesired frames on the stack during frame search.
*
* @return size consumed by a statement.
*/
static inline size_t
parser_statement_length (uint8_t type) /**< type of statement */
{
static const uint8_t statement_lengths[] = {
/* PARSER_STATEMENT_BLOCK */
1,
/* PARSER_STATEMENT_BLOCK_SCOPE */
(uint8_t) (sizeof (parser_block_statement_t) + 1),
/* PARSER_STATEMENT_PRIVATE_SCOPE */
(uint8_t) (sizeof (parser_block_statement_t) + 1),
/* PARSER_STATEMENT_BLOCK_CONTEXT */
(uint8_t) (sizeof (parser_block_statement_t) + sizeof (parser_block_context_t) + 1),
/* PARSER_STATEMENT_PRIVATE_CONTEXT */
(uint8_t) (sizeof (parser_block_statement_t) + sizeof (parser_block_context_t) + 1),
/* PARSER_STATEMENT_LABEL */
(uint8_t) (sizeof (parser_label_statement_t) + 1),
/* PARSER_STATEMENT_IF */
(uint8_t) (sizeof (parser_if_else_statement_t) + 1),
/* PARSER_STATEMENT_ELSE */
(uint8_t) (sizeof (parser_if_else_statement_t) + 1),
/* PARSER_STATEMENT_SWITCH */
(uint8_t) (sizeof (parser_switch_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_SWITCH_NO_DEFAULT */
(uint8_t) (sizeof (parser_switch_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_DO_WHILE */
(uint8_t) (sizeof (parser_do_while_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_WHILE */
(uint8_t) (sizeof (parser_while_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_FOR */
(uint8_t) (sizeof (parser_for_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_FOR_IN */
(uint8_t) (sizeof (parser_for_in_of_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_FOR_OF */
(uint8_t) (sizeof (parser_for_in_of_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_FOR_AWAIT_OF */
(uint8_t) (sizeof (parser_for_in_of_statement_t) + sizeof (parser_loop_statement_t) + 1),
/* PARSER_STATEMENT_WITH */
(uint8_t) (sizeof (parser_with_statement_t) + 1 + 1),
/* PARSER_STATEMENT_TRY */
(uint8_t) (sizeof (parser_try_statement_t) + 1),
};
JERRY_ASSERT (type >= PARSER_STATEMENT_BLOCK && type <= PARSER_STATEMENT_TRY);
return statement_lengths[type - PARSER_STATEMENT_BLOCK];
} /* parser_statement_length */
/**
* Parse expression enclosed in parens.
*/
static inline void
parser_parse_enclosed_expr (parser_context_t *context_p) /**< context */
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR);
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
} /* parser_parse_enclosed_expr */
/**
* Create a block context.
*
* @return true - when a context is created, false - otherwise
*/
static bool
parser_push_block_context (parser_context_t *context_p, /**< context */
bool is_private) /**< is private (bound to a statement) context */
{
JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_BLOCK);
parser_block_statement_t block_statement;
block_statement.scope_stack_top = context_p->scope_stack_top;
block_statement.scope_stack_reg_top = context_p->scope_stack_reg_top;
bool is_context_needed = false;
if (scanner_is_context_needed (context_p, PARSER_CHECK_BLOCK_CONTEXT))
{
parser_block_context_t block_context;
#ifndef JERRY_NDEBUG
PARSER_PLUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_BLOCK_CONTEXT_STACK_ALLOCATION);
#endif /* !JERRY_NDEBUG */
parser_emit_cbc_forward_branch (context_p, CBC_BLOCK_CREATE_CONTEXT, &block_context.branch);
parser_stack_push (context_p, &block_context, sizeof (parser_block_context_t));
is_context_needed = true;
}
scanner_create_variables (context_p, SCANNER_CREATE_VARS_NO_OPTS);
parser_stack_push (context_p, &block_statement, sizeof (parser_block_statement_t));
uint8_t statement_type;
if (is_private)
{
statement_type = (is_context_needed ? PARSER_STATEMENT_PRIVATE_CONTEXT : PARSER_STATEMENT_PRIVATE_SCOPE);
}
else
{
statement_type = (is_context_needed ? PARSER_STATEMENT_BLOCK_CONTEXT : PARSER_STATEMENT_BLOCK_SCOPE);
}
parser_stack_push_uint8 (context_p, statement_type);
return is_context_needed;
} /* parser_push_block_context */
/**
* Pop block context.
*/
static void
parser_pop_block_context (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->stack_top_uint8 == PARSER_STATEMENT_BLOCK_SCOPE
|| context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_SCOPE
|| context_p->stack_top_uint8 == PARSER_STATEMENT_BLOCK_CONTEXT
|| context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_CONTEXT);
uint8_t type = context_p->stack_top_uint8;
parser_block_statement_t block_statement;
parser_stack_pop_uint8 (context_p);
parser_stack_pop (context_p, &block_statement, sizeof (parser_block_statement_t));
context_p->scope_stack_top = block_statement.scope_stack_top;
context_p->scope_stack_reg_top = block_statement.scope_stack_reg_top;
if (type == PARSER_STATEMENT_BLOCK_CONTEXT || type == PARSER_STATEMENT_PRIVATE_CONTEXT)
{
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_BLOCK_CONTEXT_STACK_ALLOCATION);
#ifndef JERRY_NDEBUG
PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_BLOCK_CONTEXT_STACK_ALLOCATION);
#endif /* !JERRY_NDEBUG */
parser_block_context_t block_context;
parser_stack_pop (context_p, &block_context, sizeof (parser_block_context_t));
parser_emit_cbc (context_p, CBC_CONTEXT_END);
parser_set_branch_to_current_position (context_p, &block_context.branch);
}
parser_stack_iterator_init (context_p, &context_p->last_statement);
} /* parser_pop_block_context */
/**
* Validate lexical context for a declaration.
*/
static void
parser_validate_lexical_context (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_LET || context_p->token.type == LEXER_KEYW_CONST
|| context_p->token.type == LEXER_KEYW_CLASS);
if (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM)
{
parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT);
}
} /* parser_validate_lexical_context */
/**
* Parse var statement.
*/
static void
parser_parse_var_statement (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_VAR || context_p->token.type == LEXER_KEYW_LET
|| context_p->token.type == LEXER_KEYW_CONST);
uint8_t declaration_type = context_p->token.type;
if (declaration_type != LEXER_KEYW_VAR)
{
parser_validate_lexical_context (context_p);
}
while (true)
{
if (lexer_check_next_characters (context_p, LIT_CHAR_LEFT_SQUARE, LIT_CHAR_LEFT_BRACE))
{
parser_pattern_flags_t flags = PARSER_PATTERN_BINDING;
if (declaration_type == LEXER_KEYW_LET)
{
flags |= PARSER_PATTERN_LET;
}
else if (declaration_type == LEXER_KEYW_CONST)
{
flags |= PARSER_PATTERN_CONST;
}
parser_parse_initializer_by_next_char (context_p, flags);
}
else
{
lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL);
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL);
#if JERRY_DEBUGGER || JERRY_LINE_INFO
parser_line_counter_t ident_line_counter = context_p->token.line;
#endif /* JERRY_DEBUGGER || JERRY_LINE_INFO */
#if JERRY_LINE_INFO
parser_line_counter_t ident_column_counter = context_p->token.column;
#endif /* JERRY_LINE_INFO */
#if JERRY_MODULE_SYSTEM
parser_module_append_export_name (context_p);
#endif /* JERRY_MODULE_SYSTEM */
if (declaration_type != LEXER_KEYW_VAR && context_p->token.keyword_type == LEXER_KEYW_LET)
{
parser_raise_error (context_p, PARSER_ERR_LEXICAL_LET_BINDING);
}
if (context_p->next_scanner_info_p->source_p == context_p->source_p)
{
JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_ERR_REDECLARED);
parser_raise_error (context_p, PARSER_ERR_VARIABLE_REDECLARED);
}
lexer_next_token (context_p);
if (context_p->token.type == LEXER_ASSIGN)
{
#if JERRY_DEBUGGER
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
&& ident_line_counter != context_p->last_breakpoint_line)
{
parser_emit_cbc (context_p, CBC_BREAKPOINT_DISABLED);
parser_flush_cbc (context_p);
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST, ident_line_counter);
context_p->last_breakpoint_line = ident_line_counter;
}
#endif /* JERRY_DEBUGGER */
#if JERRY_LINE_INFO
parser_line_info_append (context_p, ident_line_counter, ident_column_counter);
#endif /* JERRY_LINE_INFO */
uint16_t index = context_p->lit_object.index;
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
cbc_opcode_t opcode = CBC_ASSIGN_SET_IDENT;
uint16_t function_literal_index = parser_check_anonymous_function_declaration (context_p);
if (function_literal_index == PARSER_ANONYMOUS_CLASS)
{
uint16_t name_index = scanner_save_literal (context_p, index);
parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_CLASS_NAME, name_index);
}
else if (function_literal_index < PARSER_NAMED_FUNCTION)
{
parser_set_function_name (context_p, function_literal_index, index, 0);
}
if (declaration_type != LEXER_KEYW_VAR && (index < PARSER_REGISTER_START))
{
opcode = CBC_INIT_LET;
if (scanner_literal_is_created (context_p, index))
{
opcode = CBC_ASSIGN_LET_CONST;
}
else if (declaration_type == LEXER_KEYW_CONST)
{
opcode = CBC_INIT_CONST;
}
}
parser_emit_cbc_literal (context_p, (uint16_t) opcode, index);
}
else if (declaration_type == LEXER_KEYW_LET)
{
parser_emit_cbc (context_p, CBC_PUSH_UNDEFINED);
uint16_t index = context_p->lit_object.index;
cbc_opcode_t opcode = CBC_MOV_IDENT;
if (index < PARSER_REGISTER_START)
{
opcode = (scanner_literal_is_created (context_p, index) ? CBC_ASSIGN_LET_CONST : CBC_INIT_LET);
}
parser_emit_cbc_literal (context_p, (uint16_t) opcode, index);
}
else if (declaration_type == LEXER_KEYW_CONST)
{
parser_raise_error (context_p, PARSER_ERR_MISSING_ASSIGN_AFTER_CONST);
}
}
if (context_p->token.type != LEXER_COMMA)
{
break;
}
}
#if JERRY_MODULE_SYSTEM
context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT);
#endif /* JERRY_MODULE_SYSTEM */
} /* parser_parse_var_statement */
/**
* Parse function statement.
*/
static void
parser_parse_function_statement (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_FUNCTION);
bool is_single_statement = (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM) != 0;
if (JERRY_UNLIKELY (is_single_statement))
{
if (context_p->status_flags & PARSER_IS_STRICT)
{
parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT);
}
if (context_p->stack_top_uint8 == PARSER_STATEMENT_IF || context_p->stack_top_uint8 == PARSER_STATEMENT_ELSE)
{
/* There must be a parser error later if this check fails. */
if (context_p->next_scanner_info_p->source_p == context_p->source_p)
{
parser_push_block_context (context_p, true);
}
}
else if (context_p->stack_top_uint8 == PARSER_STATEMENT_LABEL)
{
parser_stack_iterator_t iterator;
parser_stack_iterator_init (context_p, &iterator);
parser_stack_iterator_skip (&iterator, sizeof (parser_label_statement_t) + 1);
while (true)
{
uint8_t type = parser_stack_iterator_read_uint8 (&iterator);
if (type == PARSER_STATEMENT_LABEL)
{
parser_stack_iterator_skip (&iterator, sizeof (parser_label_statement_t) + 1);
continue;
}
if (parser_statement_flags[type] & PARSER_STATM_HAS_BLOCK)
{
break;
}
parser_raise_error (context_p, PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK);
}
}
else
{
parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT);
}
}
#if JERRY_FUNCTION_TO_STRING
if (!(context_p->next_scanner_info_p->u8_arg & SCANNER_FUNCTION_ASYNC))
{
context_p->function_start_p = context_p->token.lit_location.char_p;
}
#endif /* JERRY_FUNCTION_TO_STRING */
#if JERRY_DEBUGGER
parser_line_counter_t debugger_line = context_p->token.line;
parser_line_counter_t debugger_column = context_p->token.column;
#endif /* JERRY_DEBUGGER */
bool is_generator_function = false;
if (lexer_consume_generator (context_p))
{
if (is_single_statement)
{
parser_raise_error (context_p, PARSER_ERR_GENERATOR_IN_SINGLE_STATEMENT_POS);
}
is_generator_function = true;
}
lexer_expect_identifier (context_p, LEXER_NEW_IDENT_LITERAL);
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL);
if (context_p->next_scanner_info_p->source_p == context_p->source_p
&& context_p->next_scanner_info_p->type == SCANNER_TYPE_ERR_REDECLARED)
{
parser_raise_error (context_p, PARSER_ERR_VARIABLE_REDECLARED);
}
uint16_t function_name_index = context_p->lit_object.index;
#if JERRY_MODULE_SYSTEM
parser_module_append_export_name (context_p);
context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT);
#endif /* JERRY_MODULE_SYSTEM */
uint32_t status_flags = PARSER_FUNCTION_CLOSURE;
if (context_p->token.keyword_type >= LEXER_FIRST_NON_STRICT_ARGUMENTS)
{
status_flags |= PARSER_HAS_NON_STRICT_ARG;
}
if (is_generator_function)
{
status_flags |= PARSER_IS_GENERATOR_FUNCTION | PARSER_DISALLOW_AWAIT_YIELD;
}
if (context_p->next_scanner_info_p->u8_arg & SCANNER_FUNCTION_ASYNC)
{
status_flags |= PARSER_IS_ASYNC_FUNCTION | PARSER_DISALLOW_AWAIT_YIELD;
}
#if JERRY_DEBUGGER
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
lexer_literal_t *name_p = context_p->lit_object.literal_p;
jerry_debugger_send_string (JERRY_DEBUGGER_FUNCTION_NAME,
JERRY_DEBUGGER_NO_SUBTYPE,
name_p->u.char_p,
name_p->prop.length);
/* Reset token position for the function. */
context_p->token.line = debugger_line;
context_p->token.column = debugger_column;
}
#endif /* JERRY_DEBUGGER */
JERRY_ASSERT (context_p->scope_stack_top >= 2);
parser_scope_stack_t *scope_stack_p = context_p->scope_stack_p + context_p->scope_stack_top - 2;
uint16_t literal_index = context_p->lit_object.index;
while (literal_index != scope_stack_p->map_from)
{
scope_stack_p--;
JERRY_ASSERT (scope_stack_p >= context_p->scope_stack_p);
}
JERRY_ASSERT (scope_stack_p[1].map_from == PARSER_SCOPE_STACK_FUNC);
if (!(context_p->status_flags & PARSER_IS_STRICT)
&& (scope_stack_p >= context_p->scope_stack_p + context_p->scope_stack_global_end))
{
bool copy_value = true;
parser_scope_stack_t *stack_p = context_p->scope_stack_p;
while (stack_p < scope_stack_p)
{
if (literal_index == stack_p->map_from && (stack_p->map_to & PARSER_SCOPE_STACK_NO_FUNCTION_COPY))
{
copy_value = false;
break;
}
stack_p++;
}
if (copy_value)
{
stack_p = context_p->scope_stack_p;
while (stack_p < scope_stack_p)
{
if (literal_index == stack_p->map_from)
{
JERRY_ASSERT (!(stack_p->map_to & PARSER_SCOPE_STACK_NO_FUNCTION_COPY));
uint16_t map_to = scanner_decode_map_to (stack_p);
uint16_t opcode = ((map_to >= PARSER_REGISTER_START) ? CBC_ASSIGN_LITERAL_SET_IDENT : CBC_COPY_TO_GLOBAL);
parser_emit_cbc_literal_value (context_p, opcode, scanner_decode_map_to (scope_stack_p), map_to);
break;
}
stack_p++;
}
parser_flush_cbc (context_p);
}
if (JERRY_UNLIKELY (context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_SCOPE
|| context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_CONTEXT))
{
parser_pop_block_context (context_p);
}
}
lexer_literal_t *literal_p = PARSER_GET_LITERAL ((size_t) scope_stack_p[1].map_to);
JERRY_ASSERT ((literal_p->type == LEXER_UNUSED_LITERAL || literal_p->type == LEXER_FUNCTION_LITERAL)
&& literal_p->status_flags == 0);
ecma_compiled_code_t *compiled_code_p = parser_parse_function (context_p, status_flags);
if (literal_p->type == LEXER_FUNCTION_LITERAL)
{
ecma_bytecode_deref (literal_p->u.bytecode_p);
}
literal_p->u.bytecode_p = compiled_code_p;
literal_p->type = LEXER_FUNCTION_LITERAL;
parser_compiled_code_set_function_name (context_p, compiled_code_p, function_name_index, 0);
lexer_next_token (context_p);
} /* parser_parse_function_statement */
/**
* Parse if statement (starting part).
*/
static void
parser_parse_if_statement_start (parser_context_t *context_p) /**< context */
{
parser_if_else_statement_t if_statement;
parser_parse_enclosed_expr (context_p);
parser_emit_cbc_forward_branch (context_p, CBC_BRANCH_IF_FALSE_FORWARD, &if_statement.branch);
parser_stack_push (context_p, &if_statement, sizeof (parser_if_else_statement_t));
parser_stack_push_uint8 (context_p, PARSER_STATEMENT_IF);
parser_stack_iterator_init (context_p, &context_p->last_statement);
} /* parser_parse_if_statement_start */
/**
* Parse if statement (ending part).
*
* @return true - if parsing an 'else' statement
* false - otherwise
*/
static bool
parser_parse_if_statement_end (parser_context_t *context_p) /**< context */
{
parser_if_else_statement_t if_statement;
parser_if_else_statement_t else_statement;
parser_stack_iterator_t iterator;
JERRY_ASSERT (context_p->stack_top_uint8 == PARSER_STATEMENT_IF);
if (context_p->token.type != LEXER_KEYW_ELSE)
{
parser_stack_pop_uint8 (context_p);
parser_stack_pop (context_p, &if_statement, sizeof (parser_if_else_statement_t));
parser_stack_iterator_init (context_p, &context_p->last_statement);
parser_set_branch_to_current_position (context_p, &if_statement.branch);
return false;
}
parser_stack_change_last_uint8 (context_p, PARSER_STATEMENT_ELSE);
parser_stack_iterator_init (context_p, &iterator);
parser_stack_iterator_skip (&iterator, 1);
parser_stack_iterator_read (&iterator, &if_statement, sizeof (parser_if_else_statement_t));
parser_emit_cbc_forward_branch (context_p, CBC_JUMP_FORWARD, &else_statement.branch);
parser_set_branch_to_current_position (context_p, &if_statement.branch);
parser_stack_iterator_write (&iterator, &else_statement, sizeof (parser_if_else_statement_t));
lexer_next_token (context_p);
return true;
} /* parser_parse_if_statement_end */
/**
* Parse with statement (starting part).
*/
static void
parser_parse_with_statement_start (parser_context_t *context_p) /**< context */
{
parser_with_statement_t with_statement;
if (context_p->status_flags & PARSER_IS_STRICT)
{
parser_raise_error (context_p, PARSER_ERR_WITH_NOT_ALLOWED);
}
parser_parse_enclosed_expr (context_p);
#ifndef JERRY_NDEBUG
PARSER_PLUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_WITH_CONTEXT_STACK_ALLOCATION);
#endif /* !JERRY_NDEBUG */
uint8_t inside_with = (context_p->status_flags & PARSER_INSIDE_WITH) != 0;
context_p->status_flags |= PARSER_INSIDE_WITH;
parser_emit_cbc_ext_forward_branch (context_p, CBC_EXT_WITH_CREATE_CONTEXT, &with_statement.branch);
parser_stack_push (context_p, &with_statement, sizeof (parser_with_statement_t));
parser_stack_push_uint8 (context_p, inside_with);
parser_stack_push_uint8 (context_p, PARSER_STATEMENT_WITH);
parser_stack_iterator_init (context_p, &context_p->last_statement);
} /* parser_parse_with_statement_start */
/**
* Parse with statement (ending part).
*/
static void
parser_parse_with_statement_end (parser_context_t *context_p) /**< context */
{
parser_with_statement_t with_statement;
JERRY_ASSERT (context_p->status_flags & PARSER_INSIDE_WITH);
parser_stack_pop_uint8 (context_p);
if (!context_p->stack_top_uint8)
{
context_p->status_flags &= (uint32_t) ~PARSER_INSIDE_WITH;
}
parser_stack_pop_uint8 (context_p);
parser_stack_pop (context_p, &with_statement, sizeof (parser_with_statement_t));
parser_stack_iterator_init (context_p, &context_p->last_statement);
parser_flush_cbc (context_p);
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_WITH_CONTEXT_STACK_ALLOCATION);
#ifndef JERRY_NDEBUG
PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_WITH_CONTEXT_STACK_ALLOCATION);
#endif /* !JERRY_NDEBUG */
parser_emit_cbc (context_p, CBC_CONTEXT_END);
parser_set_branch_to_current_position (context_p, &with_statement.branch);
} /* parser_parse_with_statement_end */
/**
* Parse do-while statement (ending part).
*/
static void
parser_parse_do_while_statement_end (parser_context_t *context_p) /**< context */
{
parser_loop_statement_t loop;
JERRY_ASSERT (context_p->stack_top_uint8 == PARSER_STATEMENT_DO_WHILE);
if (context_p->token.type != LEXER_KEYW_WHILE)
{
parser_raise_error (context_p, PARSER_ERR_WHILE_EXPECTED);
}
parser_stack_iterator_t iterator;
parser_stack_iterator_init (context_p, &iterator);
parser_stack_iterator_skip (&iterator, 1);
parser_stack_iterator_read (&iterator, &loop, sizeof (parser_loop_statement_t));
parser_set_continues_to_current_position (context_p, loop.branch_list_p);
JERRY_ASSERT (context_p->next_scanner_info_p->source_p != context_p->source_p);
parser_parse_enclosed_expr (context_p);
if (context_p->last_cbc_opcode != CBC_PUSH_FALSE)
{
cbc_opcode_t opcode = CBC_BRANCH_IF_TRUE_BACKWARD;
if (context_p->last_cbc_opcode == CBC_LOGICAL_NOT)
{
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
opcode = CBC_BRANCH_IF_FALSE_BACKWARD;
}
else if (context_p->last_cbc_opcode == CBC_PUSH_TRUE)
{
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
opcode = CBC_JUMP_BACKWARD;
}
parser_do_while_statement_t do_while_statement;
parser_stack_iterator_skip (&iterator, sizeof (parser_loop_statement_t));
parser_stack_iterator_read (&iterator, &do_while_statement, sizeof (parser_do_while_statement_t));
parser_emit_cbc_backward_branch (context_p, (uint16_t) opcode, do_while_statement.start_offset);
}
else
{
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
}
parser_stack_pop (context_p, NULL, 1 + sizeof (parser_loop_statement_t) + sizeof (parser_do_while_statement_t));
parser_stack_iterator_init (context_p, &context_p->last_statement);
parser_set_breaks_to_current_position (context_p, loop.branch_list_p);
} /* parser_parse_do_while_statement_end */
/**
* Parse while statement (starting part).
*/
static void
parser_parse_while_statement_start (parser_context_t *context_p) /**< context */
{
parser_while_statement_t while_statement;
parser_loop_statement_t loop;
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_WHILE);
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
JERRY_ASSERT (context_p->next_scanner_info_p->source_p != context_p->source_p
|| context_p->next_scanner_info_p->type == SCANNER_TYPE_WHILE);
if (context_p->next_scanner_info_p->source_p != context_p->source_p)
{
/* The prescanner couldn't find the end of the while condition. */
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR);
JERRY_ASSERT (context_p->token.type != LEXER_RIGHT_PAREN);
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
}
parser_emit_cbc_forward_branch (context_p, CBC_JUMP_FORWARD, &while_statement.branch);
JERRY_ASSERT (context_p->last_cbc_opcode == PARSER_CBC_UNAVAILABLE);
while_statement.start_offset = context_p->byte_code_size;
scanner_get_location (&while_statement.condition_location, context_p);
scanner_set_location (context_p, &((scanner_location_info_t *) context_p->next_scanner_info_p)->location);
scanner_release_next (context_p, sizeof (scanner_location_info_t));
scanner_seek (context_p);
lexer_next_token (context_p);
loop.branch_list_p = NULL;
parser_stack_push (context_p, &while_statement, sizeof (parser_while_statement_t));
parser_stack_push (context_p, &loop, sizeof (parser_loop_statement_t));
parser_stack_push_uint8 (context_p, PARSER_STATEMENT_WHILE);
parser_stack_iterator_init (context_p, &context_p->last_statement);
} /* parser_parse_while_statement_start */
/**
* Parse while statement (ending part).
*/
static void JERRY_ATTR_NOINLINE