summaryrefslogtreecommitdiffstats
path: root/tests/compile.test
blob: 6ef697ca7308338d6fcb5ed7c8e3770d74796c04 (plain)
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
# This file contains tests for the files tclCompile.c, tclCompCmds.c and
# tclLiteral.c
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands. Sourcing this file into Tcl runs the tests and generates output
# for errors. No output means no errors were found.
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

package require tcltest 2
namespace import -force ::tcltest::*

::tcltest::loadTestedCommands
catch [list package require -exact Tcltest [info patchlevel]]

testConstraint exec       [llength [info commands exec]]
testConstraint memory     [llength [info commands memory]]
testConstraint testevalex [llength [info commands testevalex]]

# The following tests are very incomplete, although the rest of the
# test suite covers this file fairly well.

catch {rename p ""}
catch {namespace delete test_ns_compile}
catch {unset x}
catch {unset y}
catch {unset a}

test compile-1.1 {TclCompileString: look up cmds in proc ns, not current ns} -setup {
    catch {namespace delete test_ns_compile}
    catch {unset x}
} -body {
    set x 123
    namespace eval test_ns_compile {
        proc set {args} {
            global x
            lappend x test_ns_compile::set
        }
        proc p {} {
            set 0
        }
    }
    list [test_ns_compile::p] [set x]
} -result {{123 test_ns_compile::set} {123 test_ns_compile::set}}
test compile-1.2 {TclCompileString, error result is reset if TclGetLong determines word isn't an integer} {
    proc p {x} {info commands 3m}
    list [catch {p} msg] $msg
} {1 {wrong # args: should be "p x"}}

test compile-2.1 {TclCompileDollarVar: global scalar name with ::s} -setup {
    catch {unset x}
} -body {
    set x 123
    list $::x [expr {"x" in [info globals]}]
} -result {123 1}
test compile-2.2 {TclCompileDollarVar: global scalar name with ::s} -setup {
    catch {unset y}
} -body {
    proc p {} {
        set ::y 789
        return $::y
    }
    list [p] $::y [expr {"y" in [info globals]}]
} -result {789 789 1}
test compile-2.3 {TclCompileDollarVar: global array name with ::s} -setup {
    catch {unset a}
} -body {
    set ::a(1) 2
    list $::a(1) [set ::a($::a(1)) 3] $::a(2) [expr {"a" in [info globals]}]
} -result {2 3 3 1}
test compile-2.4 {TclCompileDollarVar: global scalar name with ::s} -setup {
    catch {unset a}
} -body {
    proc p {} {
        set ::a(1) 1
        return $::a($::a(1))
    }
    list [p] $::a(1) [expr {"a" in [info globals]}]
} -result {1 1 1}
test compile-2.5 {TclCompileDollarVar: global array, called as ${arrName(0)}} -setup {
    catch {unset a}
} -body {
    proc p {} {
	global a
        set a(1) 1
        return ${a(1)}$::a(1)$a(1)
    }
    list [p] $::a(1) [expr {"a" in [info globals]}]
} -result {111 1 1}

test compile-3.1 {TclCompileCatchCmd: only catch cmds with scalar vars are compiled inline} -setup {
    catch {unset a}
} -body {
    set a(1) xyzzyx
    proc p {} {
        global a
        catch {set x 123} a(1)
    }
    list [p] $a(1)
} -result {0 123}
test compile-3.2 {TclCompileCatchCmd: non-local variables} {
    set ::foo 1
    proc catch-test {} {
	catch {set x 3} ::foo
    }
    catch-test
    return $::foo
} 3
test compile-3.3 {TclCompileCatchCmd: overagressive compiling [bug 219184]} {
    proc catch-test {str} {
	catch [eval $str GOOD]
	error BAD
    }
    catch {catch-test error} ::foo
    return $::foo
} {GOOD}
test compile-3.4 {TclCompileCatchCmd: bcc'ed [return] is caught} {
    proc foo {} {
	set fail [catch {
	    return 1
	}] ; # {}
	return 2
    }
    foo
} {2}
test compile-3.5 {TclCompileCatchCmd: recover from error, [Bug 705406]} {
    proc foo {} {
	catch {
	    if {[a]} {
		if b {}
	    }
	}
    }
    list [catch foo msg] $msg
} {0 1}
test compile-3.6 {TclCompileCatchCmd: error in storing result [Bug 3098302]} {*}{
     -setup {
	 namespace eval catchtest {
	     variable result1 {}
	 }
	 trace add variable catchtest::result1 write catchtest::failtrace
	 proc catchtest::failtrace {n1 n2 op} {
	     return -code error "trace on $n1 fails by request"
	 }
     }
    -body {
	proc catchtest::x {} {
	    variable result1
	    set count 0
	    for {set i 0} {$i < 10} {incr i} {
		set status2 [catch {
		    set status1 [catch {
			return -code error -level 0 "original failure"
		    } result1 options1]
		} result2 options2]
		incr count
	    }
	    list $count $result2
	}
	catchtest::x
    }
    -result {10 {can't set "result1": trace on result1 fails by request}}
    -cleanup {namespace delete catchtest}
}

test compile-3.7 {TclCompileCatchCmd: error in storing options [Bug 3098302]} {*}{
     -setup {
	 namespace eval catchtest {
	     variable options1 {}
	 }
	 trace add variable catchtest::options1 write catchtest::failtrace
	 proc catchtest::failtrace {n1 n2 op} {
	     return -code error "trace on $n1 fails by request"
	 }
     }
    -body {
	proc catchtest::x {} {
	    variable options1
	    set count 0
	    for {set i 0} {$i < 10} {incr i} {
		set status2 [catch {
		    set status1 [catch {
			return -code error -level 0 "original failure"
		    } result1 options1]
		} result2 options2]
		incr count
	    }
	    list $count $result2
	}
	catchtest::x
    }
    -result {10 {can't set "options1": trace on options1 fails by request}}
    -cleanup {namespace delete catchtest}
}

test compile-4.1 {TclCompileForCmd: command substituted test expression} {
    set i 0
    set j 0
    # Should be "forever"
    for {} [expr $i < 3] {} {
	set j [incr i]
	if {$j > 3} break
    }
    set j
} {4}

test compile-5.1 {TclCompileForeachCmd: exception stack} {
    proc foreach-exception-test {} {
	foreach array(index) [list 1 2 3] break
	foreach array(index) [list 1 2 3] break
	foreach scalar [list 1 2 3] break
    }
    list [catch foreach-exception-test result] $result
} {0 {}}
test compile-5.2 {TclCompileForeachCmd: non-local variables} {
    set ::foo 1
    proc foreach-test {} {
	foreach ::foo {1 2 3} {}
    }
    foreach-test
    set ::foo
} 3

test compile-6.1 {TclCompileSetCmd: global scalar names with ::s} -setup {
    catch {unset x}
    catch {unset y}
} -body {
    set x 123
    proc p {} {
        set ::y 789
        return $::y
    }
    list $::x [expr {"x" in [info globals]}] \
         [p] $::y [expr {"y" in [info globals]}]
} -result {123 1 789 789 1}
test compile-6.2 {TclCompileSetCmd: global array names with ::s} -setup {
    catch {unset a}
} -body {
    set ::a(1) 2
    proc p {} {
        set ::a(1) 1
        return $::a($::a(1))
    }
    list $::a(1) [p] [set ::a($::a(1)) 3] $::a(1) [expr {"a" in [info globals]}]
} -result {2 1 3 3 1}
test compile-6.3 {TclCompileSetCmd: namespace var names with ::s} -setup {
    catch {namespace delete test_ns_compile}
    catch {unset x}
} -body {
    namespace eval test_ns_compile {
        variable v hello
        variable arr
        set ::x $::test_ns_compile::v
	set ::test_ns_compile::arr(1) 123
    }
    list $::x $::test_ns_compile::arr(1)
} -result {hello 123}

test compile-7.1 {TclCompileWhileCmd: command substituted test expression} {
    set i 0
    set j 0
    # Should be "forever"
    while [expr $i < 3] {
	set j [incr i]
	if {$j > 3} break
    }
    set j
} {4}

test compile-8.1 {CollectArgInfo: binary data} {
    list [catch "string length \000foo" msg] $msg
} {0 4}
test compile-8.2 {CollectArgInfo: binary data} {
    list [catch "string length foo\000" msg] $msg
} {0 4}
test compile-8.3 {CollectArgInfo: handle "]" at end of command properly} {
    set x ]
} {]}

test compile-9.1 {UpdateStringOfByteCode: called for duplicate of compiled empty object} {
    proc p {} {
        set x {}
        eval $x
        append x { }
        eval $x
    }
    p
} {}

test compile-10.1 {BLACKBOX: exception stack overflow} {
    set x {{0}}
    set y 0
    while {$y < 100} {
	if !$x {incr y}
    }
} {}

test compile-11.1 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} {
	# shared object - Interp result && Var 'r'
	set r [list foobar]
	# command that will add error to result
	lindex a bogus
    }}
} -returnCodes error -result {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}
test compile-11.2 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; string index a bogus }}
} -returnCodes error -result {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}
test compile-11.3 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; string index a 0o9 }}
} -returnCodes error -match glob -result {*invalid octal number*}
test compile-11.4 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; array set var {one two many} }}
} -returnCodes error -result {list must have an even number of elements}
test compile-11.5 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; incr foo bar baz}}
} -returnCodes error -result {wrong # args: should be "incr varName ?increment?"}
test compile-11.6 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; incr}}
} -returnCodes error -result {wrong # args: should be "incr varName ?increment?"}
test compile-11.7 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; expr !a }}
} -returnCodes error -match glob -result *
test compile-11.8 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; expr {!a} }}
} -returnCodes error -match glob -result *
test compile-11.9 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
    apply {{} { set r [list foobar] ; llength "\{" }}
    list [catch {p} msg] $msg
} -returnCodes error -result {unmatched open brace in list}

#
# Special section for tests of tclLiteral.c
# The following tests check for incorrect memory handling in
# TclReleaseLiteral. They are only effective when tcl is compiled with
# TCL_MEM_DEBUG
#
# Special test for leak on interp delete [Bug 467523].
test compile-12.1 {testing literal leak on interp delete} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
} -constraints memory -body {
    set end [getbytes]
    for {set i 0} {$i < 5} {incr i} {
	interp create foo
	foo eval {
	    namespace eval bar {}
	}
	interp delete foo
	set tmp $end
	set end [getbytes]
    }
    set leakedBytes [expr {$end - $tmp}]
} -cleanup {
    rename getbytes {}
    unset -nocomplain end i tmp leakedBytes
} -result 0
# Special test for a memory error in a preliminary fix of [Bug 467523].  It
# requires executing a helpfile.  Presumably the child process is used because
# when this test fails, it crashes.
test compile-12.2 {testing error on literal deletion} -constraints {memory exec} -body {
    set sourceFile [makeFile {
	for {set i 0} {$i < 5} {incr i} {
	    namespace eval bar {}
	    namespace delete bar
	}
	puts 0
    } source.file]
    exec [interpreter] $sourceFile
} -cleanup {
    catch {removeFile $sourceFile}
} -result 0
# Test to catch buffer overrun in TclCompileTokens from buf 530320
test compile-12.3 {check for a buffer overrun} -body {
    proc crash {} {
	puts $array([expr {a+2}])
    }
    crash
} -returnCodes error -cleanup {
    rename crash {}
} -match glob -result *
test compile-12.4 {TclCleanupLiteralTable segfault} -body {
    # Tcl Bug 1001997
    # Here, we're trying to test a case that causes a crash in
    # TclCleanupLiteralTable.  The conditions that we're trying to establish
    # are:
    # - TclCleanupLiteralTable is attempting to clean up a bytecode object in
    #   the literal table.
    # - The bytecode object in question contains the only reference to another
    #   literal.
    # - The literal in question is in the same hash bucket as the bytecode
    #   object, and immediately follows it in the chain.
    # Since newly registered literals are added at the FRONT of the bucket
    # chains, and since the bytecode object is registered before its literals,
    # this is difficult to achieve.  What we do is:
    #  (a) do a [namespace eval] of a string that's calculated to hash into
    #      the same bucket as a literal that it contains.  In this case, the
    #      script and the variable 'bugbug' land in the same bucket.
    #  (b) do a [namespace eval] of a string that contains enough literals to
    #      force TclRegisterLiteral to rebuild the global literal table.  The
    #      newly created hash buckets will contain the literals, IN REVERSE
    #      ORDER, thus putting the bytecode immediately ahead of 'bugbug' and
    #      'bug4345bug'.  The bytecode object will contain the only references
    #      to those two literals.
    #  (c) Delete the interpreter to invoke TclCleanupLiteralTable and tickle
    #      the bug.
    proc foo {} {
	set i [interp create]
	$i eval {
	    namespace eval ::w {concat 4649; variable bugbug}
	    namespace eval ::w {
		concat x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 \
		    x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 \
		    x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 \
		    x31 x32 X33 X34 X35 X36 X37 X38 X39 X40 \
		    x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 \
		    x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 \
		    x61 x62 x63 x64
		concat y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 \
		    y11 y12 y13 y14 y15 y16 y17 y18 y19 y20 \
		    y21 y22 y23 y24 y25 y26 y27 y28 y29 y30 \
		    y31 y32 Y33 Y34 Y35 Y36 Y37 Y38 Y39 Y40 \
		    y41 y42 y43 y44 y45 y46 y47 y48 y49 y50 \
		    y51 y52 y53 y54 y55 y56 y57 y58 y59 y60 \
		    y61 y62 y63 y64
		concat z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 \
		    z11 z12 z13 z14 z15 z16 z17 z18 z19 z20 \
		    z21 z22 z23 z24 z25 z26 z27 z28 z29 z30 \
		    z31 z32
	    }
	}
	interp delete $i; # must not crash
	return ok
    }
    foo
} -cleanup {
    rename foo {}
} -result ok

# Special test for underestimating the maxStackSize required for a compiled
# command. A failure will cause a segfault in the child process.
test compile-13.1 {testing underestimate of maxStackSize in list cmd} {exec} {
    set body {set x [list}
    for {set i 0} {$i < 3000} {incr i} {
	append body " $i"
    }
    append body {]; puts OK}
    regsub BODY {proc crash {} {BODY}; crash} $body script
    list [catch {exec [interpreter] << $script} msg] $msg
} {0 OK}

# Tests compile-14.* for [Bug 599788] [Bug 0c043a175a47da8c2342]
test compile-14.1 {testing errors in element name; segfault?} {} {
     catch {set a([error])} msg1
     catch {set bubba([join $abba $jubba]) $vol} msg2
     list $msg1 $msg2
} {{wrong # args: should be "error message ?errorInfo? ?errorCode?"} {can't read "abba": no such variable}}

test compile-14.2 {testing element name "$"} -body {
    unset -nocomplain a
    set a() 1
    set a(1) 2
    set a($) 3
    list [set a()] [set a(1)] [set a($)] [unset a() a(1); lindex [array names a] 0]
} -cleanup {unset a} -result [list 1 2 3 {$}]


# Tests compile-15.* cover Tcl Bug 633204
test compile-15.1 {proper TCL_RETURN code from [return]} {
    apply {{} {catch return}}
} 2
test compile-15.2 {proper TCL_RETURN code from [return]} {
    apply {{} {catch {return foo}}}
} 2
test compile-15.3 {proper TCL_RETURN code from [return]} {
    apply {{} {catch {return $::tcl_library}}}
} 2
test compile-15.4 {proper TCL_RETURN code from [return]} {
    apply {{} {catch {return [info library]}}}
} 2
test compile-15.5 {proper TCL_RETURN code from [return]} {
    apply {{} {catch {set a 1}; return}}
} ""

for {set noComp 0} {$noComp <= 1} {incr noComp} {

if $noComp {
    interp alias {} run {} testevalex
    set constraints testevalex
} else {
    interp alias {} run {} if 1
    set constraints {}
}

test compile-16.1.$noComp {TclCompileScript: word expansion} $constraints {
    run "list [string repeat {{*}a } 255]"
} [lrepeat 255 a]
test compile-16.2.$noComp {TclCompileScript: word expansion} $constraints {
    run "list [string repeat {{*}a } 256]"
} [lrepeat 256 a]
test compile-16.3.$noComp {TclCompileScript: word expansion} $constraints {
    run "list [string repeat {{*}a } 257]"
} [lrepeat 257 a]
test compile-16.4.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list}
} {}
test compile-16.5.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list {*}{x y z}}
} {x y z}
test compile-16.6.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list {*}[list x y z]}
} {x y z}
test compile-16.7.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list {*}[list x y z][list x y z]}
} {x y zx y z}
test compile-16.8.$noComp {TclCompileScript: word expansion} -body {
    set l {x y z}
    run {{*}list {*}$l}
} -constraints $constraints -cleanup {
    unset l
} -result {x y z}
test compile-16.9.$noComp {TclCompileScript: word expansion} -body {
    set l {x y z}
    run {{*}list {*}$l$l}
} -constraints $constraints -cleanup {
    unset l
} -result {x y zx y z}
test compile-16.10.$noComp {TclCompileScript: word expansion} -body {
    run {{*}\{}
} -constraints $constraints -returnCodes error \
-result {unmatched open brace in list}
test compile-16.11.$noComp {TclCompileScript: word expansion} -body {
    proc badList {} {return \{}
    run {{*}[badList]}
} -constraints $constraints -cleanup {
    rename badList {}
} -returnCodes error  -result {unmatched open brace in list}
test compile-16.12.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list x y z}
} {x y z}
test compile-16.13.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list x y {*}z}
} {x y z}
test compile-16.14.$noComp {TclCompileScript: word expansion} $constraints {
    run {{*}list x {*}y z}
} {x y z}
test compile-16.15.$noComp {TclCompileScript: word expansion} $constraints {
    run {list x y {*}z}
} {x y z}
test compile-16.16.$noComp {TclCompileScript: word expansion} $constraints {
    run {list x {*}y z}
} {x y z}
test compile-16.17.$noComp {TclCompileScript: word expansion} $constraints {
    run {list {*}x y z}
} {x y z}

# These tests note that expansion can in theory cause the number of arguments
# to a command to exceed INT_MAX, which is as big as objc is allowed to get.
#
# In practice, it seems we will run out of memory before we confront this
# issue. Note that compiled operations run out of memory at smaller objc
# values than direct string evaluation.
#
# These tests are constrained as knownBug because they are likely to cause
# memory allocation panics somewhere, and we don't want panics in the test
# suite.
#
test compile-16.18.$noComp {TclCompileScript: word expansion} -body {
    proc LongList {} {return [lrepeat [expr {1<<10}] x]}
    llength [run "list [string repeat {{*}[LongList] } [expr {1<<10}]]"]
} -constraints [linsert $constraints 0 knownBug] -cleanup {
    rename LongList {}
} -returnCodes ok  -result [expr {1<<20}]
test compile-16.19.$noComp {TclCompileScript: word expansion} -body {
    proc LongList {} {return [lrepeat [expr {1<<11}] x]}
    llength [run "list [string repeat {{*}[LongList] } [expr {1<<11}]]"]
} -constraints [linsert $constraints 0 knownBug] -cleanup {
    rename LongList {}
} -returnCodes ok  -result [expr {1<<22}]
test compile-16.20.$noComp {TclCompileScript: word expansion} -body {
    proc LongList {} {return [lrepeat [expr {1<<12}] x]}
    llength [run "list [string repeat {{*}[LongList] } [expr {1<<12}]]"]
} -constraints [linsert $constraints 0 knownBug] -cleanup {
    rename LongList {}
} -returnCodes ok  -result [expr {1<<24}]
# This is the one that should cause overflow
test compile-16.21.$noComp {TclCompileScript: word expansion} -body {
    proc LongList {} {return [lrepeat [expr {1<<16}] x]}
    llength [run "list [string repeat {{*}[LongList] } [expr {1<<16}]]"]
} -constraints [linsert $constraints 0 knownBug] -cleanup {
    rename LongList {}
} -returnCodes ok  -result [expr {wide(1)<<32}]
test compile-16.22.$noComp {
    Bug 845412: TclCompileScript: word expansion not mandatory
} -body {
    # This test may crash and will fail unless Bug 845412 is fixed.
    proc ReturnResults args {return $args}
    run "ReturnResults [string repeat {x } 260]"
} -constraints $constraints -cleanup {
    rename ReturnResults {}
} -returnCodes ok -result [string trim [string repeat {x } 260]]
test compile-16.23.$noComp {
    Bug 1032805: defer parse error until run time
} -constraints $constraints -body {
    namespace eval x {
	run {
	    proc if {a b} {uplevel 1 [list set $a $b]}
	    if 1 {syntax {}{}}
	}
    }
} -cleanup {
    namespace delete x
} -returnCodes ok -result {syntax {}{}}
test compile-16.24.$noComp {
    Bug 1638414: bad list constant as first expanded term
} -constraints $constraints -body {
    run "{*}\"\{foo bar\""
} -returnCodes error -result {unmatched open brace in list}
test compile-16.25.$noComp {TclCompileScript: word expansion, naked backslashes} $constraints {
    run {list {*}{a \n b}}
} {a {
} b}
test compile-16.26.$noComp {TclCompileScript: word expansion, protected backslashes} $constraints {
    run {list {*}{a {\n} b}}
} {a {\n} b}
}	;# End of noComp loop

# These tests are messy because it wrecks the interpreter it runs in!  They
# demonstrate issues arising from [FRQ 1101710]
test compile-17.1 {Command interpretation binding for compiled code} -constraints knownBug -setup {
    set i [interp create]
} -body {
    $i eval {
	if 1 {
	    expr [
		proc expr args {return substituted}
		format {[subst compiled]}
	    ]
	}
    }
} -cleanup {
    interp delete $i
} -result substituted
test compile-17.2 {Command interpretation binding for non-compiled code} -setup {
    set i [interp create]
} -body {
    $i eval {
	if 1 {
	    [subst expr] [
		proc expr args {return substituted}
		format {[subst compiled]}
	    ]
	}
    }
} -cleanup {
    interp delete $i
} -result substituted

# This tests the supported parts of the unsupported [disassemble] command. It
# does not check the format of disassembled bytecode though; that's liable to
# change without warning.

set disassemblables [linsert [join {
    lambda method objmethod proc script
} ", "] end-1 or]
test compile-18.1 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble
} -match glob -result {wrong # args: should be "*"}
test compile-18.2 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble ?
} -result "bad type \"?\": must be $disassemblables"
test compile-18.3 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble lambda
} -match glob -result {wrong # args: should be "* lambda lambdaTerm"}
test compile-18.4 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble lambda \{
} -result "can't interpret \"\{\" as a lambda expression"
test compile-18.5 {disassembler - basics} -body {
    # Allow any string: the result format is not defined anywhere!
    tcl::unsupported::disassemble lambda {{} {}}
} -match glob -result *
test compile-18.6 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble proc
} -match glob -result {wrong # args: should be "* proc procName"}
test compile-18.7 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble proc nosuchproc
} -result {"nosuchproc" isn't a procedure}
test compile-18.8 {disassembler - basics} -setup {
    proc chewonthis {} {}
} -body {
    # Allow any string: the result format is not defined anywhere!
    tcl::unsupported::disassemble proc chewonthis
} -cleanup {
    rename chewonthis {}
} -match glob -result *
test compile-18.9 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble script
} -match glob -result {wrong # args: should be "* script script"}
test compile-18.10 {disassembler - basics} -body {
    # Allow any string: the result format is not defined anywhere!
    tcl::unsupported::disassemble script {}
} -match glob -result *
test compile-18.11 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble method
} -match glob -result {wrong # args: should be "* method className methodName"}
test compile-18.12 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble method nosuchclass foo
} -result {nosuchclass does not refer to an object}
test compile-18.13 {disassembler - basics} -returnCodes error -setup {
    oo::object create justanobject
} -body {
    tcl::unsupported::disassemble method justanobject foo
} -cleanup {
    justanobject destroy
} -result {"justanobject" is not a class}
test compile-18.14 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble method oo::object nosuchmethod
} -result {unknown method "nosuchmethod"}
test compile-18.15 {disassembler - basics} -setup {
    oo::class create foo {method bar {} {}}
} -body {
    # Allow any string: the result format is not defined anywhere!
    tcl::unsupported::disassemble method foo bar
} -cleanup {
    foo destroy
} -match glob -result *
test compile-18.16 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble objmethod
} -match glob -result {wrong # args: should be "* objmethod objectName methodName"}
test compile-18.17 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble objmethod nosuchobject foo
} -result {nosuchobject does not refer to an object}
test compile-18.18 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::disassemble objmethod oo::object nosuchmethod
} -result {unknown method "nosuchmethod"}
test compile-18.19 {disassembler - basics} -setup {
    oo::object create foo
    oo::objdefine foo {method bar {} {}}
} -body {
    # Allow any string: the result format is not defined anywhere!
    tcl::unsupported::disassemble objmethod foo bar
} -cleanup {
    foo destroy
} -match glob -result *
# There never was a compile-18.20.
# The keys of the dictionary produced by [getbytecode] are defined.
set bytecodekeys {literals variables exception instructions auxiliary commands script namespace stackdepth exceptdepth}
test compile-18.21 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode
} -match glob -result {wrong # args: should be "*"}
test compile-18.22 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode ?
} -result "bad type \"?\": must be $disassemblables"
test compile-18.23 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode lambda
} -match glob -result {wrong # args: should be "* lambda lambdaTerm"}
test compile-18.24 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode lambda \{
} -result "can't interpret \"\{\" as a lambda expression"
test compile-18.25 {disassembler - basics} -body {
    dict keys [tcl::unsupported::getbytecode lambda {{} {}}]
} -result "$bytecodekeys initiallinenumber sourcefile"
test compile-18.26 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode proc
} -match glob -result {wrong # args: should be "* proc procName"}
test compile-18.27 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode proc nosuchproc
} -result {"nosuchproc" isn't a procedure}
test compile-18.28 {disassembler - basics} -setup {
    proc chewonthis {} {}
} -body {
    dict keys [tcl::unsupported::getbytecode proc chewonthis]
} -cleanup {
    rename chewonthis {}
} -result "$bytecodekeys initiallinenumber sourcefile"
test compile-18.28.1 {disassembler - tricky bit} -setup {
    eval [list proc chewonthis {} {}]
} -body {
    dict keys [tcl::unsupported::getbytecode proc chewonthis]
} -cleanup {
    rename chewonthis {}
} -result $bytecodekeys
test compile-18.28.2 {disassembler - tricky bit} -setup {
    eval {proc chewonthis {} {}}
} -body {
    dict keys [tcl::unsupported::getbytecode proc chewonthis]
} -cleanup {
    rename chewonthis {}
} -result "$bytecodekeys initiallinenumber sourcefile"
test compile-18.28.3 {disassembler - tricky bit} -setup {
    proc Proc {n a b} {
	proc $n $a $b
    }
    Proc chewonthis {} {}
} -body {
    dict keys [tcl::unsupported::getbytecode proc chewonthis]
} -cleanup {
    rename Proc {}
    rename chewonthis {}
} -result $bytecodekeys
test compile-18.28.4 {disassembler - tricky bit} -setup {
    proc Proc {n a b} {
	tailcall proc $n $a $b
    }
    Proc chewonthis {} {}
} -body {
    dict keys [tcl::unsupported::getbytecode proc chewonthis]
} -cleanup {
    rename Proc {}
    rename chewonthis {}
} -result "$bytecodekeys initiallinenumber sourcefile"
test compile-18.29 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode script
} -match glob -result {wrong # args: should be "* script script"}
test compile-18.30 {disassembler - basics} -body {
    dict keys [tcl::unsupported::getbytecode script {}]
} -result $bytecodekeys
test compile-18.31 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode method
} -match glob -result {wrong # args: should be "* method className methodName"}
test compile-18.32 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode method nosuchclass foo
} -result {nosuchclass does not refer to an object}
test compile-18.33 {disassembler - basics} -returnCodes error -setup {
    oo::object create justanobject
} -body {
    tcl::unsupported::getbytecode method justanobject foo
} -cleanup {
    justanobject destroy
} -result {"justanobject" is not a class}
test compile-18.34 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode method oo::object nosuchmethod
} -result {unknown method "nosuchmethod"}
test compile-18.35 {disassembler - basics} -setup {
    oo::class create foo {method bar {} {}}
} -body {
    dict keys [tcl::unsupported::getbytecode method foo bar]
} -cleanup {
    foo destroy
} -result "$bytecodekeys initiallinenumber sourcefile"
test compile-18.36 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode objmethod
} -match glob -result {wrong # args: should be "* objmethod objectName methodName"}
test compile-18.37 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode objmethod nosuchobject foo
} -result {nosuchobject does not refer to an object}
test compile-18.38 {disassembler - basics} -returnCodes error -body {
    tcl::unsupported::getbytecode objmethod oo::object nosuchmethod
} -result {unknown method "nosuchmethod"}
test compile-18.39 {disassembler - basics} -setup {
    oo::object create foo
    oo::objdefine foo {method bar {} {}}
} -body {
    dict keys [tcl::unsupported::getbytecode objmethod foo bar]
} -cleanup {
    foo destroy
} -result "$bytecodekeys initiallinenumber sourcefile"

test compile-19.0 {Bug 3614102: reset stack housekeeping} -body {
    # This will panic in a --enable-symbols=compile build, unless bug is fixed.
    apply {{} {list [if 1]}}
} -returnCodes error -match glob -result *

test compile-20.1 {ensure there are no infinite loops in optimizing} {
    tcl::unsupported::disassemble script {
	while 1 {
	    return -code continue -level 0
	}
    }
    return
} {}
test compile-20.2 {ensure there are no infinite loops in optimizing} {
    tcl::unsupported::disassemble script {
	while 1 {
	    while 1 {
		return -code break -level 0
	    }
	}
    }
    return
} {}

test compile-21.1 {stack balance management} {
    apply {{} {
	set result {}
	while 1 {
	    lappend result a
	    lappend result [list b [break]]
	    lappend result c
	}
	return $result
    }}
} a
test compile-21.2 {stack balance management} {
    apply {{} {
	set result {}
	while {[incr i] <= 10} {
	    lappend result $i
	    lappend result [list b [continue] c]
	    lappend result c
	}
	return $result
    }}
} {1 2 3 4 5 6 7 8 9 10}
test compile-21.3 {stack balance management} {
    apply {args {
	set result {}
	while 1 {
	    lappend result a
	    lappend result [concat {*}$args [break]]
	    lappend result c
	}
	return $result
    }} P Q R S T
} a
test compile-21.4 {stack balance management} {
    apply {args {
	set result {}
	while {[incr i] <= 10} {
	    lappend result $i
	    lappend result [concat {*}$args [continue] c]
	    lappend result c
	}
	return $result
    }} P Q R S T
} {1 2 3 4 5 6 7 8 9 10}

# TODO sometime - check that bytecode from tbcload is *not* disassembled.

# cleanup
catch {rename p ""}
catch {namespace delete test_ns_compile}
catch {unset x}
catch {unset y}
catch {unset a}
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# fill-column: 78
# End: