lib/Evo/Class/Attrs/helpers.c

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 147 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Evo/Class/Attrs/eca.c ↑ Summary lib/Evo/Class/Attrs/xs.c »
1 0 static inline AV *sv2av(SV *self) {
2 0 ▶ if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) 0 0 0
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) 0 0 0
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) 0 0 0
3 0 croak("Not an ARRAY ref");
4 0 return (AV *)SvRV(self);
5 }
6  
7 0 static inline SV *av_fetch_or_croak(AV *av, int i) {
8 dTHX;
9 0 SV **tmp = av_fetch(av, i, 0);
10 0 ▶ if (!tmp) croak("Can't fetch %d", i);
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) croak("Can't fetch %d", i); 0 0 0
11 0 return *tmp;
12 }
13  
14 0 static inline SV *hv_he_store_or_croak(HV *hv, SV *key, SV *val) {
15 dTHX;
16 0 HE *he = hv_fetch_ent(hv, key, TRUE, 0U);
17 0 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 0 0 0
18 0 SvREFCNT_dec(val);
19 0 croak("Can't store value");
20 }
21 0 SV *sv = HeVAL(he);
22 0 ▶ SvSetMagicSV(sv, val);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV(sv, val); 0 0 0
SvSetMagicSV(sv, val); 0 0 0
23 0 return sv;
24 }
25  
26 0 static inline void hv_stores_or_croak(HV *hv, const char *key, SV *val) {
27 dTHX;
28 0 ▶ if (hv_store(hv, key, strlen(key), val, 0U)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(hv, key, strlen(key), val, 0U)) return; 0 0 0
29 0 SvREFCNT_dec(val);
30 0 croak("Can't store value");
31 }
32  
33 0 static inline ECAslot *sv2slot(SV *sv) {
34 dTHX;
35 0 return INT2PTR(ECAslot *, SvUV(sv));
36 }
37  
38 0 static SV *psv_to_slotsv(SV *sv) {
39 dTHX;
40 0 ECAslot *slot = sv2slot(sv);
41 0 HV *hv = newHV();
42  
43 // may be NULL
44 0 hv_stores_or_croak(hv, "value",
45 0 ▶ newSVsv(slot->value ? slot->value : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
newSVsv(slot->value ? slot->value : &PL_sv_undef)); 0 0 0
46 0 hv_stores_or_croak(hv, "check",
47 0 ▶ newSVsv(slot->check ? slot->check : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
newSVsv(slot->check ? slot->check : &PL_sv_undef)); 0 0 0
48 0 hv_stores_or_croak(hv, "inject",
49 0 ▶ newSVsv(slot->inject ? slot->inject : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
newSVsv(slot->inject ? slot->inject : &PL_sv_undef)); 0 0 0
50  
51 0 hv_stores_or_croak(hv, "name", newSVsv(slot->key)); // always defined
52  
53 0 ▶ hv_stores_or_croak(hv, "ro", newSVsv(slot->is_ro ? &PL_sv_yes : &PL_sv_no));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores_or_croak(hv, "ro", newSVsv(slot->is_ro ? &PL_sv_yes : &PL_sv_no)); 0 0 0
54 0 hv_stores_or_croak(hv, "method",
55 0 ▶ newSVsv(slot->is_method ? &PL_sv_yes : &PL_sv_no));
Branch: 1 branch
BranchTrueFalseTotal
newSVsv(slot->is_method ? &PL_sv_yes : &PL_sv_no)); 0 0 0
56 0 hv_stores_or_croak(hv, "type", newSViv(slot->type));
57 0 return newRV_noinc((SV *)hv);
58 }
59  
60 // it's the fastest way to hanlde arguments as perls (my %hash = @_)
61 // point uniq to the element of args from which it will be uniq, return
62 // length of uniq array. Complex, need code review
63 //
64 //
65 // the idea is to refill a stack from the end, picking up only uniq values and
66 // decreasing the offset (if current value is uniq)
67 //
68 // In the simple form(without KV, keys only): A B B. OFFSET is *
69 //
70 // 1. [A B *], current is B
71 // B is uniq, so decrease OFFSET. and write it (don't touch in our case
72 // because it's already here)
73 // 2) [A * B]. current is B.
74 // B is found. Don't decrease offset
75 // 3) [A * B]. current is A
76 // A is uniq. Write it and decrease offset
77 // 4) [* A B], no current - end of loop. OFFSET++ (0++) to point to A
78 //
79 // So here is results: OFFSET from the beginning is 1 (skip one element):
80 //
81 // STACK now is:
82 // A [A B]
83 // return addres to the second element
84 //
85 // The function below works the same way, except it operates K,V elements
86 //
87 0 int args_to_uniq(SV *args[], int len_args, SV ***uniq) {
88 dTHX;
89 0 ▶ if (len_args % 2) croak("Not even list");
Branch: 1 branch
BranchTrueFalseTotal
if (len_args % 2) croak("Not even list"); 0 0 0
90  
91 0 int end = len_args - 1;
92 0 int offset = end;
93 0 ▶ for (int i = end; i >= 0; i -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = end; i >= 0; i -= 2) { 0 0 0
94 0 SV *curkey = args[i - 1];
95 0 SV *curval = args[i];
96 int j;
97 0 ▶ for (j = end; j > offset; j -= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (j = end; j > offset; j -= 2) 0 0 0
98 0 ▶ if (!strcmp(SvPV_nolen(args[j - 1]), SvPV_nolen(curkey))) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!strcmp(SvPV_nolen(args[j - 1]), SvPV_nolen(curkey))) break; 0 0 0
if (!strcmp(SvPV_nolen(args[j - 1]), SvPV_nolen(curkey))) break; 0 0 0
99  
100 // if j == offset, we've reached the end without a match +
101 0 ▶ if (j == offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == offset) { 0 0 0
102 // if i!= j, replace value (because if i=j all array may be already
103 // uniq and we don't need to overwrite anything)
104 0 ▶ if (j != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (j != i) { 0 0 0
105 0 args[offset - 1] = curkey;
106 0 args[offset] = curval;
107 };
108 0 offset -= 2;
109 }
110 };
111  
112 0 offset++; // jump to next key index, because can be -1 here
113 0 *uniq = &args[offset];
114 0 return len_args - offset;
115 }
116  
117 0 static inline void do_check(SV *cv, SV *value, SV *key) {
118 dTHX;
119 0 SV *ok = &PL_sv_undef, *msg = &PL_sv_undef;
120  
121 0 dSP;
122 0 ENTER;
123 0 SAVETMPS;
124  
125 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
126 0 PUSHs(sv_mortalcopy(value));
127 0 PUTBACK;
128  
129 0 int count = call_sv(cv, G_ARRAY);
130 // could return 0 or 1 or 2 or more
131 0 SPAGAIN;
132  
133 0 ▶ if (count) count == 1 ? (ok = POPs) : (msg = POPs, ok = POPs);
Branch: 2 branches
BranchTrueFalseTotal
if (count) count == 1 ? (ok = POPs) : (msg = POPs, ok = POPs); 0 0 0
if (count) count == 1 ? (ok = POPs) : (msg = POPs, ok = POPs); 0 0 0
134  
135 0 ▶ if (!SvTRUE(ok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(ok)) { 0 0 0
136 0 ▶ croak("Bad value \"%s\" for attribute \"%s\": %s", SvPV_nolen(value),
Branch: 2 branches
BranchTrueFalseTotal
croak("Bad value \"%s\" for attribute \"%s\": %s", SvPV_nolen(value), 0 0 0
croak("Bad value \"%s\" for attribute \"%s\": %s", SvPV_nolen(value), 0 0 0
137 SvPV_nolen(key), SvTRUE(msg) ? SvPV_nolen(msg) : "");
138 }
139  
140 0 PUTBACK;
141 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
142 0 LEAVE;
143 0 };
144  
145 // invoce cv, passing arg, store result of that invocation to hash, return an SV
146 // from HeVAL
147 0 static inline SV *invoke_and_store(SV *arg, SV *cv, HV *hash, SV *key) {
148 dTHX;
149 0 dSP;
150  
151 0 ENTER;
152 0 SAVETMPS;
153  
154 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
155 0 PUSHs(sv_mortalcopy(arg));
156 0 PUTBACK;
157  
158 0 int count = call_sv(cv, G_SCALAR);
159 0 ▶ if (count != 1) croak("bad count");
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) croak("bad count"); 0 0 0
160 0 SPAGAIN;
161 0 SV *tmp = POPs;
162 0 SV *result = hv_he_store_or_croak(hash, key, tmp);
163  
164 0 PUTBACK;
165 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
166 0 LEAVE;
167  
168 0 return result;
169 }
« lib/Evo/Class/Attrs/eca.c ↑ Summary lib/Evo/Class/Attrs/xs.c »