lib/Data/Checks.xs

filter: Statementstmt 90.1% 101 / 112 Branchbran 73.4% 47 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.0% 148 / 176 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/ckcall_constfold.c.inc ↑ Summary t/test.xs »
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2024 -- leonerd@leonerd.org.uk
5 */
6  
7 #define PERL_NO_GET_CONTEXT
8  
9 #include "EXTERN.h"
10 #include "perl.h"
11 #include "XSUB.h"
12  
13 #define HAVE_DATA_CHECKS_IMPL
14 #include "DataChecks.h"
15  
16 struct DataChecks_Checker
17 {
18 CV *cv;
19 struct Constraint *constraint;
20 SV *arg0;
21 SV *assertmess;
22 };
23  
24 #include "perl-backcompat.c.inc"
25  
26 #include "newOP_CUSTOM.c.inc"
27 #include "optree-additions.c.inc"
28  
29 #include "constraints.h"
30  
31 #define warn_deprecated(...) Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED), __VA_ARGS__)
32  
33 30 static struct DataChecks_Checker *S_DataChecks_make_checkdata(pTHX_ SV *checkspec)
34 {
35 30 HV *stash = NULL;
36 30 CV *checkcv = NULL;
37 30 struct Constraint *constraint = NULL;
38  
39 30 ▶ if(SvROK(checkspec) && SvOBJECT(SvRV(checkspec)))
Branch: 2 branches
BranchTrueFalseTotal
if(SvROK(checkspec) && SvOBJECT(SvRV(checkspec))) 29 1 30
if(SvROK(checkspec) && SvOBJECT(SvRV(checkspec))) 28 1 29
40 28 stash = SvSTASH(SvRV(checkspec));
41 2 ▶ else if(SvPOK(checkspec) && (stash = gv_stashsv(checkspec, GV_NOADD_NOINIT)))
Branch: 2 branches
BranchTrueFalseTotal
else if(SvPOK(checkspec) && (stash = gv_stashsv(checkspec, GV_NOADD_NOINIT))) 1 1 2
else if(SvPOK(checkspec) && (stash = gv_stashsv(checkspec, GV_NOADD_NOINIT))) 1 0 1
42 ; /* checkspec is package name */
43 1 ▶ else if(SvROK(checkspec) && !SvOBJECT(SvRV(checkspec)) && SvTYPE(SvRV(checkspec)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
else if(SvROK(checkspec) && !SvOBJECT(SvRV(checkspec)) && SvTYPE(SvRV(checkspec)) == SVt_PVCV) { 1 0 1
else if(SvROK(checkspec) && !SvOBJECT(SvRV(checkspec)) && SvTYPE(SvRV(checkspec)) == SVt_PVCV) { 1 0 1
else if(SvROK(checkspec) && !SvOBJECT(SvRV(checkspec)) && SvTYPE(SvRV(checkspec)) == SVt_PVCV) { 1 0 1
44 /* checkspec is a code reference */
45 1 warn_deprecated("Using a CODE reference as a constraint checker is deprecated");
46 1 checkcv = (CV *)SvREFCNT_inc(SvRV(checkspec));
47 1 checkspec = NULL;
48 }
49 else
50 0 croak("Expected the checker expression to yield an object or code reference or package name; got %" SVf " instead",
51 SVfARG(checkspec));
52  
53 30 ▶ if(stash && sv_isa(checkspec, "Data::Checks::Constraint")) {
Branch: 2 branches
BranchTrueFalseTotal
if(stash && sv_isa(checkspec, "Data::Checks::Constraint")) { 29 1 30
if(stash && sv_isa(checkspec, "Data::Checks::Constraint")) { 27 2 29
54 27 constraint = (struct Constraint *)SvPVX(SvRV(checkspec));
55 /* arg0 will store checkspec pointer, thus ensuring this SV is retained */
56 }
57 3 ▶ else if(!checkcv) {
Branch: 1 branch
BranchTrueFalseTotal
else if(!checkcv) { 2 1 3
58 GV *methgv;
59 2 ▶ if(!(methgv = gv_fetchmeth_pv(stash, "check", -1, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if(!(methgv = gv_fetchmeth_pv(stash, "check", -1, 0))) 0 2 2
60 0 croak("Expected that the checker expression can ->check");
61 2 ▶ if(!GvCV(methgv))
Branch: 1 branch
BranchTrueFalseTotal
if(!GvCV(methgv)) 0 2 2
62 0 croak("Expected that methgv has a GvCV");
63 2 checkcv = (CV *)SvREFCNT_inc(GvCV(methgv));
64 }
65  
66 struct DataChecks_Checker *checker;
67 30 Newx(checker, 1, struct DataChecks_Checker);
68  
69 30 *checker = (struct DataChecks_Checker){
70 .cv = checkcv,
71 .constraint = constraint,
72 30 .arg0 = SvREFCNT_inc(checkspec),
73 };
74  
75 30 return checker;
76 }
77  
78 5 static void S_DataChecks_free_checkdata(pTHX_ struct DataChecks_Checker *checker)
79 {
80 5 ▶ if(checker->assertmess)
Branch: 1 branch
BranchTrueFalseTotal
if(checker->assertmess) 5 0 5
81 5 SvREFCNT_dec(checker->assertmess);
82  
83 5 SvREFCNT_dec(checker->cv);
84  
85 5 ▶ if(checker->arg0)
Branch: 1 branch
BranchTrueFalseTotal
if(checker->arg0) 4 1 5
86 4 SvREFCNT_dec(checker->arg0);
87  
88 5 Safefree(checker);
89 5 }
90  
91 30 static void S_DataChecks_gen_assertmess(pTHX_ struct DataChecks_Checker *checker, SV *name, SV *constraint)
92 {
93 30 ▶ if(!constraint || !SvOK(constraint)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!constraint || !SvOK(constraint)) { 30 0 30
if(!constraint || !SvOK(constraint)) { 27 3 30
94 27 ▶ if(checker->constraint)
Branch: 1 branch
BranchTrueFalseTotal
if(checker->constraint) 27 0 27
95 27 constraint = stringify_constraint(checker->constraint);
96 0 ▶ else if(checker->arg0) {
Branch: 1 branch
BranchTrueFalseTotal
else if(checker->arg0) { 0 0 0
97 0 constraint = sv_newmortal();
98 0 sv_copypv(constraint, checker->arg0);
99 }
100 else
101 0 croak("gen_assertmess requires a constraint name if the constraint is a CODE reference");
102 }
103 30 checker->assertmess = newSVpvf("%" SVf " requires a value satisfying %" SVf,
104 SVfARG(name), SVfARG(constraint));
105 30 }
106  
107 static XOP xop_invoke_checkfunc;
108 5 static OP *pp_invoke_checkfunc(pTHX)
109 {
110 5 dSP;
111 5 struct Constraint *constraint = (struct Constraint *)cUNOP_AUX->op_aux;
112 5 SV *value = POPs;
113  
114 5 ▶ PUSHs(boolSV((*constraint->func)(aTHX_ constraint, value)));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(boolSV((*constraint->func)(aTHX_ constraint, value))); 3 2 5
115  
116 5 RETURN;
117 }
118  
119 #define make_checkop(checker, argop) S_DataChecks_make_checkop(aTHX_ checker, argop)
120 6 static OP *S_DataChecks_make_checkop(pTHX_ struct DataChecks_Checker *checker, OP *argop)
121 {
122 6 ▶ if(checker->constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if(checker->constraint) { 3 3 6
123 3 return newUNOP_AUX_CUSTOM(&pp_invoke_checkfunc, OPf_WANT_SCALAR,
124 argop,
125 (UNOP_AUX_item *)checker->constraint);
126 }
127  
128 3 ▶ if(checker->cv && checker->arg0)
Branch: 2 branches
BranchTrueFalseTotal
if(checker->cv && checker->arg0) 3 0 3
if(checker->cv && checker->arg0) 2 1 3
129 /* checkcv($checker, ARGOP) ... */
130 2 return newLISTOPn(OP_ENTERSUB, OPf_WANT_SCALAR|OPf_STACKED,
131 newSVOP(OP_CONST, 0, SvREFCNT_inc(checker->arg0)),
132 argop,
133 newSVOP(OP_CONST, 0, SvREFCNT_inc(checker->cv)),
134 NULL);
135  
136 1 ▶ if(checker->cv)
Branch: 1 branch
BranchTrueFalseTotal
if(checker->cv) 1 0 1
137 /* checkcv(ARGOP) ... */
138 1 return newLISTOPn(OP_ENTERSUB, OPf_WANT_SCALAR|OPf_STACKED,
139 argop,
140 newSVOP(OP_CONST, 0, SvREFCNT_inc(checker->cv)),
141 NULL);
142  
143 0 croak("ARGH unsure how to make checkop");
144 }
145  
146 6 static OP *S_DataChecks_make_assertop(pTHX_ struct DataChecks_Checker *checker, U32 flags, OP *argop)
147 {
148 6 U32 want = flags & OPf_WANT; flags &= ~OPf_WANT;
149 6 bool want_void = (want == OPf_WANT_VOID);
150  
151 6 ▶ if(flags)
Branch: 1 branch
BranchTrueFalseTotal
if(flags) 0 6 6
152 0 croak("TODO: make_assertop with flags 0x%x", flags);
153  
154 6 OP *o = newLOGOP(OP_OR, 0,
155 make_checkop(checker, argop),
156 /* ... or die MESSAGE */
157 newLISTOPn(OP_DIE, 0,
158 newSVOP(OP_CONST, 0, SvREFCNT_inc(checker->assertmess)),
159 NULL));
160  
161 6 ▶ if(want_void) {
Branch: 1 branch
BranchTrueFalseTotal
if(want_void) { 1 5 6
162 /* Wrap it in a full enter/leave pair so it unstacks correctly */
163 1 o->op_flags |= OPf_PARENS;
164 1 o = op_contextualize(op_scope(o), OPf_WANT_VOID);
165 }
166  
167 6 return o;
168 }
169  
170 0 static OP *S_DataChecks_make_assertop_v0(pTHX_ struct DataChecks_Checker *checker, OP *argop)
171 {
172 0 return S_DataChecks_make_assertop(aTHX_ checker, 0, argop);
173 }
174  
175 172 static bool S_DataChecks_check_value(pTHX_ struct DataChecks_Checker *checker, SV *value)
176 {
177 172 ▶ if(checker->constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if(checker->constraint) { 160 12 172
178 160 return (*checker->constraint->func)(aTHX_ checker->constraint, value);
179 }
180  
181 12 dSP;
182  
183 12 ENTER;
184 12 SAVETMPS;
185  
186 12 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 12 12
187 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
188 12 ▶ if(checker->arg0)
Branch: 1 branch
BranchTrueFalseTotal
if(checker->arg0) 8 4 12
189 8 PUSHs(sv_mortalcopy(checker->arg0));
190 12 PUSHs(value); /* Yes we're pushing the SV itself */
191 12 PUTBACK;
192  
193 12 call_sv((SV *)checker->cv, G_SCALAR);
194  
195 12 SPAGAIN;
196  
197 12 bool ok = SvTRUEx(POPs);
198  
199 12 PUTBACK;
200  
201 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 12 0 12
202 12 LEAVE;
203  
204 12 return ok;
205 }
206  
207 8 static void S_DataChecks_assert_value(pTHX_ struct DataChecks_Checker *checker, SV *value)
208 {
209 8 ▶ if(check_value(checker, value))
Branch: 1 branch
BranchTrueFalseTotal
if(check_value(checker, value)) 4 4 8
210 4 return;
211  
212 4 croak_sv(checker->assertmess);
213 }
214  
215 MODULE = Data::Checks PACKAGE = Data::Checks::Debug
216  
217 void stringify_constraint(SV *sv)
218 PPCODE:
219 /* Prevent XSUB from double-mortalising it */
220 46 PUSHs(stringify_constraint_sv(extract_constraint(sv)));
221 46 XSRETURN(1);
222  
223 MODULE = Data::Checks PACKAGE = Data::Checks::Constraint
224  
225 void DESTROY(SV *self)
226 CODE:
227 {
228 189 struct Constraint *c = (struct Constraint *)SvPVX(SvRV(self));
229 346 ▶ for(int i = c->n - 1; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for(int i = c->n - 1; i >= 0; i--) 157 189 346
230 157 SvREFCNT_dec(c->args[i]);
231 }
232  
233 bool check(SV *self, SV *value)
234 CODE:
235 2 struct Constraint *c = (struct Constraint *)SvPVX(SvRV(self));
236 2 RETVAL = (c->func)(aTHX_ c, value);
237 OUTPUT:
238 RETVAL
239  
240 MODULE = Data::Checks PACKAGE = Data::Checks
241  
242 BOOT:
243 13 sv_setiv(*hv_fetchs(PL_modglobal, "Data::Checks/ABIVERSION_MIN", GV_ADD), 0);
244 13 sv_setiv(*hv_fetchs(PL_modglobal, "Data::Checks/ABIVERSION_MAX", GV_ADD), DATACHECKS_ABI_VERSION);
245  
246 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/make_checkdata()@0", GV_ADD),
247 PTR2UV(&S_DataChecks_make_checkdata));
248 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/free_checkdata()@0", GV_ADD),
249 PTR2UV(&S_DataChecks_free_checkdata));
250 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/gen_assertmess()@0", GV_ADD),
251 PTR2UV(&S_DataChecks_gen_assertmess));
252 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/make_assertop()@0", GV_ADD),
253 PTR2UV(&S_DataChecks_make_assertop_v0));
254 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/make_assertop()@1", GV_ADD),
255 PTR2UV(&S_DataChecks_make_assertop));
256 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/check_value()@0", GV_ADD),
257 PTR2UV(&S_DataChecks_check_value));
258 13 sv_setuv(*hv_fetchs(PL_modglobal, "Data::Checks/assert_value()@0", GV_ADD),
259 PTR2UV(&S_DataChecks_assert_value));
260  
261 13 boot_Data_Checks__constraints(aTHX);
262  
263 13 XopENTRY_set(&xop_invoke_checkfunc, xop_name, "invoke_checkfunc");
264 13 XopENTRY_set(&xop_invoke_checkfunc, xop_desc, "invoke checkfunc");
265 13 XopENTRY_set(&xop_invoke_checkfunc, xop_class, OA_UNOP_AUX);
266 13 Perl_custom_op_register(aTHX_ &pp_invoke_checkfunc, &xop_invoke_checkfunc);
« hax/ckcall_constfold.c.inc ↑ Summary t/test.xs »