include/punk/punk_import.h

filter: Statementstmt 96.9% 129 / 133 Branchbran 64.0% 73 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.7% 202 / 247 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/future.xs ↑ Summary xs/reqid.xs »
1 /* punk_import.h - `use Punk` and the DSL it exports, in C.
2 *
3 * Every keyword is a magic CV carrying the application registrar it forwards
4 * to (the punk_closure pattern from punk_static.h, the same one Punk::Model's
5 * DSL uses), rather than a Perl closure over $app.
6 *
7 * This is a boot-time path - import runs once per application class - so the
8 * point is not speed. It is that the DSL surface is now described in one table
9 * instead of twenty-five near-identical closures, and that adding a keyword
10 * means adding a row.
11 *
12 * Include after punk_static.h (punk_closure, punk_clos_cap).
13 */
14  
15 #ifndef PUNK_IMPORT_H
16 #define PUNK_IMPORT_H
17  
18 /* How a keyword relates to the registrar method behind it. */
19 enum {
20 PKW_FWD = 0, /* $app->$method(@_) - most of them */
21 PKW_NOARG, /* $app->$method() - to_app ignores its class */
22 PKW_SELF, /* $app - punk_app */
23 PKW_ROUTE, /* $app->route($HTTP, @_, []) - get/post/... */
24 PKW_HELPER /* $app->helper(@_, $caller) - helpers know their owner */
25 };
26  
27 /* capture slots */
28 enum { PKI_APP = 0, PKI_KIND, PKI_NAME, PKI_CALLER };
29  
30 /* The one body behind every exported keyword. Which of the five shapes it
31 * takes is in the capture, so there is a single place where a DSL call becomes
32 * a registrar call. */
33 XS_INTERNAL(pki_kw_cb);
34 63220 XS_INTERNAL(pki_kw_cb) {
35 63220 dXSARGS;
36 63220 AV *cap = punk_clos_cap(aTHX_ cv);
37 SV *app, *name;
38 IV kind;
39 int i, count;
40 63220 I32 want = GIMME_V;
41  
42 63220 ▶ if (!cap) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) XSRETURN_EMPTY; 0 63220 63220
43 63220 app = *av_fetch(cap, PKI_APP, 0);
44 63220 kind = SvIV(*av_fetch(cap, PKI_KIND, 0));
45 63220 name = *av_fetch(cap, PKI_NAME, 0);
46  
47 63220 ▶ if (kind == PKW_SELF) { /* punk_app: the registrar itself */
Branch: 1 branch
BranchTrueFalseTotal
if (kind == PKW_SELF) { /* punk_app: the registrar itself */ 230 62990 63220
48 230 ST(0) = sv_2mortal(newSVsv(app));
49 230 XSRETURN(1);
50 }
51  
52 62990 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 62990 62990
53 62990 switch (kind) {
54 676 case PKW_NOARG:
55 676 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 676 676
56 676 PUSHs(app);
57 676 break;
58  
59 61093 case PKW_ROUTE:
60 /* $app->route($METHOD, $path, $target, [], $opts?) - the empty
61 * guard list is what `under` fills in for a scoped route; the
62 * optional trailing hashref is the route options */
63 61093 ▶ if (items > 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) 1 61092 61093
64 1 croak("Punk: a route takes a path, a target and at most "
65 "one options hashref");
66 61092 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 61092 61092
67 61092 PUSHs(app);
68 61092 PUSHs(name);
69 61092 ▶ PUSHs(items > 0 ? ST(0) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(items > 0 ? ST(0) : &PL_sv_undef); 61092 0 61092
70 61092 ▶ PUSHs(items > 1 ? ST(1) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(items > 1 ? ST(1) : &PL_sv_undef); 61092 0 61092
71 61092 mPUSHs(newRV_noinc((SV *)newAV()));
72 61092 ▶ if (items > 2) PUSHs(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) PUSHs(ST(2)); 208 60884 61092
73 61092 break;
74  
75 6 case PKW_HELPER:
76 /* the owning class rides along so a collision can name both */
77 6 ▶ EXTEND(SP, items + 2);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items + 2); 6 0 6
EXTEND(SP, items + 2); 0 6 6
78 6 PUSHs(app);
79 18 ▶ for (i = 0; i < items; i++) PUSHs(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) PUSHs(ST(i)); 12 6 18
80 6 PUSHs(*av_fetch(cap, PKI_CALLER, 0));
81 6 break;
82  
83 1215 default: /* PKW_FWD */
84 1215 ▶ EXTEND(SP, items + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items + 1); 1215 0 1215
EXTEND(SP, items + 1); 0 1215 1215
85 1215 PUSHs(app);
86 3952 ▶ for (i = 0; i < items; i++) PUSHs(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) PUSHs(ST(i)); 2737 1215 3952
87 1215 break;
88 }
89 62989 PUTBACK;
90  
91 /* the caller's context is propagated: `my $s = under ...` wants the scope
92 * object, and a chaining keyword wants its $self back */
93 64880 ▶ count = call_method(kind == PKW_ROUTE ? "route"
Branch: 2 branches
BranchTrueFalseTotal
count = call_method(kind == PKW_ROUTE ? "route" 1897 61092 62989
count = call_method(kind == PKW_ROUTE ? "route" 1891 6 1897
94 : kind == PKW_HELPER ? "helper"
95 : SvPV_nolen(name),
96 want == G_VOID ? G_VOID : want);
97 62563 SPAGAIN;
98 62563 ▶ if (want == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_VOID) XSRETURN_EMPTY; 61959 604 62563
99  
100 /* The results sit above the arguments we were called with, but XSRETURN
101 * returns the slots at ax - so they have to be copied down. Into mortals
102 * first, and through a scratch vector, because the two regions can overlap
103 * and a direct move would clobber a value not yet read. */
104 604 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 604 0 604
105 SV **tmp;
106 int j;
107 604 Newx(tmp, count, SV *);
108 1208 ▶ for (j = 0; j < count; j++) tmp[j] = sv_2mortal(newSVsv(SP[j - count + 1]));
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) tmp[j] = sv_2mortal(newSVsv(SP[j - count + 1])); 604 604 1208
109 1208 ▶ for (j = 0; j < count; j++) ST(j) = tmp[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) ST(j) = tmp[j]; 604 604 1208
110 604 Safefree(tmp);
111 }
112 604 XSRETURN(count);
113 }
114  
115 /* The DSL, as a table. A keyword is a row: its name, the registrar method it
116 * reaches, and which of the five shapes it takes. */
117 typedef struct pki_kw {
118 const char *kw; /* what the application writes */
119 const char *method; /* the Punk::App method behind it (or the HTTP verb) */
120 int kind;
121 } pki_kw;
122  
123 static const pki_kw PKI_KEYWORDS[] = {
124 /* the verbs: one route each, with the method they mean */
125 { "get", "GET", PKW_ROUTE },
126 { "post", "POST", PKW_ROUTE },
127 { "put", "PUT", PKW_ROUTE },
128 { "patch", "PATCH", PKW_ROUTE },
129 { "del", "DELETE", PKW_ROUTE },
130 { "any", "ANY", PKW_ROUTE },
131  
132 /* everything that forwards straight through */
133 { "under", "under", PKW_FWD },
134 { "api", "api", PKW_FWD },
135 { "websocket", "websocket", PKW_FWD },
136 { "sse", "sse", PKW_FWD },
137 { "session", "session", PKW_FWD },
138 { "ua", "ua", PKW_FWD },
139 { "csrf", "csrf", PKW_FWD },
140 { "cors", "cors", PKW_FWD },
141 { "headers", "headers", PKW_FWD },
142 { "proxy", "proxy", PKW_FWD },
143 { "max_body", "max_body", PKW_FWD },
144 { "auth", "auth", PKW_FWD },
145 { "auth_guard", "auth_guard", PKW_FWD },
146 { "logging", "logging", PKW_FWD },
147 { "docs", "docs", PKW_FWD },
148 { "static", "static", PKW_FWD },
149 { "markdown", "markdown", PKW_FWD },
150 { "mount", "mount", PKW_FWD },
151 { "views", "views", PKW_FWD },
152 { "cache", "cache", PKW_FWD },
153 { "config", "config", PKW_FWD },
154 { "secret", "secret", PKW_FWD },
155 { "database", "database", PKW_FWD },
156 { "model", "model_class", PKW_FWD },
157 { "hook", "hook", PKW_FWD },
158 { "rate_limit", "rate_limit", PKW_FWD },
159 { "middleware", "middleware", PKW_FWD },
160 { "on_error", "on_error", PKW_FWD },
161 { "on_not_found", "on_not_found", PKW_FWD },
162 { "plugin", "plugin", PKW_FWD },
163  
164 { "helper", "helper", PKW_HELPER },
165 { "to_app", "compile", PKW_NOARG },
166 { "upload_dir", "upload_dir", PKW_FWD },
167 { "host", "host", PKW_FWD },
168 { "favicon", "favicon", PKW_FWD },
169 { "punk_app", NULL, PKW_SELF },
170 { NULL, NULL, 0 }
171 };
172  
173 /* ---- plugin keywords ------------------------------------------------------
174 *
175 * A plugin that wants its own declaration keyword (`task`, `queue`, `cron`...)
176 * calls $app->install_kw($name => $code) rather than assigning to a glob in the
177 * application class. The keyword it gets is the same shape as the ones above -
178 * a magic CV, named for the class it lands in, installed from C - so a plugin
179 * never needs `no strict 'refs'` and every keyword in an application arrived
180 * through one door.
181 *
182 * They are ordinary CVs, not calls lifted into the BEGIN phase: lifting wants
183 * Devel::CallParser and perl 5.14, and Punk claims 5.10. A CV installed at
184 * import time is compile-time-visible to every later line on every perl, which
185 * is what a declaration keyword needs. */
186  
187 /* forward @_ to the plugin's code and return what it returns, in the caller's
188 * context - `my $q = queue();` is a keyword call too */
189 XS_INTERNAL(pki_pkw_cb);
190 15 XS_INTERNAL(pki_pkw_cb) {
191 15 dXSARGS;
192 15 AV *cap = punk_clos_cap(aTHX_ cv);
193 15 ▶ SV **code = cap ? av_fetch(cap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **code = cap ? av_fetch(cap, 0, 0) : NULL; 15 0 15
194 15 I32 want = GIMME_V;
195 int count, i;
196  
197 15 ▶ if (!code) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!code) XSRETURN_EMPTY; 0 15 15
198  
199 15 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15 15
200 15 ▶ EXTEND(SP, items);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items); 15 0 15
EXTEND(SP, items); 0 15 15
201 38 ▶ for (i = 0; i < items; i++) PUSHs(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) PUSHs(ST(i)); 23 15 38
202 15 PUTBACK;
203 15 count = call_sv(*code, want == G_VOID ? G_VOID : want);
204 15 SPAGAIN;
205 15 ▶ if (want == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_VOID) XSRETURN_EMPTY; 12 3 15
206  
207 /* the results sit above the arguments; copy them down to ax through a
208 * scratch vector, since the two regions can overlap (see pki_kw_cb) */
209 3 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 3 0 3
210 SV **tmp;
211 int j;
212 3 Newx(tmp, count, SV *);
213 8 ▶ for (j = 0; j < count; j++) tmp[j] = sv_2mortal(newSVsv(SP[j - count + 1]));
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) tmp[j] = sv_2mortal(newSVsv(SP[j - count + 1])); 5 3 8
214 8 ▶ for (j = 0; j < count; j++) ST(j) = tmp[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) ST(j) = tmp[j]; 5 3 8
215 3 Safefree(tmp);
216 }
217 3 XSRETURN(count);
218 }
219  
220 /* is this name part of the core DSL? installing over `get` or `plugin` would
221 * break the application in a way no error message would explain */
222 72 static int pki_is_dsl(const char *name) {
223 int i;
224 3012 ▶ for (i = 0; PKI_KEYWORDS[i].kw; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PKI_KEYWORDS[i].kw; i++) 2942 70 3012
225 2942 ▶ if (strEQ(name, PKI_KEYWORDS[i].kw)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, PKI_KEYWORDS[i].kw)) return 1; 2 2940 2942
226 70 return 0;
227 }
228  
229 /* Install one plugin keyword into an application class. Idempotent by CV
230 * identity: a keyword already carrying this same code is left alone, which is
231 * what a plugin installing from both its import and its register does. */
232 45 static void pki_install_kw(pTHX_ SV *caller, SV *name, SV *code) {
233 STRLEN cl, nl;
234 45 ▶ const char *cs = SvPV_const(caller, cl);
Branch: 1 branch
BranchTrueFalseTotal
const char *cs = SvPV_const(caller, cl); 45 0 45
235 45 const char *ns = SvPV_const(name, nl);
236 45 const char *full = form("%.*s::%.*s", (int)cl, cs, (int)nl, ns);
237 45 CV *old = get_cv(full, 0);
238 AV *cap;
239  
240 45 ▶ if (old) {
Branch: 1 branch
BranchTrueFalseTotal
if (old) { 0 45 45
241 0 AV *ocap = punk_clos_cap(aTHX_ old);
242 0 ▶ SV **oc = ocap ? av_fetch(ocap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **oc = ocap ? av_fetch(ocap, 0, 0) : NULL; 0 0 0
243 0 ▶ if (oc && SvROK(*oc) && SvROK(code) && SvRV(*oc) == SvRV(code)) return;
Branch: 4 branches
BranchTrueFalseTotal
if (oc && SvROK(*oc) && SvROK(code) && SvRV(*oc) == SvRV(code)) return; 0 0 0
if (oc && SvROK(*oc) && SvROK(code) && SvRV(*oc) == SvRV(code)) return; 0 0 0
if (oc && SvROK(*oc) && SvROK(code) && SvRV(*oc) == SvRV(code)) return; 0 0 0
if (oc && SvROK(*oc) && SvROK(code) && SvRV(*oc) == SvRV(code)) return; 0 0 0
244 }
245 45 cap = newAV();
246 45 av_push(cap, newSVsv(code));
247 45 (void)punk_closure_named(aTHX_ full, pki_pkw_cb, cap);
248 }
249  
250 /* One application registrar per class, cached in %Punk::APPS the way the Perl
251 * import did - a second `use Punk` in the same package must not start a second
252 * application. */
253 1111 static SV *pki_app_for(pTHX_ SV *caller, int *fresh) {
254 1111 HV *apps = get_hv("Punk::APPS", GV_ADD);
255 1111 HE *he = hv_fetch_ent(apps, caller, 0, 0);
256 SV *app;
257 1111 ▶ if (fresh) *fresh = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fresh) *fresh = 0; 1111 0 1111
258 1111 ▶ if (he && HeVAL(he) && SvROK(HeVAL(he))) return HeVAL(he);
Branch: 3 branches
BranchTrueFalseTotal
if (he && HeVAL(he) && SvROK(HeVAL(he))) return HeVAL(he); 1 1110 1111
if (he && HeVAL(he) && SvROK(HeVAL(he))) return HeVAL(he); 1 0 1
if (he && HeVAL(he) && SvROK(HeVAL(he))) return HeVAL(he); 1 0 1
259 1110 ▶ if (fresh) *fresh = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (fresh) *fresh = 1; 1110 0 1110
260  
261 {
262 1110 dSP;
263 int count;
264 1110 ENTER; SAVETMPS;
265 1110 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1110 1110
266 1110 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 1110 1110
267 1110 mPUSHs(newSVpvs("Punk::App"));
268 1110 mPUSHs(newSVpvs("caller"));
269 1110 PUSHs(caller);
270 1110 PUTBACK;
271 1110 count = call_method("new", G_SCALAR);
272 1110 SPAGAIN;
273 1110 ▶ app = count > 0 ? newSVsv(POPs) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
app = count > 0 ? newSVsv(POPs) : &PL_sv_undef; 1110 0 1110
274 1110 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1110 0 1110
275 }
276 1110 ▶ if (!SvROK(app))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(app)) 0 1110 1110
277 0 croak("Punk: Punk::App->new returned no registrar");
278 1110 (void)hv_store_ent(apps, caller, app, 0);
279 1110 return app;
280 }
281  
282 /* strict and warnings, exactly as `use strict; use warnings;` would: both are
283 * plain import calls that set hint bits in the scope being compiled, and
284 * calling them from here reaches the same scope a Perl import would. */
285 2222 static void pki_pragma(pTHX_ const char *pragma) {
286 2222 dSP;
287 2222 ENTER; SAVETMPS;
288 2222 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2222 2222
289 2222 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2222 2222
290 2222 mPUSHs(newSVpv(pragma, 0));
291 2222 PUTBACK;
292 2222 (void)call_method("import", G_VOID | G_DISCARD);
293 2222 SPAGAIN;
294 2222 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2222 0 2222
295 2222 }
296  
297 /* Install the whole DSL into the calling package. */
298 1111 static void pki_export(pTHX_ SV *caller) {
299 1111 int fresh = 0;
300 1111 SV *app = pki_app_for(aTHX_ caller, &fresh);
301 STRLEN cl;
302 1111 const char *cs = SvPV_const(caller, cl);
303 int i;
304  
305 /* A second `use Punk` in the same package extends that application rather
306 * than starting another - so the keywords are already installed, and
307 * reinstalling them would only warn about redefining every one. */
308 1111 ▶ if (!fresh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) return; 1 1110 1111
309  
310 47730 ▶ for (i = 0; PKI_KEYWORDS[i].kw; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PKI_KEYWORDS[i].kw; i++) { 46620 1110 47730
311 46620 const pki_kw *k = &PKI_KEYWORDS[i];
312 46620 AV *cap = newAV();
313 SV *code;
314 46620 av_push(cap, newSVsv(app));
315 46620 av_push(cap, newSViv(k->kind));
316 46620 ▶ av_push(cap, k->method ? newSVpv(k->method, 0) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, k->method ? newSVpv(k->method, 0) : newSV(0)); 45510 1110 46620
317 46620 ▶ if (k->kind == PKW_HELPER) av_push(cap, newSVsv(caller));
Branch: 1 branch
BranchTrueFalseTotal
if (k->kind == PKW_HELPER) av_push(cap, newSVsv(caller)); 1110 45510 46620
318 46620 code = punk_closure(aTHX_ pki_kw_cb, cap);
319 {
320 46620 GV *gv = gv_fetchpvn_flags(form("%.*s::%s", (int)cl, cs, k->kw),
321 cl + 2 + strlen(k->kw), GV_ADD, SVt_PVCV);
322 46620 sv_setsv((SV *)gv, code);
323 }
324 46620 SvREFCNT_dec(code);
325 }
326 }
327  
328 #endif /* PUNK_IMPORT_H */
« xs/future.xs ↑ Summary xs/reqid.xs »