forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.cpp
More file actions
903 lines (804 loc) · 35.4 KB
/
CodeBlock.cpp
File metadata and controls
903 lines (804 loc) · 35.4 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
/*
* Copyright (c) 2016-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "CodeBlock.h"
#include "parser/Script.h"
#include "runtime/Context.h"
#include "interpreter/ByteCode.h"
#include "runtime/Environment.h"
#include "runtime/EnvironmentRecord.h"
namespace Escargot {
void* NativeCodeBlock::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(NativeCodeBlock)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(NativeCodeBlock, m_context));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(NativeCodeBlock));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void* InterpretedCodeBlock::operator new(size_t size)
{
#ifdef NDEBUG
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(InterpretedCodeBlock)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_context));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_script));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_byteCodeBlock));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_parent));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_children));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_parameterNames));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_identifierInfos));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock, m_blockInfos));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(InterpretedCodeBlock));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
#else
return CustomAllocator<InterpretedCodeBlock>().allocate(1);
#endif
}
void* InterpretedCodeBlockWithRareData::operator new(size_t size)
{
#ifdef NDEBUG
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(InterpretedCodeBlockWithRareData)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_context));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_script));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_byteCodeBlock));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_parent));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_children));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_parameterNames));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_identifierInfos));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_blockInfos));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlockWithRareData, m_rareData));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(InterpretedCodeBlockWithRareData));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
#else
return CustomAllocator<InterpretedCodeBlockWithRareData>().allocate(1);
#endif
}
void* InterpretedCodeBlock::BlockInfo::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(InterpretedCodeBlock::BlockInfo)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(InterpretedCodeBlock::BlockInfo, m_identifiers));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(InterpretedCodeBlock));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void InterpretedCodeBlock::initBlockScopeInformation(ASTScopeContext* scopeCtx)
{
const ASTBlockContextVector& blockScopes = scopeCtx->m_childBlockScopes;
m_blockInfos.resizeWithUninitializedValues(blockScopes.size());
for (size_t i = 0; i < blockScopes.size(); i++) {
BlockInfo* info = new BlockInfo(
#ifndef NDEBUG
blockScopes[i]->m_loc
#endif
);
// if block comes from switch statement, we should allocate variables on heap.
// because our we can track only heap variables are initialized by user
// we can track that {let, const} variables are initialized by user only if variables are allocated in heap
bool everyVariablesShouldUseHeapStorage = info->m_nodeType == ASTNodeType::SwitchStatement;
m_canAllocateEnvironmentOnStack = m_canAllocateEnvironmentOnStack && !everyVariablesShouldUseHeapStorage;
info->m_canAllocateEnvironmentOnStack = m_canAllocateEnvironmentOnStack;
info->m_shouldAllocateEnvironment = true;
info->m_nodeType = blockScopes[i]->m_nodeType;
info->m_parentBlockIndex = blockScopes[i]->m_parentBlockIndex;
info->m_blockIndex = blockScopes[i]->m_blockIndex;
info->m_identifiers.resizeWithUninitializedValues(blockScopes[i]->m_names.size());
for (size_t j = 0; j < blockScopes[i]->m_names.size(); j++) {
BlockIdentifierInfo idInfo;
idInfo.m_needToAllocateOnStack = m_canUseIndexedVariableStorage && !everyVariablesShouldUseHeapStorage;
idInfo.m_isMutable = !blockScopes[i]->m_names[j].isConstBinding();
idInfo.m_indexForIndexedStorage = SIZE_MAX;
idInfo.m_name = blockScopes[i]->m_names[j].name();
info->m_identifiers[j] = idInfo;
}
m_blockInfos[i] = info;
}
}
InterpretedCodeBlock* InterpretedCodeBlock::createInterpretedCodeBlock(Context* ctx, Script* script, StringView src, ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction)
{
if (UNLIKELY(scopeCtx->m_needRareData)) {
return new InterpretedCodeBlockWithRareData(ctx, script, src, scopeCtx, isEvalCode, isEvalCodeInFunction);
}
return new InterpretedCodeBlock(ctx, script, src, scopeCtx, isEvalCode, isEvalCodeInFunction);
}
InterpretedCodeBlock* InterpretedCodeBlock::createInterpretedCodeBlock(Context* ctx, Script* script, StringView src, ASTScopeContext* scopeCtx, InterpretedCodeBlock* parentBlock, bool isEvalCode, bool isEvalCodeInFunction)
{
if (UNLIKELY(scopeCtx->m_needRareData)) {
return new InterpretedCodeBlockWithRareData(ctx, script, src, scopeCtx, parentBlock, isEvalCode, isEvalCodeInFunction);
}
return new InterpretedCodeBlock(ctx, script, src, scopeCtx, parentBlock, isEvalCode, isEvalCodeInFunction);
}
// create an empty InterpretedCodeBlock
InterpretedCodeBlock* InterpretedCodeBlock::createInterpretedCodeBlock(Context* ctx, Script* script, bool needRareData)
{
if (UNLIKELY(needRareData)) {
return new InterpretedCodeBlockWithRareData(ctx, script);
}
return new InterpretedCodeBlock(ctx, script);
}
InterpretedCodeBlock::InterpretedCodeBlock(Context* ctx, Script* script, StringView src, ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction)
: CodeBlock(ctx)
, m_script(script)
, m_src(src)
, m_byteCodeBlock(nullptr)
, m_parent(nullptr)
, m_children(nullptr)
, m_functionStart(1, 1, 0)
#if !(defined NDEBUG) || defined ESCARGOT_DEBUGGER
, m_bodyEndLOC(SIZE_MAX, SIZE_MAX, SIZE_MAX)
#endif
, m_functionLength(0)
, m_parameterCount(0)
, m_identifierOnStackCount(0)
, m_identifierOnHeapCount(0)
, m_lexicalBlockStackAllocatedIdentifierMaximumDepth(0)
, m_functionBodyBlockIndex(0)
, m_lexicalBlockIndexFunctionLocatedIn(0)
, m_isFunctionNameSaveOnHeap(false)
, m_isFunctionNameExplicitlyDeclared(false)
, m_canUseIndexedVariableStorage(false)
, m_canAllocateVariablesOnStack(false)
, m_canAllocateEnvironmentOnStack(false)
, m_hasDescendantUsesNonIndexedVariableStorage(false)
, m_hasEval(false)
, m_hasWith(false)
, m_isStrict(false)
, m_inWith(false)
, m_isEvalCode(false)
, m_isEvalCodeInFunction(false)
, m_usesArgumentsObject(false)
, m_isFunctionExpression(false)
, m_isFunctionDeclaration(false)
, m_isArrowFunctionExpression(false)
, m_isOneExpressionOnlyVirtualArrowFunctionExpression(false)
, m_isClassConstructor(false)
, m_isDerivedClassConstructor(false)
, m_isObjectMethod(false)
, m_isClassMethod(false)
, m_isClassStaticMethod(false)
, m_isGenerator(false)
, m_isAsync(false)
, m_needsVirtualIDOperation(false)
, m_hasArrowParameterPlaceHolder(false)
, m_hasParameterOtherThanIdentifier(false)
, m_allowSuperCall(false)
, m_allowSuperProperty(false)
, m_allowArguments(false)
#ifndef NDEBUG
, m_scopeContext(scopeCtx)
#endif
{
recordGlobalParsingInfo(scopeCtx, isEvalCode, isEvalCodeInFunction);
}
InterpretedCodeBlock::InterpretedCodeBlock(Context* ctx, Script* script, StringView src, ASTScopeContext* scopeCtx, InterpretedCodeBlock* parentBlock, bool isEvalCode, bool isEvalCodeInFunction)
: CodeBlock(ctx)
, m_script(script)
, m_src(StringView(src, scopeCtx->m_functionStartLOC.index, scopeCtx->m_bodyEndLOC.index))
, m_byteCodeBlock(nullptr)
, m_parent(parentBlock)
, m_children(nullptr)
, m_functionName(scopeCtx->m_functionName)
, m_functionStart(scopeCtx->m_functionStartLOC)
#if !(defined NDEBUG) || defined ESCARGOT_DEBUGGER
, m_bodyEndLOC(SIZE_MAX, SIZE_MAX, SIZE_MAX)
#endif
, m_functionLength(scopeCtx->m_functionLength)
, m_parameterCount(scopeCtx->m_parameterCount)
, m_identifierOnStackCount(0)
, m_identifierOnHeapCount(0)
, m_lexicalBlockStackAllocatedIdentifierMaximumDepth(0)
, m_functionBodyBlockIndex(0)
, m_lexicalBlockIndexFunctionLocatedIn(scopeCtx->m_lexicalBlockIndexFunctionLocatedIn)
, m_isFunctionNameSaveOnHeap(false)
, m_isFunctionNameExplicitlyDeclared(false)
, m_canUseIndexedVariableStorage(false)
, m_canAllocateVariablesOnStack(false)
, m_canAllocateEnvironmentOnStack(false)
, m_hasDescendantUsesNonIndexedVariableStorage(false)
, m_hasEval(false)
, m_hasWith(false)
, m_isStrict(false)
, m_inWith(false)
, m_isEvalCode(false)
, m_isEvalCodeInFunction(false)
, m_usesArgumentsObject(false)
, m_isFunctionExpression(false)
, m_isFunctionDeclaration(false)
, m_isArrowFunctionExpression(false)
, m_isOneExpressionOnlyVirtualArrowFunctionExpression(false)
, m_isClassConstructor(false)
, m_isDerivedClassConstructor(false)
, m_isObjectMethod(false)
, m_isClassMethod(false)
, m_isClassStaticMethod(false)
, m_isGenerator(false)
, m_isAsync(false)
, m_needsVirtualIDOperation(false)
, m_hasArrowParameterPlaceHolder(false)
, m_hasParameterOtherThanIdentifier(false)
, m_allowSuperCall(false)
, m_allowSuperProperty(false)
, m_allowArguments(false)
#ifndef NDEBUG
, m_scopeContext(scopeCtx)
#endif
{
recordFunctionParsingInfo(scopeCtx, isEvalCode, isEvalCodeInFunction);
}
InterpretedCodeBlock::InterpretedCodeBlock(Context* ctx, Script* script)
: CodeBlock(ctx)
, m_script(script)
, m_src()
, m_byteCodeBlock(nullptr)
, m_parent(nullptr)
, m_children(nullptr)
, m_functionName()
, m_functionStart(SIZE_MAX, SIZE_MAX, SIZE_MAX)
#if !(defined NDEBUG) || defined ESCARGOT_DEBUGGER
, m_bodyEndLOC(SIZE_MAX, SIZE_MAX, SIZE_MAX)
#endif
, m_functionLength(0)
, m_parameterCount(0)
, m_identifierOnStackCount(0)
, m_identifierOnHeapCount(0)
, m_lexicalBlockStackAllocatedIdentifierMaximumDepth(0)
, m_functionBodyBlockIndex(0)
, m_lexicalBlockIndexFunctionLocatedIn(0)
, m_isFunctionNameSaveOnHeap(false)
, m_isFunctionNameExplicitlyDeclared(false)
, m_canUseIndexedVariableStorage(false)
, m_canAllocateVariablesOnStack(false)
, m_canAllocateEnvironmentOnStack(false)
, m_hasDescendantUsesNonIndexedVariableStorage(false)
, m_hasEval(false)
, m_hasWith(false)
, m_isStrict(false)
, m_inWith(false)
, m_isEvalCode(false)
, m_isEvalCodeInFunction(false)
, m_usesArgumentsObject(false)
, m_isFunctionExpression(false)
, m_isFunctionDeclaration(false)
, m_isArrowFunctionExpression(false)
, m_isOneExpressionOnlyVirtualArrowFunctionExpression(false)
, m_isClassConstructor(false)
, m_isDerivedClassConstructor(false)
, m_isObjectMethod(false)
, m_isClassMethod(false)
, m_isClassStaticMethod(false)
, m_isGenerator(false)
, m_isAsync(false)
, m_needsVirtualIDOperation(false)
, m_hasArrowParameterPlaceHolder(false)
, m_hasParameterOtherThanIdentifier(false)
, m_allowSuperCall(false)
, m_allowSuperProperty(false)
, m_allowArguments(false)
#ifndef NDEBUG
, m_scopeContext(nullptr)
#endif
{
}
void InterpretedCodeBlock::recordGlobalParsingInfo(ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction)
{
m_isStrict = scopeCtx->m_isStrict;
m_isClassConstructor = scopeCtx->m_isClassConstructor;
m_isDerivedClassConstructor = scopeCtx->m_isDerivedClassConstructor;
m_isObjectMethod = scopeCtx->m_isObjectMethod;
m_isClassMethod = scopeCtx->m_isClassMethod;
m_isClassStaticMethod = scopeCtx->m_isClassStaticMethod;
m_isGenerator = scopeCtx->m_isGenerator;
m_isAsync = scopeCtx->m_isAsync;
m_hasEval = scopeCtx->m_hasEval;
m_hasWith = scopeCtx->m_hasWith;
m_inWith = scopeCtx->m_inWith;
m_isEvalCode = isEvalCode;
m_isEvalCodeInFunction = isEvalCodeInFunction;
m_canUseIndexedVariableStorage = !m_hasEval && !m_isEvalCode && !m_hasWith;
m_canAllocateEnvironmentOnStack = m_canUseIndexedVariableStorage;
m_canAllocateVariablesOnStack = m_canAllocateEnvironmentOnStack;
m_allowSuperCall = scopeCtx->m_allowSuperCall;
m_allowSuperProperty = scopeCtx->m_allowSuperProperty;
m_allowArguments = scopeCtx->m_allowArguments;
const ASTScopeContextNameInfoVector& innerIdentifiers = scopeCtx->m_varNames;
m_identifierInfos.resize(innerIdentifiers.size());
for (size_t i = 0; i < innerIdentifiers.size(); i++) {
IdentifierInfo info;
info.m_needToAllocateOnStack = false;
info.m_isMutable = true;
info.m_isParameterName = innerIdentifiers[i].isParameterName();
info.m_isExplicitlyDeclaredOrParameterName = innerIdentifiers[i].isExplicitlyDeclaredOrParameterName();
info.m_isVarDeclaration = innerIdentifiers[i].isVarDeclaration();
info.m_indexForIndexedStorage = SIZE_MAX;
info.m_name = innerIdentifiers[i].name();
m_identifierInfos[i] = info;
}
initBlockScopeInformation(scopeCtx);
}
void InterpretedCodeBlock::recordFunctionParsingInfo(ASTScopeContext* scopeCtx, bool isEvalCode, bool isEvalCodeInFunction)
{
m_isStrict = scopeCtx->m_isStrict;
m_hasEval = scopeCtx->m_hasEval;
m_hasWith = scopeCtx->m_hasWith;
m_inWith = scopeCtx->m_inWith;
m_hasArrowParameterPlaceHolder = scopeCtx->m_hasArrowParameterPlaceHolder;
m_hasParameterOtherThanIdentifier = scopeCtx->m_hasParameterOtherThanIdentifier;
m_functionBodyBlockIndex = scopeCtx->m_functionBodyBlockIndex;
m_isEvalCode = isEvalCode;
m_isEvalCodeInFunction = isEvalCodeInFunction;
bool isFE = scopeCtx->m_nodeType == FunctionExpression || scopeCtx->m_nodeType == ArrowFunctionExpression;
bool isFD = scopeCtx->m_nodeType == FunctionDeclaration;
m_isFunctionDeclaration = isFD;
m_isFunctionExpression = isFE;
m_isArrowFunctionExpression = scopeCtx->m_isArrowFunctionExpression;
m_isOneExpressionOnlyVirtualArrowFunctionExpression = scopeCtx->m_isOneExpressionOnlyVirtualArrowFunctionExpression;
m_isClassConstructor = scopeCtx->m_isClassConstructor;
m_isDerivedClassConstructor = scopeCtx->m_isDerivedClassConstructor;
m_isObjectMethod = scopeCtx->m_isObjectMethod;
m_isClassMethod = scopeCtx->m_isClassMethod;
m_isClassStaticMethod = scopeCtx->m_isClassStaticMethod;
m_isGenerator = scopeCtx->m_isGenerator;
m_isAsync = scopeCtx->m_isAsync;
m_allowSuperCall = scopeCtx->m_allowSuperCall;
m_allowSuperProperty = scopeCtx->m_allowSuperProperty;
m_allowArguments = scopeCtx->m_allowArguments;
const AtomicStringTightVector& parameterNames = scopeCtx->m_parameters;
if (parameterNames.size() > 0) {
size_t parameterNamesCount = parameterNames.size();
m_parameterNames.resizeWithUninitializedValues(parameterNamesCount);
for (size_t i = 0; i < parameterNamesCount; i++) {
m_parameterNames[i] = parameterNames[i];
}
}
m_canUseIndexedVariableStorage = !m_hasEval && !m_isEvalCode && !m_hasWith;
m_canAllocateEnvironmentOnStack = m_canUseIndexedVariableStorage && !m_isGenerator && !m_isAsync;
m_canAllocateVariablesOnStack = true;
m_hasDescendantUsesNonIndexedVariableStorage = false;
const ASTScopeContextNameInfoVector& innerIdentifiers = scopeCtx->m_varNames;
m_identifierInfos.resizeWithUninitializedValues(innerIdentifiers.size());
for (size_t i = 0; i < innerIdentifiers.size(); i++) {
IdentifierInfo info;
info.m_needToAllocateOnStack = m_canUseIndexedVariableStorage;
info.m_isMutable = true;
info.m_isParameterName = innerIdentifiers[i].isParameterName();
info.m_isExplicitlyDeclaredOrParameterName = innerIdentifiers[i].isExplicitlyDeclaredOrParameterName();
info.m_isVarDeclaration = innerIdentifiers[i].isVarDeclaration();
info.m_indexForIndexedStorage = SIZE_MAX;
info.m_name = innerIdentifiers[i].name();
m_identifierInfos[i] = info;
}
initBlockScopeInformation(scopeCtx);
}
void InterpretedCodeBlock::captureArguments()
{
AtomicString arguments = m_context->staticStrings().arguments;
ASSERT(!hasParameterName(arguments));
ASSERT(!isGlobalCodeBlock() && !isArrowFunctionExpression());
if (m_usesArgumentsObject) {
return;
}
m_usesArgumentsObject = true;
if (findVarName(arguments) == SIZE_MAX) {
IdentifierInfo info;
info.m_needToAllocateOnStack = true;
info.m_isMutable = true;
info.m_isParameterName = false;
info.m_isExplicitlyDeclaredOrParameterName = false;
info.m_isVarDeclaration = false;
info.m_indexForIndexedStorage = SIZE_MAX;
info.m_name = arguments;
m_identifierInfos.pushBack(info);
auto idMap = identifierInfoMap();
if (idMap) {
idMap->insert(std::make_pair(arguments, m_identifierInfos.size() - 1));
}
}
if (m_functionLength && shouldHaveMappedArguments()) {
ASSERT(m_functionLength == m_parameterNames.size());
// Unmapped arguments object doesn't connect arguments object property with arguments variable
m_canAllocateEnvironmentOnStack = false;
for (size_t j = 0; j < m_parameterNames.size(); j++) {
for (size_t k = 0; k < m_identifierInfos.size(); k++) {
if (m_identifierInfos[k].m_name == m_parameterNames[j]) {
m_identifierInfos[k].m_needToAllocateOnStack = false;
break;
}
}
}
}
}
std::pair<bool, size_t> InterpretedCodeBlock::tryCaptureIdentifiersFromChildCodeBlock(LexicalBlockIndex blockIndex, AtomicString name)
{
auto r = findNameWithinBlock(blockIndex, name);
if (std::get<0>(r)) {
auto& id = m_blockInfos[std::get<1>(r)]->m_identifiers[std::get<2>(r)];
ASSERT(id.m_name == name);
id.m_needToAllocateOnStack = false;
return std::make_pair(true, std::get<1>(r));
}
if (blockIndex < m_functionBodyBlockIndex) {
if (!isParameterName(name)) {
return std::make_pair(false, SIZE_MAX);
}
}
size_t idx = findVarName(name);
if (idx != SIZE_MAX) {
m_identifierInfos[idx].m_needToAllocateOnStack = false;
return std::make_pair(true, SIZE_MAX);
}
return std::make_pair(false, SIZE_MAX);
}
void InterpretedCodeBlock::markHeapAllocatedEnvironmentFromHere(LexicalBlockIndex blockIndex, InterpretedCodeBlock* to)
{
InterpretedCodeBlock* c = this;
while (c) {
InterpretedCodeBlock::BlockInfo* bi = nullptr;
for (size_t i = 0; i < c->m_blockInfos.size(); i++) {
if (c->m_blockInfos[i]->m_blockIndex == blockIndex) {
bi = c->m_blockInfos[i];
break;
}
}
while (bi && bi->m_canAllocateEnvironmentOnStack) {
bi->m_canAllocateEnvironmentOnStack = false;
if (bi->m_parentBlockIndex == LEXICAL_BLOCK_INDEX_MAX) {
break;
}
for (size_t i = 0; i < c->m_blockInfos.size(); i++) {
if (c->m_blockInfos[i]->m_blockIndex == bi->m_parentBlockIndex) {
bi = c->m_blockInfos[i];
break;
}
}
}
c->m_canAllocateEnvironmentOnStack = false;
blockIndex = c->lexicalBlockIndexFunctionLocatedIn();
if (c == to) {
return;
}
c = c->parent();
}
}
void InterpretedCodeBlock::computeBlockVariables(LexicalBlockIndex currentBlockIndex, size_t currentStackAllocatedVariableIndex, size_t& maxStackAllocatedVariableDepth)
{
InterpretedCodeBlock::BlockInfo* bi = nullptr;
for (size_t i = 0; i < m_blockInfos.size(); i++) {
if (m_blockInfos[i]->m_blockIndex == currentBlockIndex) {
bi = m_blockInfos[i];
break;
}
}
ASSERT(bi != nullptr);
size_t heapIndex = 0;
size_t stackSize = 0;
for (size_t i = 0; i < bi->m_identifiers.size(); i++) {
if (!m_canAllocateVariablesOnStack) {
bi->m_identifiers[i].m_needToAllocateOnStack = false;
}
if (bi->m_identifiers[i].m_needToAllocateOnStack) {
bi->m_identifiers[i].m_indexForIndexedStorage = currentStackAllocatedVariableIndex;
stackSize++;
currentStackAllocatedVariableIndex++;
maxStackAllocatedVariableDepth = std::max(maxStackAllocatedVariableDepth, currentStackAllocatedVariableIndex);
} else {
bi->m_identifiers[i].m_indexForIndexedStorage = heapIndex++;
bi->m_canAllocateEnvironmentOnStack = false;
}
}
// if there is no heap indexed variable, we can skip allocate env
bool isThereHeapVariable = false;
for (size_t i = 0; i < bi->m_identifiers.size(); i++) {
if (!bi->m_identifiers[i].m_needToAllocateOnStack) {
isThereHeapVariable = true;
}
}
bi->m_shouldAllocateEnvironment = isThereHeapVariable;
for (size_t i = 0; i < m_blockInfos.size(); i++) {
if (m_blockInfos[i]->m_parentBlockIndex == currentBlockIndex) {
computeBlockVariables(m_blockInfos[i]->m_blockIndex, currentStackAllocatedVariableIndex, maxStackAllocatedVariableDepth);
}
}
}
// global block id map
// [this, ..]
// function block id map
// [this, functionName, arg0, arg1...]
void InterpretedCodeBlock::computeVariables()
{
// we should check m_inWith
// because CallFunctionInWithScope needs LoadByName
m_canAllocateVariablesOnStack = !m_isEvalCode && !m_hasDescendantUsesNonIndexedVariableStorage && m_canUseIndexedVariableStorage && !m_inWith;
if (m_canAllocateEnvironmentOnStack) {
// we should check m_inWith
// because CallFunctionInWithScope needs LoadByName
m_canAllocateEnvironmentOnStack = !m_isEvalCode && !m_hasDescendantUsesNonIndexedVariableStorage && m_canUseIndexedVariableStorage && !m_inWith;
}
if (m_functionName.string()->length()) {
if (m_isFunctionExpression) {
// name of function expression is immutable
for (size_t i = 0; i < m_identifierInfos.size(); i++) {
if (m_identifierInfos[i].m_name == m_functionName && !m_identifierInfos[i].m_isExplicitlyDeclaredOrParameterName) {
if (m_isFunctionExpression) {
m_identifierInfos[i].m_isMutable = false;
}
break;
}
}
}
}
if (canUseIndexedVariableStorage() && !isGlobalCodeBlock()) {
size_t s = isGlobalCodeBlock() ? 1 : 2;
size_t h = 0;
for (size_t i = 0; i < m_identifierInfos.size(); i++) {
if (!m_canAllocateVariablesOnStack) {
m_identifierInfos[i].m_needToAllocateOnStack = false;
}
if (m_identifierInfos[i].m_name == m_functionName) {
if (m_identifierInfos[i].m_isExplicitlyDeclaredOrParameterName) {
m_isFunctionNameExplicitlyDeclared = true;
}
if (!m_identifierInfos[i].m_needToAllocateOnStack) {
m_isFunctionNameSaveOnHeap = true;
m_identifierInfos[i].m_indexForIndexedStorage = h;
h++;
} else {
if (m_isFunctionNameExplicitlyDeclared) {
s++;
}
m_identifierInfos[i].m_indexForIndexedStorage = 1;
}
continue;
}
if (m_identifierInfos[i].m_needToAllocateOnStack) {
m_identifierInfos[i].m_indexForIndexedStorage = s;
s++;
} else {
m_identifierInfos[i].m_indexForIndexedStorage = h;
h++;
}
}
size_t currentStackAllocatedVariableIndex = 0;
size_t maxStackAllocatedVariableDepth = 0;
computeBlockVariables(0, currentStackAllocatedVariableIndex, maxStackAllocatedVariableDepth);
m_lexicalBlockStackAllocatedIdentifierMaximumDepth = maxStackAllocatedVariableDepth;
m_identifierOnStackCount = s;
m_identifierOnHeapCount = h;
} else if (isGlobalCodeBlock() && script()->isModule()) {
size_t s = 1;
size_t h = 0;
for (size_t i = 0; i < m_identifierInfos.size(); i++) {
m_identifierInfos[i].m_needToAllocateOnStack = false;
m_identifierInfos[i].m_indexForIndexedStorage = h;
h++;
}
m_identifierOnStackCount = s;
m_identifierOnHeapCount = h;
m_lexicalBlockStackAllocatedIdentifierMaximumDepth = 0;
if (!canUseIndexedVariableStorage()) {
for (size_t i = 0; i < m_blockInfos.size(); i++) {
m_blockInfos[i]->m_canAllocateEnvironmentOnStack = false;
m_blockInfos[i]->m_shouldAllocateEnvironment = m_blockInfos[i]->m_identifiers.size();
for (size_t j = 0; j < m_blockInfos[i]->m_identifiers.size(); j++) {
m_blockInfos[i]->m_identifiers[j].m_indexForIndexedStorage = SIZE_MAX;
m_blockInfos[i]->m_identifiers[j].m_needToAllocateOnStack = false;
}
}
} else {
ASSERT(isGlobalCodeBlock());
size_t currentStackAllocatedVariableIndex = 0;
size_t maxStackAllocatedVariableDepth = 0;
computeBlockVariables(0, currentStackAllocatedVariableIndex, maxStackAllocatedVariableDepth);
m_lexicalBlockStackAllocatedIdentifierMaximumDepth = maxStackAllocatedVariableDepth;
}
InterpretedCodeBlock::BlockInfo* bi = nullptr;
for (size_t i = 0; i < m_blockInfos.size(); i++) {
if (m_blockInfos[i]->m_blockIndex == 0) {
bi = m_blockInfos[i];
break;
}
}
ASSERT(!!bi);
// global {let, const} declaration should be processed on ModuleEnvironmentRecord
for (size_t i = 0; i < bi->m_identifiers.size(); i++) {
bi->m_identifiers[i].m_indexForIndexedStorage = i + identifierInfos().size();
bi->m_identifiers[i].m_needToAllocateOnStack = false;
}
bi->m_shouldAllocateEnvironment = false;
} else {
if (m_isEvalCodeInFunction) {
AtomicString arguments = m_context->staticStrings().arguments;
for (size_t i = 0; i < m_identifierInfos.size(); i++) {
if (m_identifierInfos[i].m_name == arguments) {
m_identifierInfos.erase(i);
break;
}
}
}
size_t s = isGlobalCodeBlock() ? 1 : 2;
size_t h = 0;
for (size_t i = 0; i < m_identifierInfos.size(); i++) {
m_identifierInfos[i].m_needToAllocateOnStack = false;
if (m_identifierInfos[i].m_name == m_functionName) {
if (m_identifierInfos[i].m_isExplicitlyDeclaredOrParameterName) {
m_isFunctionNameExplicitlyDeclared = true;
}
m_isFunctionNameSaveOnHeap = true;
m_identifierInfos[i].m_indexForIndexedStorage = h;
h++;
continue;
}
m_identifierInfos[i].m_indexForIndexedStorage = h;
h++;
}
m_identifierOnStackCount = s;
m_identifierOnHeapCount = h;
m_lexicalBlockStackAllocatedIdentifierMaximumDepth = 0;
if (!canUseIndexedVariableStorage()) {
for (size_t i = 0; i < m_blockInfos.size(); i++) {
m_blockInfos[i]->m_canAllocateEnvironmentOnStack = false;
m_blockInfos[i]->m_shouldAllocateEnvironment = m_blockInfos[i]->m_identifiers.size();
for (size_t j = 0; j < m_blockInfos[i]->m_identifiers.size(); j++) {
m_blockInfos[i]->m_identifiers[j].m_indexForIndexedStorage = SIZE_MAX;
m_blockInfos[i]->m_identifiers[j].m_needToAllocateOnStack = false;
}
}
} else {
ASSERT(isGlobalCodeBlock());
size_t currentStackAllocatedVariableIndex = 0;
size_t maxStackAllocatedVariableDepth = 0;
computeBlockVariables(0, currentStackAllocatedVariableIndex, maxStackAllocatedVariableDepth);
m_lexicalBlockStackAllocatedIdentifierMaximumDepth = maxStackAllocatedVariableDepth;
}
if (isGlobalCodeBlock()) {
InterpretedCodeBlock::BlockInfo* bi = nullptr;
for (size_t i = 0; i < m_blockInfos.size(); i++) {
if (m_blockInfos[i]->m_blockIndex == 0) {
bi = m_blockInfos[i];
break;
}
}
ASSERT(!!bi);
// global {let, const} declaration should be processed on GlobalEnvironmentRecord
for (size_t i = 0; i < bi->m_identifiers.size(); i++) {
bi->m_identifiers[i].m_indexForIndexedStorage = SIZE_MAX;
bi->m_identifiers[i].m_needToAllocateOnStack = false;
}
bi->m_shouldAllocateEnvironment = false;
}
}
}
bool InterpretedCodeBlock::needsToLoadThisBindingFromEnvironment()
{
if (isClassConstructor()) {
return true;
}
if (isArrowFunctionExpression()) {
InterpretedCodeBlock* cb = m_parent;
while (cb) {
if (cb->isArrowFunctionExpression()) {
// pass through
} else if (cb->isClassConstructor()) {
return true;
} else {
break;
}
cb = cb->m_parent;
}
return false;
}
return false;
}
InterpretedCodeBlock::IndexedIdentifierInfo InterpretedCodeBlock::indexedIdentifierInfo(const AtomicString& name, ByteCodeGenerateContext* generatorContext)
{
size_t upperIndex = 0;
IndexedIdentifierInfo info;
InterpretedCodeBlock* blk = this;
LexicalBlockIndex blockIndex = generatorContext->m_lexicalBlockIndex;
LexicalBlockIndex startBlockIndex = blockIndex;
while (blk && blk->canUseIndexedVariableStorage()) {
// search block first.
while (true) {
InterpretedCodeBlock::BlockInfo* bi = nullptr;
for (size_t i = 0; i < blk->m_blockInfos.size(); i++) {
if (blk->m_blockInfos[i]->m_blockIndex == blockIndex) {
bi = blk->m_blockInfos[i];
break;
}
}
ASSERT(bi);
for (size_t i = 0; i < bi->m_identifiers.size(); i++) {
if (bi->m_identifiers[i].m_name == name) {
info.m_isResultSaved = true;
info.m_isStackAllocated = bi->m_identifiers[i].m_needToAllocateOnStack;
info.m_index = bi->m_identifiers[i].m_indexForIndexedStorage;
if (info.m_isStackAllocated) {
info.m_index += identifierOnStackCount();
}
info.m_upperIndex = upperIndex + generatorContext->m_openedNonBlockEnvCount;
info.m_isMutable = bi->m_identifiers[i].m_isMutable;
info.m_type = IndexedIdentifierInfo::DeclarationType::LexicallyDeclared;
info.m_blockIndex = bi->m_blockIndex;
if (blk->isGlobalCodeBlock() && !blk->script()->isModule() && bi->m_parentBlockIndex == LEXICAL_BLOCK_INDEX_MAX) {
info.m_isGlobalLexicalVariable = true;
} else {
info.m_isGlobalLexicalVariable = false;
ASSERT(info.m_index != SIZE_MAX);
}
return info;
}
}
if (bi->m_shouldAllocateEnvironment) {
upperIndex++;
}
if (bi->m_parentBlockIndex == LEXICAL_BLOCK_INDEX_MAX) {
break;
}
blockIndex = bi->m_parentBlockIndex;
}
if (blk->isGlobalCodeBlock() && !blk->script()->isModule()) {
break;
}
bool canUseVarName;
if (startBlockIndex < blk->functionBodyBlockIndex()) {
canUseVarName = blk->isParameterName(name) || name == blk->functionName() || name == context()->staticStrings().arguments;
} else {
canUseVarName = true;
}
if (canUseVarName) {
size_t index = blk->findVarName(name);
if (index != SIZE_MAX) {
ASSERT(blk->m_identifierInfos[index].m_indexForIndexedStorage != SIZE_MAX);
info.m_isResultSaved = true;
info.m_isGlobalLexicalVariable = false;
info.m_isStackAllocated = blk->m_identifierInfos[index].m_needToAllocateOnStack;
info.m_upperIndex = upperIndex + generatorContext->m_openedNonBlockEnvCount;
info.m_isMutable = blk->m_identifierInfos[index].m_isMutable;
info.m_index = blk->m_identifierInfos[index].m_indexForIndexedStorage;
info.m_type = IndexedIdentifierInfo::DeclarationType::VarDeclared;
return info;
}
}
if (blk == this) {
upperIndex += 1;
} else {
upperIndex += !blk->canAllocateEnvironmentOnStack();
}
startBlockIndex = blockIndex = blk->lexicalBlockIndexFunctionLocatedIn();
blk = blk->parent();
}
return info;
}
} // namespace Escargot