Debian Codegraph @

tests/compile.test

raw
1 # This file contains tests for the files tclCompile.c, tclCompCmds.c and
2 # tclLiteral.c
3 #
4 # This file contains a collection of tests for one or more of the Tcl built-in
5 # commands. Sourcing this file into Tcl runs the tests and generates output
6 # for errors. No output means no errors were found.
7 #
8 # Copyright (c) 1997 by Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint exec [llength [info commands exec]]
23 testConstraint memory [llength [info commands memory]]
24 testConstraint testevalex [llength [info commands testevalex]]
25
26 # The following tests are very incomplete, although the rest of the
27 # test suite covers this file fairly well.
28
29 catch {rename p ""}
30 catch {namespace delete test_ns_compile}
31 catch {unset x}
32 catch {unset y}
33 catch {unset a}
34
35 test compile-1.1 {TclCompileString: look up cmds in proc ns, not current ns} -setup {
36 catch {namespace delete test_ns_compile}
37 catch {unset x}
38 } -body {
39 set x 123
40 namespace eval test_ns_compile {
41 proc set {args} {
42 global x
43 lappend x test_ns_compile::set
44 }
45 proc p {} {
46 set 0
47 }
48 }
49 list [test_ns_compile::p] [set x]
50 } -result {{123 test_ns_compile::set} {123 test_ns_compile::set}}
51 test compile-1.2 {TclCompileString, error result is reset if TclGetLong determines word isn't an integer} {
52 proc p {x} {info commands 3m}
53 list [catch {p} msg] $msg
54 } {1 {wrong # args: should be "p x"}}
55
56 test compile-2.1 {TclCompileDollarVar: global scalar name with ::s} -setup {
57 catch {unset x}
58 } -body {
59 set x 123
60 list $::x [expr {"x" in [info globals]}]
61 } -result {123 1}
62 test compile-2.2 {TclCompileDollarVar: global scalar name with ::s} -setup {
63 catch {unset y}
64 } -body {
65 proc p {} {
66 set ::y 789
67 return $::y
68 }
69 list [p] $::y [expr {"y" in [info globals]}]
70 } -result {789 789 1}
71 test compile-2.3 {TclCompileDollarVar: global array name with ::s} -setup {
72 catch {unset a}
73 } -body {
74 set ::a(1) 2
75 list $::a(1) [set ::a($::a(1)) 3] $::a(2) [expr {"a" in [info globals]}]
76 } -result {2 3 3 1}
77 test compile-2.4 {TclCompileDollarVar: global scalar name with ::s} -setup {
78 catch {unset a}
79 } -body {
80 proc p {} {
81 set ::a(1) 1
82 return $::a($::a(1))
83 }
84 list [p] $::a(1) [expr {"a" in [info globals]}]
85 } -result {1 1 1}
86 test compile-2.5 {TclCompileDollarVar: global array, called as ${arrName(0)}} -setup {
87 catch {unset a}
88 } -body {
89 proc p {} {
90 global a
91 set a(1) 1
92 return ${a(1)}$::a(1)$a(1)
93 }
94 list [p] $::a(1) [expr {"a" in [info globals]}]
95 } -result {111 1 1}
96
97 test compile-3.1 {TclCompileCatchCmd: only catch cmds with scalar vars are compiled inline} -setup {
98 catch {unset a}
99 } -body {
100 set a(1) xyzzyx
101 proc p {} {
102 global a
103 catch {set x 123} a(1)
104 }
105 list [p] $a(1)
106 } -result {0 123}
107 test compile-3.2 {TclCompileCatchCmd: non-local variables} {
108 set ::foo 1
109 proc catch-test {} {
110 catch {set x 3} ::foo
111 }
112 catch-test
113 return $::foo
114 } 3
115 test compile-3.3 {TclCompileCatchCmd: overagressive compiling [bug 219184]} {
116 proc catch-test {str} {
117 catch [eval $str GOOD]
118 error BAD
119 }
120 catch {catch-test error} ::foo
121 return $::foo
122 } {GOOD}
123 test compile-3.4 {TclCompileCatchCmd: bcc'ed [return] is caught} {
124 proc foo {} {
125 set fail [catch {
126 return 1
127 }] ; # {}
128 return 2
129 }
130 foo
131 } {2}
132 test compile-3.5 {TclCompileCatchCmd: recover from error, [Bug 705406]} {
133 proc foo {} {
134 catch {
135 if {[a]} {
136 if b {}
137 }
138 }
139 }
140 list [catch foo msg] $msg
141 } {0 1}
142 test compile-3.6 {TclCompileCatchCmd: error in storing result [Bug 3098302]} {*}{
143 -setup {
144 namespace eval catchtest {
145 variable result1 {}
146 }
147 trace add variable catchtest::result1 write catchtest::failtrace
148 proc catchtest::failtrace {n1 n2 op} {
149 return -code error "trace on $n1 fails by request"
150 }
151 }
152 -body {
153 proc catchtest::x {} {
154 variable result1
155 set count 0
156 for {set i 0} {$i < 10} {incr i} {
157 set status2 [catch {
158 set status1 [catch {
159 return -code error -level 0 "original failure"
160 } result1 options1]
161 } result2 options2]
162 incr count
163 }
164 list $count $result2
165 }
166 catchtest::x
167 }
168 -result {10 {can't set "result1": trace on result1 fails by request}}
169 -cleanup {namespace delete catchtest}
170 }
171
172 test compile-3.7 {TclCompileCatchCmd: error in storing options [Bug 3098302]} {*}{
173 -setup {
174 namespace eval catchtest {
175 variable options1 {}
176 }
177 trace add variable catchtest::options1 write catchtest::failtrace
178 proc catchtest::failtrace {n1 n2 op} {
179 return -code error "trace on $n1 fails by request"
180 }
181 }
182 -body {
183 proc catchtest::x {} {
184 variable options1
185 set count 0
186 for {set i 0} {$i < 10} {incr i} {
187 set status2 [catch {
188 set status1 [catch {
189 return -code error -level 0 "original failure"
190 } result1 options1]
191 } result2 options2]
192 incr count
193 }
194 list $count $result2
195 }
196 catchtest::x
197 }
198 -result {10 {can't set "options1": trace on options1 fails by request}}
199 -cleanup {namespace delete catchtest}
200 }
201
202 test compile-4.1 {TclCompileForCmd: command substituted test expression} {
203 set i 0
204 set j 0
205 # Should be "forever"
206 for {} [expr {$i < 3}] {} {
207 set j [incr i]
208 if {$j > 3} break
209 }
210 set j
211 } {4}
212
213 test compile-5.1 {TclCompileForeachCmd: exception stack} {
214 proc foreach-exception-test {} {
215 foreach array(index) [list 1 2 3] break
216 foreach array(index) [list 1 2 3] break
217 foreach scalar [list 1 2 3] break
218 }
219 list [catch foreach-exception-test result] $result
220 } {0 {}}
221 test compile-5.2 {TclCompileForeachCmd: non-local variables} {
222 set ::foo 1
223 proc foreach-test {} {
224 foreach ::foo {1 2 3} {}
225 }
226 foreach-test
227 set ::foo
228 } 3
229 test compile-5.3 {TclCompileForeachCmd: [Bug b9b2079e6d]} -setup {
230 proc demo {} {
231 foreach x y {
232 if 1 break else
233 }
234 }
235 } -body {
236 demo
237 } -cleanup {
238 rename demo {}
239 } -returnCodes error -result {wrong # args: no script following "else" argument}
240
241 test compile-6.1 {TclCompileSetCmd: global scalar names with ::s} -setup {
242 catch {unset x}
243 catch {unset y}
244 } -body {
245 set x 123
246 proc p {} {
247 set ::y 789
248 return $::y
249 }
250 list $::x [expr {"x" in [info globals]}] \
251 [p] $::y [expr {"y" in [info globals]}]
252 } -result {123 1 789 789 1}
253 test compile-6.2 {TclCompileSetCmd: global array names with ::s} -setup {
254 catch {unset a}
255 } -body {
256 set ::a(1) 2
257 proc p {} {
258 set ::a(1) 1
259 return $::a($::a(1))
260 }
261 list $::a(1) [p] [set ::a($::a(1)) 3] $::a(1) [expr {"a" in [info globals]}]
262 } -result {2 1 3 3 1}
263 test compile-6.3 {TclCompileSetCmd: namespace var names with ::s} -setup {
264 catch {namespace delete test_ns_compile}
265 catch {unset x}
266 } -body {
267 namespace eval test_ns_compile {
268 variable v hello
269 variable arr
270 set ::x $::test_ns_compile::v
271 set ::test_ns_compile::arr(1) 123
272 }
273 list $::x $::test_ns_compile::arr(1)
274 } -result {hello 123}
275
276 test compile-7.1 {TclCompileWhileCmd: command substituted test expression} {
277 set i 0
278 set j 0
279 # Should be "forever"
280 while [expr {$i < 3}] {
281 set j [incr i]
282 if {$j > 3} break
283 }
284 set j
285 } {4}
286
287 test compile-8.1 {CollectArgInfo: binary data} {
288 list [catch "string length \000foo" msg] $msg
289 } {0 4}
290 test compile-8.2 {CollectArgInfo: binary data} {
291 list [catch "string length foo\000" msg] $msg
292 } {0 4}
293 test compile-8.3 {CollectArgInfo: handle "]" at end of command properly} {
294 set x ]
295 } {]}
296
297 test compile-9.1 {UpdateStringOfByteCode: called for duplicate of compiled empty object} {
298 proc p {} {
299 set x {}
300 eval $x
301 append x { }
302 eval $x
303 }
304 p
305 } {}
306
307 test compile-10.1 {BLACKBOX: exception stack overflow} {
308 set x {{0}}
309 set y 0
310 while {$y < 100} {
311 if !$x {incr y}
312 }
313 } {}
314
315 test compile-11.1 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
316 apply {{} {
317 # shared object - Interp result && Var 'r'
318 set r [list foobar]
319 # command that will add error to result
320 lindex a bogus
321 }}
322 } -returnCodes error -result {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}
323 test compile-11.2 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
324 apply {{} { set r [list foobar] ; string index a bogus }}
325 } -returnCodes error -result {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}
326 test compile-11.3 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
327 apply {{} { set r [list foobar] ; string index a 0o9 }}
328 } -returnCodes error -match glob -result {*invalid octal number*}
329 test compile-11.4 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
330 apply {{} { set r [list foobar] ; array set var {one two many} }}
331 } -returnCodes error -result {list must have an even number of elements}
332 test compile-11.5 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
333 apply {{} { set r [list foobar] ; incr foo bar baz}}
334 } -returnCodes error -result {wrong # args: should be "incr varName ?increment?"}
335 test compile-11.6 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
336 apply {{} { set r [list foobar] ; incr}}
337 } -returnCodes error -result {wrong # args: should be "incr varName ?increment?"}
338 test compile-11.7 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
339 apply {{} { set r [list foobar] ; expr [concat !a] }}
340 } -returnCodes error -match glob -result *
341 test compile-11.8 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
342 apply {{} { set r [list foobar] ; expr {!a} }}
343 } -returnCodes error -match glob -result *
344 test compile-11.9 {Tcl_Append*: ensure Tcl_ResetResult is used properly} -body {
345 apply {{} { set r [list foobar] ; llength "\{" }}
346 list [catch {p} msg] $msg
347 } -returnCodes error -result {unmatched open brace in list}
348
349 #
350 # Special section for tests of tclLiteral.c
351 # The following tests check for incorrect memory handling in
352 # TclReleaseLiteral. They are only effective when tcl is compiled with
353 # TCL_MEM_DEBUG
354 #
355 # Special test for leak on interp delete [Bug 467523].
356 test compile-12.1 {testing literal leak on interp delete} -setup {
357 proc getbytes {} {
358 set lines [split [memory info] "\n"]
359 lindex $lines 3 3
360 }
361 } -constraints memory -body {
362 set end [getbytes]
363 for {set i 0} {$i < 5} {incr i} {
364 interp create foo
365 foo eval {
366 namespace eval bar {}
367 }
368 interp delete foo
369 set tmp $end
370 set end [getbytes]
371 }
372 set leakedBytes [expr {$end - $tmp}]
373 } -cleanup {
374 rename getbytes {}
375 unset -nocomplain end i tmp leakedBytes
376 } -result 0
377 # Special test for a memory error in a preliminary fix of [Bug 467523]. It
378 # requires executing a helpfile. Presumably the child process is used because
379 # when this test fails, it crashes.
380 test compile-12.2 {testing error on literal deletion} -constraints {memory exec} -body {
381 set sourceFile [makeFile {
382 for {set i 0} {$i < 5} {incr i} {
383 namespace eval bar {}
384 namespace delete bar
385 }
386 puts 0
387 } source.file]
388 exec [interpreter] $sourceFile
389 } -cleanup {
390 catch {removeFile $sourceFile}
391 } -result 0
392 # Test to catch buffer overrun in TclCompileTokens from buf 530320
393 test compile-12.3 {check for a buffer overrun} -body {
394 proc crash {} {
395 puts $array([expr {a+2}])
396 }
397 crash
398 } -returnCodes error -cleanup {
399 rename crash {}
400 } -match glob -result *
401 test compile-12.4 {TclCleanupLiteralTable segfault} -body {
402 # Tcl Bug 1001997
403 # Here, we're trying to test a case that causes a crash in
404 # TclCleanupLiteralTable. The conditions that we're trying to establish
405 # are:
406 # - TclCleanupLiteralTable is attempting to clean up a bytecode object in
407 # the literal table.
408 # - The bytecode object in question contains the only reference to another
409 # literal.
410 # - The literal in question is in the same hash bucket as the bytecode
411 # object, and immediately follows it in the chain.
412 # Since newly registered literals are added at the FRONT of the bucket
413 # chains, and since the bytecode object is registered before its literals,
414 # this is difficult to achieve. What we do is:
415 # (a) do a [namespace eval] of a string that's calculated to hash into
416 # the same bucket as a literal that it contains. In this case, the
417 # script and the variable 'bugbug' land in the same bucket.
418 # (b) do a [namespace eval] of a string that contains enough literals to
419 # force TclRegisterLiteral to rebuild the global literal table. The
420 # newly created hash buckets will contain the literals, IN REVERSE
421 # ORDER, thus putting the bytecode immediately ahead of 'bugbug' and
422 # 'bug4345bug'. The bytecode object will contain the only references
423 # to those two literals.
424 # (c) Delete the interpreter to invoke TclCleanupLiteralTable and tickle
425 # the bug.
426 proc foo {} {
427 set i [interp create]
428 $i eval {
429 namespace eval ::w {concat 4649; variable bugbug}
430 namespace eval ::w {
431 concat x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 \
432 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 \
433 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 \
434 x31 x32 X33 X34 X35 X36 X37 X38 X39 X40 \
435 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 \
436 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 \
437 x61 x62 x63 x64
438 concat y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 \
439 y11 y12 y13 y14 y15 y16 y17 y18 y19 y20 \
440 y21 y22 y23 y24 y25 y26 y27 y28 y29 y30 \
441 y31 y32 Y33 Y34 Y35 Y36 Y37 Y38 Y39 Y40 \
442 y41 y42 y43 y44 y45 y46 y47 y48 y49 y50 \
443 y51 y52 y53 y54 y55 y56 y57 y58 y59 y60 \
444 y61 y62 y63 y64
445 concat z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 \
446 z11 z12 z13 z14 z15 z16 z17 z18 z19 z20 \
447 z21 z22 z23 z24 z25 z26 z27 z28 z29 z30 \
448 z31 z32
449 }
450 }
451 interp delete $i; # must not crash
452 return ok
453 }
454 foo
455 } -cleanup {
456 rename foo {}
457 } -result ok
458
459 # Special test for underestimating the maxStackSize required for a compiled
460 # command. A failure will cause a segfault in the child process.
461 test compile-13.1 {testing underestimate of maxStackSize in list cmd} {exec} {
462 set body {set x [list}
463 for {set i 0} {$i < 3000} {incr i} {
464 append body " $i"
465 }
466 append body {]; puts OK}
467 regsub BODY {proc crash {} {BODY}; crash} $body script
468 list [catch {exec [interpreter] << $script} msg] $msg
469 } {0 OK}
470
471 # Tests of nested compile (body in body compilation), should not generate stack overflow
472 # (with abnormal program termination), bug [fec0c17d39]:
473 proc _ti_gencode {} {
474 # creates test interpreter on demand with [gencode] generator:
475 if {[interp exists ti]} {
476 return
477 }
478 interp create ti
479 ti eval {proc gencode {nr {cmd eval} {nl 0}} {
480 set code ""
481 set e ""; if {$nl} {set e "\n"}
482 for {set i 0} {$i < $nr} {incr i} {
483 append code "$cmd \{$e"
484 }
485 append code "lappend result 1$e"
486 for {set i 0} {$i < $nr} {incr i} {
487 append code "\}$e"
488 }
489 #puts [format "%% %.40s ... %d bytes" $code [string length $code]]
490 return $code
491 }}
492 }
493 test compile-13.2 {TclCompileScript: testing expected nested scripts compilation} -setup {
494 # dynamic constraint - ensure the stack is large enough on this box for this test:
495 if {
496 [testConstraint unix] &&
497 ![catch { exec sh -c {ulimit -s} } stsz] &&
498 $stsz ne "unlimited" && $stsz <= 2048
499 } {
500 tcltest::Skip "too small stack limit ($stsz <= 2048)"
501 }
502 _ti_gencode
503 interp recursionlimit ti [expr {10000+50}]
504 ti eval {set result {}}
505 } -body {
506 # Test different compilation variants (instructions evalStk, invokeStk, etc),
507 # with 1500 (1000 in debug) nested scripts (bodies). If you get SO/SF exceptions on some low-stack
508 # boxes or systems, please don't decrease it (either provide new or extend a constraint above)
509 ti eval {foreach cmd {eval "if 1" try catch} {
510 set c [gencode [expr {![::tcl::pkgconfig get debug] ? 1500 : 1000}] $cmd]
511 if 1 $c
512 }}
513 ti eval {set result}
514 } -result {1 1 1 1}
515 test compile-13.3 {TclCompileScript: testing check of max depth by nested scripts compilation} -setup {
516 _ti_gencode
517 interp recursionlimit ti 100
518 ti eval {set result {}}
519 } -body {
520 # Test different compilation variants (instructions evalStk, invokeStk, etc),
521 # with 500 nested scripts (bodies). It must generate "too many nested compilations"
522 # error for any variant we're testing here:
523 ti eval {foreach cmd {eval "if 1" try catch} {
524 set c [gencode 500 $cmd]
525 lappend errors [catch $c e] $e
526 }}
527 #puts $errors
528 # all of nested calls exceed the limit, so must end with "too many nested compilations"
529 # (or evaluations, depending on compile method/instruction and "mixed" compile within
530 # evaluation), so no one succeeds, the result must be empty:
531 ti eval {set result}
532 } -result {}
533 #
534 # clean up:
535 if {[interp exists ti]} {
536 interp delete ti
537 }
538 rename _ti_gencode {}
539
540 # Tests compile-14.* for [Bug 599788] [Bug 0c043a175a47da8c2342]
541 test compile-14.1 {testing errors in element name; segfault?} {} {
542 catch {set a([error])} msg1
543 catch {set bubba([join $abba $jubba]) $vol} msg2
544 list $msg1 $msg2
545 } {{wrong # args: should be "error message ?errorInfo? ?errorCode?"} {can't read "abba": no such variable}}
546
547 test compile-14.2 {testing element name "$"} -body {
548 unset -nocomplain a
549 set a() 1
550 set a(1) 2
551 set a($) 3
552 list [set a()] [set a(1)] [set a($)] [unset a() a(1); lindex [array names a] 0]
553 } -cleanup {unset a} -result [list 1 2 3 {$}]
554
555
556 # Tests compile-15.* cover Tcl Bug 633204
557 test compile-15.1 {proper TCL_RETURN code from [return]} {
558 apply {{} {catch return}}
559 } 2
560 test compile-15.2 {proper TCL_RETURN code from [return]} {
561 apply {{} {catch {return foo}}}
562 } 2
563 test compile-15.3 {proper TCL_RETURN code from [return]} {
564 apply {{} {catch {return $::tcl_library}}}
565 } 2
566 test compile-15.4 {proper TCL_RETURN code from [return]} {
567 apply {{} {catch {return [info library]}}}
568 } 2
569 test compile-15.5 {proper TCL_RETURN code from [return]} {
570 apply {{} {catch {set a 1}; return}}
571 } ""
572
573 for {set noComp 0} {$noComp <= 1} {incr noComp} {
574
575 if {$noComp} {
576 interp alias {} run {} testevalex
577 set constraints testevalex
578 } else {
579 interp alias {} run {} if 1
580 set constraints {}
581 }
582
583 test compile-16.1.$noComp {TclCompileScript: word expansion} $constraints {
584 run "list [string repeat {{*}a } 255]"
585 } [lrepeat 255 a]
586 test compile-16.2.$noComp {TclCompileScript: word expansion} $constraints {
587 run "list [string repeat {{*}a } 256]"
588 } [lrepeat 256 a]
589 test compile-16.3.$noComp {TclCompileScript: word expansion} $constraints {
590 run "list [string repeat {{*}a } 257]"
591 } [lrepeat 257 a]
592 test compile-16.4.$noComp {TclCompileScript: word expansion} $constraints {
593 run {{*}list}
594 } {}
595 test compile-16.5.$noComp {TclCompileScript: word expansion} $constraints {
596 run {{*}list {*}{x y z}}
597 } {x y z}
598 test compile-16.6.$noComp {TclCompileScript: word expansion} $constraints {
599 run {{*}list {*}[list x y z]}
600 } {x y z}
601 test compile-16.7.$noComp {TclCompileScript: word expansion} $constraints {
602 run {{*}list {*}[list x y z][list x y z]}
603 } {x y zx y z}
604 test compile-16.8.$noComp {TclCompileScript: word expansion} -body {
605 set l {x y z}
606 run {{*}list {*}$l}
607 } -constraints $constraints -cleanup {
608 unset l
609 } -result {x y z}
610 test compile-16.9.$noComp {TclCompileScript: word expansion} -body {
611 set l {x y z}
612 run {{*}list {*}$l$l}
613 } -constraints $constraints -cleanup {
614 unset l
615 } -result {x y zx y z}
616 test compile-16.10.$noComp {TclCompileScript: word expansion} -body {
617 run {{*}\{}
618 } -constraints $constraints -returnCodes error \
619 -result {unmatched open brace in list}
620 test compile-16.11.$noComp {TclCompileScript: word expansion} -body {
621 proc badList {} {return \{}
622 run {{*}[badList]}
623 } -constraints $constraints -cleanup {
624 rename badList {}
625 } -returnCodes error -result {unmatched open brace in list}
626 test compile-16.12.$noComp {TclCompileScript: word expansion} $constraints {
627 run {{*}list x y z}
628 } {x y z}
629 test compile-16.13.$noComp {TclCompileScript: word expansion} $constraints {
630 run {{*}list x y {*}z}
631 } {x y z}
632 test compile-16.14.$noComp {TclCompileScript: word expansion} $constraints {
633 run {{*}list x {*}y z}
634 } {x y z}
635 test compile-16.15.$noComp {TclCompileScript: word expansion} $constraints {
636 run {list x y {*}z}
637 } {x y z}
638 test compile-16.16.$noComp {TclCompileScript: word expansion} $constraints {
639 run {list x {*}y z}
640 } {x y z}
641 test compile-16.17.$noComp {TclCompileScript: word expansion} $constraints {
642 run {list {*}x y z}
643 } {x y z}
644
645 # These tests note that expansion can in theory cause the number of arguments
646 # to a command to exceed INT_MAX, which is as big as objc is allowed to get.
647 #
648 # In practice, it seems we will run out of memory before we confront this
649 # issue. Note that compiled operations run out of memory at smaller objc
650 # values than direct string evaluation.
651 #
652 # These tests are constrained as knownBug because they are likely to cause
653 # memory allocation panics somewhere, and we don't want panics in the test
654 # suite.
655 #
656 test compile-16.18.$noComp {TclCompileScript: word expansion} -body {
657 proc LongList {} {return [lrepeat [expr {1<<10}] x]}
658 llength [run "list [string repeat {{*}[LongList] } [expr {1<<10}]]"]
659 } -constraints [linsert $constraints 0 knownBug] -cleanup {
660 rename LongList {}
661 } -returnCodes ok -result [expr {1<<20}]
662 test compile-16.19.$noComp {TclCompileScript: word expansion} -body {
663 proc LongList {} {return [lrepeat [expr {1<<11}] x]}
664 llength [run "list [string repeat {{*}[LongList] } [expr {1<<11}]]"]
665 } -constraints [linsert $constraints 0 knownBug] -cleanup {
666 rename LongList {}
667 } -returnCodes ok -result [expr {1<<22}]
668 test compile-16.20.$noComp {TclCompileScript: word expansion} -body {
669 proc LongList {} {return [lrepeat [expr {1<<12}] x]}
670 llength [run "list [string repeat {{*}[LongList] } [expr {1<<12}]]"]
671 } -constraints [linsert $constraints 0 knownBug] -cleanup {
672 rename LongList {}
673 } -returnCodes ok -result [expr {1<<24}]
674 # This is the one that should cause overflow
675 test compile-16.21.$noComp {TclCompileScript: word expansion} -body {
676 proc LongList {} {return [lrepeat [expr {1<<16}] x]}
677 llength [run "list [string repeat {{*}[LongList] } [expr {1<<16}]]"]
678 } -constraints [linsert $constraints 0 knownBug] -cleanup {
679 rename LongList {}
680 } -returnCodes ok -result [expr {wide(1)<<32}]
681 test compile-16.22.$noComp {
682 Bug 845412: TclCompileScript: word expansion not mandatory
683 } -body {
684 # This test may crash and will fail unless Bug 845412 is fixed.
685 proc ReturnResults args {return $args}
686 run "ReturnResults [string repeat {x } 260]"
687 } -constraints $constraints -cleanup {
688 rename ReturnResults {}
689 } -returnCodes ok -result [string trim [string repeat {x } 260]]
690 test compile-16.23.$noComp {
691 Bug 1032805: defer parse error until run time
692 } -constraints $constraints -body {
693 namespace eval x {
694 run {
695 proc if {a b} {uplevel 1 [list set $a $b]}
696 if 1 {syntax {}{}}
697 }
698 }
699 } -cleanup {
700 namespace delete x
701 } -returnCodes ok -result {syntax {}{}}
702 test compile-16.24.$noComp {
703 Bug 1638414: bad list constant as first expanded term
704 } -constraints $constraints -body {
705 run "{*}\"\{foo bar\""
706 } -returnCodes error -result {unmatched open brace in list}
707 test compile-16.25.$noComp {TclCompileScript: word expansion, naked backslashes} $constraints {
708 run {list {*}{a \n b}}
709 } {a {
710 } b}
711 test compile-16.26.$noComp {TclCompileScript: word expansion, protected backslashes} $constraints {
712 run {list {*}{a {\n} b}}
713 } {a {\n} b}
714 } ;# End of noComp loop
715
716 # These tests are messy because it wrecks the interpreter it runs in! They
717 # demonstrate issues arising from [FRQ 1101710]
718 test compile-17.1 {Command interpretation binding for compiled code} -constraints knownBug -setup {
719 set i [interp create]
720 } -body {
721 $i eval {
722 if 1 {
723 expr [
724 proc expr args {return substituted}
725 format {[subst compiled]}
726 ]
727 }
728 }
729 } -cleanup {
730 interp delete $i
731 } -result substituted
732 test compile-17.2 {Command interpretation binding for non-compiled code} -setup {
733 set i [interp create]
734 } -body {
735 $i eval {
736 if 1 {
737 [subst expr] [
738 proc expr args {return substituted}
739 format {[subst compiled]}
740 ]
741 }
742 }
743 } -cleanup {
744 interp delete $i
745 } -result substituted
746
747 # This tests the supported parts of the unsupported [disassemble] command. It
748 # does not check the format of disassembled bytecode though; that's liable to
749 # change without warning.
750
751 set disassemblables [linsert [join {
752 constructor destructor lambda method objmethod proc script
753 } ", "] end-1 or]
754 test compile-18.1 {disassembler - basics} -returnCodes error -body {
755 tcl::unsupported::disassemble
756 } -match glob -result {wrong # args: should be "*"}
757 test compile-18.2 {disassembler - basics} -returnCodes error -body {
758 tcl::unsupported::disassemble ?
759 } -result "bad type \"?\": must be $disassemblables"
760 test compile-18.3 {disassembler - basics} -returnCodes error -body {
761 tcl::unsupported::disassemble lambda
762 } -match glob -result {wrong # args: should be "* lambda lambdaTerm"}
763 test compile-18.4 {disassembler - basics} -returnCodes error -body {
764 tcl::unsupported::disassemble lambda \{
765 } -result "can't interpret \"\{\" as a lambda expression"
766 test compile-18.5 {disassembler - basics} -body {
767 # Allow any string: the result format is not defined anywhere!
768 tcl::unsupported::disassemble lambda {{} {}}
769 } -match glob -result *
770 test compile-18.6 {disassembler - basics} -returnCodes error -body {
771 tcl::unsupported::disassemble proc
772 } -match glob -result {wrong # args: should be "* proc procName"}
773 test compile-18.7 {disassembler - basics} -returnCodes error -body {
774 tcl::unsupported::disassemble proc nosuchproc
775 } -result {"nosuchproc" isn't a procedure}
776 test compile-18.8 {disassembler - basics} -setup {
777 proc chewonthis {} {}
778 } -body {
779 # Allow any string: the result format is not defined anywhere!
780 tcl::unsupported::disassemble proc chewonthis
781 } -cleanup {
782 rename chewonthis {}
783 } -match glob -result *
784 test compile-18.9 {disassembler - basics} -returnCodes error -body {
785 tcl::unsupported::disassemble script
786 } -match glob -result {wrong # args: should be "* script script"}
787 test compile-18.10 {disassembler - basics} -body {
788 # Allow any string: the result format is not defined anywhere!
789 tcl::unsupported::disassemble script {}
790 } -match glob -result *
791 test compile-18.11 {disassembler - basics} -returnCodes error -body {
792 tcl::unsupported::disassemble method
793 } -match glob -result {wrong # args: should be "* method className methodName"}
794 test compile-18.12 {disassembler - basics} -returnCodes error -body {
795 tcl::unsupported::disassemble method nosuchclass foo
796 } -result {nosuchclass does not refer to an object}
797 test compile-18.13 {disassembler - basics} -returnCodes error -setup {
798 oo::object create justanobject
799 } -body {
800 tcl::unsupported::disassemble method justanobject foo
801 } -cleanup {
802 justanobject destroy
803 } -result {"justanobject" is not a class}
804 test compile-18.14 {disassembler - basics} -returnCodes error -body {
805 tcl::unsupported::disassemble method oo::object nosuchmethod
806 } -result {unknown method "nosuchmethod"}
807 test compile-18.15 {disassembler - basics} -setup {
808 oo::class create foo {method bar {} {}}
809 } -body {
810 # Allow any string: the result format is not defined anywhere!
811 tcl::unsupported::disassemble method foo bar
812 } -cleanup {
813 foo destroy
814 } -match glob -result *
815 test compile-18.16 {disassembler - basics} -returnCodes error -body {
816 tcl::unsupported::disassemble objmethod
817 } -match glob -result {wrong # args: should be "* objmethod objectName methodName"}
818 test compile-18.17 {disassembler - basics} -returnCodes error -body {
819 tcl::unsupported::disassemble objmethod nosuchobject foo
820 } -result {nosuchobject does not refer to an object}
821 test compile-18.18 {disassembler - basics} -returnCodes error -body {
822 tcl::unsupported::disassemble objmethod oo::object nosuchmethod
823 } -result {unknown method "nosuchmethod"}
824 test compile-18.19 {disassembler - basics} -setup {
825 oo::object create foo
826 oo::objdefine foo {method bar {} {}}
827 } -body {
828 # Allow any string: the result format is not defined anywhere!
829 tcl::unsupported::disassemble objmethod foo bar
830 } -cleanup {
831 foo destroy
832 } -match glob -result *
833 # There never was a compile-18.20.
834 # The keys of the dictionary produced by [getbytecode] are defined.
835 set bytecodekeys {literals variables exception instructions auxiliary commands script namespace stackdepth exceptdepth}
836 test compile-18.21 {disassembler - basics} -returnCodes error -body {
837 tcl::unsupported::getbytecode
838 } -match glob -result {wrong # args: should be "*"}
839 test compile-18.22 {disassembler - basics} -returnCodes error -body {
840 tcl::unsupported::getbytecode ?
841 } -result "bad type \"?\": must be $disassemblables"
842 test compile-18.23 {disassembler - basics} -returnCodes error -body {
843 tcl::unsupported::getbytecode lambda
844 } -match glob -result {wrong # args: should be "* lambda lambdaTerm"}
845 test compile-18.24 {disassembler - basics} -returnCodes error -body {
846 tcl::unsupported::getbytecode lambda \{
847 } -result "can't interpret \"\{\" as a lambda expression"
848 test compile-18.25 {disassembler - basics} -body {
849 dict keys [tcl::unsupported::getbytecode lambda {{} {}}]
850 } -result "$bytecodekeys initiallinenumber sourcefile"
851 test compile-18.26 {disassembler - basics} -returnCodes error -body {
852 tcl::unsupported::getbytecode proc
853 } -match glob -result {wrong # args: should be "* proc procName"}
854 test compile-18.27 {disassembler - basics} -returnCodes error -body {
855 tcl::unsupported::getbytecode proc nosuchproc
856 } -result {"nosuchproc" isn't a procedure}
857 test compile-18.28 {disassembler - basics} -setup {
858 proc chewonthis {} {}
859 } -body {
860 dict keys [tcl::unsupported::getbytecode proc chewonthis]
861 } -cleanup {
862 rename chewonthis {}
863 } -result "$bytecodekeys initiallinenumber sourcefile"
864 test compile-18.28.1 {disassembler - tricky bit} -setup {
865 eval [list proc chewonthis {} {}]
866 } -body {
867 dict keys [tcl::unsupported::getbytecode proc chewonthis]
868 } -cleanup {
869 rename chewonthis {}
870 } -result $bytecodekeys
871 test compile-18.28.2 {disassembler - tricky bit} -setup {
872 eval {proc chewonthis {} {}}
873 } -body {
874 dict keys [tcl::unsupported::getbytecode proc chewonthis]
875 } -cleanup {
876 rename chewonthis {}
877 } -result "$bytecodekeys initiallinenumber sourcefile"
878 test compile-18.28.3 {disassembler - tricky bit} -setup {
879 proc Proc {n a b} {
880 proc $n $a $b
881 }
882 Proc chewonthis {} {}
883 } -body {
884 dict keys [tcl::unsupported::getbytecode proc chewonthis]
885 } -cleanup {
886 rename Proc {}
887 rename chewonthis {}
888 } -result $bytecodekeys
889 test compile-18.28.4 {disassembler - tricky bit} -setup {
890 proc Proc {n a b} {
891 tailcall proc $n $a $b
892 }
893 Proc chewonthis {} {}
894 } -body {
895 dict keys [tcl::unsupported::getbytecode proc chewonthis]
896 } -cleanup {
897 rename Proc {}
898 rename chewonthis {}
899 } -result "$bytecodekeys initiallinenumber sourcefile"
900 test compile-18.29 {disassembler - basics} -returnCodes error -body {
901 tcl::unsupported::getbytecode script
902 } -match glob -result {wrong # args: should be "* script script"}
903 test compile-18.30 {disassembler - basics} -body {
904 dict keys [tcl::unsupported::getbytecode script {}]
905 } -result $bytecodekeys
906 test compile-18.31 {disassembler - basics} -returnCodes error -body {
907 tcl::unsupported::getbytecode method
908 } -match glob -result {wrong # args: should be "* method className methodName"}
909 test compile-18.32 {disassembler - basics} -returnCodes error -body {
910 tcl::unsupported::getbytecode method nosuchclass foo
911 } -result {nosuchclass does not refer to an object}
912 test compile-18.33 {disassembler - basics} -returnCodes error -setup {
913 oo::object create justanobject
914 } -body {
915 tcl::unsupported::getbytecode method justanobject foo
916 } -cleanup {
917 justanobject destroy
918 } -result {"justanobject" is not a class}
919 test compile-18.34 {disassembler - basics} -returnCodes error -body {
920 tcl::unsupported::getbytecode method oo::object nosuchmethod
921 } -result {unknown method "nosuchmethod"}
922 test compile-18.35 {disassembler - basics} -setup {
923 oo::class create foo {method bar {} {}}
924 } -body {
925 dict keys [tcl::unsupported::getbytecode method foo bar]
926 } -cleanup {
927 foo destroy
928 } -result "$bytecodekeys initiallinenumber sourcefile"
929 test compile-18.36 {disassembler - basics} -returnCodes error -body {
930 tcl::unsupported::getbytecode objmethod
931 } -match glob -result {wrong # args: should be "* objmethod objectName methodName"}
932 test compile-18.37 {disassembler - basics} -returnCodes error -body {
933 tcl::unsupported::getbytecode objmethod nosuchobject foo
934 } -result {nosuchobject does not refer to an object}
935 test compile-18.38 {disassembler - basics} -returnCodes error -body {
936 tcl::unsupported::getbytecode objmethod oo::object nosuchmethod
937 } -result {unknown method "nosuchmethod"}
938 test compile-18.39 {disassembler - basics} -setup {
939 oo::object create foo
940 oo::objdefine foo {method bar {} {}}
941 } -body {
942 dict keys [tcl::unsupported::getbytecode objmethod foo bar]
943 } -cleanup {
944 foo destroy
945 } -result "$bytecodekeys initiallinenumber sourcefile"
946 test compile-18.40 {disassembler - basics} -returnCodes error -body {
947 tcl::unsupported::disassemble constructor
948 } -match glob -result {wrong # args: should be "* constructor className"}
949 test compile-18.41 {disassembler - basics} -returnCodes error -body {
950 tcl::unsupported::disassemble constructor nosuchclass
951 } -result {nosuchclass does not refer to an object}
952 test compile-18.42 {disassembler - basics} -returnCodes error -setup {
953 oo::object create justanobject
954 } -body {
955 tcl::unsupported::disassemble constructor justanobject
956 } -cleanup {
957 justanobject destroy
958 } -result {"justanobject" is not a class}
959 test compile-18.43 {disassembler - basics} -returnCodes error -setup {
960 oo::class create constructorless
961 } -body {
962 tcl::unsupported::disassemble constructor constructorless
963 } -cleanup {
964 constructorless destroy
965 } -result {"constructorless" has no defined constructor}
966 test compile-18.44 {disassembler - basics} -setup {
967 oo::class create foo {constructor {} {set x 1}}
968 } -body {
969 # Allow any string: the result format is not defined anywhere!
970 tcl::unsupported::disassemble constructor foo
971 } -cleanup {
972 foo destroy
973 } -match glob -result *
974 test compile-18.45 {disassembler - basics} -returnCodes error -body {
975 tcl::unsupported::getbytecode constructor
976 } -match glob -result {wrong # args: should be "* constructor className"}
977 test compile-18.46 {disassembler - basics} -returnCodes error -body {
978 tcl::unsupported::getbytecode constructor nosuchobject
979 } -result {nosuchobject does not refer to an object}
980 test compile-18.47 {disassembler - basics} -returnCodes error -setup {
981 oo::class create constructorless
982 } -body {
983 tcl::unsupported::getbytecode constructor constructorless
984 } -cleanup {
985 constructorless destroy
986 } -result {"constructorless" has no defined constructor}
987 test compile-18.48 {disassembler - basics} -setup {
988 oo::class create foo {constructor {} {set x 1}}
989 } -body {
990 dict keys [tcl::unsupported::getbytecode constructor foo]
991 } -cleanup {
992 foo destroy
993 } -result "$bytecodekeys"
994 # There is no compile-18.49
995 test compile-18.50 {disassembler - basics} -returnCodes error -body {
996 tcl::unsupported::disassemble destructor
997 } -match glob -result {wrong # args: should be "* destructor className"}
998 test compile-18.51 {disassembler - basics} -returnCodes error -body {
999 tcl::unsupported::disassemble destructor nosuchclass
1000 } -result {nosuchclass does not refer to an object}
1001 test compile-18.52 {disassembler - basics} -returnCodes error -setup {
1002 oo::object create justanobject
1003 } -body {
1004 tcl::unsupported::disassemble destructor justanobject
1005 } -cleanup {
1006 justanobject destroy
1007 } -result {"justanobject" is not a class}
1008 test compile-18.53 {disassembler - basics} -returnCodes error -setup {
1009 oo::class create constructorless
1010 } -body {
1011 tcl::unsupported::disassemble destructor constructorless
1012 } -cleanup {
1013 constructorless destroy
1014 } -result {"constructorless" has no defined destructor}
1015 test compile-18.54 {disassembler - basics} -setup {
1016 oo::class create foo {destructor {set x 1}}
1017 } -body {
1018 # Allow any string: the result format is not defined anywhere!
1019 tcl::unsupported::disassemble destructor foo
1020 } -cleanup {
1021 foo destroy
1022 } -match glob -result *
1023 test compile-18.55 {disassembler - basics} -returnCodes error -body {
1024 tcl::unsupported::getbytecode destructor
1025 } -match glob -result {wrong # args: should be "* destructor className"}
1026 test compile-18.56 {disassembler - basics} -returnCodes error -body {
1027 tcl::unsupported::getbytecode destructor nosuchobject
1028 } -result {nosuchobject does not refer to an object}
1029 test compile-18.57 {disassembler - basics} -returnCodes error -setup {
1030 oo::class create constructorless
1031 } -body {
1032 tcl::unsupported::getbytecode destructor constructorless
1033 } -cleanup {
1034 constructorless destroy
1035 } -result {"constructorless" has no defined destructor}
1036 test compile-18.58 {disassembler - basics} -setup {
1037 oo::class create foo {destructor {set x 1}}
1038 } -body {
1039 dict keys [tcl::unsupported::getbytecode destructor foo]
1040 } -cleanup {
1041 foo destroy
1042 } -result "$bytecodekeys"
1043
1044 test compile-19.0 {Bug 3614102: reset stack housekeeping} -body {
1045 # This will panic in a --enable-symbols=compile build, unless bug is fixed.
1046 apply {{} {list [if 1]}}
1047 } -returnCodes error -match glob -result *
1048
1049 test compile-20.1 {ensure there are no infinite loops in optimizing} {
1050 tcl::unsupported::disassemble script {
1051 while 1 {
1052 return -code continue -level 0
1053 }
1054 }
1055 return
1056 } {}
1057 test compile-20.2 {ensure there are no infinite loops in optimizing} {
1058 tcl::unsupported::disassemble script {
1059 while 1 {
1060 while 1 {
1061 return -code break -level 0
1062 }
1063 }
1064 }
1065 return
1066 } {}
1067
1068 test compile-21.1 {stack balance management} {
1069 apply {{} {
1070 set result {}
1071 while 1 {
1072 lappend result a
1073 lappend result [list b [break]]
1074 lappend result c
1075 }
1076 return $result
1077 }}
1078 } a
1079 test compile-21.2 {stack balance management} {
1080 apply {{} {
1081 set result {}
1082 while {[incr i] <= 10} {
1083 lappend result $i
1084 lappend result [list b [continue] c]
1085 lappend result c
1086 }
1087 return $result
1088 }}
1089 } {1 2 3 4 5 6 7 8 9 10}
1090 test compile-21.3 {stack balance management} {
1091 apply {args {
1092 set result {}
1093 while 1 {
1094 lappend result a
1095 lappend result [concat {*}$args [break]]
1096 lappend result c
1097 }
1098 return $result
1099 }} P Q R S T
1100 } a
1101 test compile-21.4 {stack balance management} {
1102 apply {args {
1103 set result {}
1104 while {[incr i] <= 10} {
1105 lappend result $i
1106 lappend result [concat {*}$args [continue] c]
1107 lappend result c
1108 }
1109 return $result
1110 }} P Q R S T
1111 } {1 2 3 4 5 6 7 8 9 10}
1112
1113 # TODO sometime - check that bytecode from tbcload is *not* disassembled.
1114
1115 # cleanup
1116 catch {rename p ""}
1117 catch {namespace delete test_ns_compile}
1118 catch {unset x}
1119 catch {unset y}
1120 catch {unset a}
1121 ::tcltest::cleanupTests
1122 return
1123
1124 # Local Variables:
1125 # mode: tcl
1126 # fill-column: 78
1127 # End: