| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2013-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Devel::MAT::Context 0.56; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 10 | ▶ | use v5.20; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 10 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 10 | ▶ | use feature qw( postderef signatures ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 10 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 10 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 10 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | C<Devel::MAT::Context> - represent a single call context state | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | Objects in this class represent a single level of state from the call context. | ||||||||||||||||||||
| 24 | These contexts represent function calls between perl functions. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 10 | my %types; | |||||||||||||||||||
| 29 | 30 | ▶ | sub register_type ( $pkg, $id ) | ||||||||||||||||||
|
Subroutine
register_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 30 | { | ||||||||||||||||||||
| 31 | 30 | $types{$id} = $pkg; | |||||||||||||||||||
| 32 | # generate the ->type constant method | ||||||||||||||||||||
| 33 | 30 | ( my $typename = $pkg ) =~ s/^Devel::MAT::Context:://; | |||||||||||||||||||
| 34 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 30 | ▶ | *{"${pkg}::type"} = sub (@) { $typename }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 5 | ▶ | sub new ( $class, $type, $df, $bytes, $, $strs ) | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 39 | { | ||||||||||||||||||||
| 40 | 5 | ▶ | $types{$type} or croak "Cannot load unknown CTX type $type"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 5 | my $self = bless {}, $types{$type}; | |||||||||||||||||||
| 43 | 5 | weaken( $self->{df} = $df ); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | ( $self->{gimme}, $self->{line} ) = unpack "C $df->{uint_fmt}", $bytes; | |||||||||||||||||||
| 46 | 5 | ( $self->{file} ) = @$strs; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 5 | return $self; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub load_v0_1 ( $class, $type, $df ) | ||||||||||||||||||
|
Subroutine
load_v0_1: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 52 | { | ||||||||||||||||||||
| 53 | 0 | ▶ | $types{$type} or croak "Cannot load unknown CTX type $type"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | my $self = bless {}, $types{$type}; | |||||||||||||||||||
| 56 | 0 | weaken( $self->{df} = $df ); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Standard fields all Contexts have | ||||||||||||||||||||
| 59 | 0 | $self->{gimme} = $df->_read_u8; | |||||||||||||||||||
| 60 | 0 | $self->{file} = $df->_read_str; | |||||||||||||||||||
| 61 | 0 | $self->{line} = $df->_read_uint; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | $self->_load_v0_1( $df ); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | return $self; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =head1 COMMON METHODS | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =for highlighter language=perl | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head2 gimme | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | $gimme = $ctx->gimme; | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Returns the gimme value of the call context. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 10 | my @GIMMES = ( undef, qw( void scalar array ) ); | |||||||||||||||||||
| 83 | 0 | ▶ | sub gimme ( $self ) | ||||||||||||||||||
|
Subroutine
gimme: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 84 | { | ||||||||||||||||||||
| 85 | 0 | return $GIMMES[ $self->{gimme} ]; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head2 file | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head2 line | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head2 location | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | $file = $ctx->file; | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | $line = $ctx->line; | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | $location = $ctx->location; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Returns the file, line or location as (C<FILE line LINE>). | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 5 | ▶ | sub file ( $self ) { return $self->{file} } | ||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 105 | 5 | ▶ | sub line ( $self ) { return $self->{line} } | ||||||||||||||||||
|
Subroutine
line: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub location ( $self ) | ||||||||||||||||||
|
Subroutine
location: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 108 | { | ||||||||||||||||||||
| 109 | 0 | return "$self->{file} line $self->{line}"; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | package Devel::MAT::Context::SUB 0.56; | ||||||||||||||||||||
| 113 | 10 | ▶ | use base qw( Devel::MAT::Context ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 114 | 10 | __PACKAGE__->register_type( 1 ); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 Devel::MAT::Context::SUB | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | Represents a context which is a subroutine call. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 3 | ▶ | sub load ( $self, $bytes, $ptrs, $ ) | ||||||||||||||||||
|
Subroutine
load: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 123 | { | ||||||||||||||||||||
| 124 | 3 | my $df = $self->{df}; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 3 | ( $self->{olddepth} ) = unpack "$df->{u32_fmt}", $bytes; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 3 | ( $self->{cv_at}, $self->{args_at} ) = @$ptrs; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 3 | ▶ | undef $self->{args_at} if $df->perlversion ge "5.23.8"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | ▶ | sub _load_v0_1 ( $self, $df ) | ||||||||||||||||||
|
Subroutine
_load_v0_1: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 134 | { | ||||||||||||||||||||
| 135 | 0 | $self->{olddepth} = -1; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | $self->{cv_at} = $df->_read_ptr; | |||||||||||||||||||
| 138 | 0 | $self->{args_at} = $df->_read_ptr; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | undef $self->{args_at} if $df->perlversion ge "5.23.8"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head2 cv | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | $cv = $ctx->cv; | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Returns the CV which this call is to. | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head2 args | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | $args = $ctx->args; | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Returns the arguments AV which represents the C<@_> argument array. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 olddepth | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | $olddepth = $ctx->olddepth; | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | Returns the old depth of the context (that is, the depth the CV would be at | ||||||||||||||||||||
| 160 | after this context returns). | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =head2 depth | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | $depth = $ctx->depth; | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | Returns the actual depth of the context. This is inferred at load time by | ||||||||||||||||||||
| 167 | considering the C<olddepth> of the next inner-nested call to the same CV, or | ||||||||||||||||||||
| 168 | from the actual C<depth> of the CV is no other call exists. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 8 | ▶ | sub cv ( $self ) { return $self->{df}->sv_at( $self->{cv_at} ) } | ||||||||||||||||||
|
Subroutine
cv: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 2 | ▶ | sub args ( $self ) | ||||||||||||||||||
|
Subroutine
args: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 175 | { | ||||||||||||||||||||
| 176 | # Perl 5.23.8 removed blk_sub.argarray so we have to go the long way round | ||||||||||||||||||||
| 177 | 2 | ▶ | $self->{args_at} //= do { | ||||||||||||||||||
|
Condition33%1/3
$self->{"args_at"} //= do {
my $cv = $self->cv;
my $args = $cv->pad($self->depth)->elem(0);
$args->addr
}
MC/DC0%0/2
$self->{"args_at"} //= do {
my $cv = $self->cv;
my $args = $cv->pad($self->depth)->elem(0);
$args->addr
}
$self->{"args_at"}
do {
my $cv = $self->cv;
my $args = $cv->pad($self->depth)->elem(0);
$args->addr
}
Truth table33%1/3
A //= B
A= $self->{"args_at"}B= do {
my $cv = $self->cv;
my $args = $cv->pad($self->depth)->elem(0);
$args->addr
}
| |||||||||||||||||||||
| 178 | 2 | my $cv = $self->cv; | |||||||||||||||||||
| 179 | 2 | my $args = $cv->pad( $self->depth )->elem( 0 ); | |||||||||||||||||||
| 180 | 2 | $args->addr; | |||||||||||||||||||
| 181 | }; | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 2 | return $self->{df}->sv_at( $self->{args_at} ); | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 4 | ▶ | sub olddepth ( $self ) { return $self->{olddepth} } | ||||||||||||||||||
|
Subroutine
olddepth: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 3 | ▶ | sub _set_depth ( $self, $depth ) { $self->{depth} = $depth } | ||||||||||||||||||
|
Subroutine
_set_depth: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 189 | 3 | ▶ | sub depth ( $self ) { return $self->{depth} } | ||||||||||||||||||
|
Subroutine
depth: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | package Devel::MAT::Context::TRY 0.56; | ||||||||||||||||||||
| 192 | 10 | ▶ | use base qw( Devel::MAT::Context ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 193 | 10 | __PACKAGE__->register_type( 2 ); | |||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head1 Devel::MAT::Context::TRY | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Represents a context which is a block C<eval {}> call. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | sub load ( $self, $, $, $ ) {} | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | sub _load_v0_1 ( $self, $ ) {} | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | package Devel::MAT::Context::EVAL 0.56; | ||||||||||||||||||||
| 206 | 10 | ▶ | use base qw( Devel::MAT::Context ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 207 | 10 | __PACKAGE__->register_type( 3 ); | |||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head1 Devel::MAT::Context::EVAL | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Represents a context which is a string C<eval EXPR> call. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 1 | ▶ | sub load ( $self, $, $ptrs, $ ) | ||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 216 | { | ||||||||||||||||||||
| 217 | 1 | ( $self->{code_at} ) = @$ptrs; | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 0 | ▶ | sub _load_v0_1 ( $self, $df ) | ||||||||||||||||||
|
Subroutine
_load_v0_1: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 221 | { | ||||||||||||||||||||
| 222 | 0 | $self->{code_at} = $df->_read_ptr; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 code | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | $sv = $ctx->code; | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | Returns the SV containing the text string being evaluated. | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 1 | ▶ | sub code ( $self ) { return $self->{df}->sv_at( $self->{code_at} ) } | ||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head1 AUTHOR | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 10 | 0x55AA; | |||||||||||||||||||