lib/Object/mop-class.xsi

filter: Statementstmt 87.8% 181 / 206 Branchbran 73.6% 84 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.8% 265 / 320 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Object/Pad.xs ↑ Summary hax/exec_optree.c.inc »
1  
2 SV *
3 _create_class(pkg, name, ...)
4 SV *pkg
5 SV *name
6 ALIAS:
7 _create_class = METATYPE_CLASS
8 _create_role = METATYPE_ROLE
9 CODE:
10 {
11 PERL_UNUSED_ARG(pkg);
12 6 dKWARG(2);
13 6 SV *superclassname = NULL;
14 6 bool set_compclassmeta = false;
15 6 bool set_abstract = false;
16  
17 {
18 const COP *cop;
19  
20 6 const HV *mystash = CopSTASH(PL_curcop);
21  
22 6 ▶ for(int level = 0; level < 20; level++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int level = 0; level < 20; level++) { 6 0 6
23 6 const PERL_CONTEXT *cx = caller_cx(level, NULL);
24 6 ▶ if(!cx)
Branch: 1 branch
BranchTrueFalseTotal
if(!cx) 0 6 6
25 0 break;
26  
27 6 cop = cx->blk_oldcop;
28  
29 6 ▶ if(CopSTASH(cop) != mystash)
Branch: 1 branch
BranchTrueFalseTotal
if(CopSTASH(cop) != mystash) 6 0 6
30 6 break;
31  
32 0 cop = NULL;
33 }
34  
35 6 ▶ if(cop && !cophh_exists_pvs(CopHINTHASH_get(cop), "Object::Pad/experimental(mop)", 0))
Branch: 2 branches
BranchTrueFalseTotal
if(cop && !cophh_exists_pvs(CopHINTHASH_get(cop), "Object::Pad/experimental(mop)", 0)) 6 0 6
if(cop && !cophh_exists_pvs(CopHINTHASH_get(cop), "Object::Pad/experimental(mop)", 0)) 0 6 6
36 0 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
37 "The Object::Pad MOP API is experimental and may be changed or removed without notice");
38 }
39  
40 static const char *args[] = {
41 "extends",
42 "isa",
43 "_set_compclassmeta",
44 "abstract",
45 NULL
46 };
47 18 ▶ while(KWARG_NEXT(args)) {
Branch: 1 branch
BranchTrueFalseTotal
while(KWARG_NEXT(args)) { 6 6 12
48 6 switch(kwarg) {
49 1 case 0: /* extends */
50 case 1: /* isa */
51 1 ▶ if(ix != METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(ix != METATYPE_CLASS) 0 1 1
52 0 croak("Only a class may extend another");
53 1 superclassname = sv_mortalcopy(kwval);
54 1 break;
55  
56 4 case 2: /* _set_compclassmeta */
57 4 set_compclassmeta = SvTRUE(kwval);
58 4 break;
59  
60 1 case 3: /* abstract */
61 1 set_abstract = SvTRUE(kwval);
62 1 break;
63 }
64 }
65  
66 6 ClassMeta *meta = mop_create_class(ix, name);
67  
68 6 ▶ if(superclassname && SvOK(superclassname))
Branch: 2 branches
BranchTrueFalseTotal
if(superclassname && SvOK(superclassname)) 1 5 6
if(superclassname && SvOK(superclassname)) 1 0 1
69 1 mop_class_set_superclass(meta, superclassname);
70  
71 6 ▶ if(set_abstract)
Branch: 1 branch
BranchTrueFalseTotal
if(set_abstract) 1 5 6
72 1 meta->abstract = true;
73  
74 6 mop_class_begin(meta);
75  
76 6 RETVAL = newSV(0);
77 6 sv_setref_uv(RETVAL, "Object::Pad::MOP::Class", PTR2UV(meta));
78  
79 6 ▶ if(set_compclassmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if(set_compclassmeta) { 4 2 6
80 4 compclassmeta_set(meta);
81  
82 4 CV *cv = newXS(NULL, &xsub_mop_class_seal, __FILE__);
83 4 CvXSUBANY(cv).any_ptr = meta;
84  
85 4 ▶ if(!PL_unitcheckav)
Branch: 1 branch
BranchTrueFalseTotal
if(!PL_unitcheckav) 2 2 4
86 2 PL_unitcheckav = newAV();
87 4 av_push(PL_unitcheckav, (SV *)cv);
88 }
89 }
90 OUTPUT:
91 RETVAL
92  
93 bool
94 is_class(ClassMeta *self)
95 ALIAS:
96 is_class = METATYPE_CLASS
97 is_role = METATYPE_ROLE
98 CODE:
99 4 ▶ RETVAL = (self->type == ix);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (self->type == ix); 2 2 4
100 OUTPUT:
101 RETVAL
102  
103 bool
104 is_abstract(ClassMeta *self)
105 CODE:
106 4 ▶ RETVAL = self->abstract;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->abstract; 3 1 4
107 OUTPUT:
108 RETVAL
109  
110 SV *
111 name(ClassMeta *self)
112 CODE:
113 10 RETVAL = SvREFCNT_inc(self->name);
114 OUTPUT:
115 RETVAL
116  
117 void
118 superclasses(ClassMeta *self)
119 PPCODE:
120 2 ▶ if(self->type == METATYPE_CLASS && self->cls.supermeta) {
Branch: 2 branches
BranchTrueFalseTotal
if(self->type == METATYPE_CLASS && self->cls.supermeta) { 2 0 2
if(self->type == METATYPE_CLASS && self->cls.supermeta) { 1 1 2
121 1 PUSHs(sv_newmortal());
122 1 sv_setref_uv(ST(0), "Object::Pad::MOP::Class", PTR2UV(self->cls.supermeta));
123 1 XSRETURN(1);
124 }
125  
126 1 XSRETURN(0);
127  
128 void
129 direct_roles(ClassMeta *self)
130 ALIAS:
131 direct_roles = 0
132 all_roles = 1
133 PPCODE:
134 {
135 6 U32 count = 0;
136  
137 /* TODO Consider recursion */
138 U32 i;
139 6 switch(self->type) {
140 6 case METATYPE_CLASS: {
141 U32 nroles;
142 6 RoleEmbedding **embeddings = NULL;
143 6 switch(ix) {
144 3 case 0: embeddings = mop_class_get_direct_roles(self, &nroles); break;
145 3 case 1: embeddings = mop_class_get_all_roles(self, &nroles); break;
146 }
147  
148 9 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 3 6 9
149 3 SV *sv = sv_newmortal();
150 3 sv_setref_uv(sv, "Object::Pad::MOP::Class", PTR2UV(embeddings[i]->rolemeta));
151 3 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 3 3
152 3 count++;
153 }
154 6 break;
155 }
156  
157 0 case METATYPE_ROLE:
158 0 break;
159 }
160  
161 6 XSRETURN(count);
162 }
163  
164 void
165 add_role(ClassMeta *self, role)
166 SV *role
167 ALIAS:
168 compose_role = 0
169 CODE:
170 {
171 2 ClassMeta *rolemeta = NULL;
172  
173 PERL_UNUSED_VAR(ix);
174  
175 2 ▶ if(SvROK(role)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(role)) { 1 1 2
176 1 ▶ if(!sv_derived_from(role, "Object::Pad::MOP::Class"))
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_derived_from(role, "Object::Pad::MOP::Class")) 0 1 1
177 0 croak("Expected a role name string or Object::Pad::MOP::Class; got %" SVf, SVfARG(role));
178  
179 1 rolemeta = MUST_CLASSMETA_FROM_RV(role);
180 }
181 else {
182 1 HV *rolestash = gv_stashsv(role, 0);
183 /* Don't attempt to `require` it; that is caller's responsibilty */
184 1 ▶ if(!rolestash)
Branch: 1 branch
BranchTrueFalseTotal
if(!rolestash) 0 1 1
185 0 croak("Role %" SVf " does not exist", SVfARG(role));
186  
187 1 GV **metagvp = (GV **)hv_fetchs(rolestash, "META", 0);
188 1 ▶ if(metagvp)
Branch: 1 branch
BranchTrueFalseTotal
if(metagvp) 1 0 1
189 1 rolemeta = NUM2PTR(ClassMeta *, SvUV(SvRV(GvSV(*metagvp))));
190 }
191  
192 2 ▶ if(!rolemeta || rolemeta->type != METATYPE_ROLE)
Branch: 2 branches
BranchTrueFalseTotal
if(!rolemeta || rolemeta->type != METATYPE_ROLE) 2 0 2
if(!rolemeta || rolemeta->type != METATYPE_ROLE) 0 2 2
193 0 croak("%" SVf " is not a role", SVfARG(role));
194  
195 2 mop_class_begin(self);
196  
197 2 mop_class_add_role(self, rolemeta);
198 }
199  
200 void
201 add_BUILD(ClassMeta *self, code)
202 CV *code
203 CODE:
204 1 mop_class_begin(self);
205  
206 1 mop_class_add_BUILD(self, (CV *)SvREFCNT_inc((SV *)code));
207  
208 SV *
209 add_method(ClassMeta *self, mname, ...)
210 SV *mname
211 CODE:
212 {
213 7 ▶ if(items < 3)
Branch: 1 branch
BranchTrueFalseTotal
if(items < 3) 0 7 7
214 0 croak_xs_usage(cv, "self, mname, ..., code");
215 7 SV *ref = ST(items-1); items--;
216 7 ▶ if(!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVCV) 7 0 7
if(!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVCV) 0 7 7
217 0 croak("Expected CODE reference");
218  
219 7 dKWARG(2);
220  
221 /* Take a copy now to run FETCH magic */
222 7 mname = sv_2mortal(newSVsv(mname));
223  
224 7 mop_class_begin(self);
225  
226 7 ▶ if(SvOK(mname) && SvPOK(mname) && strEQ(SvPVX(mname), "BUILD")) {
Branch: 3 branches
BranchTrueFalseTotal
if(SvOK(mname) && SvPOK(mname) && strEQ(SvPVX(mname), "BUILD")) { 6 1 7
if(SvOK(mname) && SvPOK(mname) && strEQ(SvPVX(mname), "BUILD")) { 6 0 6
if(SvOK(mname) && SvPOK(mname) && strEQ(SvPVX(mname), "BUILD")) { 0 6 6
227 0 croak("Adding a method called BUILD is not supported; use ->add_BUILD directly");
228 }
229  
230 7 MethodMeta *methodmeta = mop_class_add_method_cv(self, mname, (CV *)SvREFCNT_inc(CV_FROM_REF(ref)));
231  
232 static const char *args[] = {
233 "common",
234 NULL
235 };
236 9 ▶ while(KWARG_NEXT(args)) {
Branch: 1 branch
BranchTrueFalseTotal
while(KWARG_NEXT(args)) { 1 4 5
237 1 ▶ switch(kwarg) {
Branch: 1 branch
BranchTrueFalseTotal
switch(kwarg) { 1 0 1
238 1 case 0: /* common */
239 1 methodmeta->is_common = SvTRUE(kwval);
240 1 break;
241 }
242 }
243  
244 4 RETVAL = newSV(0);
245 4 sv_setref_uv(RETVAL, "Object::Pad::MOP::Method", PTR2UV(methodmeta));
246 }
247 OUTPUT:
248 RETVAL
249  
250 void
251 get_direct_method(ClassMeta *self, methodname)
252 SV *methodname
253 ALIAS:
254 get_method = 1
255 PPCODE:
256 {
257 7 bool recurse = !!ix;
258 7 ClassMeta *class = self;
259  
260 do {
261 8 AV *methods = class->direct_methods;
262 8 U32 nmethods = av_count(methods);
263  
264 U32 i;
265 9 ▶ for(i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nmethods; i++) { 6 3 9
266 6 MethodMeta *methodmeta = (MethodMeta *)AvARRAY(methods)[i];
267  
268 6 ▶ if(!sv_eq(methodmeta->name, methodname))
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_eq(methodmeta->name, methodname)) 1 5 6
269 1 continue;
270  
271 5 ST(0) = sv_newmortal();
272 5 sv_setref_iv(ST(0), "Object::Pad::MOP::Method", PTR2UV(methodmeta));
273 5 XSRETURN(1);
274 }
275  
276 3 ▶ if(class->type == METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(class->type == METATYPE_CLASS) 3 0 3
277 3 class = class->cls.supermeta;
278 else
279 0 class = NULL;
280 3 ▶ } while(recurse && class);
Branch: 2 branches
BranchTrueFalseTotal
} while(recurse && class); 1 2 3
} while(recurse && class); 1 0 1
281  
282 2 croak("Class %" SVf " does not have a method called '%" SVf "'",
283 SVfARG(self->name), SVfARG(methodname));
284 }
285  
286 void
287 direct_methods(ClassMeta *self)
288 ALIAS:
289 all_methods = 1
290 PPCODE:
291 3 bool recurse = !!ix;
292  
293 /* A hash to remove overrides */
294 3 HV *mnames = NULL;
295 3 ▶ if(recurse) {
Branch: 1 branch
BranchTrueFalseTotal
if(recurse) { 2 1 3
296 2 mnames = newHV();
297 2 SAVEFREESV(mnames);
298 }
299  
300 3 U32 retcount = 0;
301  
302 do {
303 4 AV *methods = self->direct_methods;
304 4 U32 nmethods = av_count(methods);
305  
306 4 ▶ EXTEND(SP, retcount + nmethods); /* might be an overestimate but don't worry */
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, retcount + nmethods); /* might be an overestimate but don't worry */ 0 4 4
307 U32 i;
308 8 ▶ for(i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nmethods; i++) { 4 4 8
309 4 MethodMeta *methodmeta = (MethodMeta *)AvARRAY(methods)[i];
310  
311 4 ▶ if(mnames && hv_exists_ent(mnames, methodmeta->name, 0))
Branch: 2 branches
BranchTrueFalseTotal
if(mnames && hv_exists_ent(mnames, methodmeta->name, 0)) 3 1 4
if(mnames && hv_exists_ent(mnames, methodmeta->name, 0)) 1 2 3
312 1 continue;
313  
314 3 ST(retcount) = sv_newmortal();
315 3 sv_setref_iv(ST(retcount), "Object::Pad::MOP::Method", PTR2UV(methodmeta));
316 3 retcount++;
317  
318 3 hv_store_ent(mnames, methodmeta->name, &PL_sv_yes, 0);
319 }
320  
321 4 ▶ if(self->type == METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(self->type == METATYPE_CLASS) 4 0 4
322 4 self = self->cls.supermeta;
323 else
324 0 self = NULL;
325 4 ▶ } while(recurse && self);
Branch: 2 branches
BranchTrueFalseTotal
} while(recurse && self); 3 1 4
} while(recurse && self); 1 2 3
326  
327 3 XSRETURN(retcount);
328  
329 void
330 add_required_method(ClassMeta *self, mname)
331 SV *mname
332 CODE:
333 1 mop_class_begin(self);
334  
335 1 mop_class_add_required_method(self, mname);
336  
337 SV *
338 add_field(ClassMeta *self, fieldname, ...)
339 SV *fieldname
340 CODE:
341 {
342 13 dKWARG(2);
343  
344 13 mop_class_begin(self);
345  
346 13 FieldMeta *fieldmeta = mop_class_add_field(self, sv_mortalcopy(fieldname));
347  
348 static const char *args[] = {
349 "default",
350 "param",
351 "reader",
352 "writer",
353 "mutator",
354 "accessor",
355 "weak",
356 "attributes",
357 NULL,
358 };
359 27 ▶ while(KWARG_NEXT(args)) {
Branch: 1 branch
BranchTrueFalseTotal
while(KWARG_NEXT(args)) { 9 9 18
360 9 switch(kwarg) {
361 1 case 0: /* default */
362 1 mop_field_set_default_sv(fieldmeta, newSVsv(kwval));
363 1 break;
364  
365 2 case 1: /* param */
366 2 mop_field_apply_attribute(fieldmeta, "param", kwval);
367 2 break;
368  
369 4 case 2: /* reader */
370 4 mop_field_apply_attribute(fieldmeta, "reader", kwval);
371 4 break;
372  
373 1 case 3: /* writer */
374 1 mop_field_apply_attribute(fieldmeta, "writer", kwval);
375 1 break;
376  
377 0 case 4: /* mutator */
378 0 mop_field_apply_attribute(fieldmeta, "mutator", kwval);
379 0 break;
380  
381 0 case 5: /* accessor */
382 0 mop_field_apply_attribute(fieldmeta, "accessor", kwval);
383 0 break;
384  
385 0 case 6: /* weak */
386 0 mop_field_apply_attribute(fieldmeta, "weak", NULL);
387 0 break;
388  
389 1 case 7: /* attributes */
390 {
391 1 ▶ if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVAV) 1 0 1
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVAV) 0 1 1
392 0 croak("Expected 'attributes' to be given an ARRAY reference");
393 1 AV *attributes = AV_FROM_REF(kwval);
394 2 ▶ for(IV ix = 0; ix < av_count(attributes); ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for(IV ix = 0; ix < av_count(attributes); ix += 2) { 1 1 2
395 1 SV *attrname = *av_fetch(attributes, ix, 0);
396 1 ▶ SV *attrval = ix+1 < av_count(attributes) ? *av_fetch(attributes, ix + 1, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *attrval = ix+1 < av_count(attributes) ? *av_fetch(attributes, ix + 1, 0) : &PL_sv_undef; 1 0 1
397  
398 1 mop_field_apply_attribute(fieldmeta, SvPV_nolen(attrname), attrval);
399 }
400 }
401 }
402 }
403  
404 9 mop_field_seal(fieldmeta);
405  
406 9 RETVAL = newSV(0);
407 9 sv_setref_uv(RETVAL, "Object::Pad::MOP::Field", PTR2UV(fieldmeta));
408 }
409 OUTPUT:
410 RETVAL
411  
412 void
413 get_field(ClassMeta *self, fieldname)
414 SV *fieldname
415 PPCODE:
416 {
417 9 FieldMeta *fieldmeta = mop_class_find_field(self, fieldname, FIND_FIELD_ONLY_DIRECT);
418 9 ▶ if(fieldmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if(fieldmeta) { 8 1 9
419 8 ST(0) = sv_newmortal();
420 8 sv_setref_iv(ST(0), "Object::Pad::MOP::Field", PTR2UV(fieldmeta));
421 8 XSRETURN(1);
422 }
423  
424 1 croak("Class %" SVf " does not have a field called '%" SVf "'",
425 self->name, fieldname);
426 }
427  
428 void
429 fields(ClassMeta *self)
430 PPCODE:
431 2 AV *fields = self->fields;
432 2 U32 nfields = av_count(fields);
433  
434 2 ▶ EXTEND(SP, nfields);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, nfields); 0 2 2
435  
436 2 U32 retcount = 0;
437 FIELDOFFSET i;
438 4 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 2 2 4
439 2 FieldMeta *fieldmeta = (FieldMeta *)AvARRAY(fields)[i];
440 2 ▶ if(!fieldmeta->is_direct)
Branch: 1 branch
BranchTrueFalseTotal
if(!fieldmeta->is_direct) 1 1 2
441 1 continue;
442  
443 1 ST(i) = sv_newmortal();
444 1 sv_setref_iv(ST(i), "Object::Pad::MOP::Field", PTR2UV(fieldmeta));
445 1 retcount++;
446 }
447 2 XSRETURN(retcount);
448  
449 void
450 required_method_names(ClassMeta *self)
451 PPCODE:
452 1 ▶ if(self->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(self->type != METATYPE_ROLE) 0 1 1
453 0 croak("Can only call ->required_method_names on a metaclass for a role");
454  
455 1 AV *required_methods = self->requiremethods;
456 1 U32 nmethods = av_count(required_methods);
457  
458 1 ▶ EXTEND(SP, nmethods);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, nmethods); 0 1 1
459  
460 int i;
461 3 ▶ for(i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nmethods; i++) { 2 1 3
462 2 ST(i) = sv_2mortal(newSVsv(AvARRAY(required_methods)[i]));
463 }
464 1 XSRETURN(nmethods);
465  
466 void
467 seal(ClassMeta *self)
468 CODE:
469 1 mop_class_seal(self);
« lib/Object/Pad.xs ↑ Summary hax/exec_optree.c.inc »