forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
12377 lines (10964 loc) · 414 KB
/
parse.cpp
File metadata and controls
12377 lines (10964 loc) · 414 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 (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
#include "FormalsUtil.h"
#include "..\Runtime\Language\SourceDynamicProfileManager.h"
#if DBG_DUMP
void PrintPnodeWIndent(ParseNode *pnode,int indentAmt);
#endif
const char* const nopNames[knopLim]= {
#define PTNODE(nop,sn,pc,nk,grfnop,json) sn,
#include "ptlist.h"
};
void printNop(int nop) {
printf("%s\n",nopNames[nop]);
}
const uint ParseNode::mpnopgrfnop[knopLim] =
{
#define PTNODE(nop,sn,pc,nk,grfnop,json) grfnop,
#include "ptlist.h"
};
bool Parser::BindDeferredPidRefs() const
{
return m_scriptContext->GetConfig()->BindDeferredPidRefs();
}
bool Parser::IsES6DestructuringEnabled() const
{
return m_scriptContext->GetConfig()->IsES6DestructuringEnabled();
}
struct DeferredFunctionStub
{
RestorePoint restorePoint;
uint fncFlags;
uint nestedCount;
DeferredFunctionStub *deferredStubs;
#if DEBUG
charcount_t ichMin;
#endif
};
struct StmtNest
{
union
{
struct
{
ParseNodePtr pnodeStmt; // This statement node.
ParseNodePtr pnodeLab; // Labels for this statement.
};
struct
{
bool isDeferred : 1;
OpCode op; // This statement operation.
LabelId* pLabelId; // Labels for this statement.
};
};
StmtNest *pstmtOuter; // Enclosing statement.
};
struct BlockInfoStack
{
StmtNest pstmt;
ParseNode *pnodeBlock;
ParseNodePtr *m_ppnodeLex; // lexical variable list tail
BlockInfoStack *pBlockInfoOuter; // containing block's BlockInfoStack
BlockInfoStack *pBlockInfoFunction; // nearest function's BlockInfoStack (if pnodeBlock is a function, this points to itself)
};
#if DEBUG
Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground, size_t size)
#else
Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground)
#endif
: m_nodeAllocator(L"Parser", alloc ? alloc : scriptContext->GetThreadContext()->GetPageAllocator(), Parser::OutOfMemory),
// use the GuestArena directly for keeping the RegexPattern* alive during byte code generation
m_registeredRegexPatterns(scriptContext->GetGuestArena())
{
AssertMsg(size == sizeof(Parser), "verify conditionals affecting the size of Parser agree");
Assert(scriptContext != nullptr);
m_isInBackground = isBackground;
m_phtbl = nullptr;
m_pscan = nullptr;
m_deferringAST = FALSE;
m_stoppedDeferredParse = FALSE;
m_hasParallelJob = false;
m_doingFastScan = false;
m_scriptContext = scriptContext;
m_pCurrentAstSize = nullptr;
m_parsingDuplicate = 0;
m_arrayDepth = 0;
m_funcInArrayDepth = 0;
m_parenDepth = 0;
m_funcInArray = 0;
m_tryCatchOrFinallyDepth = 0;
m_UsesArgumentsAtGlobal = false;
m_currentNodeFunc = nullptr;
m_currentNodeDeferredFunc = nullptr;
m_currentNodeNonLambdaFunc = nullptr;
m_currentNodeNonLambdaDeferredFunc = nullptr;
m_currentNodeProg = nullptr;
m_currDeferredStub = nullptr;
m_pstmtCur = nullptr;
m_currentBlockInfo = nullptr;
m_currentScope = nullptr;
m_currentDynamicBlock = nullptr;
m_catchPidRefList = nullptr;
m_grfscr = fscrNil;
m_length = 0;
m_originalLength = 0;
m_nextFunctionId = nullptr;
m_errorCallback = nullptr;
m_uncertainStructure = FALSE;
currBackgroundParseItem = nullptr;
backgroundParseItems = nullptr;
fastScannedRegExpNodes = nullptr;
m_fUseStrictMode = strictMode;
m_InAsmMode = false;
m_deferAsmJs = true;
m_scopeCountNoAst = 0;
m_fExpectExternalSource = 0;
m_parseType = ParseType_Upfront;
m_deferEllipsisError = false;
m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperDisallowed;
}
Parser::~Parser(void)
{
if (m_scriptContext == nullptr || m_scriptContext->GetGuestArena() == nullptr)
{
// If the scriptContext or guestArena have gone away, there is no point clearing each item of this list.
// Just reset it so that destructor of the SList will be no-op
m_registeredRegexPatterns.Reset();
}
if (this->m_hasParallelJob)
{
#if ENABLE_BACKGROUND_PARSING
// Let the background threads know that they can decommit their arena pages.
BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
Assert(bgp);
if (bgp->Processor()->ProcessesInBackground())
{
JsUtil::BackgroundJobProcessor *processor = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor());
bool result = processor->IterateBackgroundThreads([&](JsUtil::ParallelThreadData *threadData)->bool {
threadData->canDecommit = true;
return false;
});
Assert(result);
}
#endif
}
Release();
}
void Parser::OutOfMemory()
{
throw ParseExceptionObject(ERRnoMemory);
}
void Parser::Error(HRESULT hr)
{
Assert(FAILED(hr));
m_err.Throw(hr);
}
void Parser::Error(HRESULT hr, ParseNodePtr pnode)
{
if (pnode && pnode->ichLim)
{
Error(hr, pnode->ichMin, pnode->ichLim);
}
else
{
Error(hr);
}
}
void Parser::Error(HRESULT hr, charcount_t ichMin, charcount_t ichLim)
{
m_pscan->SetErrorPosition(ichMin, ichLim);
Error(hr);
}
void Parser::IdentifierExpectedError(const Token& token)
{
Assert(token.tk != tkID);
HRESULT hr;
if (token.IsReservedWord())
{
if (token.IsKeyword())
{
hr = ERRKeywordNotId;
}
else
{
Assert(token.IsFutureReservedWord(true));
if (token.IsFutureReservedWord(false))
{
// Future reserved word in strict and non-strict modes
hr = ERRFutureReservedWordNotId;
}
else
{
// Future reserved word only in strict mode. The token would have been converted to tkID by the scanner if not
// in strict mode.
Assert(IsStrictMode());
hr = ERRFutureReservedWordInStrictModeNotId;
}
}
}
else
{
hr = ERRnoIdent;
}
Error(hr);
}
CatchPidRefList *Parser::EnsureCatchPidRefList()
{
if (this->m_catchPidRefList == nullptr)
{
this->m_catchPidRefList = Anew(&m_nodeAllocator, CatchPidRefList);
}
return this->m_catchPidRefList;
}
HRESULT Parser::ValidateSyntax(LPCUTF8 pszSrc, size_t encodedCharCount, bool isGenerator, CompileScriptException *pse, void (Parser::*validateFunction)())
{
AssertPsz(pszSrc);
AssertMemN(pse);
if (this->IsBackgroundParser())
{
PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
}
else
{
PROBE_STACK(m_scriptContext, Js::Constants::MinStackDefault);
}
HRESULT hr;
SmartFPUControl smartFpuControl;
DebugOnly( m_err.fInited = TRUE; )
BOOL fDeferSave = m_deferringAST;
try
{
hr = NOERROR;
this->PrepareScanner(false);
m_length = encodedCharCount;
m_originalLength = encodedCharCount;
// make sure deferred parsing is turned off
ULONG grfscr = fscrNil;
// Give the scanner the source and get the first token
m_pscan->SetText(pszSrc, 0, encodedCharCount, 0, grfscr);
m_pscan->SetYieldIsKeyword(isGenerator);
m_pscan->Scan();
uint nestedCount = 0;
m_pnestedCount = &nestedCount;
ParseNodePtr pnodeScope = nullptr;
m_ppnodeScope = &pnodeScope;
m_ppnodeExprScope = nullptr;
uint nextFunctionId = 0;
m_nextFunctionId = &nextFunctionId;
m_inDeferredNestedFunc = false;
m_deferringAST = true;
m_nextBlockId = 0;
if (this->BindDeferredPidRefs())
{
ParseNode *pnodeFnc = CreateNode(knopFncDecl);
pnodeFnc->sxFnc.ClearFlags();
pnodeFnc->sxFnc.SetDeclaration(false);
pnodeFnc->sxFnc.astSize = 0;
pnodeFnc->sxFnc.pnodeVars = nullptr;
pnodeFnc->sxFnc.pnodeArgs = nullptr;
pnodeFnc->sxFnc.pnodeBody = nullptr;
pnodeFnc->sxFnc.pnodeName = nullptr;
pnodeFnc->sxFnc.pnodeRest = nullptr;
pnodeFnc->sxFnc.deferredStub = nullptr;
pnodeFnc->sxFnc.SetIsGenerator(isGenerator);
m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
m_currentNodeFunc = pnodeFnc;
m_currentNodeDeferredFunc = NULL;
AssertMsg(m_pstmtCur == NULL, "Statement stack should be empty when we start parse function body");
ParseNodePtr block = StartParseBlock<false>(PnodeBlockType::Function, ScopeType_FunctionBody);
(this->*validateFunction)();
FinishParseBlock(block);
pnodeFnc->ichLim = m_pscan->IchLimTok();
pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
pnodeFnc->sxFnc.pnodeVars = nullptr;
if (m_asgToConst)
{
Error(ERRAssignmentToConst, m_asgToConst.GetIchMin(), m_asgToConst.GetIchLim());
}
}
else
{
(this->*validateFunction)();
}
// there should be nothing after successful parsing for a given construct
if (m_token.tk != tkEOF)
Error(ERRsyntax);
RELEASEPTR(m_pscan);
m_deferringAST = fDeferSave;
}
catch(ParseExceptionObject& e)
{
m_deferringAST = fDeferSave;
m_err.m_hr = e.GetError();
hr = pse->ProcessError( m_pscan, m_err.m_hr, /* pnodeBase */ NULL);
}
return hr;
}
HRESULT Parser::ParseSourceInternal(
__out ParseNodePtr* parseTree, LPCUTF8 pszSrc, size_t offsetInBytes, size_t encodedCharCount, charcount_t offsetInChars,
bool fromExternal, ULONG grfscr, CompileScriptException *pse, Js::LocalFunctionId * nextFunctionId, ULONG lineNumber, SourceContextInfo * sourceContextInfo)
{
AssertMem(parseTree);
AssertPsz(pszSrc);
AssertMemN(pse);
double startTime = m_scriptContext->GetThreadContext()->ParserTelemetry.Now();
if (this->IsBackgroundParser())
{
PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
}
else
{
PROBE_STACK(m_scriptContext, Js::Constants::MinStackDefault);
}
#ifdef PROFILE_EXEC
m_scriptContext->ProfileBegin(Js::ParsePhase);
#endif
JS_ETW(EventWriteJSCRIPT_PARSE_START(m_scriptContext,0));
*parseTree = NULL;
m_sourceLim = 0;
m_grfscr = grfscr;
m_sourceContextInfo = sourceContextInfo;
ParseNodePtr pnodeBase = NULL;
HRESULT hr;
SmartFPUControl smartFpuControl;
DebugOnly( m_err.fInited = TRUE; )
try
{
this->PrepareScanner(fromExternal);
if ((grfscr & fscrEvalCode) != 0)
{
// This makes the parser to believe when eval() is called, it accept any super access in global scope.
this->m_parsingSuperRestrictionState = Parser::ParsingSuperRestrictionState_SuperCallAndPropertyAllowed;
}
// parse the source
pnodeBase = Parse(pszSrc, offsetInBytes, encodedCharCount, offsetInChars, grfscr, lineNumber, nextFunctionId, pse);
AssertNodeMem(pnodeBase);
// Record the actual number of words parsed.
m_sourceLim = pnodeBase->ichLim - offsetInChars;
// TODO: The assert can be false positive in some scenarios and chuckj to fix it later
// Assert(utf8::ByteIndexIntoCharacterIndex(pszSrc + offsetInBytes, encodedCharCount, fromExternal ? utf8::doDefault : utf8::doAllowThreeByteSurrogates) == m_sourceLim);
#if DBG_DUMP
if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::ParsePhase))
{
PrintPnodeWIndent(pnodeBase,4);
fflush(stdout);
}
#endif
*parseTree = pnodeBase;
hr = NOERROR;
}
catch(ParseExceptionObject& e)
{
m_err.m_hr = e.GetError();
hr = pse->ProcessError( m_pscan, m_err.m_hr, pnodeBase);
}
if (this->m_hasParallelJob)
{
#if ENABLE_BACKGROUND_PARSING
///// Wait here for remaining jobs to finish. Then look for errors, do final const bindings.
// pleath TODO: If there are remaining jobs, let the main thread help finish them.
BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
Assert(bgp);
CompileScriptException se;
this->WaitForBackgroundJobs(bgp, &se);
BackgroundParseItem *failedItem = bgp->GetFailedBackgroundParseItem();
if (failedItem)
{
CompileScriptException *bgPse = failedItem->GetPSE();
Assert(bgPse);
*pse = *bgPse;
hr = failedItem->GetHR();
bgp->SetFailedBackgroundParseItem(nullptr);
}
if (this->fastScannedRegExpNodes != nullptr)
{
this->FinishBackgroundRegExpNodes();
}
for (BackgroundParseItem *item = this->backgroundParseItems; item; item = item->GetNext())
{
Parser *parser = item->GetParser();
parser->FinishBackgroundPidRefs(item, this != parser);
}
#endif
}
// done with the scanner
RELEASEPTR(m_pscan);
#ifdef PROFILE_EXEC
m_scriptContext->ProfileEnd(Js::ParsePhase);
#endif
JS_ETW(EventWriteJSCRIPT_PARSE_STOP(m_scriptContext, 0));
ThreadContext *threadContext = m_scriptContext->GetThreadContext();
threadContext->ParserTelemetry.LogTime(threadContext->ParserTelemetry.Now() - startTime);
return hr;
}
#if ENABLE_BACKGROUND_PARSING
void Parser::WaitForBackgroundJobs(BackgroundParser *bgp, CompileScriptException *pse)
{
// The scan of the script is done, but there may be unfinished background jobs in the queue.
// Enlist the main thread to help with those.
BackgroundParseItem *item;
if (!*bgp->GetPendingBackgroundItemsPtr())
{
// We're done.
return;
}
// Save parser state, since we'll need to restore it in order to bind references correctly later.
this->m_isInBackground = true;
this->SetCurrBackgroundParseItem(nullptr);
uint blockIdSave = this->m_nextBlockId;
uint functionIdSave = *this->m_nextFunctionId;
StmtNest *pstmtSave = this->m_pstmtCur;
if (!bgp->Processor()->ProcessesInBackground())
{
// No background thread. Just walk the jobs with no locking and process them.
for (item = bgp->GetNextUnprocessedItem(); item; item = bgp->GetNextUnprocessedItem())
{
bgp->Processor()->RemoveJob(item);
bool succeeded = bgp->Process(item, this, pse);
bgp->JobProcessed(item, succeeded);
}
Assert(!*bgp->GetPendingBackgroundItemsPtr());
}
else
{
// Background threads. We need to have the critical section in order to:
// - Check for unprocessed jobs;
// - Remove jobs from the processor queue;
// - Do JobsProcessed work (such as removing jobs from the BackgroundParser's unprocessed list).
CriticalSection *pcs = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor())->GetCriticalSection();
pcs->Enter();
for (;;)
{
// Grab a job (in lock)
item = bgp->GetNextUnprocessedItem();
if (item == nullptr)
{
break;
}
bgp->Processor()->RemoveJob(item);
pcs->Leave();
// Process job (if there is one) (outside lock)
bool succeeded = bgp->Process(item, this, pse);
pcs->Enter();
bgp->JobProcessed(item, succeeded);
}
pcs->Leave();
// Wait for the background threads to finish jobs they're already processing (if any).
// TODO: Replace with a proper semaphore.
while(*bgp->GetPendingBackgroundItemsPtr());
}
Assert(!*bgp->GetPendingBackgroundItemsPtr());
// Restore parser state.
this->m_pstmtCur = pstmtSave;
this->m_isInBackground = false;
this->m_nextBlockId = blockIdSave;
*this->m_nextFunctionId = functionIdSave;
}
void Parser::FinishBackgroundPidRefs(BackgroundParseItem *item, bool isOtherParser)
{
for (BlockInfoStack *blockInfo = item->GetParseContext()->currentBlockInfo; blockInfo; blockInfo = blockInfo->pBlockInfoOuter)
{
if (isOtherParser)
{
this->BindPidRefs<true>(blockInfo, item->GetMaxBlockId());
}
else
{
this->BindPidRefs<false>(blockInfo, item->GetMaxBlockId());
}
}
}
void Parser::FinishBackgroundRegExpNodes()
{
// We have a list of RegExp nodes that we saw on the UI thread in functions we're parallel parsing,
// and for each background job we have a list of RegExp nodes for which we couldn't allocate patterns.
// We need to copy the pattern pointers from the UI thread nodes to the corresponding nodes on the
// background nodes.
// There may be UI thread nodes for which there are no background thread equivalents, because the UI thread
// has to assume that the background thread won't defer anything.
// Note that because these lists (and the list of background jobs) are SList's built by prepending, they are
// all in reverse lexical order.
Assert(!this->IsBackgroundParser());
Assert(this->fastScannedRegExpNodes);
Assert(this->backgroundParseItems != nullptr);
BackgroundParseItem *currBackgroundItem;
#if DBG
for (currBackgroundItem = this->backgroundParseItems;
currBackgroundItem;
currBackgroundItem = currBackgroundItem->GetNext())
{
if (currBackgroundItem->RegExpNodeList())
{
FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
{
Assert(pnode->sxPid.regexPattern == nullptr);
}
NEXT_DLIST_ENTRY;
}
}
#endif
// Hook up the patterns allocated on the main thread to the nodes created on the background thread.
// Walk the list of foreground nodes, advancing through the work items and looking up each item.
// Note that the background thread may have chosen to defer a given RegEx literal, so not every foreground
// node will have a matching background node. Doesn't matter for correctness.
// (It's inefficient, of course, to have to restart the inner loop from the beginning of the work item's
// list, but it should be unusual to have many RegExes in a single work item's chunk of code. Figure out how
// to start the inner loop from a known internal node within the list if that turns out to be important.)
currBackgroundItem = this->backgroundParseItems;
FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeFgnd, this->fastScannedRegExpNodes)
{
Assert(pnodeFgnd->nop == knopRegExp);
Assert(pnodeFgnd->sxPid.regexPattern != nullptr);
bool quit = false;
while (!quit)
{
// Find the next work item with a RegEx in it.
while (currBackgroundItem && currBackgroundItem->RegExpNodeList() == nullptr)
{
currBackgroundItem = currBackgroundItem->GetNext();
}
if (!currBackgroundItem)
{
break;
}
// Walk the RegExps in the work item.
FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeBgnd, currBackgroundItem->RegExpNodeList())
{
Assert(pnodeBgnd->nop == knopRegExp);
if (pnodeFgnd->ichMin <= pnodeBgnd->ichMin)
{
// Either we found a match, or the next background node is past the foreground node.
// In any case, we can stop searching.
if (pnodeFgnd->ichMin == pnodeBgnd->ichMin)
{
Assert(pnodeFgnd->ichLim == pnodeBgnd->ichLim);
pnodeBgnd->sxPid.regexPattern = pnodeFgnd->sxPid.regexPattern;
}
quit = true;
break;
}
}
NEXT_DLIST_ENTRY;
if (!quit)
{
// Need to advance to the next work item.
currBackgroundItem = currBackgroundItem->GetNext();
}
}
}
NEXT_DLIST_ENTRY;
#if DBG
for (currBackgroundItem = this->backgroundParseItems;
currBackgroundItem;
currBackgroundItem = currBackgroundItem->GetNext())
{
if (currBackgroundItem->RegExpNodeList())
{
FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
{
Assert(pnode->sxPid.regexPattern != nullptr);
}
NEXT_DLIST_ENTRY;
}
}
#endif
}
#endif
LabelId* Parser::CreateLabelId(IdentToken* pToken)
{
LabelId* pLabelId;
pLabelId = (LabelId*)m_nodeAllocator.Alloc(sizeof(LabelId));
if (NULL == pLabelId)
Error(ERRnoMemory);
pLabelId->pid = pToken->pid;
pLabelId->next = NULL;
return pLabelId;
}
/*****************************************************************************
The following set of routines allocate parse tree nodes of various kinds.
They catch an exception on out of memory.
*****************************************************************************/
static const int g_mpnopcbNode[] =
{
#define PTNODE(nop,sn,pc,nk,ok,json) kcbPn##nk,
#include "ptlist.h"
};
const Js::RegSlot NoRegister = (Js::RegSlot)-1;
const Js::RegSlot OneByteRegister = (Js::RegSlot_OneByte)-1;
void Parser::InitNode(OpCode nop,ParseNodePtr pnode) {
pnode->nop = nop;
pnode->grfpn = PNodeFlags::fpnNone;
pnode->location = NoRegister;
pnode->emitLabels = false;
pnode->isUsed = true;
pnode->notEscapedUse = false;
pnode->isInList = false;
pnode->isCallApplyTargetLoad = false;
}
// Create nodes using Arena
template <OpCode nop>
ParseNodePtr Parser::StaticCreateNodeT(ArenaAllocator* alloc, charcount_t ichMin, charcount_t ichLim)
{
ParseNodePtr pnode = StaticAllocNode<nop>(alloc);
InitNode(nop,pnode);
// default - may be changed
pnode->ichMin = ichMin;
pnode->ichLim = ichLim;
return pnode;
}
ParseNodePtr
Parser::StaticCreateBlockNode(ArenaAllocator* alloc, charcount_t ichMin , charcount_t ichLim, int blockId, PnodeBlockType blockType)
{
ParseNodePtr pnode = StaticCreateNodeT<knopBlock>(alloc, ichMin, ichLim);
InitBlockNode(pnode, blockId, blockType);
return pnode;
}
void Parser::InitBlockNode(ParseNodePtr pnode, int blockId, PnodeBlockType blockType)
{
Assert(pnode->nop == knopBlock);
pnode->sxBlock.pnodeScopes = nullptr;
pnode->sxBlock.pnodeNext = nullptr;
pnode->sxBlock.scope = nullptr;
pnode->sxBlock.enclosingBlock = nullptr;
pnode->sxBlock.pnodeLexVars = nullptr;
pnode->sxBlock.pnodeStmt = nullptr;
pnode->sxBlock.pnodeLastValStmt = nullptr;
pnode->sxBlock.callsEval = false;
pnode->sxBlock.childCallsEval = false;
pnode->sxBlock.blockType = blockType;
pnode->sxBlock.blockId = blockId;
if (blockType != PnodeBlockType::Regular)
{
pnode->grfpn |= PNodeFlags::fpnSyntheticNode;
}
}
// Create Node with limit
template <OpCode nop>
ParseNodePtr Parser::CreateNodeT(charcount_t ichMin,charcount_t ichLim)
{
Assert(!this->m_deferringAST);
ParseNodePtr pnode = StaticCreateNodeT<nop>(&m_nodeAllocator, ichMin, ichLim);
Assert(m_pCurrentAstSize != NULL);
*m_pCurrentAstSize += GetNodeSize<nop>();
return pnode;
}
ParseNodePtr Parser::CreateDeclNode(OpCode nop, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
{
ParseNodePtr pnode = CreateNode(nop);
pnode->sxVar.InitDeclNode(pid, NULL);
if (symbolType != STUnknown)
{
pnode->sxVar.sym = AddDeclForPid(pnode, pid, symbolType, errorOnRedecl);
}
return pnode;
}
Symbol* Parser::AddDeclForPid(ParseNodePtr pnode, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
{
Assert(pnode->IsVarLetOrConst());
PidRefStack *refForUse = nullptr, *refForDecl = nullptr;
BlockInfoStack *blockInfo;
bool fBlockScope = false;
if (m_scriptContext->GetConfig()->IsBlockScopeEnabled() &&
(pnode->nop != knopVarDecl || symbolType == STFunction))
{
Assert(m_pstmtCur);
if (m_pstmtCur->isDeferred)
{
// Deferred parsing: there's no pnodeStmt node, only an opcode on the Stmt struct.
if (m_pstmtCur->op != knopBlock)
{
// Let/const declared in a bare statement context.
Error(ERRDeclOutOfStmt);
}
if (m_pstmtCur->pstmtOuter && m_pstmtCur->pstmtOuter->op == knopSwitch)
{
// Let/const declared inside a switch block (requiring conservative use-before-decl check).
pnode->sxVar.isSwitchStmtDecl = true;
}
}
else
{
if (m_pstmtCur->pnodeStmt->nop != knopBlock)
{
// Let/const declared in a bare statement context.
Error(ERRDeclOutOfStmt);
}
if (m_pstmtCur->pstmtOuter && m_pstmtCur->pstmtOuter->pnodeStmt->nop == knopSwitch)
{
// Let/const declared inside a switch block (requiring conservative use-before-decl check).
pnode->sxVar.isSwitchStmtDecl = true;
}
}
fBlockScope = pnode->nop != knopVarDecl ||
(
!GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope ||
GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope->GetScopeType() != ScopeType_GlobalEvalBlock
);
}
if (fBlockScope)
{
blockInfo = GetCurrentBlockInfo();
}
else
{
blockInfo = GetCurrentFunctionBlockInfo();
}
// If we are creating an 'arguments' Sym at function block scope, create it in
// the parameter scope instead. That way, if we need to reuse the Sym for the
// actual arguments object at the end of the function, we don't need to move it
// into the parameter scope.
if (pid == wellKnownPropertyPids.arguments
&& pnode->nop == knopVarDecl
&& blockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Function
&& blockInfo->pBlockInfoOuter != nullptr
&& blockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter)
{
blockInfo = blockInfo->pBlockInfoOuter;
}
int maxScopeId = blockInfo->pnodeBlock->sxBlock.blockId;
// The body of catch may have let declared variable. In the case of pattern, found at catch parameter level,
// we need to search the duplication at that scope level as well - thus extending the scope lookup range.
if (IsES6DestructuringEnabled()
&& fBlockScope
&& blockInfo->pBlockInfoOuter != nullptr
&& blockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.scope != nullptr
&& blockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.scope->GetScopeType() == ScopeType_CatchParamPattern)
{
maxScopeId = blockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.blockId;
}
if (blockInfo->pnodeBlock->sxBlock.scope != nullptr && blockInfo->pnodeBlock->sxBlock.scope->GetScopeType() == ScopeType_FunctionBody)
{
// Check if there is a parameter scope and try to get it first.
BlockInfoStack *outerBlockInfo = blockInfo->pBlockInfoOuter;
if (outerBlockInfo != nullptr && outerBlockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter)
{
maxScopeId = outerBlockInfo->pnodeBlock->sxBlock.blockId;
}
}
refForDecl = this->FindOrAddPidRef(pid, blockInfo->pnodeBlock->sxBlock.blockId, maxScopeId);
if (refForDecl == nullptr)
{
Error(ERRnoMemory);
}
if (blockInfo == GetCurrentBlockInfo())
{
refForUse = refForDecl;
}
else
{
refForUse = this->PushPidRef(pid);
}
pnode->sxVar.symRef = refForUse->GetSymRef();
Symbol *sym = refForDecl->GetSym();
if (sym != nullptr)
{
// Multiple declarations in the same scope. 3 possibilities: error, existing one wins, new one wins.
switch (pnode->nop)
{
case knopLetDecl:
case knopConstDecl:
if (!sym->GetDecl()->sxVar.isBlockScopeFncDeclVar)
{
Assert(errorOnRedecl);
// Redeclaration error.
Error(ERRRedeclaration);
}
else
{
// (New) let/const hides the (old) var
sym->SetSymbolType(symbolType);
sym->SetDecl(pnode);
}
break;
case knopVarDecl:
if (sym->GetDecl() == nullptr)
{
Assert(symbolType == STFunction);
sym->SetDecl(pnode);
break;
}
switch (sym->GetDecl()->nop)
{
case knopLetDecl:
case knopConstDecl:
// Destructuring made possible to have the formals to be the let bind. But that shouldn't throw the error.
if (errorOnRedecl && (!IsES6DestructuringEnabled() || sym->GetSymbolType() != STFormal))
{
Error(ERRRedeclaration);
}
// If !errorOnRedecl, (old) let/const hides the (new) var, so do nothing.
break;
case knopVarDecl:
// Legal redeclaration. Who wins?
if (errorOnRedecl || sym->GetDecl()->sxVar.isBlockScopeFncDeclVar)
{
if (symbolType == STFormal ||
(symbolType == STFunction && sym->GetSymbolType() != STFormal) ||
sym->GetSymbolType() == STVariable)
{
// New decl wins.
sym->SetSymbolType(symbolType);
sym->SetDecl(pnode);
}
}
break;
}
break;
}
}
else
{
Scope *scope = blockInfo->pnodeBlock->sxBlock.scope;
if (scope == nullptr)
{
Assert(blockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Regular &&
m_scriptContext->GetConfig()->IsBlockScopeEnabled());
scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, ScopeType_Block);
blockInfo->pnodeBlock->sxBlock.scope = scope;
PushScope(scope);
}
if (scope->GetScopeType() == ScopeType_GlobalEvalBlock)
{
Assert(fBlockScope);
Assert(scope->GetEnclosingScope() == m_currentNodeProg->sxProg.scope);
// Check for same-named decl in Global scope.
PidRefStack *pidRefOld = pid->GetPidRefForScopeId(0);
if (pidRefOld && pidRefOld->GetSym())
{
Error(ERRRedeclaration);
}
}
else if (scope->GetScopeType() == ScopeType_Global && (this->m_grfscr & fscrEvalCode) &&
!(m_functionBody && m_functionBody->GetScopeInfo()))
{
// Check for same-named decl in GlobalEvalBlock scope. Note that this is not necessary
// if we're compiling a deferred nested function and the global scope was restored from cached info,
// because in that case we don't need a GlobalEvalScope.
Assert(!fBlockScope || (this->m_grfscr & fscrConsoleScopeEval) == fscrConsoleScopeEval);
PidRefStack *pidRefOld = pid->GetPidRefForScopeId(1);
if (pidRefOld && pidRefOld->GetSym())
{
Error(ERRRedeclaration);
}
}
if ((scope->GetScopeType() == ScopeType_FunctionBody || scope->GetScopeType() == ScopeType_Parameter) && symbolType != STFunction)
{
ParseNodePtr pnodeFnc = GetCurrentFunctionNode();
AnalysisAssert(pnodeFnc);
if (pnodeFnc->sxFnc.pnodeName &&
pnodeFnc->sxFnc.pnodeName->nop == knopVarDecl &&
pnodeFnc->sxFnc.pnodeName->sxVar.pid == pid)
{
// Named function expression has its name hidden by a local declaration.
// This is important to know if we don't know whether nested deferred functions refer to it,
// because if the name has a non-local reference then we have to create a scope object.
m_currentNodeFunc->sxFnc.SetNameIsHidden();
}
}
if (!sym)
{
const wchar_t *name = reinterpret_cast<const wchar_t*>(pid->Psz());
int nameLength = pid->Cch();
SymbolName const symName(name, nameLength);
Assert(!scope->FindLocalSymbol(symName));
sym = Anew(&m_nodeAllocator, Symbol, symName, pnode, symbolType);
scope->AddNewSymbol(sym);
sym->SetPid(pid);
}
refForDecl->SetSym(sym);
}
return sym;