-
Notifications
You must be signed in to change notification settings - Fork 138
/
exceptions.t
788 lines (712 loc) · 14.6 KB
/
exceptions.t
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
#! perl
# Copyright (C) 2001-2014, Parrot Foundation.
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 32,
qw[run_command slurp_file];
use Parrot::Test::Util 'create_tempfile';
=head1 NAME
t/op/exceptions.t - Exception Handling
=head1 SYNOPSIS
% prove t/op/exceptions.t
=head1 DESCRIPTION
Tests C<Exception> and C<ExceptionHandler> PMCs.
=cut
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh label - pop_eh" );
.pcc_sub :main main:
push_eh _handler
print "ok 1\n"
pop_eh
print "ok 2\n"
end
_handler:
end
CODE
ok 1
ok 2
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh eh - pop_eh" );
.pcc_sub :main main:
new P29, 'ExceptionHandler'
push_eh P29
print "ok 1\n"
pop_eh
print "ok 2\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh - throw" );
.pcc_sub :main main:
print "main\n"
push_eh _handler
new P30, 'Exception'
throw P30
print "not reached\n"
end
_handler:
print "caught it\n"
end
CODE
main
caught it
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh eh - throw" );
.pcc_sub :main main:
print "main\n"
new P29, 'ExceptionHandler'
set_label P29, _handler
push_eh P29
new P30, 'Exception'
throw P30
print "not reached\n"
end
_handler:
print "caught it\n"
end
CODE
main
caught it
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "get_results" );
.pcc_sub :main main:
print "main\n"
push_eh handler
new P1, 'Exception'
set P1, "just pining"
throw P1
print "not reached\n"
end
handler:
get_results "0", P0
set S0, P0
print "caught it\n"
typeof S1, P0
print S1
print "\n"
print S0
print "\n"
null P5
end
CODE
main
caught it
Exception
just pining
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "get_results - be sure registers are ok" );
.pcc_sub :main main:
# see also #38459
print "main\n"
new P0, 'Integer'
push_eh handler
new P1, 'Exception'
set P1, "just pining"
throw P1
print "not reached\n"
end
handler:
get_results "0", P1
inc P0
print "ok\n"
end
CODE
main
ok
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', ".get_results() - PIR" );
.sub main :main
print "main\n"
push_eh _handler
new $P1, 'Exception'
set $P1, "just pining"
throw $P1
print "not reached\n"
end
_handler:
.local pmc e
.local string s
.get_results (e)
s = e
print "caught it\n"
typeof $S1, e
print $S1
print "\n"
print s
print "\n"
null $P5
.end
CODE
main
caught it
Exception
just pining
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh - throw - message" );
.pcc_sub :main main:
print "main\n"
push_eh _handler
new P30, 'Exception'
set P30, "something happened"
throw P30
print "not reached\n"
end
_handler:
get_results "0", P5
set S0, P5
print "caught it\n"
print S0
print "\n"
end
CODE
main
caught it
something happened
OUTPUT
pasm_error_output_like( <<'CODE', <<'OUTPUT', "throw - no handler" );
.pcc_sub :main main:
new P0, 'Exception'
set P0, "something happened"
throw P0
print "not reached\n"
end
CODE
/something happened/
OUTPUT
pasm_error_output_like( <<'CODE', <<'OUTPUT', "throw - no handler, no message" );
.pcc_sub :main main:
push_eh _handler
new P0, 'Exception'
pop_eh
throw P0
print "not reached\n"
end
_handler:
end
CODE
/No exception handler and no message/
OUTPUT
pasm_error_output_like( <<'CODE', <<'OUTPUT', "throw - no handler, no message" );
.pcc_sub :main main:
new P0, 'Exception'
throw P0
print "not reached\n"
end
CODE
/No exception handler and no message/
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "2 exception handlers" );
.pcc_sub :main main:
print "main\n"
push_eh _handler1
push_eh _handler2
new P30, 'Exception'
set P30, "something happened"
throw P30
print "not reached\n"
end
_handler1:
get_results "0", P5
set S0, P5
print "caught it in 1\n"
print S0
print "\n"
end
_handler2:
get_results "0", P0
set S0, P0
print "caught it in 2\n"
print S0
print "\n"
end
CODE
main
caught it in 2
something happened
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "2 exception handlers, throw next" );
.pcc_sub :main main:
print "main\n"
push_eh _handler1
push_eh _handler2
new P30, 'Exception'
set P30, "something happened"
throw P30
print "not reached\n"
end
_handler1:
get_results "0", P5
set S0, P5
print "caught it in 1\n"
print S0
print "\n"
end
_handler2:
get_results "0", P5
set S0, P5
print "caught it in 2\n"
print S0
print "\n"
rethrow P5
end
CODE
main
caught it in 2
something happened
caught it in 1
something happened
OUTPUT
pasm_output_is( <<'CODE', <<OUT, "die" );
.pcc_sub :main main:
push_eh _handler
die 3, 100
print "not reached\n"
end
_handler:
print "caught it\n"
end
CODE
caught it
OUT
pasm_output_is( <<'CODE', <<OUT, "die, error, severity" );
.pcc_sub :main main:
push_eh _handler
die 3, 100
print "not reached\n"
end
_handler:
get_results "0", P5
print "caught it\n"
set I0, P5['severity']
print "severity "
print I0
print "\n"
end
CODE
caught it
severity 3
OUT
pasm_error_output_like( <<'CODE', <<OUT, "die - no handler" );
.pcc_sub :main main:
die 3, 100
print "not reached\n"
end
_handler:
print "caught it\n"
end
CODE
/No exception handler and no message/
OUT
pasm_output_is( <<'CODE', '', "exit exception" );
.pcc_sub :main main:
noop
exit 0
print "not reached\n"
end
CODE
pasm_output_is( <<'CODE', <<'OUTPUT', "push_eh - throw" );
.pcc_sub :main main:
print "main\n"
push_eh handler
print "ok\n"
new P30, 'Exception'
throw P30
print "not reached\n"
end
handler:
print "caught it\n"
end
CODE
main
ok
caught it
OUTPUT
1;
pir_error_output_like( <<'CODE', <<'OUTPUT', 'pop_eh with no handler' );
.sub main :main
pop_eh
print "no exceptions.\n"
.end
CODE
/No handler to delete./
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', 'pop_eh out of context (2)');
.sub main :main
.local pmc outer, cont
push_eh handler
test1()
print "skipped.\n"
goto done
handler:
.local pmc exception
.get_results (exception)
$S0 = exception
print "Error: "
print $S0
print "\n"
done:
print "done.\n"
.end
.sub test1
.local pmc exit
print "[in test1]\n"
## pop_eh is illegal here, and signals an exception.
pop_eh
print "[cleared]\n"
.end
CODE
[in test1]
Error: No handler to delete.
done.
OUTPUT
# stringification is handled by a vtable, which runs in a second
# runloop. when an error in the method tries to go to a Error_Handler defined
# outside it, it winds up going to the inner runloop, giving strange results.
pir_output_is( <<'CODE', <<'OUTPUT', 'pop_eh out of context (2)' );
.sub main :main
$P0 = get_hll_global ['Foo'], 'load'
$P0()
$P0 = new 'Foo'
push_eh catch
$S0 = $P0
pop_eh
say "huh?"
.return()
catch:
.get_results ($P1)
finalize $P1
say "caught"
.return()
.end
.namespace ['Foo']
.sub load
$P0 = newclass 'Foo'
.end
.sub get_string :vtable :method
$P0 = new 'Exception'
throw $P0
.end
CODE
caught
OUTPUT
pir_error_output_like( <<'CODE', <<'OUTPUT', "throw in main, no handler" );
.sub main :main
print "main\n"
$P0 = new 'Exception'
throw $P0
.return()
.end
CODE
/^main
No exception handler/
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "exit_handler via exit exception" );
.sub main :main
.local pmc a
.lex 'a', a
a = new 'Integer'
a = 42
push_eh handler
exit 0
handler:
.const 'Sub' $P0 = 'exit_handler'
capture_lex $P0
.tailcall $P0()
.end
.sub exit_handler :outer(main)
say "at_exit"
.local pmc a
a = find_lex 'a'
print 'a = '
say a
.end
CODE
at_exit
a = 42
OUTPUT
## Regression test for r14697. This probably won't be needed when PDD23 is
## fully implemented.
pir_error_output_like( <<'CODE', <<'OUTPUT', "invoke handler in calling sub" );
## This tests that error handlers are out of scope when invoked (necessary for
## rethrow) when the error is signalled in another sub.
.sub main :main
push_eh handler
broken()
print "not reached.\n"
handler:
.local pmc exception
.get_results (exception)
$S0 = exception
print "in handler.\n"
print $S0
print "\n"
rethrow exception
.end
.sub broken
$P0 = new 'Exception'
$P0 = "something broke"
throw $P0
.end
CODE
/\Ain handler.
something broke
something broke
current inst/
OUTPUT
pir_error_output_like( <<'CODE', <<'OUTPUT', 'die_s' );
.sub main :main
die 'We are dying str!'
.end
CODE
/We are dying str!/
OUTPUT
pir_error_output_like( <<'CODE', <<'OUTPUT', 'die_p' );
.sub main :main
.local pmc msg
msg = new 'String'
msg = 'We are dying pmc!'
die msg
.end
CODE
/We are dying pmc!/
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "resuming after exception handled - goto label" );
.sub main :main
print "before calling setup_foo\n"
setup_foo()
print "after calling setup_foo\n"
end
.end
.sub setup_foo
print "in setup_foo\n"
newclass $P0, "Foo"
push_eh handler
newclass $P0, "Foo"
pop_eh
resume:
print "running more code\n"
.return()
handler:
print "in handler\n"
goto resume
.end
CODE
before calling setup_foo
in setup_foo
in handler
running more code
after calling setup_foo
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "resuming after exception handled - return from cont" );
.sub main :main
print "before calling setup_foo\n"
setup_foo()
print "after calling setup_foo\n"
end
.end
.sub setup_foo
print "in setup_foo\n"
newclass $P0, "Foo"
push_eh handler
newclass $P0, "Foo"
pop_eh
resume:
print "never reached\n"
handler:
print "in handler\n"
.return()
.end
CODE
before calling setup_foo
in setup_foo
in handler
after calling setup_foo
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "resuming after exception handled - return from cont" );
# This test is a simplified version of PGE's grammar creation code.
.sub main :main
$P1 = newclass 'FirstClass'
$P1 = newclass 'MakerClass'
$P0 = new 'String'
$P0 = 'Foo'
$P1 = newclass $P0
print "before compile\n"
compile($P0)
print "after compile\n"
end
.end
.sub compile
.param string classname
print "in compile subroutine\n"
$P0 = new 'FirstClass'
$P1 = $P0.'compile'(classname)
print "returned from handler\nException message: "
say $P1
.end
.namespace [ "FirstClass" ]
.sub 'compile' :method
.param pmc name
print "in compile method\n"
$P1 = new 'String'
$P1 = "no exception\n"
make_grammar:
push_eh handler
$P0 = new 'MakerClass'
$P0.'make'(name)
pop_eh
.return($P1)
handler:
get_results "0", $P2
print "in handler\n"
.return ($P2)
.end
.namespace [ "MakerClass" ]
.sub 'make' :method
.param pmc name
print "in make method\n"
$P0 = newclass name
print "after newclass, never reached\n"
.end
CODE
before compile
in compile subroutine
in compile method
in make method
in handler
returned from handler
Exception message: Class Foo already registered
after compile
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "Resumable exceptions" );
.sub main :main
push_eh _handler
new $P1, 'Exception'
say 'Before throwing'
throw $P1
say 'After throwing'
end
_handler:
.local pmc e
.local string s
.local pmc c
.get_results (e)
s = e
say 'In the exception handler'
c = e['resume']
c()
.end
CODE
Before throwing
In the exception handler
After throwing
OUTPUT
pir_output_is( <<'CODE', <<'OUTPUT', "Resumable exceptions from a different context");
.sub main :main
push_eh catcher
'foo'()
pop_eh
say 'ok 4'
.return ()
catcher:
.get_results ($P0)
$P1 = $P0['resume']
say 'in the handler'
$P1()
.end
.sub 'foo'
say 'ok 1'
$P0 = new 'Exception'
throw $P0
say 'ok 2'
$P0 = new 'Exception'
throw $P0
say 'ok 3'
.end
CODE
ok 1
in the handler
ok 2
in the handler
ok 3
ok 4
OUTPUT
# Test massaged from TT #2188
SKIP: {
skip "with valgrind", 1 if $ENV{VALGRIND};
sub compile_wx {
my $code = shift;
my ($fh, $wx_filename) = create_tempfile( SUFFIX => '.winxed', UNLINK => 1 );
(undef, my $pir_filename) = create_tempfile( SUFFIX => '.pir', UNLINK => 1 );
(undef, my $pbc_filename) = create_tempfile( SUFFIX => '.pbc', UNLINK => 1 );
print $fh $code;
if (system(qw[./winxed --target=pir -o], $pir_filename, $wx_filename)) {
die "Couldn't compile winxed code";
}
if (system(qw[./parrot -o], $pbc_filename, $pir_filename)) {
die "couldn't compile PIR";
}
close $fh;
return $pbc_filename;
}
my $friend = compile_wx(<<'WX1'); my $exptest = compile_wx(sprintf(<<'WX2', $friend));
namespace em {
class Friend {
function Friend() {}
function call() {
throw new 'Exception'({
"message" : "Damned ! an exception !",
"severity" : 100});
}
}
}
WX1
$load '%s';
namespace em {
class Friend;
class User {
function User() {}
function call0() {
try {
self.call1();
} catch(ex) {
say(ex['message']);
for (string exstr in ex.backtrace_strings()) {
for (string str in split("\n",exstr)) {
say(str);
}
}
}
self.call1();
}
function call1() {
var friend = new em.Friend();
friend.call();
}
}
}
function main[main](var argv) {
var user = new em.User();
user.call0();
}
WX2
(undef, my $user_bt_file) = create_tempfile( UNLINK => 1 );
(undef, my $auto_bt_file) = create_tempfile( UNLINK => 1 );
run_command("./parrot $exptest",
STDOUT => $user_bt_file,
STDERR => $auto_bt_file);
my @user_bt = split /\n/, slurp_file($user_bt_file);
my @auto_bt = split /\n/, slurp_file($auto_bt_file);
# 3rd backtrace frame differs intentionally
delete $user_bt[3]; delete $auto_bt[3];
is_deeply([@user_bt], [@auto_bt],
"user-level backtraces the same as automatically generated backtraces");
}
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: