lib/Evo/Class/Attrs/xs.c

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 149 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Evo/Class/Attrs/helpers.c ↑ Summary lib/Evo/Lib/XS.xs »
1 /*
2 * You may want to know that:
3 * * I use perl's magic feature to bind an ::Attrs instance to xs_new, ECAslot
4 * slot to xs_attr (MGVTBL ATTRS_TBL);
5 *
6 * * I use magic mg_dup and mg_free to clear and duplicate(for threads) ECAslot
7 * instance (MGVTBL ECA_TBL)
8 *
9 * * For performance reason, CvXSUBANY(cv).any_ptr is used to fetch bound data
10 * unless perl was build without usemultiplicity. This will improve performance
11 * by 2-4%. This technique was borrowed from Mouse. MAGIC is still used to mark
12 * variable for GC.
13 *
14 * * I use a perl's array as an object for this instance because using pure C
15 * structure will make code more complex without real benefits
16 *
17 */
18  
19 /* xs_new
20 * 0) args_to_uniq behaves like %opts = @_; (delete duplicates). And we can
21 * avoid creating hash
22 *
23 * I) Iterate through slots
24 * 1) Iterate through array of args in inner loop, skip consumed(NULL).
25 * a) compare slot->key with arg's name. If matched, mark as consumed (NULL).
26 * Continue parent's loop(simulate by goto, could be rewritten by function)
27 * b) Check, if slot->type is on of REQ/DEF/ type, do action
28 *
29 * II) If there are not consumed args, croak "unknown attribute"
30 */
31  
32 static MGVTBL ATTRS_TBL = {0, 0, 0, 0, 0, 0, 0, 0};
33  
34 0 static void xs_new(pTHX_ SV *cv) {
35 PERL_UNUSED_VAR(cv);
36 0 dXSARGS;
37 0 ▶ if (items < 1) croak("Usage: class, ref");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: class, ref"); 0 0 0
38  
39 SV *class;
40 0 ▶ if (sv_isobject(ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(ST(0))) { 0 0 0
41 0 HV *stash = SvSTASH(SvRV(ST(0)));
42 0 ▶ char *class_name = HvNAME(stash);
Branch: 6 branches
BranchTrueFalseTotal
char *class_name = HvNAME(stash); 0 0 0
char *class_name = HvNAME(stash); 0 0 0
char *class_name = HvNAME(stash); 0 0 0
char *class_name = HvNAME(stash); 0 0 0
char *class_name = HvNAME(stash); 0 0 0
char *class_name = HvNAME(stash); 0 0 0
43 0 class = newSVpv(class_name, 0);
44 0 sv_2mortal(class);
45 } else {
46 0 class = ST(0);
47 }
48  
49 // $class::EVO_CLASS_ATTRS
50 0 const char *SUFFIX = "::EVO_CLASS_ATTRS";
51 0 STRLEN need_len = SvCUR(class) + strlen(SUFFIX) + 1;
52 0 char attrs_name[need_len];
53 0 strcpy(attrs_name, SvPV_nolen(class));
54 0 strncat(attrs_name, SUFFIX, need_len);
55 0 AV *slots = sv2av(get_sv(attrs_name, 0));
56  
57 0 HV *hash = newHV();
58 0 SV *obj = sv_2mortal(newRV_noinc((SV *)hash)); // don't move to the end(leaks)
59  
60 SV **args; // uniq args
61 0 int args_count = args_to_uniq(&ST(1), items - 1, &args); // skip 1(class)
62  
63 0 int slots_count = av_top_index(slots) + 1;
64 0 ▶ for (int i = 0; i < slots_count; i++) { // NEXT_SLOT:
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < slots_count; i++) { // NEXT_SLOT: 0 0 0
65 0 ECAslot *slot = sv2slot(av_fetch_or_croak(slots, i));
66  
67 // iterage args, null if matched
68 0 ▶ for (int j = 0; j < args_count; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < args_count; j += 2) { 0 0 0
69 0 SV *tmp = args[j];
70 0 ▶ if (!tmp) continue; // already matched
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) continue; // already matched 0 0 0
71  
72 0 ▶ if (!sv_cmp(tmp, slot->key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_cmp(tmp, slot->key)) { 0 0 0
73 0 ▶ if (slot->check) do_check(slot->check, args[j + 1], slot->key);
Branch: 1 branch
BranchTrueFalseTotal
if (slot->check) do_check(slot->check, args[j + 1], slot->key); 0 0 0
74 0 hv_he_store_or_croak(hash, slot->key, args[j + 1]);
75 0 args[j] = NULL;
76 0 goto NEXT_SLOT;
77 args[j] = NULL; // mark as consumed
78 }
79 }
80  
81 // slot not found in passed args, decide what to do
82 0 switch (slot->type) {
83 0 case ECA_REQUIRED:
84 0 croak("Attribute \"%s\" is required", SvPV_nolen(slot->key));
85 break;
86 0 case ECA_DEFAULT:
87 0 hv_he_store_or_croak(hash, slot->key, slot->value);
88 0 break;
89 0 case ECA_DEFAULT_CODE:
90 0 invoke_and_store(class, slot->value, hash, slot->key);
91 0 break;
92 0 case ECA_LAZY:
93 case ECA_OPTIONAL:
94 0 break;
95 }
96  
97 0 NEXT_SLOT:; // simulate continue label
98 }
99  
100 // todo: croak all superfluous
101 0 ▶ for (int j = 0; j < args_count; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < args_count; j += 2) { 0 0 0
102 0 ▶ if (args[j]) croak("Unknown attribute %s", SvPV_nolen(args[j]));
Branch: 1 branch
BranchTrueFalseTotal
if (args[j]) croak("Unknown attribute %s", SvPV_nolen(args[j])); 0 0 0
103 }
104  
105 0 sv_bless(obj, gv_stashsv(class, GV_ADD));
106 0 ST(0) = obj;
107 0 XSRETURN(1);
108 }
109  
110 0 static void xs_attr(pTHX_ SV *cv) {
111 0 dXSARGS;
112 // checks
113 0 ▶ if (items < 1) croak("Bad usage");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Bad usage"); 0 0 0
114 0 SV *self = ST(0);
115 0 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) croak("Not a HASH");
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) croak("Not a HASH"); 0 0 0
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) croak("Not a HASH"); 0 0 0
116 0 HV *hash = (HV *)SvRV(self);
117  
118 0 SV *result = &PL_sv_undef;
119  
120 #ifndef MULTIPLICITY
121 0 ECAslot *slot = CvXSUBANY(cv).any_ptr;
122 #else
123 MAGIC *mg = mg_findext(cv, PERL_MAGIC_ext, &ATTRS_TBL);
124 ECAslot *slot = sv2slot(mg->mg_obj);
125 #endif
126  
127 0 ▶ if (items == 1) { // get
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { // get 0 0 0
128 0 HE *he = hv_fetch_ent(hash, slot->key, 0, 0);
129 0 ▶ if (he) {
Branch: 1 branch
BranchTrueFalseTotal
if (he) { 0 0 0
130 0 result = HeVAL(he);
131 } else {
132 0 ▶ if (slot->type == ECA_LAZY) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot->type == ECA_LAZY) { 0 0 0
133 0 result = invoke_and_store(self, slot->value, hash, slot->key);
134 } else {
135 0 result = &PL_sv_undef;
136 }
137 }
138 } else { // set
139 0 ▶ if (slot->is_ro)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->is_ro) 0 0 0
140 0 croak("Attribute \"%s\" is readonly", SvPV_nolen(slot->key));
141 0 ▶ if (slot->check) do_check(slot->check, ST(1), slot->key);
Branch: 1 branch
BranchTrueFalseTotal
if (slot->check) do_check(slot->check, ST(1), slot->key); 0 0 0
142 0 hv_he_store_or_croak(hash, slot->key, ST(1));
143 0 result = self;
144 }
145  
146 0 ST(0) = result;
147 0 XSRETURN(1);
148 };
149  
150 /* XS FUNCTIONS */
151 0 static SV *attrs__gen_attr(SV *self, char *name, int type, SV *value, SV *check,
152 bool is_ro, SV *inject, bool method) {
153 dTHX;
154 0 AV *av = sv2av(self);
155 0 SV *slot_sv = eca_new_sv(name, type, value, check, is_ro, inject, method);
156 0 ECAslot *slot = sv2slot(slot_sv);
157  
158 // register... i will be either last + 1 or matched element
159 0 int i, last = av_top_index(av);
160 0 ▶ for (i = 0; i <= last; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= last; i++) { 0 0 0
161 0 SV *tmp_sv = av_fetch_or_croak(av, i);
162 0 ECAslot *cur = sv2slot(tmp_sv);
163 0 ▶ if (!sv_cmp(cur->key, slot->key)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_cmp(cur->key, slot->key)) break; 0 0 0
164 }
165 0 ▶ if (!av_store(av, i, slot_sv)) croak("Can't store");
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(av, i, slot_sv)) croak("Can't store"); 0 0 0
166  
167 // generate cv
168 0 CV *xsub = newXS(NULL, (XSUBADDR_t)xs_attr, __FILE__);
169 0 sv_magicext((SV *)xsub, slot_sv, PERL_MAGIC_ext, &ATTRS_TBL, NULL, 0);
170 #ifndef MULTIPLICITY
171 0 CvXSUBANY(xsub).any_ptr = sv2slot(slot_sv);
172 #endif
173 0 return newRV_noinc((SV *)xsub);
174 }
175  
176 0 static SV *attrs_gen_new(SV *self) {
177 dTHX;
178 PERL_UNUSED_VAR(self);
179 0 CV *xsub = newXS(NULL, (XSUBADDR_t)xs_new, __FILE__);
180 0 return newRV_noinc((SV *)xsub);
181 }
182  
183 0 static bool attrs_exists(SV *self, SV *name) {
184 dTHX;
185  
186 0 AV *av = sv2av(self);
187 0 int i, size = av_top_index(av) + 1;
188  
189 0 ▶ for (i = 0; i < size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; i++) { 0 0 0
190 0 SV *tmp_sv = av_fetch_or_croak(av, i);
191 0 ECAslot *slot = sv2slot(tmp_sv);
192 0 ▶ if (!sv_cmp(name, slot->key)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_cmp(name, slot->key)) return 1; 0 0 0
193 }
194 0 return false;
195 }
« lib/Evo/Class/Attrs/helpers.c ↑ Summary lib/Evo/Lib/XS.xs »