src/class.c

filter: Statementstmt 83.4% 1226 / 1469 Branchbran 56.0% 581 / 1036 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.1% 1807 / 2505 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/force_list_keeping_pushmark.c.inc ↑ Summary src/field.c »
1 /* vi: set ft=xs : */
2 #define PERL_NO_GET_CONTEXT
3  
4 #include "EXTERN.h"
5 #include "perl.h"
6 #include "XSUB.h"
7  
8 #ifdef HAVE_DMD_HELPER
9 # define WANT_DMD_API_044
10 # include "DMD_helper.h"
11 #endif
12  
13 #include "perl-backcompat.c.inc"
14 #include "sv_setrv.c.inc"
15  
16 #include "perl-additions.c.inc"
17 #include "lexer-additions.c.inc"
18 #include "forbid_outofblock_ops.c.inc"
19 #include "force_list_keeping_pushmark.c.inc"
20 #include "optree-additions.c.inc"
21 #include "newOP_CUSTOM.c.inc"
22 #include "cv_copy_flags.c.inc"
23 #include "OP_HELEMEXISTSOR.c.inc"
24  
25 #include "object_pad.h"
26 #include "class.h"
27 #include "field.h"
28  
29 #undef register_class_attribute
30  
31 #ifdef DEBUGGING
32 # define DEBUG_OVERRIDE_PLCURCOP
33 # define DEBUG_SET_CURCOP_LINE(line) CopLINE_set(PL_curcop, line)
34 #else
35 # undef DEBUG_OVERRIDE_PLCURCOP
36 # define DEBUG_SET_CURCOP_LINE(line)
37 #endif
38  
39 #define COP_SEQ_RANGE_LOW_set(sv,val) \
40 STMT_START { (sv)->xpadn_low = (val); } STMT_END
41  
42 #ifndef COP_SEQMAX_INC
43 #define COP_SEQMAX_INC \
44 (PL_cop_seqmax++, \
45 (void)(PL_cop_seqmax == PERL_PADSEQ_INTRO && PL_cop_seqmax++))
46 #endif
47  
48 #define need_PLparser() ObjectPad__need_PLparser(aTHX)
49 void ObjectPad__need_PLparser(pTHX); /* in Object/Pad.xs */
50  
51 /* Empty MGVTBL simply for locating instance backing AV */
52 static MGVTBL vtbl_backingav = {};
53  
54 RoleEmbedding ObjectPad__embedding_standalone = {
55 LINNET_INIT(LINNET_VAL_ROLEEMBEDDING)
56 };
57  
58 typedef struct ClassAttributeRegistration ClassAttributeRegistration;
59  
60 struct ClassAttributeRegistration {
61 ClassAttributeRegistration *next;
62  
63 const char *name;
64 STRLEN permit_hintkeylen;
65  
66 const struct ClassHookFuncs *funcs;
67 void *funcdata;
68 };
69  
70 static ClassAttributeRegistration *classattrs = NULL;
71  
72 427 static void register_class_attribute(const char *name, const struct ClassHookFuncs *funcs, void *funcdata)
73 {
74 ClassAttributeRegistration *reg;
75 427 Newx(reg, 1, struct ClassAttributeRegistration);
76  
77 427 *reg = (struct ClassAttributeRegistration){
78 .name = name,
79 .funcs = funcs,
80 .funcdata = funcdata,
81 };
82  
83 427 ▶ if(funcs->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->permit_hintkey) 0 427 427
84 0 reg->permit_hintkeylen = strlen(funcs->permit_hintkey);
85 else
86 427 reg->permit_hintkeylen = 0;
87  
88 427 reg->next = classattrs;
89 427 classattrs = reg;
90 427 }
91  
92 struct ClassHookFuncs_v57 {
93 U32 ver;
94 U32 flags;
95 const char *permit_hintkey;
96 bool (*apply)(pTHX_ ClassMeta *classmeta, SV *value, SV **hookdata_ptr, void *funcdata);
97 /* No pre- or post-seal */
98 void (*post_add_field)(pTHX_ ClassMeta *classmeta, SV *hookdata, void *funcdata, FieldMeta *fieldmeta);
99 };
100  
101 0 void ObjectPad_register_class_attribute(pTHX_ const char *name, const struct ClassHookFuncs *funcs, void *funcdata)
102 {
103 0 ▶ if(funcs->ver < 57)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->ver < 57) 0 0 0
104 0 croak("Mismatch in third-party class attribute ABI version field: module wants %d, we require >= 57\n",
105 funcs->ver);
106 0 ▶ if(funcs->ver > OBJECTPAD_ABIVERSION)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->ver > OBJECTPAD_ABIVERSION) 0 0 0
107 0 croak("Mismatch in third-party class attribute ABI version field: attribute supplies %d, module wants %d\n",
108 funcs->ver, OBJECTPAD_ABIVERSION);
109  
110 0 ▶ if(!name || !(name[0] >= 'A' && name[0] <= 'Z'))
Branch: 3 branches
BranchTrueFalseTotal
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 0 0 0
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 0 0 0
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 0 0 0
111 0 croak("Third-party class attribute names must begin with a capital letter");
112  
113 0 ▶ if(!funcs->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(!funcs->permit_hintkey) 0 0 0
114 0 croak("Third-party class attributes require a permit hinthash key");
115  
116 0 ▶ if(funcs->ver < OBJECTPAD_ABIVERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->ver < OBJECTPAD_ABIVERSION) { 0 0 0
117 0 const struct ClassHookFuncs_v57 *funcs_v57 = (const struct ClassHookFuncs_v57 *)funcs;
118  
119 struct ClassHookFuncs *funcs_v76;
120 0 Newx(funcs_v76, 1, struct ClassHookFuncs);
121  
122 0 *funcs_v76 = (struct ClassHookFuncs){
123 .ver = OBJECTPAD_ABIVERSION,
124 0 .flags = funcs_v57->flags,
125 0 .permit_hintkey = funcs_v57->permit_hintkey,
126 0 .apply = funcs_v57->apply,
127 0 .post_add_field = funcs_v57->post_add_field,
128 };
129  
130 0 funcs = funcs_v76;
131 }
132  
133 0 register_class_attribute(name, funcs, funcdata);
134 0 }
135  
136 21 void ObjectPad_mop_class_apply_attribute(pTHX_ ClassMeta *classmeta, const char *name, SV *value)
137 {
138 21 HV *hints = GvHV(PL_hintgv);
139  
140 21 ▶ if(value && (!SvPOK(value) || !SvCUR(value)))
Branch: 3 branches
BranchTrueFalseTotal
if(value && (!SvPOK(value) || !SvCUR(value))) 21 0 21
if(value && (!SvPOK(value) || !SvCUR(value))) 16 5 21
if(value && (!SvPOK(value) || !SvCUR(value))) 0 16 16
141 5 value = NULL;
142  
143 ClassAttributeRegistration *reg;
144 79 ▶ for(reg = classattrs; reg; reg = reg->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(reg = classattrs; reg; reg = reg->next) { 79 0 79
145 79 ▶ if(!strEQ(name, reg->name))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(name, reg->name)) 58 21 79
146 58 continue;
147  
148 21 ▶ if(reg->funcs->permit_hintkey &&
Branch: 2 branches
BranchTrueFalseTotal
if(reg->funcs->permit_hintkey && 0 21 21
if(reg->funcs->permit_hintkey && 0 0 0
149 0 ▶ (!hints || !hv_fetch(hints, reg->funcs->permit_hintkey, reg->permit_hintkeylen, 0)))
Branch: 1 branch
BranchTrueFalseTotal
(!hints || !hv_fetch(hints, reg->funcs->permit_hintkey, reg->permit_hintkeylen, 0))) 0 0 0
150 0 continue;
151  
152 21 ▶ if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_NO_VALUE) && value)
Branch: 2 branches
BranchTrueFalseTotal
if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_NO_VALUE) && value) 5 16 21
if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_NO_VALUE) && value) 0 5 5
153 0 croak("Attribute :%s does not permit a value", name);
154 21 ▶ if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_MUST_VALUE) && !value)
Branch: 2 branches
BranchTrueFalseTotal
if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_MUST_VALUE) && !value) 16 5 21
if((reg->funcs->flags & OBJECTPAD_FLAG_ATTR_MUST_VALUE) && !value) 0 16 16
155 0 croak("Attribute :%s requires a value", name);
156  
157 21 SV *attrdata = value;
158  
159 21 ▶ if(reg->funcs->apply) {
Branch: 1 branch
BranchTrueFalseTotal
if(reg->funcs->apply) { 21 0 21
160 21 ▶ if(!(*reg->funcs->apply)(aTHX_ classmeta, value, &attrdata, reg->funcdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!(*reg->funcs->apply)(aTHX_ classmeta, value, &attrdata, reg->funcdata)) 20 0 20
161 20 return;
162 }
163  
164 0 ▶ if(!classmeta->hooks)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->hooks) 0 0 0
165 0 classmeta->hooks = newAV();
166  
167 struct ClassHook *hook;
168 0 Newx(hook, 1, struct ClassHook);
169  
170 0 *hook = (struct ClassHook){
171 0 .funcs = reg->funcs,
172 0 .funcdata = reg->funcdata,
173 .attrdata = attrdata,
174 };
175  
176 0 av_push(classmeta->hooks, (SV *)hook);
177  
178 0 ▶ if(value && value != attrdata)
Branch: 2 branches
BranchTrueFalseTotal
if(value && value != attrdata) 0 0 0
if(value && value != attrdata) 0 0 0
179 0 SvREFCNT_dec(value);
180  
181 0 return;
182 }
183  
184 0 croak("Unrecognised class attribute :%s", name);
185 }
186  
187 0 static ClassAttributeRegistration *get_active_registration(pTHX_ const char *name)
188 {
189 0 COPHH *cophh = CopHINTHASH_get(PL_curcop);
190  
191 0 ▶ for(ClassAttributeRegistration *reg = classattrs; reg; reg = reg->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(ClassAttributeRegistration *reg = classattrs; reg; reg = reg->next) { 0 0 0
192 0 ▶ if(!strEQ(name, reg->name))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(name, reg->name)) 0 0 0
193 0 continue;
194  
195 0 if(reg->funcs->permit_hintkey &&
196 0 !cophh_fetch_pvn(cophh, reg->funcs->permit_hintkey, reg->permit_hintkeylen, 0, 0))
197 0 continue;
198  
199 0 return reg;
200 }
201  
202 0 return NULL;
203 }
204  
205 0 struct ClassHook *ObjectPad_mop_class_get_attribute(pTHX_ ClassMeta *classmeta, const char *name)
206 {
207 /* First, work out what hookfuncs the name maps to */
208 0 ClassAttributeRegistration *reg = get_active_registration(aTHX_ name);
209  
210 0 ▶ if(!reg)
Branch: 1 branch
BranchTrueFalseTotal
if(!reg) 0 0 0
211 0 return NULL;
212  
213 /* Now lets see if classmeta has one */
214  
215 0 ▶ if(!classmeta->hooks)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->hooks) 0 0 0
216 0 return NULL;
217  
218 U32 hooki;
219 0 ▶ for(hooki = 0; hooki < av_count(classmeta->hooks); hooki++) {
Branch: 1 branch
BranchTrueFalseTotal
for(hooki = 0; hooki < av_count(classmeta->hooks); hooki++) { 0 0 0
220 0 struct ClassHook *hook = (struct ClassHook *)AvARRAY(classmeta->hooks)[hooki];
221  
222 0 ▶ if(hook->funcs == reg->funcs)
Branch: 1 branch
BranchTrueFalseTotal
if(hook->funcs == reg->funcs) 0 0 0
223 0 return hook;
224 }
225  
226 0 return NULL;
227 }
228  
229 0 AV *ObjectPad_mop_class_get_attribute_values(pTHX_ ClassMeta *classmeta, const char *name)
230 {
231 /* First, work out what hookfuncs the name maps to */
232  
233 0 ClassAttributeRegistration *reg = get_active_registration(aTHX_ name);
234  
235 0 ▶ if(!reg)
Branch: 1 branch
BranchTrueFalseTotal
if(!reg) 0 0 0
236 0 return NULL;
237  
238 /* Now lets see if classmeta has one */
239  
240 0 ▶ if(!classmeta->hooks)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->hooks) 0 0 0
241 0 return NULL;
242  
243 0 AV *ret = NULL;
244  
245 U32 hooki;
246 0 ▶ for(hooki = 0; hooki < av_count(classmeta->hooks); hooki++) {
Branch: 1 branch
BranchTrueFalseTotal
for(hooki = 0; hooki < av_count(classmeta->hooks); hooki++) { 0 0 0
247 0 struct ClassHook *hook = (struct ClassHook *)AvARRAY(classmeta->hooks)[hooki];
248  
249 0 ▶ if(hook->funcs != reg->funcs)
Branch: 1 branch
BranchTrueFalseTotal
if(hook->funcs != reg->funcs) 0 0 0
250 0 continue;
251  
252 0 ▶ if(!ret)
Branch: 1 branch
BranchTrueFalseTotal
if(!ret) 0 0 0
253 0 ret = newAV();
254  
255 0 av_push(ret, newSVsv(hook->attrdata));
256 }
257  
258 0 return ret;
259 }
260  
261 22 ClassMeta *ObjectPad_mop_get_class_for_stash(pTHX_ HV *stash)
262 {
263 22 GV **gvp = (GV **)hv_fetchs(stash, "META", 0);
264 22 ▶ if(!gvp)
Branch: 1 branch
BranchTrueFalseTotal
if(!gvp) 0 22 22
265 0 ▶ croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash)));
Branch: 3 branches
BranchTrueFalseTotal
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
266  
267 22 return MUST_CLASSMETA(SvUV(SvRV(GvSV(*gvp))));
268 }
269  
270 0 SV *ObjectPad_mop_class_get_name(pTHX_ ClassMeta *class)
271 {
272 0 return class->name;
273 }
274  
275 #define make_instance_fields(classmeta, fieldstore, roleoffset) S_make_instance_fields(aTHX_ classmeta, fieldstore, roleoffset)
276 176 static void S_make_instance_fields(pTHX_ const ClassMeta *classmeta, SV *fieldstore, FIELDOFFSET roleoffset)
277 {
278 assert(classmeta->type == METATYPE_ROLE || roleoffset == 0);
279  
280 176 ▶ if(classmeta->start_fieldix) {
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->start_fieldix) { 26 150 176
281 /* Superclass actually has some fields */
282 assert(classmeta->type == METATYPE_CLASS);
283 assert(classmeta->cls.supermeta->sealed);
284  
285 26 make_instance_fields(classmeta->cls.supermeta, fieldstore, 0);
286 }
287  
288 176 AV *fields = classmeta->fields;
289 176 I32 nfields = av_count(fields);
290  
291 176 ▶ if(SvTYPE(fieldstore) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(fieldstore) == SVt_PVAV) 161 15 176
292 161 av_extend((AV *)fieldstore, classmeta->next_fieldix - 1 + roleoffset);
293  
294 I32 i;
295 357 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 181 176 357
296 181 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
297 181 ▶ if(!fieldmeta->is_direct)
Branch: 1 branch
BranchTrueFalseTotal
if(!fieldmeta->is_direct) 2 179 181
298 2 continue;
299 179 char sigil = SvPV_nolen(fieldmeta->name)[0];
300  
301 179 FIELDOFFSET fieldix = fieldmeta->fieldix + roleoffset;
302  
303 /* We can't av_push() because REPR_KEYS would break here */
304 SV **svp;
305 #ifdef HAVE_SVt_PVOBJ
306 179 ▶ if(SvTYPE(fieldstore) == SVt_PVOBJ) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(fieldstore) == SVt_PVOBJ) { 17 162 179
307 17 svp = &ObjectFIELDS(fieldstore)[fieldix];
308 17 *svp = newSV(0);
309 }
310 else
311 #endif
312 {
313 162 svp = av_fetch_simple((AV *)fieldstore, fieldix, TRUE);
314 }
315 assert(svp);
316  
317 179 switch(sigil) {
318 159 case '$':
319 /* simply fetching will create the SV */
320 159 break;
321  
322 11 case '@':
323 11 sv_setrv_noinc(*svp, (SV *)newAV());
324 11 break;
325  
326 9 case '%':
327 9 sv_setrv_noinc(*svp, (SV *)newHV());
328 9 break;
329  
330 0 default:
331 0 croak("ARGH: not sure how to handle a slot sigil %c\n", sigil);
332 }
333 }
334  
335 176 ▶ if(classmeta->type == METATYPE_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->type == METATYPE_CLASS) { 158 18 176
336 U32 nroles;
337 158 RoleEmbedding **embeddings = mop_class_get_direct_roles(classmeta, &nroles);
338  
339 assert(classmeta->type == METATYPE_CLASS || nroles == 0);
340  
341 176 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 18 158 176
342 18 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(embeddings[i]);
343 18 ClassMeta *rolemeta = embedding->rolemeta;
344  
345 assert(rolemeta->sealed);
346  
347 18 make_instance_fields(rolemeta, fieldstore, embedding->offset);
348 }
349 }
350 176 }
351  
352 #define alias_fieldkeys_into_av(classmeta, hv, backingav) S_alias_fieldkeys_into_av(aTHX_ classmeta, hv, backingav)
353 3 static void S_alias_fieldkeys_into_av(pTHX_ ClassMeta *classmeta, HV *hv, AV *backingav)
354 {
355 3 ▶ if(classmeta->cls.supermeta)
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->cls.supermeta) 0 3 3
356 0 alias_fieldkeys_into_av(classmeta->cls.supermeta, hv, backingav);
357  
358 3 AV *fields = classmeta->fields;
359 3 I32 nfields = av_count(fields);
360  
361 I32 i;
362 12 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 9 3 12
363 9 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
364 9 ▶ if(!fieldmeta->is_direct)
Branch: 1 branch
BranchTrueFalseTotal
if(!fieldmeta->is_direct) 0 9 9
365 0 continue;
366  
367 9 SV *fieldkey = newSVpvf("%" SVf "/%" SVf, classmeta->name, fieldmeta->name);
368 9 HE *he = hv_fetch_ent(hv, fieldkey, 1, 0);
369 9 SvREFCNT_dec(fieldkey);
370  
371 9 av_store(backingav, fieldmeta->fieldix, SvREFCNT_inc(HeVAL(he)));
372 }
373 3 }
374  
375 533 SV *ObjectPad_get_obj_fieldstore(pTHX_ SV *self, enum ReprType repr, bool create)
376 {
377 533 SV *rv = SvRV(self);
378  
379 533 switch(repr) {
380 457 case REPR_NATIVE:
381 457 ▶ if(SvTYPE(rv) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVAV) 0 457 457
382 0 croak("Not an ARRAY reference");
383  
384 457 return rv;
385  
386 case REPR_HASH:
387 33 case_REPR_HASH:
388 {
389 33 ▶ if(SvTYPE(rv) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVHV) 0 33 33
390 0 croak("Not a HASH reference");
391 33 ▶ SV **backingsvp = hv_fetchs((HV *)rv, "Object::Pad/slots", create);
Branch: 1 branch
BranchTrueFalseTotal
SV **backingsvp = hv_fetchs((HV *)rv, "Object::Pad/slots", create); 7 26 33
392 33 ▶ if(create && !SvOK(*backingsvp))
Branch: 2 branches
BranchTrueFalseTotal
if(create && !SvOK(*backingsvp)) 7 26 33
if(create && !SvOK(*backingsvp)) 7 0 7
393 7 sv_setrv_noinc(*backingsvp, (SV *)newAV());
394  
395 /* A method invoked during a superclass constructor of a classic perl
396 * class might encounter $self without fields. If this is the case we'll
397 * have to create the fields now
398 * https://rt.cpan.org/Ticket/Display.html?id=132263
399 */
400 33 ▶ if(!backingsvp) {
Branch: 1 branch
BranchTrueFalseTotal
if(!backingsvp) { 2 31 33
401 2 struct ClassMeta *classmeta = mop_get_class_for_stash(SvSTASH(rv));
402 2 SV *fieldstore = (SV *)newAV();
403  
404 2 make_instance_fields(classmeta, fieldstore, 0);
405  
406 2 backingsvp = hv_fetchs((HV *)rv, "Object::Pad/slots", TRUE);
407 2 sv_setrv_noinc(*backingsvp, fieldstore);
408 }
409 33 ▶ if(!SvROK(*backingsvp) || SvTYPE(SvRV(*backingsvp)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(*backingsvp) || SvTYPE(SvRV(*backingsvp)) != SVt_PVAV) 33 0 33
if(!SvROK(*backingsvp) || SvTYPE(SvRV(*backingsvp)) != SVt_PVAV) 0 33 33
410 0 croak("Expected $self->{\"Object::Pad/slots\"} to be an ARRAY reference");
411 33 return SvRV(*backingsvp);
412 }
413  
414 case REPR_MAGIC:
415 8 case_REPR_MAGIC:
416 {
417 8 MAGIC *mg = mg_findext(rv, PERL_MAGIC_ext, &vtbl_backingav);
418 8 ▶ if(!mg && create)
Branch: 2 branches
BranchTrueFalseTotal
if(!mg && create) 2 6 8
if(!mg && create) 2 0 2
419 2 mg = sv_magicext(rv, (SV *)newAV(), PERL_MAGIC_ext, &vtbl_backingav, NULL, 0);
420 8 ▶ if(!mg)
Branch: 1 branch
BranchTrueFalseTotal
if(!mg) 0 8 8
421 0 croak("Expected to find backing AV magic extension");
422 8 return mg->mg_obj;
423 }
424  
425 38 case REPR_AUTOSELECT:
426 38 ▶ if(SvTYPE(rv) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) == SVt_PVHV) 30 8 38
427 30 goto case_REPR_HASH;
428 8 goto case_REPR_MAGIC;
429  
430 3 case REPR_KEYS:
431 {
432 /* TODO: This representation is going to be sloooooow
433 */
434 3 ▶ if(SvTYPE(rv) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVHV) 0 3 3
435 0 croak("Not a HASH reference");
436 3 HV *hv = (HV *)rv;
437 3 AV *backingav = newAV();
438 3 SAVEFREESV((SV *)backingav);
439 3 alias_fieldkeys_into_av(mop_get_class_for_stash(SvSTASH(rv)), hv, backingav);
440 3 return (SV *)backingav;
441 }
442  
443 32 case REPR_PVOBJ:
444 #ifdef HAVE_SVt_PVOBJ
445 32 ▶ if(SvTYPE(rv) != SVt_PVOBJ)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVOBJ) 0 32 32
446 0 croak("ARGH not an SVt_PVOBJ");
447  
448 32 return rv;
449 #else
450 croak("ARGH cannot SVt_PVOBJ on this version of perl");
451 #endif
452 }
453  
454 0 croak("ARGH unhandled repr type");
455 }
456  
457 0 SV *ObjectPad_get_obj_backingav(pTHX_ SV *self, enum ReprType repr, bool create)
458 {
459 0 ▶ if(repr == REPR_PVOBJ)
Branch: 1 branch
BranchTrueFalseTotal
if(repr == REPR_PVOBJ) 0 0 0
460 0 croak("ARGH cannot get_obj_backingav for REPR_PVOBJ because it isn't an AV");
461 else
462 0 return get_obj_fieldstore(self, repr, create);
463 }
464  
465 #define embed_cv(cv, embedding) S_embed_cv(aTHX_ cv, embedding)
466 59 static CV *S_embed_cv(pTHX_ CV *cv, RoleEmbedding *embedding)
467 {
468 assert(cv);
469 assert(CvOUTSIDE(cv));
470  
471 /* Perl core's cv_clone() would break in some situation here; see
472 * https://rt.cpan.org/Ticket/Display.html?id=141483
473 */
474 59 CV *embedded_cv = cv_copy_flags(cv, 0);
475 59 SV *embeddingsv = embedding->embeddingsv;
476  
477 assert(SvTYPE(embeddingsv) == SVt_PV && SvLEN(embeddingsv) >= sizeof(RoleEmbedding));
478  
479 59 PAD *pad1 = PadlistARRAY(CvPADLIST(embedded_cv))[1];
480 59 PadARRAY(pad1)[PADIX_EMBEDDING] = SvREFCNT_inc(embeddingsv);
481  
482 59 return embedded_cv;
483 }
484  
485 11 RoleEmbedding *ObjectPad__get_embedding_from_pad(pTHX)
486 {
487 /* Embedding info is stored in pad1; PAD_SVl() will look at CvDEPTH. We'll
488 * have to grab it manually */
489 11 PAD *pad1 = PadlistARRAY(CvPADLIST(find_runcv(0)))[1];
490 11 SV *embeddingsv = PadARRAY(pad1)[PADIX_EMBEDDING];
491 11 ▶ if(embeddingsv && embeddingsv != &PL_sv_undef)
Branch: 2 branches
BranchTrueFalseTotal
if(embeddingsv && embeddingsv != &PL_sv_undef) 11 0 11
if(embeddingsv && embeddingsv != &PL_sv_undef) 11 0 11
492 11 return MUST_ROLEEMBEDDING(SvPVX(embeddingsv));
493 else
494 0 return NULL;
495 }
496  
497 500 RoleEmbedding **ObjectPad_mop_class_get_direct_roles(pTHX_ const ClassMeta *meta, U32 *nroles)
498 {
499 assert(meta->type == METATYPE_CLASS);
500 500 AV *roles = meta->cls.direct_roles;
501 500 *nroles = av_count(roles);
502 500 return (RoleEmbedding **)AvARRAY(roles);
503 }
504  
505 79 RoleEmbedding **ObjectPad_mop_class_get_all_roles(pTHX_ const ClassMeta *meta, U32 *nroles)
506 {
507 assert(meta->type == METATYPE_CLASS);
508 79 AV *roles = meta->cls.embedded_roles;
509 79 *nroles = av_count(roles);
510 79 return (RoleEmbedding **)AvARRAY(roles);
511 }
512  
513 152 void ObjectPad__prepare_method_parse(pTHX_ ClassMeta *meta)
514 {
515 /* Save the methodscope for this subparse, in case of nested methods
516 * (RT132321)
517 */
518 152 SAVESPTR(meta->methodscope);
519  
520 /* While creating the new scope CV we need to ENTER a block so as not to
521 * break any interpvars
522 */
523 152 ENTER;
524 152 SAVESPTR(PL_comppad);
525 152 SAVESPTR(PL_comppad_name);
526 152 SAVESPTR(PL_curpad);
527  
528 152 CV *methodscope = meta->methodscope = MUTABLE_CV(newSV_type(SVt_PVCV));
529 152 CvPADLIST(methodscope) = pad_new(padnew_SAVE);
530  
531 152 PL_comppad = PadlistARRAY(CvPADLIST(methodscope))[1];
532 152 PL_comppad_name = PadlistNAMES(CvPADLIST(methodscope));
533 152 PL_curpad = AvARRAY(PL_comppad);
534  
535 /* We can't actually add the fields yet because we don't know if it'll be
536 * a :common method. Just save the seqnum for what they would be
537 */
538 152 meta->methodscope_seq = PL_cop_seqmax;
539 152 ▶ COP_SEQMAX_INC;
Branch: 2 branches
BranchTrueFalseTotal
COP_SEQMAX_INC; 0 152 152
COP_SEQMAX_INC; 0 0 0
540  
541 152 LEAVE;
542 152 }
543  
544 151 void ObjectPad__start_method_parse(pTHX_ ClassMeta *meta, bool is_common)
545 {
546 /* Splice in the field scope CV in */
547 151 CV *methodscope = meta->methodscope;
548  
549 151 ▶ if(CvANON(PL_compcv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvANON(PL_compcv)) 5 146 151
550 5 CvANON_on(methodscope);
551  
552 151 CvOUTSIDE (methodscope) = CvOUTSIDE (PL_compcv);
553 151 CvOUTSIDE_SEQ(methodscope) = CvOUTSIDE_SEQ(PL_compcv);
554  
555 151 CvOUTSIDE(PL_compcv) = methodscope;
556  
557 151 ▶ if(!is_common) {
Branch: 1 branch
BranchTrueFalseTotal
if(!is_common) { 146 5 151
558 /* instance method */
559 146 extend_pad_vars(meta);
560 146 intro_my();
561  
562 146 ENTER;
563 146 SAVESPTR(PL_comppad);
564 146 SAVESPTR(PL_comppad_name);
565 146 SAVESPTR(PL_curpad);
566  
567 146 PL_comppad = PadlistARRAY(CvPADLIST(methodscope))[1];
568 146 PL_comppad_name = PadlistNAMES(CvPADLIST(methodscope));
569 146 PL_curpad = AvARRAY(PL_comppad);
570  
571 /* Pretend we saw these variables at an earlier time */
572 assert(meta->methodscope_seq < CvOUTSIDE_SEQ(PL_compcv));
573 146 SAVEI32(PL_cop_seqmax);
574 146 PL_cop_seqmax = meta->methodscope_seq;
575  
576 146 add_fields_to_pad(meta, 0);
577  
578 146 intro_my();
579  
580 146 LEAVE;
581 }
582 else {
583 /* :common method */
584 PADOFFSET padix;
585  
586 5 padix = pad_add_name_pvs("$class", 0, NULL, NULL);
587 5 ▶ if(padix != PADIX_SELF)
Branch: 1 branch
BranchTrueFalseTotal
if(padix != PADIX_SELF) 0 5 5
588 0 croak("ARGH: Expected that padix[$class] = 1");
589 5 intro_my();
590 }
591  
592 151 ▶ if(meta->type == METATYPE_ROLE) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_ROLE) { 16 135 151
593 16 PAD *pad1 = PadlistARRAY(CvPADLIST(PL_compcv))[1];
594  
595 16 ▶ if(meta->role_is_invokable) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->role_is_invokable) { 3 13 16
596 3 SV *sv = PadARRAY(pad1)[PADIX_EMBEDDING];
597 3 ▶ SvUPGRADE(sv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PV); 3 0 3
598 3 SvPOK_on(sv);
599 3 SvLEN(sv) = 0;
600 3 SvPVX(sv) = (void *)&ObjectPad__embedding_standalone;
601 }
602 else {
603 13 SvREFCNT_dec(PadARRAY(pad1)[PADIX_EMBEDDING]);
604 /* Unembedded role CVs store the entire class map */
605 13 PadARRAY(pad1)[PADIX_EMBEDDING] = SvREFCNT_inc(meta->role.applied_classes);
606 }
607 }
608 151 }
609  
610 173 void ObjectPad__add_fields_to_pad(pTHX_ ClassMeta *meta, U32 since_field)
611 {
612 173 AV *fields = meta->fields;
613 173 U32 nfields = av_count(fields);
614  
615 U32 i;
616 307 ▶ for(i = since_field; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = since_field; i < nfields; i++) { 134 173 307
617 134 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
618  
619 /* Skip the anonymous ones */
620 134 ▶ if(SvCUR(fieldmeta->name) < 2)
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(fieldmeta->name) < 2) 0 134 134
621 0 continue;
622  
623 /* includes the non-direct ones */
624  
625 /* Claim these are all STATE variables just to quiet the "will not stay
626 * shared" warning */
627 134 pad_add_name_sv(fieldmeta->name, padadd_STATE, NULL, NULL);
628 }
629 173 }
630  
631 #define find_padix_for_field(fieldmeta) S_find_padix_for_field(aTHX_ fieldmeta)
632 95 static PADOFFSET S_find_padix_for_field(pTHX_ FieldMeta *fieldmeta)
633 {
634 95 const char *fieldname = SvPVX(fieldmeta->name);
635 #if HAVE_PERL_VERSION(5, 20, 0)
636 95 const PADNAMELIST *nl = PadlistNAMES(CvPADLIST(PL_compcv));
637 95 PADNAME **names = PadnamelistARRAY(nl);
638 PADOFFSET padix;
639  
640 352 ▶ for(padix = 1; padix <= PadnamelistMAXNAMED(nl); padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = 1; padix <= PadnamelistMAXNAMED(nl); padix++) { 352 0 352
641 352 PADNAME *name = names[padix];
642  
643 352 ▶ if(!name || !PadnameLEN(name))
Branch: 2 branches
BranchTrueFalseTotal
if(!name || !PadnameLEN(name)) 341 11 352
if(!name || !PadnameLEN(name)) 9 332 341
644 20 continue;
645  
646 332 const char *pv = PadnamePV(name);
647 332 ▶ if(!pv)
Branch: 1 branch
BranchTrueFalseTotal
if(!pv) 0 332 332
648 0 continue;
649  
650 /* field names are all OUTER vars. This is necessary so we don't get
651 * confused by signatures params of the same name
652 * https://rt.cpan.org/Ticket/Display.html?id=134456
653 */
654 332 ▶ if(!PadnameOUTER(name))
Branch: 1 branch
BranchTrueFalseTotal
if(!PadnameOUTER(name)) 237 95 332
655 237 continue;
656 95 ▶ if(!strEQ(pv, fieldname))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(pv, fieldname)) 0 95 95
657 0 continue;
658  
659 /* TODO: for extra robustness we could compare the SV * in the pad itself */
660  
661 95 return padix;
662 }
663  
664 0 return NOT_IN_PAD;
665 #else
666 /* Before the new pad API, the best we can do is call pad_findmy_pv()
667 * It won't get confused about signatures params because these perls are too
668 * old for signatures anyway
669 */
670 return pad_findmy_pv(fieldname, 0);
671 #endif
672 }
673  
674 0 FieldMeta *ObjectPad_get_field_for_padix(pTHX_ PADOFFSET padix)
675 {
676 0 CV *scope = PL_compcv;
677  
678 0 ClassMeta *classmeta = get_compclassmeta();
679  
680 0 ▶ while(scope && scope != classmeta->methodscope) {
Branch: 2 branches
BranchTrueFalseTotal
while(scope && scope != classmeta->methodscope) { 0 0 0
while(scope && scope != classmeta->methodscope) { 0 0 0
681 0 PADNAMELIST *pnl = PadlistNAMES(CvPADLIST(scope));
682 0 PADNAME *name = PadnamelistARRAY(pnl)[padix];
683  
684 /* field names are all OUTER vars */
685 0 ▶ if(!PadnameOUTER(name))
Branch: 1 branch
BranchTrueFalseTotal
if(!PadnameOUTER(name)) 0 0 0
686 0 return NULL;
687  
688 0 padix = PARENT_PAD_INDEX(name);
689 0 scope = CvOUTSIDE(scope);
690 }
691  
692 0 ▶ if(!scope)
Branch: 1 branch
BranchTrueFalseTotal
if(!scope) 0 0 0
693 0 return NULL;
694  
695 /* padix refers to the pad offset within the methodscope, and we know that
696 * the method scope contains all the fields in the right order
697 */
698 0 AV *fields = classmeta->fields;
699 0 ▶ if(av_count(fields) <= (padix - 1))
Branch: 1 branch
BranchTrueFalseTotal
if(av_count(fields) <= (padix - 1)) 0 0 0
700 0 return NULL;
701  
702 0 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[padix - 1]);
703  
704 /* TODO: assert on the field name being equal to the padname */
705  
706 0 return fieldmeta;
707 }
708  
709 #define prepend_methstart_ops(meta, outerscope, body, is_common) S_prepend_methstart_ops(aTHX_ meta, outerscope, body, is_common)
710 344 static OP *S_prepend_methstart_ops(pTHX_ ClassMeta *meta, CV *outerscope, OP *body, bool is_common)
711 {
712 344 ▶ U32 opflags_if_role = (meta->type == METATYPE_ROLE) ? OPfMETHSTART_ROLE : 0;
Branch: 1 branch
BranchTrueFalseTotal
U32 opflags_if_role = (meta->type == METATYPE_ROLE) ? OPfMETHSTART_ROLE : 0; 44 300 344
713 344 AV *fields = meta->fields;
714 344 I32 nfields = av_count(fields);
715  
716 344 ▶ PADNAMELIST *fieldnames = outerscope ? PadlistNAMES(CvPADLIST(outerscope)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PADNAMELIST *fieldnames = outerscope ? PadlistNAMES(CvPADLIST(outerscope)) : NULL; 145 199 344
717 344 ▶ PADNAME **snames = fieldnames ? PadnamelistARRAY(fieldnames) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PADNAME **snames = fieldnames ? PadnamelistARRAY(fieldnames) : NULL; 145 199 344
718  
719 344 OP *ops = NULL, *methstartop;
720  
721 #if HAVE_PERL_VERSION(5, 26, 0)
722 /* If we're on a perl with signatures, we will already have inserted an
723 * OP_ARGELEM to perform the `$self = shift` behaviour rather than letting
724 * OP_METHSTART do it. We need to put OP_METHSTART immediately after this,
725 * so that its field bindings are visible to subsequent param defaulting
726 * expressions that use field vars.
727 */
728 344 OP *argcheck = NULL;
729 {
730 344 OP *o = body;
731 1319 ▶ while(o) {
Branch: 1 branch
BranchTrueFalseTotal
while(o) { 1319 0 1319
732 1319 ▶ if(o->op_type == OP_ARGCHECK) {
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_type == OP_ARGCHECK) { 11 1308 1319
733 11 argcheck = o;
734 11 break;
735 }
736 1308 ▶ else if(o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE)
Branch: 2 branches
BranchTrueFalseTotal
else if(o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE) 982 326 1308
else if(o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE) 0 982 982
737 326 ▶ o = OpSIBLING(o);
Branch: 1 branch
BranchTrueFalseTotal
o = OpSIBLING(o); 326 0 326
738 982 ▶ else if(o->op_flags & OPf_KIDS)
Branch: 1 branch
BranchTrueFalseTotal
else if(o->op_flags & OPf_KIDS) 649 333 982
739 649 o = cUNOPo->op_first;
740 else
741 333 break;
742 }
743 }
744  
745 344 ▶ if(argcheck) {
Branch: 1 branch
BranchTrueFalseTotal
if(argcheck) { 11 333 344
746 OP *next;
747  
748 11 ▶ next = OpSIBLING(argcheck);
Branch: 1 branch
BranchTrueFalseTotal
next = OpSIBLING(argcheck); 11 0 11
749 11 ▶ if(next->op_type == OP_NEXTSTATE || next->op_type == OP_DBSTATE)
Branch: 2 branches
BranchTrueFalseTotal
if(next->op_type == OP_NEXTSTATE || next->op_type == OP_DBSTATE) 0 11 11
if(next->op_type == OP_NEXTSTATE || next->op_type == OP_DBSTATE) 0 0 0
750 11 ▶ next = OpSIBLING(next);
Branch: 1 branch
BranchTrueFalseTotal
next = OpSIBLING(next); 11 0 11
751  
752 /* next ought to be the $self OP_ARGELEM we inserted */
753 assert(next);
754 assert(next->op_type == OP_ARGELEM);
755 assert(next->op_targ == PADIX_SELF);
756 assert(PTR2UV(cUNOP_AUXx(next)->op_aux) == 0);
757  
758 11 OP *selfargelem = next;
759 11 ▶ next = OpSIBLING(selfargelem);
Branch: 1 branch
BranchTrueFalseTotal
next = OpSIBLING(selfargelem); 11 0 11
760  
761 11 ▶ if(!is_common)
Branch: 1 branch
BranchTrueFalseTotal
if(!is_common) 10 1 11
762 10 methstartop = newMETHSTARTOP(0 | opflags_if_role | (meta->repr << 8));
763 else
764 1 methstartop = newCOMMONMETHSTARTOP(0 | (meta->repr << 8));
765  
766 11 OpMORESIB_set(selfargelem, methstartop);
767  
768 11 OpMORESIB_set(methstartop, next);
769 }
770 else
771 #endif
772 {
773 333 ▶ if(!is_common)
Branch: 1 branch
BranchTrueFalseTotal
if(!is_common) 330 3 333
774 330 ops = op_append_list(OP_LINESEQ, ops,
775 methstartop = newMETHSTARTOP(OPf_STACKED | opflags_if_role | (meta->repr << 8))
776 );
777 else
778 3 ops = op_append_list(OP_LINESEQ, ops,
779 methstartop = newCOMMONMETHSTARTOP(OPf_STACKED | (meta->repr << 8)));
780 }
781  
782 344 ▶ if(!is_common) {
Branch: 1 branch
BranchTrueFalseTotal
if(!is_common) { 340 4 344
783 340 AV *fieldmap = newAV();
784 340 U32 fieldcount = 0, max_fieldix = 0;
785  
786 340 SAVEFREESV((SV *)fieldmap);
787  
788 340 PADNAME **padnames = PadnamelistARRAY(PadlistNAMES(CvPADLIST(PL_compcv)));
789 340 U32 cop_seq_low = COP_SEQ_RANGE_LOW(padnames[PADIX_SELF]);
790  
791 int i;
792 582 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 242 340 582
793 242 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
794  
795 242 ▶ if(snames) {
Branch: 1 branch
BranchTrueFalseTotal
if(snames) { 107 135 242
796 107 PADNAME *fieldname = snames[i + 1];
797  
798 107 ▶ if(!fieldname || PadnameREFCNT(fieldname) < 2)
Branch: 2 branches
BranchTrueFalseTotal
if(!fieldname || PadnameREFCNT(fieldname) < 2) 107 0 107
if(!fieldname || PadnameREFCNT(fieldname) < 2) 12 95 107
799 12 continue;
800 }
801  
802 /* TODO: Find a better test for initfields so it doesn't think we capture
803 * every field declared up til now. */
804 230 FIELDOFFSET fieldix = fieldmeta->fieldix;
805 95 PADOFFSET padix = outerscope ? find_padix_for_field(fieldmeta)
806 230 ▶ : pad_findmy_pv(SvPVX(fieldmeta->name), 0);
Branch: 1 branch
BranchTrueFalseTotal
: pad_findmy_pv(SvPVX(fieldmeta->name), 0); 95 135 230
807  
808 230 ▶ if(padix == NOT_IN_PAD)
Branch: 1 branch
BranchTrueFalseTotal
if(padix == NOT_IN_PAD) 110 120 230
809 110 continue;
810  
811 120 U8 private = 0;
812 240 switch(SvPV_nolen(fieldmeta->name)[0]) {
813 100 case '$': private = OPpFIELDPAD_SV; break;
814 13 case '@': private = OPpFIELDPAD_AV; break;
815 7 case '%': private = OPpFIELDPAD_HV; break;
816 }
817  
818 PERL_UNUSED_VAR(opflags_if_role);
819 assert((fieldix & ~FIELDIX_MASK) == 0);
820 120 av_store(fieldmap, padix, newSVuv(((UV)private << FIELDIX_TYPE_SHIFT) | fieldix));
821 120 fieldcount++;
822 120 ▶ if(fieldix > max_fieldix)
Branch: 1 branch
BranchTrueFalseTotal
if(fieldix > max_fieldix) 38 82 120
823 38 max_fieldix = fieldix;
824  
825 120 ▶ if(snames) {
Branch: 1 branch
BranchTrueFalseTotal
if(snames) { 95 25 120
826 95 PADNAME *fieldname = snames[i + 1];
827  
828 /* Unshare the padname so the one in the methodscope pad returns to refcount 1 */
829 95 PADNAME *newpadname = newPADNAMEpvn(PadnamePV(fieldname), PadnameLEN(fieldname));
830 95 PadnameREFCNT_dec(padnames[padix]);
831 95 padnames[padix] = newpadname;
832  
833 /* Turn off OUTER and set a valid COP sequence range, so the lexical is
834 * visible to eval(), PadWalker, perldb, etc.. */
835 95 PadnameOUTER_off(newpadname);
836 95 COP_SEQ_RANGE_LOW(newpadname) = cop_seq_low;
837 95 COP_SEQ_RANGE_HIGH(newpadname) = PL_cop_seqmax;
838 }
839 }
840  
841 340 ▶ if(fieldcount) {
Branch: 1 branch
BranchTrueFalseTotal
if(fieldcount) { 88 252 340
842 UNOP_AUX_item *aux;
843 88 Newx(aux, 2 + fieldcount*2, UNOP_AUX_item);
844 88 cUNOP_AUXx(methstartop)->op_aux = aux;
845  
846 88 (aux++)->uv = fieldcount;
847 88 (aux++)->uv = max_fieldix;
848  
849 536 ▶ for(Size_t i = 0; i < av_count(fieldmap); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < av_count(fieldmap); i++) { 448 88 536
850 448 ▶ if(!AvARRAY(fieldmap)[i] || !SvOK(AvARRAY(fieldmap)[i]))
Branch: 2 branches
BranchTrueFalseTotal
if(!AvARRAY(fieldmap)[i] || !SvOK(AvARRAY(fieldmap)[i])) 120 328 448
if(!AvARRAY(fieldmap)[i] || !SvOK(AvARRAY(fieldmap)[i])) 0 120 120
851 328 continue;
852  
853 120 (aux++)->uv = i;
854 120 (aux++)->uv = SvUV(AvARRAY(fieldmap)[i]);
855 }
856 }
857 }
858  
859 344 return op_append_list(OP_LINESEQ, ops, body);
860 }
861  
862 148 OP *ObjectPad__finish_method_parse(pTHX_ ClassMeta *meta, bool is_common, OP *body)
863 {
864 assert(meta->methodscope && SvTYPE(meta->methodscope) == SVt_PVCV);
865  
866 /* If we have no body that means this was a bodyless method
867 * declaration; a required method for a role
868 */
869 148 ▶ if(body && !is_common) {
Branch: 2 branches
BranchTrueFalseTotal
if(body && !is_common) { 145 3 148
if(body && !is_common) { 141 4 145
870 {
871 141 ENTER;
872 141 SAVEVPTR(PL_curcop);
873  
874 141 PADNAME **padnames = PadnamelistARRAY(PadlistNAMES(CvPADLIST(PL_compcv)));
875  
876 /* See https://rt.cpan.org/Ticket/Display.html?id=132428
877 * https://github.com/Perl/perl5/issues/17754
878 */
879 PADOFFSET padix;
880 506 ▶ for(padix = PADIX_SELF + 1; padix <= PadnamelistMAX(PadlistNAMES(CvPADLIST(PL_compcv))); padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = PADIX_SELF + 1; padix <= PadnamelistMAX(PadlistNAMES(CvPADLIST(PL_compcv))); padix++) { 365 141 506
881 365 PADNAME *pn = padnames[padix];
882  
883 365 ▶ if(PadnameIsNULL(pn) || !PadnameLEN(pn))
Branch: 2 branches
BranchTrueFalseTotal
if(PadnameIsNULL(pn) || !PadnameLEN(pn)) 339 26 365
if(PadnameIsNULL(pn) || !PadnameLEN(pn)) 22 317 339
884 364 continue;
885  
886 317 const char *pv = PadnamePV(pn);
887 317 ▶ if(!pv || !strEQ(pv, "$self"))
Branch: 2 branches
BranchTrueFalseTotal
if(!pv || !strEQ(pv, "$self")) 317 0 317
if(!pv || !strEQ(pv, "$self")) 316 1 317
888 316 continue;
889  
890 1 COP *padcop = NULL;
891 1 ▶ if(find_cop_for_lvintro(padix, body, &padcop))
Branch: 1 branch
BranchTrueFalseTotal
if(find_cop_for_lvintro(padix, body, &padcop)) 1 0 1
892 1 PL_curcop = padcop;
893 1 warn("\"my\" variable $self masks earlier declaration in same scope");
894 }
895  
896 141 LEAVE;
897 }
898  
899 141 body = prepend_methstart_ops(meta, meta->methodscope, body, false);
900 }
901 7 ▶ else if(body && is_common) {
Branch: 2 branches
BranchTrueFalseTotal
else if(body && is_common) { 4 3 7
else if(body && is_common) { 4 0 4
902 4 body = prepend_methstart_ops(meta, meta->methodscope, body, true);
903 }
904  
905 148 meta->methodscope = NULL;
906  
907 /* Restore CvOUTSIDE(PL_compcv) back to where it should be */
908 {
909 148 CV *outside = CvOUTSIDE(PL_compcv);
910 148 PADNAMELIST *pnl = PadlistNAMES(CvPADLIST(PL_compcv));
911 148 PADNAMELIST *outside_pnl = PadlistNAMES(CvPADLIST(outside));
912  
913 /* Lexical captures will need their parent pad index fixing
914 * Technically these only matter for CvANON because they're only used when
915 * reconstructing the parent pad captures by OP_ANONCODE. But we might as
916 * well be polite and fix them for all CVs
917 */
918 PADOFFSET padix;
919 675 ▶ for(padix = 1; padix <= PadnamelistMAX(pnl); padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = 1; padix <= PadnamelistMAX(pnl); padix++) { 527 148 675
920 527 PADNAME *pn = PadnamelistARRAY(pnl)[padix];
921 527 ▶ if(PadnameIsNULL(pn) ||
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsNULL(pn) || 499 28 527
922 499 ▶ !PadnameOUTER(pn) ||
Branch: 1 branch
BranchTrueFalseTotal
!PadnameOUTER(pn) || 35 464 499
923 35 ▶ !PARENT_PAD_INDEX(pn))
Branch: 1 branch
BranchTrueFalseTotal
!PARENT_PAD_INDEX(pn)) 0 35 35
924 492 continue;
925  
926 35 PADNAME *outside_pn = PadnamelistARRAY(outside_pnl)[PARENT_PAD_INDEX(pn)];
927  
928 35 PARENT_PAD_INDEX_set(pn, PARENT_PAD_INDEX(outside_pn));
929 35 ▶ if(!PadnameOUTER(outside_pn))
Branch: 1 branch
BranchTrueFalseTotal
if(!PadnameOUTER(outside_pn)) 0 35 35
930 0 PadnameOUTER_off(pn);
931 }
932  
933 148 CvOUTSIDE(PL_compcv) = CvOUTSIDE(outside);
934 148 CvOUTSIDE_SEQ(PL_compcv) = CvOUTSIDE_SEQ(outside);
935 }
936  
937 148 return body;
938 }
939  
940 11 void ObjectPad__prepare_adjust_params(pTHX_ ClassMeta *meta)
941 {
942 /* Skip the PADIX_EMBEDDING slot if not already done so */
943 11 ▶ if(meta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type != METATYPE_ROLE) 10 1 11
944 10 pad_add_name_pvs("", 0, NULL, NULL);
945  
946 11 PADOFFSET params_padix = pad_add_name_pvs("%(params)", 0, NULL, NULL);
947 assert(params_padix == PADIX_PARAMS);
948 PERL_UNUSED_VAR(params_padix);
949  
950 11 intro_my();
951 11 }
952  
953 9 void ObjectPad__parse_adjust_params(pTHX_ ClassMeta *meta, AV *params)
954 {
955 /* This is a custom parser because XPK won't handle this */
956 9 ▶ if(lex_peek_unichar(0) != '(')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '(') 0 9 9
957 0 croak("Expected ADJUST :params signature in parens");
958 9 lex_read_unichar(0);
959  
960 9 ▶ if(!meta->parammap)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->parammap) 8 1 9
961 8 meta->parammap = newHV();
962  
963 9 HV *parammap = meta->parammap;
964  
965 9 bool seen_slurpy = false;
966  
967 8 while(1) {
968 17 lex_read_space(0);
969  
970 /* Should now follow a sequence of comma-separated elements; each element is
971 * :$NAME or
972 * :$NAME = EXPR
973 * :$NAME //= EXPR
974 * :$NAME ||= EXPR
975 * The final one may also be
976 * %NAME
977 */
978 17 char c = lex_peek_unichar(0);
979 17 ▶ if(c == ')')
Branch: 1 branch
BranchTrueFalseTotal
if(c == ')') 2 15 17
980 2 break;
981  
982 15 ▶ if(seen_slurpy)
Branch: 1 branch
BranchTrueFalseTotal
if(seen_slurpy) 0 15 15
983 0 croak("Cannot have more parameters after the final slurpy one");
984  
985 15 ▶ if(c == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == ':') { 13 2 15
986 13 lex_read_unichar(0);
987 13 lex_read_space(0);
988  
989 13 SV *varname = lex_scan_lexvar();
990 13 lex_read_space(0);
991  
992 13 ▶ if(SvPVX(varname)[0] != '$')
Branch: 1 branch
BranchTrueFalseTotal
if(SvPVX(varname)[0] != '$') 0 13 13
993 0 croak("Expected a named scalar parameter");
994  
995 13 SV *paramname = newSVpvn(SvPVX(varname)+1, SvCUR(varname)-1);
996  
997 13 check_colliding_param(meta, paramname);
998  
999 12 PADOFFSET padix = pad_add_name_sv(varname, 0, NULL, NULL);
1000  
1001 ParamMeta *parammeta;
1002 12 Newx(parammeta, 1, struct ParamMeta);
1003  
1004 12 *parammeta = (struct ParamMeta){
1005 LINNET_INIT(LINNET_VAL_PARAMMETA)
1006 .name = paramname,
1007 .class = meta,
1008 .type = PARAM_ADJUST,
1009 .adjust.padix = padix,
1010 };
1011  
1012 12 av_push(params, newSVuv(PTR2UV((SV *)parammeta)));
1013 12 hv_store_ent(parammap, paramname, (SV *)parammeta, 0);
1014  
1015 12 ▶ if(lex_consume("=")) {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_consume("=")) { 7 5 12
1016 7 lex_read_space(0);
1017 7 parammeta->adjust.defexpr = parse_termexpr(0);
1018 }
1019 5 ▶ else if(lex_consume("//=")) {
Branch: 1 branch
BranchTrueFalseTotal
else if(lex_consume("//=")) { 1 4 5
1020 1 lex_read_space(0);
1021 1 parammeta->adjust.defexpr = parse_termexpr(0);
1022 1 parammeta->adjust.def_if_undef = 1;
1023 }
1024 4 ▶ else if(lex_consume("||=")) {
Branch: 1 branch
BranchTrueFalseTotal
else if(lex_consume("||=")) { 1 3 4
1025 1 lex_read_space(0);
1026 1 parammeta->adjust.defexpr = parse_termexpr(0);
1027 1 parammeta->adjust.def_if_false = 1;
1028 }
1029  
1030 12 intro_my();
1031 }
1032 2 ▶ else if(c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
else if(c == '%') { 2 0 2
1033 2 SV *varname = lex_scan_lexvar();
1034  
1035 /* Lets now be evil and simply rename %(params) to this. Due to the way
1036 * that the PADNAME structure itself contains the string, we can't
1037 * just change the name *inside* it. Instead we'll have to allocate a
1038 * new one and swap it in.
1039 */
1040 2 PADNAME **pnp = &PadnamelistARRAY(PL_comppad_name)[PADIX_PARAMS];
1041  
1042 2 PADNAME *new_pn = newPADNAMEpvn(SvPVX(varname), SvCUR(varname));
1043 2 COP_SEQ_RANGE_LOW_set(new_pn, COP_SEQ_RANGE_LOW(*pnp));
1044  
1045 2 PadnameREFCNT_dec(*pnp);
1046 2 *pnp = new_pn;
1047  
1048 /* Don't need to intro_my() because the padname has already been
1049 * introduced
1050 */
1051  
1052 2 seen_slurpy = true;
1053 }
1054 else
1055 0 croak("Expected a named scalar parameter or slurpy hash");
1056  
1057 14 lex_read_space(0);
1058 14 c = lex_peek_unichar(0);
1059  
1060 14 ▶ if(c == ')')
Branch: 1 branch
BranchTrueFalseTotal
if(c == ')') 6 8 14
1061 6 break;
1062 8 ▶ if(c != ',')
Branch: 1 branch
BranchTrueFalseTotal
if(c != ',') 0 8 8
1063 0 croak("Expected , or end of signature parens");
1064  
1065 8 lex_read_unichar(0);
1066 }
1067  
1068 /* consume the ')' */
1069 8 lex_read_unichar(0);
1070  
1071 8 lex_read_space(0);
1072 8 }
1073  
1074 14 static OP *pp_bind_params_hv(pTHX)
1075 {
1076 14 HV *params = HV_FROM_REF(*av_fetch(GvAV(PL_defgv), 0, 0));
1077  
1078 14 SAVESPTR(PAD_SVl(PADIX_PARAMS));
1079 14 PAD_SVl(PADIX_PARAMS) = SvREFCNT_inc(params);
1080 14 save_freesv((SV *)params);
1081  
1082 14 return NORMAL;
1083 }
1084  
1085 9 OP *ObjectPad__finish_adjust_params(pTHX_ ClassMeta *meta, AV *params, OP *body)
1086 {
1087 9 OP *paramsops = NULL;
1088  
1089 9 paramsops = op_append_elem(OP_LINESEQ, paramsops,
1090 newOP_CUSTOM(&pp_bind_params_hv, 0));
1091  
1092 21 ▶ for(U32 i = 0; params && i < av_count(params); i++) {
Branch: 2 branches
BranchTrueFalseTotal
for(U32 i = 0; params && i < av_count(params); i++) { 21 0 21
for(U32 i = 0; params && i < av_count(params); i++) { 12 9 21
1093 12 ParamMeta *parammeta = MUST_PARAMMETA(SvUV(AvARRAY(params)[i]));
1094  
1095 12 SV *paramname = parammeta->name;
1096 12 OP *defexpr = parammeta->adjust.defexpr;
1097  
1098 12 ▶ if(!defexpr)
Branch: 1 branch
BranchTrueFalseTotal
if(!defexpr) 3 9 12
1099 3 defexpr = newop_croak_from_constructor(
1100 newSVpvf("Required parameter '%" SVf "' is missing for %" SVf " constructor",
1101 SVfARG(paramname), SVfARG(meta->name)));
1102  
1103 OP *helemop =
1104 12 newBINOP(OP_HELEM, 0,
1105 newPADxVOP(OP_PADHV, OPf_REF, PADIX_PARAMS),
1106 newSVOP(OP_CONST, 0, SvREFCNT_inc(paramname)));
1107  
1108 OP *rhs;
1109 12 ▶ if(parammeta->adjust.def_if_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if(parammeta->adjust.def_if_undef) { 1 11 12
1110 /* delete $(params){KEY} // DEFEXPR */
1111 1 rhs = newLOGOP(OP_DOR, 0, newUNOP(OP_DELETE, 0, helemop), defexpr);
1112 }
1113 11 ▶ else if(parammeta->adjust.def_if_false) {
Branch: 1 branch
BranchTrueFalseTotal
else if(parammeta->adjust.def_if_false) { 1 10 11
1114 /* delete $(params){KEY} || DEFEXPR */
1115 1 rhs = newLOGOP(OP_OR, 0, newUNOP(OP_DELETE, 0, helemop), defexpr);
1116 }
1117 else {
1118 /* Equivalent of
1119 * exists $(params){KEY} ? delete $(params){KEY} : DEFEXPR; */
1120 10 rhs = newHELEMEXISTSOROP(OPpHELEMEXISTSOR_DELETE << 8, helemop, defexpr);
1121 }
1122  
1123 12 paramsops = op_append_elem(OP_LINESEQ, paramsops,
1124 newBINOP(OP_SASSIGN, 0,
1125 rhs,
1126 newPADxVOP(OP_PADSV, OPf_MOD|OPf_REF, parammeta->adjust.padix)));
1127 }
1128  
1129 9 return op_append_list(OP_LINESEQ, paramsops, body);
1130 }
1131  
1132 157 MethodMeta *ObjectPad_mop_class_add_method(pTHX_ ClassMeta *meta, SV *methodname)
1133 {
1134 157 AV *methods = meta->direct_methods;
1135  
1136 157 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 157 157
1137 0 croak("Cannot add a new method to a class that is not yet begun");
1138 157 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 157 157
1139 0 croak("Cannot add a new method to an already-sealed class");
1140  
1141 157 ▶ if(!methodname || !SvOK(methodname) || !SvCUR(methodname))
Branch: 3 branches
BranchTrueFalseTotal
if(!methodname || !SvOK(methodname) || !SvCUR(methodname)) 157 0 157
if(!methodname || !SvOK(methodname) || !SvCUR(methodname)) 156 1 157
if(!methodname || !SvOK(methodname) || !SvCUR(methodname)) 1 155 156
1142 2 croak("methodname must not be undefined or empty");
1143  
1144 U32 i;
1145 221 ▶ for(i = 0; i < av_count(methods); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(methods); i++) { 68 153 221
1146 68 MethodMeta *methodmeta = MUST_METHODMETA(AvARRAY(methods)[i]);
1147 68 ▶ if(sv_eq(methodmeta->name, methodname)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sv_eq(methodmeta->name, methodname)) { 2 66 68
1148 2 ▶ if(methodmeta->role)
Branch: 1 branch
BranchTrueFalseTotal
if(methodmeta->role) 1 1 2
1149 1 croak("Method '%" SVf "' clashes with the one provided by role %" SVf,
1150 SVfARG(methodname), SVfARG(methodmeta->role->name));
1151 else
1152 1 croak("Cannot add another method named %" SVf, methodname);
1153 }
1154 }
1155  
1156 MethodMeta *methodmeta;
1157 153 Newx(methodmeta, 1, MethodMeta);
1158  
1159 153 *methodmeta = (MethodMeta){
1160 LINNET_INIT(LINNET_VAL_METHODMETA)
1161 153 .name = SvREFCNT_inc(methodname),
1162 .class = meta,
1163 };
1164  
1165 153 av_push(methods, (SV *)methodmeta);
1166  
1167 153 return methodmeta;
1168 }
1169  
1170 47 MethodMeta *ObjectPad_mop_class_add_method_cv(pTHX_ ClassMeta *meta, SV *methodname, CV *cv)
1171 {
1172 47 MethodMeta *methodmeta = mop_class_add_method(meta, methodname);
1173  
1174 44 I32 klen = SvCUR(methodname);
1175 44 ▶ if(SvUTF8(methodname))
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(methodname)) 34 10 44
1176 34 klen = -klen;
1177 44 GV **gvp = (GV **)hv_fetch(meta->stash, SvPVX(methodname), klen, GV_ADD);
1178 44 gv_init_sv(*gvp, meta->stash, methodname, 0);
1179 44 GvMULTI_on(*gvp);
1180  
1181 44 GvCV_set(*gvp, cv);
1182 44 CvGV_set(cv, *gvp);
1183  
1184 44 return methodmeta;
1185 }
1186  
1187 141 FieldMeta *ObjectPad_mop_class_add_field(pTHX_ ClassMeta *meta, SV *fieldname)
1188 {
1189 141 AV *fields = meta->fields;
1190  
1191 141 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 141 141
1192 0 croak("Cannot add a new field to a class that is not yet begun");
1193 141 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 141 141
1194 0 croak("Cannot add a new field to an already-sealed class");
1195  
1196 141 ▶ if(!fieldname || !SvOK(fieldname) || !SvCUR(fieldname))
Branch: 3 branches
BranchTrueFalseTotal
if(!fieldname || !SvOK(fieldname) || !SvCUR(fieldname)) 141 0 141
if(!fieldname || !SvOK(fieldname) || !SvCUR(fieldname)) 140 1 141
if(!fieldname || !SvOK(fieldname) || !SvCUR(fieldname)) 1 139 140
1197 2 croak("fieldname must not be undefined or empty");
1198  
1199 139 ▶ switch(SvPV_nolen(fieldname)[0]) {
Branch: 1 branch
BranchTrueFalseTotal
switch(SvPV_nolen(fieldname)[0]) { 138 1 139
1200 138 case '$':
1201 case '@':
1202 case '%':
1203 138 break;
1204  
1205 1 default:
1206 1 croak("fieldname must begin with a sigil");
1207 }
1208  
1209 138 ▶ if(mop_class_find_field(meta, fieldname, 0))
Branch: 1 branch
BranchTrueFalseTotal
if(mop_class_find_field(meta, fieldname, 0)) 1 137 138
1210 1 croak("Cannot add another field named %" SVf, fieldname);
1211  
1212 137 FieldMeta *fieldmeta = mop_create_field(fieldname, meta->next_fieldix, meta);
1213  
1214 137 av_push(fields, (SV *)fieldmeta);
1215 137 meta->next_fieldix++;
1216  
1217 137 ▶ MOP_CLASS_RUN_HOOKS(meta, post_add_field, fieldmeta);
Branch: 3 branches
BranchTrueFalseTotal
MOP_CLASS_RUN_HOOKS(meta, post_add_field, fieldmeta); 0 0 0
MOP_CLASS_RUN_HOOKS(meta, post_add_field, fieldmeta); 0 137 137
MOP_CLASS_RUN_HOOKS(meta, post_add_field, fieldmeta); 0 0 0
1218  
1219 137 return fieldmeta;
1220 }
1221  
1222 168 FieldMeta *ObjectPad_mop_class_find_field(pTHX_ ClassMeta *meta, SV *fieldname, U32 flags)
1223 {
1224 168 AV *fields = meta->fields;
1225  
1226 168 U32 i, nfields = av_count(fields);
1227 241 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 99 142 241
1228 99 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
1229 99 ▶ if(SvCUR(fieldmeta->name) < 2)
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(fieldmeta->name) < 2) 5 94 99
1230 5 continue;
1231  
1232 94 ▶ if((flags & FIND_FIELD_ONLY_DIRECT) && !(fieldmeta->is_direct))
Branch: 2 branches
BranchTrueFalseTotal
if((flags & FIND_FIELD_ONLY_DIRECT) && !(fieldmeta->is_direct)) 10 84 94
if((flags & FIND_FIELD_ONLY_DIRECT) && !(fieldmeta->is_direct)) 1 9 10
1233 1 continue;
1234 93 ▶ if((flags & FIND_FIELD_ONLY_INHERITABLE) && !(fieldmeta->is_inheritable))
Branch: 2 branches
BranchTrueFalseTotal
if((flags & FIND_FIELD_ONLY_INHERITABLE) && !(fieldmeta->is_inheritable)) 4 89 93
if((flags & FIND_FIELD_ONLY_INHERITABLE) && !(fieldmeta->is_inheritable)) 0 4 4
1235 0 continue;
1236  
1237 93 ▶ if(sv_eq(fieldmeta->name, fieldname))
Branch: 1 branch
BranchTrueFalseTotal
if(sv_eq(fieldmeta->name, fieldname)) 26 67 93
1238 26 return fieldmeta;
1239 }
1240  
1241 142 return NULL;
1242 }
1243  
1244 18 void ObjectPad_mop_class_add_BUILD(pTHX_ ClassMeta *meta, CV *cv)
1245 {
1246 18 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 18 18
1247 0 croak("Cannot add a new BUILD phaser to a class that is not yet begun");
1248 18 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 18 18
1249 0 croak("Cannot add a BUILD phaser to an already-sealed class");
1250 18 ▶ if(meta->strict_params)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->strict_params) 0 18 18
1251 0 croak("Cannot add a BUILD to a class with :strict(params)");
1252  
1253 18 ▶ if(!meta->buildcvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->buildcvs) 15 3 18
1254 15 meta->buildcvs = newAV();
1255  
1256 18 av_push(meta->buildcvs, (SV *)cv);
1257 18 }
1258  
1259 30 void ObjectPad_mop_class_add_ADJUST(pTHX_ ClassMeta *meta, CV *cv)
1260 {
1261 30 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 30 30
1262 0 croak("Cannot add a new ADJUST phaser to a class that is not yet begun");
1263 30 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 30 30
1264 0 croak("Cannot add an ADJUST(PARAMS) phaser to an already-sealed class");
1265  
1266 30 warn_outofblock_ops(CvROOT(cv), "Using %s to leave an ADJUST phaser is discouraged and will be removed in a later version");
1267  
1268 30 ▶ if(!meta->adjustcvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->adjustcvs) 26 4 30
1269 26 meta->adjustcvs = newAV();
1270  
1271 30 meta->has_adjust = true;
1272  
1273 30 av_push(meta->adjustcvs, (SV *)cv);
1274 30 }
1275  
1276 1 void ObjectPad_mop_class_add_APPLY(pTHX_ ClassMeta *meta, CV *cv)
1277 {
1278 1 ▶ if(meta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type != METATYPE_ROLE) 0 1 1
1279 0 croak("Can only add a new APPLY phaser to a role");
1280 1 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 1 1
1281 0 croak("Cannot add a new APPLY phaser to a role that is not yet begun");
1282 1 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 1 1
1283 0 croak("Cannot add an APPLY phaser to an already-sealed role");
1284  
1285 1 ▶ if(!meta->role.applycvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->role.applycvs) 1 0 1
1286 1 meta->role.applycvs = newAV();
1287  
1288 1 av_push(meta->role.applycvs, (SV *)cv);
1289 1 }
1290  
1291 6 void ObjectPad_mop_class_add_required_method(pTHX_ ClassMeta *meta, SV *methodname)
1292 {
1293 6 ▶ if(!meta->abstract)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->abstract) 0 6 6
1294 0 croak("Can only add a required method to a role or abstract class");
1295  
1296 6 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 6 6
1297 0 croak("Cannot add a new required method to a class that is not yet begun");
1298 6 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 6 6
1299 0 croak("Cannot add a new required method to an already-sealed class");
1300  
1301 6 av_push(meta->requiremethods, SvREFCNT_inc(methodname));
1302 6 }
1303  
1304 #define mop_class_implements_role(meta, rolemeta) S_mop_class_implements_role(aTHX_ meta, rolemeta)
1305 53 static bool S_mop_class_implements_role(pTHX_ ClassMeta *meta, ClassMeta *rolemeta)
1306 {
1307 U32 i, n;
1308 53 switch(meta->type) {
1309 44 case METATYPE_CLASS: {
1310 44 RoleEmbedding **embeddings = mop_class_get_all_roles(meta, &n);
1311 49 ▶ for(i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < n; i++) 8 41 49
1312 8 ▶ if(MUST_ROLEEMBEDDING(embeddings[i])->rolemeta == rolemeta)
Branch: 1 branch
BranchTrueFalseTotal
if(MUST_ROLEEMBEDDING(embeddings[i])->rolemeta == rolemeta) 3 5 8
1313 3 return true;
1314  
1315 41 break;
1316 }
1317  
1318 9 case METATYPE_ROLE: {
1319 9 ClassMeta **roles = (ClassMeta **)AvARRAY(meta->role.superroles);
1320 9 U32 n = av_count(meta->role.superroles);
1321 /* TODO: this isn't super-efficient in deep cross-linked heirarchies */
1322 11 ▶ for(i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < n; i++) { 2 9 11
1323 2 ▶ if(MUST_CLASSMETA(roles[i]) == rolemeta)
Branch: 1 branch
BranchTrueFalseTotal
if(MUST_CLASSMETA(roles[i]) == rolemeta) 0 2 2
1324 0 return true;
1325 2 ▶ if(mop_class_implements_role(roles[i], rolemeta))
Branch: 1 branch
BranchTrueFalseTotal
if(mop_class_implements_role(roles[i], rolemeta)) 0 2 2
1326 0 return true;
1327 }
1328 9 break;
1329 }
1330 }
1331  
1332 50 return false;
1333 }
1334  
1335 #define copy_requiremethods_from(dst, src) S_copy_requiremethods_from(aTHX_ dst, src)
1336 45 static void S_copy_requiremethods_from(pTHX_ ClassMeta *dst, ClassMeta *src)
1337 {
1338 45 U32 nmethods = av_count(src->requiremethods);
1339 54 ▶ for(U32 i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(U32 i = 0; i < nmethods; i++) { 9 45 54
1340 9 av_push(dst->requiremethods, SvREFCNT_inc(AvARRAY(src->requiremethods)[i]));
1341 }
1342 45 }
1343  
1344 #define embed_role(class, role) S_embed_role(aTHX_ class, role)
1345 41 static RoleEmbedding *S_embed_role(pTHX_ ClassMeta *classmeta, ClassMeta *rolemeta)
1346 {
1347 U32 i;
1348  
1349 41 ▶ if(classmeta->type != METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->type != METATYPE_CLASS) 0 41 41
1350 0 croak("Can only apply to a class");
1351 41 ▶ if(rolemeta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->type != METATYPE_ROLE) 0 41 41
1352 0 croak("Can only apply a role to a class");
1353  
1354 41 HV *srcstash = rolemeta->stash;
1355 41 HV *dststash = classmeta->stash;
1356  
1357 41 SV *embeddingsv = newSV(sizeof(RoleEmbedding));
1358 assert(SvTYPE(embeddingsv) == SVt_PV && SvLEN(embeddingsv) >= sizeof(RoleEmbedding));
1359  
1360 41 RoleEmbedding *embedding = (RoleEmbedding *)SvPVX(embeddingsv);
1361  
1362 41 *embedding = (RoleEmbedding){
1363 LINNET_INIT(LINNET_VAL_ROLEEMBEDDING)
1364 .embeddingsv = embeddingsv,
1365 .rolemeta = rolemeta,
1366 .classmeta = classmeta,
1367 .offset = -1,
1368 };
1369  
1370 41 av_push(classmeta->cls.embedded_roles, (SV *)embedding);
1371 41 hv_store_ent(rolemeta->role.applied_classes, classmeta->name, (SV *)embedding, 0);
1372  
1373 41 ▶ U32 nbuilds = rolemeta->buildcvs ? av_count(rolemeta->buildcvs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
U32 nbuilds = rolemeta->buildcvs ? av_count(rolemeta->buildcvs) : 0; 1 40 41
1374 42 ▶ for(i = 0; i < nbuilds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nbuilds; i++) { 1 41 42
1375 1 CV *buildcv = (CV *)AvARRAY(rolemeta->buildcvs)[i];
1376  
1377 1 CV *embedded_buildcv = embed_cv(buildcv, embedding);
1378  
1379 1 ▶ if(!classmeta->buildcvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->buildcvs) 1 0 1
1380 1 classmeta->buildcvs = newAV();
1381  
1382 1 av_push(classmeta->buildcvs, (SV *)embedded_buildcv);
1383 }
1384  
1385 41 ▶ U32 nadjusts = rolemeta->adjustcvs ? av_count(rolemeta->adjustcvs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
U32 nadjusts = rolemeta->adjustcvs ? av_count(rolemeta->adjustcvs) : 0; 3 38 41
1386 44 ▶ for(i = 0; i < nadjusts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nadjusts; i++) { 3 41 44
1387 3 CV *cv = (CV *)AvARRAY(rolemeta->adjustcvs)[i];
1388  
1389 3 CV *embedded_cv = embed_cv(cv, embedding);
1390  
1391 3 mop_class_add_ADJUST(classmeta, embedded_cv);
1392 }
1393  
1394 41 ▶ if(rolemeta->has_adjust)
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->has_adjust) 3 38 41
1395 3 classmeta->has_adjust = true;
1396  
1397 41 U32 nmethods = av_count(rolemeta->direct_methods);
1398 58 ▶ for(i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nmethods; i++) { 17 41 58
1399 17 MethodMeta *methodmeta = MUST_METHODMETA(AvARRAY(rolemeta->direct_methods)[i]);
1400 17 SV *mname = methodmeta->name;
1401  
1402 17 HE *he = hv_fetch_ent(srcstash, mname, 0, 0);
1403 17 ▶ SV *heval = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *heval = he ? HeVAL(he) : NULL; 17 0 17
1404 17 CV *cv =
1405 34 ▶ !heval ? NULL :
Branch: 1 branch
BranchTrueFalseTotal
!heval ? NULL : 17 0 17
1406 /* perl since 5.41.9 might store RV to CV directly in the stash */
1407 34 ▶ SvROK(heval) && SvTYPE(SvRV(heval)) == SVt_PVCV ? (CV *)SvRV(heval) :
Branch: 2 branches
BranchTrueFalseTotal
SvROK(heval) && SvTYPE(SvRV(heval)) == SVt_PVCV ? (CV *)SvRV(heval) : 0 17 17
SvROK(heval) && SvTYPE(SvRV(heval)) == SVt_PVCV ? (CV *)SvRV(heval) : 0 0 0
1408 17 ▶ SvTYPE(heval) == SVt_PVGV ? GvCV(heval) :
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(heval) == SVt_PVGV ? GvCV(heval) : 17 0 17
1409 NULL;
1410  
1411 17 ▶ if(!cv)
Branch: 1 branch
BranchTrueFalseTotal
if(!cv) 0 17 17
1412 0 croak("ARGH expected to find CODE called %" SVf " in package %" SVf,
1413 SVfARG(mname), SVfARG(rolemeta->name));
1414  
1415 {
1416 17 MethodMeta *dstmethodmeta = mop_class_add_method(classmeta, mname);
1417 17 dstmethodmeta->role = rolemeta;
1418 17 dstmethodmeta->is_common = methodmeta->is_common;
1419 }
1420  
1421 17 GV **gvp = (GV **)hv_fetch(dststash, SvPVX(mname), SvCUR(mname), GV_ADD);
1422 17 gv_init_sv(*gvp, dststash, mname, 0);
1423 17 GvMULTI_on(*gvp);
1424  
1425 17 ▶ if(GvCV(*gvp))
Branch: 1 branch
BranchTrueFalseTotal
if(GvCV(*gvp)) 0 17 17
1426 0 croak("Method '%" SVf "' clashes with the one provided by role %" SVf,
1427 SVfARG(mname), SVfARG(rolemeta->name));
1428  
1429 17 ▶ if(!methodmeta->is_common) {
Branch: 1 branch
BranchTrueFalseTotal
if(!methodmeta->is_common) { 17 0 17
1430 17 CV *newcv = embed_cv(cv, embedding);
1431 17 GvCV_set(*gvp, newcv);
1432 17 CvGV_set(newcv, *gvp);
1433 }
1434 else
1435 /* :common methods don't get an embedding */
1436 0 GvCV_set(*gvp, (CV *)SvREFCNT_inc((SV *)cv));
1437 }
1438  
1439 41 copy_requiremethods_from(classmeta, rolemeta);
1440  
1441 41 return embedding;
1442 }
1443  
1444 51 void ObjectPad_mop_class_add_role(pTHX_ ClassMeta *dstmeta, ClassMeta *rolemeta)
1445 {
1446 51 ▶ if(!dstmeta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!dstmeta->begun) 0 51 51
1447 0 croak("Cannot add a new role to a class that is not yet begun");
1448 51 ▶ if(dstmeta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(dstmeta->sealed) 0 51 51
1449 0 croak("Cannot add a role to an already-sealed class");
1450 /* Can't currently do this as it breaks t/77mop-create-role.t
1451 if(!rolemeta->sealed)
1452 croak("Cannot add a role that is not yet sealed");
1453 */
1454  
1455 51 ▶ if(mop_class_implements_role(dstmeta, rolemeta))
Branch: 1 branch
BranchTrueFalseTotal
if(mop_class_implements_role(dstmeta, rolemeta)) 3 48 51
1456 3 return;
1457  
1458 48 switch(dstmeta->type) {
1459 41 case METATYPE_CLASS: {
1460 U32 nroles;
1461 41 ▶ if((nroles = av_count(rolemeta->role.superroles)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if((nroles = av_count(rolemeta->role.superroles)) > 0) { 8 33 41
1462 8 ClassMeta **roles = (ClassMeta **)AvARRAY(rolemeta->role.superroles);
1463 U32 i;
1464 17 ▶ for(i = 0; i < nroles; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) 9 8 17
1465 9 mop_class_add_role(dstmeta, MUST_CLASSMETA(roles[i]));
1466 }
1467  
1468 41 RoleEmbedding *embedding = embed_role(dstmeta, rolemeta);
1469 41 av_push(dstmeta->cls.direct_roles, (SV *)embedding);
1470 41 return;
1471 }
1472  
1473 7 case METATYPE_ROLE:
1474 7 av_push(dstmeta->role.superroles, (SV *)rolemeta);
1475 7 return;
1476 }
1477 }
1478  
1479 40 void ObjectPad_mop_class_load_and_add_role(pTHX_ ClassMeta *meta, SV *rolename, SV *rolever)
1480 {
1481 40 HV *rolestash = gv_stashsv(rolename, 0);
1482 40 ▶ if(!rolestash || !hv_fetchs(rolestash, "META", 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!rolestash || !hv_fetchs(rolestash, "META", 0)) { 40 0 40
if(!rolestash || !hv_fetchs(rolestash, "META", 0)) { 0 40 40
1483 /* Try to`require` the module then attempt a second time */
1484 0 load_module(PERL_LOADMOD_NOIMPORT, newSVsv(rolename), NULL, NULL);
1485 0 rolestash = gv_stashsv(rolename, 0);
1486 }
1487  
1488 40 ▶ if(!rolestash)
Branch: 1 branch
BranchTrueFalseTotal
if(!rolestash) 0 40 40
1489 0 croak("Role %" SVf " does not exist", SVfARG(rolename));
1490  
1491 40 ▶ if(rolever && SvOK(rolever))
Branch: 2 branches
BranchTrueFalseTotal
if(rolever && SvOK(rolever)) 1 39 40
if(rolever && SvOK(rolever)) 0 1 1
1492 0 ensure_module_version(rolename, rolever);
1493  
1494 40 GV **metagvp = (GV **)hv_fetchs(rolestash, "META", 0);
1495 40 ClassMeta *rolemeta = NULL;
1496 40 ▶ if(metagvp)
Branch: 1 branch
BranchTrueFalseTotal
if(metagvp) 40 0 40
1497 40 rolemeta = MUST_CLASSMETA(SvUV(SvRV(GvSV(*metagvp))));
1498  
1499 40 ▶ if(!rolemeta || rolemeta->type != METATYPE_ROLE)
Branch: 2 branches
BranchTrueFalseTotal
if(!rolemeta || rolemeta->type != METATYPE_ROLE) 40 0 40
if(!rolemeta || rolemeta->type != METATYPE_ROLE) 0 40 40
1500 0 croak("%" SVf " is not a role", SVfARG(rolename));
1501  
1502 40 mop_class_add_role(meta, rolemeta);
1503 40 }
1504  
1505 #define embed_fieldhook(roleh, offset) S_embed_fieldhook(aTHX_ roleh, offset)
1506 1 static struct FieldHook *S_embed_fieldhook(pTHX_ struct FieldHook *roleh, FIELDOFFSET offset)
1507 {
1508 struct FieldHook *classh;
1509 1 Newx(classh, 1, struct FieldHook);
1510  
1511 1 *classh = (struct FieldHook){
1512 1 .fieldix = roleh->fieldix + offset,
1513 1 .fieldmeta = roleh->fieldmeta,
1514 1 .funcs = roleh->funcs,
1515 1 .attrdata = roleh->attrdata,
1516 };
1517  
1518 1 return classh;
1519 }
1520  
1521 #define mop_class_apply_role(embedding) S_mop_class_apply_role(aTHX_ embedding)
1522 40 static void S_mop_class_apply_role(pTHX_ RoleEmbedding *embedding)
1523 {
1524 40 ClassMeta *classmeta = embedding->classmeta;
1525 40 ClassMeta *rolemeta = embedding->rolemeta;
1526  
1527 40 ▶ if(classmeta->type != METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->type != METATYPE_CLASS) 0 40 40
1528 0 croak("Can only apply to a class");
1529 40 ▶ if(rolemeta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->type != METATYPE_ROLE) 0 40 40
1530 0 croak("Can only apply a role to a class");
1531  
1532 assert(embedding->offset == -1);
1533 40 embedding->offset = classmeta->next_fieldix;
1534  
1535 40 ▶ if(rolemeta->parammap) {
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->parammap) { 5 35 40
1536 5 HV *src = rolemeta->parammap;
1537  
1538 5 ▶ if(!classmeta->parammap)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->parammap) 5 0 5
1539 5 classmeta->parammap = newHV();
1540  
1541 5 HV *dst = classmeta->parammap;
1542  
1543 5 hv_iterinit(src);
1544  
1545 HE *iter;
1546 10 ▶ while((iter = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while((iter = hv_iternext(src))) { 5 5 10
1547 5 STRLEN klen = HeKLEN(iter);
1548 5 void *key = HeKEY(iter);
1549  
1550 5 ▶ if(klen < 0 ? hv_exists_ent(dst, (SV *)key, HeHASH(iter))
Branch: 1 branch
BranchTrueFalseTotal
if(klen < 0 ? hv_exists_ent(dst, (SV *)key, HeHASH(iter)) 0 5 5
1551 5 : hv_exists(dst, (char *)key, klen))
1552 0 ▶ croak("Named parameter '%" SVf "' clashes with the one provided by role %" SVf,
Branch: 2 branches
BranchTrueFalseTotal
croak("Named parameter '%" SVf "' clashes with the one provided by role %" SVf, 0 0 0
croak("Named parameter '%" SVf "' clashes with the one provided by role %" SVf, 0 0 0
1553 SVfARG(HeSVKEY_force(iter)), SVfARG(rolemeta->name));
1554  
1555 5 ParamMeta *roleparammeta = MUST_PARAMMETA(HeVAL(iter));
1556 ParamMeta *classparammeta;
1557 5 Newx(classparammeta, 1, struct ParamMeta);
1558  
1559 5 *classparammeta = (struct ParamMeta){
1560 LINNET_INIT(LINNET_VAL_PARAMMETA)
1561 5 .name = SvREFCNT_inc(roleparammeta->name),
1562 5 .class = roleparammeta->class,
1563 5 .type = roleparammeta->type,
1564 };
1565  
1566 5 switch(roleparammeta->type) {
1567 5 case PARAM_FIELD:
1568 5 classparammeta->field.fieldmeta = roleparammeta->field.fieldmeta;
1569 5 classparammeta->field.fieldix = roleparammeta->field.fieldix + embedding->offset;
1570 5 break;
1571  
1572 0 case PARAM_ADJUST:
1573 0 classparammeta->adjust.padix = roleparammeta->adjust.padix;
1574 0 classparammeta->adjust.defexpr = roleparammeta->adjust.defexpr; /* no refcnt on optrees */
1575 0 break;
1576 }
1577  
1578 if(klen < 0)
1579 hv_store_ent(dst, HeSVKEY(iter), (SV *)classparammeta, HeHASH(iter));
1580 else
1581 5 hv_store(dst, HeKEY(iter), klen, (SV *)classparammeta, HeHASH(iter));
1582 }
1583 }
1584  
1585 40 ▶ if(rolemeta->fieldhooks_makefield) {
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->fieldhooks_makefield) { 0 40 40
1586 0 ▶ if(!classmeta->fieldhooks_makefield)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->fieldhooks_makefield) 0 0 0
1587 0 classmeta->fieldhooks_makefield = newAV();
1588  
1589 U32 i;
1590 0 ▶ for(i = 0; i < av_count(rolemeta->fieldhooks_makefield); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(rolemeta->fieldhooks_makefield); i++) { 0 0 0
1591 0 struct FieldHook *roleh = (struct FieldHook *)AvARRAY(rolemeta->fieldhooks_makefield)[i];
1592 0 av_push(classmeta->fieldhooks_makefield, (SV *)embed_fieldhook(roleh, embedding->offset));
1593 }
1594 }
1595  
1596 40 ▶ if(rolemeta->fieldhooks_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->fieldhooks_construct) { 1 39 40
1597 1 ▶ if(!classmeta->fieldhooks_construct)
Branch: 1 branch
BranchTrueFalseTotal
if(!classmeta->fieldhooks_construct) 1 0 1
1598 1 classmeta->fieldhooks_construct = newAV();
1599  
1600 U32 i;
1601 2 ▶ for(i = 0; i < av_count(rolemeta->fieldhooks_construct); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(rolemeta->fieldhooks_construct); i++) { 1 1 2
1602 1 struct FieldHook *roleh = (struct FieldHook *)AvARRAY(rolemeta->fieldhooks_construct)[i];
1603 1 av_push(classmeta->fieldhooks_construct, (SV *)embed_fieldhook(roleh, embedding->offset));
1604 }
1605 }
1606  
1607 40 classmeta->next_fieldix += rolemeta->next_fieldix;
1608  
1609 40 ▶ if(rolemeta->role.applycvs) {
Branch: 1 branch
BranchTrueFalseTotal
if(rolemeta->role.applycvs) { 2 38 40
1610 /* TODO: if APPLY phasers exist they should *replace* the built-in behaviour */
1611 2 dSP;
1612  
1613 2 AV *applycvs = rolemeta->role.applycvs;
1614  
1615 2 SV *classmop = sv_newmortal();
1616 2 sv_setref_uv(classmop, "Object::Pad::MOP::Class", PTR2UV(classmeta));
1617  
1618 int i;
1619 4 ▶ for(i = 0; i < av_count(applycvs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(applycvs); i++) { 2 2 4
1620 2 CV *applycv = (CV *)AvARRAY(applycvs)[i];
1621  
1622 2 ENTER;
1623 2 SAVETMPS;
1624 2 SPAGAIN;
1625  
1626 2 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2 2
1627 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1628 2 PUSHs(sv_mortalcopy(classmop));
1629 2 PUTBACK;
1630  
1631 assert(applycv);
1632 2 call_sv((SV *)applycv, G_VOID);
1633  
1634 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
1635 2 LEAVE;
1636 }
1637 }
1638 40 }
1639  
1640 171 static void S_apply_roles(pTHX_ ClassMeta *dstmeta, ClassMeta *srcmeta)
1641 {
1642 U32 nroles;
1643 171 RoleEmbedding **arr = mop_class_get_direct_roles(srcmeta, &nroles);
1644 U32 i;
1645 211 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 40 171 211
1646 40 mop_class_apply_role(MUST_ROLEEMBEDDING(arr[i]));
1647 }
1648 171 }
1649  
1650 51 void ObjectPad__check_colliding_param(pTHX_ ClassMeta *classmeta, SV *paramname)
1651 {
1652 51 HV *parammap = classmeta->parammap;
1653 assert(parammap);
1654  
1655 51 HE *he = hv_fetch_ent(parammap, paramname, 0, 0);
1656 51 ▶ if(!he)
Branch: 1 branch
BranchTrueFalseTotal
if(!he) 48 3 51
1657 48 return;
1658  
1659 3 ParamMeta *colliding_parammeta = MUST_PARAMMETA(HeVAL(he));
1660 3 ClassMeta *origclassmeta = colliding_parammeta->class;
1661  
1662 3 ▶ if(origclassmeta != classmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(origclassmeta != classmeta) 0 3 3
1663 0 croak("Already have a named constructor parameter called '%" SVf "' inherited from %" SVf,
1664 SVfARG(paramname), SVfARG(origclassmeta->name));
1665 else
1666 3 croak("Already have a named constructor parameter called '%" SVf "'", SVfARG(paramname));
1667 }
1668  
1669 176 static OP *pp_alias_params(pTHX)
1670 {
1671 176 dSP;
1672 176 PADOFFSET padix = PADIX_PARAMS;
1673  
1674 176 SV *params = POPs;
1675  
1676 176 ▶ if(SvTYPE(params) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(params) != SVt_PVHV) 71 105 176
1677 71 RETURN;
1678  
1679 105 SAVESPTR(PAD_SVl(padix));
1680 105 PAD_SVl(padix) = SvREFCNT_inc(params);
1681 105 save_freesv(params);
1682  
1683 105 RETURN;
1684 }
1685  
1686 199 static void S_generate_initfields_method(pTHX_ ClassMeta *meta)
1687 {
1688 int i;
1689  
1690 199 ENTER;
1691  
1692 199 need_PLparser();
1693  
1694 199 I32 floor_ix = PL_savestack_ix;
1695 {
1696 199 SAVEI32(PL_subline);
1697 199 save_item(PL_subname);
1698  
1699 199 resume_compcv(&meta->initfields_compcv);
1700 }
1701  
1702 199 SAVEFREESV(PL_compcv);
1703  
1704 199 I32 save_ix = block_start(TRUE);
1705  
1706 #ifdef DEBUG_OVERRIDE_PLCURCOP
1707 SAVESPTR(PL_curcop);
1708 PL_curcop = meta->tmpcop;
1709 CopLINE_set(PL_curcop, __LINE__);
1710 #endif
1711  
1712 199 OP *ops = NULL;
1713  
1714 199 ops = op_append_list(OP_LINESEQ, ops,
1715 newSTATEOP(0, NULL, NULL));
1716  
1717 /* A more optimised implementation of this method would be able to generate
1718 * a @self lexical and OP_REFASSIGN it, but that would only work on newer
1719 * perls. For now we'll take the small performance hit of RV2AV every time
1720 */
1721  
1722 199 ops = op_append_list(OP_LINESEQ, ops,
1723 newUNOP_CUSTOM(&pp_alias_params, 0,
1724 newOP(OP_SHIFT, OPf_SPECIAL)));
1725  
1726 /* TODO: Icky horrible implementation; if our fieldoffset > 0 then
1727 * we must be a subclass
1728 */
1729 199 ▶ if(meta->start_fieldix) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->start_fieldix) { 23 176 199
1730 23 struct ClassMeta *supermeta = meta->cls.supermeta;
1731  
1732 assert(supermeta->sealed);
1733 assert(supermeta->initfields);
1734  
1735 DEBUG_SET_CURCOP_LINE(__LINE__);
1736  
1737 23 ops = op_append_list(OP_LINESEQ, ops,
1738 newSTATEOP(0, NULL, NULL));
1739  
1740 23 ops = op_append_list(OP_LINESEQ, ops,
1741 /* Build an OP_ENTERSUB for supermeta's initfields */
1742 newLISTOPn(OP_ENTERSUB, OPf_WANT_VOID|OPf_STACKED,
1743 newPADxVOP(OP_PADSV, 0, PADIX_SELF),
1744 newPADxVOP(OP_PADHV, OPf_REF, PADIX_PARAMS),
1745 newSVOP(OP_CONST, 0, (SV *)supermeta->initfields),
1746 NULL));
1747 }
1748  
1749 199 ▶ if(meta->initfields_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->initfields_lines) { 91 108 199
1750 91 ops = op_append_list(OP_LINESEQ, ops,
1751 meta->initfields_lines);
1752 }
1753  
1754 199 ▶ if(meta->type == METATYPE_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS) { 168 31 199
1755 U32 nroles;
1756 168 RoleEmbedding **embeddings = mop_class_get_direct_roles(meta, &nroles);
1757  
1758 206 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 38 168 206
1759 38 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(embeddings[i]);
1760 38 ClassMeta *rolemeta = embedding->rolemeta;
1761  
1762 38 ▶ if(!rolemeta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(!rolemeta->sealed) 1 37 38
1763 1 mop_class_seal(rolemeta);
1764  
1765 assert(rolemeta->sealed);
1766 assert(rolemeta->initfields);
1767  
1768 DEBUG_SET_CURCOP_LINE(__LINE__);
1769  
1770 38 ops = op_append_list(OP_LINESEQ, ops,
1771 newSTATEOP(0, NULL, NULL));
1772  
1773 38 ops = op_append_list(OP_LINESEQ, ops,
1774 newLISTOPn(OP_ENTERSUB, OPf_WANT_VOID|OPf_STACKED,
1775 newPADxVOP(OP_PADSV, 0, PADIX_SELF),
1776 newPADxVOP(OP_PADHV, OPf_REF, PADIX_PARAMS),
1777 newSVOP(OP_CONST, 0, (SV *)embed_cv(rolemeta->initfields, embedding)),
1778 NULL));
1779 }
1780 }
1781  
1782 /* TODO: This will create a method start op that appears to capture every
1783 * field except the final one. There's not a lot we can do about this
1784 * without duplicating a lot of the `methodscope` structure for initfields,
1785 * except more complex due to the multiple suspend/resume nature of parsing
1786 * it.
1787 */
1788 199 ops = prepend_methstart_ops(meta, NULL, ops, false);
1789  
1790 199 SvREFCNT_inc(PL_compcv);
1791 199 ops = block_end(save_ix, ops);
1792  
1793 /* newATTRSUB will capture PL_curstash */
1794 199 SAVESPTR(PL_curstash);
1795 199 PL_curstash = meta->stash;
1796  
1797 199 meta->initfields = newATTRSUB(floor_ix, NULL, NULL, NULL, ops);
1798  
1799 assert(meta->initfields);
1800 assert(CvOUTSIDE(meta->initfields));
1801  
1802 199 LEAVE;
1803 199 }
1804  
1805 205 void ObjectPad_mop_class_seal(pTHX_ ClassMeta *meta)
1806 {
1807 205 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 14 191 205
1808 14 mop_class_begin(meta);
1809 205 ▶ if(meta->sealed) /* idempotent */
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) /* idempotent */ 3 202 205
1810 3 return;
1811  
1812 202 ▶ MOP_CLASS_RUN_HOOKS_NOARGS(meta, pre_seal);
Branch: 3 branches
BranchTrueFalseTotal
MOP_CLASS_RUN_HOOKS_NOARGS(meta, pre_seal); 0 0 0
MOP_CLASS_RUN_HOOKS_NOARGS(meta, pre_seal); 0 202 202
MOP_CLASS_RUN_HOOKS_NOARGS(meta, pre_seal); 0 0 0
1813  
1814 202 ▶ if(meta->type == METATYPE_CLASS &&
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS && 171 31 202
1815 171 ▶ meta->cls.supermeta && !meta->cls.supermeta->sealed) {
Branch: 2 branches
BranchTrueFalseTotal
meta->cls.supermeta && !meta->cls.supermeta->sealed) { 32 139 171
meta->cls.supermeta && !meta->cls.supermeta->sealed) { 0 32 32
1816 /* Must defer sealing until superclass is sealed first
1817 * (RT133190)
1818 */
1819 0 ClassMeta *supermeta = meta->cls.supermeta;
1820 0 ▶ if(!supermeta->pending_submeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!supermeta->pending_submeta) 0 0 0
1821 0 supermeta->pending_submeta = newAV();
1822 0 av_push(supermeta->pending_submeta, (SV *)meta);
1823 0 return;
1824 }
1825  
1826 202 ▶ if(meta->type == METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS) 171 31 202
1827 171 S_apply_roles(aTHX_ meta, meta);
1828  
1829 202 ▶ if(!meta->abstract) {
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->abstract) { 167 35 202
1830 167 U32 nmethods = av_count(meta->requiremethods);
1831 U32 i;
1832 172 ▶ for(i = 0; i < nmethods; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nmethods; i++) { 8 164 172
1833 8 SV *mname = AvARRAY(meta->requiremethods)[i];
1834  
1835 8 GV *gv = gv_fetchmeth_sv(meta->stash, mname, 0, 0);
1836 8 ▶ if(gv && GvCV(gv))
Branch: 2 branches
BranchTrueFalseTotal
if(gv && GvCV(gv)) 5 3 8
if(gv && GvCV(gv)) 5 0 5
1837 5 continue;
1838  
1839 3 croak("Class %" SVf " does not provide a required method named '%" SVf "'",
1840 SVfARG(meta->name), SVfARG(mname));
1841 }
1842 }
1843  
1844 199 ▶ if(meta->type == METATYPE_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS) { 168 31 199
1845 168 GV *gv = gv_fetchmeth_pvs(meta->stash, "BUILDARGS", -1, 0);
1846 assert(gv); assert(SvTYPE(gv) == SVt_PVGV);
1847  
1848 168 ▶ if(GvSTASH(gv) != gv_stashpvs("Object::Pad::UNIVERSAL", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(GvSTASH(gv) != gv_stashpvs("Object::Pad::UNIVERSAL", 0)) 5 163 168
1849 5 meta->has_buildargs = true;
1850 }
1851  
1852 199 ▶ if(meta->strict_params && meta->buildcvs)
Branch: 2 branches
BranchTrueFalseTotal
if(meta->strict_params && meta->buildcvs) 5 194 199
if(meta->strict_params && meta->buildcvs) 0 5 5
1853 0 croak("Class %" SVf " cannot be :strict(params) because it has BUILD phasers",
1854 SVfARG(meta->name));
1855  
1856 {
1857 199 AV *fields = meta->fields;
1858 199 U32 nfields = av_count(fields);
1859  
1860 U32 i;
1861 334 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 135 199 334
1862 135 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
1863  
1864 U32 hooki;
1865 214 ▶ for(hooki = 0; fieldmeta->hooks && hooki < av_count(fieldmeta->hooks); hooki++) {
Branch: 2 branches
BranchTrueFalseTotal
for(hooki = 0; fieldmeta->hooks && hooki < av_count(fieldmeta->hooks); hooki++) { 135 79 214
for(hooki = 0; fieldmeta->hooks && hooki < av_count(fieldmeta->hooks); hooki++) { 79 56 135
1866 79 struct FieldHook *h = (struct FieldHook *)AvARRAY(fieldmeta->hooks)[hooki];
1867  
1868 79 ▶ if(*h->funcs->post_makefield) {
Branch: 1 branch
BranchTrueFalseTotal
if(*h->funcs->post_makefield) { 0 79 79
1869 0 ▶ if(!meta->fieldhooks_makefield)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->fieldhooks_makefield) 0 0 0
1870 0 meta->fieldhooks_makefield = newAV();
1871  
1872 struct FieldHook *fasth;
1873 0 Newx(fasth, 1, struct FieldHook);
1874  
1875 0 *fasth = (struct FieldHook){
1876 0 .fieldix = fieldmeta->fieldix,
1877 .fieldmeta = fieldmeta,
1878 0 .funcs = h->funcs,
1879 0 .funcdata = h->funcdata,
1880 0 .attrdata = h->attrdata,
1881 };
1882  
1883 0 av_push(meta->fieldhooks_makefield, (SV *)fasth);
1884 }
1885  
1886 79 ▶ if(*h->funcs->post_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if(*h->funcs->post_construct) { 3 76 79
1887 3 ▶ if(!meta->fieldhooks_construct)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->fieldhooks_construct) 3 0 3
1888 3 meta->fieldhooks_construct = newAV();
1889  
1890 struct FieldHook *fasth;
1891 3 Newx(fasth, 1, struct FieldHook);
1892  
1893 3 *fasth = (struct FieldHook){
1894 3 .fieldix = fieldmeta->fieldix,
1895 .fieldmeta = fieldmeta,
1896 3 .funcs = h->funcs,
1897 3 .funcdata = h->funcdata,
1898 3 .attrdata = h->attrdata,
1899 };
1900  
1901 3 av_push(meta->fieldhooks_construct, (SV *)fasth);
1902 }
1903 }
1904 }
1905 }
1906  
1907 199 S_generate_initfields_method(aTHX_ meta);
1908  
1909 199 ▶ if(meta->adjust_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->adjust_lines) { 2 197 199
1910 2 ENTER;
1911  
1912 2 need_PLparser();
1913  
1914 2 I32 floor_ix = PL_savestack_ix;
1915 {
1916 2 SAVEI32(PL_subline);
1917 2 save_item(PL_subname);
1918  
1919 2 resume_compcv(&meta->adjust_compcv);
1920 }
1921  
1922 2 SvREFCNT_inc(PL_compcv);
1923  
1924 2 OP *body = finish_adjust_params(meta, meta->adjust_params, meta->adjust_lines);
1925  
1926 2 meta->methodscope = meta->adjust_methodscope;
1927  
1928 2 body = finish_method_parse(meta, FALSE, body);
1929  
1930 2 CV *adjustcv = newATTRSUB(floor_ix, NULL, NULL, NULL, body);
1931  
1932 2 mop_class_add_ADJUST(meta, adjustcv);
1933  
1934 2 LEAVE;
1935 }
1936  
1937 199 meta->sealed = true;
1938  
1939 199 ▶ MOP_CLASS_RUN_HOOKS_NOARGS(meta, post_seal);
Branch: 3 branches
BranchTrueFalseTotal
MOP_CLASS_RUN_HOOKS_NOARGS(meta, post_seal); 0 0 0
MOP_CLASS_RUN_HOOKS_NOARGS(meta, post_seal); 0 199 199
MOP_CLASS_RUN_HOOKS_NOARGS(meta, post_seal); 0 0 0
1940  
1941 199 ▶ if(meta->pending_submeta) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->pending_submeta) { 0 199 199
1942 int i;
1943 0 SV **arr = AvARRAY(meta->pending_submeta);
1944 0 ▶ for(i = 0; i < av_count(meta->pending_submeta); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(meta->pending_submeta); i++) { 0 0 0
1945 0 ClassMeta *submeta = MUST_CLASSMETA(arr[i]);
1946 0 arr[i] = &PL_sv_undef;
1947  
1948 0 mop_class_seal(submeta);
1949 }
1950  
1951 0 SvREFCNT_dec(meta->pending_submeta);
1952 0 meta->pending_submeta = NULL;
1953 }
1954 }
1955  
1956 XS_INTERNAL(injected_constructor);
1957 133 XS_INTERNAL(injected_constructor)
1958 {
1959 133 dXSARGS;
1960 133 const ClassMeta *meta = MUST_CLASSMETA(XSANY.any_ptr);
1961 133 SV *class = ST(0);
1962 133 SV *self = NULL;
1963  
1964 assert(meta->type == METATYPE_CLASS);
1965 133 ▶ if(meta->abstract)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->abstract) 1 132 133
1966 1 croak("Cannot directly construct an instance of abstract class '%" SVf "'",
1967 SVfARG(meta->name));
1968 132 ▶ if(!meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->sealed) 0 132 132
1969 0 croak("Cannot yet invoke '%" SVf "' constructor before the class is complete", SVfARG(class));
1970  
1971 #ifdef DEBUG_OVERRIDE_PLCURCOP
1972 COP *prevcop = PL_curcop;
1973 PL_curcop = meta->tmpcop;
1974 CopLINE_set(PL_curcop, __LINE__);
1975 #endif
1976  
1977 /* An AV storing the @_ args to pass to foreign constructor and all the
1978 * build phasers
1979 * This does not include $self
1980 */
1981 132 AV *args = newAV();
1982 132 I32 nargs = 0;
1983 132 SAVEFREESV(args);
1984  
1985 132 ▶ if(meta->has_buildargs) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->has_buildargs) { 5 127 132
1986 /* @args = $class->BUILDARGS(@_) */
1987 5 ENTER;
1988 5 SAVETMPS;
1989  
1990 #ifdef DEBUG_OVERRIDE_PLCURCOP
1991 SAVEVPTR(PL_curcop);
1992 PL_curcop = prevcop;
1993 #endif
1994  
1995 /* Splice in an extra copy of `class` so we get one there for the foreign
1996 * constructor */
1997 5 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 5 5
1998  
1999 5 SV **argstart = SP - items + 2;
2000 5 ▶ PUSHMARK(argstart - 1);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(argstart - 1); 0 5 5
2001  
2002 SV **svp;
2003 12 ▶ for(svp = SP; svp >= argstart; svp--)
Branch: 1 branch
BranchTrueFalseTotal
for(svp = SP; svp >= argstart; svp--) 7 5 12
2004 7 *(svp+1) = *svp;
2005 5 *argstart = class;
2006 5 SP++;
2007 5 PUTBACK;
2008  
2009 5 nargs = call_method("BUILDARGS", G_ARRAY);
2010  
2011 5 SPAGAIN;
2012  
2013 12 ▶ for(svp = SP - nargs + 1; svp <= SP; svp++)
Branch: 1 branch
BranchTrueFalseTotal
for(svp = SP - nargs + 1; svp <= SP; svp++) 7 5 12
2014 7 av_push_simple(args, SvREFCNT_inc(*svp));
2015  
2016 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
2017 5 LEAVE;
2018 }
2019 else {
2020 127 nargs = items - 1;
2021  
2022 SV **svp;
2023 277 ▶ for(svp = SP - nargs + 1; svp <= SP; svp++)
Branch: 1 branch
BranchTrueFalseTotal
for(svp = SP - nargs + 1; svp <= SP; svp++) 150 127 277
2024 150 av_push_simple(args, SvREFCNT_inc(*svp));
2025 }
2026  
2027 132 bool need_makefields = true;
2028  
2029 132 ▶ if(!meta->cls.foreign_new) {
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->cls.foreign_new) { 122 10 132
2030 122 HV *stash = gv_stashsv(class, 0);
2031 122 ▶ if(!stash)
Branch: 1 branch
BranchTrueFalseTotal
if(!stash) 0 122 122
2032 0 croak("Unable to find stash for class %" SVf, class);
2033  
2034 122 switch(meta->repr) {
2035 115 case REPR_NATIVE:
2036 case REPR_AUTOSELECT:
2037 DEBUG_SET_CURCOP_LINE(__LINE__);
2038 115 self = sv_2mortal(newRV_noinc((SV *)newAV()));
2039 115 sv_bless(self, stash);
2040 115 break;
2041  
2042 2 case REPR_HASH:
2043 case REPR_KEYS:
2044 DEBUG_SET_CURCOP_LINE(__LINE__);
2045 2 self = sv_2mortal(newRV_noinc((SV *)newHV()));
2046 2 sv_bless(self, stash);
2047 2 break;
2048  
2049 5 case REPR_PVOBJ:
2050 #ifdef HAVE_SVt_PVOBJ
2051 {
2052 DEBUG_SET_CURCOP_LINE(__LINE__);
2053 /* TODO: Perl needs to export newSVobject() */
2054 5 U32 fieldcount = meta->next_fieldix;
2055 5 SV *obj = newSV_type(SVt_PVOBJ);
2056 5 Newx(ObjectFIELDS(obj), fieldcount, SV *);
2057 5 ObjectMAXFIELD(obj) = fieldcount - 1;
2058 5 Zero(ObjectFIELDS(obj), fieldcount, SV *);
2059 5 self = sv_2mortal(newRV_noinc(obj));
2060 5 sv_bless(self, stash);
2061 }
2062 #else
2063 croak("ARGH cannot SVt_PVOBJ on this version of perl");
2064 #endif
2065 5 break;
2066  
2067 0 case REPR_MAGIC:
2068 0 croak("ARGH cannot use :repr(magic) without a foreign superconstructor");
2069 break;
2070 }
2071 }
2072 else {
2073 DEBUG_SET_CURCOP_LINE(__LINE__);
2074  
2075 {
2076 10 ENTER;
2077 10 SAVETMPS;
2078  
2079 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
2080 10 ▶ EXTEND(SP, nargs);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs); 10 0 10
EXTEND(SP, nargs); 0 10 10
2081  
2082 10 SV **argstart = SP - nargs;
2083 10 SV **argtop = SP;
2084 SV **svp;
2085  
2086 10 mPUSHs(newSVsv(class));
2087  
2088 /* Push a copy of the args in case the (foreign) constructor mutates
2089 * them. We still need them for BUILDALL */
2090 15 ▶ for(svp = argstart + 1; svp <= argtop; svp++)
Branch: 1 branch
BranchTrueFalseTotal
for(svp = argstart + 1; svp <= argtop; svp++) 5 10 15
2091 5 PUSHs(*svp);
2092 10 PUTBACK;
2093  
2094 assert(meta->cls.foreign_new);
2095 10 call_sv((SV *)meta->cls.foreign_new, G_SCALAR);
2096 10 SPAGAIN;
2097  
2098 10 self = SvREFCNT_inc(POPs);
2099  
2100 10 PUTBACK;
2101 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
2102 10 LEAVE;
2103 }
2104  
2105 10 ▶ if(!SvROK(self) || !SvOBJECT(SvRV(self))) {
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(self) || !SvOBJECT(SvRV(self))) { 10 0 10
if(!SvROK(self) || !SvOBJECT(SvRV(self))) { 0 10 10
2106 #ifdef DEBUG_OVERRIDE_PLCURCOP
2107 PL_curcop = prevcop;
2108 #endif
2109 0 croak("Expected %" SVf "->SUPER::new to return a blessed reference", class);
2110 }
2111 10 SV *rv = SvRV(self);
2112  
2113 /* It's possible a foreign superclass constructor invoked a `method` and
2114 * thus initfields has already been called. Check here and set
2115 * need_makefields false if so.
2116 */
2117  
2118 10 switch(meta->repr) {
2119 0 case REPR_NATIVE:
2120 0 croak("ARGH shouldn't ever have REPR_NATIVE with foreign_new");
2121 0 case REPR_PVOBJ:
2122 0 croak("ARGH shouldn't ever have REPR_PVOBJ with foreign_new");
2123  
2124 case REPR_HASH:
2125 8 case_REPR_HASH:
2126 8 case REPR_KEYS:
2127 8 ▶ if(SvTYPE(rv) != SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVHV) { 0 8 8
2128 #ifdef DEBUG_OVERRIDE_PLCURCOP
2129 PL_curcop = prevcop;
2130 #endif
2131 0 croak("Expected %" SVf "->SUPER::new to return a blessed HASH reference", class);
2132 }
2133  
2134 8 need_makefields = !hv_exists(MUTABLE_HV(rv), "Object::Pad/slots", 17);
2135 8 break;
2136  
2137 case REPR_MAGIC:
2138 2 case_REPR_MAGIC:
2139 /* Anything goes */
2140  
2141 2 need_makefields = !mg_findext(rv, PERL_MAGIC_ext, &vtbl_backingav);
2142 2 break;
2143  
2144 10 case REPR_AUTOSELECT:
2145 10 ▶ if(SvTYPE(rv) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) == SVt_PVHV) 8 2 10
2146 8 goto case_REPR_HASH;
2147 2 goto case_REPR_MAGIC;
2148 }
2149  
2150 10 sv_2mortal(self);
2151 }
2152  
2153 SV *fieldstore;
2154  
2155 132 ▶ if(need_makefields) {
Branch: 1 branch
BranchTrueFalseTotal
if(need_makefields) { 130 2 132
2156 130 fieldstore = get_obj_fieldstore(self, meta->repr, TRUE);
2157 130 make_instance_fields(meta, fieldstore, 0);
2158 }
2159 else {
2160 2 fieldstore = get_obj_fieldstore(self, meta->repr, FALSE);
2161 }
2162  
2163 132 ▶ SV **fieldsvs = fieldstore_fields(fieldstore);
Branch: 1 branch
BranchTrueFalseTotal
SV **fieldsvs = fieldstore_fields(fieldstore); 5 127 132
2164  
2165 132 ▶ if(meta->fieldhooks_makefield || meta->fieldhooks_construct) {
Branch: 2 branches
BranchTrueFalseTotal
if(meta->fieldhooks_makefield || meta->fieldhooks_construct) { 132 0 132
if(meta->fieldhooks_makefield || meta->fieldhooks_construct) { 4 128 132
2166 /* We need to set up a fake pad so these hooks can still get PADIX_SELF / PADIX_FIELDS */
2167  
2168 /* This MVP is just sufficient enough to let PAD_SVl(PADIX_SELF) work */
2169 4 SAVEVPTR(PL_curpad);
2170 4 Newx(PL_curpad, 3, SV *);
2171 4 SAVEFREEPV(PL_curpad);
2172  
2173 4 PAD_SVl(PADIX_SELF) = self;
2174 4 PAD_SVl(PADIX_FIELDS) = fieldstore;
2175 }
2176  
2177 132 ▶ if(meta->fieldhooks_makefield) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->fieldhooks_makefield) { 0 132 132
2178 DEBUG_SET_CURCOP_LINE(__LINE__);
2179  
2180 0 AV *fieldhooks = meta->fieldhooks_makefield;
2181  
2182 U32 i;
2183 0 ▶ for(i = 0; i < av_count(fieldhooks); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(fieldhooks); i++) { 0 0 0
2184 0 struct FieldHook *h = (struct FieldHook *)AvARRAY(fieldhooks)[i];
2185 0 FIELDOFFSET fieldix = h->fieldix;
2186  
2187 0 (*h->funcs->post_makefield)(aTHX_ h->fieldmeta, h->attrdata, h->funcdata, fieldsvs[fieldix]);
2188 }
2189 }
2190  
2191 132 HV *paramhv = NULL;
2192 132 ▶ if(meta->parammap || meta->has_adjust || meta->strict_params) {
Branch: 3 branches
BranchTrueFalseTotal
if(meta->parammap || meta->has_adjust || meta->strict_params) { 85 47 132
if(meta->parammap || meta->has_adjust || meta->strict_params) { 72 13 85
if(meta->parammap || meta->has_adjust || meta->strict_params) { 1 71 72
2193 61 paramhv = newHV();
2194 61 SAVEFREESV((SV *)paramhv);
2195  
2196 61 ▶ if(nargs % 2)
Branch: 1 branch
BranchTrueFalseTotal
if(nargs % 2) 0 61 61
2197 0 warn("Odd-length list passed to %" SVf " constructor", class);
2198  
2199 /* TODO: I'm sure there's an newHV_from_AV() around somewhere */
2200 61 SV **argsv = AvARRAY(args);
2201  
2202 IV idx;
2203 127 ▶ for(idx = 0; idx < nargs; idx += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for(idx = 0; idx < nargs; idx += 2) { 66 61 127
2204 66 SV *name = argsv[idx];
2205 66 ▶ SV *value = idx < nargs-1 ? argsv[idx+1] : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *value = idx < nargs-1 ? argsv[idx+1] : &PL_sv_undef; 66 0 66
2206  
2207 66 hv_store_ent(paramhv, name, SvREFCNT_inc(value), 0);
2208 }
2209 }
2210  
2211 {
2212 /* Run initfields */
2213 132 ENTER;
2214 #ifdef DEBUG_OVERRIDE_PLCURCOP
2215 SAVEVPTR(PL_curcop);
2216 PL_curcop = prevcop;
2217 #endif
2218  
2219 132 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 132 132
2220 132 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 132 132
2221 132 PUSHs(self);
2222 132 ▶ if(paramhv)
Branch: 1 branch
BranchTrueFalseTotal
if(paramhv) 61 71 132
2223 61 PUSHs((SV *)paramhv);
2224 else
2225 71 PUSHs(&PL_sv_undef);
2226 132 PUTBACK;
2227  
2228 assert(meta->initfields);
2229 132 call_sv((SV *)meta->initfields, G_VOID);
2230  
2231 131 LEAVE;
2232 }
2233  
2234 131 ▶ if(meta->buildcvs) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->buildcvs) { 18 113 131
2235 DEBUG_SET_CURCOP_LINE(__LINE__);
2236  
2237 18 AV *buildcvs = meta->buildcvs;
2238 18 SV **argsvs = AvARRAY(args);
2239 int i;
2240 40 ▶ for(i = 0; i < av_count(buildcvs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(buildcvs); i++) { 22 18 40
2241 22 CV *buildcv = (CV *)AvARRAY(buildcvs)[i];
2242  
2243 22 ENTER;
2244 22 SAVETMPS;
2245 22 SPAGAIN;
2246  
2247 22 ▶ EXTEND(SP, nargs);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs); 22 0 22
EXTEND(SP, nargs); 0 22 22
2248  
2249 22 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 22 22
2250  
2251 22 PUSHs(self);
2252  
2253 int argi;
2254 46 ▶ for(argi = 0; argi < nargs; argi++)
Branch: 1 branch
BranchTrueFalseTotal
for(argi = 0; argi < nargs; argi++) 24 22 46
2255 24 PUSHs(argsvs[argi]);
2256 22 PUTBACK;
2257  
2258 assert(buildcv);
2259 22 call_sv((SV *)buildcv, G_VOID);
2260  
2261 22 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 20 22
2262 22 LEAVE;
2263 }
2264 }
2265  
2266 131 ▶ if(meta->adjustcvs) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->adjustcvs) { 27 104 131
2267 DEBUG_SET_CURCOP_LINE(__LINE__);
2268  
2269 27 AV *adjustcvs = meta->adjustcvs;
2270 U32 i;
2271 58 ▶ for(i = 0; i < av_count(adjustcvs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(adjustcvs); i++) { 32 26 58
2272 32 CV *cv = (CV *)AvARRAY(adjustcvs)[i];
2273  
2274 32 ENTER;
2275 32 SAVETMPS;
2276 32 SPAGAIN;
2277  
2278 32 ▶ EXTEND(SP, 1 + !!paramhv);
Branch: 3 branches
BranchTrueFalseTotal
EXTEND(SP, 1 + !!paramhv); 32 0 32
EXTEND(SP, 1 + !!paramhv); 0 32 32
EXTEND(SP, 1 + !!paramhv); 0 0 0
2279  
2280 32 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 32 32
2281 32 PUSHs(self);
2282 32 ▶ if(paramhv)
Branch: 1 branch
BranchTrueFalseTotal
if(paramhv) 32 0 32
2283 32 mPUSHs(newRV_inc((SV *)paramhv));
2284 32 PUTBACK;
2285  
2286 assert(cv);
2287 32 call_sv((SV *)cv, G_VOID);
2288  
2289 31 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 31 0 31
2290 31 LEAVE;
2291 }
2292 }
2293  
2294 130 ▶ if(meta->strict_params && hv_iterinit(paramhv) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(meta->strict_params && hv_iterinit(paramhv) > 0) { 7 123 130
if(meta->strict_params && hv_iterinit(paramhv) > 0) { 4 3 7
2295 4 HE *he = hv_iternext(paramhv);
2296  
2297 /* Concat all the param names, in no particular order
2298 * TODO: consider sorting them but that's quite expensive and tricky in XS */
2299  
2300 4 SV *params = newSVpvn("", 0);
2301 4 SAVEFREESV(params);
2302 4 ▶ sv_catpvf(params, "'%" SVf "'", SVfARG(HeSVKEY_force(he)));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(params, "'%" SVf "'", SVfARG(HeSVKEY_force(he))); 4 0 4
sv_catpvf(params, "'%" SVf "'", SVfARG(HeSVKEY_force(he))); 0 4 4
2303  
2304 4 ▶ while((he = hv_iternext(paramhv)))
Branch: 1 branch
BranchTrueFalseTotal
while((he = hv_iternext(paramhv))) 0 4 4
2305 0 ▶ sv_catpvf(params, ", '%" SVf "'", SVfARG(HeSVKEY_force(he)));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(params, ", '%" SVf "'", SVfARG(HeSVKEY_force(he))); 0 0 0
sv_catpvf(params, ", '%" SVf "'", SVfARG(HeSVKEY_force(he))); 0 0 0
2306  
2307 #ifdef DEBUG_OVERRIDE_PLCURCOP
2308 PL_curcop = prevcop;
2309 #endif
2310 4 croak("Unrecognised parameters for %" SVf " constructor: %" SVf,
2311 SVfARG(meta->name), SVfARG(params));
2312 }
2313  
2314 126 ▶ if(meta->fieldhooks_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->fieldhooks_construct) { 4 122 126
2315 DEBUG_SET_CURCOP_LINE(__LINE__);
2316  
2317 4 AV *fieldhooks = meta->fieldhooks_construct;
2318  
2319 U32 i;
2320 8 ▶ for(i = 0; i < av_count(fieldhooks); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < av_count(fieldhooks); i++) { 4 4 8
2321 4 struct FieldHook *h = (struct FieldHook *)AvARRAY(fieldhooks)[i];
2322 4 FIELDOFFSET fieldix = h->fieldix;
2323  
2324 4 (*h->funcs->post_construct)(aTHX_ h->fieldmeta, h->attrdata, h->funcdata, fieldsvs[fieldix]);
2325 }
2326 }
2327  
2328 #ifdef DEBUG_OVERRIDE_PLCURCOP
2329 PL_curcop = prevcop;
2330 #endif
2331 126 ST(0) = self;
2332 126 XSRETURN(1);
2333 }
2334  
2335 XS_INTERNAL(injected_constructor_role);
2336 1 XS_INTERNAL(injected_constructor_role)
2337 {
2338 1 const ClassMeta *meta = MUST_CLASSMETA(XSANY.any_ptr);
2339 1 croak("Cannot directly construct an instance of role '%" SVf "'",
2340 SVfARG(meta->name));
2341 }
2342  
2343 18 XS_INTERNAL(injected_DOES)
2344 {
2345 18 dXSARGS;
2346 18 const ClassMeta *meta = MUST_CLASSMETA(XSANY.any_ptr);
2347 18 SV *self = ST(0);
2348 18 SV *wantrole = ST(1);
2349  
2350 PERL_UNUSED_ARG(items);
2351  
2352 18 CV *cv_does = NULL;
2353  
2354 24 ▶ while(meta != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while(meta != NULL) { 20 4 24
2355 20 ▶ AV *roles = meta->type == METATYPE_CLASS ? meta->cls.direct_roles : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *roles = meta->type == METATYPE_CLASS ? meta->cls.direct_roles : NULL; 20 0 20
2356 20 ▶ I32 nroles = roles ? av_count(roles) : 0;
Branch: 1 branch
BranchTrueFalseTotal
I32 nroles = roles ? av_count(roles) : 0; 20 0 20
2357  
2358 20 ▶ if(!cv_does && meta->cls.foreign_does)
Branch: 2 branches
BranchTrueFalseTotal
if(!cv_does && meta->cls.foreign_does) 20 0 20
if(!cv_does && meta->cls.foreign_does) 4 16 20
2359 4 cv_does = meta->cls.foreign_does;
2360  
2361 20 ▶ if(sv_eq(meta->name, wantrole)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sv_eq(meta->name, wantrole)) { 3 17 20
2362 3 XSRETURN_YES;
2363 }
2364  
2365 int i;
2366 23 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 17 6 23
2367 17 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(AvARRAY(roles)[i]);
2368 17 ▶ if(sv_eq(embedding->rolemeta->name, wantrole)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sv_eq(embedding->rolemeta->name, wantrole)) { 11 6 17
2369 11 XSRETURN_YES;
2370 }
2371 }
2372  
2373 6 ▶ meta = meta->type == METATYPE_CLASS ? meta->cls.supermeta : NULL;
Branch: 1 branch
BranchTrueFalseTotal
meta = meta->type == METATYPE_CLASS ? meta->cls.supermeta : NULL; 6 0 6
2374 }
2375  
2376 4 ▶ if (cv_does) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv_does) { 2 2 4
2377 /* return $self->DOES(@_); */
2378 2 dSP;
2379  
2380 2 ENTER;
2381 2 SAVETMPS;
2382  
2383 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
2384 2 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2 2
2385 2 PUSHs(self);
2386 2 PUSHs(wantrole);
2387 2 PUTBACK;
2388  
2389 2 int count = call_sv((SV*)cv_does, G_SCALAR);
2390  
2391 2 SPAGAIN;
2392  
2393 2 bool ret = false;
2394  
2395 2 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 2 0 2
2396 2 ret = POPi;
2397  
2398 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 1 2
2399 2 LEAVE;
2400  
2401 2 ▶ if(ret)
Branch: 1 branch
BranchTrueFalseTotal
if(ret) 2 0 2
2402 2 XSRETURN_YES;
2403 }
2404 else {
2405 /* We need to also respond to Object::Pad::UNIVERSAL and UNIVERSAL */
2406 2 ▶ if(sv_derived_from_sv(self, wantrole, 0))
Branch: 1 branch
BranchTrueFalseTotal
if(sv_derived_from_sv(self, wantrole, 0)) 0 2 2
2407 0 XSRETURN_YES;
2408 }
2409  
2410 2 XSRETURN_NO;
2411 }
2412  
2413 2 static OP *pp_croak_from_constructor(pTHX)
2414 {
2415 2 dSP;
2416  
2417 /* Walk up the caller stack to find the COP of the first caller; i.e. the
2418 * first one that wasn't in src/class.c
2419 */
2420 2 I32 count = 0;
2421 const PERL_CONTEXT *cx;
2422 2 ▶ while((cx = caller_cx(count, NULL))) {
Branch: 1 branch
BranchTrueFalseTotal
while((cx = caller_cx(count, NULL))) { 2 0 2
2423 2 ▶ const char *copfile = CopFILE(cx->blk_oldcop);
Branch: 1 branch
BranchTrueFalseTotal
const char *copfile = CopFILE(cx->blk_oldcop); 2 0 2
2424 2 ▶ if(!copfile|| strNE(copfile, "src/class.c")) {
Branch: 2 branches
BranchTrueFalseTotal
if(!copfile|| strNE(copfile, "src/class.c")) { 2 0 2
if(!copfile|| strNE(copfile, "src/class.c")) { 2 0 2
2425 2 PL_curcop = cx->blk_oldcop;
2426 2 break;
2427 }
2428 0 count++;
2429 }
2430  
2431 2 croak_sv(POPs);
2432 }
2433  
2434 24 OP *ObjectPad__newop_croak_from_constructor(pTHX_ SV *message)
2435 {
2436 24 return newUNOP_CUSTOM(&pp_croak_from_constructor, 0,
2437 newSVOP(OP_CONST, 0, message));
2438 }
2439  
2440 215 ClassMeta *ObjectPad_mop_create_class(pTHX_ enum MetaType type, SV *name)
2441 {
2442 assert(type == METATYPE_CLASS || type == METATYPE_ROLE);
2443  
2444 215 HV *stash = gv_stashsv(name, GV_ADD);
2445  
2446 ClassMeta *meta;
2447 215 Newx(meta, 1, ClassMeta);
2448  
2449 430 *meta = (ClassMeta){
2450 LINNET_INIT(LINNET_VAL_CLASSMETA)
2451 .type = type,
2452 .repr = REPR_AUTOSELECT,
2453 215 .name = SvREFCNT_inc(name),
2454  
2455 /* all roles are abstract */
2456 215 .abstract = (type == METATYPE_ROLE),
2457  
2458 .stash = stash,
2459  
2460 .next_fieldix = -1,
2461  
2462 215 .fields = newAV(),
2463 215 .direct_methods = newAV(),
2464 215 .requiremethods = newAV(),
2465 };
2466  
2467 215 switch(type) {
2468 183 case METATYPE_CLASS:
2469 183 meta->cls.direct_roles = newAV();
2470 183 meta->cls.embedded_roles = newAV();
2471 183 break;
2472  
2473 32 case METATYPE_ROLE:
2474 32 meta->role.superroles = newAV();
2475 32 meta->role.applied_classes = newHV();
2476 32 break;
2477 }
2478  
2479 215 need_PLparser();
2480  
2481 215 ▶ if(!PL_compcv) {
Branch: 1 branch
BranchTrueFalseTotal
if(!PL_compcv) { 2 213 215
2482 /* We require the initfields CV to have a CvOUTSIDE, or else cv_clone()
2483 * will segv when we compose role fields. Any class dynamically generated
2484 * by string eval() will likely not get one, because it won't inherit a
2485 * PL_compcv here. We'll fake it up
2486 * See also https://rt.cpan.org/Ticket/Display.html?id=137952
2487 */
2488 2 SAVEVPTR(PL_compcv);
2489 2 PL_compcv = find_runcv(0);
2490  
2491 assert(PL_compcv);
2492 }
2493  
2494 215 ENTER;
2495 215 SAVEGENERICSV(PL_curstash);
2496  
2497 215 PL_curstash = (HV *)SvREFCNT_inc(meta->stash);
2498 215 ▶ if(!IN_PERL_COMPILETIME) {
Branch: 1 branch
BranchTrueFalseTotal
if(!IN_PERL_COMPILETIME) { 6 209 215
2499 // We need to trick this too
2500 6 SAVESPTR(PL_curcop); PL_curcop = &PL_compiling;
2501 }
2502  
2503 /* Prepare meta->initfields for containing a CV parsing operation */
2504 {
2505 215 I32 floor_ix = start_subparse(FALSE, 0);
2506  
2507 215 extend_pad_vars(meta);
2508  
2509 /* Skip padix==3 so we're aligned again */
2510 215 ▶ if(meta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type != METATYPE_ROLE) 183 32 215
2511 183 pad_add_name_pvs("", 0, NULL, NULL);
2512  
2513 215 PADOFFSET padix = pad_add_name_pvs("%params", 0, NULL, NULL);
2514 215 ▶ if(padix != PADIX_PARAMS)
Branch: 1 branch
BranchTrueFalseTotal
if(padix != PADIX_PARAMS) 0 215 215
2515 0 croak("ARGH: Expected that padix[%%params] = 4");
2516  
2517 215 intro_my();
2518  
2519 215 suspend_compcv(&meta->initfields_compcv);
2520  
2521 215 ▶ LEAVE_SCOPE(floor_ix);
Branch: 1 branch
BranchTrueFalseTotal
LEAVE_SCOPE(floor_ix); 215 0 215
2522 }
2523  
2524 215 ▶ if(hv_fetchs(GvHV(PL_hintgv), "Object::Pad/experimental(composed_adjust)", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(GvHV(PL_hintgv), "Object::Pad/experimental(composed_adjust)", 0)) { 3 212 215
2525 3 meta->composed_adjust = TRUE;
2526  
2527 3 prepare_method_parse(meta);
2528  
2529 3 I32 floor_ix = start_subparse(FALSE, 0);
2530  
2531 3 start_method_parse(meta, FALSE);
2532  
2533 3 prepare_adjust_params(meta);
2534 3 meta->adjust_params = newAV();
2535  
2536 3 suspend_compcv(&meta->adjust_compcv);
2537 3 meta->adjust_methodscope = meta->methodscope;
2538  
2539 3 meta->next_field_for_adjust = 0;
2540  
2541 3 ▶ LEAVE_SCOPE(floor_ix);
Branch: 1 branch
BranchTrueFalseTotal
LEAVE_SCOPE(floor_ix); 3 0 3
2542 }
2543  
2544 215 meta->tmpcop = (COP *)newSTATEOP(0, NULL, NULL);
2545 215 CopFILE_set(meta->tmpcop, __FILE__);
2546  
2547 215 meta->methodscope = NULL;
2548  
2549 215 meta->initfields_lines = NULL;
2550  
2551 {
2552 /* Create the constructor CV but don't add it to symbol table yet */
2553 215 XSUBADDR_t constructor_func =
2554 (type == METATYPE_CLASS) ? injected_constructor
2555 215 ▶ : injected_constructor_role;
Branch: 1 branch
BranchTrueFalseTotal
: injected_constructor_role; 183 32 215
2556  
2557 215 CV *cv = meta->constructor = newXS(NULL, constructor_func, __FILE__);
2558 215 CvANON_off(cv);
2559 215 CvXSUBANY(cv).any_ptr = meta;
2560 }
2561  
2562 {
2563 215 CV *doescv = newXS_flags("DOES", injected_DOES, __FILE__, NULL, 0);
2564 215 CvXSUBANY(doescv).any_ptr = meta;
2565 }
2566  
2567 {
2568 215 GV **gvp = (GV **)hv_fetchs(stash, "META", GV_ADD);
2569 215 GV *gv = *gvp;
2570 215 gv_init_pvn(gv, stash, "META", 4, 0);
2571 215 GvMULTI_on(gv);
2572  
2573 SV *sv;
2574 215 ▶ sv_setref_uv(sv = GvSVn(gv), "Object::Pad::MOP::Class", PTR2UV(meta));
Branch: 1 branch
BranchTrueFalseTotal
sv_setref_uv(sv = GvSVn(gv), "Object::Pad::MOP::Class", PTR2UV(meta)); 0 215 215
2575  
2576 215 newCONSTSUB(meta->stash, "META", sv);
2577 }
2578  
2579 #if HAVE_PERL_VERSION(5, 26, 0)
2580 /* On Perl 5.26 and above we can create and grab the @ISA array now while
2581 * we have the PL_curstash set right
2582 * On earlier perls it doesn't work this way so we have to go the long way
2583 * around
2584 */
2585 215 meta->isa = get_av("ISA", GV_ADDMG);
2586 #else
2587 {
2588 SV *isaname = newSVpvf("%" SVf "::ISA", name);
2589 SAVEFREESV(isaname);
2590  
2591 meta->isa = get_av(SvPV_nolen(isaname), GV_ADD | (SvFLAGS(isaname) & SVf_UTF8));
2592 }
2593 #endif
2594  
2595 215 LEAVE;
2596  
2597 215 return meta;
2598 }
2599  
2600 44 void ObjectPad_mop_class_set_superclass(pTHX_ ClassMeta *meta, SV *superclassname)
2601 {
2602 assert(meta->type == METATYPE_CLASS);
2603  
2604 44 ▶ if(meta->has_superclass)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->has_superclass) 0 44 44
2605 0 croak("Class already has a superclass, cannot add another");
2606  
2607 44 av_push(meta->isa, SvREFCNT_inc(superclassname));
2608  
2609 44 ClassMeta *supermeta = NULL;
2610  
2611 44 HV *superstash = gv_stashsv(superclassname, 0);
2612 44 GV **metagvp = (GV **)hv_fetchs(superstash, "META", 0);
2613 44 ▶ if(metagvp)
Branch: 1 branch
BranchTrueFalseTotal
if(metagvp) 32 12 44
2614 32 supermeta = MUST_CLASSMETA(SvUV(SvRV(GvSV(*metagvp))));
2615  
2616 44 ▶ if(supermeta) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta) { 32 12 44
2617 /* A subclass of an Object::Pad class */
2618 32 ▶ if(supermeta->type != METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->type != METATYPE_CLASS) 0 32 32
2619 0 croak("%" SVf " is not a class", SVfARG(superclassname));
2620  
2621 /* If it isn't yet sealed (e.g. because we're an inner class of it),
2622 * seal it now
2623 */
2624 32 ▶ if(!supermeta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(!supermeta->sealed) 2 30 32
2625 2 mop_class_seal(supermeta);
2626  
2627 32 meta->start_fieldix = supermeta->next_fieldix;
2628 32 meta->repr = supermeta->repr;
2629 32 meta->cls.foreign_new = supermeta->cls.foreign_new;
2630  
2631 32 ▶ if(supermeta->buildcvs) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->buildcvs) { 4 28 32
2632 4 ▶ if(!meta->buildcvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->buildcvs) 4 0 4
2633 4 meta->buildcvs = newAV();
2634  
2635 4 av_push_from_av_noinc(meta->buildcvs, supermeta->buildcvs);
2636 }
2637  
2638 32 ▶ if(supermeta->adjustcvs) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->adjustcvs) { 5 27 32
2639 5 ▶ if(!meta->adjustcvs)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->adjustcvs) 5 0 5
2640 5 meta->adjustcvs = newAV();
2641  
2642 5 av_push_from_av_noinc(meta->adjustcvs, supermeta->adjustcvs);
2643 }
2644  
2645 32 ▶ if(supermeta->fieldhooks_makefield) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->fieldhooks_makefield) { 0 32 32
2646 0 ▶ if(!meta->fieldhooks_makefield)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->fieldhooks_makefield) 0 0 0
2647 0 meta->fieldhooks_makefield = newAV();
2648  
2649 0 av_push_from_av_noinc(meta->fieldhooks_makefield, supermeta->fieldhooks_makefield);
2650 }
2651  
2652 32 ▶ if(supermeta->fieldhooks_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->fieldhooks_construct) { 2 30 32
2653 2 ▶ if(!meta->fieldhooks_construct)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->fieldhooks_construct) 2 0 2
2654 2 meta->fieldhooks_construct = newAV();
2655  
2656 2 av_push_from_av_noinc(meta->fieldhooks_construct, supermeta->fieldhooks_construct);
2657 }
2658  
2659 32 ▶ if(supermeta->parammap) {
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->parammap) { 7 25 32
2660 7 HV *old = supermeta->parammap;
2661 7 HV *new = meta->parammap = newHV();
2662  
2663 7 hv_iterinit(old);
2664  
2665 HE *iter;
2666 15 ▶ while((iter = hv_iternext(old))) {
Branch: 1 branch
BranchTrueFalseTotal
while((iter = hv_iternext(old))) { 8 7 15
2667 8 STRLEN klen = HeKLEN(iter);
2668 /* Don't SvREFCNT_inc() the values because they aren't really SV *s */
2669 /* Subclasses *DIRECTLY SHARE* their param metas because the
2670 * information in them is directly compatible
2671 */
2672 if(klen < 0)
2673 hv_store_ent(new, HeSVKEY(iter), HeVAL(iter), HeHASH(iter));
2674 else
2675 8 hv_store(new, HeKEY(iter), klen, HeVAL(iter), HeHASH(iter));
2676 }
2677 }
2678  
2679 32 ▶ if(supermeta->abstract)
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->abstract) 4 28 32
2680 4 copy_requiremethods_from(meta, supermeta);
2681  
2682 32 ▶ if(supermeta->has_adjust)
Branch: 1 branch
BranchTrueFalseTotal
if(supermeta->has_adjust) 5 27 32
2683 5 meta->has_adjust = true;
2684  
2685 U32 nroles;
2686 32 RoleEmbedding **embeddings = mop_class_get_all_roles(supermeta, &nroles);
2687 32 ▶ if(nroles) {
Branch: 1 branch
BranchTrueFalseTotal
if(nroles) { 5 27 32
2688 U32 i;
2689 10 ▶ for(i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nroles; i++) { 5 5 10
2690 5 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(embeddings[i]);
2691 5 ClassMeta *rolemeta = embedding->rolemeta;
2692  
2693 5 av_push(meta->cls.embedded_roles, (SV *)embedding);
2694 5 hv_store_ent(rolemeta->role.applied_classes, meta->name, (SV *)embedding, 0);
2695 }
2696 }
2697 }
2698 else {
2699 /* A subclass of a foreign class */
2700 12 meta->cls.foreign_new = fetch_superclass_method_pv(meta->stash, "new", 3, -1);
2701 12 ▶ if(!meta->cls.foreign_new)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->cls.foreign_new) 0 12 12
2702 0 croak("Unable to find SUPER::new for %" SVf, superclassname);
2703  
2704 12 meta->cls.foreign_does = fetch_superclass_method_pv(meta->stash, "DOES", 4, -1);
2705 }
2706  
2707 44 meta->has_superclass = true;
2708 44 meta->cls.supermeta = supermeta;
2709 44 }
2710  
2711 44 void ObjectPad_mop_class_load_and_set_superclass(pTHX_ ClassMeta *class, SV *supername, SV *superver)
2712 {
2713 44 ▶ if(class->type != METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(class->type != METATYPE_CLASS) 0 44 44
2714 0 croak("Only a class may extend another");
2715  
2716 44 HV *superstash = gv_stashsv(supername, 0);
2717 44 ▶ if(!superstash || !hv_fetchs(superstash, "new", 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!superstash || !hv_fetchs(superstash, "new", 0)) { 44 0 44
if(!superstash || !hv_fetchs(superstash, "new", 0)) { 1 43 44
2718 /* Try to `require` the module then attempt a second time */
2719 /* load_module() will modify the name argument and take ownership of it */
2720 1 load_module(PERL_LOADMOD_NOIMPORT, newSVsv(supername), NULL, NULL);
2721 1 superstash = gv_stashsv(supername, 0);
2722 }
2723  
2724 44 ▶ if(!superstash)
Branch: 1 branch
BranchTrueFalseTotal
if(!superstash) 0 44 44
2725 0 croak("Superclass %" SVf " does not exist", supername);
2726  
2727 44 ▶ if(superver && SvOK(superver))
Branch: 2 branches
BranchTrueFalseTotal
if(superver && SvOK(superver)) 4 40 44
if(superver && SvOK(superver)) 1 3 4
2728 1 ensure_module_version(supername, superver);
2729  
2730 43 mop_class_set_superclass(class, supername);
2731 43 }
2732  
2733 4 void ObjectPad_mop_class_inherit_from_superclass(pTHX_ ClassMeta *meta, SV **args, size_t nargs)
2734 {
2735 4 ▶ if(!meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->begun) 0 4 4
2736 0 croak("Cannot inherit into a class that is not yet begun");
2737 4 ▶ if(meta->sealed)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->sealed) 0 4 4
2738 0 croak("Cannot inherit into an already-sealed class");
2739  
2740 4 ClassMeta *supermeta = meta->cls.supermeta;
2741 4 ▶ if(meta->type != METATYPE_CLASS || !supermeta)
Branch: 2 branches
BranchTrueFalseTotal
if(meta->type != METATYPE_CLASS || !supermeta) 4 0 4
if(meta->type != METATYPE_CLASS || !supermeta) 0 4 4
2742 0 croak("Cannot inherit into a non-class or from a non-Object::Pad-based superclass");
2743  
2744 8 ▶ for(int i = 0; i < nargs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < nargs; i++) { 4 4 8
2745 4 SV *arg = args[i];
2746  
2747 4 ▶ if(SvPVX(arg)[0] == '$') {
Branch: 1 branch
BranchTrueFalseTotal
if(SvPVX(arg)[0] == '$') { 4 0 4
2748 /* A field name */
2749 4 FieldMeta *superfield = mop_class_find_field(supermeta, arg, FIND_FIELD_ONLY_INHERITABLE);
2750 4 ▶ if(!superfield)
Branch: 1 branch
BranchTrueFalseTotal
if(!superfield) 0 4 4
2751 0 croak("Superclass does not have a field named %" SVf " (or it is not :inheritable",
2752 SVfARG(arg));
2753 assert(superfield->fieldix < meta->next_fieldix);
2754  
2755 4 ▶ if(mop_class_find_field(meta, arg, 0))
Branch: 1 branch
BranchTrueFalseTotal
if(mop_class_find_field(meta, arg, 0)) 0 4 4
2756 0 croak("Cannot add another field named %" SVf, arg);
2757  
2758 4 FieldMeta *fieldmeta = mop_create_field(superfield->name, superfield->fieldix, meta);
2759 4 fieldmeta->is_direct = false;
2760  
2761 4 av_push(meta->fields, (SV *)fieldmeta);
2762 /* TODO: Think about running some field hooks?? */
2763 }
2764 else
2765 0 croak("Unrecognised inherit argument '%" SVf "'", SVfARG(arg));
2766 }
2767 4 }
2768  
2769 208 void ObjectPad_mop_class_prepare_parse(pTHX_ ClassMeta *meta)
2770 {
2771 208 ▶ if(meta->lexical_new) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->lexical_new) { 2 206 208
2772 2 CV *cv = meta->constructor;
2773  
2774 #ifdef CVf_LEXICAL
2775 2 CvLEXICAL_on(cv);
2776 #endif
2777  
2778 2 PADOFFSET padix = pad_add_name_pvs("&new", padadd_STATE, NULL, NULL);
2779  
2780 2 SV **padslot = &(PadARRAY(PL_comppad))[padix];
2781  
2782 2 SvREFCNT_dec(*padslot);
2783 2 *padslot = (SV *)cv;
2784  
2785 2 GV *namegv = (GV *)newSV(0);
2786 2 gv_init_pvn(namegv, meta->stash, "new", 3, 0);
2787 2 CvGV_set(cv, namegv);
2788  
2789 2 SvREFCNT_dec(namegv);
2790  
2791 2 intro_my();
2792 }
2793 208 }
2794  
2795 404 void ObjectPad_mop_class_begin(pTHX_ ClassMeta *meta)
2796 {
2797 404 ▶ if(meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->begun) 195 209 404
2798 /* idempotent */
2799 195 return;
2800  
2801 /* Inject the constructor in the symbol table if it isn't lexical */
2802 209 ▶ if(!meta->lexical_new) {
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->lexical_new) { 207 2 209
2803 207 HV *stash = meta->stash;
2804 207 GV **gvp = (GV **)hv_fetchs(stash, "new", GV_ADD);
2805 207 GV *gv = *gvp;
2806 207 gv_init_pvn(gv, stash, "new", 3, 0);
2807 207 GvMULTI_on(gv);
2808  
2809 207 GvCV_set(gv, meta->constructor);
2810 }
2811  
2812 209 ▶ if(meta->type == METATYPE_CLASS && !meta->cls.supermeta) {
Branch: 2 branches
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS && !meta->cls.supermeta) { 178 31 209
if(meta->type == METATYPE_CLASS && !meta->cls.supermeta) { 146 32 178
2813 146 av_push(meta->isa, newSVpvs("Object::Pad::UNIVERSAL"));
2814 }
2815  
2816 209 ▶ if(meta->type == METATYPE_CLASS &&
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_CLASS && 178 31 209
2817 178 ▶ meta->repr == REPR_AUTOSELECT && !meta->cls.foreign_new)
Branch: 2 branches
BranchTrueFalseTotal
meta->repr == REPR_AUTOSELECT && !meta->cls.foreign_new) 144 34 178
meta->repr == REPR_AUTOSELECT && !meta->cls.foreign_new) 132 12 144
2818 132 meta->repr = REPR_NATIVE;
2819  
2820 209 meta->begun = true;
2821 209 meta->next_fieldix = meta->start_fieldix;
2822 }
2823  
2824 /*******************
2825 * Attribute hooks *
2826 *******************/
2827  
2828 #ifndef isSPACE_utf8_safe
2829 /* this isn't really safe but it's the best we can do */
2830 # define isSPACE_utf8_safe(p, e) (PERL_UNUSED_ARG(e), isSPACE_utf8(p))
2831 #endif
2832  
2833 #define split_package_ver(value, pkgname, pkgversion) S_split_package_ver(aTHX_ value, pkgname, pkgversion)
2834 4 static const char *S_split_package_ver(pTHX_ SV *value, SV *pkgname, SV *pkgversion)
2835 {
2836 4 const char *start = SvPVX(value), *p = start, *end = start + SvCUR(value);
2837  
2838 42 ▶ while(*p && !isSPACE_utf8_safe(p, end))
Branch: 26 branches
BranchTrueFalseTotal
while(*p && !isSPACE_utf8_safe(p, end)) 38 4 42
while(*p && !isSPACE_utf8_safe(p, end)) 0 38 38
while(*p && !isSPACE_utf8_safe(p, end)) 38 0 38
while(*p && !isSPACE_utf8_safe(p, end)) 38 0 38
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && !isSPACE_utf8_safe(p, end)) 0 0 0
2839 38 p += UTF8SKIP(p);
2840  
2841 4 sv_setpvn(pkgname, start, p - start);
2842 4 ▶ if(SvUTF8(value))
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(value)) 0 4 4
2843 0 SvUTF8_on(pkgname);
2844  
2845 4 ▶ while(*p && isSPACE_utf8_safe(p, end))
Branch: 26 branches
BranchTrueFalseTotal
while(*p && isSPACE_utf8_safe(p, end)) 0 4 4
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
2846 0 p += UTF8SKIP(p);
2847  
2848 4 ▶ if(*p) {
Branch: 1 branch
BranchTrueFalseTotal
if(*p) { 0 4 4
2849 /* scan_version() gets upset about trailing content. We need to extract
2850 * exactly what it wants
2851 */
2852 0 start = p;
2853 0 ▶ if(*p == 'v')
Branch: 1 branch
BranchTrueFalseTotal
if(*p == 'v') 0 0 0
2854 0 p++;
2855 0 ▶ while(*p && strchr("0123456789._", *p))
Branch: 2 branches
BranchTrueFalseTotal
while(*p && strchr("0123456789._", *p)) 0 0 0
while(*p && strchr("0123456789._", *p)) 0 0 0
2856 0 p++;
2857 0 SV *tmpsv = newSVpvn(start, p - start);
2858 0 SAVEFREESV(tmpsv);
2859  
2860 0 scan_version(SvPVX(tmpsv), pkgversion, FALSE);
2861 }
2862  
2863 4 ▶ while(*p && isSPACE_utf8_safe(p, end))
Branch: 26 branches
BranchTrueFalseTotal
while(*p && isSPACE_utf8_safe(p, end)) 0 4 4
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
while(*p && isSPACE_utf8_safe(p, end)) 0 0 0
2864 0 p += UTF8SKIP(p);
2865  
2866 4 return p;
2867 }
2868  
2869 /* :isa */
2870  
2871 3 static bool classhook_isa_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata, void *_funcdata)
2872 {
2873 3 SV *superclassname = newSV(0), *superclassver = newSV(0);
2874 3 SAVEFREESV(superclassname);
2875 3 SAVEFREESV(superclassver);
2876  
2877 3 const char *end = split_package_ver(value, superclassname, superclassver);
2878  
2879 3 ▶ if(*end)
Branch: 1 branch
BranchTrueFalseTotal
if(*end) 0 3 3
2880 0 croak("Unexpected characters while parsing :isa() attribute: %s", end);
2881  
2882 3 mop_class_load_and_set_superclass(classmeta, superclassname, superclassver);
2883  
2884 3 return FALSE;
2885 }
2886  
2887 static const struct ClassHookFuncs classhooks_isa = {
2888 .ver = OBJECTPAD_ABIVERSION,
2889 .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
2890 .apply = &classhook_isa_apply,
2891 };
2892  
2893 /* :does */
2894  
2895 1 static bool classhook_does_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata, void *_funcdata)
2896 {
2897 1 SV *rolename = newSV(0), *rolever = newSV(0);
2898 1 SAVEFREESV(rolename);
2899 1 SAVEFREESV(rolever);
2900  
2901 1 const char *end = split_package_ver(value, rolename, rolever);
2902  
2903 1 ▶ if(*end)
Branch: 1 branch
BranchTrueFalseTotal
if(*end) 0 1 1
2904 0 croak("Unexpected characters while parsing :does() attribute: %s", end);
2905  
2906 1 mop_class_begin(classmeta);
2907  
2908 1 mop_class_load_and_add_role(classmeta, rolename, rolever);
2909  
2910 1 return FALSE;
2911 }
2912  
2913 static const struct ClassHookFuncs classhooks_does = {
2914 .ver = OBJECTPAD_ABIVERSION,
2915 .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
2916 .apply = &classhook_does_apply,
2917 };
2918  
2919 /* :abstract */
2920  
2921 3 static bool classhook_abstract_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata_ptr, void *_funcdata)
2922 {
2923 3 ▶ if(classmeta->type == METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->type == METATYPE_ROLE) 0 3 3
2924 0 warn("All roles are already abstract; there is no need to declare them as such");
2925  
2926 3 classmeta->abstract = TRUE;
2927  
2928 3 return FALSE;
2929 }
2930  
2931 static const struct ClassHookFuncs classhooks_abstract = {
2932 .ver = OBJECTPAD_ABIVERSION,
2933 .flags = OBJECTPAD_FLAG_ATTR_NO_VALUE,
2934 .apply = &classhook_abstract_apply,
2935 };
2936  
2937 /* :repr */
2938  
2939 4 static bool classhook_repr_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata, void *_funcdata)
2940 {
2941 4 char *val = SvPV_nolen(value); /* all comparisons are ASCII */
2942  
2943 4 ▶ if(strEQ(val, "native")) {
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(val, "native")) { 1 3 4
2944 1 ▶ if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new)
Branch: 2 branches
BranchTrueFalseTotal
if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new) 1 0 1
if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new) 1 0 1
2945 1 croak("Cannot switch a subclass of a foreign superclass type to :repr(native)");
2946 0 classmeta->repr = REPR_NATIVE;
2947 }
2948 3 ▶ else if(strEQ(val, "HASH"))
Branch: 1 branch
BranchTrueFalseTotal
else if(strEQ(val, "HASH")) 1 2 3
2949 1 classmeta->repr = REPR_HASH;
2950 2 ▶ else if(strEQ(val, "magic")) {
Branch: 1 branch
BranchTrueFalseTotal
else if(strEQ(val, "magic")) { 0 2 2
2951 0 ▶ if(classmeta->type != METATYPE_CLASS || !classmeta->cls.foreign_new)
Branch: 2 branches
BranchTrueFalseTotal
if(classmeta->type != METATYPE_CLASS || !classmeta->cls.foreign_new) 0 0 0
if(classmeta->type != METATYPE_CLASS || !classmeta->cls.foreign_new) 0 0 0
2952 0 croak("Cannot switch to :repr(magic) without a foreign superclass");
2953 0 classmeta->repr = REPR_MAGIC;
2954 }
2955 2 ▶ else if(strEQ(val, "keys"))
Branch: 1 branch
BranchTrueFalseTotal
else if(strEQ(val, "keys")) 1 1 2
2956 1 classmeta->repr = REPR_KEYS;
2957 1 ▶ else if(strEQ(val, "pvobj")) {
Branch: 1 branch
BranchTrueFalseTotal
else if(strEQ(val, "pvobj")) { 1 0 1
2958 1 ▶ if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new)
Branch: 2 branches
BranchTrueFalseTotal
if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new) 1 0 1
if(classmeta->type == METATYPE_CLASS && classmeta->cls.foreign_new) 0 1 1
2959 0 croak("Cannot switch a subclass of a foreign superclass type to :repr(pvobj)");
2960 #ifdef HAVE_SVt_PVOBJ
2961 1 classmeta->repr = REPR_PVOBJ;
2962 #else
2963 croak("Cannot switch to :repr(pvobj) on Perl " PERL_VERSION_STRING);
2964 #endif
2965 }
2966 0 ▶ else if(strEQ(val, "default") || strEQ(val, "autoselect"))
Branch: 2 branches
BranchTrueFalseTotal
else if(strEQ(val, "default") || strEQ(val, "autoselect")) 0 0 0
else if(strEQ(val, "default") || strEQ(val, "autoselect")) 0 0 0
2967 0 classmeta->repr = REPR_AUTOSELECT;
2968 else
2969 0 croak("Unrecognised class representation type %" SVf, SVfARG(value));
2970  
2971 3 return FALSE;
2972 }
2973  
2974 static const struct ClassHookFuncs classhooks_repr = {
2975 .ver = OBJECTPAD_ABIVERSION,
2976 .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
2977 .apply = &classhook_repr_apply,
2978 };
2979  
2980 /* :compat */
2981  
2982 3 static bool classhook_compat_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata, void *_funcdata)
2983 {
2984 3 ▶ if(strEQ(SvPV_nolen(value), "invokable")) {
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(SvPV_nolen(value), "invokable")) { 3 0 3
2985 3 ▶ if(classmeta->type != METATYPE_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->type != METATYPE_ROLE) 0 3 3
2986 0 croak(":compat(invokable) only applies to a role");
2987  
2988 3 classmeta->role_is_invokable = true;
2989 }
2990 else
2991 0 croak("Unrecognised class compatibility argument %" SVf, SVfARG(value));
2992  
2993 3 return FALSE;
2994 }
2995  
2996 static const struct ClassHookFuncs classhooks_compat = {
2997 .ver = OBJECTPAD_ABIVERSION,
2998 .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
2999 .apply = &classhook_compat_apply,
3000 };
3001  
3002 /* :strict */
3003  
3004 5 static bool classhook_strict_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata_ptr, void *_funcdata)
3005 {
3006 5 ▶ if(strEQ(SvPV_nolen(value), "params"))
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(SvPV_nolen(value), "params")) 5 0 5
3007 5 classmeta->strict_params = TRUE;
3008 else
3009 0 croak("Unrecognised class strictness type %" SVf, SVfARG(value));
3010  
3011 5 return FALSE;
3012 }
3013  
3014 static const struct ClassHookFuncs classhooks_strict = {
3015 .ver = OBJECTPAD_ABIVERSION,
3016 .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
3017 .apply = &classhook_strict_apply,
3018 };
3019  
3020 /* :lexical_new */
3021  
3022 2 static bool classhook_lexicalnew_apply(pTHX_ ClassMeta *classmeta, SV *value, SV **attrdata_ptr, void *_funcdata)
3023 {
3024 2 classmeta->lexical_new = TRUE;
3025  
3026 2 return FALSE;
3027 }
3028  
3029 static const struct ClassHookFuncs classhooks_lexicalnew = {
3030 .ver = OBJECTPAD_ABIVERSION,
3031 .flags = OBJECTPAD_FLAG_ATTR_NO_VALUE,
3032 .apply = &classhook_lexicalnew_apply,
3033 };
3034  
3035 61 void ObjectPad__boot_classes(pTHX)
3036 {
3037 61 register_class_attribute("isa", &classhooks_isa, NULL);
3038 61 register_class_attribute("does", &classhooks_does, NULL);
3039 61 register_class_attribute("abstract", &classhooks_abstract, NULL);
3040 61 register_class_attribute("repr", &classhooks_repr, NULL);
3041 61 register_class_attribute("compat", &classhooks_compat, NULL);
3042 61 register_class_attribute("strict", &classhooks_strict, NULL);
3043 61 register_class_attribute("lexical_new", &classhooks_lexicalnew, NULL);
3044  
3045 #ifdef HAVE_DMD_HELPER
3046 DMD_ADD_ROOT((SV *)&vtbl_backingav, "the Object::Pad backing AV VTBL");
3047 #endif
3048 61 }
« hax/force_list_keeping_pushmark.c.inc ↑ Summary src/field.c »