lib/FP/Repl/Stack.pm

filter: Statementstmt 38.2% 49 / 128 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 14 / 28 Podpod 0.0% 0 / 5 totaltotal 28.5% 63 / 221 CC 25 SCAR 28.0
CC
25
Cov
27%
args_text
42.0
__ANON__
42.0
equal
30.0
CRAP 16.4
« lib/Chj/xIOUtil.pm ↑ Summary lib/Chj/xopen.pm »
1 #
2 # Copyright (c) 2015-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Repl::Stack
13  
14 =head1 SYNOPSIS
15  
16 my $stack = FP::Repl::Stack->get($numbers_of_levels_to_skip);
17 my $f = $stack->frame($frameno);
18 $f->package
19 $f->args
20 ...
21 # or:
22 $stack->package($frameno)
23 $stack->args($frameno)
24 ...
25  
26 # the whole thing as string:
27 $stack->backtrace
28  
29 =head1 DESCRIPTION
30  
31 I'm pretty sure this is re-inventing some wheel..
32  
33 =head1 SEE ALSO
34  
35 Implements: L<FP::Abstract::Show>, L<FP::Abstract::Pure>
36  
37 L<FP::Repl::StackPlus>
38  
39 =head1 NOTE
40  
41 This is alpha software! Read the status section in the package README
42 or on the L<website|http://functional-perl.org/>.
43  
44 =cut
45  
46 package FP::Repl::Stack;
47  
48 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 our @fields;
53  
54 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 @fields = qw(args
56 package filename line subroutine hasargs
57 wantarray evaltext is_require hints bitmask
58 hinthash)
59 }
60  
61 {
62  
63 2 package FP::Repl::StackFrame;
64 2 ▶ use Chj::TerseDumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶ use FP::Div "Chomp";
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶ use Chj::singlequote qw(singlequote_many with_maxlen);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶ use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 2 ▶ use FP::Struct [@fields], 'FP::Struct::Show', 'FP::Abstract::Pure';
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 0 ▶ sub args_text {
Subroutine args_text: not called CC=6 SCAR=37.4
72 0 my $s = shift;
73 0 my ($indent, $mode) = @_;
74 0 my $args = $s->args;
75 my $str = join ",\n", map {
76 0 ▶ if ($mode eq "d") {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'd') { } 0 0 0
elsif ($mode eq 's' or $mode eq 'S') { } 0 0 0
elsif ($mode eq 'p') { } 0 0 0
77  
78 # XX reinvention forever, too: how to *shorten*-dump a
79 # value? Data::Dumper does not seem to support it?
80 0 local $Data::Dumper::Maxdepth = 1;
81  
82 # ^ ok helps a bit sometimes. XX But will now be
83 # confusing, as there's no way to know references from
84 # (accidentally) stringified references
85 0 Chomp(TerseDumper($_))
86 ▶ } elsif (
Condition0%0/3
$mode eq "s" or $mode eq "S"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "s" or $mode eq "S"
$mode eq "s" $mode eq "S"
Truth table0%0/3
A or B
A= $mode eq "s"B= $mode eq "S"
ABresult
1X1
011
000
87 $mode eq "s" or $mode eq "S" # no point using perltidy here, OK?
88 )
89 {
90 0 show($_)
91 } elsif ($mode eq "p") {
92 0 "$_"
93 } else {
94 0 die "unknown mode '$mode'";
95 }
96 } @$args;
97 0 ▶ $str = "\n$str" if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
98 0 $str =~ s/\n/\n$indent/g; # XX there's also $Data::Dumper::Pad
99 0 $str
100 }
101  
102 0 ▶ sub desc {
Subroutine desc: not called CC=1 SCAR=6.9
103 0 my ($s, $mode) = @_;
104 0 ( $s->subroutine . "("
105 . $s->args_text(" ", $mode)
106 . "\n) called at "
107 . $s->filename
108 . " line "
109 . $s->line)
110 }
111  
112 # one-line-desc
113 0 ▶ sub oneline {
Subroutine oneline: not called CC=2 SCAR=17.9
114 0 my $s = shift;
115 0 my ($maybe_prefix) = @_;
116 (
117 ($maybe_prefix // "\t") #parens needed!
118 . $s->subroutine . "("
119 0 ▶ . with_maxlen(64, sub { singlequote_many(@{ $s->args }) }) . ")"
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$maybe_prefix // "\t"
l!l
00
MC/DC0%0/1
$maybe_prefix // "\t"
$maybe_prefix
Truth table0%0/2
A
A= $maybe_prefix
Aresult
11
00
120 . " called at "
121 . $s->filename
122 . " line "
123 . $s->line . "\n"
124 )
125 }
126  
127 # CAREFUL: equal stackframes still don't need to be the *same*
128 # stackframe!
129 0 ▶ sub equal {
Subroutine equal: not called CC=5 SCAR=34.0
130 0 my $s = shift;
131 0 my ($v) = @_;
132 0 ▶ my $equal_standard_fields = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
133 0 ▶ my $eq = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
134 0 my ($m) = @_;
135  
136 #$s->$m eq $v->$m
137 0 my $S = $s->$m;
138 0 my $V = $v->$m;
139 0 ▶ if (defined $S) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $S) { } 0 0 0
140 0 ▶ if (defined $V) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $V) { } 0 0 0
141 0 $S eq $V
142 } else {
143 0 0
144 }
145 } else {
146 0 ▶ if (defined $V) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $V) { } 0 0 0
147 0 0
148 } else {
149 0 1 # both undefined
150 }
151 }
152 0 };
153 (
154 0 ▶ &$eq("package")
Branch: 1 branch
BranchTrueFalseTotal
&$eq("package") and &$eq("filename") and &$eq("line") and &$eq("subroutine") and &$eq("hasargs") and &$eq("wantarray") 0 0 0
Condition0%0/12
&$eq("package") and &$eq("filename") and &$eq("line") and &$eq("subroutine") and &$eq("hasargs")
!ll&&!rl&&r
000
&$eq("package") and &$eq("filename") and &$eq("line") and &$eq("subroutine")
!ll&&!rl&&r
000
&$eq("package") and &$eq("filename") and &$eq("line")
!ll&&!rl&&r
000
&$eq("package") and &$eq("filename")
!ll&&!rl&&r
000
MC/DC0%0/5
&$eq("package") and &$eq("filename") and &$eq("line") and &$eq("subroutine") and &$eq("hasargs")
&$eq("package") &$eq("filename") &$eq("line") &$eq("subroutine") &$eq("hasargs")
Truth table0%0/6
A and B and C and D and E
A= &$eq("package")B= &$eq("filename")C= &$eq("line")D= &$eq("subroutine")E= &$eq("hasargs")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
155 and &$eq("filename")
156 and &$eq("line")
157 and &$eq("subroutine")
158 and &$eq("hasargs")
159 and &$eq("wantarray")
160  
161 #and
162 #&$eq ("")
163 # hints bitmask hinthash ?
164 )
165 0 };
166 0 ▶ if (defined $v->args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v->args) { } 0 0 0
167 0 ▶ if (defined $s->args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->args) { } 0 0 0
168 (
169 0 ▶ &$equal_standard_fields and do {
Branch: 1 branch
BranchTrueFalseTotal
&$equal_standard_fields and do { require FP::DumperEqual; FP::DumperEqual::dumperequal($v->args, $s->args) } 0 0 0
170 0 require FP::DumperEqual;
171 0 FP::DumperEqual::dumperequal($v->args, $s->args)
172 }
173 )
174 } else {
175 0 ''
176 }
177 } else {
178 0 ▶ if (defined $s->args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->args) { } 0 0 0
179 0 ''
180 } else {
181 0 &$equal_standard_fields
182 }
183 }
184 }
185  
186 2 _END_
187 }
188  
189 27 ▶our $make_frame_accessor = sub {
Subroutine __ANON__: called CC=1 SCAR=2.9
190 27 my ($method) = @_;
191 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
192 0 my $s = shift;
193 0 my ($frameno, @rest) = @_;
194 0 my $nf = $s->num_frames;
195 0 ▶ $frameno < $nf
Branch: 1 branch
BranchTrueFalseTotal
$frameno < $nf or die 'frame number must be between 0..' . ($nf - 1) . ", got: $frameno" 0 0 0
196 or die "frame number must be between 0.."
197 . ($nf - 1)
198 . ", got: $frameno";
199 0 $s->frames->[$frameno]->$method(@rest)
200 }
201 27 };
202  
203 27 ▶our $make_perhaps_frame_accessor = sub {
Subroutine __ANON__: called CC=1 SCAR=2.6
204 27 my ($method) = @_;
205 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
206 0 my $s = shift;
207 0 my ($frameno, @rest) = @_;
208 0 my $nf = $s->num_frames;
209 0 ▶ ($frameno < $nf ? ($s->frames->[$frameno]->$method(@rest)) : ())
Branch: 1 branch
BranchTrueFalseTotal
$frameno < $nf ? : 0 0 0
210 }
211 27 };
212  
213 2 ▶use FP::Struct ["frames"], 'FP::Struct::Show', 'FP::Abstract::Pure';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 2 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
217 0 my $class = shift;
218 0 my ($skip) = @_;
219  
220 package DB;
221 0 my @frames;
222 0 ▶ while (my @vals = caller($skip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@vals) = caller $skip) 0 0 0
223 0 my $subargs = [@DB::args];
224  
225 # XX how to handle this?: "@DB::args might have
226 # information from the previous time "caller" was
227 # called" (perlfunc on 'caller')
228 0 push @frames, FP::Repl::StackFrame->new($subargs, @vals);
229 0 $skip++;
230 }
231 0 $class->new(\@frames);
232 }
233  
234 0 ▶sub frame {
Subroutine frame: not called CC=2 SCAR=17.9
Pod undocumented
235 0 my $s = shift;
236 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
237 0 my ($i) = @_;
238 0 $s->frames->[$i]
239 }
240  
241 0 ▶sub num_frames {
Subroutine num_frames: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my $s = shift;
243 0 scalar @{ $s->frames }
244 }
245  
246 0 ▶sub max_frameno {
Subroutine max_frameno: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my $s = shift;
248 0 $#{ $s->frames }
249 }
250  
251 2 for (@fields, "desc") {
252 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
253 26 *{$_} = &$make_frame_accessor($_);
254 26 *{"perhaps_$_"} = &$make_perhaps_frame_accessor($_);
255 }
256  
257 0 ▶sub backtrace {
Subroutine backtrace: not called CC=3 SCAR=24.8
Pod undocumented
258 0 my $s = shift;
259 0 my ($maybe_skip) = @_;
260 0 ▶ my $skip = $maybe_skip // 0;
Condition0%0/2
$maybe_skip // 0
l!l
00
MC/DC0%0/1
$maybe_skip // 0
$maybe_skip
Truth table0%0/2
A
A= $maybe_skip
Aresult
11
00
261 0 my $fs = $s->frames;
262 0 my @f;
263 0 for my $i ($skip .. $#$fs) {
264 0 push @f, $$fs[$i]->oneline("$i\t")
265 }
266 0 join("", @f)
267 }
268  
269 2 _END_
« lib/Chj/xIOUtil.pm ↑ Summary lib/Chj/xopen.pm »