lib/clown.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 63.8% 23 / 36 Conditioncond 51.4% 18 / 35 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 2 totaltotal 69.4% 125 / 180 CC 31 SCAR 31.0
CC
31
Cov
74%
MODIFY_CODE_ATTRIBUTES
38.7
tweak
6.8
import
4.1
CRAP 22.3
« blib/lib/sealed.pm ↑ Summary blib/lib/ModPerl/RegistryCookerSealed.pm »
1 # SPDX License Identifier: Apache License 2.0
2 #
3 # provides ithread-safe :Sealed subroutine attributes: use with care!
4 #
5 # Author: Joe Schaefer <joe@sunstarsys.com>
6  
7 package clown;
8 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use B::Generate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use B::Deparse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION;
15 1 our $DEBUG;
16  
17 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 our $VERSION = qv(1.0.4);
19 }
20  
21 1 my %valid_attrs = (clown => 1);
22  
23  
24 2 ▶sub tweak :prototype($\@\@\@$$\%) {
Subroutine tweak: called CC=6 SCAR=19.2
Pod undocumented
25 2 my ($op, $lexical_varnames, $pads, $op_stack, $cv_obj, $pad_names, $processed_op) = @_;
26 2 my ($idx, $sub_name);
27  
28 2 ▶ if (ref($op) eq "B::PADOP") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $op eq 'B::PADOP') { } 0 2 2
29  
30 0 my $padix = $op->padix;
31 # A little prayer
32 # Not sure if this works better pre-ithread cloning, or post-ithread cloning.
33 # I've only used it post-ithread cloning, so YMMV.
34 # $targ collisions? ordering is a WAG with the @op_stack walker down below.
35  
36 0 ▶ $sub_name = substr $$pads[$idx++][$padix], 1 until defined $sub_name;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub_name or do { $sub_name = substr($$pads[$idx++][$padix], 1) } 0 0 0
37 }
38 else {
39 2 $sub_name = substr ${$op->sv->object_2svref}, 1;
40 }
41  
42 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶ die "\&$sub_name does not exist" unless defined *$sub_name and defined *$sub_name{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *$sub_name and defined *$sub_name{"CODE"} 1 1 2
Condition66%2/3
defined *$sub_name and defined *$sub_name{"CODE"}
!ll&&!rl&&r
011
MC/DC50%1/2
defined *$sub_name and defined *$sub_name{"CODE"}
defined *$sub_name defined *$sub_name{"CODE"}
Truth table66%2/3
A and B
A= defined *$sub_nameB= defined *$sub_name{"CODE"}
ABresult
0X0
100
111
44 1 $op = $op->next;
45 1 ▶ push @$op_stack, $op if $$op;
Branch: 1 branch
BranchTrueFalseTotal
if $$op 1 0 1
46 1 return ($op, 1);
47 }
48  
49 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=0.6
Pod undocumented
50 0 my $pkg = caller;
51 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 # can segfault
53 0 eval "BEGIN {
54 while (my (undef, \$v) = each %{$pkg\::}) {
55 eval 'ref(\$v) eq q(CODE)' or next;
56 MODIFY_CODE_ATTRIBUTES(\$pkg, \$v, \"clown\");
57 }
58 }";
59 }
60  
61 2 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=23 SCAR=36.6
62 2 my ($class, $rv, @attrs) = @_;
63 2 local $@;
64  
65 2 ▶ if ((not defined $DEBUG or $DEBUG ne "disabled") and grep $valid_attrs{+lc}, @attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($clown::DEBUG) || $clown::DEBUG ne "disabled" and grep $valid_attrs{lc $_}, @attrs) 2 0 2
Condition33%2/6
!defined($clown::DEBUG) || $clown::DEBUG ne "disabled" and grep $valid_attrs{lc $_}, @attrs
!ll&&!rl&&r
002
not defined $clown::DEBUG or $clown::DEBUG ne "disabled"
l!l&&r!l&&!r
020
MC/DC0%0/3
!defined($clown::DEBUG) || $clown::DEBUG ne "disabled" and grep $valid_attrs{lc $_}, @attrs
not defined $clown::DEBUG $clown::DEBUG ne "disabled" grep $valid_attrs{lc $_}, @attrs
Truth table20%1/5
not(A) or B and C
A= not defined $clown::DEBUGB= $clown::DEBUG ne "disabled"C= grep $valid_attrs{lc $_}, @attrs
ABCresult
00X0
1X00
0100
1X11
0111
66  
67 2 my $cv_obj = B::svref_2object($rv);
68 2 my @op_stack = $cv_obj->START;
69 2 my ($pad_names, @p) = $cv_obj->PADLIST->ARRAY;
70 2 my @pads = map $_->object_2svref, @p;
71 2 my @lexical_varnames = $pad_names->ARRAY;
72 2 my %processed_op;
73 my $tweaked;
74  
75 2 ▶ while (my $op = shift @op_stack and not defined $^S) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @op_stack and not defined $^S) 23 1 24
Condition66%2/3
my $op = shift @op_stack and not defined $^S
!ll&&!rl&&r
1023
MC/DC50%1/2
my $op = shift @op_stack and not defined $^S
my $op = shift @op_stack not defined $^S
Truth table66%2/3
A and not(B)
A= my $op = shift @op_stackB= not defined $^S
ABresult
0X0
100
111
76 23 ▶ ref $op and $$op and not $processed_op{$$op}++
Branch: 1 branch
BranchTrueFalseTotal
ref $op and $$op and not $processed_op{$$op}++ or next 11 12 23
Condition83%5/6
ref $op and $$op and not $processed_op{$$op}++
!ll&&!rl&&r
11111
ref $op and $$op
!ll&&!rl&&r
0122
MC/DC66%2/3
ref $op and $$op and not $processed_op{$$op}++
ref $op $$op not $processed_op{$$op}++
Truth table75%3/4
A and B and not(C)
A= ref $opB= $$opC= not $processed_op{$$op}++
ABCresult
0XX0
10X0
1100
1111
77 or next;
78  
79 11 ▶ $op->dump if defined $DEBUG and $DEBUG eq 'dump';
Branch: 1 branch
BranchTrueFalseTotal
if defined $clown::DEBUG and $clown::DEBUG eq "dump" 11 0 11
Condition33%1/3
defined $clown::DEBUG and $clown::DEBUG eq "dump"
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $clown::DEBUG and $clown::DEBUG eq "dump"
defined $clown::DEBUG $clown::DEBUG eq "dump"
Truth table33%1/3
A and B
A= defined $clown::DEBUGB= $clown::DEBUG eq "dump"
ABresult
0X0
100
111
80  
81 11 ▶ if ($op->isa("B::PADOP") or $op->isa("B::SVOP")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->isa("B::PADOP") or $op->isa("B::SVOP")) 2 9 11
Condition66%2/3
$op->isa("B::PADOP") or $op->isa("B::SVOP")
l!l&&r!l&&!r
029
MC/DC50%1/2
$op->isa("B::PADOP") or $op->isa("B::SVOP")
$op->isa("B::PADOP") $op->isa("B::SVOP")
Truth table66%2/3
A or B
A= $op->isa("B::PADOP")B= $op->isa("B::SVOP")
ABresult
1X1
011
000
82 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 $tweaked += eval {tweak $op, @lexical_varnames, @pads, @op_stack,
84 $cv_obj, $pad_names, %processed_op};
85 2 ▶ die __PACKAGE__ . ": tweak() aborted: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
86 }
87  
88 10 ▶ if ($op->isa("B::PMOP")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->isa('B::PMOP')) { } 0 10 10
elsif ($op->can('first')) { } 5 5 10
89 0 push @op_stack, $op->pmreplroot, $op->pmreplstart, $op->next;
90 }
91 elsif ($op->can("first")) {
92 5 ▶ for (my $kid = $op->first; ref $kid and $$kid; $kid = $kid->sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $kid and $$kid) 7 5 12
Condition66%2/3
ref $kid and $$kid
!ll&&!rl&&r
057
MC/DC50%1/2
ref $kid and $$kid
ref $kid $$kid
Truth table66%2/3
A and B
A= ref $kidB= $$kid
ABresult
0X0
100
111
93 7 push @op_stack, $kid;
94 }
95 5 unshift @op_stack, $op->next;
96 }
97 else {
98 5 unshift @op_stack, $op->next, $op->parent;
99 }
100  
101 }
102  
103 1 ▶ if (defined $DEBUG and $DEBUG eq "deparse" and $tweaked) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clown::DEBUG and $clown::DEBUG eq "deparse" and $tweaked) 0 1 1
Condition33%2/6
defined $clown::DEBUG and $clown::DEBUG eq "deparse" and $tweaked
!ll&&!rl&&r
100
defined $clown::DEBUG and $clown::DEBUG eq "deparse"
!ll&&!rl&&r
010
MC/DC0%0/3
defined $clown::DEBUG and $clown::DEBUG eq "deparse" and $tweaked
defined $clown::DEBUG $clown::DEBUG eq "deparse" $tweaked
Truth table25%1/4
A and B and C
A= defined $clown::DEBUGB= $clown::DEBUG eq "deparse"C= $tweaked
ABCresult
0XX0
10X0
1100
1111
104 0 ▶ eval {warn "sub ", $cv_obj->GV->NAME // "__UNKNOWN__", " :clown ",
Condition0%0/2
$cv_obj->GV->NAME // "__UNKNOWN__"
l!l
00
MC/DC0%0/1
$cv_obj->GV->NAME // "__UNKNOWN__"
$cv_obj->GV->NAME
Truth table0%0/2
A
A= $cv_obj->GV->NAME
Aresult
11
00
105 B::Deparse->new->coderef2text($rv), "\n"};
106 0 ▶ warn "B::Deparse: coderef2text() aborted: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
107 }
108 }
109 1 return grep !$valid_attrs{+lc}, @attrs;
110 }
111  
112 1 ▶sub import {
Subroutine import: called CC=4 SCAR=14.2
113 1 ▶ no warnings qw/uninitialized redefine/;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 $DEBUG = $_[1];
115 1 local $@;
116 1 my $pkg = caller;
117 1 ▶ eval "package $pkg; use types; use class"
Branch: 1 branch
BranchTrueFalseTotal
if $clown::DEBUG eq "types" 0 1 1
118 if $DEBUG eq 'types'; # enable perl type system
119  
120 1 ▶ eval "package $pkg; CHECK { package $pkg; clown::all() }"
Branch: 1 branch
BranchTrueFalseTotal
if $clown::DEBUG eq "all" 0 1 1
121 if $DEBUG eq 'all';
122  
123 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
124 }
125  
126  
127 1 1;
128  
129 __END__
« blib/lib/sealed.pm ↑ Summary blib/lib/ModPerl/RegistryCookerSealed.pm »