-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSpecialSymbolCapture.js
1000 lines (906 loc) · 51.7 KB
/
SpecialSymbolCapture.js
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.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
var lambdaForGlobalThis = () => this;
var tests = [
{
name: "Simple function defined at global scope with a this binding and global has no this reference",
body: function () {
WScript.LoadScript(`
function bar() {
this.o = 'bar';
}
var _this = { o: 'global' };
bar.call(_this);
assert.areEqual('bar', _this.o, "Function bar mutated _this, which was loaded as the 'this' binding inside bar");
`);
}
},
{
name: "Simple binding of 'this' in global function",
body: function () {
WScript.LoadScript(`
assert.areEqual(undefined, this.o, "Initial value of 'this.o' is undefined");
this.o = 'global';
assert.areEqual('global', this.o, "Writing property to 'this' binding in global function");
`);
}
},
{
name: "Lambda capturing global 'this' binding. Note: This isn't really a capture.",
body: function () {
WScript.LoadScript(`
var lambdaCapturesGlobalThis = str => this.o = str
this.o = 'global';
lambdaCapturesGlobalThis('called at global');
assert.areEqual('called at global', this.o, "Lambda modifying global this binding called from global function");
this.o = 'global';
function foo() {
lambdaCapturesGlobalThis('called from nested function');
}
foo();
assert.areEqual('called from nested function', this.o, "Lambda modifying global this binding called from nested function");
this.o = 'global';
var globalLambda = () => lambdaCapturesGlobalThis('called from global lambda')
globalLambda();
assert.areEqual('called from global lambda', this.o, "Lambda modifying global this binding called from global lambda");
this.o = 'global';
eval("lambdaCapturesGlobalThis('called from global eval');");
assert.areEqual('called from global eval', this.o, "Lambda modifying global this binding called from global eval");
`);
}
},
{
name: "Function with eval and lambda both modifying 'this'",
body: function () {
WScript.LoadScript(`
function bar() {
eval('this.o = "eval bar"');
(() => this.o = 'lambda bar')()
}
var _this = { o: 'global' };
bar.call(_this);
assert.areEqual('lambda bar', _this.o, "Lambda was the last thing to modify 'this' binding inside bar");
`);
}
},
{
name: "Capture of nested 'this' object",
body: function () {
function foo() {
var count = 0;
this.o = 'foo';
eval("count++; assert.areEqual('foo', this.o, 'Eval capture of nested-function this binding')");
eval(`count++; eval("assert.areEqual('foo', this.o, 'Nested eval capture of nested-function this binding')")`);
(() => {count++; assert.areEqual('foo', this.o, 'Lambda capture of nested function this binding')})();
(() => () => {count++; assert.areEqual('foo', this.o, 'Nested lambda capture of nested function this binding')})()();
(() => eval(`count++; assert.areEqual('foo', this.o, 'Lambda with eval capture of nested function this binding')`))();
(() => eval(`count++; eval("assert.areEqual('foo', this.o, 'Lambda with nested eval capture of nested function this binding')")`))();
eval(`(() => {count++; assert.areEqual('foo', this.o, 'Eval with lambda capture of nested-function this binding')})()`);
eval("eval(`(() => {count++; assert.areEqual('foo', this.o, 'Nested eval with lambda capture of nested-function this binding')})()`)");
eval("(() => eval(`(() => {count++; assert.areEqual('foo', this.o, 'Nested eval and nested lambda capture of nested-function this binding')})()`))()");
eval("count++; assert.areEqual('foo', this.o, 'Eval with nested lambda in which the eval also references this binding'); (() => assert.areEqual('foo', this.o, 'Eval with lambda capture of nested-function this binding'))();");
assert.areEqual(10, count, 'Called correct number of test functions');
return true;
}
assert.isTrue(foo.call({o:'test'}), "Test function completes");
}
},
{
name: "Eval modifying 'this' inside a nested function which doesn't itself reference 'this' binding",
body: function () {
WScript.LoadScript(`
function foo() {
this.o = 'foo';
function bar() {
eval('this.o = "eval bar"');
}
bar();
}
this.o = 'global';
foo();
assert.areEqual('eval bar', this.o, "Eval was the last thing to modify 'this' binding inside bar");
`);
}
},
{
name: "Various usage patterns for 'this'",
body: function () {
WScript.LoadScript(`
eval('this.o = "global eval"');
assert.areEqual('global eval', this.o, "Global eval modifying this");
(() => this.o = 'global lambda')();
assert.areEqual('global lambda', this.o, "Global lambda modifying this");
function bar () {
this.o = 'bar';
return this;
}
var _this = bar();
assert.areEqual('bar', this.o, "Nested function modifies it's own this binding");
assert.areEqual(_this, this, "bar returns local this which is the same object as global this");
function baz() {
this.o = 'baz';
return this;
}
_this = baz.call({});
assert.areEqual('bar', this.o, "Function baz doesn't modify global this object");
assert.isFalse(_this === this, "baz returns local this which is not the same object as global this");
function bot(_this) {
_this.o = 'bot';
return _this;
}
_this = bot(this);
assert.areEqual('bot', this.o, "Nested function modifies global this object via a different binding");
assert.areEqual(_this, this, "bot returns local this which is the same object as global this");
function foo() {
assert.areEqual('global', this.o, "The global this binding is passed as the this argument to foo");
this.o = 'foo';
assert.areEqual('foo', this.o, "Simple property assignment");
function bar() {
this.o = 'bar';
return this;
}
_this = bar.call(this);
assert.areEqual('bar', this.o, "Nested function modifies outside this via it's own this binding");
assert.areEqual(_this, this, "bar returns local this which is the same object as foo's this");
function baz() {
this.o = 'baz';
return this;
}
_this = baz.call({});
assert.areEqual('bar', this.o, "Nested function modifies it's own this binding");
assert.isFalse(_this === this, "baz returns local this which is not the same object as foo's this");
function bot(_this) {
_this.o = 'bot';
return _this;
}
_this = bot(this);
assert.areEqual('bot', this.o, "Nested function modifies outside this via a different binding");
assert.areEqual(_this, this, "bot returns local this which is the same object as foo's this");
return this;
}
this.o = 'global';
_this = foo.call(this);
assert.areEqual(_this, this, "foo returns local this which is the same object as global this");
`);
}
},
{
name: "Strict mode outside 'this' object is not captured by nested functions",
body: function () {
WScript.LoadScript(`
"use strict";
this.o = 'global';
function bar() {
assert.areEqual(undefined, this, "Global this object is not loaded by default in nested functions in strict mode");
}
bar();
function baz() {
this.o = 'baz';
return this;
}
var _this = baz.call({});
assert.isFalse(_this === this, "baz returns this which is not bound to global this");
assert.areEqual('baz', _this.o, "baz returned this which was passed to it via call()");
function foo() {
assert.isTrue(this !== undefined, "foo must be called with an object or something");
function bar() {
assert.areEqual(undefined, this, "Outside this object is not loaded by default in nested functions in strict mode");
}
bar();
}
foo.call({});
`);
}
},
{
name: "Special names are invalid assignment targets",
body: function () {
assert.throws(() => eval(`
function f() {
if (0) new.target = 1;
}
`), SyntaxError, "", "Invalid left-hand side in assignment.");
assert.throws(() => eval(`
function f() {
if (0) this = 1;
}
`), SyntaxError, "", "Invalid left-hand side in assignment.");
assert.throws(() => eval(`
function f() {
if (0) super = 1;
}
`), SyntaxError, "", "Invalid use of the 'super' keyword");
}
},
{
name: "Global-scope `new.target` keyword is early syntax error",
body: function () {
var syntaxError;
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; new.target;`), syntaxError, "'new.target' is invalid syntax at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; () => new.target`), syntaxError, "'new.target' is invalid in arrow at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; () => () => new.target`), syntaxError, "'new.target' is invalid in nested arrow at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval('new.target');`), syntaxError, "'new.target' is invalid in eval at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval("eval('new.target');")`), syntaxError, "'new.target' is invalid in nested eval at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; eval('() => new.target')`), syntaxError, "'new.target' is invalid in arrow nested in eval at global scope", "Invalid use of the 'new.target' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; (() => eval('new.target'))()`), syntaxError, "'new.target' is invalid in eval nested in arrow at global scope", "Invalid use of the 'new.target' keyword");
}
},
{
name: "Simple captures of new.target within ordinary functions",
body: function () {
function foo() {
return new.target;
}
assert.areEqual(foo, new foo(), "Simple function returning new.target binding");
assert.areEqual(undefined, foo(), "Simple function binds new.target to undefined without new keyword");
function foo2() {
var a = () => new.target;
return a();
}
assert.areEqual(foo2, new foo2(), "Function returning new.target captured by nested arrow");
assert.areEqual(undefined, foo2(), "Function with arrow capturing new.target binds new.target to undefined without new keyword");
function foo3() {
return eval('new.target');
}
assert.areEqual(foo3, new foo3(), "Function returning new.target captured by nested eval");
assert.areEqual(undefined, foo3(), "Function with eval capturing new.target binds new.target to undefined without new keyword");
function foo4() {
var a = () => () => new.target;
return a()();
}
assert.areEqual(foo4, new foo4(), "Function returning new.target captured by nested arrows");
assert.areEqual(undefined, foo4(), "Function with nested arrows capturing new.target binds new.target to undefined without new keyword");
function foo5() {
return eval("eval('new.target');");
}
assert.areEqual(foo5, new foo5(), "Function returning new.target captured by nested evals");
assert.areEqual(undefined, foo5(), "Function with nested evals capturing new.target binds new.target to undefined without new keyword");
function foo6() {
var a = () => eval('new.target');
return a();
}
assert.areEqual(foo6, new foo6(), "Function returning new.target captured by arrow captured by eval");
assert.areEqual(undefined, foo6(), "Function with nested evals capturing new.target binds new.target to undefined without new keyword");
}
},
{
name: "Base class empty constructor symbol binding",
body: function() {
class BaseDefault { }
assert.areEqual(BaseDefault, new BaseDefault().constructor, "Base class with default constructor");
eval(`
class BaseEvalDefault { }
assert.areEqual(BaseEvalDefault, new BaseEvalDefault().constructor, "Base class defined in eval with default constructor");
`);
class BaseEmpty {
constructor() { }
}
assert.areEqual(BaseEmpty, new BaseEmpty().constructor, "Base class with empty constructor");
eval(`
class BaseEvalEmpty {
constructor() { }
}
assert.areEqual(BaseEvalEmpty, new BaseEvalEmpty().constructor, "Base class defined in eval with empty constructor");
`);
}
},
{
name: "Base class 'this' binding",
body: function() {
class BaseReturnThis {
constructor() {
return this;
}
}
assert.areEqual(BaseReturnThis, new BaseReturnThis().constructor, "Base class with constructor that returns this");
class BaseReturnThisEval {
constructor() {
return eval('this');
}
}
assert.areEqual(BaseReturnThisEval, new BaseReturnThisEval().constructor, "Base class with constructor that returns this captured by eval");
class BaseReturnThisArrow {
constructor() {
return (() => this)();
}
}
assert.areEqual(BaseReturnThisArrow, new BaseReturnThisArrow().constructor, "Base class with constructor that returns this captured by arrow");
eval(`
class BaseEvalReturnThis {
constructor() {
return this;
}
}
assert.areEqual(BaseEvalReturnThis, new BaseEvalReturnThis().constructor, "Base class defined in eval with constructor that returns this");
`);
eval(`
class BaseEvalReturnThisEval {
constructor() {
return eval('this');
}
}
assert.areEqual(BaseEvalReturnThisEval, new BaseEvalReturnThisEval().constructor, "Base class defined in eval with constructor that returns this captured via eval");
`);
eval(`
class BaseEvalReturnThisLambda {
constructor() {
return (() => this)();
}
}
assert.areEqual(BaseEvalReturnThisLambda, new BaseEvalReturnThisLambda().constructor, "Base class defined in eval with constructor that returns this captured via arrow");
`);
eval(`
class BaseEvalReturnThisEvalLambda {
constructor() {
return eval('(() => this)()');
}
}
assert.areEqual(BaseEvalReturnThisEvalLambda, new BaseEvalReturnThisEvalLambda().constructor, "Base class defined in eval with constructor that returns this captured via arrow inside eval");
`);
eval(`
class BaseEvalReturnThisLambdaEval {
constructor() {
return (() => eval('this'))();
}
}
assert.areEqual(BaseEvalReturnThisLambdaEval, new BaseEvalReturnThisLambdaEval().constructor, "Base class defined in eval with constructor that returns this captured via eval inside arrow");
`);
var called = false;
class BaseVerifyThis {
constructor() {
assert.areEqual(BaseVerifyThis, this.constructor, "Base class this binding is an instance of the class");
called = true;
}
}
assert.areEqual(BaseVerifyThis, new BaseVerifyThis().constructor, "Base class constructor uses this object");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyThisEval {
constructor() {
eval(`
assert.areEqual(BaseVerifyThisEval, this.constructor, "Base class this binding is an instance of the class");
called = true;
`);
}
}
assert.areEqual(BaseVerifyThisEval, new BaseVerifyThisEval().constructor, "Base class constructor uses this object inside eval");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyThisArrow {
constructor() {
assert.areEqual(BaseVerifyThisArrow, (() => this)().constructor, "Base class this binding is an instance of the class");
called = true;
}
}
assert.areEqual(BaseVerifyThisArrow, new BaseVerifyThisArrow().constructor, "Base class constructor uses this object inside arrow");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyThisEvalArrow {
constructor() {
assert.areEqual(BaseVerifyThisEvalArrow, eval('(() => this)()').constructor, "Base class this binding is an instance of the class");
called = true;
}
}
assert.areEqual(BaseVerifyThisEvalArrow, new BaseVerifyThisEvalArrow().constructor, "Base class constructor uses this object inside arrow inside eval");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyThisArrowEval {
constructor() {
assert.areEqual(BaseVerifyThisArrowEval, (() => eval('this'))().constructor, "Base class this binding is an instance of the class");
called = true;
}
}
assert.areEqual(BaseVerifyThisArrowEval, new BaseVerifyThisArrowEval().constructor, "Base class constructor uses this object inside eval inside arrow");
assert.isTrue(called, "Constructor was actually called");
}
},
{
name: "Base class new.target binding",
body: function() {
var called = false;
class BaseVerifyNewTarget {
constructor() {
assert.areEqual(BaseVerifyNewTarget, new.target, "Base class called as new expression gets new.target");
called = true;
}
}
assert.areEqual(BaseVerifyNewTarget, new BaseVerifyNewTarget().constructor, "Base class constructor uses new.target");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyNewTargetEval {
constructor() {
eval(`
assert.areEqual(BaseVerifyNewTargetEval, new.target, "Base class called as new expression gets new.target");
called = true;
`);
}
}
assert.areEqual(BaseVerifyNewTargetEval, new BaseVerifyNewTargetEval().constructor, "Base class constructor uses new.target inside eval");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyNewTargetLambda {
constructor() {
(() => {
assert.areEqual(BaseVerifyNewTargetLambda, new.target, "Base class called as new expression gets new.target");
called = true;
})();
}
}
assert.areEqual(BaseVerifyNewTargetLambda, new BaseVerifyNewTargetLambda().constructor, "Base class constructor uses new.target inside lambda");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyNewTargetEvalLambda {
constructor() {
eval(`
(() => {
assert.areEqual(BaseVerifyNewTargetEvalLambda, new.target, "Base class called as new expression gets new.target");
called = true;
})();
`);
}
}
assert.areEqual(BaseVerifyNewTargetEvalLambda, new BaseVerifyNewTargetEvalLambda().constructor, "Base class constructor uses new.target inside lambda inside eval");
assert.isTrue(called, "Constructor was actually called");
called = false;
class BaseVerifyNewTargetLambdaEval {
constructor() {
(() => {
eval(`
assert.areEqual(BaseVerifyNewTargetLambdaEval, new.target, "Base class called as new expression gets new.target");
called = true;
`);
})();
}
}
assert.areEqual(BaseVerifyNewTargetLambdaEval, new BaseVerifyNewTargetLambdaEval().constructor, "Base class constructor uses new.target inside eval inside lambda");
assert.isTrue(called, "Constructor was actually called");
called = false;
eval(`
class BaseEvalVerifyNewTarget {
constructor() {
assert.areEqual(BaseEvalVerifyNewTarget, new.target, "Base class called as new expression gets new.target");
called = true;
}
}
assert.areEqual(BaseEvalVerifyNewTarget, new BaseEvalVerifyNewTarget().constructor, "Base class defined in eval constructor uses new.target");
assert.isTrue(called, "Constructor was actually called");
`);
}
},
{
name: "Base class super call is syntax error",
body: function() {
var syntaxError;
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { super(); } }`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { eval("super();"); } }; new Base();`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
assert.throws(() => WScript.LoadScript(`syntaxError = SyntaxError; class Base { constructor() { () => super(); } }`), syntaxError, "'super' call is invalid syntax in a base class", "Invalid use of the 'super' keyword");
}
},
{
name: "Class derived from null special symbol binding",
body: function() {
class DerivedDefault extends null { }
assert.throws(() => new DerivedDefault(), TypeError, "Class derived from null can't be instantiated", "Function is not a constructor");
class DerivedSuper extends null {
constructor() {
super();
}
}
assert.throws(() => new DerivedSuper(), TypeError, "Class derived from null can't make a super call", "Function is not a constructor");
}
},
{
name: "Derived class 'this' binding",
body: function() {
class Base { }
class DerivedReturnThis extends Base {
constructor() {
super();
return this;
}
}
assert.areEqual(DerivedReturnThis, new DerivedReturnThis().constructor, "Derived class with constructor that returns this");
class DerivedReturnThisEval extends Base {
constructor() {
super();
return eval('this');
}
}
assert.areEqual(DerivedReturnThisEval, new DerivedReturnThisEval().constructor, "Derived class with constructor that returns this captured by eval");
class DerivedReturnThisArrow extends Base {
constructor() {
super();
return (() => this)();
}
}
assert.areEqual(DerivedReturnThisArrow, new DerivedReturnThisArrow().constructor, "Derived class with constructor that returns this captured by arrow");
class DerivedReturnThisEvalArrow extends Base {
constructor() {
super();
return eval('(() => this)()');
}
}
assert.areEqual(DerivedReturnThisEvalArrow, new DerivedReturnThisEvalArrow().constructor, "Derived class with constructor that returns this captured by arrow inside eval");
class DerivedReturnThisArrowEval extends Base {
constructor() {
super();
return (() => eval('this'))();
}
}
assert.areEqual(DerivedReturnThisArrowEval, new DerivedReturnThisArrowEval().constructor, "Derived class with constructor that returns this captured by eval inside arrow");
eval(`
class DerivedEvalReturnThis extends Base {
constructor() {
super();
return this;
}
}
assert.areEqual(DerivedEvalReturnThis, new DerivedEvalReturnThis().constructor, "Derived class defined in eval with constructor that returns this");
`);
eval(`
class DerivedEvalReturnThisEval extends Base {
constructor() {
super();
return eval('this');
}
}
assert.areEqual(DerivedEvalReturnThisEval, new DerivedEvalReturnThisEval().constructor, "Derived class defined in eval with constructor that returns this captured by eval");
`);
eval(`
class DerivedEvalReturnThisLambda extends Base {
constructor() {
super();
return (() => this)();
}
}
assert.areEqual(DerivedEvalReturnThisLambda, new DerivedEvalReturnThisLambda().constructor, "Derived class defined in eval with constructor that returns this captured by arrow");
`);
}
},
{
name: "Derived class verifying 'this' binding",
body: function() {
class Base { }
var called = false;
class DerivedVerifyThis extends Base {
constructor() {
super();
assert.areEqual(DerivedVerifyThis, this.constructor, "Derived class with constructor that verifies this");
called = true;
}
}
assert.areEqual(DerivedVerifyThis, new DerivedVerifyThis().constructor, "Derived class with constructor that verifies this");
assert.isTrue(called, "Constructor was called");
called = false;
class DerivedVerifyThisLambda extends Base {
constructor() {
super();
(() => {
assert.areEqual(DerivedVerifyThisLambda, this.constructor, "Derived class with constructor that verifies this captured in lambda");
called = true;
})();
}
}
assert.areEqual(DerivedVerifyThisLambda, new DerivedVerifyThisLambda().constructor, "Derived class with constructor that verifies this captured by arrow");
assert.isTrue(called, "Constructor was called");
eval(`
called = false;
class DerivedEvalVerifyThis extends Base {
constructor() {
super();
assert.areEqual(DerivedEvalVerifyThis, this.constructor, "Derived class defined in eval with constructor that verifies this");
called = true;
}
}
assert.areEqual(DerivedEvalVerifyThis, new DerivedEvalVerifyThis().constructor, "Derived class defined in eval with constructor that verifies this");
assert.isTrue(called, "Constructor was called");
`);
}
},
{
name: "Assignment to special symbols is invalid",
body: function() {
function test(code, message) {
assert.throws(() => WScript.LoadScript(code), SyntaxError, message, "Invalid left-hand side in assignment.");
}
function testglobal(symname) {
test(`${symname} = 'something'`, `Assignment to global '${symname}'`);
test(`(() => ${symname} = 'something')()`, `Assignment to global '${symname}' captured by lambda`);
test(`eval("${symname} = 'something'")`, `Assignment to global '${symname}' in eval`);
test(`(() => eval("${symname} = 'something'"))()`, `Assignment to global '${symname}' captured by lambda in eval`);
test(`eval("(() => ${symname} = 'something')()")`, `Assignment to global '${symname}' captured by eval in lambda`);
}
function testlocal(symname) {
test(`function foo() { ${symname} = 'something'; }; foo();`, `Assignment to function '${symname}'`);
test(`function foo() { eval("${symname} = 'something'"); }; foo();`, `Assignment to function '${symname}' in nested eval`);
test(`function foo() { (() => ${symname} = 'something')(); }; foo();`, `Assignment to function '${symname}' in nested lambda`);
}
testglobal('this');
testlocal('this');
testlocal('new.target');
}
},
{
name: "Eval 'this' binding",
body: function() {
function SloppyModeCapture() {
assert.areEqual('object', typeof this, "'this' object given to functions in sloppy mode");
this.o = 'SloppyModeCapture';
eval(`assert.areEqual('SloppyModeCapture', this.o, "Direct eval captures 'this' in sloppy mode")`);
eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Direct strict mode eval captures 'this' in sloppy mode")`);
var not_eval = eval;
not_eval(`assert.areEqual('SloppyModeCapture', this.o, "Indirect eval captures 'this' in sloppy mode")`);
not_eval(`'use strict'; assert.areEqual('SloppyModeCapture', this.o, "Indirect strict mode eval captures 'this' in sloppy mode")`);
}
SloppyModeCapture();
function StrictModeSanity() {
'use strict';
assert.areEqual(undefined, this, "Strict mode function doesn't get a 'this' object");
}
StrictModeSanity();
lambdaForGlobalThis().o = 'global';
function StrictModeCapture() {
'use strict';
assert.areEqual('object', typeof this, "'this' object should be passed-in");
eval(`assert.areEqual('StrictModeCapture', this.o, "Direct eval captures 'this' in strict mode")`);
eval(`'use strict'; assert.areEqual('StrictModeCapture', this.o, "Direct strict mode eval captures 'this' in strict mode")`);
var not_eval = eval;
not_eval(`assert.areEqual('global', this.o, "Indirect eval captures global 'this' in strict mode")`);
not_eval(`'use strict'; assert.areEqual('global', this.o, "Indirect strict mode eval captures global 'this' in strict mode")`);
}
StrictModeCapture.call({o:'StrictModeCapture'});
}
},
{
name: "Super reference patterns",
body: function() {
var key_bar = 'bar';
var key_foo = 'foo';
function verifyThis() {
assert.areEqual(obj, this, "'this' passed via super method call should be the object");
return 'bar';
}
var obj = {
callSuperMethodViaDot() { return super.bar(); },
callSuperMethodViaDotArrow() { return (() => super.bar())(); },
callSuperMethodViaDotEval() { return eval('super.bar();'); },
getSuperPropertyViaDot() { return super.foo; },
getSuperPropertyViaDotArrow() { return (() => super.foo)(); },
getSuperPropertyViaDotEval() { return eval('super.foo;'); },
callSuperMethodViaIndex() { return super[key_bar](); },
callSuperMethodViaIndexArrow() { return (() => super[key_bar]())(); },
callSuperMethodViaIndexEval() { return eval('super[key_bar]();'); },
getSuperPropertyViaIndex() { return super[key_foo]; },
getSuperPropertyViaIndexArrow() { return (() => super[key_foo])(); },
getSuperPropertyViaIndexEval() { return eval('super[key_foo];'); },
assignSuperPropertyViaDot() { super.foo = 'something'; return super.foo; },
assignSuperPropertyViaDotArrow() { (() => super.foo = 'something')(); return super.foo; },
assignSuperPropertyViaDotEval() { eval("super.foo = 'something';"); return super.foo; },
assignSuperPropertyViaIndex() { super[key_foo] = 'something'; return super.foo; },
assignSuperPropertyViaIndexArrow() { (() => super[key_foo] = 'something')(); return super.foo; },
assignSuperPropertyViaIndexEval() { eval("super[key_foo] = 'something';"); return super.foo; },
deleteSuperPropertyViaDot() { delete super.foo; },
deleteSuperPropertyViaDotArrow() { (() => delete super.foo)(); },
deleteSuperPropertyViaDotEval() { eval('delete super.foo;'); },
}
var proto = {
bar: verifyThis,
foo: 'foo',
}
Object.setPrototypeOf(obj, proto);
assert.areEqual('bar', obj.callSuperMethodViaDot(), "Call method via 'super.method()'");
assert.areEqual('bar', obj.callSuperMethodViaDotArrow(), "Call method via 'super.method()' in arrow");
assert.areEqual('bar', obj.callSuperMethodViaDotEval(), "Call method via 'super.method()' in eval");
assert.areEqual('foo', obj.getSuperPropertyViaDot(), "Get property via 'super.property'");
assert.areEqual('foo', obj.getSuperPropertyViaDotArrow(), "Get property via 'super.property' in arrow");
assert.areEqual('foo', obj.getSuperPropertyViaDotEval(), "Get property via 'super.property' in eval");
assert.areEqual('bar', obj.callSuperMethodViaIndex(), "Call method via 'super[method]()'");
assert.areEqual('bar', obj.callSuperMethodViaIndexArrow(), "Call method via 'super[method]()' in arrow");
assert.areEqual('bar', obj.callSuperMethodViaIndexEval(), "Call method via 'super[method]()' in eval");
assert.areEqual('foo', obj.getSuperPropertyViaIndex(), "Get property via 'super[property]'");
assert.areEqual('foo', obj.getSuperPropertyViaIndexArrow(), "Get property via 'super[property]' in arrow");
assert.areEqual('foo', obj.getSuperPropertyViaIndexEval(), "Get property via 'super[property]' in eval");
assert.areEqual('foo', obj.assignSuperPropertyViaDot(), "Assign property via 'super.property'");
assert.areEqual('foo', obj.assignSuperPropertyViaDotArrow(), "Assign property via 'super.property' in arrow");
assert.areEqual('foo', obj.assignSuperPropertyViaDotEval(), "Assign property via 'super.property' in eval");
assert.areEqual('something', obj.assignSuperPropertyViaIndex(), "Assign property via 'super[property]'");
assert.areEqual('something', obj.assignSuperPropertyViaIndexArrow(), "Assign property via 'super[property]' in arrow");
assert.areEqual('something', obj.assignSuperPropertyViaIndexEval(), "Assign property via 'super[property]' in eval");
assert.throws(() => obj.deleteSuperPropertyViaDot(), ReferenceError, "Delete property via 'super.property'", "Unable to delete property with a super reference");
assert.throws(() => obj.deleteSuperPropertyViaDotArrow(), ReferenceError, "Delete property via 'super.property' in arrow", "Unable to delete property with a super reference");
assert.throws(() => obj.deleteSuperPropertyViaDotEval(), ReferenceError, "Delete property via 'super.property' in eval", "Unable to delete property with a super reference");
}
},
{
name: "Split-scope with eval in parameter scope",
body: function() {
function f1({a:a}, c = eval("a")) {
return c;
}
assert.areEqual('ok', f1({a:'ok'}), "Eval references previous non-simple formal");
function f2({a:a}, c = eval('this')) {
return c;
}
var _this = {};
assert.areEqual(_this, f2.call(_this, {}), "Eval references 'this' binding");
function f3({a:a}, c = eval('new.target')) {
return c;
}
assert.areEqual(f3, new f3({}), "Eval references 'new.target' binding");
class base {
prop() {
assert.areEqual(c1, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
return 'prop';
}
returnThis() {
assert.areEqual(c2, this.constructor, "Be sure the 'this' binding flows down the super reference calls")
return this;
}
}
var prop_name = 'prop';
class c1 extends base {
constructor({a:a}, c = eval('super()')) {
assert.areEqual(c1, new.target, "'new.target' binding is correct in class constructor with split-scope");
assert.areEqual(this, c, "Result of super() should be the same as 'this'");
assert.areEqual('prop', super.prop(), "Super reference should bind correctly");
}
doSuperRefViaDot({a:a}, c = eval('super.prop()')) {
return c;
}
doSuperRefViaIndex({a:a}, c = eval('super[prop_name]()')) {
return c;
}
}
var inst = new c1({});
assert.areEqual(c1, inst.constructor, "Eval references 'super' in a constructor call");
assert.areEqual('prop', inst.doSuperRefViaDot({}), "Eval references 'super' as a super property reference via dot");
assert.areEqual('prop', inst.doSuperRefViaIndex({}), "Eval references 'super' as a super property reference via index");
class c2 extends base {
constructor({a:a}, c = eval('super(); super.returnThis();')) {
assert.areEqual(c2, new.target, "'new.target' binding is correct in class constructor with split-scope");
assert.areEqual(this, c, "Result of super.returnThis() should be the same as 'this'");
}
}
var inst = new c2({});
assert.areEqual(c2, inst.constructor, "Eval references 'super' in a constructor call");
}
},
{
name: "Formal argument named 'arguments'",
body: function() {
function foo(arguments) {
assert.areEqual('arguments', arguments, "Arguments named formal accessed directly");
assert.areEqual('arguments', (() => arguments)(), "Arguments named formal accessed through a lambda capture");
assert.areEqual('arguments', eval('arguments'), "Arguments named formal accessed through an eval");
}
foo('arguments');
}
},
{
name: "Global 'this' binding captured by strict-mode arrow",
body: function() {
WScript.LoadScript(`"use strict";
assert.areEqual(this, (() => this)(), "Lambda should load the global 'this' value itself via LdThis (not StrictLdThis)");
`);
WScript.LoadScript(`
assert.areEqual(this, (() => { "use strict"; return this; })(), "Lambda which has a 'use strict' clause inside");
`);
WScript.LoadScript(`"use strict";
assert.areEqual('object', typeof (() => this)(), "Verify lambda can load global 'this' value even if the global body itself does not have a 'this' binding");
`);
}
},
{
name: "PidRefStack might be out-of-order when we try to add special symbol var decl",
body: function() {
// Failure causes an assert to fire
WScript.LoadScript(`(a = function() { this }, b = (this)) => {}`);
assert.throws(() => WScript.LoadScript(`[ a = function () { this; } ((this)) = 1 ] = []`), ReferenceError, "Not a valid destructuring assignment but should not fire assert", "Invalid left-hand side in assignment");
}
},
{
name: "Non-split scope with default arguments referencing special names",
body: function() {
var _this = {}
function foo(a = this) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
}
foo.call(_this)
function bar(a = this) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
}
bar.call(_this)
function baz(a = this, c = function() { return 'c'; }) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
}
baz.call(_this)
function baz2(a = this, c = function() { function nested() { return 'c' }; return nested; }) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c()(), "Function decl nested in function expression assigned to default argument");
}
baz2.call(_this)
function baz3(c = function() { return 'c'; }, a = this) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c(), "Function expression in param scope default argument is bound to the correct slot");
}
baz3.call(_this)
function bat(a = this, c = () => 'c') {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c(), "Lambda expression in param scope default argument is bound to the correct slot");
}
bat.call(_this)
function bat2(a = this, c = () => () => 'c') {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
}
bat2.call(_this)
function bat3(c = () => () => 'c', a = this) {
eval('');
assert.areEqual(_this, a, "Correct default value was assigned");
assert.areEqual(_this, this, "Regular 'this' binding is correct");
function b() { return 'b'; }
assert.areEqual('b', b(), "Nested functions are bound to the correct slot");
assert.areEqual('c', c()(), "Lambda function decl nested in lambda expression assigned to default argument");
}
bat3.call(_this)
}
},
{
name: "Loading 'this' binding as a call target",
body: function() {
assert.throws(() => WScript.LoadScript(`with({}) { this() }`), TypeError, "Loading global 'this' binding as a call target should always throw - 'this' is an object", "Function expected");
assert.throws(() => this(), TypeError, "Capturing function 'this' binding and emitting as a call target should throw if 'this' is not a function", "Function expected");
}
},
{
name: "Class expression as call target without 'this' binding",
body: function() {
assert.throws(() => WScript.LoadScript(`(class classExpr {}())`), TypeError, "Class expression called at global scope", "Class constructor cannot be called without the new keyword");
assert.throws(() => WScript.LoadScript(`(() => (class classExpr {}()))()`), TypeError, "Class expression called in global lambda", "Class constructor cannot be called without the new keyword");
}
},
{
name: "Indirect eval should not create a 'this' binding",
body: function() {
WScript.LoadScript(`
this.eval("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called off of this capturing this'))()");
this['eval']("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called from a property index capturing this'))()");
var _eval = 'eval';
this[_eval]("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval called from a property index capturing this'))()");
_eval = eval;
_eval("(() => assert.areEqual('global', this.o, 'Lambda in indirect eval capturing this'))()");
`);
}
}
]
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });