blib/lib/Async/Defer.pm

filter: Statementstmt 98.8% 334 / 338 Branchbran 96.5% 139 / 144 Conditioncond 90.3% 56 / 62 MC/DCmcdc 82.5% 33 / 40 Subroutinesub 100% 47 / 47 Podpod 100% 16 / 16 totaltotal 96.6% 625 / 647 CC 98 SCAR 22.5
CC
98
Cov
97%
done
20.0
throw
19.0
_skip_while
13.0
CRAP 9.5
↑ Summary
1 package Async::Defer;
2 17 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 our $VERSION = 'v1.0.1';
9  
10 17 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ## no critic (ProhibitBuiltinHomonyms)
14  
15 17 ▶use constant NOT_RUNNING=> -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use constant OP_CODE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use constant OP_DEFER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use constant OP_IF => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use constant OP_ELSE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use constant OP_ENDIF => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use constant OP_WHILE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use constant OP_ENDWHILE=> 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use constant OP_TRY => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use constant OP_CATCH => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use constant OP_FINALLY => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use constant OP_ENDTRY => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 17 my %SELF;
29  
30  
31 195 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 195 my ($class) = @_;
33 195 my $this = bless {}, $class;
34 195 $SELF{refaddr $this} = {
35 parent => undef, # parent Defer object, if any
36 opcode => [], # [[OP_CODE,$sub], [OP_TRY], ...]
37 pc => NOT_RUNNING,# point to _CURRENT_ opcode, if any
38 iter => [], # [[1,$outer_while_pc], [8,$inner_while_pc], ...]
39 findone => undef, # undef or ['continue'] or ['break'] or ['throw',$err]
40 };
41 195 return $this;
42 }
43  
44 62 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
45 62 my ($this) = @_;
46 62 delete $SELF{refaddr $this};
47 62 return;
48 }
49  
50 7 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
51 7 my ($this) = @_;
52 7 my $self = $SELF{refaddr $this};
53  
54 7 my $clone = __PACKAGE__->new();
55 7 my $clone_self = $SELF{refaddr $clone};
56  
57 7 $clone_self->{opcode} = [ @{ $self->{opcode} } ];
58 7 %{$clone} = %{$this};
59 7 return $clone;
60 }
61  
62 203 ▶sub iter {
Subroutine iter: called CC=2 SCAR=6.9
Pod documented
63 203 my ($this) = @_;
64 203 my $self = $SELF{refaddr $this};
65  
66 203 ▶ if (!@{ $self->{iter} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"iter"};}) 5 198 203
67 5 croak 'iter() can be used only inside while';
68 }
69  
70 198 return $self->{iter}[-1][0];
71 }
72  
73 706 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
74 706 my ($this, $op, @params) = @_;
75 706 my $self = $SELF{refaddr $this};
76  
77 706 ▶ if ($self->{pc} != NOT_RUNNING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pc"} != -1) 8 698 706
78 8 croak 'unable to modify while running';
79 }
80  
81 698 push @{ $self->{opcode} }, [ $op, @params ];
82 698 return $this;
83 }
84  
85 297 ▶sub do {
Subroutine do: called CC=3 SCAR=11.0
Pod documented
86 297 my ($this, $task, @more_tasks) = @_;
87 297 ▶ if(@more_tasks){
Branch: 1 branch
BranchTrueFalseTotal
if (@more_tasks) 3 294 297
88 3 for ($task, @more_tasks) {
89 15 $this->do($_);
90 }
91 3 return $this;
92 }
93 294 given (ref $task) {
94 294 when ('CODE') {
95 198 return $this->_add(OP_CODE, $task);
96 }
97 96 when (__PACKAGE__) {
98 76 return $this->_add(OP_DEFER, $task);
99 }
100 20 when ('ARRAY') {
101 10 my %task = map { $_ => $task->[$_] } 0 .. $#{ $task };
102 10 return $this->_add(OP_CODE, _do_batch(1, %task));
103 }
104 10 when ('HASH') {
105 9 return $this->_add(OP_CODE, _do_batch(0, %{ $task }));
106 }
107 1 default {
108 1 croak 'require CODE/Defer object or ARRAY/HASH in first param'
109 }
110 }
111 }
112  
113 19 ▶sub _do_batch {
Subroutine _do_batch: called CC=3 SCAR=11.0
114 19 my ($is_array, %task) = @_;
115  
116 # Isolate each task in own Defer object to guarantee they won't be
117 # surprised by shared state.
118 19 for my $key (keys %task) {
119 43 my $task;
120 43 given (ref $task{$key}) {
121 43 when ('CODE') {
122 39 $task = __PACKAGE__->new();
123 39 $task->do( $task{$key} );
124 }
125 4 when (__PACKAGE__) {
126 4 $task = $task{$key}->clone();
127 }
128 0 default {
129 0 ▶ my $pos = $is_array ? $key+1 : "{$key}";
Branch: 1 branch
BranchTrueFalseTotal
$is_array ? : 0 0 0
130 0 croak 'require CODE/Defer object in param '.$pos;
131 }
132 }
133 43 $task{$key} = $task;
134 }
135  
136 19 ▶ return sub{
Subroutine __ANON__: called CC=5 SCAR=16.1
137 19 my ($d, @taskparams) = @_;
138 my %taskparams
139 = !$is_array ? (@taskparams)
140 19 ▶ : (map { ($_ => $taskparams[$_]) } 0 .. $#taskparams);
Branch: 1 branch
BranchTrueFalseTotal
!$is_array ? : 9 10 19
141  
142 19 ▶ if (!keys %task) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %task) 2 17 19
143 2 return $d->done();
144 }
145  
146 17 my %taskresults = map { $_ => undef } keys %task;
147 17 for my $key (sort keys %task) { # sort just to simplify testing
148 43 my $t = __PACKAGE__->new();
149 43 $t->try();
150 43 $t->do( $task{$key} );
151 $t->catch(
152 2 ▶ qr/.*/ms => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
153 2 my ($t,$err) = @_; ## no critic (ProhibitReusedNames)
154 2 $t->{err} = $err;
155 2 $t->done();
156 },
157 19 ▶ FINALLY => sub{
Subroutine __ANON__: called CC=4 SCAR=13.9
158 19 my ($t, @result) = @_; ## no critic (ProhibitReusedNames)
159 19 ▶ $taskresults{$key} = $t->{err} // \@result;
Condition100%2/2
$t->{"err"} // \@result
l!l
217
MC/DC100%1/1
$t->{"err"} // \@result
$t->{"err"}
Truth table100%2/2
A
A= $t->{"err"}
Aresult
11
00
160 19 ▶ if (!any {!defined} values %taskresults) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { not defined $_; } , values %taskresults)) 5 14 19
161 my @taskresults
162 = !$is_array ? %taskresults
163 5 ▶ : map { $taskresults{$_-1} } 1 .. keys %taskresults;
Branch: 1 branch
BranchTrueFalseTotal
!$is_array ? : 2 3 5
164 5 $d->done(@taskresults);
165 }
166 19 return $t->done();
167 },
168 43 );
169 43 ▶ $t->run( undef, @{ $taskparams{$key} || [] } );
Condition100%2/2
$taskparams{$key} || []
l!l
2023
MC/DC100%1/1
$taskparams{$key} || []
$taskparams{$key}
Truth table100%2/2
A
A= $taskparams{$key}
Aresult
11
00
170 }
171 19 };
172 }
173  
174 23 ▶sub if {
Subroutine if: called CC=3 SCAR=11.0
Pod documented
175 23 my ($this, $code) = @_;
176 23 ▶ if (!$code || ref $code ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code or ref $code ne "CODE") 1 22 23
Condition66%2/3
not $code or ref $code ne "CODE"
l!l&&r!l&&!r
1022
MC/DC50%1/2
not $code or ref $code ne "CODE"
not $code ref $code ne "CODE"
Truth table66%2/3
not(A) or B
A= not $codeB= ref $code ne "CODE"
ABresult
1X1
011
000
177 1 croak 'require CODE in first param';
178 }
179 22 return $this->_add(OP_IF, $code);
180 }
181  
182 15 ▶sub else {
Subroutine else: called CC=1 SCAR=0.0
Pod documented
183 15 my ($this) = @_;
184 15 return $this->_add(OP_ELSE);
185 }
186  
187 22 ▶sub end_if {
Subroutine end_if: called CC=1 SCAR=0.0
Pod documented
188 22 my ($this) = @_;
189 22 return $this->_add(OP_ENDIF);
190 }
191  
192 21 ▶sub while {
Subroutine while: called CC=3 SCAR=11.0
Pod documented
193 21 my ($this, $code) = @_;
194 21 ▶ if (!$code || ref $code ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code or ref $code ne "CODE") 1 20 21
Condition66%2/3
not $code or ref $code ne "CODE"
l!l&&r!l&&!r
1020
MC/DC50%1/2
not $code or ref $code ne "CODE"
not $code ref $code ne "CODE"
Truth table66%2/3
not(A) or B
A= not $codeB= ref $code ne "CODE"
ABresult
1X1
011
000
195 1 croak 'require CODE in first param';
196 }
197 20 return $this->_add(OP_WHILE, $code);
198 }
199  
200 19 ▶sub end_while {
Subroutine end_while: called CC=1 SCAR=0.0
Pod documented
201 19 my ($this) = @_;
202 19 return $this->_add(OP_ENDWHILE);
203 }
204  
205 87 ▶sub try {
Subroutine try: called CC=1 SCAR=0.0
Pod documented
206 87 my ($this) = @_;
207 87 return $this->_add(OP_TRY);
208 }
209  
210 91 ▶sub catch {
Subroutine catch: called CC=8 SCAR=20.8
Pod documented
211 91 my ($this, @param) = @_;
212 91 ▶ if (2 > @param) {
Branch: 2 branches
BranchTrueFalseTotal
if (2 > @param) { } 2 89 91
elsif (@param % 2) { } 1 88 89
213 2 croak 'require at least 2 params';
214 } elsif (@param % 2) {
215 1 croak 'require even number of params';
216 }
217  
218 88 my ($finally, @catch);
219 88 ▶ while (my ($cond, $code) = splice @param, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@param, 0, 2)) 146 88 234
220 146 ▶ if ($cond eq 'FINALLY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond eq 'FINALLY') { } 62 84 146
221 62 ▶ $finally ||= $code;
Condition66%2/3
$finally ||= $code
l!l&&r!l&&!r
1610
MC/DC0%0/2
$finally ||= $code
$finally $code
Truth table66%2/3
A ||= B
A= $finallyB= $code
ABresult
1X1
011
000
222 } else {
223 84 push @catch, $cond, $code;
224 }
225 }
226  
227 88 ▶ if (@catch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@catch) 80 8 88
228 80 $this->_add(OP_CATCH, @catch);
229 }
230 87 ▶ if ($finally) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally) 61 26 87
231 61 $this->_add(OP_FINALLY, $finally);
232 }
233 87 return $this->_add(OP_ENDTRY);
234 }
235  
236 244 ▶sub _check_stack {
Subroutine _check_stack: called CC=13 SCAR=25.7
237 244 my ($self) = @_;
238 244 my @stack;
239 244 my %op_open = (
240 OP_IF() => 'end_if()',
241 OP_WHILE() => 'end_while()',
242 OP_TRY() => 'catch()',
243 );
244 244 my %op_close = (
245 OP_ENDIF() => [ OP_IF, 'end_if()' ],
246 OP_ENDWHILE() => [ OP_WHILE, 'end_while()' ],
247 OP_ENDTRY() => [ OP_TRY, 'catch()' ],
248 );
249 244 my $extra = 0;
250 244 ▶ for (my $i = 0; $i < @{ $self->{opcode} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"opcode"};}) 1023 236 1259
251 1023 my ($op) = @{ $self->{opcode}[ $i ] };
252  
253 1023 ▶ if ($op == OP_CATCH || $op == OP_FINALLY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 9 or $op == 10) 188 835 1023
Condition100%3/3
$op == 9 or $op == 10
l!l&&r!l&&!r
10484835
MC/DC100%2/2
$op == 9 or $op == 10
$op == 9 $op == 10
Truth table100%3/3
A or B
A= $op == 9B= $op == 10
ABresult
1X1
011
000
254 188 $extra++;
255 }
256  
257 1023 ▶ if ($op_open{$op}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($op_open{$op}) { } 198 825 1023
elsif ($op_close{$op}) { } 192 633 825
elsif ($op == 4) { } 32 601 633
258 198 push @stack, [$op,0]; # second number is counter for seen OP_ELSE
259 }
260 elsif ($op_close{$op}) {
261 192 my ($close_op, $close_func) = @{ $op_close{$op} };
262 192 ▶ if (@stack && $stack[-1][0] == $close_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack and $stack[-1][0] == $close_op) { } 186 6 192
Condition100%3/3
@stack and $stack[-1][0] == $close_op
!ll&&!rl&&r
33186
MC/DC100%2/2
@stack and $stack[-1][0] == $close_op
@stack $stack[-1][0] == $close_op
Truth table100%3/3
A and B
A= @stackB= $stack[-1][0] == $close_op
ABresult
0X0
100
111
263 186 pop @stack;
264 } else {
265 6 croak 'unexpected '.$close_func.' at operation '.($i+1-$extra);
266 }
267 }
268 elsif ($op == OP_ELSE) {
269 32 ▶ if (!(@stack && $stack[-1][0] == OP_IF)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @stack && $stack[-1][0] == 3) { } 1 31 32
elsif ($stack[-1][1]) { } 1 30 31
Condition66%2/3
@stack && $stack[-1][0] == 3
!ll&&!rl&&r
1031
MC/DC50%1/2
@stack && $stack[-1][0] == 3
@stack $stack[-1][0] == 3
Truth table66%2/3
A && B
A= @stackB= $stack[-1][0] == 3
ABresult
0X0
100
111
270 1 croak 'unexpected else() at operation '.($i+1-$extra);
271 }
272 elsif ($stack[-1][1]) {
273 1 croak 'unexpected double else() at operation '.($i+1-$extra);
274 }
275 30 $stack[-1][1]++;
276 }
277 }
278 236 ▶ if (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 5 231 236
279 5 croak 'expected '.$op_open{ $stack[-1][0] }.' at end';
280 }
281 231 return;
282 }
283  
284 247 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
285 247 my ($this, $d, @result) = @_;
286 247 my $self = $SELF{refaddr $this};
287  
288 247 my %op_stmt = map {$_=>1} OP_CODE, OP_DEFER, OP_FINALLY;
289 247 ▶ if (!any {$op_stmt{ $_->[0] }} @{ $self->{opcode} }) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $op_stmt{$_->[0]}; } , @{$$self{"opcode"};})) 2 245 247
290 2 croak 'no operations to run, use do() first';
291 }
292 245 ▶ if ($self->{pc} != NOT_RUNNING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pc"} != -1) 1 244 245
293 1 croak 'already running';
294 }
295 244 _check_stack($self);
296  
297 231 ▶ if(ref($d) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq "CODE") 1 230 231
298 1 my $callback = $d;
299 1 $d = __PACKAGE__->new();
300 $d->do(
301 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
302 1 my ($defer, @results) = @_;
303 1 $callback->(@results);
304 1 $defer->done;
305 }
306 1 );
307 }
308  
309 231 $self->{parent} = $d;
310 231 $this->done(@result);
311 228 return;
312 }
313  
314 2201 ▶sub _op {
Subroutine _op: called CC=2 SCAR=6.9
315 2201 my ($self) = @_;
316 2201 my ($op, @params) = @{ $self->{opcode}[ $self->{pc} ] };
317 2201 ▶ return wantarray ? ($op, @params) : $op;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1105 1096 2201
318 }
319  
320 733 ▶sub done {
Subroutine done: called CC=20 SCAR=30.0
Pod documented
321 733 my ($this, @result) = @_;
322 733 my $self = $SELF{refaddr $this};
323  
324 # If OP_FINALLY was called while processing continue(), break() or throw(),
325 # and it has finished with done() - continue with continue/break/throw by
326 # calling them _again_ instead of done().
327 733 ▶ if ($self->{findone}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"findone"}) 23 710 733
328 23 my ($method, @param) = @{ $self->{findone} };
329 23 return $this->$method(@param);
330 }
331  
332 710 ▶ while (++$self->{pc} <= $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"pc"} <= $#{$$self{"opcode"};}) 921 161 1082
333 921 my ($opcode, @param) = _op($self);
334  
335 # @result received from previous opcode will be available to next
336 # opcode only if these opcodes stay one-after-one without any
337 # other opcodes between them (like OP_IF, for example).
338 # Only exception is (no-op) OP_TRY, OP_CATCH and OP_ENDTRY.
339 # This limitation should help user to avoid subtle bugs.
340 921 given ($opcode) {
341 921 when (OP_CODE) {
342 361 return $param[0]->($this, @result);
343 }
344 560 when (OP_DEFER) {
345 76 return $param[0]->run($this, @result);
346 }
347 484 when (OP_FINALLY) {
348 40 return $param[0]->($this, @result);
349 }
350 444 when ([OP_TRY,OP_CATCH,OP_ENDTRY]) {
351 215 next;
352 }
353 }
354 229 @result = ();
355  
356 229 given ($opcode) {
357 229 when (OP_IF) {
358 # true - do nothing (i.e. just move to next opcode)
359 # false - skip to nearest OP_ELSE or OP_ENDIF
360 52 ▶ if (!$param[0]->( $this )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param[0]->($this)) 28 24 52
361 28 my $stack = 0;
362 28 ▶ while (++$self->{pc} <= $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"pc"} <= $#{$$self{"opcode"};}) 117 0 117
363 117 my $op = _op($self);
364 117 ▶ $op == OP_ELSE && !$stack ? last
Branch: 4 branches
BranchTrueFalseTotal
$op == 4 && !$stack ? : 12 105 117
$op == 5 && !$stack ? : 16 89 105
$op == 3 ? : 3 86 89
$op == 5 ? : 3 83 86
Condition100%6/6
$op == 4 and not $stack
!ll&&!rl&&r
103212
$op == 5 and not $stack
!ll&&!rl&&r
86316
MC/DC100%2/2
$op == 4 and not $stack
$op == 4 not $stack
MC/DC100%2/2
$op == 5 and not $stack
$op == 5 not $stack
Truth table100%3/3
A and not(B)
A= $op == 4B= not $stack
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= $op == 5B= not $stack
ABresult
0X0
100
111
365 : $op == OP_ENDIF && !$stack ? last
366 : $op == OP_IF ? $stack++
367 : $op == OP_ENDIF ? $stack--
368 : next;
369 }
370 }
371 }
372 177 when (OP_ELSE) {
373 # skip this OP_ELSE branch to nearest OP_ENDIF
374 11 my $stack = 0;
375 11 ▶ while (++$self->{pc} <= $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"pc"} <= $#{$$self{"opcode"};}) 34 0 34
376 34 my $op = _op($self);
377 34 ▶ $op == OP_ENDIF && !$stack ? last
Branch: 3 branches
BranchTrueFalseTotal
$op == 5 && !$stack ? : 11 23 34
$op == 3 ? : 3 20 23
$op == 5 ? : 3 17 20
Condition100%3/3
$op == 5 and not $stack
!ll&&!rl&&r
20311
MC/DC100%2/2
$op == 5 and not $stack
$op == 5 not $stack
Truth table100%3/3
A and not(B)
A= $op == 5B= not $stack
ABresult
0X0
100
111
378 : $op == OP_IF ? $stack++
379 : $op == OP_ENDIF ? $stack--
380 : next;
381 }
382 }
383 166 when (OP_WHILE) {
384 # We can "enter" OP_WHILE in two cases - for the first time,
385 # OR because of continue() called inside this OP_WHILE.
386 98 ▶ if (!@{$self->{iter}} || $self->{iter}[-1][1] != $self->{pc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->{"iter"};} or $self->{"iter"}[-1][1] != $self->{"pc"}) 33 65 98
Condition100%3/3
not @{$self->{"iter"};} or $self->{"iter"}[-1][1] != $self->{"pc"}
l!l&&r!l&&!r
27665
MC/DC100%2/2
not @{$self->{"iter"};} or $self->{"iter"}[-1][1] != $self->{"pc"}
not @{$self->{"iter"};} $self->{"iter"}[-1][1] != $self->{"pc"}
Truth table100%3/3
not(A) or B
A= not @{$self->{"iter"};}B= $self->{"iter"}[-1][1] != $self->{"pc"}
ABresult
1X1
011
000
387 33 push @{ $self->{iter} }, [ 1, $self->{pc} ];
388 }
389 # We now already "inside" this OP_WHILE, so we can use break()
390 # to exit _this_ OP_WHILE.
391 98 ▶ if (!$param[0]->( $this )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param[0]->($this)) 23 75 98
392 23 return $this->break();
393 }
394 }
395 68 when (OP_ENDWHILE) {
396 # We now still "inside" current OP_WHILE, so we can use continue()
397 # to repeat _this_ OP_WHILE.
398 49 return $this->continue();
399 }
400 }
401 }
402  
403 161 $self->{pc} = NOT_RUNNING;
404 161 ▶ if ($self->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"}) 53 108 161
405 53 return $self->{parent}->done(@result);
406 }
407  
408 # If we're here, done() was called by last opcode, and this is
409 # top-level Defer object, nothing more to do - STOP.
410 }
411  
412 # Before executing continue/break logic we have to find and execute all
413 # OP_FINALLY for all already open OP_TRY blocks within this OP_WHILE.
414 # So, this helper skip opcodes inside this OP_WHILE until it found
415 # either OP_FINALLY or OP_ENDWHILE or last opcode.
416 132 ▶sub _skip_while {
Subroutine _skip_while: called CC=13 SCAR=25.7
417 132 my ($self) = @_;
418  
419 # 1. continue() can be called exactly on OP_ENDWHILE (by done())
420 # 2. continue/break can be called by last opcode
421 # In both cases we shouldn't do anything (including moving {pc} forward).
422 132 ▶ if (_op($self) == OP_ENDWHILE || $self->{pc} == $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (_op($self) == 7 or $self->{"pc"} == $#{$$self{"opcode"};}) 49 83 132
Condition66%2/3
_op($self) == 7 or $self->{"pc"} == $#{$$self{"opcode"};}
l!l&&r!l&&!r
49083
MC/DC50%1/2
_op($self) == 7 or $self->{"pc"} == $#{$$self{"opcode"};}
_op($self) == 7 $self->{"pc"} == $#{$$self{"opcode"};}
Truth table66%2/3
A or B
A= _op($self) == 7B= $self->{"pc"} == $#{$$self{"opcode"};}
ABresult
1X1
011
000
423 49 return;
424 }
425  
426 83 my $stack = 0;
427 83 my $trystack = 0;
428 83 ▶ while (++$self->{pc} < $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"pc"} < $#{$$self{"opcode"};}) 294 39 333
429 294 my $op = _op($self);
430 294 ▶ $op == OP_ENDWHILE && !$stack ? last
Branch: 6 branches
BranchTrueFalseTotal
$op == 7 && !$stack ? : 10 284 294
$op == 6 ? : 2 282 284
$op == 7 ? : 2 280 282
$op == 8 ? : 17 263 280
$op == 11 && $trystack ? : 17 246 263
$op == 10 && !$trystack ? : 34 212 246
Condition100%9/9
$op == 7 and not $stack
!ll&&!rl&&r
282210
$op == 11 and $trystack
!ll&&!rl&&r
2123417
$op == 10 and not $trystack
!ll&&!rl&&r
1961634
MC/DC100%2/2
$op == 7 and not $stack
$op == 7 not $stack
MC/DC100%2/2
$op == 11 and $trystack
$op == 11 $trystack
MC/DC100%2/2
$op == 10 and not $trystack
$op == 10 not $trystack
Truth table100%3/3
A and not(B)
A= $op == 7B= not $stack
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $op == 11B= $trystack
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= $op == 10B= not $trystack
ABresult
0X0
100
111
431 : $op == OP_WHILE ? $stack++
432 : $op == OP_ENDWHILE ? $stack--
433 : $op == OP_TRY ? $trystack++
434 : $op == OP_ENDTRY && $trystack ? $trystack--
435 : $op == OP_FINALLY && !$trystack ? last
436 : next;
437 }
438  
439 83 return;
440 }
441  
442 88 ▶sub continue {
Subroutine continue: called CC=8 SCAR=20.8
Pod documented
443 88 my ($this) = @_;
444 88 my $self = $SELF{refaddr $this};
445  
446 # Any next call to continue/break/throw cancels current continue/break/throw (if any).
447 88 $self->{findone} = undef;
448  
449 88 _skip_while($self);
450 88 my ($op, @param) = _op($self);
451 88 ▶ if ($op == OP_FINALLY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 10) 21 67 88
452 # If OP_FINALLY ends with done() - call continue() again instead.
453 21 $self->{findone} = ['continue'];
454 21 return $param[0]->($this);
455 }
456  
457 # We now at OP_ENDWHILE, rewind to corresponding OP_WHILE.
458 67 my $stack = 0;
459 67 ▶ while (--$self->{pc} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"pc"} > 0) 444 60 504
460 444 $op = _op($self);
461 444 ▶ $op == OP_WHILE && !$stack ? last
Branch: 3 branches
BranchTrueFalseTotal
$op == 6 && !$stack ? : 7 437 444
$op == 7 ? : 6 431 437
$op == 6 ? : 6 425 431
Condition100%3/3
$op == 6 and not $stack
!ll&&!rl&&r
43167
MC/DC100%2/2
$op == 6 and not $stack
$op == 6 not $stack
Truth table100%3/3
A and not(B)
A= $op == 6B= not $stack
ABresult
0X0
100
111
462 : $op == OP_ENDWHILE ? $stack++
463 : $op == OP_WHILE ? $stack--
464 : next;
465 }
466  
467 # If continue was called outside OP_WHILE there is no iteration number.
468 67 ▶ if (@{ $self->{iter} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"iter"};}) 65 2 67
469 65 $self->{iter}[-1][0]++;
470 }
471  
472 # Step one opcode back because done() will move one opcode forward
473 # and so process this OP_WHILE.
474 67 --$self->{pc};
475 67 return $this->done();
476 }
477  
478 44 ▶sub break {
Subroutine break: called CC=2 SCAR=6.9
Pod documented
479 44 my ($this) = @_;
480 44 my $self = $SELF{refaddr $this};
481  
482 # Any next call to continue/break/throw cancels current continue/break/throw (if any).
483 44 $self->{findone} = undef;
484  
485 44 _skip_while($self);
486 44 my ($op, @param) = _op($self);
487 44 ▶ if ($op == OP_FINALLY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 10) 13 31 44
488 # If OP_FINALLY ends with done() - call break() again instead.
489 13 $self->{findone} = ['break'];
490 13 return $param[0]->($this);
491 }
492  
493 # We now at OP_ENDWHILE.
494 31 pop @{ $self->{iter} };
495 31 return $this->done();
496 }
497  
498 58 ▶sub throw {
Subroutine throw: called CC=19 SCAR=29.5
Pod documented
499 58 my ($this, $err) = @_;
500 58 my $self = $SELF{refaddr $this};
501 58 ▶ $err //= q{};
Condition100%2/2
$err //= ""
l!l
4315
MC/DC100%1/1
$err //= ""
$err
Truth table100%2/2
A
A= $err
Aresult
11
00
502  
503 # Any next call to continue/break/throw cancels current continue/break/throw (if any).
504 58 $self->{findone} = undef;
505  
506 # If throw() was called by break opcode in this OP_TRY (either OP_FINALLY,
507 # or OP_CATCH if there no OP_FINALLY in this OP_TRY), then we should look
508 # for handler in outer OP_TRY, not in this one.
509 # So we set $stack=1 to skip over current OP_TRY's OP_ENDTRY.
510 58 ▶ my ($nextop) = @{ $self->{opcode}[ $self->{pc} + 1 ] || [] };
Condition100%2/2
$self->{'opcode'}[$self->{'pc'} + 1] || []
l!l
544
MC/DC100%1/1
$self->{'opcode'}[$self->{'pc'} + 1] || []
$self->{'opcode'}[$self->{'pc'} + 1]
Truth table100%2/2
A
A= $self->{'opcode'}[$self->{'pc'} + 1]
Aresult
11
00
511 58 ▶ my $stack = $nextop && $nextop == OP_ENDTRY ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nextop && $nextop == 11 ? : 14 44 58
Condition100%3/3
$nextop and $nextop == 11
!ll&&!rl&&r
44014
MC/DC100%2/2
$nextop and $nextop == 11
$nextop $nextop == 11
Truth table100%3/3
A and B
A= $nextopB= $nextop == 11
ABresult
0X0
100
111
512 # Skip until OP_CATCH or OP_FINALLY in current OP_TRY block.
513 # If while skipping we exit some OP_WHILE(s) - pop their iterators.
514 58 ▶ while (++$self->{pc} <= $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"pc"} <= $#{$$self{"opcode"};}) 75 6 81
515 75 my $op = _op($self);
516 $op == OP_CATCH && !$stack ? last
517 : $op == OP_FINALLY && !$stack ? last
518 : $op == OP_TRY ? $stack++
519 : $op == OP_ENDTRY ? $stack--
520 0 ▶ : $op == OP_WHILE ? push @{ $self->{iter} }, [ 1, $self->{pc} ]
Branch: 1 branch
BranchTrueFalseTotal
$op == 7 ? : 3 4 7
521 75 ▶ : $op == OP_ENDWHILE ? pop @{ $self->{iter} }
Branch: 5 branches
BranchTrueFalseTotal
$op == 9 && !$stack ? : 44 31 75
$op == 10 && !$stack ? : 8 23 31
$op == 8 ? : 1 22 23
$op == 11 ? : 15 7 22
$op == 6 ? : 0 7 7
Condition83%5/6
$op == 9 and not $stack
!ll&&!rl&&r
30144
$op == 10 and not $stack
!ll&&!rl&&r
2308
MC/DC100%2/2
$op == 9 and not $stack
$op == 9 not $stack
MC/DC50%1/2
$op == 10 and not $stack
$op == 10 not $stack
Truth table100%3/3
A and not(B)
A= $op == 9B= not $stack
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= $op == 10B= not $stack
ABresult
0X0
100
111
522 : next;
523 }
524  
525 58 ▶ if ($self->{pc} > $#{ $self->{opcode} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pc"} > $#{$$self{"opcode"};}) 6 52 58
526 6 ▶ if ($self->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parent'}) { } 4 2 6
527 4 return $self->{parent}->throw($err);
528 } else {
529 2 croak 'uncatched exception in Defer: '.$err;
530 }
531 }
532  
533 52 my ($op, @param) = _op($self);
534 52 ▶ if ($op == OP_CATCH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 9) { } 44 8 52
535 44 ▶ while (my ($cond, $code) = splice @param, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@param, 0, 2)) 49 5 54
536 49 ▶ if ($err =~ /$cond/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /$cond/msu) 39 10 49
537 39 return $code->($this, $err);
538 }
539 }
540 # Re-throw exception if no one regex in this OP_CATCH match it.
541 5 return $this->throw($err);
542 }
543 else { # OP_FINALLY
544 # If OP_FINALLY ends with done() - call throw($err) again instead.
545 8 $self->{findone} = ['throw', $err];
546 8 return $param[0]->($this, $err);
547 }
548 }
549  
550  
551 17 1; # Magic true value required at end of module
552 __END__
↑ Summary