Seal.xs

filter: Statementstmt 90.5% 603 / 666 Branchbran 71.3% 404 / 566 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.7% 1007 / 1232 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Catalyst/Seal/Guard.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "include/cs_slot.h"
7  
8 /* G_LIST is the 5.36 spelling of G_ARRAY. Unguarded it breaks the build on
9 * everything older. */
10 #ifndef G_LIST
11 # define G_LIST G_ARRAY
12 #endif
13  
14 /* GvCV_set arrived in 5.13. Before that the slot was assigned directly. */
15 #ifndef GvCV_set
16 # define GvCV_set(gv, cv) (GvCV(gv) = (cv))
17 #endif
18  
19 /* Catalyst::ClassData's accessor calls Moose::Util::find_meta on every read,
20 * to answer a question that stops changing at setup_finalize. This replaces it
21 * with a constant, and falls back to the original for anything the constant
22 * cannot answer: a write, or an invocant that is not the class we resolved
23 * the value for. */
24  
25 /* Put a coderef into a glob's CODE slot, leaving every other slot alone.
26 *
27 * Only the CODE slot, because ClassData keeps the value itself in the same
28 * glob's SCALAR slot and the write that triggered this is about to go there.
29 *
30 * Not sv_setsv on the glob: that is a glob assignment, and a glob assignment
31 * over an existing subroutine warns "Subroutine %s redefined" against the
32 * warning bits of whoever happened to call the accessor. They did not ask for
33 * this and cannot silence it. */
34 static void
35 9 cs_set_cv(pTHX_ SV *fq, CV *code)
36 {
37 9 GV *gv = gv_fetchsv(fq, GV_ADD, SVt_PVCV);
38 CV *old;
39  
40 9 ▶ if (!gv)
Branch: 1 branch
BranchTrueFalseTotal
if (!gv) 0 9 9
41 0 return;
42  
43 9 old = GvCV(gv);
44 9 GvCV_set(gv, (CV *) SvREFCNT_inc((SV *) code));
45 9 GvCVGEN(gv) = 0;
46 9 ▶ if (old)
Branch: 1 branch
BranchTrueFalseTotal
if (old) 9 0 9
47 9 SvREFCNT_dec((SV *) old);
48 }
49  
50 static void
51 5 cs_unseal(pTHX_ cs_slot *slot)
52 {
53 CV *orig;
54  
55 5 ▶ if (slot->unsealed)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->unsealed) 0 5 5
56 0 return;
57 5 slot->unsealed = 1;
58  
59 5 orig = (CV *) SvRV(slot->orig);
60 5 cs_set_cv(aTHX_ slot->fq, orig);
61 5 ▶ if (slot->fq_alias)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->fq_alias) 4 1 5
62 4 cs_set_cv(aTHX_ slot->fq_alias, orig);
63  
64 /* Our own CV is running right now. The slot holds a reference to it, so
65 * dropping it out of the globs above cannot free it under us. */
66 5 mro_method_changed_in(slot->stash);
67 }
68  
69 /* Call the shadowed accessor with the arguments we were given and leave its
70 * results where the caller expects ours. Returns the number of values.
71 *
72 * The slow path only: a write, or a subclass created after sealing. Copying
73 * both ways costs nothing there and keeps the stack handling obvious. */
74 static I32
75 2878 cs_delegate(pTHX_ SV *code, I32 items, I32 ax)
76 {
77 2878 dSP;
78 I32 count, i;
79 2878 I32 gimme = GIMME_V;
80 SV **argv;
81 SV **out;
82  
83 2878 ▶ Newx(argv, items ? items : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, items ? items : 1, SV *); 2878 0 2878
84 6386 ▶ for (i = 0; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) 3508 2878 6386
85 3508 argv[i] = ST(i);
86  
87 2878 ENTER;
88 2878 SAVETMPS;
89  
90 2878 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2878 2878
91 2878 ▶ EXTEND(SP, items);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items); 2878 0 2878
EXTEND(SP, items); 0 2878 2878
92 6386 ▶ for (i = 0; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) 3508 2878 6386
93 3508 PUSHs(argv[i]);
94 2878 PUTBACK;
95  
96 2878 count = call_sv(code, gimme);
97  
98 2872 SPAGAIN;
99 2872 ▶ Newx(out, count ? count : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(out, count ? count : 1, SV *); 1145 1727 2872
100 4017 ▶ for (i = count - 1; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) 1145 2872 4017
101 1145 out[i] = newSVsv(POPs);
102 2872 PUTBACK;
103  
104 2872 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1145 1727 2872
105 2872 LEAVE;
106  
107 /* Back to our own frame. Rewind to just below our arguments and put the
108 * results there instead. */
109 2872 SPAGAIN;
110 2872 SP = PL_stack_base + ax - 1;
111 2872 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 2872 0 2872
EXTEND(SP, count); 0 2872 2872
112 2872 PUTBACK;
113  
114 4017 ▶ for (i = 0; i < count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) 1145 2872 4017
115 1145 ST(i) = sv_2mortal(out[i]);
116  
117 2872 Safefree(argv);
118 2872 Safefree(out);
119 2872 return count;
120 }
121  
122 static void
123 19218 cs_const_accessor(pTHX_ CV *cv)
124 {
125 19218 dXSARGS;
126 19218 cs_slot *slot = (cs_slot *) CvXSUBANY(cv).any_ptr;
127 SV *inv;
128 19218 int mine = 0;
129  
130 19218 ▶ if (!slot)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) 0 19218 19218
131 0 croak("Catalyst::Seal: sealed accessor with no slot");
132  
133 19218 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 19218 19218
134 0 croak("Usage: %s($class)", SvPV_nolen(slot->name));
135  
136 /* Is this the class the value was resolved for? A subclass created after
137 * sealing inherits this XSUB but not the answer, so it goes the long way
138 * round rather than being told the parent's value. */
139 19218 inv = ST(0);
140 19218 ▶ if (SvROK(inv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(inv)) { 14319 4899 19218
141 14319 SV *rv = SvRV(inv);
142 14319 ▶ if (SvOBJECT(rv) && SvSTASH(rv) == slot->stash)
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(rv) && SvSTASH(rv) == slot->stash) 14319 0 14319
if (SvOBJECT(rv) && SvSTASH(rv) == slot->stash) 14315 4 14319
143 14315 mine = 1;
144 }
145 4899 ▶ else if (SvOK(inv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(inv)) { 4899 0 4899
146 STRLEN il, pl;
147 4899 const char *ip = SvPV_const(inv, il);
148 4899 ▶ const char *pp = SvPV_const(slot->pkg, pl);
Branch: 1 branch
BranchTrueFalseTotal
const char *pp = SvPV_const(slot->pkg, pl); 4899 0 4899
149 4899 ▶ if (il == pl && memEQ(ip, pp, il))
Branch: 2 branches
BranchTrueFalseTotal
if (il == pl && memEQ(ip, pp, il)) 4874 25 4899
if (il == pl && memEQ(ip, pp, il)) 4874 0 4874
150 4874 mine = 1;
151 }
152  
153 19218 ▶ if (mine && items == 1 && !slot->unsealed) {
Branch: 3 branches
BranchTrueFalseTotal
if (mine && items == 1 && !slot->unsealed) { 19189 29 19218
if (mine && items == 1 && !slot->unsealed) { 19184 5 19189
if (mine && items == 1 && !slot->unsealed) { 19184 0 19184
154 /* The stock accessor ends in a bare "return;" when it finds nothing
155 * defined, anywhere in the ISA. That is an empty list in list context,
156 * not a one element list holding undef, and a class data value that
157 * was explicitly set to undef is indistinguishable from one that was
158 * never set: both take that branch. */
159 19184 ▶ if (!SvOK(slot->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(slot->value)) { 4 19180 19184
160 4 ▶ if (GIMME_V == G_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) 3 1 4
161 3 XSRETURN_EMPTY;
162 1 XSRETURN_UNDEF;
163 }
164 19180 ST(0) = slot->value;
165 19180 XSRETURN(1);
166 }
167  
168 /* A write puts the original accessor back for good: from here on the value
169 * can change and a constant would be a lie.
170 *
171 * Only a write to the class we resolved for. A subclass writing through
172 * the inherited XSUB is storing into its own glob and says nothing about
173 * ours, so unsealing there would give up the constant for every class in
174 * the tree the first time any one of them is written to. */
175 34 ▶ if (mine && items > 1)
Branch: 2 branches
BranchTrueFalseTotal
if (mine && items > 1) 5 29 34
if (mine && items > 1) 5 0 5
176 5 cs_unseal(aTHX_ slot);
177  
178 34 XSRETURN(cs_delegate(aTHX_ slot->orig, items, ax));
179 }
180  
181 /* A slot name, hashed once at seal time. */
182 static void
183 804 cs_key_init(pTHX_ cs_key *k, SV *name)
184 {
185 STRLEN len;
186 804 const char *p = SvPV_const(name, len);
187 804 k->key = newSVsv(name);
188 804 ▶ PERL_HASH(k->hash, p, len);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(k->hash, p, len); 804 0 804
189 804 }
190  
191 /* The entry for a slot, or NULL when the slot is not there. Absence and undef
192 * are different questions here: Moose's predicate asks the first, so the
193 * entry rather than the value is what the callers below test. */
194 static HE *
195 10596 cs_slot_he(pTHX_ HV *hv, cs_key *k)
196 {
197 10596 return hv_fetch_ent(hv, k->key, 0, k->hash);
198 }
199  
200 /* Is this invocant a blessed hash we can read a slot out of? */
201 #define CS_IS_HASH_OBJECT(sv) \
202 (SvROK(sv) && SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV)
203  
204 /* A sealed attribute reader.
205 *
206 * Moose's inlined reader is already cheap, about 88ns against this XSUB's 44,
207 * so the only thing worth doing here is the fetch and nothing else. Everything
208 * that is not "an instance of exactly this class, reading" goes to the reader
209 * we shadowed: a class method call, a write, an instance of a subclass.
210 *
211 * A lazy attribute whose slot is not there yet also delegates, which is how
212 * this avoids reimplementing Moose's builders entirely. The stock reader
213 * builds and stores it, and every call after that takes the fast path.
214 *
215 * A WRITE is answered here only for an attribute the Perl side has checked is
216 * a plain one: no type constraint, no coercion, no trigger, no weak reference,
217 * no initializer. What Moose's inlined writer does for such an attribute is
218 * store a copy and return it, and so does this. Everything else still
219 * delegates, which is what keeps a predicate honest for a lazy attribute
220 * nothing has written. */
221 static void
222 59563 cs_attr_reader(pTHX_ CV *cv)
223 {
224 59563 dXSARGS;
225 59563 cs_reader *r = (cs_reader *) CvXSUBANY(cv).any_ptr;
226 SV *inv, *rv;
227 HE *he;
228  
229 59563 ▶ if (!r)
Branch: 1 branch
BranchTrueFalseTotal
if (!r) 0 59563 59563
230 0 croak("Catalyst::Seal: sealed reader with no slot");
231  
232 59563 ▶ if (items != 1 && !(items == 2 && r->rw))
Branch: 3 branches
BranchTrueFalseTotal
if (items != 1 && !(items == 2 && r->rw)) 8767 50796 59563
if (items != 1 && !(items == 2 && r->rw)) 8767 0 8767
if (items != 1 && !(items == 2 && r->rw)) 611 8156 8767
233 611 goto slow;
234  
235 58952 inv = ST(0);
236 58952 ▶ if (!SvROK(inv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(inv)) 1 58951 58952
237 1 goto slow;
238  
239 58951 rv = SvRV(inv);
240 /* A blessed object is a hash reference, so SvTYPE alone would accept an
241 * object of any class built on any other type. SvOBJECT is the question
242 * actually being asked. */
243 58951 ▶ if (!SvOBJECT(rv) || SvTYPE(rv) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(rv) || SvTYPE(rv) != SVt_PVHV) 58951 0 58951
if (!SvOBJECT(rv) || SvTYPE(rv) != SVt_PVHV) 0 58951 58951
244 0 goto slow;
245 58951 ▶ if (SvSTASH(rv) != r->stash)
Branch: 1 branch
BranchTrueFalseTotal
if (SvSTASH(rv) != r->stash) 3 58948 58951
246 3 goto slow;
247  
248 58948 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 8156 50792 58948
249 /* $_[0]{slot} = $_[1] - a copy, because that is what an assignment
250 * into a hash is, and returning the stored element, because that is
251 * what the assignment evaluates to. */
252 8156 SV *nv = newSVsv(ST(1));
253 8156 he = hv_store_ent((HV *) rv, r->key, nv, r->hash);
254 8156 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 0 8156 8156
255 0 SvREFCNT_dec(nv);
256 0 goto slow;
257 }
258 8156 ST(0) = HeVAL(he);
259 8156 XSRETURN(1);
260 }
261  
262 50792 he = hv_fetch_ent((HV *) rv, r->key, 0, r->hash);
263 50792 ▶ if (he) {
Branch: 1 branch
BranchTrueFalseTotal
if (he) { 48201 2591 50792
264 48201 ST(0) = HeVAL(he);
265 48201 XSRETURN(1);
266 }
267 2591 ▶ if (!r->lazy)
Branch: 1 branch
BranchTrueFalseTotal
if (!r->lazy) 369 2222 2591
268 369 XSRETURN_UNDEF;
269  
270 2222 slow:
271 2837 XSRETURN(cs_delegate(aTHX_ r->orig, items, ax));
272 }
273  
274 /* Catalyst::depth: scalar @{ shift->stack || [] }.
275 *
276 * An empty or absent slot is 0 rather than a delegation, because that is the
277 * answer the stock body's `|| []` gives. Anything that is not an array
278 * reference is not a stack this understands, and goes the long way. */
279 static void
280 6 cs_slot_count(pTHX_ CV *cv)
281 {
282 6 dXSARGS;
283 6 cs_count *r = (cs_count *) CvXSUBANY(cv).any_ptr;
284 SV *inv, *rv, *val;
285 HE *he;
286  
287 6 ▶ if (!r)
Branch: 1 branch
BranchTrueFalseTotal
if (!r) 0 6 6
288 0 croak("Catalyst::Seal: sealed count with no slot");
289  
290 6 ▶ if (items != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) 0 6 6
291 0 goto slow;
292  
293 6 inv = ST(0);
294 6 ▶ if (!CS_IS_HASH_OBJECT(inv))
Branch: 3 branches
BranchTrueFalseTotal
if (!CS_IS_HASH_OBJECT(inv)) 6 0 6
if (!CS_IS_HASH_OBJECT(inv)) 6 0 6
if (!CS_IS_HASH_OBJECT(inv)) 0 6 6
295 0 goto slow;
296  
297 6 rv = SvRV(inv);
298 6 ▶ if (SvSTASH(rv) != r->stash)
Branch: 1 branch
BranchTrueFalseTotal
if (SvSTASH(rv) != r->stash) 1 5 6
299 1 goto slow;
300  
301 5 he = cs_slot_he(aTHX_ (HV *) rv, &r->slot);
302 5 ▶ if (!he || !SvOK(HeVAL(he)))
Branch: 2 branches
BranchTrueFalseTotal
if (!he || !SvOK(HeVAL(he))) 4 1 5
if (!he || !SvOK(HeVAL(he))) 1 3 4
303 2 XSRETURN_IV(0);
304  
305 3 val = HeVAL(he);
306 3 ▶ if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 3 0 3
if (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) 0 3 3
307 0 goto slow;
308  
309 3 XSRETURN_IV(av_len((AV *) SvRV(val)) + 1);
310  
311 1 slow:
312 1 XSRETURN(cs_delegate(aTHX_ r->orig, items, ax));
313 }
314  
315 /* Call $code with the arguments this XSUB was given, minus the invocant, and
316 * leave its results where ours belong. The arguments are already contiguous on
317 * the stack, so this passes them where they lie rather than copying: the mark
318 * goes on the invocant's own slot, which is the one immediately below them.
319 *
320 * The results then land one slot too high - call_sv leaves them at mark + 1 -
321 * so they are moved down by one. That is a pointer move of however many values
322 * were returned, against a copy of every argument and every result, which is
323 * what cs_delegate does for the paths where clarity is worth more than speed. */
324 static I32
325 3529 cs_call_shifted(pTHX_ SV *code, I32 items, I32 ax)
326 {
327 3529 dSP;
328 I32 count;
329 3529 I32 gimme = GIMME_V;
330  
331 3529 SP = PL_stack_base + ax + items - 1;
332 3529 ▶ PUSHMARK(PL_stack_base + ax);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(PL_stack_base + ax); 0 3529 3529
333 3529 PUTBACK;
334  
335 3529 count = call_sv(code, gimme);
336  
337 3524 SPAGAIN;
338 3524 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 3524 0 3524
339 3524 Move(PL_stack_base + ax + 1, PL_stack_base + ax, count, SV *);
340 3524 SP = PL_stack_base + ax + count - 1;
341 3524 PUTBACK;
342  
343 3524 return count;
344 }
345  
346 /* Catalyst::Action::execute: $self->code->(@_) */
347 static void
348 3530 cs_action_execute(pTHX_ CV *cv)
349 {
350 3530 dXSARGS;
351 3530 cs_action *a = (cs_action *) CvXSUBANY(cv).any_ptr;
352 HE *he;
353 SV *code;
354  
355 3530 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 0 3530 3530
356 0 croak("Catalyst::Seal: sealed action method with no slot");
357  
358 3530 ▶ if (items < 1 || !CS_IS_HASH_OBJECT(ST(0)))
Branch: 4 branches
BranchTrueFalseTotal
if (items < 1 || !CS_IS_HASH_OBJECT(ST(0))) 3530 0 3530
if (items < 1 || !CS_IS_HASH_OBJECT(ST(0))) 3530 0 3530
if (items < 1 || !CS_IS_HASH_OBJECT(ST(0))) 3530 0 3530
if (items < 1 || !CS_IS_HASH_OBJECT(ST(0))) 0 3530 3530
359 0 goto slow;
360  
361 3530 he = cs_slot_he(aTHX_ (HV *) SvRV(ST(0)), &a->code);
362 3530 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 1 3529 3530
363 1 goto slow;
364  
365 3529 code = HeVAL(he);
366 3529 ▶ if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 3529 0 3529
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 0 3529 3529
367 0 goto slow;
368  
369 3529 XSRETURN(cs_call_shifted(aTHX_ code, items, ax));
370  
371 1 slow:
372 1 XSRETURN(cs_delegate(aTHX_ a->orig, items, ax));
373 }
374  
375 /* Catalyst::Action::dispatch:
376 *
377 * $self->has_instance ? $c->execute($self->instance, $self)
378 * : $c->execute($self->class, $self)
379 *
380 * has_instance is Moose's predicate, which asks whether the slot is there and
381 * not whether its value is defined, so this asks the same. */
382 static void
383 3532 cs_action_dispatch(pTHX_ CV *cv)
384 {
385 3532 dXSARGS;
386 3532 cs_action *a = (cs_action *) CvXSUBANY(cv).any_ptr;
387 HE *he;
388 SV *target;
389 I32 count;
390 3532 I32 gimme = GIMME_V;
391  
392 3532 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 0 3532 3532
393 0 croak("Catalyst::Seal: sealed action method with no slot");
394  
395 3532 ▶ if (items != 2 || !CS_IS_HASH_OBJECT(ST(0)))
Branch: 4 branches
BranchTrueFalseTotal
if (items != 2 || !CS_IS_HASH_OBJECT(ST(0))) 3532 0 3532
if (items != 2 || !CS_IS_HASH_OBJECT(ST(0))) 3532 0 3532
if (items != 2 || !CS_IS_HASH_OBJECT(ST(0))) 3532 0 3532
if (items != 2 || !CS_IS_HASH_OBJECT(ST(0))) 0 3532 3532
396 0 goto slow;
397  
398 3532 he = cs_slot_he(aTHX_ (HV *) SvRV(ST(0)), &a->instance);
399 3532 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 3529 3 3532
400 3529 he = cs_slot_he(aTHX_ (HV *) SvRV(ST(0)), &a->klass);
401 3529 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 3529 3529
402 0 goto slow;
403 }
404 3532 target = HeVAL(he);
405  
406 /* $c->execute($target, $self). A method call, not the resolved body: a
407 * plugin's execute is found the same way the stock code would find it,
408 * and the interpreter's method cache makes the lookup a hash hit. */
409 3532 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3532 3532
410 3532 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 3532 3532
411 3532 PUSHs(ST(1));
412 3532 PUSHs(target);
413 3532 PUSHs(ST(0));
414 3532 PUTBACK;
415  
416 7064 count = call_method(SvPV_nolen(a->method), gimme);
417  
418 3531 SPAGAIN;
419 3531 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 2 3529 3531
420 2 Move(SP - count + 1, PL_stack_base + ax, count, SV *);
421 3531 SP = PL_stack_base + ax + count - 1;
422 3531 PUTBACK;
423 3531 XSRETURN(count);
424  
425 0 slow:
426 0 XSRETURN(cs_delegate(aTHX_ a->orig, items, ax));
427 }
428  
429 /* ---- parameters ----------------------------------------------------------
430 *
431 * A query string reaches an application through a percent-decode and a UTF-8
432 * decode, run separately over every name and every value, each through a
433 * closure and an eval. Four pairs cost about ten microseconds that way, which
434 * is more than the rest of the request's parsing put together.
435 *
436 * Both halves are here. Nothing about them is clever: what makes them worth
437 * having is that they are one pass over the bytes with no Perl frame in it.
438 */
439  
440 /* Percent-decode in place of Catalyst::Engine::unescape_uri:
441 *
442 * s/(?:%([0-9A-Fa-f]{2})|\+)/defined $1 ? chr(hex($1)) : ' '/eg
443 *
444 * A '%' that does not begin two hex digits is not an escape and stays as it
445 * is, which is what that regex does and not what a stricter decoder would. */
446 432 static int cs_hexval(unsigned char c)
447 {
448 432 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 432 0 432
if (c >= '0' && c <= '9') return c - '0'; 318 114 432
449 114 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 3 111 114
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 1 2 3
450 113 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 113 0 113
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 111 2 113
451 2 return -1;
452 }
453  
454 static void
455 910 cs_unescape(pTHX_ const char *s, STRLEN len, SV *out)
456 {
457 STRLEN i;
458 char *d;
459  
460 910 ▶ SvUPGRADE(out, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(out, SVt_PV); 910 0 910
461 910 ▶ SvGROW(out, len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len + 1); 910 0 910
SvGROW(out, len + 1); 910 0 910
462 910 d = SvPVX(out);
463  
464 6691 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 5781 910 6691
465 5781 unsigned char c = (unsigned char) s[i];
466 5781 ▶ if (c == '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') { 2 5779 5781
467 2 *d++ = ' ';
468 }
469 5995 ▶ else if (c == '%' && i + 2 < len) {
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '%' && i + 2 < len) { 218 5561 5779
else if (c == '%' && i + 2 < len) { 216 2 218
470 216 int hi = cs_hexval((unsigned char) s[i + 1]);
471 216 int lo = cs_hexval((unsigned char) s[i + 2]);
472 216 ▶ if (hi >= 0 && lo >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi >= 0 && lo >= 0) { 215 1 216
if (hi >= 0 && lo >= 0) { 215 0 215
473 215 *d++ = (char) ((hi << 4) | lo);
474 215 i += 2;
475 }
476 else {
477 1 *d++ = (char) c;
478 }
479 }
480 else {
481 5563 *d++ = (char) c;
482 }
483 }
484  
485 910 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
486 910 *SvEND(out) = '\0';
487 910 SvPOK_only(out);
488 910 SvUTF8_off(out);
489 910 }
490  
491 /* Strict UTF-8, as Encode's "UTF-8" means it and not as perl's own "utf8"
492 * does. No overlong form, no surrogate, nothing above U+10FFFF, nothing
493 * truncated - and no noncharacter either, which is the one that is easy to
494 * miss: Encode refuses all sixty-six of U+FDD0..U+FDEF and U+xFFFE/U+xFFFF,
495 * measured against Encode 3.21 rather than read off a specification.
496 *
497 * Anything this refuses goes back to Encode, which refuses it in the words
498 * Catalyst's error handling expects, so the cost of being wrong in this
499 * direction is a slow path and the cost of being wrong in the other is a
500 * request that should have been refused and was not. */
501 static int
502 1022 cs_utf8_strict_ok(const unsigned char *s, STRLEN len)
503 {
504 1022 STRLEN i = 0;
505  
506 6951 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 5984 967 6951
507 5984 unsigned char c = s[i];
508 STRLEN need, k;
509 unsigned char lo2, hi2;
510 UV cp;
511  
512 5984 ▶ if (c < 0x80) { i++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { i++; continue; } 5895 89 5984
513  
514 89 ▶ if (c >= 0xC2 && c <= 0xDF) { need = 1; lo2 = 0x80; hi2 = 0xBF; }
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xC2 && c <= 0xDF) { need = 1; lo2 = 0x80; hi2 = 0xBF; } 86 3 89
if (c >= 0xC2 && c <= 0xDF) { need = 1; lo2 = 0x80; hi2 = 0xBF; } 6 80 86
515 83 ▶ else if (c == 0xE0) { need = 2; lo2 = 0xA0; hi2 = 0xBF; }
Branch: 1 branch
BranchTrueFalseTotal
else if (c == 0xE0) { need = 2; lo2 = 0xA0; hi2 = 0xBF; } 2 81 83
516 81 ▶ else if (c >= 0xE1 && c <= 0xEC) { need = 2; lo2 = 0x80; hi2 = 0xBF; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xE1 && c <= 0xEC) { need = 2; lo2 = 0x80; hi2 = 0xBF; } 78 3 81
else if (c >= 0xE1 && c <= 0xEC) { need = 2; lo2 = 0x80; hi2 = 0xBF; } 13 65 78
517 68 ▶ else if (c == 0xED) { need = 2; lo2 = 0x80; hi2 = 0x9F; }
Branch: 1 branch
BranchTrueFalseTotal
else if (c == 0xED) { need = 2; lo2 = 0x80; hi2 = 0x9F; } 3 65 68
518 65 ▶ else if (c >= 0xEE && c <= 0xEF) { need = 2; lo2 = 0x80; hi2 = 0xBF; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xEE && c <= 0xEF) { need = 2; lo2 = 0x80; hi2 = 0xBF; } 62 3 65
else if (c >= 0xEE && c <= 0xEF) { need = 2; lo2 = 0x80; hi2 = 0xBF; } 42 20 62
519 23 ▶ else if (c == 0xF0) { need = 3; lo2 = 0x90; hi2 = 0xBF; }
Branch: 1 branch
BranchTrueFalseTotal
else if (c == 0xF0) { need = 3; lo2 = 0x90; hi2 = 0xBF; } 8 15 23
520 15 ▶ else if (c >= 0xF1 && c <= 0xF3) { need = 3; lo2 = 0x80; hi2 = 0xBF; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xF1 && c <= 0xF3) { need = 3; lo2 = 0x80; hi2 = 0xBF; } 12 3 15
else if (c >= 0xF1 && c <= 0xF3) { need = 3; lo2 = 0x80; hi2 = 0xBF; } 3 9 12
521 12 ▶ else if (c == 0xF4) { need = 3; lo2 = 0x80; hi2 = 0x8F; }
Branch: 1 branch
BranchTrueFalseTotal
else if (c == 0xF4) { need = 3; lo2 = 0x80; hi2 = 0x8F; } 5 7 12
522 7 else return 0;
523  
524 82 ▶ if (i + need >= len) return 0; /* truncated */
Branch: 1 branch
BranchTrueFalseTotal
if (i + need >= len) return 0; /* truncated */ 2 80 82
525 80 ▶ if (s[i + 1] < lo2 || s[i + 1] > hi2) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i + 1] < lo2 || s[i + 1] > hi2) return 0; 79 1 80
if (s[i + 1] < lo2 || s[i + 1] > hi2) return 0; 2 77 79
526 164 ▶ for (k = 2; k <= need; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 2; k <= need; k++) 87 77 164
527 87 ▶ if (s[i + k] < 0x80 || s[i + k] > 0xBF) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i + k] < 0x80 || s[i + k] > 0xBF) return 0; 87 0 87
if (s[i + k] < 0x80 || s[i + k] > 0xBF) return 0; 0 87 87
528  
529 77 cp = (UV)(c & (0x3F >> need)); /* 5, 4 then 3 payload bits */
530 241 ▶ for (k = 1; k <= need; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k <= need; k++) 164 77 241
531 164 cp = (cp << 6) | (UV)(s[i + k] & 0x3F);
532  
533 77 ▶ if ((cp & 0xFFFE) == 0xFFFE) return 0; /* U+xFFFE, U+xFFFF */
Branch: 1 branch
BranchTrueFalseTotal
if ((cp & 0xFFFE) == 0xFFFE) return 0; /* U+xFFFE, U+xFFFF */ 9 68 77
534 68 ▶ if (cp >= 0xFDD0 && cp <= 0xFDEF) return 0; /* the other sixty-two */
Branch: 2 branches
BranchTrueFalseTotal
if (cp >= 0xFDD0 && cp <= 0xFDEF) return 0; /* the other sixty-two */ 45 23 68
if (cp >= 0xFDD0 && cp <= 0xFDEF) return 0; /* the other sixty-two */ 34 11 45
535  
536 34 i += need + 1;
537 }
538  
539 967 return 1;
540 }
541  
542 /* The bytes are already the UTF-8 form of the characters they stand for, so
543 * a valid string is decoded by saying so. Encode returns a string flagged
544 * UTF-8 whatever it held - ASCII and empty included - and so does this. */
545 static int
546 1022 cs_utf8_decode(pTHX_ SV *sv)
547 {
548 STRLEN len;
549 1022 const char *p = SvPV_const(sv, len);
550 1022 ▶ if (!cs_utf8_strict_ok((const unsigned char *) p, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!cs_utf8_strict_ok((const unsigned char *) p, len)) 55 967 1022
551 55 return 0;
552 967 SvUTF8_on(sv);
553 967 return 1;
554 }
555  
556 /* A sealed constructor.
557 *
558 * What Moose's inlined constructor does for a class whose attributes are all
559 * plain is: work out the parameters, bless a hash, fill it from the parameters
560 * and the defaults, and run the BUILD methods. Every decision in the middle of
561 * that - which key an attribute reads, whether it has a default and what kind,
562 * whether anything has to be checked - was settled when the class was made
563 * immutable, and is in the template this walks.
564 *
565 * Anything the template could not settle takes the stock constructor, entire.
566 * There is no half-built instance to hand over: the escape happens before the
567 * first BUILD runs, and the hash built up to that point is simply dropped. */
568 static void
569 1136 cs_constructor(pTHX_ CV *cv)
570 {
571 1136 dXSARGS;
572 1136 cs_ctor *c = (cs_ctor *) CvXSUBANY(cv).any_ptr;
573 1136 HV *params = NULL;
574 1136 SV *params_rv = NULL;
575 HV *self;
576 SV *rv;
577 int i;
578  
579 1136 ▶ if (!c)
Branch: 1 branch
BranchTrueFalseTotal
if (!c) 0 1136 1136
580 0 croak("Catalyst::Seal: sealed constructor with no template");
581  
582 /* A subclass created after the seal inherits this XSUB but not the
583 * template that was resolved for its parent. */
584 1136 ▶ if (items < 1 || SvROK(ST(0)))
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || SvROK(ST(0))) 1136 0 1136
if (items < 1 || SvROK(ST(0))) 0 1136 1136
585 0 goto slow;
586 {
587 STRLEN il, cl;
588 1136 const char *ip = SvPV_const(ST(0), il);
589 1136 ▶ const char *cp = SvPV_const(c->class, cl);
Branch: 1 branch
BranchTrueFalseTotal
const char *cp = SvPV_const(c->class, cl); 1136 0 1136
590 1136 ▶ if (il != cl || memNE(ip, cp, il))
Branch: 2 branches
BranchTrueFalseTotal
if (il != cl || memNE(ip, cp, il)) 1134 2 1136
if (il != cl || memNE(ip, cp, il)) 0 1134 1134
591 2 goto slow;
592 }
593  
594 1134 ▶ if (c->buildargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->buildargs) { 377 757 1134
595 /* A class with its own BUILDARGS: call it exactly as the stock
596 * constructor would, with the arguments as they arrived.
597 *
598 * What it returns is almost certainly mortal, and this frame's
599 * FREETMPS is about to run, so a reference is taken on the hash
600 * itself and wrapped in an RV that belongs to the frame BELOW - the
601 * one whose temporaries live as long as this XSUB does. */
602 377 dSP;
603 I32 count;
604 377 HV *got = NULL;
605  
606 377 ENTER;
607 377 SAVETMPS;
608 377 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 377 377
609 377 ▶ EXTEND(SP, items);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items); 377 0 377
EXTEND(SP, items); 0 377 377
610 1129 ▶ for (i = 0; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) 752 377 1129
611 752 PUSHs(ST(i));
612 377 PUTBACK;
613 377 count = call_sv(c->buildargs, G_SCALAR);
614 377 SPAGAIN;
615 377 ▶ if (count) {
Branch: 1 branch
BranchTrueFalseTotal
if (count) { 377 0 377
616 377 SV *res = POPs;
617 377 ▶ if (SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV 377 0 377
if (SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV 377 0 377
618 377 ▶ && !SvOBJECT(SvRV(res)))
Branch: 1 branch
BranchTrueFalseTotal
&& !SvOBJECT(SvRV(res))) 377 0 377
619 377 got = (HV *) SvREFCNT_inc(SvRV(res));
620 }
621 377 PUTBACK;
622 377 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 377 0 377
623 377 LEAVE;
624  
625 377 ▶ if (!got)
Branch: 1 branch
BranchTrueFalseTotal
if (!got) 0 377 377
626 0 goto slow;
627 377 params = got;
628 377 params_rv = sv_2mortal(newRV_noinc((SV *) got));
629 }
630 757 ▶ else if (items == 2 && SvROK(ST(1))
Branch: 2 branches
BranchTrueFalseTotal
else if (items == 2 && SvROK(ST(1)) 736 21 757
else if (items == 2 && SvROK(ST(1)) 735 1 736
631 735 ▶ && SvTYPE(SvRV(ST(1))) == SVt_PVHV && !SvOBJECT(SvRV(ST(1)))) {
Branch: 2 branches
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(1))) == SVt_PVHV && !SvOBJECT(SvRV(ST(1)))) { 735 0 735
&& SvTYPE(SvRV(ST(1))) == SVt_PVHV && !SvOBJECT(SvRV(ST(1)))) { 735 0 735
632 /* Moose's own BUILDARGS hands a single hash reference straight
633 * through, so the hash BUILD sees is the caller's own. */
634 735 params_rv = ST(1);
635 735 params = (HV *) SvRV(params_rv);
636 }
637 22 ▶ else if (!((items - 1) % 2)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!((items - 1) % 2)) { 21 1 22
638 21 params = newHV();
639 21 params_rv = sv_2mortal(newRV_noinc((SV *) params));
640 32 ▶ for (i = 1; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) 11 21 32
641 11 (void) hv_store_ent(params, ST(i), newSVsv(ST(i + 1)), 0);
642 }
643 else {
644 1 goto slow; /* an odd number of arguments: Moose says what to do */
645 }
646  
647 /* Moose lets a caller hand in the instance to build into. Rare, and not
648 * something a template can describe. */
649 1133 ▶ if (hv_exists(params, "__INSTANCE__", 12))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(params, "__INSTANCE__", 12)) 0 1133 1133
650 0 goto slow;
651  
652 1133 self = newHV();
653 1133 hv_ksplit(self, c->nattrs);
654 1133 rv = sv_2mortal(newRV_noinc((SV *) self));
655 1133 (void) sv_bless(rv, c->stash);
656  
657 23678 ▶ for (i = 0; i < c->nattrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->nattrs; i++) { 22547 1131 23678
658 22547 cs_ctor_attr *a = &c->attrs[i];
659 22547 HE *given = NULL;
660 22547 SV *stored = NULL;
661  
662 22547 ▶ if (a->init_arg)
Branch: 1 branch
BranchTrueFalseTotal
if (a->init_arg) 21416 1131 22547
663 21416 given = hv_fetch_ent(params, a->init_arg, 0, a->init_hash);
664  
665 22547 ▶ if (given) {
Branch: 1 branch
BranchTrueFalseTotal
if (given) { 747 21800 22547
666 747 ▶ if (a->flags & CS_A_GUARDED)
Branch: 1 branch
BranchTrueFalseTotal
if (a->flags & CS_A_GUARDED) 0 747 747
667 0 goto slow;
668 747 ▶ if (a->flags & CS_A_TYPED) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->flags & CS_A_TYPED) { 369 378 747
669 /* The constraint's own check, so what passes here is exactly
670 * what passes there. A value that fails takes the long way,
671 * where Moose raises the error it would have raised anyway -
672 * this has no business writing that message. */
673 369 dSP;
674 I32 count;
675 int good;
676 369 ENTER;
677 369 SAVETMPS;
678 369 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 369 369
679 369 ▶ XPUSHs(HeVAL(given));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(HeVAL(given)); 0 369 369
680 369 PUTBACK;
681 369 count = call_sv(a->check, G_SCALAR);
682 369 SPAGAIN;
683 369 ▶ good = count ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
good = count ? SvTRUE(TOPs) : 0; 369 0 369
684 369 ▶ if (count) (void) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count) (void) POPs; 369 0 369
685 369 PUTBACK;
686 369 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 369 0 369
687 369 LEAVE;
688 369 ▶ if (!good)
Branch: 1 branch
BranchTrueFalseTotal
if (!good) 1 368 369
689 1 goto slow;
690 }
691 746 stored = newSVsv(HeVAL(given));
692 }
693 else {
694 21800 switch (a->kind) {
695 2272 case CS_D_CONST:
696 2272 stored = newSVsv(a->dflt);
697 2272 break;
698 1136 case CS_D_HASH:
699 1136 stored = newRV_noinc((SV *) newHV());
700 1136 break;
701 1500 case CS_D_ARRAY:
702 1500 stored = newRV_noinc((SV *) newAV());
703 1500 break;
704 756 case CS_D_CODE: {
705 756 dSP;
706 I32 count;
707 756 ENTER;
708 756 SAVETMPS;
709 756 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 756 756
710 756 ▶ XPUSHs(rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(rv); 0 756 756
711 756 PUTBACK;
712 756 count = call_sv(a->dflt, G_SCALAR);
713 756 SPAGAIN;
714 756 ▶ stored = count ? newSVsv(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
stored = count ? newSVsv(POPs) : newSV(0); 756 0 756
715 756 PUTBACK;
716 756 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 756 0 756
717 756 LEAVE;
718 756 break;
719 }
720 16136 default:
721 /* No default. Required without one is Moose's error to
722 * raise, and it raises it better than a template could. */
723 16136 ▶ if (a->flags & CS_A_REQUIRED)
Branch: 1 branch
BranchTrueFalseTotal
if (a->flags & CS_A_REQUIRED) 1 16135 16136
724 1 goto slow;
725 16135 continue;
726 }
727 }
728  
729 6410 ▶ if (!hv_store_ent(self, a->slot.key, stored, a->slot.hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store_ent(self, a->slot.key, stored, a->slot.hash)) { 0 6410 6410
730 0 SvREFCNT_dec(stored);
731 0 goto slow;
732 }
733 6410 ▶ if ((a->flags & CS_A_WEAK) && SvROK(stored))
Branch: 2 branches
BranchTrueFalseTotal
if ((a->flags & CS_A_WEAK) && SvROK(stored)) 372 6038 6410
if ((a->flags & CS_A_WEAK) && SvROK(stored)) 372 0 372
734 372 sv_rvweaken(stored);
735 }
736  
737 1131 ▶ if (c->nbuilds) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->nbuilds) { 1131 0 1131
738 1131 AV *builds = (AV *) SvRV(c->builds);
739 2639 ▶ for (i = 0; i < c->nbuilds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->nbuilds; i++) { 1508 1131 2639
740 1508 SV **b = av_fetch(builds, i, 0);
741 1508 dSP;
742 1508 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 0 1508 1508
743 0 continue;
744 1508 ENTER;
745 1508 SAVETMPS;
746 1508 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1508 1508
747 1508 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1508 1508
748 1508 PUSHs(rv);
749 1508 PUSHs(params_rv);
750 1508 PUTBACK;
751 1508 call_sv(*b, G_VOID | G_DISCARD);
752 1508 SPAGAIN;
753 1508 PUTBACK;
754 1508 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 1508 1508
755 1508 LEAVE;
756 }
757 }
758  
759 1131 c->fast++;
760 1131 ST(0) = rv;
761 1131 XSRETURN(1);
762  
763 5 slow:
764 5 c->slow++;
765 5 XSRETURN(cs_delegate(aTHX_ c->orig, items, ax));
766 }
767  
768 MODULE = Catalyst::Seal PACKAGE = Catalyst::Seal
769  
770 PROTOTYPES: DISABLE
771  
772 void
773 _install_const(pkg, name, value, orig, alias = NULL)
774 SV *pkg
775 SV *name
776 SV *value
777 SV *orig
778 SV *alias
779 PREINIT:
780 cs_slot *slot;
781 CV *cv;
782 HV *stash;
783 STRLEN plen, nlen;
784 const char *pstr, *nstr;
785 SV *fq, *fq_alias;
786 PPCODE:
787 626 ▶ if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 626 0 626
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 0 626 626
788 0 croak("Catalyst::Seal::_install_const: orig must be a code reference");
789  
790 626 ▶ pstr = SvPV_const(pkg, plen);
Branch: 1 branch
BranchTrueFalseTotal
pstr = SvPV_const(pkg, plen); 626 0 626
791 626 nstr = SvPV_const(name, nlen);
792  
793 626 stash = gv_stashpvn(pstr, plen, GV_ADD);
794 626 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 626 626
795 0 croak("Catalyst::Seal::_install_const: no stash for %" SVf, SVfARG(pkg));
796  
797 626 fq = newSVpvn(pstr, plen);
798 626 sv_catpvs(fq, "::");
799 626 sv_catpvn(fq, nstr, nlen);
800  
801 /* mk_classdata's second name for the same accessor. Optional, because not
802 * everything sealed here is a class data pair: sealing "config" under the
803 * convention would install a _config_accessor that never existed. */
804 626 ▶ if (alias && SvOK(alias)) {
Branch: 2 branches
BranchTrueFalseTotal
if (alias && SvOK(alias)) { 24 602 626
if (alias && SvOK(alias)) { 0 24 24
805 STRLEN alen;
806 0 const char *astr = SvPV_const(alias, alen);
807 0 fq_alias = newSVpvn(pstr, plen);
808 0 sv_catpvs(fq_alias, "::");
809 0 sv_catpvn(fq_alias, astr, alen);
810 }
811 626 ▶ else if (!alias) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!alias) { 602 24 626
812 602 fq_alias = newSVpvn(pstr, plen);
813 602 sv_catpvs(fq_alias, "::_");
814 602 sv_catpvn(fq_alias, nstr, nlen);
815 602 sv_catpvs(fq_alias, "_accessor");
816 }
817 else {
818 24 fq_alias = NULL;
819 }
820  
821 626 Newxz(slot, 1, cs_slot);
822 626 slot->value = newSVsv(value);
823 626 slot->orig = newSVsv(orig);
824 626 slot->pkg = newSVsv(pkg);
825 626 slot->name = newSVsv(name);
826 626 slot->fq = fq;
827 626 slot->fq_alias = fq_alias;
828 626 slot->stash = (HV *) SvREFCNT_inc((SV *) stash);
829 626 slot->unsealed = 0;
830  
831 /* newXS installs into the named glob and warns about redefinition against
832 * the caller's warning bits, so the Perl side does this inside a
833 * "no warnings 'redefine'". */
834 626 cv = newXS(SvPV_nolen(fq), cs_const_accessor, __FILE__);
835 626 CvXSUBANY(cv).any_ptr = slot;
836 626 slot->cv = (CV *) SvREFCNT_inc((SV *) cv);
837  
838 626 ▶ if (fq_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if (fq_alias) { 602 24 626
839 602 cv = newXS(SvPV_nolen(fq_alias), cs_const_accessor, __FILE__);
840 602 CvXSUBANY(cv).any_ptr = slot;
841 602 slot->cv_alias = (CV *) SvREFCNT_inc((SV *) cv);
842 }
843  
844 626 mro_method_changed_in(stash);
845 626 XSRETURN_YES;
846  
847 # The header build, as one pass over the environment.
848 #
849 # `field` is the memo the Perl side keeps: an environment key maps to 0 for a
850 # key that is not a header, to an array of [ spelling, standard case ] for one
851 # whose spelling has been learned, or to 1 for one that has to go the long way.
852 # A key that is not in the memo at all has never been seen.
853 #
854 # Everything this cannot place - an unlearned key, a value that is not a plain
855 # string, a key marked for the long way - is pushed onto `todo` and handed back
856 # for Perl to finish. After the first request of each shape that list is empty.
857 void
858 _build_headers(env, field, headers, std)
859 HV *env
860 HV *field
861 HV *headers
862 HV *std
863 PREINIT:
864 HE *he;
865 AV *todo;
866 PPCODE:
867 366 todo = newAV();
868  
869 366 hv_iterinit(env);
870 8418 ▶ while ((he = hv_iternext(env))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(env))) { 8052 366 8418
871 8052 SV *key = hv_iterkeysv(he);
872 8052 SV *known = NULL;
873 HE *fe;
874 AV *pair;
875 SV **spell, **case_;
876 SV *value;
877  
878 8052 fe = hv_fetch_ent(field, key, 0, 0);
879 8052 ▶ if (!fe) { /* never seen: Perl learns it */
Branch: 1 branch
BranchTrueFalseTotal
if (!fe) { /* never seen: Perl learns it */ 264 7788 8052
880 264 av_push(todo, newSVsv(key));
881 264 continue;
882 }
883 7788 known = HeVAL(fe);
884  
885 7788 ▶ if (!SvOK(known) || !SvTRUE(known))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(known) || !SvTRUE(known)) 7788 0 7788
if (!SvOK(known) || !SvTRUE(known)) 6726 1062 7788
886 6726 continue; /* not a header at all */
887  
888 1062 ▶ if (!SvROK(known) || SvTYPE(SvRV(known)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(known) || SvTYPE(SvRV(known)) != SVt_PVAV) { 1062 0 1062
if (!SvROK(known) || SvTYPE(SvRV(known)) != SVt_PVAV) { 0 1062 1062
889 0 av_push(todo, newSVsv(key)); /* marked for the long way */
890 0 continue;
891 }
892  
893 1062 value = HeVAL(he);
894 1062 ▶ if (!SvOK(value) || SvROK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(value) || SvROK(value)) { 1062 0 1062
if (!SvOK(value) || SvROK(value)) { 0 1062 1062
895 /* An undefined value is a delete rather than a store, and a
896 * reference is not a string. Both are HTTP::Headers' business. */
897 0 av_push(todo, newSVsv(key));
898 0 continue;
899 }
900  
901 1062 pair = (AV *) SvRV(known);
902 1062 spell = av_fetch(pair, 0, 0);
903 1062 ▶ if (!spell) {
Branch: 1 branch
BranchTrueFalseTotal
if (!spell) { 0 1062 1062
904 0 av_push(todo, newSVsv(key));
905 0 continue;
906 }
907 1062 (void) hv_store_ent(headers, *spell, newSVsv(value), 0);
908  
909 1062 case_ = av_fetch(pair, 1, 0);
910 1062 ▶ if (case_ && SvOK(*case_))
Branch: 2 branches
BranchTrueFalseTotal
if (case_ && SvOK(*case_)) 1062 0 1062
if (case_ && SvOK(*case_)) 0 1062 1062
911 0 (void) hv_store_ent(std, *spell, newSVsv(*case_), 0);
912 }
913  
914 366 PUSHs(sv_2mortal(newRV_noinc((SV *) todo)));
915  
916 # One value, percent-decoded and then UTF-8 decoded. Returns undef where the
917 # bytes are not strict UTF-8, which is the caller's signal to let Encode have
918 # them: refusing is Encode's job and Catalyst has a handler for it.
919 void
920 _decode_param(value, unescape = 1, decode = 1)
921 SV *value
922 int unescape
923 int decode
924 PREINIT:
925 SV *out;
926 STRLEN len;
927 const char *p;
928 PPCODE:
929 128 ▶ if (!SvOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) 0 128 128
930 0 XSRETURN_UNDEF;
931  
932 128 out = sv_newmortal();
933 128 ▶ if (unescape) {
Branch: 1 branch
BranchTrueFalseTotal
if (unescape) { 8 120 128
934 8 p = SvPV_const(value, len);
935 8 cs_unescape(aTHX_ p, len, out);
936 }
937 else {
938 120 sv_setsv(out, value);
939 120 SvUTF8_off(out);
940 }
941  
942 128 ▶ if (decode && !cs_utf8_decode(aTHX_ out))
Branch: 2 branches
BranchTrueFalseTotal
if (decode && !cs_utf8_decode(aTHX_ out)) 120 8 128
if (decode && !cs_utf8_decode(aTHX_ out)) 53 67 120
943 53 XSRETURN_EMPTY; /* not ours to answer */
944  
945 75 ST(0) = out;
946 75 XSRETURN(1);
947  
948 # A query string, split and decoded in one pass, as the hash Catalyst puts in
949 # query_parameters when it is not using Hash::MultiValue: one value for a name
950 # that appeared once, an array reference for a name that appeared more than
951 # once, in the order they arrived.
952 #
953 # Returns an empty list if any part of it is not strict UTF-8, so the whole
954 # query goes back to the stock parser rather than being half decoded here.
955 void
956 _parse_query(qs, decode = 1)
957 SV *qs
958 int decode
959 PREINIT:
960 STRLEN len, i;
961 const char *s;
962 HV *out;
963 376 SV *keywords = NULL;
964 376 ▶ int first = 1;
Branch: 1 branch
BranchTrueFalseTotal
int first = 1; 0 376 376
965 PPCODE:
966 376 ▶ if (!SvOK(qs))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(qs)) 0 376 376
967 0 XSRETURN_EMPTY;
968 376 s = SvPV_const(qs, len);
969  
970 376 out = newHV();
971 376 sv_2mortal((SV *) out);
972  
973 376 i = 0;
974 378 ▶ while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* s/\A[&;]+// */
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* s/\A[&;]+// */ 327 51 378
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* s/\A[&;]+// */ 2 325 327
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* s/\A[&;]+// */ 0 325 325
975  
976 826 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 452 374 826
977 452 STRLEN start = i, eq = 0;
978 452 int have_eq = 0;
979 452 SV *name, *value = NULL;
980 HE *he;
981  
982 7093 ▶ while (i < len && s[i] != '&' && s[i] != ';') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] != '&' && s[i] != ';') { 6768 325 7093
while (i < len && s[i] != '&' && s[i] != ';') { 6642 126 6768
while (i < len && s[i] != '&' && s[i] != ';') { 6641 1 6642
983 6641 ▶ if (!have_eq && s[i] == '=') { eq = i; have_eq = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!have_eq && s[i] == '=') { eq = i; have_eq = 1; } 1595 5046 6641
if (!have_eq && s[i] == '=') { eq = i; have_eq = 1; } 450 1145 1595
984 6641 i++;
985 }
986  
987 452 name = sv_newmortal();
988 452 ▶ cs_unescape(aTHX_ s + start, (have_eq ? eq : i) - start, name);
Branch: 1 branch
BranchTrueFalseTotal
cs_unescape(aTHX_ s + start, (have_eq ? eq : i) - start, name); 450 2 452
989 452 ▶ if (decode && !cs_utf8_decode(aTHX_ name))
Branch: 2 branches
BranchTrueFalseTotal
if (decode && !cs_utf8_decode(aTHX_ name)) 452 0 452
if (decode && !cs_utf8_decode(aTHX_ name)) 0 452 452
990 0 XSRETURN_EMPTY;
991  
992 452 ▶ if (have_eq) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_eq) { 450 2 452
993 450 value = sv_newmortal();
994 450 cs_unescape(aTHX_ s + eq + 1, i - eq - 1, value);
995 450 ▶ if (decode && !cs_utf8_decode(aTHX_ value))
Branch: 2 branches
BranchTrueFalseTotal
if (decode && !cs_utf8_decode(aTHX_ value)) 450 0 450
if (decode && !cs_utf8_decode(aTHX_ value)) 2 448 450
996 2 XSRETURN_EMPTY;
997 }
998  
999 /* An isindex query: the first pair with no '=' is the keywords, and
1000 * the stock parser records it while still adding it as a parameter. */
1001 450 ▶ if (first && !have_eq)
Branch: 2 branches
BranchTrueFalseTotal
if (first && !have_eq) 323 127 450
if (first && !have_eq) 1 322 323
1002 1 keywords = name;
1003 450 first = 0;
1004  
1005 450 he = hv_fetch_ent(out, name, 0, 0);
1006 450 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 447 3 450
1007 447 ▶ (void) hv_store_ent(out, name,
Branch: 1 branch
BranchTrueFalseTotal
(void) hv_store_ent(out, name, 445 2 447
1008 value ? newSVsv(value) : newSV(0), 0);
1009 }
1010 else {
1011 3 SV *seen = HeVAL(he);
1012 3 ▶ if (SvROK(seen) && SvTYPE(SvRV(seen)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(seen) && SvTYPE(SvRV(seen)) == SVt_PVAV) { 1 2 3
if (SvROK(seen) && SvTYPE(SvRV(seen)) == SVt_PVAV) { 1 0 1
1013 1 ▶ av_push((AV *) SvRV(seen), value ? newSVsv(value) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push((AV *) SvRV(seen), value ? newSVsv(value) : newSV(0)); 1 0 1
1014 }
1015 else {
1016 2 AV *av = newAV();
1017 2 av_push(av, newSVsv(seen));
1018 2 ▶ av_push(av, value ? newSVsv(value) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(av, value ? newSVsv(value) : newSV(0)); 2 0 2
1019 2 (void) hv_store_ent(out, name, newRV_noinc((SV *) av), 0);
1020 }
1021 }
1022  
1023 578 ▶ while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* split /[&;]+/ */
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* split /[&;]+/ */ 255 323 578
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* split /[&;]+/ */ 126 129 255
while (i < len && (s[i] == '&' || s[i] == ';')) i++; /* split /[&;]+/ */ 2 127 129
1024 }
1025  
1026 374 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 374 374
1027 374 PUSHs(sv_2mortal(newRV_inc((SV *) out)));
1028 374 ▶ PUSHs(keywords ? keywords : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(keywords ? keywords : &PL_sv_undef); 1 373 374
1029  
1030 # How many constructions a sealed constructor answered itself, and how many it
1031 # handed on. For the test suite: an XSUB that is installed and never taken is
1032 # indistinguishable from one that works, until something counts.
1033 void
1034 _ctor_counts(code)
1035 SV *code
1036 PREINIT:
1037 CV *cv;
1038 cs_ctor *c;
1039 PPCODE:
1040 14 ▶ if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 14 0 14
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 0 14 14
1041 0 XSRETURN_EMPTY;
1042 14 cv = (CV *) SvRV(code);
1043 14 ▶ if (!CvISXSUB(cv) || CvXSUB(cv) != cs_constructor)
Branch: 2 branches
BranchTrueFalseTotal
if (!CvISXSUB(cv) || CvXSUB(cv) != cs_constructor) 14 0 14
if (!CvISXSUB(cv) || CvXSUB(cv) != cs_constructor) 0 14 14
1044 0 XSRETURN_EMPTY;
1045 14 c = (cs_ctor *) CvXSUBANY(cv).any_ptr;
1046 14 ▶ if (!c)
Branch: 1 branch
BranchTrueFalseTotal
if (!c) 0 14 14
1047 0 XSRETURN_EMPTY;
1048 14 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 14 14
1049 14 PUSHs(sv_2mortal(newSVuv(c->fast)));
1050 14 PUSHs(sv_2mortal(newSVuv(c->slow)));
1051  
1052 # Whether a given sealed accessor has been unsealed by a write. For the test
1053 # suite, and for CATALYST_SEAL_DEBUG reporting.
1054 void
1055 _is_sealed(code)
1056 SV *code
1057 PREINIT:
1058 CV *cv;
1059 cs_slot *slot;
1060 PPCODE:
1061 12 ▶ if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 12 0 12
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 0 12 12
1062 0 XSRETURN_UNDEF;
1063 12 cv = (CV *) SvRV(code);
1064 12 ▶ if (CvISXSUB(cv) && CvXSUB(cv) == cs_const_accessor) {
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) && CvXSUB(cv) == cs_const_accessor) { 10 2 12
if (CvISXSUB(cv) && CvXSUB(cv) == cs_const_accessor) { 6 4 10
1065 6 slot = (cs_slot *) CvXSUBANY(cv).any_ptr;
1066 6 ▶ if (slot && !slot->unsealed)
Branch: 2 branches
BranchTrueFalseTotal
if (slot && !slot->unsealed) 6 0 6
if (slot && !slot->unsealed) 6 0 6
1067 6 XSRETURN_YES;
1068 0 XSRETURN_NO;
1069 }
1070 6 ▶ if (CvISXSUB(cv) && (CvXSUB(cv) == cs_attr_reader
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) && (CvXSUB(cv) == cs_attr_reader 4 2 6
if (CvISXSUB(cv) && (CvXSUB(cv) == cs_attr_reader 3 1 4
1071 3 ▶ || CvXSUB(cv) == cs_slot_count
Branch: 1 branch
BranchTrueFalseTotal
|| CvXSUB(cv) == cs_slot_count 2 1 3
1072 2 ▶ || CvXSUB(cv) == cs_constructor
Branch: 1 branch
BranchTrueFalseTotal
|| CvXSUB(cv) == cs_constructor 2 0 2
1073 2 ▶ || CvXSUB(cv) == cs_action_execute
Branch: 1 branch
BranchTrueFalseTotal
|| CvXSUB(cv) == cs_action_execute 1 1 2
1074 1 ▶ || CvXSUB(cv) == cs_action_dispatch))
Branch: 1 branch
BranchTrueFalseTotal
|| CvXSUB(cv) == cs_action_dispatch)) 1 0 1
1075 4 XSRETURN_YES;
1076 2 XSRETURN_UNDEF;
1077  
1078 void
1079 _install_reader(pkg, name, key, orig, lazy, rw = 0)
1080 SV *pkg
1081 SV *name
1082 SV *key
1083 SV *orig
1084 int lazy
1085 int rw
1086 PREINIT:
1087 cs_reader *r;
1088 CV *cv;
1089 HV *stash;
1090 STRLEN plen, nlen, klen;
1091 const char *pstr, *nstr, *kstr;
1092 U32 hash;
1093 SV *fq;
1094 PPCODE:
1095 641 ▶ if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 641 0 641
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 0 641 641
1096 0 croak("Catalyst::Seal::_install_reader: orig must be a code reference");
1097  
1098 641 ▶ pstr = SvPV_const(pkg, plen);
Branch: 1 branch
BranchTrueFalseTotal
pstr = SvPV_const(pkg, plen); 641 0 641
1099 641 ▶ nstr = SvPV_const(name, nlen);
Branch: 1 branch
BranchTrueFalseTotal
nstr = SvPV_const(name, nlen); 641 0 641
1100 641 kstr = SvPV_const(key, klen);
1101  
1102 641 stash = gv_stashpvn(pstr, plen, GV_ADD);
1103 641 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 641 641
1104 0 croak("Catalyst::Seal::_install_reader: no stash for %" SVf, SVfARG(pkg));
1105  
1106 641 fq = newSVpvn(pstr, plen);
1107 641 sv_catpvs(fq, "::");
1108 641 sv_catpvn(fq, nstr, nlen);
1109  
1110 641 ▶ PERL_HASH(hash, kstr, klen);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, kstr, klen); 641 0 641
1111  
1112 641 Newxz(r, 1, cs_reader);
1113 641 r->key = newSVsv(key);
1114 641 r->hash = hash;
1115 641 r->orig = newSVsv(orig);
1116 641 r->fq = fq;
1117 641 r->stash = (HV *) SvREFCNT_inc((SV *) stash);
1118 641 r->lazy = lazy ? 1 : 0;
1119 641 r->rw = rw ? 1 : 0;
1120  
1121 641 cv = newXS(SvPV_nolen(fq), cs_attr_reader, __FILE__);
1122 641 CvXSUBANY(cv).any_ptr = r;
1123 641 r->cv = (CV *) SvREFCNT_inc((SV *) cv);
1124  
1125 641 mro_method_changed_in(stash);
1126 641 XSRETURN_YES;
1127  
1128 # The length of an array in a slot: Catalyst::depth and nothing else so far.
1129 void
1130 _install_count(pkg, name, key, orig)
1131 SV *pkg
1132 SV *name
1133 SV *key
1134 SV *orig
1135 PREINIT:
1136 cs_count *r;
1137 CV *cv;
1138 HV *stash;
1139 STRLEN plen, nlen;
1140 const char *pstr, *nstr;
1141 SV *fq;
1142 PPCODE:
1143 12 ▶ if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 12 0 12
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 0 12 12
1144 0 croak("Catalyst::Seal::_install_count: orig must be a code reference");
1145  
1146 12 ▶ pstr = SvPV_const(pkg, plen);
Branch: 1 branch
BranchTrueFalseTotal
pstr = SvPV_const(pkg, plen); 12 0 12
1147 12 nstr = SvPV_const(name, nlen);
1148  
1149 12 stash = gv_stashpvn(pstr, plen, GV_ADD);
1150 12 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 12 12
1151 0 croak("Catalyst::Seal::_install_count: no stash for %" SVf, SVfARG(pkg));
1152  
1153 12 fq = newSVpvn(pstr, plen);
1154 12 sv_catpvs(fq, "::");
1155 12 sv_catpvn(fq, nstr, nlen);
1156  
1157 12 Newxz(r, 1, cs_count);
1158 12 cs_key_init(aTHX_ &r->slot, key);
1159 12 r->orig = newSVsv(orig);
1160 12 r->stash = (HV *) SvREFCNT_inc((SV *) stash);
1161  
1162 12 cv = newXS(SvPV_nolen(fq), cs_slot_count, __FILE__);
1163 12 CvXSUBANY(cv).any_ptr = r;
1164 12 r->cv = (CV *) SvREFCNT_inc((SV *) cv);
1165 12 SvREFCNT_dec(fq);
1166  
1167 12 mro_method_changed_in(stash);
1168 12 XSRETURN_YES;
1169  
1170 # A sealed constructor. `attrs` is an array of
1171 #
1172 # [ init_arg | undef, slot, kind, default, flags ]
1173 #
1174 # in the order the stock constructor fills them, and `builds` an array of the
1175 # BUILD bodies in the order BUILDALL would call them. `buildargs` is the
1176 # class's own BUILDARGS, or undef where Moose's is in force.
1177 void
1178 _install_ctor(pkg, name, orig, buildargs, attrs, builds)
1179 SV *pkg
1180 SV *name
1181 SV *orig
1182 SV *buildargs
1183 SV *attrs
1184 SV *builds
1185 PREINIT:
1186 cs_ctor *c;
1187 CV *cv;
1188 HV *stash;
1189 AV *av;
1190 STRLEN plen, nlen;
1191 const char *pstr, *nstr;
1192 SV *fq;
1193 int i;
1194 PPCODE:
1195 36 ▶ if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 36 0 36
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 0 36 36
1196 0 croak("Catalyst::Seal::_install_ctor: orig must be a code reference");
1197 36 ▶ if (!SvROK(attrs) || SvTYPE(SvRV(attrs)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(attrs) || SvTYPE(SvRV(attrs)) != SVt_PVAV) 36 0 36
if (!SvROK(attrs) || SvTYPE(SvRV(attrs)) != SVt_PVAV) 0 36 36
1198 0 croak("Catalyst::Seal::_install_ctor: attrs must be an array reference");
1199 36 ▶ if (!SvROK(builds) || SvTYPE(SvRV(builds)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(builds) || SvTYPE(SvRV(builds)) != SVt_PVAV) 36 0 36
if (!SvROK(builds) || SvTYPE(SvRV(builds)) != SVt_PVAV) 0 36 36
1200 0 croak("Catalyst::Seal::_install_ctor: builds must be an array reference");
1201  
1202 36 ▶ pstr = SvPV_const(pkg, plen);
Branch: 1 branch
BranchTrueFalseTotal
pstr = SvPV_const(pkg, plen); 36 0 36
1203 36 nstr = SvPV_const(name, nlen);
1204  
1205 36 stash = gv_stashpvn(pstr, plen, GV_ADD);
1206 36 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 36 36
1207 0 croak("Catalyst::Seal::_install_ctor: no stash for %" SVf, SVfARG(pkg));
1208  
1209 36 fq = newSVpvn(pstr, plen);
1210 36 sv_catpvs(fq, "::");
1211 36 sv_catpvn(fq, nstr, nlen);
1212  
1213 36 Newxz(c, 1, cs_ctor);
1214 36 c->stash = (HV *) SvREFCNT_inc((SV *) stash);
1215 36 c->class = newSVsv(pkg);
1216 36 c->orig = newSVsv(orig);
1217 36 ▶ c->buildargs = (buildargs && SvOK(buildargs)) ? newSVsv(buildargs) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
c->buildargs = (buildargs && SvOK(buildargs)) ? newSVsv(buildargs) : NULL; 36 0 36
c->buildargs = (buildargs && SvOK(buildargs)) ? newSVsv(buildargs) : NULL; 12 24 36
1218 36 c->builds = newSVsv(builds);
1219 36 c->nbuilds = (int) av_len((AV *) SvRV(builds)) + 1;
1220  
1221 36 av = (AV *) SvRV(attrs);
1222 36 c->nattrs = (int) av_len(av) + 1;
1223 36 ▶ if (c->nattrs)
Branch: 1 branch
BranchTrueFalseTotal
if (c->nattrs) 36 0 36
1224 36 Newxz(c->attrs, c->nattrs, cs_ctor_attr);
1225  
1226 756 ▶ for (i = 0; i < c->nattrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->nattrs; i++) { 720 36 756
1227 720 SV **row = av_fetch(av, i, 0);
1228 AV *r;
1229 SV **f;
1230 720 cs_ctor_attr *a = &c->attrs[i];
1231  
1232 720 ▶ if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) 720 0 720
if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) 720 0 720
if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) 0 720 720
1233 0 croak("Catalyst::Seal::_install_ctor: attribute %d is not an array", i);
1234 720 r = (AV *) SvRV(*row);
1235  
1236 720 f = av_fetch(r, 0, 0);
1237 720 ▶ if (f && SvOK(*f)) {
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) { 720 0 720
if (f && SvOK(*f)) { 684 36 720
1238 STRLEN kl;
1239 const char *kp;
1240 684 a->init_arg = newSVsv(*f);
1241 684 kp = SvPV_const(a->init_arg, kl);
1242 684 ▶ PERL_HASH(a->init_hash, kp, kl);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(a->init_hash, kp, kl); 684 0 684
1243 }
1244  
1245 720 f = av_fetch(r, 1, 0);
1246 720 ▶ if (!f || !SvOK(*f))
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) 720 0 720
if (!f || !SvOK(*f)) 0 720 720
1247 0 croak("Catalyst::Seal::_install_ctor: attribute %d has no slot", i);
1248 720 cs_key_init(aTHX_ &a->slot, *f);
1249  
1250 720 f = av_fetch(r, 2, 0);
1251 720 ▶ a->kind = f ? (int) SvIV(*f) : CS_D_NONE;
Branch: 1 branch
BranchTrueFalseTotal
a->kind = f ? (int) SvIV(*f) : CS_D_NONE; 720 0 720
1252  
1253 720 f = av_fetch(r, 3, 0);
1254 720 ▶ a->dflt = (f && SvOK(*f)) ? newSVsv(*f) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
a->dflt = (f && SvOK(*f)) ? newSVsv(*f) : NULL; 720 0 720
a->dflt = (f && SvOK(*f)) ? newSVsv(*f) : NULL; 84 636 720
1255 720 ▶ if ((a->kind == CS_D_CONST || a->kind == CS_D_CODE) && !a->dflt)
Branch: 3 branches
BranchTrueFalseTotal
if ((a->kind == CS_D_CONST || a->kind == CS_D_CODE) && !a->dflt) 648 72 720
if ((a->kind == CS_D_CONST || a->kind == CS_D_CODE) && !a->dflt) 24 624 648
if ((a->kind == CS_D_CONST || a->kind == CS_D_CODE) && !a->dflt) 12 84 96
1256 12 a->dflt = newSV(0);
1257  
1258 720 f = av_fetch(r, 4, 0);
1259 720 ▶ a->flags = f ? (int) SvIV(*f) : 0;
Branch: 1 branch
BranchTrueFalseTotal
a->flags = f ? (int) SvIV(*f) : 0; 720 0 720
1260  
1261 720 f = av_fetch(r, 5, 0);
1262 720 ▶ if (f && SvOK(*f))
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) 720 0 720
if (f && SvOK(*f)) 132 588 720
1263 132 a->check = newSVsv(*f);
1264 720 ▶ if ((a->flags & CS_A_TYPED) && !a->check)
Branch: 2 branches
BranchTrueFalseTotal
if ((a->flags & CS_A_TYPED) && !a->check) 132 588 720
if ((a->flags & CS_A_TYPED) && !a->check) 0 132 132
1265 0 croak("Catalyst::Seal::_install_ctor: attribute %d is typed with no check", i);
1266 }
1267  
1268 36 cv = newXS(SvPV_nolen(fq), cs_constructor, __FILE__);
1269 36 CvXSUBANY(cv).any_ptr = c;
1270 36 c->cv = (CV *) SvREFCNT_inc((SV *) cv);
1271 36 SvREFCNT_dec(fq);
1272  
1273 36 mro_method_changed_in(stash);
1274 36 XSRETURN_YES;
1275  
1276 # Catalyst::Action::execute and ::dispatch. `kind` is "execute" or "dispatch";
1277 # the slot names come from the metaclass rather than from a literal here.
1278 void
1279 _install_action(pkg, name, kind, code_slot, instance_slot, class_slot, orig)
1280 SV *pkg
1281 SV *name
1282 SV *kind
1283 SV *code_slot
1284 SV *instance_slot
1285 SV *class_slot
1286 SV *orig
1287 PREINIT:
1288 cs_action *a;
1289 CV *cv;
1290 HV *stash;
1291 STRLEN plen, nlen;
1292 const char *pstr, *nstr, *kstr;
1293 SV *fq;
1294 int is_dispatch;
1295 PPCODE:
1296 24 ▶ if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 24 0 24
if (!SvROK(orig) || SvTYPE(SvRV(orig)) != SVt_PVCV) 0 24 24
1297 0 croak("Catalyst::Seal::_install_action: orig must be a code reference");
1298  
1299 24 kstr = SvPV_nolen(kind);
1300 24 ▶ if (strEQ(kstr, "dispatch")) is_dispatch = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(kstr, "dispatch")) is_dispatch = 1; 12 12 24
1301 12 ▶ else if (strEQ(kstr, "execute")) is_dispatch = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(kstr, "execute")) is_dispatch = 0; 12 0 12
1302 0 else croak("Catalyst::Seal::_install_action: unknown kind '%s'", kstr);
1303  
1304 24 ▶ pstr = SvPV_const(pkg, plen);
Branch: 1 branch
BranchTrueFalseTotal
pstr = SvPV_const(pkg, plen); 24 0 24
1305 24 nstr = SvPV_const(name, nlen);
1306  
1307 24 stash = gv_stashpvn(pstr, plen, GV_ADD);
1308 24 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 24 24
1309 0 croak("Catalyst::Seal::_install_action: no stash for %" SVf, SVfARG(pkg));
1310  
1311 24 fq = newSVpvn(pstr, plen);
1312 24 sv_catpvs(fq, "::");
1313 24 sv_catpvn(fq, nstr, nlen);
1314  
1315 24 Newxz(a, 1, cs_action);
1316 24 cs_key_init(aTHX_ &a->code, code_slot);
1317 24 cs_key_init(aTHX_ &a->instance, instance_slot);
1318 24 cs_key_init(aTHX_ &a->klass, class_slot);
1319 24 a->method = newSVpvs("execute");
1320 24 a->orig = newSVsv(orig);
1321  
1322 48 ▶ cv = newXS(SvPV_nolen(fq),
Branch: 1 branch
BranchTrueFalseTotal
cv = newXS(SvPV_nolen(fq), 12 12 24
1323 is_dispatch ? cs_action_dispatch : cs_action_execute, __FILE__);
1324 24 CvXSUBANY(cv).any_ptr = a;
1325 24 a->cv = (CV *) SvREFCNT_inc((SV *) cv);
1326 24 SvREFCNT_dec(fq);
1327  
1328 24 mro_method_changed_in(stash);
1329 24 XSRETURN_YES;
« blib/lib/Catalyst/Seal/Guard.pm ↑ Summary