include/punk/punk_compile.h

filter: Statementstmt 85.6% 443 / 517 Branchbran 53.3% 302 / 566 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.7% 745 / 1083 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/headers.xs ↑ Summary include/punk/punk_ua.h »
1 /* punk_compile.h - Punk::App's boot compiler, in C.
2 *
3 * to_app calls compile(): it resolves and freezes everything the registrar
4 * accumulated into the `state` hash punk_serve.h reads, and returns the PSGI
5 * app. The app, the docs targets, the boot-time target resolver and the
6 * nonblocking Future chain are all real closures - built here with the magic-CV
7 * primitive from punk_static.h (punk_closure / punk_clos_cap), so nothing on
8 * this path is Perl. lib/Punk/App.pm is documentation only.
9 *
10 * Must be included after punk_static.h (the closure primitive), punk_serve.h
11 * (punk_serve / punk_finish_c / punk_handle_error) and punk_app.h.
12 */
13  
14 #ifndef PUNK_COMPILE_H
15 #define PUNK_COMPILE_H
16  
17 /* ---- closures ------------------------------------------------------------- */
18  
19 /* the PSGI app: capture [ $state ]; body is punk_serve($state, $env). */
20 XS_INTERNAL(pc_app_cb);
21 219533 XS_INTERNAL(pc_app_cb) {
22 219533 dXSARGS;
23 219533 AV *cap = punk_clos_cap(aTHX_ cv);
24 SV **holder;
25 pk_state *st;
26 219533 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 219533 0 219533
if (!cap || items < 1) XSRETURN_EMPTY; 0 219533 219533
27 /* slot 0 is the state hash (kept alive for the struct's borrowed
28 * pointers), slot 1 the struct the request path reads */
29 219533 holder = av_fetch(cap, 1, 0);
30 219533 ▶ st = (holder && *holder) ? pk_state_of(aTHX_ *holder) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
st = (holder && *holder) ? pk_state_of(aTHX_ *holder) : NULL; 219533 0 219533
st = (holder && *holder) ? pk_state_of(aTHX_ *holder) : NULL; 219533 0 219533
31 219533 ▶ if (!st) croak("Punk: the compiled application has no state");
Branch: 1 branch
BranchTrueFalseTotal
if (!st) croak("Punk: the compiled application has no state"); 0 219533 219533
32 219533 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) 219533 0 219533
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) 0 219533 219533
33 0 croak("Punk: the app takes a PSGI env hashref");
34 {
35 219533 HV *env = (HV *)SvRV(ST(0));
36 SV *out;
37 /* A preflight is answered here, before routing: OPTIONS on a path with
38 * no OPTIONS route would otherwise be the router's 405. */
39 219533 out = pco_preflight(aTHX_ st, env);
40 219533 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 219525 8 219533
41 219525 out = punk_serve(aTHX_ st, env);
42 /* and the response is decorated here rather than in an
43 * after-dispatch hook, because 404s and 405s never reach one */
44 219524 pco_decorate(aTHX_ st, env, &out);
45 }
46 /* security headers go on outside the branch, so the preflight 204
47 * carries the policy too */
48 219532 phd_decorate(aTHX_ st, env, &out);
49 219532 ST(0) = sv_2mortal(out);
50 }
51 219532 XSRETURN(1);
52 }
53  
54 /* a docs route target: capture [ $response ]; body is $response->() (the
55 * matched-route call passes $c, which the thunk ignores). */
56 XS_INTERNAL(pc_docs_cb);
57 8 XS_INTERNAL(pc_docs_cb) {
58 8 dXSARGS;
59 8 AV *cap = punk_clos_cap(aTHX_ cv);
60 8 ▶ SV *resp = cap ? *av_fetch(cap, 0, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *resp = cap ? *av_fetch(cap, 0, 0) : &PL_sv_undef; 8 0 8
61 SV *out;
62 int count;
63 PERL_UNUSED_VAR(items);
64 8 ENTER; SAVETMPS;
65 8 ▶ PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); PUTBACK; 0 8 8
66 8 count = call_sv(resp, G_SCALAR);
67 8 SPAGAIN;
68 8 ▶ out = count > 0 ? newSVsv(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
out = count > 0 ? newSVsv(POPs) : newSV(0); 8 0 8
69 8 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 8 0 8
70 8 ST(0) = sv_2mortal(out);
71 8 XSRETURN(1);
72 }
73  
74 /* the boot-time resolver handed to the router and api mounts: capture
75 * [ $self ]; body is $self->_resolve_target($target, $what). */
76 XS_INTERNAL(pc_resolve_cb);
77 61420 XS_INTERNAL(pc_resolve_cb) {
78 61420 dXSARGS;
79 61420 AV *cap = punk_clos_cap(aTHX_ cv);
80 61420 ▶ SV *self = cap ? *av_fetch(cap, 0, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *self = cap ? *av_fetch(cap, 0, 0) : &PL_sv_undef; 61420 0 61420
81 SV *argv[2], *r;
82 61420 ▶ argv[0] = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = items > 0 ? ST(0) : &PL_sv_undef; 61420 0 61420
83 61420 ▶ argv[1] = items > 1 ? ST(1) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = items > 1 ? ST(1) : &PL_sv_undef; 61420 0 61420
84 61420 r = pcx_call_meth(aTHX_ self, "_resolve_target", argv, 2, 1);
85 61415 ▶ ST(0) = r ? sv_2mortal(r) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = r ? sv_2mortal(r) : &PL_sv_undef; 61415 0 61415
86 61415 XSRETURN(1);
87 }
88  
89 /* ---- target resolution ---------------------------------------------------- */
90  
91 /* class->can(method) with inheritance (a ->can without the call) */
92 111 static CV *pc_class_can(pTHX_ const char *class, STRLEN cl, const char *meth) {
93 111 HV *stash = gv_stashpvn(class, cl, 0);
94 GV *gv;
95 111 ▶ if (!stash) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return NULL; 32 79 111
96 79 gv = gv_fetchmethod_autoload(stash, meth, 0);
97 79 ▶ return gv ? GvCV(gv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return gv ? GvCV(gv) : NULL; 75 4 79
98 }
99  
100 /* $self->_resolve_target($target, $what): a coderef passes through; a
101 * 'Controller#method' string resolves (loading the class if need be) to the
102 * method coderef. Returns it (+1) or croaks. */
103 61506 static SV *pc_resolve_target(pTHX_ SV *self, SV *target, SV *whatsv) {
104 61506 HV *h = app_hv(aTHX_ self);
105 123012 ▶ const char *what = SvOK(whatsv) ? SvPV_nolen(whatsv) : K_TARGET;
Branch: 1 branch
BranchTrueFalseTotal
const char *what = SvOK(whatsv) ? SvPV_nolen(whatsv) : K_TARGET; 61506 0 61506
106 const char *ts, *method;
107 STRLEN tl, metl, i, cll;
108 61506 const char *hash = NULL;
109 SV *classsv, *caller, *ns;
110 STRLEN cl2; const char *classname;
111 CV *cvp;
112  
113 61506 ▶ if (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVCV) 61461 45 61506
if (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVCV) 61461 0 61461
114 61461 return newSVsv(target);
115 45 ▶ if (!SvOK(target))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(target)) 0 45 45
116 0 croak("Punk: %s must be a coderef or 'Controller#method', got undef",
117 what);
118 45 ts = SvPV_const(target, tl);
119 303 ▶ for (i = tl; i > 0; i--) if (ts[i - 1] == '#') { hash = ts + i - 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = tl; i > 0; i--) if (ts[i - 1] == '#') { hash = ts + i - 1; break; } 45 258 303
for (i = tl; i > 0; i--) if (ts[i - 1] == '#') { hash = ts + i - 1; break; } 303 0 303
120 45 ▶ method = hash ? hash + 1 : NULL;
Branch: 1 branch
BranchTrueFalseTotal
method = hash ? hash + 1 : NULL; 45 0 45
121 45 ▶ metl = hash ? (STRLEN)(ts + tl - method) : 0;
Branch: 1 branch
BranchTrueFalseTotal
metl = hash ? (STRLEN)(ts + tl - method) : 0; 45 0 45
122 45 ▶ if (hash && metl)
Branch: 2 branches
BranchTrueFalseTotal
if (hash && metl) 45 0 45
if (hash && metl) 45 0 45
123 303 ▶ for (i = 0; i < metl; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < metl; i++) 258 45 303
124 258 ▶ if (!isWORDCHAR(method[i])) { hash = NULL; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!isWORDCHAR(method[i])) { hash = NULL; break; } 0 258 258
125 45 ▶ if (!hash || !metl)
Branch: 2 branches
BranchTrueFalseTotal
if (!hash || !metl) 45 0 45
if (!hash || !metl) 0 45 45
126 0 croak("Punk: %s must be a coderef or 'Controller#method', got '%s'",
127 what, ts);
128  
129 45 cll = (STRLEN)(hash - ts);
130 45 classsv = newSVpvn(ts, cll);
131 45 sv_2mortal(classsv);
132 45 caller = app_get(aTHX_ h, K_CALLER);
133 90 ▶ ns = sv_2mortal(newSVpvf("%s" PK_NS_CONTROLLER, caller ? SvPV_nolen(caller) : ""));
Branch: 1 branch
BranchTrueFalseTotal
ns = sv_2mortal(newSVpvf("%s" PK_NS_CONTROLLER, caller ? SvPV_nolen(caller) : "")); 45 0 45
134 45 ▶ if (!(cll >= SvCUR(ns) && memEQ(ts, SvPVX(ns), SvCUR(ns)))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(cll >= SvCUR(ns) && memEQ(ts, SvPVX(ns), SvCUR(ns)))) { 0 45 45
if (!(cll >= SvCUR(ns) && memEQ(ts, SvPVX(ns), SvCUR(ns)))) { 0 0 0
135 45 SV *full = newSVsv(ns);
136 45 sv_catsv(full, classsv);
137 45 classsv = sv_2mortal(full);
138 }
139 45 classname = SvPV_const(classsv, cl2);
140 {
141 45 SV *methsv = sv_2mortal(newSVpvn(method, metl));
142 45 const char *methname = SvPV_nolen(methsv);
143 45 SV *loaderr = NULL;
144 45 cvp = pc_class_can(aTHX_ classname, cl2, methname);
145 45 ▶ if (!cvp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cvp) { 29 16 45
146 29 SV *req = pk_require_src(aTHX_ classname);
147 29 eval_sv(req, G_SCALAR | G_EVAL);
148 29 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 29 0 29
if (SvTRUE(ERRSV)) { 7 22 29
149 7 HV *st2 = gv_stashpvn(classname, cl2, 0);
150 7 ▶ if (!(st2 && HvUSEDKEYS(st2)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(st2 && HvUSEDKEYS(st2))) 7 0 7
if (!(st2 && HvUSEDKEYS(st2))) 0 7 7
if (!(st2 && HvUSEDKEYS(st2))) 5 2 7
151 10 ▶ croak("Punk: %s names '%s' but %s does not load: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: %s names '%s' but %s does not load: %s", 5 0 5
152 what, ts, classname, SvPV_nolen(ERRSV));
153 {
154 2 SV *nofile = sv_2mortal(newSVpvs("Can't locate "));
155 2 const char *p = classname, *e = classname + cl2;
156 STRLEN nl;
157 const char *nf, *es;
158 56 ▶ while (p < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { 54 2 56
159 54 ▶ if (p + 1 < e && p[0] == ':' && p[1] == ':') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < e && p[0] == ':' && p[1] == ':') { 52 2 54
if (p + 1 < e && p[0] == ':' && p[1] == ':') { 5 47 52
if (p + 1 < e && p[0] == ':' && p[1] == ':') { 5 0 5
160 5 sv_catpvs(nofile, "/");
161 5 p += 2;
162 } else {
163 49 sv_catpvn(nofile, p, 1);
164 49 p++;
165 }
166 }
167 2 sv_catpvs(nofile, ".pm ");
168 2 nf = SvPV_const(nofile, nl);
169 2 ▶ es = SvPV_nolen(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
es = SvPV_nolen(ERRSV); 2 0 2
170 2 ▶ if (strncmp(es, nf, nl) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(es, nf, nl) != 0) 1 1 2
171 1 ▶ loaderr = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
loaderr = sv_2mortal(newSVsv(ERRSV)); 1 0 1
172 }
173 }
174 24 cvp = pc_class_can(aTHX_ classname, cl2, methname);
175 }
176 40 ▶ if (!cvp && loaderr)
Branch: 2 branches
BranchTrueFalseTotal
if (!cvp && loaderr) 2 38 40
if (!cvp && loaderr) 1 1 2
177 1 croak("Punk: %s names '%s' but %s does not load: %s",
178 what, ts, classname, SvPV_nolen(loaderr));
179 39 ▶ if (!cvp)
Branch: 1 branch
BranchTrueFalseTotal
if (!cvp) 1 38 39
180 1 croak("Punk: %s names '%s' but %s has no method '%s'",
181 what, ts, classname, methname);
182 }
183 38 return newRV_inc((SV *)cvp);
184 }
185  
186 /* ---- context class -------------------------------------------------------- */
187  
188 /* Build (once) the caller's ::_Context subclass of Punk::Context and install
189 * the registered helpers as methods on it. Returns the class name (+1). */
190 651 static SV *pc_context_class(pTHX_ SV *self) {
191 651 HV *h = app_hv(aTHX_ self);
192 651 SV *caller = app_get(aTHX_ h, K_CALLER);
193 1302 ▶ SV *classsv = newSVpvf("%s" PK_SFX_CONTEXT, caller ? SvPV_nolen(caller) : "");
Branch: 1 branch
BranchTrueFalseTotal
SV *classsv = newSVpvf("%s" PK_SFX_CONTEXT, caller ? SvPV_nolen(caller) : ""); 651 0 651
194 651 STRLEN cl; const char *class = SvPV_const(classsv, cl);
195 651 SV **defd = hv_fetchs(h, K_CTX_DEFINED, 0);
196 HV *helpers; AV *keys; SSize_t i, n;
197  
198 651 ▶ if (!(defd && *defd && SvTRUE(*defd))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(defd && *defd && SvTRUE(*defd))) { 0 651 651
if (!(defd && *defd && SvTRUE(*defd))) { 0 0 0
if (!(defd && *defd && SvTRUE(*defd))) { 0 0 0
199 651 SV *isaname = sv_2mortal(newSVpvf("%s::ISA", class));
200 651 AV *isa = get_av(SvPV_nolen(isaname), GV_ADD);
201 651 av_push(isa, newSVpvs(PK_CONTEXT));
202 651 (void)hv_stores(h, K_CTX_DEFINED, newSViv(1));
203 }
204 651 helpers = app_hash(aTHX_ h, K_HELPERS);
205 651 keys = app_sorted_keys(aTHX_ helpers);
206 651 n = av_len(keys) + 1;
207 827 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 176 651 827
208 176 SV **kp = av_fetch(keys, i, 0);
209 176 STRLEN kl; const char *k = SvPV_const(*kp, kl);
210 176 SV **rec = hv_fetch(helpers, k, (I32)kl, 0);
211 176 ▶ SV **code = (rec && *rec && SvROK(*rec))
Branch: 2 branches
BranchTrueFalseTotal
SV **code = (rec && *rec && SvROK(*rec)) 176 0 176
SV **code = (rec && *rec && SvROK(*rec)) 176 0 176
212 352 ▶ ? hv_fetchs((HV *)SvRV(*rec), K_CODE, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(*rec), K_CODE, 0) : NULL; 176 0 176
213 176 ▶ if (code && *code) {
Branch: 2 branches
BranchTrueFalseTotal
if (code && *code) { 176 0 176
if (code && *code) { 176 0 176
214 176 SV *gvname = sv_2mortal(newSVpvf("%s::%s", class, k));
215 176 GV *gv = gv_fetchsv(gvname, GV_ADD, SVt_PVCV);
216 176 sv_setsv((SV *)gv, *code); /* *{class::name} = $code */
217 }
218 }
219 651 return classsv;
220 }
221  
222 /* ---- models --------------------------------------------------------------- */
223  
224 /* Load every ${ns}*.pm across @INC; push the bare names onto out. */
225 635 static void pc_discover_namespace(pTHX_ SV *ns_sv, AV *out) {
226 635 STRLEN nsl; const char *nss = SvPV_const(ns_sv, nsl);
227 635 SV *rel = sv_2mortal(newSVpvn(nss, nsl));
228 635 HV *seen = (HV *)sv_2mortal((SV *)newHV());
229 635 AV *inc = GvAV(PL_incgv);
230 SSize_t i, n;
231 char *rp; STRLEN j, rl;
232  
233 /* rel = ns; s/::\z//; s{::}{/}g */
234 635 ▶ if (SvCUR(rel) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(rel) >= 2) { 635 0 635
235 635 rp = SvPVX(rel);
236 635 ▶ if (rp[SvCUR(rel) - 1] == ':' && rp[SvCUR(rel) - 2] == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (rp[SvCUR(rel) - 1] == ':' && rp[SvCUR(rel) - 2] == ':') 635 0 635
if (rp[SvCUR(rel) - 1] == ':' && rp[SvCUR(rel) - 2] == ':') 635 0 635
237 635 SvCUR_set(rel, SvCUR(rel) - 2);
238 }
239 635 rp = SvPVX(rel); rl = SvCUR(rel);
240 {
241 635 SV *slashed = sv_2mortal(newSVpvs(""));
242 8994 ▶ for (j = 0; j < rl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rl; j++) { 8359 635 8994
243 8359 ▶ if (rp[j] == ':' && j + 1 < rl && rp[j + 1] == ':') {
Branch: 3 branches
BranchTrueFalseTotal
if (rp[j] == ':' && j + 1 < rl && rp[j + 1] == ':') { 653 7706 8359
if (rp[j] == ':' && j + 1 < rl && rp[j + 1] == ':') { 653 0 653
if (rp[j] == ':' && j + 1 < rl && rp[j + 1] == ':') { 653 0 653
244 653 sv_catpvs(slashed, "/"); j++;
245 }
246 7706 else sv_catpvn(slashed, rp + j, 1);
247 }
248 635 rel = slashed;
249 }
250  
251 635 ▶ n = inc ? av_len(inc) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = inc ? av_len(inc) + 1 : 0; 635 0 635
252 5891 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5256 635 5891
253 5256 SV **ip = av_fetch(inc, i, 0);
254 SV *dir; DIR *dh; Direntry_t *de; AV *ents; SSize_t ei, en;
255 5256 ▶ if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 5256 0 5256
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 5256 0 5256
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 5256 0 5256
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 0 5256 5256
256 5256 dir = sv_2mortal(newSVsv(*ip));
257 5256 sv_catpvs(dir, "/"); sv_catsv(dir, rel);
258 5256 dh = PerlDir_open(SvPV_nolen(dir));
259 5256 ▶ if (!dh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dh) continue; 5254 2 5256
260 2 ents = (AV *)sv_2mortal((SV *)newAV());
261 10 ▶ while ((de = PerlDir_read(dh))) av_push(ents, newSVpv(de->d_name, 0));
Branch: 1 branch
BranchTrueFalseTotal
while ((de = PerlDir_read(dh))) av_push(ents, newSVpv(de->d_name, 0)); 8 2 10
262 2 PerlDir_close(dh);
263 2 en = av_len(ents) + 1;
264 2 ▶ if (en > 1) sortsv(AvARRAY(ents), (STRLEN)en, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (en > 1) sortsv(AvARRAY(ents), (STRLEN)en, Perl_sv_cmp); 2 0 2
265 10 ▶ for (ei = 0; ei < en; ei++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ei = 0; ei < en; ei++) { 8 2 10
266 8 SV **ep = av_fetch(ents, ei, 0);
267 8 STRLEN el; const char *es = SvPV_const(*ep, el);
268 STRLEN b; int ok;
269 SV *name, *class;
270 8 ▶ if (el <= 3 || !(es[el-3]=='.' && es[el-2]=='p' && es[el-1]=='m'))
Branch: 4 branches
BranchTrueFalseTotal
if (el <= 3 || !(es[el-3]=='.' && es[el-2]=='p' && es[el-1]=='m')) 4 4 8
if (el <= 3 || !(es[el-3]=='.' && es[el-2]=='p' && es[el-1]=='m')) 4 0 4
if (el <= 3 || !(es[el-3]=='.' && es[el-2]=='p' && es[el-1]=='m')) 4 0 4
if (el <= 3 || !(es[el-3]=='.' && es[el-2]=='p' && es[el-1]=='m')) 0 4 4
271 4 continue;
272 4 ok = el - 3 > 0;
273 25 ▶ for (b = 0; b < el - 3; b++) if (!isWORDCHAR(es[b])) { ok = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (b = 0; b < el - 3; b++) if (!isWORDCHAR(es[b])) { ok = 0; break; } 0 21 21
for (b = 0; b < el - 3; b++) if (!isWORDCHAR(es[b])) { ok = 0; break; } 21 4 25
274 4 ▶ if (!ok) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) continue; 0 4 4
275 4 name = sv_2mortal(newSVpvn(es, el - 3));
276 4 class = sv_2mortal(newSVsv(ns_sv)); sv_catsv(class, name);
277 4 ▶ if (hv_exists_ent(seen, class, 0)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(seen, class, 0)) continue; 0 4 4
278 4 (void)hv_store_ent(seen, class, PUNK_SET_TRUE, 0);
279 4 ▶ if (!pc_class_can(aTHX_ SvPVX(class), SvCUR(class),
Branch: 1 branch
BranchTrueFalseTotal
if (!pc_class_can(aTHX_ SvPVX(class), SvCUR(class), 4 0 4
280 "_punk_model_meta")) {
281 /* by module name, not by the path the scan found: a
282 * path-form require of "lib/..." is @INC-searched too and
283 * misses when the entry that held the file is relative */
284 4 SV *req = pk_require_src(aTHX_ SvPV_nolen(class));
285 4 eval_sv(req, G_SCALAR | G_EVAL);
286 4 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 4 0 4
if (SvTRUE(ERRSV)) 0 4 4
287 0 ▶ croak("Punk: model %s failed to load: %s",
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk: model %s failed to load: %s", 0 0 0
croak("Punk: model %s failed to load: %s", 0 0 0
288 SvPV_nolen(class), SvPV_nolen(ERRSV));
289 }
290 4 av_push(out, newSVsv(name));
291 }
292 }
293 635 }
294  
295 /* Resolve the registered + auto-discovered model names to classes, checking
296 * each is a Punk::Model with a table. Fills $self->{models_compiled}. */
297 650 static void pc_compile_models(pTHX_ SV *self) {
298 650 HV *h = app_hv(aTHX_ self);
299 650 SV *caller = app_get(aTHX_ h, K_CALLER);
300 1300 ▶ SV *ns = sv_2mortal(newSVpvf("%s" PK_NS_MODEL, caller ? SvPV_nolen(caller) : ""));
Branch: 1 branch
BranchTrueFalseTotal
SV *ns = sv_2mortal(newSVpvf("%s" PK_NS_MODEL, caller ? SvPV_nolen(caller) : "")); 650 0 650
301 650 AV *names = (AV *)sv_2mortal((SV *)newAV());
302 650 AV *models = app_av(aTHX_ h, K_MODELS);
303 650 SV **autop = hv_fetchs(h, K_MODEL_AUTO, 0);
304 650 SSize_t i, n = av_len(models) + 1;
305 650 int have_named = (n > 0), autoflag;
306 HV *reg;
307  
308 669 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 19 650 669
309 19 SV **e = av_fetch(models, i, 0);
310 19 ▶ if (e && *e) av_push(names, newSVsv(*e));
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) av_push(names, newSVsv(*e)); 19 0 19
if (e && *e) av_push(names, newSVsv(*e)); 19 0 19
311 }
312 2 ▶ autoflag = (autop && *autop && SvOK(*autop)) ? (SvTRUE(*autop) ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
autoflag = (autop && *autop && SvOK(*autop)) ? (SvTRUE(*autop) ? 1 : 0) 2 0 2
autoflag = (autop && *autop && SvOK(*autop)) ? (SvTRUE(*autop) ? 1 : 0) 2 0 2
313 652 ▶ : !have_named;
Branch: 1 branch
BranchTrueFalseTotal
: !have_named; 2 648 650
314 650 ▶ if (autoflag) {
Branch: 1 branch
BranchTrueFalseTotal
if (autoflag) { 635 15 650
315 HV *stash;
316 635 STRLEN nsl2 = SvCUR(ns);
317 635 pc_discover_namespace(aTHX_ ns, names);
318 /* plus any ${ns}* already in the symbol table that isa Punk::Model -
319 * gv_stashpvn wants the bare package name, so the namespace's
320 * trailing '::' comes off first (with it, the lookup misses and
321 * in-memory model classes were silently never discovered) */
322 635 ▶ if (nsl2 >= 2 && SvPVX(ns)[nsl2 - 1] == ':'
Branch: 2 branches
BranchTrueFalseTotal
if (nsl2 >= 2 && SvPVX(ns)[nsl2 - 1] == ':' 635 0 635
if (nsl2 >= 2 && SvPVX(ns)[nsl2 - 1] == ':' 635 0 635
323 635 ▶ && SvPVX(ns)[nsl2 - 2] == ':')
Branch: 1 branch
BranchTrueFalseTotal
&& SvPVX(ns)[nsl2 - 2] == ':') 635 0 635
324 635 nsl2 -= 2;
325 635 stash = gv_stashpvn(SvPVX(ns), nsl2, 0);
326 635 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 10 625 635
327 10 HE *he; AV *syms = (AV *)sv_2mortal((SV *)newAV()); SSize_t si, sn;
328 10 hv_iterinit(stash);
329 32 ▶ while ((he = hv_iternext(stash))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(stash))) { 22 10 32
330 22 ▶ STRLEN kl; const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); 0 22 22
331 22 ▶ if (kl >= 3 && k[kl-1] == ':' && k[kl-2] == ':')
Branch: 3 branches
BranchTrueFalseTotal
if (kl >= 3 && k[kl-1] == ':' && k[kl-2] == ':') 22 0 22
if (kl >= 3 && k[kl-1] == ':' && k[kl-2] == ':') 22 0 22
if (kl >= 3 && k[kl-1] == ':' && k[kl-2] == ':') 22 0 22
332 22 av_push(syms, newSVpvn(k, kl - 2));
333 }
334 10 sn = av_len(syms) + 1;
335 10 ▶ if (sn > 1) sortsv(AvARRAY(syms), (STRLEN)sn, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (sn > 1) sortsv(AvARRAY(syms), (STRLEN)sn, Perl_sv_cmp); 5 5 10
336 32 ▶ for (si = 0; si < sn; si++) {
Branch: 1 branch
BranchTrueFalseTotal
for (si = 0; si < sn; si++) { 22 10 32
337 22 SV **symp = av_fetch(syms, si, 0);
338 22 SV *class = sv_2mortal(newSVsv(ns));
339 22 sv_catsv(class, *symp);
340 22 ▶ if (sv_derived_from(sv_2mortal(newSVsv(class)), PK_MODEL))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(sv_2mortal(newSVsv(class)), PK_MODEL)) 20 2 22
341 20 av_push(names, newSVsv(*symp));
342 }
343 }
344 }
345 650 ▶ if (av_len(names) < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(names) < 0) return; 626 24 650
346  
347 24 (void)pk_require_once(aTHX_ PK_MODEL, TRUE);
348 24 reg = newHV();
349 24 n = av_len(names) + 1;
350 67 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43 24 67
351 43 SV **np = av_fetch(names, i, 0);
352 43 STRLEN nl; const char *nm = SvPV_const(*np, nl);
353 SV *class; STRLEN cl; const char *cs; SV *meta;
354 43 ▶ if (hv_exists(reg, nm, (I32)nl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(reg, nm, (I32)nl)) continue; 5 38 43
355 38 ▶ if (memchr(nm, ':', nl))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(nm, ':', nl)) 1 37 38
356 1 class = sv_2mortal(newSVsv(*np));
357 37 else { class = sv_2mortal(newSVsv(ns)); sv_catsv(class, *np); }
358 38 cs = SvPV_const(class, cl);
359 38 ▶ if (!pc_class_can(aTHX_ cs, cl, "_punk_model_meta")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pc_class_can(aTHX_ cs, cl, "_punk_model_meta")) { 1 37 38
360 1 SV *req = pk_require_src(aTHX_ cs);
361 1 eval_sv(req, G_SCALAR | G_EVAL);
362 1 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 1 0 1
if (SvTRUE(ERRSV)) 0 1 1
363 0 ▶ croak("Punk: model '%s' (%s) failed to load: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: model '%s' (%s) failed to load: %s", 0 0 0
364 nm, cs, SvPV_nolen(ERRSV));
365 }
366 38 ▶ if (!sv_derived_from(class, PK_MODEL))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(class, PK_MODEL)) 0 38 38
367 0 croak("Punk: model '%s' (%s) does not use Punk::Model", nm, cs);
368 38 meta = pcx_call_meth(aTHX_ class, "_punk_model_meta", NULL, 0, 1);
369 38 ▶ if (!(meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV)) { 38 0 38
if (!(meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV)) { 38 0 38
if (!(meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV)) { 0 38 38
370 0 ▶ if (meta) SvREFCNT_dec(meta);
Branch: 1 branch
BranchTrueFalseTotal
if (meta) SvREFCNT_dec(meta); 0 0 0
371 0 croak("Punk: model '%s' (%s) declares no table", nm, cs);
372 }
373 38 SvREFCNT_dec(meta);
374 38 (void)hv_store(reg, nm, (I32)nl, newSVsv(class), 0);
375 }
376 24 (void)hv_stores(h, K_MODELS_C, newRV_noinc((SV *)reg));
377 }
378  
379 /* the backend options for a model class: its selected database, or {} for the
380 * (possibly unconfigured) default. */
381 28 static SV *pc_db_opts_for(pTHX_ SV *self, SV *classsv) {
382 28 HV *h = app_hv(aTHX_ self);
383 28 HV *dbs = app_hash(aTHX_ h, K_DATABASES);
384 28 SV *meta = pcx_call_meth(aTHX_ classsv, "_punk_model_meta", NULL, 0, 1);
385 28 const char *wname = K_DEFAULT; STRLEN wl = 7;
386 SV **opts; SV *ret;
387 28 ▶ if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 28 0 28
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 28 0 28
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 28 0 28
388 28 SV **d = hv_fetchs((HV *)SvRV(meta), "database", 0);
389 56 ▶ if (d && *d && SvTRUE(*d)) wname = SvPV_const(*d, wl);
Branch: 3 branches
BranchTrueFalseTotal
if (d && *d && SvTRUE(*d)) wname = SvPV_const(*d, wl); 28 0 28
if (d && *d && SvTRUE(*d)) wname = SvPV_const(*d, wl); 28 0 28
if (d && *d && SvTRUE(*d)) wname = SvPV_const(*d, wl); 28 0 28
390 }
391 28 opts = hv_fetch(dbs, wname, (I32)wl, 0);
392 28 ▶ if (opts && *opts && SvOK(*opts)) ret = newSVsv(*opts);
Branch: 3 branches
BranchTrueFalseTotal
if (opts && *opts && SvOK(*opts)) ret = newSVsv(*opts); 27 1 28
if (opts && *opts && SvOK(*opts)) ret = newSVsv(*opts); 27 0 27
if (opts && *opts && SvOK(*opts)) ret = newSVsv(*opts); 27 0 27
393 1 ▶ else if (!(wl == 7 && memEQ(wname, K_DEFAULT, 7)))
Branch: 2 branches
BranchTrueFalseTotal
else if (!(wl == 7 && memEQ(wname, K_DEFAULT, 7))) 0 1 1
else if (!(wl == 7 && memEQ(wname, K_DEFAULT, 7))) 0 0 0
394 1 croak("Punk: model '%s' selects database '%s', which no database "
395 "keyword configured", SvPV_nolen(classsv), wname);
396 0 else ret = newRV_noinc((SV *)newHV());
397 27 ▶ if (meta) SvREFCNT_dec(meta);
Branch: 1 branch
BranchTrueFalseTotal
if (meta) SvREFCNT_dec(meta); 27 0 27
398 27 return ret;
399 }
400  
401 /* ---- docs (Open::API::UI) into plain GET route records -------------------- */
402  
403 /* Materialise the `docs` records into GET route records (target a closure over
404 * the UI's finished-triplet thunk), croaking if a docs path collides with a
405 * mounted spec's operation. api_mounts is the compiled [{mount,prefix,len}]. */
406 661 static AV *pc_docs_routes(pTHX_ SV *self, AV *api_mounts) {
407 661 HV *h = app_hv(aTHX_ self);
408 661 AV *extra = (AV *)sv_2mortal((SV *)newAV());
409 661 SV **docsp = hv_fetchs(h, K_DOCS, 0);
410 661 ▶ AV *docs = (docsp && *docsp && SvROK(*docsp)) ? (AV *)SvRV(*docsp) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
AV *docs = (docsp && *docsp && SvROK(*docsp)) ? (AV *)SvRV(*docsp) : NULL; 11 650 661
AV *docs = (docsp && *docsp && SvROK(*docsp)) ? (AV *)SvRV(*docsp) : NULL; 11 0 11
AV *docs = (docsp && *docsp && SvROK(*docsp)) ? (AV *)SvRV(*docsp) : NULL; 11 0 11
411 661 ▶ SSize_t i, n = docs ? av_len(docs) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = docs ? av_len(docs) + 1 : 0; 11 650 661
412 661 ▶ SSize_t amn = api_mounts ? av_len(api_mounts) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t amn = api_mounts ? av_len(api_mounts) + 1 : 0; 661 0 661
413  
414 670 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 659 670
415 11 SV **rp = av_fetch(docs, i, 0);
416 11 ▶ HV *rec = (rp && *rp && SvROK(*rp)) ? (HV *)SvRV(*rp) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
HV *rec = (rp && *rp && SvROK(*rp)) ? (HV *)SvRV(*rp) : NULL; 11 0 11
HV *rec = (rp && *rp && SvROK(*rp)) ? (HV *)SvRV(*rp) : NULL; 11 0 11
HV *rec = (rp && *rp && SvROK(*rp)) ? (HV *)SvRV(*rp) : NULL; 11 0 11
417 SV *mount, *path, *opts, *api, *ui, *routes, **x;
418 AV *nav, *rl; SSize_t ri, rn; int nn, k; SV **nargv;
419 11 ▶ if (!rec) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!rec) continue; 0 11 11
420 11 ▶ x = hv_fetchs(rec, K_MOUNT, 0); mount = (x && *x && SvOK(*x)) ? *x : NULL;
Branch: 3 branches
BranchTrueFalseTotal
x = hv_fetchs(rec, K_MOUNT, 0); mount = (x && *x && SvOK(*x)) ? *x : NULL; 11 0 11
x = hv_fetchs(rec, K_MOUNT, 0); mount = (x && *x && SvOK(*x)) ? *x : NULL; 11 0 11
x = hv_fetchs(rec, K_MOUNT, 0); mount = (x && *x && SvOK(*x)) ? *x : NULL; 8 3 11
421 11 ▶ x = hv_fetchs(rec, K_PATH, 0); path = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
x = hv_fetchs(rec, K_PATH, 0); path = (x && *x) ? *x : &PL_sv_undef; 11 0 11
x = hv_fetchs(rec, K_PATH, 0); path = (x && *x) ? *x : &PL_sv_undef; 11 0 11
422 11 ▶ x = hv_fetchs(rec, K_OPTS, 0); opts = (x && *x) ? *x : NULL;
Branch: 2 branches
BranchTrueFalseTotal
x = hv_fetchs(rec, K_OPTS, 0); opts = (x && *x) ? *x : NULL; 11 0 11
x = hv_fetchs(rec, K_OPTS, 0); opts = (x && *x) ? *x : NULL; 11 0 11
423 11 ▶ if (!mount) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mount) { 3 8 11
424 HV *m0h; SV **mm;
425 3 ▶ if (amn < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (amn < 1) 1 2 3
426 1 croak("Punk: docs needs an api mount (none registered)");
427 2 ▶ if (amn > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (amn > 1) 0 2 2
428 0 croak("Punk: docs needs the mount named (several apis "
429 "are registered)");
430 2 m0h = (HV *)SvRV(*av_fetch(api_mounts, 0, 0));
431 2 mm = hv_fetchs(m0h, K_MOUNT, 0);
432 2 ▶ mount = (mm && *mm) ? *mm : NULL;
Branch: 2 branches
BranchTrueFalseTotal
mount = (mm && *mm) ? *mm : NULL; 2 0 2
mount = (mm && *mm) ? *mm : NULL; 2 0 2
433 }
434 10 ▶ if (!pk_require_once(aTHX_ PK_OA_UI, FALSE))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_require_once(aTHX_ PK_OA_UI, FALSE)) 0 10 10
435 0 ▶ croak("Punk: the docs keyword needs Open::API::UI - Open::API "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: the docs keyword needs Open::API::UI - Open::API " 0 0 0
436 "0.03+ with Template::Stencil and Markdown::Simple - and "
437 "it failed to load: %s", SvPV_nolen(ERRSV));
438 10 api = sv_2mortal(pcx_call_meth(aTHX_ mount, K_API, NULL, 0, 1));
439 10 nav = (AV *)sv_2mortal((SV *)newAV());
440 10 av_push(nav, newSVpvs(K_API)); av_push(nav, newSVsv(api));
441 10 av_push(nav, newSVpvs(K_PATH)); av_push(nav, newSVsv(path));
442 /* The docs UI does a double-submit: it reads the mirror cookie and
443 * sends the header. Hand it the real names when csrf is on, so
444 * try-it-out can write; 0 (off) when it is not. */
445 10 av_push(nav, newSVpvs(K_CSRF));
446 {
447 10 SV **cc = hv_fetchs(h, K_CSRF, 0);
448 10 ▶ if (cc && *cc && SvROK(*cc) && SvTYPE(SvRV(*cc)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (cc && *cc && SvROK(*cc) && SvTYPE(SvRV(*cc)) == SVt_PVHV) { 0 10 10
if (cc && *cc && SvROK(*cc) && SvTYPE(SvRV(*cc)) == SVt_PVHV) { 0 0 0
if (cc && *cc && SvROK(*cc) && SvTYPE(SvRV(*cc)) == SVt_PVHV) { 0 0 0
if (cc && *cc && SvROK(*cc) && SvTYPE(SvRV(*cc)) == SVt_PVHV) { 0 0 0
449 0 HV *cfg = (HV *)SvRV(*cc), *ui = newHV();
450 static const char *const pass[] = { "header", "cookie", NULL };
451 int pi;
452 0 ▶ for (pi = 0; pass[pi]; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pass[pi]; pi++) { 0 0 0
453 0 SV **e = hv_fetch(cfg, pass[pi], (I32)strlen(pass[pi]), 0);
454 0 ▶ if (e && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) 0 0 0
if (e && *e && SvOK(*e)) 0 0 0
if (e && *e && SvOK(*e)) 0 0 0
455 0 (void)hv_store(ui, pass[pi], (I32)strlen(pass[pi]),
456 newSVsv(*e), 0);
457 }
458 0 av_push(nav, newRV_noinc((SV *)ui));
459 }
460 10 else av_push(nav, newSViv(0));
461 }
462 10 ▶ if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) { 10 0 10
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) { 10 0 10
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) { 10 0 10
463 10 HV *oh = (HV *)SvRV(opts); HE *oe;
464 10 hv_iterinit(oh);
465 11 ▶ while ((oe = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((oe = hv_iternext(oh))) { 1 10 11
466 1 av_push(nav, newSVsv(hv_iterkeysv(oe)));
467 1 av_push(nav, newSVsv(hv_iterval(oh, oe)));
468 }
469 }
470 10 nn = (int)(av_len(nav) + 1);
471 10 ▶ Newx(nargv, nn > 0 ? nn : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(nargv, nn > 0 ? nn : 1, SV *); 10 0 10
472 72 ▶ for (k = 0; k < nn; k++) nargv[k] = *av_fetch(nav, k, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nn; k++) nargv[k] = *av_fetch(nav, k, 0); 62 10 72
473 10 ui = sv_2mortal(pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_OA_UI)),
474 "new", nargv, nn, 1));
475 10 Safefree(nargv);
476 10 routes = sv_2mortal(pcx_call_meth(aTHX_ ui, "routes", NULL, 0, 1));
477 10 ▶ rl = (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
rl = (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) 10 0 10
rl = (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) 10 0 10
478 20 ▶ ? (AV *)SvRV(routes) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(routes) : NULL; 10 0 10
479 10 ▶ rn = rl ? av_len(rl) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
rn = rl ? av_len(rl) + 1 : 0; 10 0 10
480 55 ▶ for (ri = 0; ri < rn; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < rn; ri++) { 46 9 55
481 46 SV **rr = av_fetch(rl, ri, 0);
482 46 ▶ HV *r = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
HV *r = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; 46 0 46
HV *r = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; 46 0 46
HV *r = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; 46 0 46
483 SV **rpathp, **respp; STRLEN rpl; const char *rps;
484 HV *route; SV *target; AV *dcap; SSize_t mi;
485 46 ▶ if (!r) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) continue; 0 46 46
486 46 rpathp = hv_fetchs(r, K_PATH, 0);
487 46 rps = SvPV_const(*rpathp, rpl);
488 91 ▶ for (mi = 0; mi < amn; mi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (mi = 0; mi < amn; mi++) { 46 45 91
489 46 HV *mh = (HV *)SvRV(*av_fetch(api_mounts, mi, 0));
490 46 SV **pfp = hv_fetchs(mh, K_PREFIX, 0);
491 46 SV **lp = hv_fetchs(mh, K_LEN, 0);
492 46 SV **mmp = hv_fetchs(mh, K_MOUNT, 0);
493 STRLEN pfl; const char *pf;
494 46 ▶ IV len = (lp && *lp) ? SvIV(*lp) : 0;
Branch: 2 branches
BranchTrueFalseTotal
IV len = (lp && *lp) ? SvIV(*lp) : 0; 46 0 46
IV len = (lp && *lp) ? SvIV(*lp) : 0; 46 0 46
495 46 SV *sub, *mapi, *firsthit = NULL; int mc;
496 92 ▶ if (pfp && *pfp && SvOK(*pfp)) pf = SvPV_const(*pfp, pfl);
Branch: 3 branches
BranchTrueFalseTotal
if (pfp && *pfp && SvOK(*pfp)) pf = SvPV_const(*pfp, pfl); 46 0 46
if (pfp && *pfp && SvOK(*pfp)) pf = SvPV_const(*pfp, pfl); 46 0 46
if (pfp && *pfp && SvOK(*pfp)) pf = SvPV_const(*pfp, pfl); 46 0 46
497 0 else { pf = ""; pfl = 0; }
498 46 ▶ if (!(rpl >= pfl && memEQ(rps, pf, pfl))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(rpl >= pfl && memEQ(rps, pf, pfl))) continue; 46 0 46
if (!(rpl >= pfl && memEQ(rps, pf, pfl))) continue; 40 6 46
499 6 ▶ sub = sv_2mortal((rpl > (STRLEN)len)
Branch: 1 branch
BranchTrueFalseTotal
sub = sv_2mortal((rpl > (STRLEN)len) 6 0 6
500 ? newSVpvn(rps + len, rpl - len)
501 : newSVpvs("/"));
502 6 ▶ mapi = sv_2mortal(pcx_call_meth(aTHX_ (mmp && *mmp) ? *mmp
Branch: 2 branches
BranchTrueFalseTotal
mapi = sv_2mortal(pcx_call_meth(aTHX_ (mmp && *mmp) ? *mmp 6 0 6
mapi = sv_2mortal(pcx_call_meth(aTHX_ (mmp && *mmp) ? *mmp 6 0 6
503 : &PL_sv_undef, K_API, NULL, 0, 1));
504 {
505 6 dSP;
506 6 ENTER; SAVETMPS;
507 6 ▶ PUSHMARK(SP); EXTEND(SP, 3);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 3); 0 6 6
PUSHMARK(SP); EXTEND(SP, 3); 0 6 6
508 6 PUSHs(mapi);
509 6 PUSHs(sv_2mortal(newSVpvs("GET")));
510 6 PUSHs(sub);
511 6 PUTBACK;
512 6 mc = call_method("match", G_ARRAY);
513 6 SPAGAIN;
514 6 ▶ if (mc > 0) firsthit = SvREFCNT_inc(SP[-mc + 1]);
Branch: 1 branch
BranchTrueFalseTotal
if (mc > 0) firsthit = SvREFCNT_inc(SP[-mc + 1]); 1 5 6
515 6 SP -= mc; PUTBACK;
516 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
517 }
518 6 ▶ if (mc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc > 0) { 1 5 6
519 1 SV *msg = sv_2mortal(newSVpvf(
520 "Punk: docs path '%.*s' is declared by the mounted "
521 "spec", (int)rpl, rps));
522 1 ▶ if (firsthit && SvOK(firsthit))
Branch: 2 branches
BranchTrueFalseTotal
if (firsthit && SvOK(firsthit)) 1 0 1
if (firsthit && SvOK(firsthit)) 1 0 1
523 1 sv_catpvf(msg, " (operation '%s')", SvPV_nolen(firsthit));
524 1 sv_catpvs(msg, "; choose another docs path");
525 1 ▶ if (firsthit) SvREFCNT_dec(firsthit);
Branch: 1 branch
BranchTrueFalseTotal
if (firsthit) SvREFCNT_dec(firsthit); 1 0 1
526 1 croak("%s", SvPV_nolen(msg));
527 }
528 5 ▶ if (firsthit) SvREFCNT_dec(firsthit);
Branch: 1 branch
BranchTrueFalseTotal
if (firsthit) SvREFCNT_dec(firsthit); 0 5 5
529 }
530 45 respp = hv_fetchs(r, K_RESPONSE, 0);
531 45 dcap = newAV();
532 45 ▶ av_push(dcap, newSVsv((respp && *respp) ? *respp : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dcap, newSVsv((respp && *respp) ? *respp : &PL_sv_undef)); 45 0 45
av_push(dcap, newSVsv((respp && *respp) ? *respp : &PL_sv_undef)); 45 0 45
533 45 target = punk_closure(aTHX_ pc_docs_cb, dcap);
534 45 route = newHV();
535 45 (void)hv_stores(route, K_METHOD, newSVpvs("GET"));
536 45 (void)hv_stores(route, K_PATH, newSVsv(*rpathp));
537 45 (void)hv_stores(route, K_TARGET, target);
538 45 (void)hv_stores(route, K_GUARDS, newRV_noinc((SV *)newAV()));
539 45 av_push(extra, newRV_noinc((SV *)route));
540 }
541 }
542 659 return extra;
543 }
544  
545 /* ---- longest-prefix ordering ---------------------------------------------- */
546  
547 20 static IV pc_len_of(pTHX_ SV *el) {
548 20 ▶ if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { 20 0 20
if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { 20 0 20
549 20 SV **l = hv_fetchs((HV *)SvRV(el), K_LEN, 0);
550 20 ▶ if (l && *l && SvOK(*l)) return SvIV(*l);
Branch: 3 branches
BranchTrueFalseTotal
if (l && *l && SvOK(*l)) return SvIV(*l); 20 0 20
if (l && *l && SvOK(*l)) return SvIV(*l); 20 0 20
if (l && *l && SvOK(*l)) return SvIV(*l); 20 0 20
551 }
552 0 return 0;
553 }
554 10 static I32 pc_cmp_len_desc(pTHX_ SV *a, SV *b) {
555 10 return (I32)(pc_len_of(aTHX_ b) - pc_len_of(aTHX_ a));
556 }
557  
558 /* ---- the nonblocking Future chain ($ret->then(done, fail)) ---------------- *
559 * capture [ $c, $on_error, $method, \@after ]; the deliver/handle-error work
560 * is punk_context.h / punk_serve.h C, exactly the blocking path's. */
561  
562 /* $app->env - the config's env, else PUNK_ENV, else production. Mortal. */
563 129 static SV *pc_app_env(pTHX_ SV *self) {
564 129 dSP;
565 int count;
566 SV *out;
567 129 ENTER; SAVETMPS;
568 129 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(self); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(self); PUTBACK; 0 129 129
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(self); PUTBACK; 0 129 129
569 129 count = call_method("env", G_SCALAR);
570 129 SPAGAIN;
571 129 ▶ out = count > 0 ? newSVsv(POPs) : newSVpvs("production");
Branch: 1 branch
BranchTrueFalseTotal
out = count > 0 ? newSVsv(POPs) : newSVpvs("production"); 129 0 129
572 129 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 129 0 129
573 129 return sv_2mortal(out);
574 }
575  
576 37 static int pc_is_head(pTHX_ SV *methsv) {
577 74 ▶ STRLEN ml; const char *m = SvOK(methsv) ? SvPV_const(methsv, ml) : "GET";
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ml; const char *m = SvOK(methsv) ? SvPV_const(methsv, ml) : "GET"; 37 0 37
578 37 ▶ if (!SvOK(methsv)) ml = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(methsv)) ml = 3; 0 37 37
579 37 ▶ return (ml == 4 && memEQ(m, "HEAD", 4));
Branch: 2 branches
BranchTrueFalseTotal
return (ml == 4 && memEQ(m, "HEAD", 4)); 1 36 37
return (ml == 4 && memEQ(m, "HEAD", 4)); 1 0 1
580 }
581 37 static AV *pc_after_of(pTHX_ SV *ref) {
582 37 ▶ return (ref && SvROK(ref) && SvTYPE(SvRV(ref)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
return (ref && SvROK(ref) && SvTYPE(SvRV(ref)) == SVt_PVAV) 37 0 37
return (ref && SvROK(ref) && SvTYPE(SvRV(ref)) == SVt_PVAV) 37 0 37
583 74 ▶ ? (AV *)SvRV(ref) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(ref) : NULL; 37 0 37
584 }
585  
586 /* A `then` callback must hand back a FUTURE, not the value it computed.
587 * Future's own sequencing enforces that - "Expected __ANON__(...) to return
588 * a Future" - and older releases enforce it strictly, so returning a raw
589 * triplet here fails on any perl whose Future predates the relaxation.
590 * Reported by CPAN Testers against 0.16 on perls 5.20, 5.22 and 5.24; it
591 * passed locally only because this box's Future no longer checks.
592 *
593 * The wrap is conditional because the two future flavours differ on this:
594 * `Future->done($v)` is a class method that mints a completed future, while
595 * Punk::Future's `done` is an instance method (its class-level constructor
596 * is done_future) and its own `then` accepts the value directly. So the
597 * capture carries a flag set at chain time from `$ret->isa('Future')`, and
598 * only that path is wrapped. Slot 4. */
599 6 static int pc_ff_flag(pTHX_ AV *cap, int slot) {
600 6 SV **wp = av_fetch(cap, slot, 0);
601 6 ▶ return (wp && *wp && SvTRUE(*wp)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (wp && *wp && SvTRUE(*wp)) ? 1 : 0; 6 0 6
return (wp && *wp && SvTRUE(*wp)) ? 1 : 0; 6 0 6
return (wp && *wp && SvTRUE(*wp)) ? 1 : 0; 6 0 6
602 }
603  
604 6 static SV *pc_ff_wrap(pTHX_ int wrap, SV *resp) {
605 SV *argv[1], *f;
606 6 ▶ if (!wrap) return resp; /* Punk::Future path */
Branch: 1 branch
BranchTrueFalseTotal
if (!wrap) return resp; /* Punk::Future path */ 0 6 6
607 6 argv[0] = sv_2mortal(resp);
608 6 f = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Future")), "done", argv, 1, 1);
609 6 ▶ return f ? f : newSVsv(argv[0]);
Branch: 1 branch
BranchTrueFalseTotal
return f ? f : newSVsv(argv[0]); 0 6 6
610 }
611  
612 /* The streaming callbacks answer through the responder and have no value to
613 * carry, but they are still `then` callbacks and still owe a future back. */
614 0 static SV *pc_ff_done_empty(pTHX) {
615 0 SV *f = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Future")), "done",
616 NULL, 0, 1);
617 0 ▶ return f ? f : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return f ? f : newSV(0); 0 0 0
618 }
619  
620 XS_INTERNAL(pc_ff_done_cb);
621 4 XS_INTERNAL(pc_ff_done_cb) {
622 4 dXSARGS;
623 4 AV *cap = punk_clos_cap(aTHX_ cv);
624 4 SV *c = *av_fetch(cap, 0, 0);
625 4 int hd = pc_is_head(aTHX_ *av_fetch(cap, 2, 0));
626 4 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 3, 0));
627 4 ▶ SV *val = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *val = items > 0 ? ST(0) : &PL_sv_undef; 4 0 4
628 4 ST(0) = sv_2mortal(pc_ff_wrap(aTHX_ pc_ff_flag(aTHX_ cap, 4),
629 punk_deliver(aTHX_ c, val, hd, aft)));
630 4 XSRETURN(1);
631 }
632  
633 /* The Punk::Future path, fused.
634 *
635 * Chaining a handler's future through `then` builds three closures per
636 * request: a done callback, a fail callback, and the one `then` makes inside
637 * itself to drive the derived future. Each is a fresh CV - punk_closure is
638 * newXS - plus a capture AV. When the handler's future is one of ours none of
639 * that indirection buys anything: this is a single reaction that reads the
640 * settled state itself and settles the derived future directly, so one
641 * closure and one AV do the work of three.
642 *
643 * Capture: [ c, on_error, method, after, derived ]. No wrap flag - the wrap
644 * exists to satisfy CPAN Future's "a then callback must return a Future"
645 * rule, and nothing here goes through `then`.
646 *
647 * The source future arrives as $_[0], which is what PFR_ON_READY passes. */
648 XS_INTERNAL(pc_ff_both_cb);
649 5 XS_INTERNAL(pc_ff_both_cb) {
650 5 dXSARGS;
651 5 AV *cap = punk_clos_cap(aTHX_ cv);
652 5 SV *c = *av_fetch(cap, 0, 0);
653 5 SV *oe = *av_fetch(cap, 1, 0);
654 5 int hd = pc_is_head(aTHX_ *av_fetch(cap, 2, 0));
655 5 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 3, 0));
656 5 SV *derived = *av_fetch(cap, 4, 0);
657 5 ▶ SV *src = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *src = items > 0 ? ST(0) : NULL; 5 0 5
658 5 ▶ punk_future *sf = src ? pf_of(aTHX_ src) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
punk_future *sf = src ? pf_of(aTHX_ src) : NULL; 5 0 5
659 SV *resp;
660 5 ▶ if (!sf || !derived) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!sf || !derived) XSRETURN_EMPTY; 5 0 5
if (!sf || !derived) XSRETURN_EMPTY; 0 5 5
661 5 ▶ if (sf->state == PF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (sf->state == PF_DONE) { 5 0 5
662 5 ▶ SV **v = sf->vals ? av_fetch(sf->vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **v = sf->vals ? av_fetch(sf->vals, 0, 0) : NULL; 5 0 5
663 5 ▶ resp = punk_deliver(aTHX_ c, v && *v ? *v : &PL_sv_undef, hd, aft);
Branch: 2 branches
BranchTrueFalseTotal
resp = punk_deliver(aTHX_ c, v && *v ? *v : &PL_sv_undef, hd, aft); 5 0 5
resp = punk_deliver(aTHX_ c, v && *v ? *v : &PL_sv_undef, hd, aft); 5 0 5
664 }
665 0 ▶ else if (sf->state == PF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sf->state == PF_FAILED) { 0 0 0
666 0 ▶ SV **e = sf->vals ? av_fetch(sf->vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = sf->vals ? av_fetch(sf->vals, 0, 0) : NULL; 0 0 0
667 0 ▶ SV *err = (e && *e && SvOK(*e)) ? *e : sv_2mortal(newSVpvs("failed"));
Branch: 3 branches
BranchTrueFalseTotal
SV *err = (e && *e && SvOK(*e)) ? *e : sv_2mortal(newSVpvs("failed")); 0 0 0
SV *err = (e && *e && SvOK(*e)) ? *e : sv_2mortal(newSVpvs("failed")); 0 0 0
SV *err = (e && *e && SvOK(*e)) ? *e : sv_2mortal(newSVpvs("failed")); 0 0 0
668 0 ▶ SV *handled = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *handled = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL); 0 0 0
669 0 resp = punk_deliver(aTHX_ c, handled, hd, aft);
670 0 SvREFCNT_dec(handled);
671 }
672 else { /* cancelled: nothing was asked for, so nothing is delivered */
673 0 pf_settle(aTHX_ pf_of(aTHX_ derived), derived, PF_CANCELLED, NULL);
674 0 XSRETURN_EMPTY;
675 }
676 {
677 5 AV *out = newAV();
678 5 av_push(out, resp); /* takes the reference punk_deliver made */
679 5 pf_settle(aTHX_ pf_of(aTHX_ derived), derived, PF_DONE, out);
680 }
681 5 XSRETURN_EMPTY;
682 }
683  
684 XS_INTERNAL(pc_ff_fail_cb);
685 2 XS_INTERNAL(pc_ff_fail_cb) {
686 2 dXSARGS;
687 2 AV *cap = punk_clos_cap(aTHX_ cv);
688 2 SV *c = *av_fetch(cap, 0, 0);
689 2 SV *oe = *av_fetch(cap, 1, 0);
690 2 int hd = pc_is_head(aTHX_ *av_fetch(cap, 2, 0));
691 2 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 3, 0));
692 2 ▶ SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed"));
Branch: 2 branches
BranchTrueFalseTotal
SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed")); 2 0 2
SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed")); 2 0 2
693 2 ▶ SV *resp = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *resp = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL); 0 2 2
694 /* the same contract as the done side: a `then` callback returns a
695 * future, not the value. A failure that answers with a 500 triplet is
696 * still a SUCCESSFUL sequencing step - the error was handled - so this
697 * wraps with done, not fail. */
698 2 ST(0) = sv_2mortal(pc_ff_wrap(aTHX_ pc_ff_flag(aTHX_ cap, 4),
699 punk_deliver(aTHX_ c, resp, hd, aft)));
700 2 SvREFCNT_dec(resp);
701 2 XSRETURN(1);
702 }
703  
704 /* ---- the streaming variant: a Future behind a psgi.streaming responder ----
705 *
706 * A raw Future handed back as the PSGI response is a shape only Hyperman
707 * understands, and anything standing between Punk and the server - the Lint
708 * and AccessLog middleware plackup adds in its development default, or this
709 * application's own `middleware` - dies on it. When the server advertises
710 * psgi.streaming, the future rides inside the standard delayed response
711 * instead: a coderef every middleware passes through, resolving to the same
712 * triplet through the same deliver path. */
713  
714 /* Hand `resp` to the responder - or, when the future resolved to a streaming
715 * coderef of its own (an SSE body, say), hand the responder to it. */
716 3 static void pc_respond(pTHX_ SV *responder, SV *resp) {
717 3 dSP;
718 3 ▶ int stream = SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV;
Branch: 2 branches
BranchTrueFalseTotal
int stream = SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV; 3 0 3
int stream = SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV; 0 3 3
719 3 ENTER; SAVETMPS;
720 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
721 3 ▶ XPUSHs(stream ? responder : resp);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(stream ? responder : resp); 0 3 3
XPUSHs(stream ? responder : resp); 0 3 3
722 3 PUTBACK;
723 3 ▶ call_sv(stream ? resp : responder, G_VOID | G_DISCARD | G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
call_sv(stream ? resp : responder, G_VOID | G_DISCARD | G_EVAL); 0 3 3
724 3 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 3 0 3
if (SvTRUE(ERRSV)) 0 3 3
725 0 ▶ warn("Punk: responder failed: %" SVf, SVfARG(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk: responder failed: %" SVf, SVfARG(ERRSV)); 0 0 0
726 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 3 3
727 3 }
728  
729 /* cap: [c, on_error, method, after, responder] - the pc_ff_* layout plus the
730 * responder the server handed the delayed response. */
731 XS_INTERNAL(pc_ffs_done_cb);
732 0 XS_INTERNAL(pc_ffs_done_cb) {
733 0 dXSARGS;
734 0 AV *cap = punk_clos_cap(aTHX_ cv);
735 0 SV *c = *av_fetch(cap, 0, 0);
736 0 int hd = pc_is_head(aTHX_ *av_fetch(cap, 2, 0));
737 0 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 3, 0));
738 0 SV *rsp = *av_fetch(cap, 4, 0);
739 0 ▶ SV *val = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *val = items > 0 ? ST(0) : &PL_sv_undef; 0 0 0
740 0 SV *trip = punk_deliver(aTHX_ c, val, hd, aft);
741 0 pc_respond(aTHX_ rsp, trip);
742 0 SvREFCNT_dec(trip);
743 0 ▶ if (pc_ff_flag(aTHX_ cap, 5)) { ST(0) = sv_2mortal(pc_ff_done_empty(aTHX)); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (pc_ff_flag(aTHX_ cap, 5)) { ST(0) = sv_2mortal(pc_ff_done_empty(aTHX)); XSRETURN(1); } 0 0 0
744 0 XSRETURN(0);
745 }
746  
747 XS_INTERNAL(pc_ffs_fail_cb);
748 0 XS_INTERNAL(pc_ffs_fail_cb) {
749 0 dXSARGS;
750 0 AV *cap = punk_clos_cap(aTHX_ cv);
751 0 SV *c = *av_fetch(cap, 0, 0);
752 0 SV *oe = *av_fetch(cap, 1, 0);
753 0 int hd = pc_is_head(aTHX_ *av_fetch(cap, 2, 0));
754 0 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 3, 0));
755 0 SV *rsp = *av_fetch(cap, 4, 0);
756 0 ▶ SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed"));
Branch: 2 branches
BranchTrueFalseTotal
SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed")); 0 0 0
SV *err = (items > 0 && SvOK(ST(0))) ? ST(0) : sv_2mortal(newSVpvs("failed")); 0 0 0
757 0 ▶ SV *resp = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *resp = punk_handle_error(aTHX_ c, err, SvOK(oe) ? oe : NULL); 0 0 0
758 0 SV *trip = punk_deliver(aTHX_ c, resp, hd, aft);
759 0 pc_respond(aTHX_ rsp, trip);
760 0 SvREFCNT_dec(trip);
761 0 SvREFCNT_dec(resp);
762 0 ▶ if (pc_ff_flag(aTHX_ cap, 5)) { ST(0) = sv_2mortal(pc_ff_done_empty(aTHX)); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (pc_ff_flag(aTHX_ cap, 5)) { ST(0) = sv_2mortal(pc_ff_done_empty(aTHX)); XSRETURN(1); } 0 0 0
763 0 XSRETURN(0);
764 }
765  
766 /* The delayed response itself. cap: [c, ret, on_error, method, after];
767 * called by the server (or the innermost middleware) with the responder.
768 *
769 * On a live Hyperman worker loop the then-chain is enough - the loop is what
770 * resolves the future, and the responder fires when it does. Without one
771 * (a foreign nonblocking server, a synthetic test env) nothing would ever
772 * drive a pending future, so it is awaited inline - the same semantics the
773 * blocking branch has always had, delivered through the responder. */
774 XS_INTERNAL(pc_ffs_cb);
775 3 XS_INTERNAL(pc_ffs_cb) {
776 3 dXSARGS;
777 3 AV *cap = punk_clos_cap(aTHX_ cv);
778 3 SV *c = *av_fetch(cap, 0, 0);
779 3 SV *ret = *av_fetch(cap, 1, 0);
780 3 ▶ SV *responder = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *responder = items > 0 ? ST(0) : &PL_sv_undef; 3 0 3
781 3 const hm_abi *A = punk_hm(aTHX);
782 3 ▶ int ffs_wrap = (SvROK(ret) && sv_derived_from(ret, "Future"));
Branch: 2 branches
BranchTrueFalseTotal
int ffs_wrap = (SvROK(ret) && sv_derived_from(ret, "Future")); 3 0 3
int ffs_wrap = (SvROK(ret) && sv_derived_from(ret, "Future")); 0 3 3
783 3 ▶ if (A && A->cur_loop(aTHX)) {
Branch: 2 branches
BranchTrueFalseTotal
if (A && A->cur_loop(aTHX)) { 3 0 3
if (A && A->cur_loop(aTHX)) { 0 3 3
784 SV *done, *fail, *argv[2], *chained;
785 AV *caps[2];
786 int j;
787 0 ▶ for (j = 0; j < 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 2; j++) { 0 0 0
788 0 AV *icap = caps[j] = newAV();
789 0 SV **oe = av_fetch(cap, 2, 0), **af = av_fetch(cap, 4, 0);
790 0 av_push(icap, newSVsv(c));
791 0 ▶ av_push(icap, (oe && SvOK(*oe)) ? newSVsv(*oe) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(icap, (oe && SvOK(*oe)) ? newSVsv(*oe) : newSV(0)); 0 0 0
av_push(icap, (oe && SvOK(*oe)) ? newSVsv(*oe) : newSV(0)); 0 0 0
792 0 av_push(icap, newSVsv(*av_fetch(cap, 3, 0)));
793 0 ▶ av_push(icap, (af && SvOK(*af)) ? newSVsv(*af)
Branch: 2 branches
BranchTrueFalseTotal
av_push(icap, (af && SvOK(*af)) ? newSVsv(*af) 0 0 0
av_push(icap, (af && SvOK(*af)) ? newSVsv(*af) 0 0 0
794 : newRV_noinc((SV *)newAV()));
795 0 av_push(icap, newSVsv(responder));
796 0 av_push(icap, newSViv(ffs_wrap));
797 }
798 0 done = sv_2mortal(punk_closure(aTHX_ pc_ffs_done_cb, caps[0]));
799 0 fail = sv_2mortal(punk_closure(aTHX_ pc_ffs_fail_cb, caps[1]));
800 0 argv[0] = done; argv[1] = fail;
801 0 chained = pcx_call_meth(aTHX_ ret, "then", argv, 2, 1);
802 /* the pending chain is held by what will resolve it */
803 0 ▶ if (chained) SvREFCNT_dec(chained);
Branch: 1 branch
BranchTrueFalseTotal
if (chained) SvREFCNT_dec(chained); 0 0 0
804 }
805 else {
806 3 dSP; int count, died, hd;
807 SV *got, *resp, *trip;
808 3 SV **oe = av_fetch(cap, 2, 0);
809 3 AV *aft = pc_after_of(aTHX_ *av_fetch(cap, 4, 0));
810 3 ENTER; SAVETMPS;
811 3 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ret); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ret); PUTBACK; 0 3 3
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ret); PUTBACK; 0 3 3
812 3 count = call_method("get", G_SCALAR | G_EVAL);
813 3 SPAGAIN;
814 3 ▶ got = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
got = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; 3 0 3
815 3 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 3 0 3
816 3 ▶ died = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; 3 0 3
817 3 hd = pc_is_head(aTHX_ *av_fetch(cap, 3, 0));
818 2 ▶ resp = died ? punk_handle_error(aTHX_ c, ERRSV,
Branch: 2 branches
BranchTrueFalseTotal
resp = died ? punk_handle_error(aTHX_ c, ERRSV, 1 0 1
resp = died ? punk_handle_error(aTHX_ c, ERRSV, 1 0 1
819 1 ▶ (oe && SvOK(*oe)) ? *oe : NULL)
Branch: 1 branch
BranchTrueFalseTotal
(oe && SvOK(*oe)) ? *oe : NULL) 0 1 1
820 3 ▶ : punk_coerce(aTHX_ c, got);
Branch: 1 branch
BranchTrueFalseTotal
: punk_coerce(aTHX_ c, got); 1 2 3
821 3 trip = punk_deliver(aTHX_ c, resp, hd, aft);
822 3 pc_respond(aTHX_ responder, trip);
823 3 SvREFCNT_dec(trip);
824 3 SvREFCNT_dec(resp);
825 3 ▶ if (got != &PL_sv_undef) SvREFCNT_dec(got);
Branch: 1 branch
BranchTrueFalseTotal
if (got != &PL_sv_undef) SvREFCNT_dec(got); 2 1 3
826 }
827 3 XSRETURN(0);
828 }
829  
830 #endif /* PUNK_COMPILE_H */
« xs/headers.xs ↑ Summary include/punk/punk_ua.h »