include/pmcp/pmcp_clos.h

filter: Statementstmt 100% 76 / 76 Branchbran 75.5% 68 / 90 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.7% 144 / 166 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_prompt.h ↑ Summary include/pmcp/pmcp_res.h »
1 /* pmcp_clos.h - closures, and every call this dist makes into Perl.
2 *
3 * Needs: pmcp.h, pmcp_compat.h.
4 *
5 * The closure is Punk-Feed's pfeed_closure, which is Punk's private
6 * punk_closure: a CV built with newXS carrying an AV of captured SVs in
7 * PERL_MAGIC_ext. Punk does not export it, so a plugin carries its own.
8 *
9 * Every call into Perl goes through pmcp_call. The rules it keeps, in one place
10 * so no later header has to remember them:
11 *
12 * - With an err slot, the call runs under G_EVAL. A die comes back as a new
13 * SV in *err (+1, the caller owns it) and the value and scope stacks are as
14 * they were. Never JMPENV_PUSH: catching the jump is not the problem,
15 * unwinding the stack bookkeeping is, and only G_EVAL does that (the stack
16 * corruption recorded in plan_maat_mcp 02).
17 * - Without one, a die propagates to the caller's caller. Only for calls
18 * where dying is the intended answer, such as a bad schema at boot.
19 * - The result is copied into a plain SV inside the ENTER/LEAVE window and
20 * handed back +1. A value mortalised inside the window is freed by its
21 * FREETMPS before the caller reads it (the logging trap in project_punk).
22 * - A call into Perl can grow the value stack, and a grown stack is
23 * reallocated. pmcp_call re-reads SP itself, but an XSUB that calls it and
24 * then pushes must re-derive its own: SP = PL_stack_base + ax - 1, not
25 * SPAGAIN, which in a PPCODE leaves SP above the arguments.
26 */
27  
28 #ifndef PMCP_CLOS_H
29 #define PMCP_CLOS_H
30  
31 /* ---- closures ------------------------------------------------------------ */
32  
33 typedef struct { AV *cap; } pmcp_clos_t;
34  
35 906 static int pmcp_clos_free(pTHX_ SV *sv, MAGIC *mg)
36 {
37 906 pmcp_clos_t *c = (pmcp_clos_t *)mg->mg_ptr;
38 PERL_UNUSED_ARG(sv);
39 906 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 906 0 906
40 906 ▶ if (c->cap) SvREFCNT_dec((SV *)c->cap);
Branch: 1 branch
BranchTrueFalseTotal
if (c->cap) SvREFCNT_dec((SV *)c->cap); 906 0 906
41 906 Safefree(c);
42 }
43 906 return 0;
44 }
45  
46 static MGVTBL pmcp_clos_vtbl = { NULL, NULL, NULL, NULL, pmcp_clos_free,
47 NULL, NULL, NULL };
48  
49 /* A code ref whose body is `body` and whose capture is `cap`. Takes ownership
50 * of cap. Returns +1. */
51 1771 static SV *pmcp_closure(pTHX_ XSUBADDR_t body, AV *cap)
52 {
53 1771 CV *cv = (CV *)newXS(NULL, body, (char *)__FILE__);
54 pmcp_clos_t *c;
55  
56 1771 Newxz(c, 1, pmcp_clos_t);
57 1771 c->cap = cap;
58 1771 sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &pmcp_clos_vtbl, (char *)c, 0);
59 1771 return newRV_noinc((SV *)cv);
60 }
61  
62 /* One capture slot, borrowed, or NULL when the slot is empty. */
63 1893 static SV *pmcp_cap(pTHX_ CV *cv, SSize_t i)
64 {
65 1893 MAGIC *mg = pmcp_mg_findext(aTHX_ (SV *)cv, PERL_MAGIC_ext, &pmcp_clos_vtbl);
66 1893 ▶ AV *cap = mg ? ((pmcp_clos_t *)mg->mg_ptr)->cap : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *cap = mg ? ((pmcp_clos_t *)mg->mg_ptr)->cap : NULL; 1893 0 1893
67 SV **e;
68 1893 ▶ if (!cap) croak(PMCP_DIST ": a closure lost its capture");
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) croak(PMCP_DIST ": a closure lost its capture"); 0 1893 1893
69 1893 e = av_fetch(cap, i, 0);
70 1893 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 1893 0 1893
return (e && *e) ? *e : NULL; 1893 0 1893
71 }
72  
73 /* The body t/02 builds a closure from: returns its first capture. */
74 2 XS_INTERNAL(pmcp_clos_echo)
75 {
76 2 dXSARGS;
77 2 SV *v = pmcp_cap(aTHX_ cv, 0);
78 PERL_UNUSED_VAR(items);
79 2 ▶ ST(0) = v ? v : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = v ? v : &PL_sv_undef; 2 0 2
80 2 XSRETURN(1);
81 }
82  
83 /* ---- calls into Perl ----------------------------------------------------- */
84  
85 /* The one call site. Exactly one of meth and code is set: meth calls
86 * $inv->$meth(@argv), code calls $code->(@argv) with inv, when given, as the
87 * first argument. flags is G_SCALAR or G_LIST. G_SCALAR gives the value, or
88 * undef when there was none. G_LIST gives an array ref of the values.
89 *
90 * argv may point into the Perl stack (an XSUB passing &ST(n)). The EXTEND
91 * below can reallocate that stack, so the pointers are copied out first.
92 *
93 * Returns +1. On a caught die returns NULL and sets *err (+1). */
94 10319 static SV *pmcp_call(pTHX_ SV *inv, const char *meth, SV *code,
95 SV **argv, int argc, I32 flags, SV **err)
96 {
97 10319 dSP;
98 I32 count, i;
99 10319 SV *ret = NULL;
100 SV *small[8];
101 10319 SV **args = small;
102 /* G_WANT, never a bare bit test: G_SCALAR is 2 and G_LIST is 3, so
103 * flags & G_LIST is true for a scalar call. */
104 10319 ▶ I32 want = ((flags & G_WANT) == G_LIST) ? G_LIST : G_SCALAR;
Branch: 1 branch
BranchTrueFalseTotal
I32 want = ((flags & G_WANT) == G_LIST) ? G_LIST : G_SCALAR; 10 10309 10319
105  
106 10319 ▶ if (err) *err = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = NULL; 3602 6717 10319
107 10319 ENTER; SAVETMPS;
108 10319 ▶ if (argc > (int)(sizeof small / sizeof small[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (argc > (int)(sizeof small / sizeof small[0])) { 2 10317 10319
109 2 Newx(args, argc, SV *);
110 2 SAVEFREEPV(args);
111 }
112 75330 ▶ for (i = 0; i < argc; i++) args[i] = argv[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) args[i] = argv[i]; 65011 10319 75330
113 10319 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10319 10319
114 10319 ▶ EXTEND(SP, argc + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, argc + 1); 10319 0 10319
EXTEND(SP, argc + 1); 0 10319 10319
115 10319 ▶ if (inv) PUSHs(inv);
Branch: 1 branch
BranchTrueFalseTotal
if (inv) PUSHs(inv); 7470 2849 10319
116 75330 ▶ for (i = 0; i < argc; i++) PUSHs(args[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) PUSHs(args[i]); 65011 10319 75330
117 10319 PUTBACK;
118  
119 10319 ▶ if (code) count = call_sv(code, want | (err ? G_EVAL : 0));
Branch: 2 branches
BranchTrueFalseTotal
if (code) count = call_sv(code, want | (err ? G_EVAL : 0)); 2849 7470 10319
if (code) count = call_sv(code, want | (err ? G_EVAL : 0)); 2680 169 2849
120 7470 ▶ else count = call_method(meth, want | (err ? G_EVAL : 0));
Branch: 1 branch
BranchTrueFalseTotal
else count = call_method(meth, want | (err ? G_EVAL : 0)); 922 6548 7470
121 10318 SPAGAIN;
122  
123 10318 ▶ if (err && SvTRUE(ERRSV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (err && SvTRUE(ERRSV)) { 3602 6716 10318
if (err && SvTRUE(ERRSV)) { 3602 0 3602
if (err && SvTRUE(ERRSV)) { 44 3558 3602
124 87 ▶ while (count-- > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
while (count-- > 0) (void)POPs; 43 44 87
125 44 ▶ *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
*err = newSVsv(ERRSV); 44 0 44
126 }
127 10274 ▶ else if (want == G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
else if (want == G_LIST) { 9 10265 10274
128 9 AV *out = newAV();
129 9 ▶ if (count > 0) av_extend(out, count - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) av_extend(out, count - 1); 9 0 9
130 300018 ▶ for (i = count - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) { 300009 9 300018
131 300009 SV *top = POPs;
132 300009 (void)av_store(out, i, newSVsv(top));
133 }
134 9 ret = newRV_noinc((SV *)out);
135 }
136 else {
137 10265 ▶ ret = count > 0 ? newSVsv(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
ret = count > 0 ? newSVsv(POPs) : newSV(0); 10265 0 10265
138 10265 ▶ while (--count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
while (--count > 0) (void)POPs; 0 10265 10265
139 }
140  
141 10318 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 10318 0 10318
142 10318 return ret;
143 }
144  
145 /* $inv->$meth(@argv). See pmcp_call for flags, err and ownership. */
146 7470 static SV *pmcp_call_method(pTHX_ SV *inv, const char *meth, SV **argv,
147 int argc, I32 flags, SV **err)
148 {
149 7470 return pmcp_call(aTHX_ inv, meth, NULL, argv, argc, flags, err);
150 }
151  
152 /* $code->(@argv). See pmcp_call for flags, err and ownership. */
153 2841 static SV *pmcp_call_sv(pTHX_ SV *code, SV **argv, int argc, I32 flags,
154 SV **err)
155 {
156 2841 return pmcp_call(aTHX_ NULL, NULL, code, argv, argc, flags, err);
157 }
158  
159 /* require Module, under G_EVAL. Returns 1, or 0 with *err set (+1) when err is
160 * given. A string eval, because require needs a bareword to map :: to /. */
161 118 static int pmcp_require(pTHX_ const char *module, SV **err)
162 {
163 118 SV *src = newSVpvf("require %s; 1", module);
164 int ok;
165  
166 118 ▶ if (err) *err = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = NULL; 118 0 118
167 118 ENTER; SAVETMPS;
168 118 (void)eval_sv(src, G_DISCARD | G_EVAL);
169 118 SvREFCNT_dec(src);
170 118 ▶ ok = !SvTRUE(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
ok = !SvTRUE(ERRSV); 118 0 118
171 118 ▶ if (!ok && err) *err = newSVsv(ERRSV);
Branch: 3 branches
BranchTrueFalseTotal
if (!ok && err) *err = newSVsv(ERRSV); 2 116 118
if (!ok && err) *err = newSVsv(ERRSV); 2 0 2
if (!ok && err) *err = newSVsv(ERRSV); 2 0 2
172 118 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 118 118
173 118 return ok;
174 }
175  
176 /* ---- predicates ---------------------------------------------------------- */
177  
178 8282 static int pmcp_is_hash(SV *sv)
179 {
180 8282 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 7967 315 8282
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 7937 30 7967
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 7912 25 7937
181 }
182  
183 2259 static int pmcp_is_array(SV *sv)
184 {
185 2259 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 2258 1 2259
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 2214 44 2258
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 1364 850 2214
186 }
187  
188 2044 static int pmcp_is_code(SV *sv)
189 {
190 2044 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 2044 0 2044
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 969 1075 2044
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 960 9 969
191 }
192  
193 #endif /* PMCP_CLOS_H */
« include/pmcp/pmcp_prompt.h ↑ Summary include/pmcp/pmcp_res.h »