blib/lib/sealed.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 339 CC 33 SCAR 52.8
CC
33
Cov
0%
tweak
462.0
MODIFY_CODE_ATTRIBUTES
90.0
import
20.0
CRAP 197.0
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 sealed;
8 use v5.28;
9  
10 use strict;
11 use warnings;
12 use version;
13  
14 use B::Generate ();
15 use B::Deparse ();
16 use XSLoader ();
17 use Filter::Util::Call;
18  
19 our $VERSION;
20 our $DEBUG;
21  
22 BEGIN {
23 our $VERSION = qv(8.7.7);
24 XSLoader::load("sealed", $VERSION);
25 }
26  
27 my %valid_attrs = (sealed => 1);
28 my $p_obj = B::svref_2object(sub {&tweak}); # template sub
29  
30 # B::PADOP (w/ ithreads) or B::SVOP
31 my $gv_op = $p_obj->START->next->next; # its op of interest
32  
33 sub tweak :prototype($\@\@\@$$\%) {
34 my ($op, $lexical_varnames, $pads, $op_stack, $cv_obj, $pad_names, $processed_op) = @_;
35 my $tweaked = 0;
36  
37 if (${$op->next} and $op->next->name eq "padsv") {
38 $op = $op->next;
39 my $type = $$lexical_varnames[$op->targ]->TYPE;
40 my $class = $type->isa("B::HV") ? $type->NAME : undef;
41  
42 while (${$op->next} and $op->next->name ne "entersub") {
43  
44 if ($op->next->name eq "pushmark") {
45 return $op->next, $tweaked if $$processed_op{+${$op->next}}++;
46 # we need to process this arg stack recursively
47 splice @_, 0, 1, $op->next;
48 ($op, my $t) = &tweak;
49 $tweaked += $t;
50 $op = $_[0]->next unless $$op and ${$op->next};
51 }
52  
53 elsif ($op->next->name eq "method_named" and defined $class) {
54 my $methop = $op->next;
55  
56 my ($method_name, $idx, $targ, $gv, $old_pad);
57  
58 if (ref($gv_op) eq "B::PADOP") {
59 $targ = $methop->targ;
60  
61 # A little prayer
62 # Not sure if this works better pre-ithread cloning, or post-ithread cloning.
63 # I've only used it post-ithread cloning, so YMMV.
64 # $targ collisions? ordering is a WAG with the @op_stack walker down below.
65  
66 $method_name = $$pads[$idx++][$targ] until defined $method_name and not
67 (ref $method_name and warn __PACKAGE__ . ": target collision: targ=$targ");
68 }
69 else {
70 $method_name = ${$methop->meth_sv->object_2svref};
71 }
72  
73 warn __PACKAGE__, ": compiling $class->$method_name lookup.\n"
74 if $DEBUG;
75 my $method = $class->can($method_name)
76 or die __PACKAGE__ . ": invalid lookup: $class->$method_name - did you " .
77 "forget to 'use $class' first?\n";
78  
79 no warnings 'uninitialized';
80  
81 my $mverify = sub {
82 goto &$method if $method == $_[0]->can($method_name);
83 require Carp;
84 $$pads[$idx][$targ] =~ s/:\w+$/:FAILED/ if ref($gv_op) eq "B::PADOP";
85 ${$methop->meth_sv->object_2svref} =~ s/:\w+$/:FAILED/
86 if ref($gv_op) ne "B::PADOP";
87 local $@;
88 eval {warn "sub ", $cv_obj->GV->NAME // "__UNKNOWN__", " :sealed ",
89 B::Deparse->new->coderef2text($cv_obj->object_2svref), "\n"};
90 Carp::confess ("sealed verify failed: $_[0]->$method_name method lookup differs " .
91 "from $class->$method_name:FAILED lookup");
92 };
93  
94 # replace $methop
95 # in the ithread case, we need to pass an arbitrary typegglob that we will "fix"
96 # in the next conditional block. we choose *tweak arbitrarily for this end.
97 # the non-ithreaded case needs no additional massaging, so we pass a CodeRef.
98 $gv = new($gv_op->name, $gv_op->flags,
99 ref($gv_op) eq "B::PADOP" ? *tweak :
100 $DEBUG eq "verify" ? $mverify : $method,
101 $cv_obj->PADLIST);
102 $gv->next($methop->next);
103 $gv->sibparent($methop->sibparent);
104 $op->next($gv);
105 $$processed_op{$$_}++ for $op, $gv, $methop;
106  
107 no warnings 'uninitialized';
108  
109 if (ref($gv) eq "B::PADOP") {
110 # we answer the prayer by resetting $$pads[--$idx][$gv->padix], which
111 # has the correct semantics (for $method) under assignment.
112  
113 my $padix = $gv->padix;
114 my (undef, @p) = $cv_obj->PADLIST->ARRAY; # new() modified PADLIST
115 $pads = [ map defined ? $_->object_2svref : $_, @p ];
116 $$pads[--$idx][$padix] = $DEBUG eq "verify" ? $mverify : $method;
117  
118 # mark our changes for B::Deparse's source code renderer
119 $$pads[$idx][$targ] .= $DEBUG ne "verify" ? ":compiled" : ":verified";
120 }
121 else {
122 # mark our changes for B::Deparse's source code renderer
123 ${$methop->meth_sv->object_2svref} .= $DEBUG ne "verify"
124 ? ":compiled" : ":verified";
125 }
126  
127 ++$tweaked;
128 }
129 }
130  
131 continue {
132 last unless $$op and ${$op->next};
133 $op = $op->next;
134 }
135 }
136  
137 push @$op_stack, $op if $$op;
138 return ($op, $tweaked);
139 }
140  
141 sub all {
142 my $pkg = caller;
143 no strict 'refs';
144 eval "BEGIN {
145 while (my (undef, \$v) = each %{$pkg\::}) {
146 eval 'ref(\$v) eq q(CODE)' or next;
147 MODIFY_CODE_ATTRIBUTES(\$pkg, \$v, \"sealed\");
148 }
149 }";
150 }
151  
152 sub MODIFY_CODE_ATTRIBUTES {
153 my ($class, $rv, @attrs) = @_;
154 local $@;
155  
156 if ((not defined $DEBUG or $DEBUG ne "disabled") and grep $valid_attrs{+lc}, @attrs) {
157  
158 my $cv_obj = B::svref_2object($rv);
159 my @op_stack = $cv_obj->START;
160 my ($pad_names, @p) = $cv_obj->PADLIST->ARRAY;
161 my @pads = map $_->object_2svref, @p;
162 my @lexical_varnames = $pad_names->ARRAY;
163 my %processed_op;
164 my $tweaked;
165  
166 while (my $op = shift @op_stack and not defined $^S) {
167 ref $op and $$op and not $processed_op{$$op}++
168 or next;
169  
170 $op->dump if defined $DEBUG and $DEBUG eq 'dump';
171  
172 if ($op->name eq "pushmark") {
173 no warnings 'uninitialized';
174 $tweaked += eval {tweak $op, @lexical_varnames, @pads, @op_stack,
175 $cv_obj, $pad_names, %processed_op};
176 warn __PACKAGE__ . ": tweak() aborted: $@" if $@;
177 }
178  
179 if ($op->isa("B::PMOP")) {
180 push @op_stack, $op->pmreplroot, $op->pmreplstart, $op->next;
181 }
182 elsif ($op->can("first")) {
183 for (my $kid = $op->first; ref $kid and $$kid; $kid = $kid->sibling) {
184 push @op_stack, $kid;
185 }
186 unshift @op_stack, $op->next;
187 }
188 else {
189 unshift @op_stack, $op->next, $op->parent;
190 }
191  
192 }
193  
194 if (defined $DEBUG and $DEBUG eq "deparse" and $tweaked) {
195 eval {warn "sub ", $cv_obj->GV->NAME // "__UNKNOWN__", " :sealed ",
196 B::Deparse->new->coderef2text($rv), "\n"};
197 warn "B::Deparse: coderef2text() aborted: $@" if $@;
198 }
199 }
200 return grep !$valid_attrs{+lc}, @attrs;
201 }
202  
203 sub import {
204 no warnings qw/uninitialized redefine/;
205 $DEBUG = $_[1];
206 local $@;
207 my $pkg = caller;
208 eval "package $pkg; use types; use class"
209 if $DEBUG eq 'types'; # enable perl type system
210  
211 eval "package $pkg; CHECK { package $pkg; sealed::all() }"
212 if $DEBUG eq 'all';
213  
214 die $@ if $@;
215 filter_add(bless []);
216 }
217  
218 sub source_munger {
219 my $pkg = shift;
220 # NEW in v8.x.y: handle signatures
221 no warnings 'uninitialized';
222 s(^
223 ([^\n]*sub\s+(\w[\w:]*)?\s* # sub declaration and name
224 (?::\s*\w+(?:\(.*?\))?)* # unspaced attrs
225 \s*(?:\(\S+\))?\s*)\((.*?)\)\s+\{ # prototype, signature and open bracket
226 )(
227 my $prefix = $1; # everything preceding the signature's arglist
228 my $name = $2; # sub name
229 local $_ = $3; # signature's arglist
230 my $suffix = "";
231 my $entry;
232 my $idx = 0;
233 s{([\w:]*)\s*(\$\w+)\s*?([/|]{0,2}=\s*[^,]+)?(\s*,\s*|\s*$)}{ # comma-separated sig args
234 $entry++ if length $1;
235  
236 $suffix .= "my $1 $2 = ";
237  
238 tr!=!!d for my $default = $3;
239 if ((substr($default,0,2) =~ tr!/!!d)==2) {
240 $suffix .= "\$_[$idx] // $default;";
241 }
242 elsif ((substr($default,0,2) =~ tr!|!!d)==2) {
243 $suffix .= "\$_[$idx] || $default;";
244 }
245 elsif ($default) {
246 $suffix .= "\@_ > $idx ? \$_[$idx] : $default;";
247 }
248 else {
249 $suffix .= "\$_[$idx];"
250 }
251  
252 ++$idx;
253 "$2$3$4" # drop the class/type info
254 }gmse;
255  
256 if ($DEBUG eq "verify") {
257 $prefix .= " ($_, \@_dummy)";
258 $suffix = "" unless $entry;
259 }
260  
261 # warn "$prefix { $suffix";
262 "$prefix { no warnings qw/experimental shadow/; $suffix";
263 )gmsex;
264  
265 # handle bare typed lexical declarations
266 s/(^|;)\s*my\s+(\w[\w:]*)\s+(\$\w+)(?=(.))/$4 eq ";"
267 ? qq($1BEGIN{local \$@; eval "require $2"} my $2 $3$4 {no strict qw!vars subs!; no warnings 'once'; $3 = $2})
268 : qq($1BEGIN{local \$@; eval "require $2"} my $2 $3)/gmse;
269 }
270  
271 sub filter {
272 my ($self) = @_;
273 my $status = filter_read;
274 source_munger scalar(caller) if $status;
275 return $status;
276 }
277  
278 1;
279  
280 __END__