-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathdeclarations.rs
More file actions
1231 lines (1081 loc) · 56.9 KB
/
declarations.rs
File metadata and controls
1231 lines (1081 loc) · 56.9 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
use super::{BindingAccessOpcode, ToJsString};
use crate::{
Context, JsNativeError, JsResult, SpannedSourceText,
bytecompiler::{ByteCompiler, FunctionCompiler, FunctionSpec, NodeKind},
vm::{CallFrame, GlobalFunctionBinding, opcode::BindingOpcode},
};
use boa_ast::{
Script,
declaration::Binding,
function::{FormalParameterList, FunctionBody},
operations::{
LexicallyScopedDeclaration, VarScopedDeclaration, all_private_identifiers_valid,
bound_names, lexically_declared_names, lexically_scoped_declarations, var_declared_names,
var_scoped_declarations,
},
scope::{FunctionScopes, Scope},
scope_analyzer::EvalDeclarationBindings,
visitor::NodeRef,
};
use boa_interner::{JStrRef, Sym};
use rustc_hash::FxHashSet;
#[cfg(feature = "annex-b")]
use boa_ast::operations::annex_b_function_declarations_names;
/// `GlobalDeclarationInstantiation ( script, env )`
///
/// This diverges from the specification by separating the context from the compilation process.
/// Many steps are skipped that are done during bytecode compilation.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-globaldeclarationinstantiation
#[cfg(not(feature = "annex-b"))]
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::ptr_arg)]
pub(crate) fn global_declaration_instantiation_context(
_annex_b_function_names: &mut Vec<Sym>,
_script: &Script,
_env: &Scope,
_context: &mut Context,
) -> JsResult<()> {
Ok(())
}
/// `GlobalDeclarationInstantiation ( script, env )`
///
/// This diverges from the specification by separating the context from the compilation process.
/// Many steps are skipped that are done during bytecode compilation.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-globaldeclarationinstantiation
#[cfg(feature = "annex-b")]
pub(crate) fn global_declaration_instantiation_context(
annex_b_function_names: &mut Vec<Sym>,
script: &Script,
env: &Scope,
context: &mut Context,
) -> JsResult<()> {
// SKIP: 1. Let lexNames be the LexicallyDeclaredNames of script.
// SKIP: 2. Let varNames be the VarDeclaredNames of script.
// SKIP: 3. For each element name of lexNames, do
// SKIP: 4. For each element name of varNames, do
// 5. Let varDeclarations be the VarScopedDeclarations of script.
// Note: VarScopedDeclarations for a Script node is TopLevelVarScopedDeclarations.
let var_declarations = var_scoped_declarations(script);
// SKIP: 6. Let functionsToInitialize be a new empty List.
// 7. Let declaredFunctionNames be a new empty List.
let mut declared_function_names = FxHashSet::default();
// 8. For each element d of varDeclarations, in reverse List order, do
for declaration in var_declarations.iter().rev() {
// a. If d is not either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
// a.i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// a.ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// a.iii. Let fn be the sole element of the BoundNames of d.
let name = match declaration {
VarScopedDeclaration::FunctionDeclaration(f) => f.name(),
VarScopedDeclaration::GeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
// a.iv. If declaredFunctionNames does not contain fn, then
// 3. Append fn to declaredFunctionNames.
if declared_function_names.insert(name.sym()) {
// SKIP: 1. Let fnDefinable be ? env.CanDeclareGlobalFunction(fn).
// SKIP: 2. If fnDefinable is false, throw a TypeError exception.
// SKIP: 4. Insert d as the first element of functionsToInitialize.
}
}
// // 9. Let declaredVarNames be a new empty List.
let mut declared_var_names = FxHashSet::default();
// 10. For each element d of varDeclarations, do
// a. If d is either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
for declaration in var_declarations {
let VarScopedDeclaration::VariableDeclaration(declaration) = declaration else {
continue;
};
// i. For each String vn of the BoundNames of d, do
for name in bound_names(&declaration) {
// 1. If declaredFunctionNames does not contain vn, then
if !declared_function_names.contains(&name) {
// SKIP: a. Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
// SKIP: b. If vnDefinable is false, throw a TypeError exception.
// c. If declaredVarNames does not contain vn, then
// i. Append vn to declaredVarNames.
declared_var_names.insert(name);
}
}
}
// 11. NOTE: No abnormal terminations occur after this algorithm step if the global object is an ordinary object.
// However, if the global object is a Proxy exotic object it may exhibit behaviours
// that cause abnormal terminations in some of the following steps.
// 12. NOTE: Annex B.3.2.2 adds additional steps at this point.
// 12. Perform the following steps:
// a. Let strict be IsStrict of script.
// b. If strict is false, then
if !script.strict() {
let lex_names = lexically_declared_names(script);
// i. Let declaredFunctionOrVarNames be the list-concatenation of declaredFunctionNames and declaredVarNames.
// ii. For each FunctionDeclaration f that is directly contained in the StatementList of a Block, CaseClause,
// or DefaultClause Contained within script, do
for f in annex_b_function_declarations_names(script) {
// 1. Let F be StringValue of the BindingIdentifier of f.
// 2. If replacing the FunctionDeclaration f with a VariableStatement that has F as a BindingIdentifier
// would not produce any Early Errors for script, then
if !lex_names.contains(&f) {
let f_string = f.to_js_string(context.interner());
// a. If env.HasLexicalDeclaration(F) is false, then
if !env.has_lex_binding(&f_string) {
// i. Let fnDefinable be ? env.CanDeclareGlobalVar(F).
let fn_definable = context.can_declare_global_function(&f_string)?;
// ii. If fnDefinable is true, then
if fn_definable {
// i. NOTE: A var binding for F is only instantiated here if it is neither
// a VarDeclaredName nor the name of another FunctionDeclaration.
// ii. If declaredFunctionOrVarNames does not contain F, then
if !declared_function_names.contains(&f) && !declared_var_names.contains(&f)
{
// i. Perform ? env.CreateGlobalVarBinding(F, false).
context.create_global_var_binding(f_string, false)?;
// ii. Append F to declaredFunctionOrVarNames.
declared_function_names.insert(f);
}
// iii. When the FunctionDeclaration f is evaluated, perform the following
// steps in place of the FunctionDeclaration Evaluation algorithm provided in 15.2.6:
// i. Let genv be the running execution context's VariableEnvironment.
// ii. Let benv be the running execution context's LexicalEnvironment.
// iii. Let fobj be ! benv.GetBindingValue(F, false).
// iv. Perform ? genv.SetMutableBinding(F, fobj, false).
// v. Return unused.
annex_b_function_names.push(f);
}
}
}
}
}
// SKIP: 13. Let lexDeclarations be the LexicallyScopedDeclarations of script.
// SKIP: 14. Let privateEnv be null.
// SKIP: 15. For each element d of lexDeclarations, do
// SKIP: 16. For each Parse Node f of functionsToInitialize, do
// SKIP: 17. For each String vn of declaredVarNames, do
// 18. Return unused.
Ok(())
}
/// `EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict )`
///
/// This diverges from the specification by separating the context from the compilation process.
/// Many steps are skipped that are done during bytecode compilation.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-evaldeclarationinstantiation
pub(crate) fn prepare_eval_declaration_instantiation(
#[allow(unused, clippy::ptr_arg)] annex_b_function_names: &mut Vec<Sym>,
body: &Script,
#[allow(unused)] strict: bool,
#[allow(unused)] var_env: &Scope,
#[allow(unused)] lex_env: &Scope,
context: &mut Context,
) -> JsResult<()> {
// SKIP: 3. If strict is false, then
// 4. Let privateIdentifiers be a new empty List.
// 5. Let pointer be privateEnv.
// 6. Repeat, while pointer is not null,
// a. For each Private Name binding of pointer.[[Names]], do
// i. If privateIdentifiers does not contain binding.[[Description]],
// append binding.[[Description]] to privateIdentifiers.
// b. Set pointer to pointer.[[OuterPrivateEnvironment]].
let private_identifiers = context.vm.frame().environments.private_name_descriptions();
let private_identifiers = private_identifiers
.into_iter()
.map(|ident| {
// TODO: Replace JStrRef with JsStr this would eliminate the to_vec call.
let ident = ident.to_vec();
context
.interner()
.get(JStrRef::Utf16(&ident))
.expect("string should be in interner")
})
.collect();
// 7. If AllPrivateIdentifiersValid of body with argument privateIdentifiers is false, throw a SyntaxError exception.
if !all_private_identifiers_valid(body, private_identifiers) {
return Err(JsNativeError::syntax()
.with_message("invalid private identifier")
.into());
}
// 2. Let varDeclarations be the VarScopedDeclarations of body.
#[cfg(feature = "annex-b")]
let var_declarations = var_scoped_declarations(body);
// SKIP: 8. Let functionsToInitialize be a new empty List.
// 9. Let declaredFunctionNames be a new empty List.
#[cfg(feature = "annex-b")]
let mut declared_function_names = FxHashSet::default();
// 10. For each element d of varDeclarations, in reverse List order, do
#[cfg(feature = "annex-b")]
for declaration in var_declarations.iter().rev() {
// a. If d is not either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
// a.i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// a.ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// a.iii. Let fn be the sole element of the BoundNames of d.
let name = match &declaration {
VarScopedDeclaration::FunctionDeclaration(f) => f.name(),
VarScopedDeclaration::GeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
// a.iv. If declaredFunctionNames does not contain fn, then
// 2. Append fn to declaredFunctionNames.
if declared_function_names.insert(name.sym()) {
// SKIP: 1. If varEnv is a Global Environment Record, then
// SKIP: 3. Insert d as the first element of functionsToInitialize.
}
}
// 11. NOTE: Annex B.3.2.3 adds additional steps at this point.
// 11. If strict is false, then
#[cfg(feature = "annex-b")]
if !strict {
let lexically_declared_names: FxHashSet<Sym> =
lexically_declared_names(body).into_iter().collect();
// a. Let declaredFunctionOrVarNames be the list-concatenation of declaredFunctionNames and declaredVarNames.
// b. For each FunctionDeclaration f that is directly contained in the StatementList
// of a Block, CaseClause, or DefaultClause Contained within body, do
for f in annex_b_function_declarations_names(body) {
// i. Let F be StringValue of the BindingIdentifier of f.
// ii. If replacing the FunctionDeclaration f with a VariableStatement that has F
// as a BindingIdentifier would not produce any Early Errors for body, then
if !lexically_declared_names.contains(&f) {
// 1. Let bindingExists be false.
let mut binding_exists = false;
// 2. Let thisEnv be lexEnv.
let mut this_env = lex_env;
// 3. Assert: The following loop will terminate.
// 4. Repeat, while thisEnv is not varEnv,
while this_env.scope_index() != lex_env.scope_index() {
let f = f.to_js_string(context.interner());
// a. If thisEnv is not an Object Environment Record, then
// i. If ! thisEnv.HasBinding(F) is true, then
if this_env.has_binding(&f) {
// i. Let bindingExists be true.
binding_exists = true;
break;
}
// b. Set thisEnv to thisEnv.[[OuterEnv]].
if let Some(outer) = this_env.outer() {
this_env = outer;
} else {
break;
}
}
// 5. If bindingExists is false and varEnv is a Global Environment Record, then
let fn_definable = if !binding_exists && var_env.is_global() {
let f = f.to_js_string(context.interner());
// a. If varEnv.HasLexicalDeclaration(F) is false, then
// b. Else,
if var_env.has_lex_binding(&f) {
// i. Let fnDefinable be false.
false
} else {
// i. Let fnDefinable be ? varEnv.CanDeclareGlobalVar(F).
context.can_declare_global_var(&f)?
}
}
// 6. Else,
else {
// a. Let fnDefinable be true.
true
};
// 7. If bindingExists is false and fnDefinable is true, then
if !binding_exists && fn_definable {
// a. If declaredFunctionOrVarNames does not contain F, then
if !declared_function_names.contains(&f) {
// i. If varEnv is a Global Environment Record, then
if var_env.is_global() {
let f = f.to_js_string(context.interner());
// i. Perform ? varEnv.CreateGlobalVarBinding(F, true).
context.create_global_var_binding(f, true)?;
}
// SKIP: ii. Else,
// SKIP: iii. Append F to declaredFunctionOrVarNames.
}
// b. When the FunctionDeclaration f is evaluated, perform the following steps
// in place of the FunctionDeclaration Evaluation algorithm provided in 15.2.6:
// i. Let genv be the running execution context's VariableEnvironment.
// ii. Let benv be the running execution context's LexicalEnvironment.
// iii. Let fobj be ! benv.GetBindingValue(F, false).
// iv. Perform ? genv.SetMutableBinding(F, fobj, false).
// v. Return unused.
annex_b_function_names.push(f);
}
}
}
}
// SKIP: 12. Let declaredVarNames be a new empty List.
// SKIP: 13. For each element d of varDeclarations, do
// SKIP: 14. NOTE: No abnormal terminations occur after this algorithm step unless varEnv is a
// Global Environment Record and the global object is a Proxy exotic object.
// SKIP: 15. Let lexDeclarations be the LexicallyScopedDeclarations of body.
// SKIP: 16. For each element d of lexDeclarations, do
// SKIP: 17. For each Parse Node f of functionsToInitialize, do
// SKIP: 18. For each String vn of declaredVarNames, do
// 19. Return unused.
Ok(())
}
impl ByteCompiler<'_> {
/// `GlobalDeclarationInstantiation ( script, env )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-globaldeclarationinstantiation
pub(crate) fn global_declaration_instantiation(&mut self, script: &Script) {
// 1. Let lexNames be the LexicallyDeclaredNames of script.
let lex_names = lexically_declared_names(script);
// 2. Let varNames be the VarDeclaredNames of script.
// 3. For each element name of lexNames, do
for name in lex_names {
let name = name.to_js_string(self.interner());
// c. Let hasRestrictedGlobal be ? env.HasRestrictedGlobalProperty(name).
// d. If hasRestrictedGlobal is true, throw a SyntaxError exception.
// Done in `Context::global_declaration_instantiation`
let index = self.get_or_insert_string(name);
self.global_lexs.push(index);
}
// 4. For each element name of varNames, do
// a. If HasLexicalDeclaration(env, name) is true, throw a SyntaxError exception.
// The scope analyzer already does this check for us.
// 5. Let varDeclarations be the VarScopedDeclarations of script.
// Note: VarScopedDeclarations for a Script node is TopLevelVarScopedDeclarations.
let var_declarations = var_scoped_declarations(script);
// 6. Let functionsToInitialize be a new empty List.
let mut functions_to_initialize = Vec::new();
// 7. Let declaredFunctionNames be a new empty List.
let mut declared_function_names = FxHashSet::default();
// 8. For each element d of varDeclarations, in reverse List order, do
for declaration in var_declarations.iter().rev() {
// a. If d is not either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
// a.i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// a.ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// a.iii. Let fn be the sole element of the BoundNames of d.
let name = match declaration {
VarScopedDeclaration::FunctionDeclaration(f) => f.name(),
VarScopedDeclaration::GeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
// a.iv. If declaredFunctionNames does not contain fn, then
if declared_function_names.insert(name.sym()) {
// 1. Let fnDefinable be ? env.CanDeclareGlobalFunction(fn).
// 2. If fnDefinable is false, throw a TypeError exception.
// Done in `Context::global_declaration_instantiation`.
// The names checked here are the same names from the functions
// in `functions_to_initialize`, but in reverse order, so we can
// reuse `global_fns` for this check.
// 4. Insert d as the first element of functionsToInitialize.
functions_to_initialize.push(declaration.clone());
}
}
functions_to_initialize.reverse();
// 9. Let declaredVarNames be a new empty List.
let mut declared_var_names = Vec::new();
let mut declared_var_names_set = FxHashSet::default();
// 10. For each element d of varDeclarations, do
// a. If d is either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
for declaration in var_declarations {
let VarScopedDeclaration::VariableDeclaration(declaration) = declaration else {
continue;
};
// i. For each String vn of the BoundNames of d, do
for name in bound_names(&declaration) {
// 1. If declaredFunctionNames does not contain vn, then
if !declared_function_names.contains(&name) {
// a. Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
// b. If vnDefinable is false, throw a TypeError exception.
// Done in `Context::global_declaration_instantiation`
// The names checked here are the same names from the functions
// in `declared_var_names`, so we can reuse `global_vars`
// for this check.
// c. If declaredVarNames does not contain vn, then
if declared_var_names_set.insert(name) {
// i. Append vn to declaredVarNames.
declared_var_names.push(name);
}
}
}
}
// 11. NOTE: No abnormal terminations occur after this algorithm step if the
// global object is an ordinary object. However, if the global object is
// a Proxy exotic object it may exhibit behaviours that cause abnormal
// terminations in some of the following steps.
// Steps 13-15 are covered by the scope analyzer.
// 16. For each Parse Node f of functionsToInitialize, do
for function in functions_to_initialize {
// a. Let fn be the sole element of the BoundNames of f.
let (name, generator, r#async, parameters, body, scopes, contains_direct_eval) =
match &function {
VarScopedDeclaration::FunctionDeclaration(f) => (
f.name(),
false,
false,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::GeneratorDeclaration(f) => (
f.name(),
true,
false,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => (
f.name(),
false,
true,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => (
f.name(),
true,
true,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
let func_span = function.linear_span();
let spanned_source_text = SpannedSourceText::new(self.source_text(), func_span);
let code = FunctionCompiler::new(spanned_source_text)
.name(name.sym().to_js_string(self.interner()))
.generator(generator)
.r#async(r#async)
.strict(self.strict())
.in_with(self.in_with)
.source_path(self.source_path.clone())
.compile(
parameters,
body,
self.variable_scope.clone(),
self.lexical_scope.clone(),
&scopes,
contains_direct_eval,
self.interner,
);
// Ensures global functions are printed when generating the global flowgraph.
let name_index = self.get_or_insert_name(name.sym());
let function_index = self.push_function_to_constants(code);
// b. Let fo be InstantiateFunctionObject of f with arguments env and privateEnv.
// c. Perform ? env.CreateGlobalFunctionBinding(fn, fo, false).
// Done in `Context::global_declaration_instantiation`
self.global_fns.push(GlobalFunctionBinding {
name_index,
function_index,
});
}
// 17 is done in `Context::global_declaration_instantiation
// 17. For each String vn of declaredVarNames, do
for var in declared_var_names {
let index = self.get_or_insert_name(var);
self.global_vars.push(index);
// a. Perform ? env.CreateGlobalVarBinding(vn, false).
// Done in `Context::global_declaration_instantiation`
}
// 18. Return unused.
}
/// `BlockDeclarationInstantiation ( code, env )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
pub(crate) fn block_declaration_instantiation<'a, N>(&mut self, block: &'a N)
where
&'a N: Into<NodeRef<'a>>,
{
// 1. Let declarations be the LexicallyScopedDeclarations of code.
let declarations = lexically_scoped_declarations(block);
// Note: Not sure if the spec is wrong here or if our implementation just differs too much,
// but we need 3.a to be finished for all declarations before 3.b can be done.
// b. If d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
// i. Let fn be the sole element of the BoundNames of d.
// ii. Let fo be InstantiateFunctionObject of d with arguments env and privateEnv.
// iii. Perform ! env.InitializeBinding(fn, fo). NOTE: This step is replaced in section B.3.2.6.
// TODO: Support B.3.2.6.
for d in declarations {
match d {
LexicallyScopedDeclaration::FunctionDeclaration(function) => {
let dst = self.register_allocator.alloc();
self.function_with_binding(function.into(), NodeKind::Declaration, &dst);
self.register_allocator.dealloc(dst);
}
LexicallyScopedDeclaration::GeneratorDeclaration(function) => {
let dst = self.register_allocator.alloc();
self.function_with_binding(function.into(), NodeKind::Declaration, &dst);
self.register_allocator.dealloc(dst);
}
LexicallyScopedDeclaration::AsyncFunctionDeclaration(function) => {
let dst = self.register_allocator.alloc();
self.function_with_binding(function.into(), NodeKind::Declaration, &dst);
self.register_allocator.dealloc(dst);
}
LexicallyScopedDeclaration::AsyncGeneratorDeclaration(function) => {
let dst = self.register_allocator.alloc();
self.function_with_binding(function.into(), NodeKind::Declaration, &dst);
self.register_allocator.dealloc(dst);
}
_ => {}
}
}
// 4. Return unused.
}
/// `EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-evaldeclarationinstantiation
pub(crate) fn eval_declaration_instantiation(
&mut self,
body: &Script,
#[allow(
unused_variables,
reason = "only used when the `annex-b` feature is enabled"
)]
strict: bool,
var_env: &Scope,
bindings: EvalDeclarationBindings,
) {
// 2. Let varDeclarations be the VarScopedDeclarations of body.
let var_declarations = var_scoped_declarations(body);
// SKIP: 3. If strict is false, then
// covered by the scope analyzer.
// NOTE: These steps depend on the current environment state are done before bytecode compilation,
// in `eval_declaration_instantiation_context`.
//
// SKIP: 4. Let privateIdentifiers be a new empty List.
// SKIP: 5. Let pointer be privateEnv.
// SKIP: 6. Repeat, while pointer is not null,
// a. For each Private Name binding of pointer.[[Names]], do
// i. If privateIdentifiers does not contain binding.[[Description]],
// append binding.[[Description]] to privateIdentifiers.
// b. Set pointer to pointer.[[OuterPrivateEnvironment]].
// SKIP: 7. If AllPrivateIdentifiersValid of body with argument privateIdentifiers is false, throw a SyntaxError exception.
// 8. Let functionsToInitialize be a new empty List.
let mut functions_to_initialize = Vec::new();
// 9. Let declaredFunctionNames be a new empty List.
let mut declared_function_names = FxHashSet::default();
// 10. For each element d of varDeclarations, in reverse List order, do
for declaration in var_declarations.iter().rev() {
// a. If d is not either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
// a.i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// a.ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// a.iii. Let fn be the sole element of the BoundNames of d.
let name = match &declaration {
VarScopedDeclaration::FunctionDeclaration(f) => f.name(),
VarScopedDeclaration::GeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => f.name(),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => f.name(),
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
// a.iv. If declaredFunctionNames does not contain fn, then
if declared_function_names.insert(name.sym()) {
// 1. If varEnv is a Global Environment Record, then
// a. Let fnDefinable be ? varEnv.CanDeclareGlobalFunction(fn).
// b. If fnDefinable is false, throw a TypeError exception.
// Done in `Context::eval_declaration_instantiation`
// The names checked here are the same names from the functions
// in `functions_to_initialize`, but in reverse order, so we can
// reuse `global_fns` for this check.
// 3. Insert d as the first element of functionsToInitialize.
functions_to_initialize.push(declaration.clone());
}
}
functions_to_initialize.reverse();
// 11. NOTE: Annex B.3.2.3 adds additional steps at this point.
// 11. If strict is false, then
#[cfg(feature = "annex-b")]
if !strict {
// NOTE: This diviates from the specification, we split the first part of defining the annex-b names
// in `eval_declaration_instantiation_context`, because it depends on the context.
if !var_env.is_global() {
for binding in bindings.new_annex_b_function_names {
// i. Let bindingExists be ! varEnv.HasBinding(F).
// ii. If bindingExists is false, then
// i. Perform ! varEnv.CreateMutableBinding(F, true).
// ii. Perform ! varEnv.InitializeBinding(F, undefined).
use crate::vm::CallFrame;
let index = self.insert_binding(binding);
self.emit_binding_access(
BindingAccessOpcode::DefInitVar,
&index,
&CallFrame::undefined_register(),
);
}
}
}
// 12. Let declaredVarNames be a new empty List.
let mut declared_var_names = Vec::new();
let mut declared_var_names_set = FxHashSet::default();
// 13. For each element d of varDeclarations, do
for declaration in var_declarations {
// a. If d is either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
let VarScopedDeclaration::VariableDeclaration(declaration) = declaration else {
continue;
};
// a.i. For each String vn of the BoundNames of d, do
for name in bound_names(&declaration) {
// 1. If declaredFunctionNames does not contain vn, then
if !declared_function_names.contains(&name) {
// a. If varEnv is a Global Environment Record, then
// i. Let vnDefinable be ? varEnv.CanDeclareGlobalVar(vn).
// ii. If vnDefinable is false, throw a TypeError exception.
// Done in `Context::eval_declaration_instantiation`
// The names checked here are the same names from the functions
// in `declared_var_names`, so we can reuse `global_vars`
// for this check.
// b. If declaredVarNames does not contain vn, then
if declared_var_names_set.insert(name) {
// i. Append vn to declaredVarNames.
declared_var_names.push(name);
}
}
}
}
// 14. NOTE: No abnormal terminations occur after this algorithm step unless varEnv is a
// Global Environment Record and the global object is a Proxy exotic object.
// 15. Let lexDeclarations be the LexicallyScopedDeclarations of body.
// 16. For each element d of lexDeclarations, do
// 17. For each Parse Node f of functionsToInitialize, do
for function in functions_to_initialize {
// a. Let fn be the sole element of the BoundNames of f.
let (name, generator, r#async, parameters, body, scopes, contains_direct_eval) =
match &function {
VarScopedDeclaration::FunctionDeclaration(f) => (
f.name(),
false,
false,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::GeneratorDeclaration(f) => (
f.name(),
true,
false,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => (
f.name(),
false,
true,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => (
f.name(),
true,
true,
f.parameters(),
f.body(),
f.scopes().clone(),
f.contains_direct_eval(),
),
VarScopedDeclaration::VariableDeclaration(_) => {
continue;
}
};
let func_span = function.linear_span();
let spanned_source_text = SpannedSourceText::new(self.source_text(), func_span);
let code = FunctionCompiler::new(spanned_source_text)
.name(name.sym().to_js_string(self.interner()))
.generator(generator)
.r#async(r#async)
.strict(self.strict())
.in_with(self.in_with)
.name_scope(None)
.compile(
parameters,
body,
self.variable_scope.clone(),
self.lexical_scope.clone(),
&scopes,
contains_direct_eval,
self.interner,
);
// b. Let fo be InstantiateFunctionObject of f with arguments lexEnv and privateEnv.
let index = self.push_function_to_constants(code);
// c. If varEnv is a Global Environment Record, then
if var_env.is_global() {
// i. Perform ? varEnv.CreateGlobalFunctionBinding(fn, fo, true).
// Done in `Context::eval_declaration_instantiation`
let name_index = self.get_or_insert_name(name.sym());
self.global_fns.push(GlobalFunctionBinding {
name_index,
function_index: index,
});
}
// d. Else,
else {
let dst = self.register_allocator.alloc();
self.emit_get_function(&dst, index);
// i. Let bindingExists be ! varEnv.HasBinding(fn).
let (binding, binding_exists) = bindings
.new_function_names
.get(&name)
.expect("binding must exist");
// ii. If bindingExists is false, then
// iii. Else,
if *binding_exists {
// 1. Perform ! varEnv.SetMutableBinding(fn, fo, false).
let index = self.insert_binding(binding.clone());
self.emit_binding_access(BindingAccessOpcode::SetName, &index, &dst);
} else {
// 1. NOTE: The following invocation cannot return an abrupt completion because of the validation preceding step 14.
// 2. Perform ! varEnv.CreateMutableBinding(fn, true).
// 3. Perform ! varEnv.InitializeBinding(fn, fo).
let index = self.insert_binding(binding.clone());
self.emit_binding_access(BindingAccessOpcode::DefInitVar, &index, &dst);
}
self.register_allocator.dealloc(dst);
}
}
// 18. For each String vn of declaredVarNames, do
for name in declared_var_names {
// a. If varEnv is a Global Environment Record, then
if var_env.is_global() {
let index = self.get_or_insert_name(name);
// i. Perform ? varEnv.CreateGlobalVarBinding(vn, true).
// Done in `Context::eval_declaration_instantiation`
self.global_vars.push(index);
}
}
// 18.b
for binding in bindings.new_var_names {
// i. Let bindingExists be ! varEnv.HasBinding(vn).
// ii. If bindingExists is false, then
// 1. NOTE: The following invocation cannot return an abrupt completion because of the validation preceding step 14.
// 2. Perform ! varEnv.CreateMutableBinding(vn, true).
// 3. Perform ! varEnv.InitializeBinding(vn, undefined).
let index = self.insert_binding(binding);
self.emit_binding_access(
BindingAccessOpcode::DefInitVar,
&index,
&CallFrame::undefined_register(),
);
}
// 19. Return unused.
}
/// `FunctionDeclarationInstantiation ( func, argumentsList )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
pub(crate) fn function_declaration_instantiation(
&mut self,
body: &FunctionBody,
formals: &FormalParameterList,
arrow: bool,
strict: bool,
generator: bool,
scopes: &FunctionScopes,
) {
// 1. Let calleeContext be the running execution context.
// 2. Let code be func.[[ECMAScriptCode]].
// 3. Let strict be func.[[Strict]].
// 4. Let formals be func.[[FormalParameters]].
// 5. Let parameterNames be the BoundNames of formals.
let mut parameter_names = bound_names(formals);
// 6. If parameterNames has any duplicate entries, let hasDuplicates be true. Otherwise, let hasDuplicates be false.
// let has_duplicates = formals.has_duplicates();
// 7. Let simpleParameterList be IsSimpleParameterList of formals.
// let simple_parameter_list = formals.is_simple();
// 8. Let hasParameterExpressions be ContainsExpression of formals.
let has_parameter_expressions = formals.has_expressions();
// 9. Let varNames be the VarDeclaredNames of code.
let var_names = var_declared_names(body);
// 10. Let varDeclarations be the VarScopedDeclarations of code.
let var_declarations = var_scoped_declarations(body);
// 11. Let lexicalNames be the LexicallyDeclaredNames of code.
let lexical_names = lexically_declared_names(body);
// 12. Let functionNames be a new empty List.
let mut function_names = FxHashSet::default();
// 13. Let functionsToInitialize be a new empty List.
let mut functions_to_initialize = Vec::new();
// 14. For each element d of varDeclarations, in reverse List order, do
for declaration in var_declarations.iter().rev() {
// a. If d is neither a VariableDeclaration nor a ForBinding nor a BindingIdentifier, then
// a.i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// a.ii. Let fn be the sole element of the BoundNames of d.
let (name, function) = match declaration {
VarScopedDeclaration::FunctionDeclaration(f) => (f.name(), FunctionSpec::from(f)),
VarScopedDeclaration::GeneratorDeclaration(f) => (f.name(), FunctionSpec::from(f)),
VarScopedDeclaration::AsyncFunctionDeclaration(f) => {
(f.name(), FunctionSpec::from(f))
}
VarScopedDeclaration::AsyncGeneratorDeclaration(f) => {
(f.name(), FunctionSpec::from(f))
}
VarScopedDeclaration::VariableDeclaration(_) => continue,
};
// a.iii. If functionNames does not contain fn, then
if function_names.insert(name.sym()) {
// 1. Insert fn as the first element of functionNames.
// 2. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// 3. Insert d as the first element of functionsToInitialize.
functions_to_initialize.push(function);
}
}
functions_to_initialize.reverse();
// 15. Let argumentsObjectNeeded be true.
let mut arguments_object_needed = true;
let arguments = Sym::ARGUMENTS;
// 16. If func.[[ThisMode]] is lexical, then
// 17. Else if parameterNames contains "arguments", then
if arrow || parameter_names.contains(&arguments) {
// 16.a. NOTE: Arrow functions never have an arguments object.
// 16.b. Set argumentsObjectNeeded to false.
// 17.a. Set argumentsObjectNeeded to false.
arguments_object_needed = false;
}
// 18. Else if hasParameterExpressions is false, then
else if !has_parameter_expressions {
//a. If functionNames contains "arguments" or lexicalNames contains "arguments", then
if function_names.contains(&arguments) || lexical_names.contains(&arguments) {
// i. Set argumentsObjectNeeded to false.
arguments_object_needed = false;
}
}
if arguments_object_needed {
arguments_object_needed = scopes.arguments_object_accessed();
}
// 19-20
drop(self.push_declarative_scope(scopes.parameters_eval_scope()));
let scope = self.lexical_scope.clone();
// 22. If argumentsObjectNeeded is true, then
//
// NOTE(HalidOdat): Has been moved up, so "arguments" gets registered as
// the first binding in the environment with index 0.
if arguments_object_needed {
let arguments = arguments.to_js_string(self.interner());
// a. If strict is true or simpleParameterList is false, then
let value = self.register_allocator.alloc();