object.c

filter: Statementstmt 98.9% 2335 / 2361 Branchbran 85.6% 1730 / 2080 (59 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.7% 4065 / 4441 (59 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Object/Proto/Install/Files.pm ↑ Summary blib/lib/Object/Proto.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "include/object_compat.h"
5  
6 /* Object flags - stored in mg_private */
7 #define OBJ_FLAG_LOCKED 0x01
8 #define OBJ_FLAG_FROZEN 0x02
9  
10 /* Built-in type IDs for inline checks */
11 typedef enum {
12 TYPE_NONE = 0,
13 TYPE_ANY,
14 TYPE_DEFINED,
15 TYPE_STR,
16 TYPE_INT,
17 TYPE_NUM,
18 TYPE_BOOL,
19 TYPE_ARRAYREF,
20 TYPE_HASHREF,
21 TYPE_CODEREF,
22 TYPE_OBJECT,
23 TYPE_CUSTOM /* Uses registered or callback check */
24 } BuiltinTypeID;
25  
26 /* Type check/coerce function signatures for external plugins */
27 typedef bool (*ObjectTypeCheckFunc)(pTHX_ SV *val);
28 typedef SV* (*ObjectTypeCoerceFunc)(pTHX_ SV *val);
29 typedef bool (*ObjectTypeCheckFuncEx)(pTHX_ SV *val, void *data);
30 typedef SV* (*ObjectTypeCoerceFuncEx)(pTHX_ SV *val, void *data);
31  
32 /* Registered type entry (for plugins) */
33 typedef struct {
34 char *name;
35 ObjectTypeCheckFunc check; /* C function for type check */
36 ObjectTypeCoerceFunc coerce; /* C function for coercion */
37 SV *perl_check; /* Fallback Perl callback */
38 SV *perl_coerce; /* Fallback Perl coercion */
39 ObjectTypeCheckFuncEx check_ex; /* C function with user-data */
40 ObjectTypeCoerceFuncEx coerce_ex; /* C coerce with user-data */
41 void *data; /* Opaque user-data for _ex functions */
42 } RegisteredType;
43  
44 /* Per-slot specification - parsed from "name:Type:default(val)" */
45 typedef struct {
46 char *name;
47 BuiltinTypeID type_id; /* Built-in type or TYPE_CUSTOM */
48 RegisteredType *registered; /* For external XS types */
49 SV *default_sv; /* Default value (immutable, refcnt'd) */
50 SV *trigger_cb; /* Trigger callback */
51 SV *coerce_cb; /* Coercion callback (Perl) */
52 SV *builder_name; /* Builder method name for lazy attrs */
53 U8 is_required; /* Croak if not provided in new() */
54 U8 is_readonly; /* Croak if set after new() */
55 U8 is_lazy; /* Build on first access, not at new() */
56 U8 has_default;
57 U8 has_trigger;
58 U8 has_coerce;
59 U8 has_type;
60 U8 has_builder; /* Has builder method */
61 U8 has_clearer; /* Generate clear_X method */
62 U8 has_predicate; /* Generate has_X method */
63 U8 is_weak; /* Weaken references when stored */
64 U8 is_private; /* Accessor only callable from the owning package */
65 U8 has_checks; /* is_readonly | is_required | has_coerce | TYPE_CUSTOM — skip block when 0 */
66 SV *clearer_name; /* Custom clearer method name */
67 SV *predicate_name; /* Custom predicate method name */
68 SV *reader_name; /* Custom reader method name (get_X style) */
69 SV *writer_name; /* Custom writer method name (set_X style) */
70 SV *init_arg; /* Alternate constructor argument name */
71 } SlotSpec;
72  
73 /* Custom op definitions */
74 static XOP object_get_xop;
75 static XOP object_set_xop;
76 static XOP object_set_typed_xop;
77  
78 /* Per-class metadata */
79 typedef struct ClassMeta_s ClassMeta; /* Forward declaration */
80  
81 /* Method modifier chain - linked list for each type */
82 typedef struct MethodModifier_s {
83 SV *callback;
84 struct MethodModifier_s *next;
85 } MethodModifier;
86  
87 /* Modified method wrapper */
88 typedef struct {
89 CV *original_cv;
90 MethodModifier *before_chain;
91 MethodModifier *after_chain;
92 MethodModifier *around_chain;
93 } ModifiedMethod;
94  
95 /* Role metadata */
96 typedef struct {
97 char *role_name;
98 char **required_methods; /* Methods consuming class MUST have */
99 IV required_count;
100 SlotSpec **slots; /* Slots the role provides */
101 IV slot_count;
102 HV *stash; /* Role's stash for provided methods */
103 } RoleMeta;
104  
105 /* Per-class metadata */
106 struct ClassMeta_s {
107 char *class_name;
108 HV *prop_to_idx; /* property name -> slot index */
109 HV *arg_to_idx; /* constructor argument name -> slot index (init_arg or property name) */
110 char **idx_to_prop; /* slot index -> property name */
111 IV slot_count;
112 HV *stash; /* cached stash pointer */
113 /* Type system extensions */
114 SlotSpec **slots; /* Per-slot specifications, NULL if no specs */
115 U8 has_any_types; /* Quick check: any slot has type checking? */
116 U8 has_any_defaults; /* Quick check: any slot has defaults? */
117 U8 has_any_triggers; /* Quick check: any slot has triggers? */
118 U8 has_any_required; /* Quick check: any slot is required? */
119 U8 has_any_lazy; /* Quick check: any slot is lazy? */
120 U8 has_any_builders; /* Quick check: any slot has builders? */
121 U8 has_any_weak; /* Quick check: any slot has weak refs? */
122 /* Singleton support */
123 SV *singleton_instance; /* Cached singleton instance, NULL if not a singleton */
124 U8 is_singleton; /* Flag: class is a singleton */
125 /* DEMOLISH support - only set if class has DEMOLISH method */
126 CV *demolish_cv; /* Cached DEMOLISH method, NULL if none */
127 /* BUILD support - called after new() */
128 CV *build_cv; /* Cached BUILD method, NULL if none */
129 U8 has_build; /* Flag: class has BUILD method */
130 /* Role support */
131 RoleMeta **consumed_roles; /* Array of consumed roles, NULL if none */
132 IV role_count;
133 /* Method modifier registry - only allocated if modifiers are used */
134 HV *modified_methods; /* method name -> ModifiedMethod*, NULL if none */
135 /* Inheritance support */
136 char **parent_classes; /* Array of parent class names, NULL if no parents */
137 struct ClassMeta_s **parent_metas; /* Array of parent ClassMeta pointers */
138 IV parent_count; /* Number of parent classes */
139 };
140  
141 /* Global class registry */
142 static HV *g_class_registry = NULL; /* class name -> ClassMeta* */
143  
144 /* Global type registry for external plugins */
145 static HV *g_type_registry = NULL; /* type name -> RegisteredType* */
146  
147 /* Global role registry */
148 static HV *g_role_registry = NULL; /* role name -> RoleMeta* */
149  
150 /* Forward declaration for FuncAccessorData */
151 typedef struct FuncAccessorData_s FuncAccessorData;
152  
153 /* Global registry for function accessor data (to avoid storing pointers in op_targ) */
154 static FuncAccessorData **g_func_accessor_registry = NULL;
155 static IV g_func_accessor_count = 0;
156 static IV g_func_accessor_capacity = 0;
157  
158 /* Forward declarations */
159 static ClassMeta* get_class_meta(pTHX_ const char *class_name, STRLEN len);
160 static void install_constructor(pTHX_ const char *class_name, ClassMeta *meta);
161 static void install_accessor(pTHX_ const char *class_name, const char *prop_name, IV idx);
162 static void install_accessor_typed(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta);
163 static void install_clearer(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name);
164 static void install_predicate(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name);
165 static void install_destroy_wrapper(pTHX_ const char *class_name, ClassMeta *meta);
166 static RoleMeta* get_role_meta(pTHX_ const char *role_name, STRLEN len);
167 XS_INTERNAL(xs_prototype);
168 XS_INTERNAL(xs_set_prototype);
169  
170 /* ============================================
171 Built-in type checking (inline)
172 ============================================ */
173  
174 4422 OBJECT_INLINE BuiltinTypeID parse_builtin_type(const char *type_str, STRLEN len) {
175 4422 ▶ if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR; 3353 1069 4422
if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR; 3151 202 3353
176 1271 ▶ if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT; 202 1069 1271
if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT; 119 83 202
177 1152 ▶ if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM; 83 1069 1152
if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM; 19 64 83
178 1133 ▶ if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY; 64 1069 1133
if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY; 58 6 64
179 1075 ▶ if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL; 12 1063 1075
if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL; 9 3 12
180 1066 ▶ if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT; 10 1056 1066
if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT; 8 2 10
181 1058 ▶ if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED; 14 1044 1058
if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED; 2 12 14
182 1056 ▶ if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF; 12 1044 1056
if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF; 3 9 12
183 1053 ▶ if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF; 9 1044 1053
if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF; 7 2 9
184 1046 ▶ if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF; 17 1029 1046
if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF; 16 1 17
185 1030 return TYPE_NONE; /* Unknown - could be custom */
186 }
187  
188 /* Inline type check - returns true if value passes check */
189 34994 OBJECT_INLINE bool check_builtin_type(pTHX_ SV *val, BuiltinTypeID type_id) {
190 34994 switch (type_id) { /* # uncoverable branch true count:11 */
191 67 case TYPE_ANY:
192 67 return true;
193 6 case TYPE_DEFINED:
194 /* Be defensive: SvOK may not catch all defined values in older Perls */
195 /*
196 # uncoverable condition left count:2
197 # uncoverable condition left count:3
198 # uncoverable condition left count:4 */
199 6 ▶ return SvOK(val) || SvIOK(val) || SvNOK(val) || SvPOK(val);
Branch: 4 branches
BranchTrueFalseTotal
return SvOK(val) || SvIOK(val) || SvNOK(val) || SvPOK(val); 3 3 6
return SvOK(val) || SvIOK(val) || SvNOK(val) || SvPOK(val); 3 0 3
return SvOK(val) || SvIOK(val) || SvNOK(val) || SvPOK(val); 3 0 3
return SvOK(val) || SvIOK(val) || SvNOK(val) || SvPOK(val); 0 3 3
200 15647 case TYPE_STR:
201 15647 ▶ return SvOK(val) && !SvROK(val); /* defined non-ref */
Branch: 2 branches
BranchTrueFalseTotal
return SvOK(val) && !SvROK(val); /* defined non-ref */ 15644 3 15647
return SvOK(val) && !SvROK(val); /* defined non-ref */ 15639 5 15644
202 10994 case TYPE_INT:
203 10994 ▶ if (SvIOK(val)) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) return true; 10964 30 10994
204 30 ▶ if (SvPOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(val)) { 26 4 30
205 /* Use strtoll for fast integer parsing */
206 STRLEN len;
207 26 const char *pv = SvPV(val, len);
208 char *endp;
209 26 ▶ if (len == 0) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return false; 1 25 26
210 25 errno = 0;
211 25 (void)strtoll(pv, &endp, 10);
212 25 ▶ return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */
Branch: 3 branches
BranchTrueFalseTotal
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 24 1 25
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 4 20 24
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 4 0 4
213 }
214 4 return false;
215 3233 case TYPE_NUM:
216 3233 ▶ if (SvNIOK(val)) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (SvNIOK(val)) return true; 3225 8 3233
217 8 ▶ if (SvPOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(val)) { 6 2 8
218 /* Use strtod for fast number parsing */
219 STRLEN len;
220 6 const char *pv = SvPV(val, len);
221 char *endp;
222 6 ▶ if (len == 0) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return false; 1 5 6
223 5 errno = 0;
224 5 (void)strtod(pv, &endp);
225 5 ▶ return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */
Branch: 3 branches
BranchTrueFalseTotal
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 4 1 5
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 2 2 4
return errno == 0 && endp == pv + len && *pv != '\0'; /* # uncoverable condition right count:3 */ 2 0 2
226 }
227 2 return false;
228 15 case TYPE_BOOL:
229 /* Accept 0, 1, "", or boolean SVs */
230 15 ▶ if (SvIOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) { 9 6 15
231 9 IV iv = SvIV(val);
232 9 ▶ return iv == 0 || iv == 1;
Branch: 2 branches
BranchTrueFalseTotal
return iv == 0 || iv == 1; 4 5 9
return iv == 0 || iv == 1; 3 1 4
233 }
234 6 ▶ return SvTRUE(val) || !SvOK(val) || (SvPOK(val) && SvCUR(val) == 0);
Branch: 4 branches
BranchTrueFalseTotal
return SvTRUE(val) || !SvOK(val) || (SvPOK(val) && SvCUR(val) == 0); 4 2 6
return SvTRUE(val) || !SvOK(val) || (SvPOK(val) && SvCUR(val) == 0); 3 1 4
return SvTRUE(val) || !SvOK(val) || (SvPOK(val) && SvCUR(val) == 0); 2 1 3
return SvTRUE(val) || !SvOK(val) || (SvPOK(val) && SvCUR(val) == 0); 1 1 2
235 3007 case TYPE_ARRAYREF:
236 3007 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV; 3006 1 3007
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV; 3005 1 3006
237 2008 case TYPE_HASHREF:
238 2008 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV; 2007 1 2008
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV; 2006 1 2007
239 4 case TYPE_CODEREF:
240 4 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV; 3 1 4
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV; 2 1 3
241 13 case TYPE_OBJECT:
242 13 ▶ return SvROK(val) && sv_isobject(val);
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && sv_isobject(val); 11 2 13
return SvROK(val) && sv_isobject(val); 8 3 11
243 0 default: /* # uncoverable statement */
244 0 return true; /* No check or unknown */ /* # uncoverable statement */
245 }
246 }
247  
248 /* Get type name for error messages */
249 10881 static const char* type_id_to_name(BuiltinTypeID type_id) {
250 /*
251 # uncoverable branch true count:11
252 # uncoverable branch true count:12 */
253 10881 switch (type_id) {
254 1 case TYPE_ANY: return "Any";
255 3 case TYPE_DEFINED: return "Defined";
256 5626 case TYPE_STR: return "Str";
257 2628 case TYPE_INT: return "Int";
258 7 case TYPE_NUM: return "Num";
259 3 case TYPE_BOOL: return "Bool";
260 2603 case TYPE_ARRAYREF: return "ArrayRef";
261 2 case TYPE_HASHREF: return "HashRef";
262 2 case TYPE_CODEREF: return "CodeRef";
263 6 case TYPE_OBJECT: return "Object";
264 0 case TYPE_CUSTOM: return "custom"; /* # uncoverable statement */
265 0 default: return "unknown"; /* # uncoverable statement */
266 }
267 }
268  
269 /* Apply coercion to a value for a slot (slot-level, C-level, and Perl-registered) */
270 62 static SV* apply_slot_coercion(pTHX_ SV *val, SlotSpec *spec) {
271 /* Slot-level coerce(callback) */
272 62 ▶ if (spec->has_coerce && spec->coerce_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce && spec->coerce_cb) { 18 44 62
if (spec->has_coerce && spec->coerce_cb) { 16 2 18
273 16 dSP;
274 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
275 16 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 16 16
276 16 PUTBACK;
277 16 call_sv(spec->coerce_cb, G_SCALAR);
278 16 SPAGAIN;
279 16 val = POPs;
280 16 PUTBACK;
281 }
282 /* C-registered type coercion with user-data (fast path) */
283 62 ▶ if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce_ex) { /* # uncoverable condition right count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce_ex) { /* # uncoverable condition right count:2 */ 44 18 62
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce_ex) { /* # uncoverable condition right count:2 */ 44 0 44
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce_ex) { /* # uncoverable condition right count:2 */ 3 41 44
284 3 val = spec->registered->coerce_ex(aTHX_ val, spec->registered->data);
285 }
286 /* C-registered type coercion (fast path) */
287 62 ▶ if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce) { /* # uncoverable condition right count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce) { /* # uncoverable condition right count:2 */ 44 18 62
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce) { /* # uncoverable condition right count:2 */ 44 0 44
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->coerce) { /* # uncoverable condition right count:2 */ 3 41 44
288 3 val = spec->registered->coerce(aTHX_ val);
289 }
290 /* Perl-registered type coercion (from register_type) */
291 62 ▶ if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->perl_coerce) { /* # uncoverable condition right count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->perl_coerce) { /* # uncoverable condition right count:2 */ 44 18 62
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->perl_coerce) { /* # uncoverable condition right count:2 */ 44 0 44
if (spec->type_id == TYPE_CUSTOM && spec->registered && spec->registered->perl_coerce) { /* # uncoverable condition right count:2 */ 4 40 44
292 4 dSP;
293 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
294 4 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 4 4
295 4 PUTBACK;
296 4 call_sv(spec->registered->perl_coerce, G_SCALAR);
297 4 SPAGAIN;
298 4 val = POPs;
299 4 PUTBACK;
300 }
301 62 return val;
302 }
303  
304 /* Check a value against a slot's type constraint (handles both C and Perl callbacks) */
305 35039 static bool check_slot_type(pTHX_ SV *val, SlotSpec *spec) {
306 /*
307 # uncoverable branch true
308 # uncoverable condition left count:1
309 # uncoverable condition left count:2 */
310 35039 ▶ if (!spec || !spec->has_type) return true;
Branch: 2 branches
BranchTrueFalseTotal
if (!spec || !spec->has_type) return true; 35039 0 35039
if (!spec || !spec->has_type) return true; 0 35039 35039
311
312 35039 ▶ if (spec->type_id != TYPE_CUSTOM) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->type_id != TYPE_CUSTOM) { 34994 45 35039
313 34994 return check_builtin_type(aTHX_ val, spec->type_id);
314 }
315
316 /*
317 # uncoverable branch true
318 # uncoverable condition left count:1 */
319 45 ▶ if (!spec->registered) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->registered) return true; 0 45 45
320
321 /* Try C function with user-data first (fast path - ~5 cycles) */
322 45 ▶ if (spec->registered->check_ex) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->check_ex) { 6 39 45
323 6 return spec->registered->check_ex(aTHX_ val, spec->registered->data);
324 }
325  
326 /* Try C function next (fast path - ~5 cycles) */
327 39 ▶ if (spec->registered->check) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->check) { 7 32 39
328 7 return spec->registered->check(aTHX_ val);
329 }
330
331 /* Fall back to Perl callback (~100 cycles) */
332 32 ▶ if (spec->registered->perl_check) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->perl_check) { 27 5 32
333 27 dSP;
334 int count;
335 27 bool result = false;
336 SV *result_sv;
337 27 ENTER;
338 27 SAVETMPS;
339 27 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 27 27
340 27 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 27 27
341 27 PUTBACK;
342 27 count = call_sv(spec->registered->perl_check, G_SCALAR);
343 27 SPAGAIN;
344 /*
345 # uncoverable branch false
346 # uncoverable condition right count:1 */
347 27 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 27 0 27
348 27 result_sv = POPs;
349 27 result = SvTRUE(result_sv);
350 }
351 27 PUTBACK;
352 27 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 27 0 27
353 27 LEAVE;
354 27 return result;
355 }
356
357 5 return true;
358 }
359  
360 /* ============================================
361 Slot spec parser: "name:Type:default(val)"
362 ============================================ */
363  
364 784 static SlotSpec* parse_slot_spec(pTHX_ const char *spec_str, STRLEN len) {
365 SlotSpec *spec;
366 784 const char *p = spec_str;
367 784 const char *end = spec_str + len;
368 const char *name_start, *name_end;
369 STRLEN name_len;
370  
371 784 Newxz(spec, 1, SlotSpec);
372  
373 /* Parse property name (before first ':') */
374 784 name_start = p;
375 3997 ▶ while (p < end && *p != ':') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ':') p++; 3716 281 3997
while (p < end && *p != ':') p++; 3213 503 3716
376 784 name_end = p;
377  
378 784 name_len = name_end - name_start;
379 784 Newx(spec->name, name_len + 1, char);
380 784 Copy(name_start, spec->name, name_len, char);
381 784 spec->name[name_len] = '\0';
382
383 /* Parse modifiers after name */
384 1622 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 839 783 1622
385 const char *mod_start;
386 const char *arg_start;
387 const char *arg_end;
388 STRLEN mod_len;
389 STRLEN arg_len;
390 int paren_depth;
391  
392 /*
393 # uncoverable branch false
394 # uncoverable condition right count:1 */
395 839 ▶ if (*p == ':') p++; /* Skip separator */
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ':') p++; /* Skip separator */ 839 0 839
396 839 ▶ if (p >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) break; 1 838 839
397  
398 838 mod_start = p;
399  
400 /* Check for function-style modifiers: default(...), trigger(...) */
401 5099 ▶ while (p < end && *p != ':' && *p != '(') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p != ':' && *p != '(') p++; 4799 300 5099
while (p < end && *p != ':' && *p != '(') p++; 4526 273 4799
while (p < end && *p != ':' && *p != '(') p++; 4261 265 4526
402  
403 838 mod_len = p - mod_start;
404  
405 838 ▶ if (p < end && *p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '(') { 538 300 838
if (p < end && *p == '(') { 265 273 538
406 /* Function-style: default(value) or trigger(&callback) */
407 265 p++;
408 265 arg_start = p;
409 265 paren_depth = 1;
410 1901 ▶ while (p < end && paren_depth > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && paren_depth > 0) { 1699 202 1901
while (p < end && paren_depth > 0) { 1636 63 1699
411 1636 ▶ if (*p == '(') paren_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '(') paren_depth++; 1 1635 1636
412 1635 ▶ else if (*p == ')') paren_depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == ')') paren_depth--; 266 1369 1635
413 1636 p++;
414 }
415 265 arg_end = p - 1; /* Before closing paren */
416 265 arg_len = arg_end - arg_start;
417
418 265 ▶ if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) { 169 96 265
if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) { 106 63 169
419 /* Parse default value */
420 106 spec->has_default = 1;
421 /* Simple default: copy as string and eval at runtime */
422 /* For now, support literal numbers and strings */
423 106 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 102 4 106
424 char *arg_copy;
425 102 Newx(arg_copy, arg_len + 1, char);
426 102 Copy(arg_start, arg_copy, arg_len, char);
427 102 arg_copy[arg_len] = '\0';
428
429 /* Try to parse as number */
430 102 ▶ if (arg_copy[0] >= '0' && arg_copy[0] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (arg_copy[0] >= '0' && arg_copy[0] <= '9') { 95 7 102
if (arg_copy[0] >= '0' && arg_copy[0] <= '9') { 58 37 95
431 116 ▶ if (strchr(arg_copy, '.')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(arg_copy, '.')) { 4 54 58
432 4 spec->default_sv = newSVnv(atof(arg_copy));
433 } else {
434 54 spec->default_sv = newSViv(atoi(arg_copy));
435 }
436 44 ▶ } else if (arg_copy[0] == '-' && arg_len > 1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (arg_copy[0] == '-' && arg_len > 1) { 4 40 44
} else if (arg_copy[0] == '-' && arg_len > 1) { 3 1 4
437 6 ▶ if (strchr(arg_copy, '.')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(arg_copy, '.')) { 1 2 3
438 1 spec->default_sv = newSVnv(atof(arg_copy));
439 } else {
440 2 spec->default_sv = newSViv(atoi(arg_copy));
441 }
442 41 ▶ } else if (arg_copy[0] == '\'' || arg_copy[0] == '"') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (arg_copy[0] == '\'' || arg_copy[0] == '"') { 40 1 41
} else if (arg_copy[0] == '\'' || arg_copy[0] == '"') { 1 39 40
443 /* String literal - strip quotes */
444 2 ▶ if (arg_len >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len >= 2) { 1 1 2
445 1 spec->default_sv = newSVpvn(arg_copy + 1, arg_len - 2);
446 } else {
447 1 spec->default_sv = newSVpvn("", 0);
448 }
449 39 ▶ } else if (strncmp(arg_copy, "undef", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "undef", 5) == 0) { 1 38 39
450 1 spec->default_sv = newSV(0);
451 38 ▶ } else if (strncmp(arg_copy, "[]", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "[]", 2) == 0) { 8 30 38
452 8 spec->default_sv = newRV_noinc((SV*)newAV());
453 30 ▶ } else if (strncmp(arg_copy, "{}", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "{}", 2) == 0) { 4 26 30
454 4 spec->default_sv = newRV_noinc((SV*)newHV());
455 } else {
456 /* Default to string */
457 26 spec->default_sv = newSVpvn(arg_copy, arg_len);
458 }
459 102 Safefree(arg_copy);
460 }
461 159 ▶ } else if (mod_len == 7 && strncmp(mod_start, "trigger", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "trigger", 7) == 0) { 63 96 159
} else if (mod_len == 7 && strncmp(mod_start, "trigger", 7) == 0) { 18 45 63
462 /* trigger(&callback) - store callback name for later resolution */
463 18 spec->has_trigger = 1;
464 /* Note: callback resolution happens at runtime in Perl layer */
465 /* For now, store as string - will be resolved in object.pm */
466 18 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 13 5 18
467 char *cb_copy;
468 13 Newx(cb_copy, arg_len + 1, char);
469 13 Copy(arg_start, cb_copy, arg_len, char);
470 13 cb_copy[arg_len] = '\0';
471 /* Store as SV for later resolution */
472 13 spec->trigger_cb = newSVpvn(cb_copy, arg_len);
473 13 Safefree(cb_copy);
474 }
475 141 ▶ } else if (mod_len == 6 && strncmp(mod_start, "coerce", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 6 && strncmp(mod_start, "coerce", 6) == 0) { 73 68 141
} else if (mod_len == 6 && strncmp(mod_start, "coerce", 6) == 0) { 15 58 73
476 /* coerce(&callback) */
477 15 spec->has_coerce = 1;
478 15 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 13 2 15
479 char *cb_copy;
480 13 Newx(cb_copy, arg_len + 1, char);
481 13 Copy(arg_start, cb_copy, arg_len, char);
482 13 cb_copy[arg_len] = '\0';
483 13 spec->coerce_cb = newSVpvn(cb_copy, arg_len);
484 13 Safefree(cb_copy);
485 }
486 126 ▶ } else if (mod_len == 7 && strncmp(mod_start, "builder", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "builder", 7) == 0) { 45 81 126
} else if (mod_len == 7 && strncmp(mod_start, "builder", 7) == 0) { 37 8 45
487 /* builder(method_name) - builder method, called at new() unless :lazy */
488 37 spec->has_builder = 1;
489 37 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 35 2 37
490 char *cb_copy;
491 35 Newx(cb_copy, arg_len + 1, char);
492 35 Copy(arg_start, cb_copy, arg_len, char);
493 35 cb_copy[arg_len] = '\0';
494 35 spec->builder_name = newSVpvn(cb_copy, arg_len);
495 35 Safefree(cb_copy);
496 } else {
497 /* Default builder name: _build_<property> */
498 char build_name[256];
499 2 snprintf(build_name, sizeof(build_name), "_build_%s", spec->name);
500 2 spec->builder_name = newSVpv(build_name, 0);
501 }
502 89 ▶ } else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 8 81 89
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 7 1 8
503 /* clearer(method_name) - custom clearer method name */
504 7 spec->has_clearer = 1;
505 7 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 6 1 7
506 char *name_copy;
507 6 Newx(name_copy, arg_len + 1, char);
508 6 Copy(arg_start, name_copy, arg_len, char);
509 6 name_copy[arg_len] = '\0';
510 6 spec->clearer_name = newSVpvn(name_copy, arg_len);
511 6 Safefree(name_copy);
512 }
513 82 ▶ } else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 8 74 82
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 7 1 8
514 /* predicate(method_name) - custom predicate method name */
515 7 spec->has_predicate = 1;
516 7 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 6 1 7
517 char *name_copy;
518 6 Newx(name_copy, arg_len + 1, char);
519 6 Copy(arg_start, name_copy, arg_len, char);
520 6 name_copy[arg_len] = '\0';
521 6 spec->predicate_name = newSVpvn(name_copy, arg_len);
522 6 Safefree(name_copy);
523 }
524 75 ▶ } else if (mod_len == 6 && strncmp(mod_start, "reader", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 6 && strncmp(mod_start, "reader", 6) == 0) { 58 17 75
} else if (mod_len == 6 && strncmp(mod_start, "reader", 6) == 0) { 28 30 58
525 /* reader(method_name) - custom getter method name */
526 28 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 27 1 28
527 char *name_copy;
528 27 Newx(name_copy, arg_len + 1, char);
529 27 Copy(arg_start, name_copy, arg_len, char);
530 27 name_copy[arg_len] = '\0';
531 27 spec->reader_name = newSVpvn(name_copy, arg_len);
532 27 Safefree(name_copy);
533 }
534 47 ▶ } else if (mod_len == 6 && strncmp(mod_start, "writer", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 6 && strncmp(mod_start, "writer", 6) == 0) { 30 17 47
} else if (mod_len == 6 && strncmp(mod_start, "writer", 6) == 0) { 29 1 30
535 /* writer(method_name) - custom setter method name */
536 29 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 28 1 29
537 char *name_copy;
538 28 Newx(name_copy, arg_len + 1, char);
539 28 Copy(arg_start, name_copy, arg_len, char);
540 28 name_copy[arg_len] = '\0';
541 28 spec->writer_name = newSVpvn(name_copy, arg_len);
542 28 Safefree(name_copy);
543 }
544 18 ▶ } else if (mod_len == 3 && strncmp(mod_start, "arg", 3) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 3 && strncmp(mod_start, "arg", 3) == 0) { 15 3 18
} else if (mod_len == 3 && strncmp(mod_start, "arg", 3) == 0) { 14 1 15
545 /* arg(init_arg_name) - alternate constructor argument name */
546 14 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 13 1 14
547 char *name_copy;
548 13 Newx(name_copy, arg_len + 1, char);
549 13 Copy(arg_start, name_copy, arg_len, char);
550 13 name_copy[arg_len] = '\0';
551 13 spec->init_arg = newSVpvn(name_copy, arg_len);
552 13 Safefree(name_copy);
553 }
554 }
555 } else {
556 /* Simple modifier: type name or flag */
557 573 ▶ if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) { 77 496 573
if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) { 42 35 77
558 42 spec->is_required = 1;
559 531 ▶ } else if (mod_len == 8 && strncmp(mod_start, "readonly", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 8 && strncmp(mod_start, "readonly", 8) == 0) { 35 496 531
} else if (mod_len == 8 && strncmp(mod_start, "readonly", 8) == 0) { 18 17 35
560 18 spec->is_readonly = 1;
561 513 ▶ } else if (mod_len == 4 && strncmp(mod_start, "lazy", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 4 && strncmp(mod_start, "lazy", 4) == 0) { 68 445 513
} else if (mod_len == 4 && strncmp(mod_start, "lazy", 4) == 0) { 41 27 68
562 41 spec->is_lazy = 1;
563 472 ▶ } else if (mod_len == 4 && strncmp(mod_start, "weak", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 4 && strncmp(mod_start, "weak", 4) == 0) { 27 445 472
} else if (mod_len == 4 && strncmp(mod_start, "weak", 4) == 0) { 15 12 27
564 15 spec->is_weak = 1;
565 457 ▶ } else if (mod_len == 7 && strncmp(mod_start, "private", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "private", 7) == 0) { 44 413 457
} else if (mod_len == 7 && strncmp(mod_start, "private", 7) == 0) { 3 41 44
566 3 spec->is_private = 1;
567 454 ▶ } else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 41 413 454
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 28 13 41
568 28 spec->has_clearer = 1;
569 /* Default clearer name: clear_<property> */
570 426 ▶ } else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 16 410 426
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 15 1 16
571 15 spec->has_predicate = 1;
572 /* Default predicate name: has_<property> */
573 } else {
574 /* Try as type name */
575 char *type_copy;
576 BuiltinTypeID type_id;
577  
578 411 Newx(type_copy, mod_len + 1, char);
579 411 Copy(mod_start, type_copy, mod_len, char);
580 411 type_copy[mod_len] = '\0';
581  
582 411 type_id = parse_builtin_type(type_copy, mod_len);
583 411 ▶ if (type_id != TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (type_id != TYPE_NONE) { 389 22 411
584 389 spec->type_id = type_id;
585 389 spec->has_type = 1;
586 } else {
587 /* Check type registry for custom types */
588 /*
589 # uncoverable branch false
590 # uncoverable condition right count:1 */
591 22 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 22 0 22
592 22 SV **svp = hv_fetch(g_type_registry, type_copy, mod_len, 0);
593 22 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 19 3 22
594 19 spec->registered = INT2PTR(RegisteredType*, SvIV(*svp));
595 19 spec->type_id = TYPE_CUSTOM;
596 19 spec->has_type = 1;
597 }
598 }
599 }
600 411 Safefree(type_copy);
601 }
602 }
603 }
604
605 784 spec->has_checks = spec->is_readonly | spec->is_required | spec->has_coerce
606 784 | (spec->type_id == TYPE_CUSTOM ? 1 : 0);
607  
608 784 return spec;
609 }
610  
611 /* Magic vtable for object flags */
612 static MGVTBL object_magic_vtbl = {
613 NULL, /* get */
614 NULL, /* set */
615 NULL, /* len */
616 NULL, /* clear */
617 NULL, /* free */
618 NULL, /* copy */
619 NULL, /* dup */
620 NULL /* local */
621 };
622  
623 /* Validate that an SV is a blessed array-backed object */
624 #define VALIDATE_OBJECT(sv, funcname) \
625 if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV || !SvOBJECT(SvRV(sv))) \
626 croak("%s: argument is not an Object::Proto object", funcname)
627  
628 /* Get object magic (for flags) */
629 43439 static MAGIC* get_object_magic(pTHX_ SV *obj) {
630 MAGIC *mg;
631 /*
632 # uncoverable branch true
633 # uncoverable condition left count:1 */
634 43439 ▶ if (!SvROK(obj)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) return NULL; 0 43439 43439
635 43439 mg = mg_find(SvRV(obj), PERL_MAGIC_ext);
636 43441 ▶ while (mg) {
Branch: 1 branch
BranchTrueFalseTotal
while (mg) { 6241 37200 43441
637 6241 ▶ if (mg->mg_virtual == &object_magic_vtbl) return mg;
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &object_magic_vtbl) return mg; 6239 2 6241
638 2 mg = mg->mg_moremagic;
639 }
640 37200 return NULL;
641 }
642  
643 /* A slot holding the shared undef is empty: before perl 5.20 av_fill put
644 &PL_sv_undef in new slots rather than NULL, and it is read-only. */
645 8287 static int slot_is_writable(SV *slot) {
646 8287 ▶ return slot && slot != &PL_sv_undef; /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
return slot && slot != &PL_sv_undef; /* # uncoverable condition right count:2 */ 8276 11 8287
return slot && slot != &PL_sv_undef; /* # uncoverable condition right count:2 */ 8276 0 8276
647 }
648  
649 /* Add object magic */
650 26 static MAGIC* add_object_magic(pTHX_ SV *obj) {
651 MAGIC *mg;
652 26 SV *rv = SvRV(obj);
653 26 mg = sv_magicext(rv, NULL, PERL_MAGIC_ext, &object_magic_vtbl, NULL, 0);
654 26 mg->mg_private = 0; /* flags */
655 26 return mg;
656 }
657  
658 /* ============================================
659 Class definition and registration
660 ============================================ */
661  
662 /* Clone a SlotSpec (deep copy for inheritance) */
663 128 static SlotSpec* clone_slot_spec(pTHX_ const SlotSpec *src) {
664 SlotSpec *dst;
665 STRLEN name_len;
666  
667 128 Newxz(dst, 1, SlotSpec);
668  
669 128 name_len = strlen(src->name);
670 128 Newx(dst->name, name_len + 1, char);
671 128 Copy(src->name, dst->name, name_len + 1, char);
672  
673 128 dst->type_id = src->type_id;
674 128 dst->registered = src->registered;
675 128 dst->is_required = src->is_required;
676 128 dst->is_readonly = src->is_readonly;
677 128 dst->is_lazy = src->is_lazy;
678 128 dst->is_weak = src->is_weak;
679 128 dst->is_private = src->is_private;
680 128 dst->has_default = src->has_default;
681 128 dst->has_trigger = src->has_trigger;
682 128 dst->has_coerce = src->has_coerce;
683 128 dst->has_type = src->has_type;
684 128 dst->has_builder = src->has_builder;
685 128 dst->has_clearer = src->has_clearer;
686 128 dst->has_predicate = src->has_predicate;
687  
688 128 ▶ if (src->default_sv) dst->default_sv = SvREFCNT_inc(src->default_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (src->default_sv) dst->default_sv = SvREFCNT_inc(src->default_sv); 51 77 128
689 128 ▶ if (src->trigger_cb) dst->trigger_cb = SvREFCNT_inc(src->trigger_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (src->trigger_cb) dst->trigger_cb = SvREFCNT_inc(src->trigger_cb); 5 123 128
690 128 ▶ if (src->coerce_cb) dst->coerce_cb = SvREFCNT_inc(src->coerce_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (src->coerce_cb) dst->coerce_cb = SvREFCNT_inc(src->coerce_cb); 5 123 128
691 128 ▶ if (src->builder_name) dst->builder_name = SvREFCNT_inc(src->builder_name);
Branch: 1 branch
BranchTrueFalseTotal
if (src->builder_name) dst->builder_name = SvREFCNT_inc(src->builder_name); 6 122 128
692 128 ▶ if (src->clearer_name) dst->clearer_name = SvREFCNT_inc(src->clearer_name);
Branch: 1 branch
BranchTrueFalseTotal
if (src->clearer_name) dst->clearer_name = SvREFCNT_inc(src->clearer_name); 2 126 128
693 128 ▶ if (src->predicate_name) dst->predicate_name = SvREFCNT_inc(src->predicate_name);
Branch: 1 branch
BranchTrueFalseTotal
if (src->predicate_name) dst->predicate_name = SvREFCNT_inc(src->predicate_name); 2 126 128
694 128 ▶ if (src->reader_name) dst->reader_name = SvREFCNT_inc(src->reader_name);
Branch: 1 branch
BranchTrueFalseTotal
if (src->reader_name) dst->reader_name = SvREFCNT_inc(src->reader_name); 3 125 128
695 128 ▶ if (src->writer_name) dst->writer_name = SvREFCNT_inc(src->writer_name);
Branch: 1 branch
BranchTrueFalseTotal
if (src->writer_name) dst->writer_name = SvREFCNT_inc(src->writer_name); 3 125 128
696 128 ▶ if (src->init_arg) dst->init_arg = SvREFCNT_inc(src->init_arg);
Branch: 1 branch
BranchTrueFalseTotal
if (src->init_arg) dst->init_arg = SvREFCNT_inc(src->init_arg); 3 125 128
697  
698 128 return dst;
699 }
700  
701 /* Merge child override modifiers onto a clone of parent spec.
702 * Only fields that are explicitly set in override are applied.
703 * This enables Moo/Moose-style '+attr' syntax for inheritance.
704 * Called at define-time only - zero runtime overhead. */
705 28 static SlotSpec* merge_slot_spec(pTHX_ const SlotSpec *parent, const SlotSpec *override) {
706 28 SlotSpec *merged = clone_slot_spec(aTHX_ parent);
707
708 /* Override type if specified */
709 28 ▶ if (override->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_type) { 2 26 28
710 2 merged->type_id = override->type_id;
711 2 merged->registered = override->registered;
712 2 merged->has_type = 1;
713 }
714
715 /* Override default if specified */
716 28 ▶ if (override->has_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_default) { 10 18 28
717 10 ▶ if (merged->default_sv) SvREFCNT_dec(merged->default_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->default_sv) SvREFCNT_dec(merged->default_sv); 9 1 10
718 10 ▶ merged->default_sv = override->default_sv ? SvREFCNT_inc(override->default_sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
merged->default_sv = override->default_sv ? SvREFCNT_inc(override->default_sv) : NULL; 9 1 10
719 10 merged->has_default = 1;
720 }
721
722 /* Override trigger if specified */
723 28 ▶ if (override->has_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_trigger) { 3 25 28
724 3 ▶ if (merged->trigger_cb) SvREFCNT_dec(merged->trigger_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->trigger_cb) SvREFCNT_dec(merged->trigger_cb); 2 1 3
725 3 ▶ merged->trigger_cb = override->trigger_cb ? SvREFCNT_inc(override->trigger_cb) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
merged->trigger_cb = override->trigger_cb ? SvREFCNT_inc(override->trigger_cb) : NULL; 2 1 3
726 3 merged->has_trigger = 1;
727 }
728
729 /* Override coerce if specified */
730 28 ▶ if (override->has_coerce) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_coerce) { 3 25 28
731 3 ▶ if (merged->coerce_cb) SvREFCNT_dec(merged->coerce_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->coerce_cb) SvREFCNT_dec(merged->coerce_cb); 2 1 3
732 3 ▶ merged->coerce_cb = override->coerce_cb ? SvREFCNT_inc(override->coerce_cb) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
merged->coerce_cb = override->coerce_cb ? SvREFCNT_inc(override->coerce_cb) : NULL; 2 1 3
733 3 merged->has_coerce = 1;
734 }
735
736 /* Override builder if specified */
737 28 ▶ if (override->has_builder) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_builder) { 2 26 28
738 2 ▶ if (merged->builder_name) SvREFCNT_dec(merged->builder_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->builder_name) SvREFCNT_dec(merged->builder_name); 1 1 2
739 2 ▶ merged->builder_name = override->builder_name ? SvREFCNT_inc(override->builder_name) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
merged->builder_name = override->builder_name ? SvREFCNT_inc(override->builder_name) : NULL; /* # uncoverable condition right count:1 */ 2 0 2
740 2 merged->has_builder = 1;
741 }
742
743 /* Boolean flags - only set if explicitly enabled in override */
744 28 ▶ if (override->is_required) merged->is_required = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_required) merged->is_required = 1; 1 27 28
745 28 ▶ if (override->is_readonly) merged->is_readonly = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_readonly) merged->is_readonly = 1; 1 27 28
746 28 ▶ if (override->is_lazy) merged->is_lazy = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_lazy) merged->is_lazy = 1; 1 27 28
747 28 ▶ if (override->is_weak) merged->is_weak = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_weak) merged->is_weak = 1; 1 27 28
748 28 ▶ if (override->is_private) merged->is_private = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_private) merged->is_private = 1; 1 27 28
749
750 /* Clearer/predicate */
751 28 ▶ if (override->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_clearer) { 3 25 28
752 3 merged->has_clearer = 1;
753 3 ▶ if (override->clearer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->clearer_name) { 2 1 3
754 2 ▶ if (merged->clearer_name) SvREFCNT_dec(merged->clearer_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->clearer_name) SvREFCNT_dec(merged->clearer_name); 1 1 2
755 2 merged->clearer_name = SvREFCNT_inc(override->clearer_name);
756 }
757 }
758 28 ▶ if (override->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_predicate) { 3 25 28
759 3 merged->has_predicate = 1;
760 3 ▶ if (override->predicate_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->predicate_name) { 2 1 3
761 2 ▶ if (merged->predicate_name) SvREFCNT_dec(merged->predicate_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->predicate_name) SvREFCNT_dec(merged->predicate_name); 1 1 2
762 2 merged->predicate_name = SvREFCNT_inc(override->predicate_name);
763 }
764 }
765
766 /* Reader/writer/init_arg */
767 28 ▶ if (override->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->reader_name) { 2 26 28
768 2 ▶ if (merged->reader_name) SvREFCNT_dec(merged->reader_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->reader_name) SvREFCNT_dec(merged->reader_name); 1 1 2
769 2 merged->reader_name = SvREFCNT_inc(override->reader_name);
770 }
771 28 ▶ if (override->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->writer_name) { 2 26 28
772 2 ▶ if (merged->writer_name) SvREFCNT_dec(merged->writer_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->writer_name) SvREFCNT_dec(merged->writer_name); 1 1 2
773 2 merged->writer_name = SvREFCNT_inc(override->writer_name);
774 }
775 28 ▶ if (override->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->init_arg) { 2 26 28
776 2 ▶ if (merged->init_arg) SvREFCNT_dec(merged->init_arg);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->init_arg) SvREFCNT_dec(merged->init_arg); 1 1 2
777 2 merged->init_arg = SvREFCNT_inc(override->init_arg);
778 }
779
780 /* Recompute has_checks flag */
781 28 merged->has_checks = merged->is_readonly | merged->is_required | merged->has_coerce
782 28 | (merged->type_id == TYPE_CUSTOM ? 1 : 0);
783
784 28 return merged;
785 }
786  
787 306 static ClassMeta* create_class_meta(pTHX_ const char *class_name, STRLEN len) {
788 ClassMeta *meta;
789 306 Newxz(meta, 1, ClassMeta);
790 306 Newxz(meta->class_name, len + 1, char);
791 306 Copy(class_name, meta->class_name, len, char);
792 306 meta->class_name[len] = '\0';
793 306 meta->prop_to_idx = newHV();
794 306 meta->arg_to_idx = newHV();
795 306 meta->idx_to_prop = NULL;
796 306 meta->slot_count = 1; /* slot 0 reserved for prototype */
797 306 meta->stash = gv_stashpvn(class_name, len, GV_ADD);
798 306 return meta;
799 }
800  
801 27978 static ClassMeta* get_class_meta(pTHX_ const char *class_name, STRLEN len) {
802 SV **svp;
803 /*
804 # uncoverable branch true
805 # uncoverable condition left count:1 */
806 27978 ▶ if (!g_class_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_class_registry) return NULL; 0 27978 27978
807 27978 svp = hv_fetch(g_class_registry, class_name, len, 0);
808 27978 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 21651 6327 27978
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 21651 0 21651
809 21651 return INT2PTR(ClassMeta*, SvIV(*svp));
810 }
811 6327 return NULL;
812 }
813  
814 306 static void register_class_meta(pTHX_ const char *class_name, STRLEN len, ClassMeta *meta) {
815 /*
816 # uncoverable branch true
817 # uncoverable condition left count:1 */
818 306 ▶ if (!g_class_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_class_registry) { 0 306 306
819 0 g_class_registry = newHV(); /* # uncoverable statement */
820 }
821 306 hv_store(g_class_registry, class_name, len, newSViv(PTR2IV(meta)), 0);
822 306 }
823  
824 /* ============================================
825 Prototype chain resolution
826 ============================================ */
827  
828 #define MAX_PROTOTYPE_DEPTH 100
829  
830 /* Resolve a property through the full prototype chain.
831 * Returns the value if found, or &PL_sv_undef if not.
832 * Detects circular references using depth limit and pointer tracking.
833 */
834 45655 static SV* resolve_property_chain(pTHX_ AV *av, IV idx) {
835 45655 int depth = 0;
836 AV *visited[MAX_PROTOTYPE_DEPTH]; /* Simple stack-based cycle detection */
837 int i;
838  
839 45785 ▶ while (av && depth < MAX_PROTOTYPE_DEPTH) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
while (av && depth < MAX_PROTOTYPE_DEPTH) { /* # uncoverable condition right count:1 */ 45785 0 45785
while (av && depth < MAX_PROTOTYPE_DEPTH) { /* # uncoverable condition right count:1 */ 45784 1 45785
840 /* Check for circular reference */
841 50772 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 4989 45783 50772
842 4989 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 1 4988 4989
843 1 warn("Circular prototype reference detected");
844 1 return &PL_sv_undef;
845 }
846 }
847 45783 visited[depth] = av;
848  
849 /* Try to fetch the property at this level */
850 45783 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 45781 2 45783
851 45781 SV *slot = AvARRAY(av)[idx];
852 45781 ▶ if (slot && SvOK(slot)) return slot;
Branch: 2 branches
BranchTrueFalseTotal
if (slot && SvOK(slot)) return slot; 45780 1 45781
if (slot && SvOK(slot)) return slot; 37800 7980 45780
853 }
854  
855 /* Follow prototype chain (slot 0) */
856 7983 ▶ if (AvFILLp(av) < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILLp(av) < 0) break; 1 7982 7983
857 {
858 7982 SV *proto_sv = AvARRAY(av)[0];
859 /*
860 # uncoverable branch true count:1 class:ignore_covered_err
861 # uncoverable condition left count:1 */
862 7982 ▶ if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break;
Branch: 3 branches
BranchTrueFalseTotal
if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break; 7982 0 7982
if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break; 131 7851 7982
if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break; 130 1 131
863 130 av = (AV*)SvRV(proto_sv);
864 }
865 130 depth++;
866 }
867  
868 7854 ▶ if (depth >= MAX_PROTOTYPE_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= MAX_PROTOTYPE_DEPTH) { 1 7853 7854
869 1 warn("Prototype chain too deep (max %d levels)", MAX_PROTOTYPE_DEPTH);
870 }
871  
872 7854 return &PL_sv_undef;
873 }
874  
875 /* ============================================
876 Custom OP: property accessor (get)
877 ============================================ */
878  
879 9 static OP* pp_object_get(pTHX) {
880 9 dSP;
881 9 SV *obj = TOPs;
882 9 IV idx = PL_op->op_targ;
883 AV *av;
884 SV *sv;
885  
886 9 ▶ if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 8 1 9
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 1 7 8
887 2 croak("Not an object");
888 }
889  
890 7 av = (AV*)SvRV(obj);
891  
892 /* Fast path: direct slot access (common case - no prototype chain) */
893 7 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 6 1 7
894 6 sv = AvARRAY(av)[idx];
895 6 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 5 1 6
if (sv && SvOK(sv)) { 1 4 5
896 1 SETs(sv);
897 1 RETURN;
898 }
899 }
900  
901 /* Slow path: check prototype chain only if prototype exists.
902 Slot 0 is always allocated - use direct access instead of av_fetch(). */
903 {
904 6 SV *proto_sv = AvARRAY(av)[0];
905 6 ▶ if (proto_sv && SvROK(proto_sv) && SvTYPE(SvRV(proto_sv)) == SVt_PVAV) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (proto_sv && SvROK(proto_sv) && SvTYPE(SvRV(proto_sv)) == SVt_PVAV) { /* # uncoverable condition right count:1 */ 6 0 6
if (proto_sv && SvROK(proto_sv) && SvTYPE(SvRV(proto_sv)) == SVt_PVAV) { /* # uncoverable condition right count:1 */ 3 3 6
if (proto_sv && SvROK(proto_sv) && SvTYPE(SvRV(proto_sv)) == SVt_PVAV) { /* # uncoverable condition right count:1 */ 2 1 3
906 2 SV *result = resolve_property_chain(aTHX_ av, idx);
907 2 SETs(result);
908 2 RETURN;
909 }
910 }
911  
912 4 SETs(&PL_sv_undef);
913 4 RETURN;
914 }
915  
916 /* ============================================
917 Custom OP: property accessor (set)
918 ============================================ */
919  
920 8 static OP* pp_object_set(pTHX) {
921 8 dSP;
922 8 SV *val = POPs;
923 8 SV *obj = TOPs;
924 8 IV idx = PL_op->op_targ;
925 SV *rv;
926 AV *av;
927  
928 8 rv = SvRV(obj);
929 8 ▶ if (!SvROK(obj) || SvTYPE(rv) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(rv) != SVt_PVAV) { 7 1 8
if (!SvROK(obj) || SvTYPE(rv) != SVt_PVAV) { 1 6 7
930 2 croak("Not an object");
931 }
932  
933 6 av = (AV*)rv;
934  
935 /* Only check magic if object has any (lazy magic - most objects don't) */
936 6 ▶ if (SvMAGICAL(rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(rv)) { 4 2 6
937 4 MAGIC *mg = get_object_magic(aTHX_ obj);
938 4 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 2 4
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
939 1 croak("Cannot modify frozen object");
940 }
941 }
942  
943 /* In-place update if slot already has an SV (avoids alloc/dealloc) */
944 5 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 4 1 5
945 4 SV *slot = AvARRAY(av)[idx];
946 4 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 3 1 4
947 3 sv_setsv(slot, val);
948 3 SETs(val);
949 3 RETURN;
950 }
951 }
952  
953 2 av_store(av, idx, newSVsv(val));
954 2 SETs(val);
955 2 RETURN;
956 }
957  
958 /* ============================================
959 Custom OP: property accessor (set) with type check
960 Uses op_private to store type ID for inline check
961 ============================================ */
962  
963 /* Helper struct to pass both idx and meta through op */
964 typedef struct {
965 IV slot_idx;
966 ClassMeta *meta;
967 } SlotOpData;
968  
969 /* Helper struct for function-style accessors (cross-class support) */
970 struct FuncAccessorData_s {
971 IV slot_idx;
972 ClassMeta *expected_class; /* Class this accessor expects */
973 SlotSpec *spec; /* Slot spec (for lazy support), may be NULL */
974 IV registry_id; /* ID in g_func_accessor_registry */
975 };
976  
977 /* Register a FuncAccessorData and return its ID */
978 268 static IV register_func_accessor_data(pTHX_ FuncAccessorData *data) {
979 268 ▶ if (g_func_accessor_count >= g_func_accessor_capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_func_accessor_count >= g_func_accessor_capacity) { 16 252 268
980 16 ▶ IV new_capacity = g_func_accessor_capacity ? g_func_accessor_capacity * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
IV new_capacity = g_func_accessor_capacity ? g_func_accessor_capacity * 2 : 64; 1 15 16
981 16 ▶ Renew(g_func_accessor_registry, new_capacity, FuncAccessorData*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_func_accessor_registry, new_capacity, FuncAccessorData*); 0 16 16
982 16 g_func_accessor_capacity = new_capacity;
983 }
984 268 data->registry_id = g_func_accessor_count;
985 268 g_func_accessor_registry[g_func_accessor_count] = data;
986 268 return g_func_accessor_count++;
987 }
988  
989 /* Look up FuncAccessorData by ID — inlined for hot path performance */
990 84745 OBJECT_INLINE FuncAccessorData* get_func_accessor_data(IV id) {
991 84745 return g_func_accessor_registry[id];
992 }
993  
994 23 static OP* pp_object_set_typed(pTHX) {
995 23 dSP;
996 23 SV *val = POPs;
997 23 SV *obj = TOPs;
998 23 SlotOpData *data = INT2PTR(SlotOpData*, PL_op->op_targ);
999 23 IV idx = data->slot_idx;
1000 23 ClassMeta *meta = data->meta;
1001 23 SlotSpec *spec = meta->slots[idx];
1002 AV *av;
1003  
1004 23 ▶ if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 22 1 23
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 1 21 22
1005 2 croak("Not an object");
1006 }
1007  
1008 21 av = (AV*)SvRV(obj);
1009  
1010 /* Check frozen/locked — only walk magic list if object has magic */
1011 21 ▶ if (SvMAGICAL(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(av)) { 4 17 21
1012 4 MAGIC *mg = get_object_magic(aTHX_ obj);
1013 4 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 2 4
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
1014 1 croak("Cannot modify frozen object");
1015 }
1016 }
1017  
1018 /* Fast-skip readonly/required/coerce when none apply */
1019 20 ▶ if (spec->has_checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_checks) { 7 13 20
1020 7 ▶ if (spec->is_readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_readonly) { 2 5 7
1021 2 croak("Cannot modify readonly slot '%s'", spec->name);
1022 }
1023 5 ▶ if (spec->is_required && !SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_required && !SvOK(val)) { 2 3 5
if (spec->is_required && !SvOK(val)) { 1 1 2
1024 1 croak("Cannot set required slot '%s' to undef", spec->name);
1025 }
1026 4 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) { 3 1 4
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) { 2 1 3
1027 3 val = apply_slot_coercion(aTHX_ val, spec);
1028 }
1029 }
1030  
1031 /* Type check using helper (handles both C and Perl callbacks) */
1032 17 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 13 4 17
1033 13 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 3 10 13
1034 1 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 1 0 1
1035 1 ? spec->registered->name
1036 4 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 2 3
1037 3 croak("Type constraint failed for '%s': expected %s",
1038 spec->name, type_name);
1039 }
1040 }
1041  
1042 /* Trigger callback ($self, $new_value) */
1043 14 ▶ if (spec->has_trigger && spec->trigger_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_trigger && spec->trigger_cb) { 3 11 14
if (spec->has_trigger && spec->trigger_cb) { 2 1 3
1044 2 dSP;
1045 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1046 2 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 2 2
1047 2 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 2 2
1048 2 PUTBACK;
1049 4 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
1050 }
1051  
1052 14 ▶ if (!spec->is_weak) {
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->is_weak) { 10 4 14
1053 /* In-place update avoids newSVsv allocation (common case) */
1054 10 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 9 1 10
1055 9 SV *slot = AvARRAY(av)[idx];
1056 9 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 8 1 9
1057 8 sv_setsv(slot, val);
1058 8 SETs(val);
1059 8 RETURN;
1060 }
1061 }
1062 2 av_store(av, idx, newSVsv(val));
1063 } else {
1064 4 SV *stored = newSVsv(val);
1065 4 av_store(av, idx, stored);
1066 4 ▶ if (SvROK(stored)) sv_rvweaken(stored);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(stored)) sv_rvweaken(stored); 1 3 4
1067 }
1068 6 SETs(val);
1069 6 RETURN;
1070 }
1071  
1072 /* ============================================
1073 Call checker for accessor
1074 ============================================ */
1075  
1076 5 static OP* accessor_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
1077 5 IV idx = SvIV(ckobj);
1078 OP *pushop, *cvop, *selfop, *argop;
1079 OP *newop;
1080  
1081 PERL_UNUSED_ARG(namegv);
1082  
1083 5 pushop = cUNOPx(entersubop)->op_first;
1084 /*
1085 # uncoverable branch false
1086 # uncoverable condition right count:1 */
1087 5 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 5 0 5
1088 5 pushop = cUNOPx(pushop)->op_first;
1089 }
1090  
1091 5 ▶ selfop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
selfop = OpSIBLING(pushop); 5 0 5
1092 5 cvop = selfop;
1093 5 argop = selfop;
1094 12 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 7 5 12
1095 7 argop = cvop;
1096 7 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 7 0 7
1097 }
1098  
1099 /* Check if there's an argument after self (setter call) */
1100 5 ▶ if (argop != selfop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != selfop) { 2 3 5
1101 /* Setter: $obj->name($value) */
1102 2 ▶ OP *valop = OpSIBLING(selfop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(selfop); 2 0 2
1103
1104 /* Detach self and val */
1105 2 OpMORESIB_set(pushop, cvop);
1106 2 OpLASTSIB_set(valop, NULL);
1107 2 OpLASTSIB_set(selfop, NULL);
1108
1109 /* Create binop with self and val - use OP_NULL to avoid assertion
1110 * failures on debugging Perls (5.14-5.20), then set OP_CUSTOM */
1111 2 newop = newBINOP(OP_NULL, 0, selfop, valop);
1112 2 newop->op_type = OP_CUSTOM;
1113 2 newop->op_ppaddr = pp_object_set;
1114 2 newop->op_targ = idx;
1115
1116 2 op_free(entersubop);
1117 2 return newop;
1118 } else {
1119 /* Getter: $obj->name */
1120 3 OpMORESIB_set(pushop, cvop);
1121 3 OpLASTSIB_set(selfop, NULL);
1122
1123 /* Use OP_NULL to avoid assertion failures on debugging Perls
1124 * (5.14-5.20), then set OP_CUSTOM */
1125 3 newop = newUNOP(OP_NULL, 0, selfop);
1126 3 newop->op_type = OP_CUSTOM;
1127 3 newop->op_ppaddr = pp_object_get;
1128 3 newop->op_targ = idx;
1129
1130 3 op_free(entersubop);
1131 3 return newop;
1132 }
1133 }
1134  
1135 /* ============================================
1136 XS Fallback functions
1137 ============================================ */
1138  
1139 /* XS fallback for new (when call checker can't optimize) */
1140 5318 XS_INTERNAL(xs_object_new_fallback) {
1141 5318 dXSARGS;
1142 5318 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
1143 AV *obj_av;
1144 SV *obj_sv;
1145 SV **ary;
1146 IV i;
1147 5318 IV start_arg = 0;
1148 IV arg_count;
1149 5318 IV slot_count = meta->slot_count;
1150 5318 int is_named = 0;
1151  
1152 /* Skip class name if passed as invocant (Cat->new or new Cat).
1153 * Fast path: compare stash pointer instead of string compare. */
1154 5318 ▶ if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */
Branch: 3 branches
BranchTrueFalseTotal
if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */ 5317 1 5318
if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */ 5315 2 5317
if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */ 5315 0 5315
1155 /* The class name is always the first arg for Cat->new() or new Cat() style */
1156 5315 start_arg = 1;
1157 }
1158  
1159 5318 arg_count = items - start_arg;
1160  
1161 /* Detect named pairs: even count and first arg is a known property/init_arg name,
1162 OR the class has defaults (unknown keys are dropped, defaults applied). */
1163 5318 ▶ if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) {
Branch: 3 branches
BranchTrueFalseTotal
if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) { 4122 1196 5318
if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) { 3922 200 4122
if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) { 3862 60 3922
1164 STRLEN len;
1165 3862 const char *pv = SvPV(ST(start_arg), len);
1166 3862 ▶ if (hv_exists(meta->prop_to_idx, pv, len) || hv_exists(meta->arg_to_idx, pv, len)
Branch: 2 branches
BranchTrueFalseTotal
if (hv_exists(meta->prop_to_idx, pv, len) || hv_exists(meta->arg_to_idx, pv, len) 77 3785 3862
if (hv_exists(meta->prop_to_idx, pv, len) || hv_exists(meta->arg_to_idx, pv, len) 66 11 77
1167 66 ▶ || meta->has_any_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
|| meta->has_any_defaults) { 1 65 66
1168 3797 is_named = 1;
1169 }
1170 }
1171  
1172 /* Create array with pre-extended size and get direct pointer */
1173 5318 obj_av = newAV();
1174 5318 av_extend(obj_av, slot_count - 1);
1175 /* Fill array length so AvARRAY access is safe */
1176 5318 AvFILLp(obj_av) = slot_count - 1;
1177 5318 ary = AvARRAY(obj_av);
1178  
1179 /* Slot 0 = prototype (initially undef - read-only is fine, never written via setter) */
1180 5318 ary[0] = &PL_sv_undef;
1181  
1182 /* Fill slots with defaults or writable undef in a single pass */
1183 17161 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 11843 5318 17161
1184 11843 SlotSpec *spec = meta->slots[i];
1185 11843 ▶ if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 11843 0 11843
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 2438 9405 11843
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 2422 16 2438
1186 2422 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 41 2381 2422
1187 41 ▶ if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVAV) { 34 7 41
1188 34 ary[i] = newRV_noinc((SV*)newAV());
1189 /*
1190 # uncoverable branch false
1191 # uncoverable condition right count:1 */
1192 7 ▶ } else if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVHV) { 7 0 7
1193 7 ary[i] = newRV_noinc((SV*)newHV());
1194 } else {
1195 0 ary[i] = newSVsv(spec->default_sv); /* # uncoverable statement */
1196 }
1197 } else {
1198 2381 ary[i] = newSVsv(spec->default_sv);
1199 }
1200 } else {
1201 9421 ary[i] = newSV(0);
1202 }
1203 }
1204  
1205 /* Create blessed reference NOW so triggers can do method dispatch */
1206 5318 obj_sv = newRV_noinc((SV*)obj_av);
1207 5318 sv_bless(obj_sv, meta->stash);
1208  
1209 5318 ▶ if (is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_named) { 3797 1521 5318
1210 /* Named arguments */
1211 8587 ▶ for (i = start_arg; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start_arg; i < items; i += 2) { 4805 3782 8587
1212 4805 SV *key_sv = ST(i);
1213 4805 ▶ SV *val_sv = (i + 1 < items) ? ST(i + 1) : &PL_sv_undef; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
SV *val_sv = (i + 1 < items) ? ST(i + 1) : &PL_sv_undef; /* # uncoverable condition right count:1 */ 4805 0 4805
1214 STRLEN key_len;
1215 4805 const char *key = SvPV(key_sv, key_len);
1216 4805 SV **idx_svp = hv_fetch(meta->arg_to_idx, key, key_len, 0);
1217 4805 ▶ if (idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx_svp) { 4801 4 4805
1218 4801 IV idx = SvIVX(*idx_svp);
1219 4801 SlotSpec *spec = meta->slots[idx];
1220
1221 /* Coerce + type check */
1222 4801 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 4801 0 4801
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 4701 100 4801
1223 4701 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 4698 3 4701
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 18 4680 4698
1224 21 val_sv = apply_slot_coercion(aTHX_ val_sv, spec);
1225 4701 ▶ if (!check_slot_type(aTHX_ val_sv, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val_sv, spec)) { 15 4686 4701
1226 5 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 5 0 5
1227 5 ? spec->registered->name
1228 20 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 5 10 15
1229 15 croak("Type constraint failed for '%s' in new(): expected %s",
1230 spec->name, type_name);
1231 }
1232 }
1233
1234 /* Trigger callback */
1235 4786 ▶ if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 4786 0 4786
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 7 4779 4786
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 6 1 7
1236 6 dSP;
1237 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
1238 6 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 6 6
1239 6 ▶ XPUSHs(val_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val_sv); 0 6 6
1240 6 PUTBACK;
1241 12 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
1242 }
1243
1244 4786 sv_setsv(ary[idx], val_sv);
1245 }
1246 }
1247 } else {
1248 /* Positional arguments */
1249 1521 IV provided = items - start_arg;
1250 1521 ▶ if (provided > slot_count - 1) provided = slot_count - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (provided > slot_count - 1) provided = slot_count - 1; 1 1520 1521
1251  
1252 2346 ▶ for (i = 0; i < provided; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < provided; i++) { 830 1516 2346
1253 830 IV idx = i + 1;
1254 830 SV *val_sv = ST(start_arg + i);
1255 830 SlotSpec *spec = meta->slots[idx];
1256
1257 /* Coerce + type check */
1258 830 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 830 0 830
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 335 495 830
1259 335 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 332 3 335
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 8 324 332
1260 11 val_sv = apply_slot_coercion(aTHX_ val_sv, spec);
1261 335 ▶ if (!check_slot_type(aTHX_ val_sv, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val_sv, spec)) { 5 330 335
1262 2 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 2 0 2
1263 2 ? spec->registered->name
1264 7 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 2 3 5
1265 5 croak("Type constraint failed for '%s' in new(): expected %s",
1266 spec->name, type_name);
1267 }
1268 }
1269
1270 /* Trigger callback */
1271 825 ▶ if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 825 0 825
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 4 821 825
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 3 1 4
1272 3 dSP;
1273 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
1274 3 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 3 3
1275 3 ▶ XPUSHs(val_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val_sv); 0 3 3
1276 3 PUTBACK;
1277 6 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
1278 }
1279
1280 825 sv_setsv(ary[idx], val_sv);
1281 }
1282 }
1283  
1284 /* Call builders for non-lazy builder slots that weren't set */
1285 5298 ▶ if (meta->has_any_builders) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_builders) { 3368 1930 5298
1286 10804 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 7438 3366 10804
1287 7438 SlotSpec *spec = meta->slots[i];
1288 7438 ▶ if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */
Branch: 4 branches
BranchTrueFalseTotal
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 7438 0 7438
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 3387 4051 7438
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 17 3370 3387
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 16 1 17
1289 /* Call builder method */
1290 16 dSP;
1291 IV count;
1292 16 ENTER;
1293 16 SAVETMPS;
1294 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
1295 16 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 16 16
1296 16 PUTBACK;
1297 32 count = call_method(SvPV_nolen(spec->builder_name), G_SCALAR);
1298 16 SPAGAIN;
1299 /*
1300 # uncoverable branch false
1301 # uncoverable condition right count:1 */
1302 16 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 16 0 16
1303 16 SV *built_val = POPs;
1304
1305 /* Coerce + type check the built value */
1306 16 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 14 2 16
1307 14 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 12 2 14
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 2 10 12
1308 4 built_val = apply_slot_coercion(aTHX_ built_val, spec);
1309 14 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 2 12 14
1310 1 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 1 0 1
1311 1 ? spec->registered->name
1312 3 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 1 2
1313 2 croak("Type constraint failed for '%s' in builder: expected %s",
1314 spec->name, type_name);
1315 }
1316 }
1317
1318 14 sv_setsv(ary[i], built_val);
1319 }
1320 14 PUTBACK;
1321 14 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 14 0 14
1322 14 LEAVE;
1323 }
1324 }
1325 }
1326  
1327 /* Check required slots */
1328 5296 ▶ if (meta->has_any_required) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_required) { 2303 2993 5296
1329 7256 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 4963 2293 7256
1330 4963 SlotSpec *spec = meta->slots[i];
1331 4963 ▶ if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 4963 0 4963
if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 2314 2649 4963
if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 10 2304 2314
1332 10 croak("Required slot '%s' not provided in new()", spec->name);
1333 }
1334 }
1335 }
1336  
1337 /* Weaken references if any slots have is_weak */
1338 5286 ▶ if (meta->has_any_weak) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_weak) { 137 5149 5286
1339 1115 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 978 137 1115
1340 978 SlotSpec *spec = meta->slots[i];
1341 978 ▶ if (spec && spec->is_weak && SvROK(ary[i])) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->is_weak && SvROK(ary[i])) { /* # uncoverable condition right count:1 */ 978 0 978
if (spec && spec->is_weak && SvROK(ary[i])) { /* # uncoverable condition right count:1 */ 153 825 978
if (spec && spec->is_weak && SvROK(ary[i])) { /* # uncoverable condition right count:1 */ 3 150 153
1342 3 sv_rvweaken(ary[i]);
1343 }
1344 }
1345 }
1346  
1347 /* Call BUILD if defined. Use tri-state: 0=unchecked, 1=has BUILD, 2=no BUILD.
1348 Avoids gv_fetchmeth on every construction once checked. */
1349 5286 ▶ if (meta->has_build == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_build == 0) { 256 5030 5286
1350 256 GV *gv = gv_fetchmeth(meta->stash, "BUILD", 5, 0);
1351 256 ▶ if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 12 244 256
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 12 0 12
1352 12 meta->has_build = 1;
1353 12 meta->build_cv = GvCV(gv);
1354 } else {
1355 244 meta->has_build = 2;
1356 }
1357 }
1358 5286 ▶ if (meta->has_build == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_build == 1) { 25 5261 5286
1359 25 dSP;
1360 25 ENTER;
1361 25 SAVETMPS;
1362 25 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 25 25
1363 25 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 25 25
1364 25 PUTBACK;
1365 25 call_method("BUILD", G_VOID | G_DISCARD);
1366 25 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 25 0 25
1367 25 LEAVE;
1368 }
1369  
1370 5286 ST(0) = sv_2mortal(obj_sv);
1371 5286 XSRETURN(1);
1372 }
1373  
1374 /* XS fallback accessor */
1375 256 XS_INTERNAL(xs_accessor_fallback) {
1376 256 dXSARGS;
1377 256 IV idx = CvXSUBANY(cv).any_iv;
1378 256 SV *self = ST(0);
1379 AV *av;
1380 SV *rv;
1381  
1382 256 rv = SvRV(self);
1383 256 ▶ if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) { 255 1 256
if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) { 1 254 255
1384 2 croak("Not an object");
1385 }
1386 254 av = (AV*)rv;
1387  
1388 254 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 44 210 254
1389 /* Setter */
1390 44 ▶ if (SvMAGICAL(rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(rv)) { 7 37 44
1391 7 MAGIC *mg = get_object_magic(aTHX_ self);
1392 7 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 4 3 7
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 3 1 4
1393 3 croak("Cannot modify frozen object");
1394 }
1395 }
1396 /* In-place update if slot already has an SV */
1397 41 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 40 1 41
1398 40 SV *slot = AvARRAY(av)[idx];
1399 40 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 38 2 40
1400 38 sv_setsv(slot, ST(1));
1401 38 ST(0) = ST(1);
1402 38 XSRETURN(1);
1403 }
1404 }
1405 3 av_store(av, idx, newSVsv(ST(1)));
1406 3 ST(0) = ST(1);
1407 3 XSRETURN(1);
1408 } else {
1409 /* Getter - fast path: direct slot access */
1410 210 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 208 2 210
1411 208 SV *sv = AvARRAY(av)[idx];
1412 208 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 207 1 208
if (sv && SvOK(sv)) { 177 30 207
1413 177 ST(0) = sv;
1414 177 XSRETURN(1);
1415 }
1416 }
1417 /* Slow path: check prototype chain */
1418 {
1419 33 SV **proto = av_fetch(av, 0, 0);
1420 33 ▶ if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 32 1 33
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 21 11 32
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 20 1 21
1421 20 SV *result = resolve_property_chain(aTHX_ av, idx);
1422 20 ST(0) = result;
1423 20 XSRETURN(1);
1424 }
1425 }
1426 13 ST(0) = &PL_sv_undef;
1427 13 XSRETURN(1);
1428 }
1429 }
1430  
1431 /* ============================================
1432 Install constructor into class
1433 ============================================ */
1434  
1435 306 static void install_constructor(pTHX_ const char *class_name, ClassMeta *meta) {
1436 char full_name[256];
1437 CV *cv;
1438  
1439 306 snprintf(full_name, sizeof(full_name), "%s::new", class_name);
1440
1441 /* Create a minimal CV that will be replaced by call checker */
1442 306 cv = newXS(full_name, xs_object_new_fallback, __FILE__);
1443 306 CvXSUBANY(cv).any_iv = PTR2IV(meta);
1444 306 }
1445  
1446 /* ============================================
1447 Custom OP: fast function-style getter
1448 op_targ = registry ID, reads object from stack
1449 ============================================ */
1450 static XOP object_func_get_xop;
1451 static XOP object_func_set_xop;
1452  
1453 80335 static OP* pp_object_func_get(pTHX) {
1454 80335 dSP;
1455 80335 SV *obj = TOPs; /* peek, don't pop */
1456 80335 FuncAccessorData *data = get_func_accessor_data(PL_op->op_targ);
1457 80335 IV idx = data->slot_idx;
1458 SV *rv;
1459 AV *av;
1460 SV *sv;
1461  
1462 80335 ▶ if (!SvROK(obj)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) croak("Not an object"); 1 80334 80335
1463 80334 rv = SvRV(obj);
1464 80334 ▶ if (SvTYPE(rv) != SVt_PVAV) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) != SVt_PVAV) croak("Not an object"); 2 80332 80334
1465 80332 av = (AV*)rv;
1466  
1467 /* Validate object is of expected class (stash pointer comparison) */
1468 /*
1469 # uncoverable branch true
1470 # uncoverable condition left count:1
1471 # uncoverable condition left count:2
1472 # uncoverable condition right count:2 */
1473 80332 ▶ if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) { 0 80332 80332
if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) { 0 0 0
1474 0 ▶ croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */
Branch: 6 branches
BranchTrueFalseTotal
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
1475 data->expected_class->class_name, /* # uncoverable statement */
1476 HvNAME(SvSTASH(rv))); /* # uncoverable statement */
1477 }
1478  
1479 /* Direct array access — no SvOK needed (func path has no prototype chain) */
1480 80332 sv = NULL;
1481 80332 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 80331 1 80332
1482 80331 sv = AvARRAY(av)[idx];
1483 80331 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 80330 1 80331
if (sv && SvOK(sv)) { 80321 9 80330
1484 80321 SETs(sv);
1485 80321 RETURN;
1486 }
1487 }
1488  
1489 /* Lazy initialization for func accessors */
1490 11 ▶ if (data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:1 */ 11 0 11
if (data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:1 */ 7 4 11
1491 7 SlotSpec *spec = data->spec;
1492 7 SV *built_val = NULL;
1493 9 ▶ if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 2 5 7
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 2 0 2
1494 2 const char *builder = SvPV_nolen(spec->builder_name);
1495 int count;
1496 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
1497 2 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 2 2
1498 2 PUTBACK;
1499 2 count = call_method(builder, G_SCALAR);
1500 2 SPAGAIN;
1501 /*
1502 # uncoverable branch false
1503 # uncoverable condition right count:1 */
1504 2 ▶ if (count > 0) built_val = newSVsv(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) built_val = newSVsv(POPs); 2 0 2
1505 0 else built_val = newSV(0); /* # uncoverable statement */
1506 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
1507 5 ▶ } else if (spec->has_default && spec->default_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (spec->has_default && spec->default_sv) { 4 1 5
} else if (spec->has_default && spec->default_sv) { 3 1 4
1508 3 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 1 3
1509 2 SV *inner = SvRV(spec->default_sv);
1510 2 ▶ if (SvTYPE(inner) == SVt_PVAV) built_val = newRV_noinc((SV*)newAV());
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) == SVt_PVAV) built_val = newRV_noinc((SV*)newAV()); 1 1 2
1511 /*
1512 # uncoverable branch false
1513 # uncoverable condition right count:1 */
1514 1 ▶ else if (SvTYPE(inner) == SVt_PVHV) built_val = newRV_noinc((SV*)newHV());
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(inner) == SVt_PVHV) built_val = newRV_noinc((SV*)newHV()); 1 0 1
1515 0 else built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
1516 } else {
1517 1 built_val = newSVsv(spec->default_sv);
1518 }
1519 }
1520 7 ▶ if (built_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) { 5 2 7
1521 5 av_store(av, idx, built_val);
1522 5 SETs(built_val);
1523 5 RETURN;
1524 }
1525 }
1526  
1527 6 SETs(&PL_sv_undef);
1528 6 RETURN;
1529 }
1530  
1531 4062 static OP* pp_object_func_set(pTHX) {
1532 4062 dSP;
1533 4062 SV *val = POPs; /* Pop value first */
1534 4062 SV *obj = TOPs; /* Object left on stack */
1535 4062 FuncAccessorData *data = get_func_accessor_data(PL_op->op_targ);
1536 4062 IV idx = data->slot_idx;
1537 SV *rv;
1538 AV *av;
1539  
1540 4062 ▶ if (!SvROK(obj)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) croak("Not an object"); 1 4061 4062
1541 4061 rv = SvRV(obj);
1542 4061 ▶ if (SvTYPE(rv) != SVt_PVAV) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) != SVt_PVAV) croak("Not an object"); 1 4060 4061
1543 4060 av = (AV*)rv;
1544  
1545 /* Validate object is of expected class (stash pointer comparison) */
1546 /*
1547 # uncoverable branch true
1548 # uncoverable condition left count:1
1549 # uncoverable condition left count:2
1550 # uncoverable condition right count:2 */
1551 4060 ▶ if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) { 0 4060 4060
if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) { 0 0 0
1552 0 ▶ croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */
Branch: 6 branches
BranchTrueFalseTotal
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
1553 data->expected_class->class_name, /* # uncoverable statement */
1554 HvNAME(SvSTASH(rv))); /* # uncoverable statement */
1555 }
1556  
1557 /* In-place update if slot already has an SV (avoids alloc/dealloc) */
1558 4060 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 4059 1 4060
1559 4059 SV *slot = AvARRAY(av)[idx];
1560 4059 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 4058 1 4059
1561 4058 sv_setsv(slot, val);
1562 4058 SETs(val);
1563 4058 RETURN;
1564 }
1565 }
1566  
1567 2 av_store(av, idx, newSVsv(val));
1568 2 SETs(val); /* Replace object with value */
1569 2 RETURN;
1570 }
1571  
1572 /* Custom ops from other modules that are safe to use as an accessor operand:
1573 * side-effect free, and leaving exactly one value on the stack. Matched by the
1574 * name the op was registered under, so neither dist needs to know how to build
1575 * against the other. Seeded with Confold's `<:`, whose whole purpose is to mark
1576 * a value as constant. */
1577 #define OBJECT_MAX_SIMPLE_CUSTOM 8
1578 static const char *g_simple_custom_ops[OBJECT_MAX_SIMPLE_CUSTOM] = { "confold" };
1579 static int g_simple_custom_op_count = 1;
1580  
1581 1 OBJECT_INLINE bool is_simple_custom_op(pTHX_ OP *op) {
1582 const char *name;
1583 int i;
1584  
1585 1 name = XopENTRYCUSTOM(op, xop_name);
1586 /*
1587 # uncoverable branch true
1588 # uncoverable condition left count:1 */
1589 1 ▶ if (!name) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return false; 0 1 1
1590  
1591 2 ▶ for (i = 0; i < g_simple_custom_op_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_simple_custom_op_count; i++) { 1 1 2
1592 /*
1593 # uncoverable branch true class:ignore_covered_err
1594 # uncoverable condition left count:1 class:ignore_covered_err */
1595 1 ▶ if (strEQ(name, g_simple_custom_ops[i])) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, g_simple_custom_ops[i])) return true; 0 1 1
1596 }
1597 1 return false;
1598 }
1599  
1600 /* Check if an op is "simple" (can be safely used in optimized accessor) */
1601 416 OBJECT_INLINE bool is_simple_op(pTHX_ OP *op) {
1602 /*
1603 # uncoverable branch true
1604 # uncoverable condition left count:1 */
1605 416 ▶ if (!op) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) return false; 0 416 416
1606 /* Simple ops: pad variables, constants, global variables */
1607 416 switch (op->op_type) {
1608 1 case OP_CUSTOM:
1609 1 return is_simple_custom_op(aTHX_ op);
1610 294 case OP_PADSV: /* $lexical */
1611 case OP_CONST: /* literal value */
1612 case OP_GV: /* *glob */
1613 case OP_GVSV: /* $global */
1614 case OP_AELEMFAST:/* $array[const] */
1615 #if defined(OP_AELEMFAST_LEX) && OP_AELEMFAST_LEX != OP_AELEMFAST
1616 case OP_AELEMFAST_LEX:
1617 #endif
1618 case OP_NULL: /* Often wraps simple ops */
1619 294 return true;
1620 121 default:
1621 121 return false;
1622 }
1623 }
1624  
1625 /* Call checker for function-style accessor: name($obj) or name($obj, $val) */
1626 348 static OP* func_accessor_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
1627 348 IV registry_id = SvIV(ckobj);
1628 348 FuncAccessorData *data = get_func_accessor_data(registry_id);
1629 OP *pushop, *cvop, *objop, *argop;
1630 OP *newop;
1631  
1632 PERL_UNUSED_ARG(namegv);
1633  
1634 /*
1635 # uncoverable branch true
1636 # uncoverable condition left count:1 */
1637 348 ▶ if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data) { 0 348 348
1638 0 return entersubop; /* Fallback if data not found */ /* # uncoverable statement */
1639 }
1640  
1641 348 pushop = cUNOPx(entersubop)->op_first;
1642 /*
1643 # uncoverable branch false
1644 # uncoverable condition right count:1 */
1645 348 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 348 0 348
1646 348 pushop = cUNOPx(pushop)->op_first;
1647 }
1648  
1649 /* Walk the op tree like the method-style accessor checker */
1650 348 ▶ objop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
objop = OpSIBLING(pushop); 348 0 348
1651 348 cvop = objop;
1652 348 argop = objop;
1653 780 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 432 348 780
1654 432 argop = cvop;
1655 432 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 432 0 432
1656 }
1657  
1658 /* Check if there's an argument after obj (setter call) */
1659 348 ▶ if (argop != objop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != objop) { 83 265 348
1660 /* Setter: name($obj, $val) - optimize to custom binop */
1661 83 ▶ OP *valop = OpSIBLING(objop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(objop); 83 0 83
1662  
1663 /* Only optimize if exactly 2 args and both are simple ops */
1664 165 ▶ if (valop && OpSIBLING(valop) == cvop && /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (valop && OpSIBLING(valop) == cvop && /* # uncoverable condition right count:1 */ 83 0 83
if (valop && OpSIBLING(valop) == cvop && /* # uncoverable condition right count:1 */ 83 0 83
1665 151 ▶ is_simple_op(aTHX_ objop) && is_simple_op(aTHX_ valop)) {
Branch: 1 branch
BranchTrueFalseTotal
is_simple_op(aTHX_ objop) && is_simple_op(aTHX_ valop)) { 39 30 69
1666 39 OpMORESIB_set(pushop, cvop);
1667 39 OpLASTSIB_set(valop, NULL);
1668 39 OpLASTSIB_set(objop, NULL);
1669  
1670 /* Use OP_NULL first, then set op_type to OP_CUSTOM.
1671 * This avoids assertion failures in newBINOP on older
1672 * debugging Perls (5.14-5.20) which check op class masks. */
1673 39 newop = newBINOP(OP_NULL, 0, objop, valop);
1674 39 newop->op_type = OP_CUSTOM;
1675 39 newop->op_ppaddr = pp_object_func_set;
1676 39 newop->op_targ = data->registry_id;
1677  
1678 39 op_free(entersubop);
1679 39 return newop;
1680 }
1681  
1682 /* Complex args - fall back to XS */
1683 44 return op_contextualize(entersubop, G_SCALAR);
1684 }
1685  
1686 /* Getter: name($obj) - optimize only if objop is simple */
1687 265 ▶ if (!is_simple_op(aTHX_ objop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_simple_op(aTHX_ objop)) { 79 186 265
1688 79 return entersubop;
1689 }
1690  
1691 186 OpMORESIB_set(pushop, cvop);
1692 186 OpLASTSIB_set(objop, NULL);
1693  
1694 /* Use OP_NULL first, then set op_type to OP_CUSTOM.
1695 * This avoids assertion failures in newUNOP on older
1696 * debugging Perls (5.14-5.20) which check op class masks. */
1697 186 newop = newUNOP(OP_NULL, 0, objop);
1698 186 newop->op_type = OP_CUSTOM;
1699 186 newop->op_ppaddr = pp_object_func_get;
1700 186 newop->op_targ = data->registry_id;
1701  
1702 186 op_free(entersubop);
1703 186 return newop;
1704 }
1705  
1706 /* XS fallback for function-style accessor */
1707 20375 XS_INTERNAL(xs_func_accessor_fallback) {
1708 20375 dXSARGS;
1709 20375 FuncAccessorData *data = INT2PTR(FuncAccessorData*, CvXSUBANY(cv).any_iv);
1710 20375 IV idx = data->slot_idx;
1711 20375 SV *obj = ST(0);
1712 AV *av;
1713  
1714 20375 ▶ if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 20372 3 20375
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 1 20371 20372
1715 4 croak("Not an object");
1716 }
1717 20371 av = (AV*)SvRV(obj);
1718  
1719 /* Validate object is of expected class */
1720 /*
1721 # uncoverable branch true
1722 # uncoverable condition left count:1
1723 # uncoverable condition left count:2
1724 # uncoverable condition right count:2 */
1725 20371 ▶ if (data->expected_class && SvSTASH(SvRV(obj)) != data->expected_class->stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (data->expected_class && SvSTASH(SvRV(obj)) != data->expected_class->stash) { 0 20371 20371
if (data->expected_class && SvSTASH(SvRV(obj)) != data->expected_class->stash) { 0 0 0
1726 0 ▶ croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */
Branch: 6 branches
BranchTrueFalseTotal
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
croak("Expected object of class '%s', got '%s'", /* # uncoverable statement */ 0 0 0
1727 data->expected_class->class_name, /* # uncoverable statement */
1728 HvNAME(SvSTASH(SvRV(obj)))); /* # uncoverable statement */
1729 }
1730  
1731 20371 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 4145 16226 20371
1732 /* In-place update if slot already has an SV */
1733 4145 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 4144 1 4145
1734 4144 SV *slot = AvARRAY(av)[idx];
1735 4144 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 4143 1 4144
1736 4143 sv_setsv(slot, ST(1));
1737 4143 ST(0) = ST(1);
1738 4143 XSRETURN(1);
1739 }
1740 }
1741 2 av_store(av, idx, newSVsv(ST(1)));
1742 2 ST(0) = ST(1);
1743 } else {
1744 /* Direct array access */
1745 16226 SV *sv = NULL;
1746 16226 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 16225 1 16226
1747 16225 sv = AvARRAY(av)[idx];
1748 }
1749 16233 ▶ if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */
Branch: 4 branches
BranchTrueFalseTotal
if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */ 16223 3 16226
if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */ 7 16216 16223
if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */ 10 0 10
if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */ 7 3 10
1750 /* Lazy initialization */
1751 7 SlotSpec *spec = data->spec;
1752 7 SV *built_val = NULL;
1753 8 ▶ if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 1 6 7
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 1 0 1
1754 1 dSP;
1755 1 const char *builder = SvPV_nolen(spec->builder_name);
1756 int count;
1757 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
1758 1 ▶ XPUSHs(ST(0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(0)); 0 1 1
1759 1 PUTBACK;
1760 1 count = call_method(builder, G_SCALAR);
1761 1 SPAGAIN;
1762 /*
1763 # uncoverable branch false
1764 # uncoverable condition right count:1 */
1765 1 ▶ if (count > 0) built_val = newSVsv(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) built_val = newSVsv(POPs); 1 0 1
1766 0 else built_val = newSV(0); /* # uncoverable statement */
1767 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
1768 6 ▶ } else if (spec->has_default && spec->default_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (spec->has_default && spec->default_sv) { 5 1 6
} else if (spec->has_default && spec->default_sv) { 3 2 5
1769 3 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 1 3
1770 2 SV *inner = SvRV(spec->default_sv);
1771 2 ▶ if (SvTYPE(inner) == SVt_PVAV) built_val = newRV_noinc((SV*)newAV());
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) == SVt_PVAV) built_val = newRV_noinc((SV*)newAV()); 1 1 2
1772 /*
1773 # uncoverable branch false
1774 # uncoverable condition right count:1 */
1775 1 ▶ else if (SvTYPE(inner) == SVt_PVHV) built_val = newRV_noinc((SV*)newHV());
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(inner) == SVt_PVHV) built_val = newRV_noinc((SV*)newHV()); 1 0 1
1776 0 else built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
1777 } else {
1778 1 built_val = newSVsv(spec->default_sv);
1779 }
1780 }
1781 7 ▶ if (built_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) { 4 3 7
1782 4 av_store(av, idx, built_val);
1783 4 ST(0) = built_val;
1784 } else {
1785 3 ST(0) = &PL_sv_undef;
1786 }
1787 } else {
1788 16219 ▶ ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef; 16217 2 16219
ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef; 16216 1 16217
1789 }
1790 }
1791 16228 XSRETURN(1);
1792 }
1793  
1794 /* Install function-style accessor in caller's namespace */
1795 268 static void install_func_accessor(pTHX_ const char *pkg, const char *prop_name, IV idx, ClassMeta *expected_class, int force, SlotSpec *spec) {
1796 char full_name[256];
1797 CV *cv;
1798 SV *ckobj;
1799 FuncAccessorData *data;
1800 IV registry_id;
1801  
1802 268 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, prop_name);
1803  
1804 /* Check if this accessor already exists */
1805 268 cv = get_cvn_flags(full_name, strlen(full_name), 0);
1806 268 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 44 224 268
1807 /*
1808 # uncoverable branch true
1809 # uncoverable condition left count:1 */
1810 44 ▶ if (!force) {
Branch: 1 branch
BranchTrueFalseTotal
if (!force) { 0 44 44
1811 0 return; /* Not forced: skip to preserve user-defined subs */ /* # uncoverable statement */
1812 }
1813 /* Forced (import_accessors/import_accessor): delete existing
1814 * to avoid "Subroutine redefined" warning from newXS. */
1815 {
1816 44 HV *stash = gv_stashpvn(pkg, strlen(pkg), 0);
1817 /*
1818 # uncoverable branch false
1819 # uncoverable condition right count:1 */
1820 44 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 44 0 44
1821 44 (void)hv_delete(stash, prop_name, strlen(prop_name), G_DISCARD);
1822 }
1823 }
1824 }
1825  
1826 /* Allocate data for this accessor and register it */
1827 268 Newx(data, 1, FuncAccessorData);
1828 268 data->slot_idx = idx;
1829 268 data->expected_class = expected_class; /* NULL for same-class, set for cross-class */
1830 268 data->spec = spec; /* NULL if no spec, used for lazy support */
1831 268 registry_id = register_func_accessor_data(aTHX_ data);
1832  
1833 268 cv = newXS(full_name, xs_func_accessor_fallback, __FILE__);
1834 268 CvXSUBANY(cv).any_iv = PTR2IV(data); /* XS fallback still uses pointer directly */
1835  
1836 268 ckobj = newSViv(registry_id);
1837 268 cv_set_call_checker(cv, func_accessor_call_checker, ckobj);
1838 }
1839  
1840 /* Object::Proto::import_accessors("Class", "targetpkg") - import fast accessors */
1841 42 XS_INTERNAL(xs_import_accessors) {
1842 42 dXSARGS;
1843 STRLEN class_len, pkg_len, prefix_len;
1844 const char *class_pv, *pkg_pv, *prefix_pv;
1845 ClassMeta *meta;
1846 IV i;
1847  
1848 42 ▶ if (items < 1) croak("Usage: Object::Proto::import_accessors($class [, $prefix [, $package]])");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::import_accessors($class [, $prefix [, $package]])"); 1 41 42
1849  
1850 41 class_pv = SvPV(ST(0), class_len);
1851  
1852 /* Optional prefix (alias prepended to accessor names) */
1853 41 prefix_pv = NULL;
1854 41 prefix_len = 0;
1855 41 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 24 17 41
if (items > 1 && SvOK(ST(1))) { 9 15 24
1856 9 prefix_pv = SvPV(ST(1), prefix_len);
1857 9 ▶ if (prefix_len == 0) prefix_pv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (prefix_len == 0) prefix_pv = NULL; 1 8 9
1858 }
1859  
1860 41 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 20 21 41
if (items > 2 && SvOK(ST(2))) { 19 1 20
1861 38 pkg_pv = SvPV(ST(2), pkg_len);
1862 } else {
1863 /* Default to caller's package, an undef package too */
1864 22 ▶ pkg_pv = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg_pv = CopSTASHPV(PL_curcop); 22 0 22
pkg_pv = CopSTASHPV(PL_curcop); 22 0 22
pkg_pv = CopSTASHPV(PL_curcop); 22 0 22
pkg_pv = CopSTASHPV(PL_curcop); 0 22 22
pkg_pv = CopSTASHPV(PL_curcop); 0 0 0
pkg_pv = CopSTASHPV(PL_curcop); 22 0 22
pkg_pv = CopSTASHPV(PL_curcop); 0 22 22
1865 22 pkg_len = strlen(pkg_pv);
1866 }
1867  
1868 41 meta = get_class_meta(aTHX_ class_pv, class_len);
1869 41 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 40 41
1870 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
1871 }
1872  
1873 /* Install function-style accessors for each property */
1874 284 ▶ for (i = 1; i < meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < meta->slot_count; i++) { 244 40 284
1875 /*
1876 # uncoverable branch false
1877 # uncoverable condition right count:1 */
1878 244 ▶ if (meta->idx_to_prop[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->idx_to_prop[i]) { 244 0 244
1879 const char *install_name;
1880 char prefixed_name[256];
1881 244 ▶ if (prefix_pv) {
Branch: 1 branch
BranchTrueFalseTotal
if (prefix_pv) { 132 112 244
1882 132 snprintf(prefixed_name, sizeof(prefixed_name), "%s%s",
1883 132 prefix_pv, meta->idx_to_prop[i]);
1884 132 install_name = prefixed_name;
1885 } else {
1886 112 install_name = meta->idx_to_prop[i];
1887 }
1888 /* Pass NULL for same-class (skip validation), meta for cross-class */
1889 244 install_func_accessor(aTHX_ pkg_pv, install_name, i,
1890 NULL, 1,
1891 /*
1892 # uncoverable condition right count:1
1893 # uncoverable condition right count:2 */
1894 244 ▶ (meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL); 244 0 244
(meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL); 244 0 244
1895 }
1896 }
1897  
1898 40 XSRETURN_EMPTY;
1899 }
1900  
1901 /* Object::Proto::import_accessor("Class", "prop", "alias") - import single accessor with alias */
1902 27 XS_INTERNAL(xs_import_accessor) {
1903 27 dXSARGS;
1904 STRLEN class_len, prop_len, alias_len, pkg_len;
1905 const char *class_pv, *prop_pv, *alias_pv, *pkg_pv;
1906 ClassMeta *meta;
1907 SV **idx_svp;
1908 IV idx;
1909  
1910 27 ▶ if (items < 2) croak("Usage: Object::Proto::import_accessor($class, $prop [, $alias [, $package]])");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::import_accessor($class, $prop [, $alias [, $package]])"); 1 26 27
1911  
1912 26 class_pv = SvPV(ST(0), class_len);
1913 26 ▶ prop_pv = SvPV(ST(1), prop_len);
Branch: 1 branch
BranchTrueFalseTotal
prop_pv = SvPV(ST(1), prop_len); 26 0 26
1914  
1915 /* Alias defaults to property name */
1916 26 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 24 2 26
if (items > 2 && SvOK(ST(2))) { 23 1 24
1917 46 alias_pv = SvPV(ST(2), alias_len);
1918 } else {
1919 3 alias_pv = prop_pv;
1920 }
1921  
1922 /* Package defaults to caller */
1923 26 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 9 17 26
1924 18 pkg_pv = SvPV(ST(3), pkg_len);
1925 } else {
1926 17 ▶ pkg_pv = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg_pv = CopSTASHPV(PL_curcop); 17 0 17
pkg_pv = CopSTASHPV(PL_curcop); 17 0 17
pkg_pv = CopSTASHPV(PL_curcop); 17 0 17
pkg_pv = CopSTASHPV(PL_curcop); 0 17 17
pkg_pv = CopSTASHPV(PL_curcop); 0 0 0
pkg_pv = CopSTASHPV(PL_curcop); 17 0 17
pkg_pv = CopSTASHPV(PL_curcop); 0 17 17
1927 17 pkg_len = strlen(pkg_pv);
1928 }
1929  
1930 26 meta = get_class_meta(aTHX_ class_pv, class_len);
1931 26 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 25 26
1932 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
1933 }
1934  
1935 /* Look up property index */
1936 25 idx_svp = hv_fetch(meta->prop_to_idx, prop_pv, prop_len, 0);
1937 25 ▶ if (!idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_svp) { 1 24 25
1938 1 croak("Property '%s' not defined in class '%s'", prop_pv, class_pv);
1939 }
1940 24 idx = SvIV(*idx_svp);
1941  
1942 /* Install with alias name — no class check, work with any compatible object */
1943 24 install_func_accessor(aTHX_ pkg_pv, alias_pv, idx,
1944 NULL, 1,
1945 /*
1946 # uncoverable condition right count:1
1947 # uncoverable condition right count:2 */
1948 24 ▶ (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL); 24 0 24
(meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL); 24 0 24
1949  
1950 24 XSRETURN_EMPTY;
1951 }
1952  
1953 /* Object::Proto::import() - export 'object' to caller's namespace */
1954 77 XS_INTERNAL(xs_import) {
1955 77 dXSARGS;
1956 const char *caller_pkg;
1957 SV *full_name;
1958 CV *define_cv, *before_cv, *after_cv, *around_cv;
1959 GV *gv;
1960  
1961 PERL_UNUSED_VAR(items);
1962  
1963 /* Get caller's package */
1964 77 ▶ caller_pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
caller_pkg = CopSTASHPV(PL_curcop); 77 0 77
caller_pkg = CopSTASHPV(PL_curcop); 77 0 77
caller_pkg = CopSTASHPV(PL_curcop); 77 0 77
caller_pkg = CopSTASHPV(PL_curcop); 0 77 77
caller_pkg = CopSTASHPV(PL_curcop); 0 0 0
caller_pkg = CopSTASHPV(PL_curcop); 77 0 77
caller_pkg = CopSTASHPV(PL_curcop); 0 77 77
1965  
1966 /* Get Object::Proto::define */
1967 77 define_cv = get_cv("Object::Proto::define", 0);
1968 /*
1969 # uncoverable branch true
1970 # uncoverable condition left count:1 */
1971 77 ▶ if (!define_cv) croak("Object::Proto::define not found");
Branch: 1 branch
BranchTrueFalseTotal
if (!define_cv) croak("Object::Proto::define not found"); 0 77 77
1972  
1973 /* Create fully qualified name: caller::object */
1974 77 full_name = newSVpvf("%s::object", caller_pkg);
1975  
1976 /* Export: create alias in caller's namespace */
1977 77 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
1978 77 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 76 1 77
1979 76 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)define_cv));
1980 76 GvIMPORTED_CV_on(gv);
1981 }
1982 77 GvMULTI_on(gv);
1983 77 SvREFCNT_dec(full_name);
1984  
1985 /* Export before/after/around modifiers */
1986 77 before_cv = get_cv("Object::Proto::before", 0);
1987 77 after_cv = get_cv("Object::Proto::after", 0);
1988 77 around_cv = get_cv("Object::Proto::around", 0);
1989  
1990 /*
1991 # uncoverable branch false
1992 # uncoverable condition right count:1 */
1993 77 ▶ if (before_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (before_cv) { 77 0 77
1994 77 full_name = newSVpvf("%s::before", caller_pkg);
1995 77 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
1996 77 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 76 1 77
1997 76 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)before_cv));
1998 76 GvIMPORTED_CV_on(gv);
1999 }
2000 77 GvMULTI_on(gv);
2001 77 SvREFCNT_dec(full_name);
2002 }
2003  
2004 /*
2005 # uncoverable branch false
2006 # uncoverable condition right count:1 */
2007 77 ▶ if (after_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (after_cv) { 77 0 77
2008 77 full_name = newSVpvf("%s::after", caller_pkg);
2009 77 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2010 77 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 76 1 77
2011 76 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)after_cv));
2012 76 GvIMPORTED_CV_on(gv);
2013 }
2014 77 GvMULTI_on(gv);
2015 77 SvREFCNT_dec(full_name);
2016 }
2017  
2018 /*
2019 # uncoverable branch false
2020 # uncoverable condition right count:1 */
2021 77 ▶ if (around_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (around_cv) { 77 0 77
2022 77 full_name = newSVpvf("%s::around", caller_pkg);
2023 77 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2024 77 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 76 1 77
2025 76 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)around_cv));
2026 76 GvIMPORTED_CV_on(gv);
2027 }
2028 77 GvMULTI_on(gv);
2029 77 SvREFCNT_dec(full_name);
2030 }
2031  
2032 /* Export role/requires/with */
2033 {
2034 static const char *names[] = { "role", "requires", "with" };
2035 int i;
2036 308 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 231 77 308
2037 231 CV *cv = get_cvn_flags(
2038 Perl_form(aTHX_ "Object::Proto::%s", names[i]),
2039 strlen("Object::Proto::") + strlen(names[i]), 0);
2040 /*
2041 # uncoverable branch false
2042 # uncoverable condition right count:1 */
2043 231 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 231 0 231
2044 231 full_name = newSVpvf("%s::%s", caller_pkg, names[i]);
2045 231 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2046 231 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 228 3 231
2047 228 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)cv));
2048 228 GvIMPORTED_CV_on(gv);
2049 }
2050 231 GvMULTI_on(gv);
2051 231 SvREFCNT_dec(full_name);
2052 }
2053 }
2054 }
2055  
2056 77 XSRETURN_EMPTY;
2057 }
2058  
2059 /* ============================================
2060 Install accessor into class
2061 ============================================ */
2062  
2063 351 static void install_accessor(pTHX_ const char *class_name, const char *prop_name, IV idx) {
2064 char full_name[256];
2065 CV *cv;
2066 SV *ckobj;
2067  
2068 351 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, prop_name);
2069  
2070 /* Check if accessor already exists to avoid redefinition warnings */
2071 351 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2072 351 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 8 343 351
2073 8 return; /* Already defined, skip */
2074 }
2075  
2076 343 cv = newXS(full_name, xs_accessor_fallback, __FILE__);
2077 343 CvXSUBANY(cv).any_iv = idx;
2078  
2079 343 ckobj = newSViv(idx);
2080 343 cv_set_call_checker(cv, accessor_call_checker, ckobj);
2081 }
2082  
2083 /* XS fallback accessor with type checking */
2084 /* Enforce a :private attribute - its accessor may only be called from code
2085 * compiled in the owning package. In an XSUB (and in a custom op) PL_curcop is
2086 * the caller's cop, so its stash names the calling package. */
2087 71784 static void object_check_private(pTHX_ SlotSpec *spec, ClassMeta *meta) {
2088 HV *stash;
2089 const char *name;
2090 const char *caller;
2091 71784 ▶ if (!spec->is_private) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->is_private) return; 71772 12 71784
2092 12 ▶ stash = PL_curcop ? CopSTASH(PL_curcop) : NULL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
stash = PL_curcop ? CopSTASH(PL_curcop) : NULL; /* # uncoverable condition right count:1 */ 12 0 12
2093 /*
2094 # uncoverable condition right count:1
2095 # uncoverable condition left count:2 */
2096 12 ▶ name = stash ? HvNAME(stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
name = stash ? HvNAME(stash) : NULL; 12 0 12
name = stash ? HvNAME(stash) : NULL; 12 0 12
name = stash ? HvNAME(stash) : NULL; 12 0 12
name = stash ? HvNAME(stash) : NULL; 0 12 12
name = stash ? HvNAME(stash) : NULL; 0 0 0
name = stash ? HvNAME(stash) : NULL; 12 0 12
name = stash ? HvNAME(stash) : NULL; 0 12 12
2097 12 ▶ caller = name ? name : ""; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
caller = name ? name : ""; /* # uncoverable condition right count:1 */ 12 0 12
2098 12 ▶ if (strEQ(caller, meta->class_name)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(caller, meta->class_name)) return; 6 6 12
2099 6 croak("cannot call private attribute %s on %s from %s",
2100 spec->name, meta->class_name, caller);
2101 }
2102  
2103 71784 XS_INTERNAL(xs_accessor_typed_fallback) {
2104 71784 dXSARGS;
2105 71784 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2106 71784 IV idx = data->slot_idx;
2107 71784 ClassMeta *meta = data->meta;
2108 71784 SlotSpec *spec = meta->slots[idx];
2109 71784 SV *self = ST(0);
2110 AV *av;
2111  
2112 71784 object_check_private(aTHX_ spec, meta);
2113  
2114 71778 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 71777 1 71778
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 71776 71777
2115 2 croak("Not an object");
2116 }
2117 71776 av = (AV*)SvRV(self);
2118  
2119 71776 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 26144 45632 71776
2120 /* Setter with type check */
2121 26144 SV *val = ST(1);
2122 26144 MAGIC *mg = get_object_magic(aTHX_ self);
2123 26144 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 26142 26144
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
2124 1 croak("Cannot modify frozen object");
2125 }
2126
2127 26143 ▶ if (spec->is_readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_readonly) { 9 26134 26143
2128 9 croak("Cannot modify readonly slot '%s'", spec->name);
2129 }
2130
2131 /* Required fields cannot be set to undef */
2132 26134 ▶ if (spec->is_required && !SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_required && !SvOK(val)) { 1009 25125 26134
if (spec->is_required && !SvOK(val)) { 2 1007 1009
2133 2 croak("Cannot set required slot '%s' to undef", spec->name);
2134 }
2135
2136 /* Coercion */
2137 26132 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 26125 7 26132
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 9 26116 26125
2138 16 val = apply_slot_coercion(aTHX_ val, spec);
2139  
2140 /* Type check */
2141 26132 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 26121 11 26132
2142 26121 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 39 26082 26121
2143 3 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 3 0 3
2144 3 ? spec->registered->name
2145 42 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 3 36 39
2146 39 croak("Type constraint failed for '%s': expected %s",
2147 spec->name, type_name);
2148 }
2149 }
2150  
2151 /* Trigger callback ($self, $new_value) */
2152 26093 ▶ if (spec->has_trigger && spec->trigger_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_trigger && spec->trigger_cb) { 6 26087 26093
if (spec->has_trigger && spec->trigger_cb) { 4 2 6
2153 4 dSP;
2154 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
2155 4 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 4 4
2156 4 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 4 4
2157 4 PUTBACK;
2158 8 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
2159 }
2160
2161 {
2162 26093 SV *stored = newSVsv(val);
2163 26093 av_store(av, idx, stored);
2164 /* Weaken reference if is_weak flag is set */
2165 26093 ▶ if (spec->is_weak && SvROK(stored)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_weak && SvROK(stored)) { 4 26089 26093
if (spec->is_weak && SvROK(stored)) { 3 1 4
2166 3 sv_rvweaken(stored);
2167 }
2168 }
2169 26093 ST(0) = val;
2170 26093 XSRETURN(1);
2171 } else {
2172 /* Getter - use prototype chain resolution, handle lazy */
2173 45632 SV *result = resolve_property_chain(aTHX_ av, idx);
2174
2175 /* Lazy initialization: if undef and is_lazy, build/default on first access */
2176 45632 ▶ if (spec->is_lazy && !SvOK(result)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_lazy && !SvOK(result)) { 7436 38196 45632
if (spec->is_lazy && !SvOK(result)) { 3833 3603 7436
2177 3833 SV *built_val = NULL;
2178
2179 7659 ▶ if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 3826 7 3833
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 3826 0 3826
2180 /* Call builder method */
2181 3826 dSP;
2182 3826 const char *builder = SvPV_nolen(spec->builder_name);
2183 int count;
2184
2185 3826 ENTER;
2186 3826 SAVETMPS;
2187 3826 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3826 3826
2188 3826 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 3826 3826
2189 3826 PUTBACK;
2190
2191 3826 count = call_method(builder, G_SCALAR);
2192
2193 3826 SPAGAIN;
2194 /*
2195 # uncoverable branch false
2196 # uncoverable condition right count:1 */
2197 3826 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 3826 0 3826
2198 /* Copy the value BEFORE FREETMPS to avoid freed scalar issue */
2199 3826 built_val = newSVsv(POPs);
2200 } else {
2201 0 built_val = newSV(0); /* undef */ /* # uncoverable statement */
2202 }
2203 3826 PUTBACK;
2204 3826 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3826 0 3826
2205 3826 LEAVE;
2206 7 ▶ } else if (spec->has_default && spec->default_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (spec->has_default && spec->default_sv) { 6 1 7
} else if (spec->has_default && spec->default_sv) { 5 1 6
2207 /* Use default value for lazy default */
2208 5 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 3 5
2209 /* Clone reference types (arrays, hashes) */
2210 2 SV *inner = SvRV(spec->default_sv);
2211 2 ▶ if (SvTYPE(inner) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) == SVt_PVAV) { 1 1 2
2212 1 built_val = newRV_noinc((SV*)newAV());
2213 /*
2214 # uncoverable branch false
2215 # uncoverable condition right count:1 */
2216 1 ▶ } else if (SvTYPE(inner) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(inner) == SVt_PVHV) { 1 0 1
2217 1 built_val = newRV_noinc((SV*)newHV());
2218 } else {
2219 0 built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
2220 }
2221 } else {
2222 3 built_val = newSVsv(spec->default_sv);
2223 }
2224 }
2225
2226 3833 ▶ if (built_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) { 3831 2 3833
2227 /* Type check the built value */
2228 3831 ▶ if (spec->has_type && SvOK(built_val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_type && SvOK(built_val)) { 3830 1 3831
if (spec->has_type && SvOK(built_val)) { 3829 1 3830
2229 3829 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 2 3827 3829
2230 1 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 1 0 1
2231 1 ? spec->registered->name
2232 3 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 1 2
2233 2 croak("Type constraint failed for lazy '%s': expected %s",
2234 spec->name, type_name);
2235 }
2236 }
2237
2238 /* Store the built value - built_val already has correct refcount from newSVsv */
2239 3829 av_store(av, idx, built_val);
2240 3829 result = built_val;
2241 }
2242 }
2243
2244 45630 ST(0) = result;
2245 45630 XSRETURN(1);
2246 }
2247 }
2248  
2249 /* Call checker for typed accessor */
2250 19 static OP* accessor_typed_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2251 19 SlotOpData *data = INT2PTR(SlotOpData*, SvIV(ckobj));
2252 19 IV idx = data->slot_idx;
2253 OP *pushop, *cvop, *selfop, *argop;
2254 OP *newop;
2255  
2256 PERL_UNUSED_ARG(namegv);
2257  
2258 /* Private slots keep the XSUB (which enforces the caller check) rather than
2259 * being inlined to a custom op that would bypass it. */
2260 19 ▶ if (data->meta->slots[idx]->is_private) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (data->meta->slots[idx]->is_private) return entersubop; 3 16 19
2261  
2262 16 pushop = cUNOPx(entersubop)->op_first;
2263 /*
2264 # uncoverable branch false
2265 # uncoverable condition right count:1 */
2266 16 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 16 0 16
2267 16 pushop = cUNOPx(pushop)->op_first;
2268 }
2269  
2270 16 ▶ selfop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
selfop = OpSIBLING(pushop); 16 0 16
2271 16 cvop = selfop;
2272 16 argop = selfop;
2273 47 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 31 16 47
2274 31 argop = cvop;
2275 31 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 31 0 31
2276 }
2277  
2278 /* Check if there's an argument after self (setter call) */
2279 16 ▶ if (argop != selfop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != selfop) { 15 1 16
2280 /* Setter: $obj->name($value) - use typed setter */
2281 15 ▶ OP *valop = OpSIBLING(selfop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(selfop); 15 0 15
2282 15 SlotSpec *spec = data->meta->slots[idx];
2283  
2284 /* When the value is already known at compile time, the type check can
2285 * be settled here instead of on every call. That is only equivalent if
2286 * a type check is the sole thing the typed setter would have done:
2287 * pp_object_set handles the frozen check and the store, but not
2288 * readonly, required, coercion, triggers or weak refs. A value that
2289 * fails falls through to the runtime path, so a type error is still
2290 * reported where it always was. */
2291 15 ▶ if (valop->op_type == OP_CONST
Branch: 1 branch
BranchTrueFalseTotal
if (valop->op_type == OP_CONST 6 9 15
2292 6 ▶ && spec->has_type
Branch: 1 branch
BranchTrueFalseTotal
&& spec->has_type 5 1 6
2293 5 ▶ && !spec->has_checks
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->has_checks 4 1 5
2294 4 ▶ && !spec->has_trigger
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->has_trigger 3 1 4
2295 3 ▶ && !spec->is_weak
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->is_weak 2 1 3
2296 2 ▶ && check_slot_type(aTHX_ cSVOPx_sv(valop), spec))
Branch: 1 branch
BranchTrueFalseTotal
&& check_slot_type(aTHX_ cSVOPx_sv(valop), spec)) 1 1 2
2297 {
2298 1 OpMORESIB_set(pushop, cvop);
2299 1 OpLASTSIB_set(valop, NULL);
2300 1 OpLASTSIB_set(selfop, NULL);
2301  
2302 1 newop = newBINOP(OP_NULL, 0, selfop, valop);
2303 1 newop->op_type = OP_CUSTOM;
2304 1 newop->op_ppaddr = pp_object_set;
2305 1 newop->op_targ = idx;
2306  
2307 1 op_free(entersubop);
2308 1 return newop;
2309 }
2310  
2311 14 OpMORESIB_set(pushop, cvop);
2312 14 OpLASTSIB_set(valop, NULL);
2313 14 OpLASTSIB_set(selfop, NULL);
2314
2315 /* Use OP_NULL to avoid assertion failures on debugging Perls
2316 * (5.14-5.20), then set OP_CUSTOM */
2317 14 newop = newBINOP(OP_NULL, 0, selfop, valop);
2318 14 newop->op_type = OP_CUSTOM;
2319 14 newop->op_ppaddr = pp_object_set_typed;
2320 14 newop->op_targ = PTR2IV(data);
2321
2322 14 op_free(entersubop);
2323 14 return newop;
2324 } else {
2325 /* Getter: $obj->name - plain getter (no type check needed) */
2326 1 OpMORESIB_set(pushop, cvop);
2327 1 OpLASTSIB_set(selfop, NULL);
2328
2329 /* Use OP_NULL to avoid assertion failures on debugging Perls
2330 * (5.14-5.20), then set OP_CUSTOM */
2331 1 newop = newUNOP(OP_NULL, 0, selfop);
2332 1 newop->op_type = OP_CUSTOM;
2333 1 newop->op_ppaddr = pp_object_get;
2334 1 newop->op_targ = idx;
2335
2336 1 op_free(entersubop);
2337 1 return newop;
2338 }
2339 }
2340  
2341 /* XS fallback for reader-only accessor (get_X style) */
2342 40 XS_INTERNAL(xs_reader_fallback) {
2343 40 dXSARGS;
2344 40 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2345 40 IV idx = data->slot_idx;
2346 40 ClassMeta *meta = data->meta;
2347 40 SlotSpec *spec = meta->slots[idx];
2348 40 SV *self = ST(0);
2349 AV *av;
2350  
2351 PERL_UNUSED_ARG(items);
2352  
2353 40 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 39 1 40
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 38 39
2354 2 croak("Not an object");
2355 }
2356 38 av = (AV*)SvRV(self);
2357  
2358 /* Handle lazy builder */
2359 /*
2360 # uncoverable condition right count:1
2361 # uncoverable condition right count:4 */
2362 38 ▶ if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) {
Branch: 4 branches
BranchTrueFalseTotal
if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) { 38 0 38
if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) { 11 27 38
if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) { 10 1 11
if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) { 10 0 10
2363 10 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 9 1 10
2364 9 SV *slot = AvARRAY(av)[idx];
2365 9 ▶ if (!slot || !SvOK(slot)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(slot)) { 8 1 9
if (!slot || !SvOK(slot)) { 7 1 8
2366 /* Call builder method */
2367 8 dSP;
2368 IV count;
2369 8 ENTER;
2370 8 SAVETMPS;
2371 8 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 8 8
2372 8 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 8 8
2373 8 PUTBACK;
2374 16 count = call_method(SvPV_nolen(spec->builder_name), G_SCALAR);
2375 8 SPAGAIN;
2376 /*
2377 # uncoverable branch false
2378 # uncoverable condition right count:1 */
2379 8 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 8 0 8
2380 8 SV *built_val = POPs;
2381
2382 /* Type check the built value */
2383 8 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 7 1 8
2384 7 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 6 1 7
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 1 5 6
2385 2 built_val = apply_slot_coercion(aTHX_ built_val, spec);
2386 7 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 2 5 7
2387 1 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 1 0 1
2388 1 ? spec->registered->name
2389 3 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 1 2
2390 2 croak("Type constraint failed for '%s' in builder: expected %s",
2391 spec->name, type_name);
2392 }
2393 }
2394
2395 6 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 5 1 6
2396 5 sv_setsv(slot, built_val);
2397 }
2398 else {
2399 1 av_store(av, idx, newSVsv(built_val));
2400 }
2401 }
2402 6 PUTBACK;
2403 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
2404 6 LEAVE;
2405 }
2406 }
2407 }
2408  
2409 /* Getter - fast path: direct slot access */
2410 36 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 33 3 36
2411 33 SV *sv = AvARRAY(av)[idx];
2412 33 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 32 1 33
if (sv && SvOK(sv)) { 28 4 32
2413 28 ST(0) = sv;
2414 28 XSRETURN(1);
2415 }
2416 }
2417 /* Slow path: check prototype chain */
2418 {
2419 8 SV **proto = av_fetch(av, 0, 0);
2420 8 ▶ if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 7 1 8
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 2 5 7
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 1 1 2
2421 1 SV *result = resolve_property_chain(aTHX_ av, idx);
2422 1 ST(0) = result;
2423 1 XSRETURN(1);
2424 }
2425 }
2426 7 ST(0) = &PL_sv_undef;
2427 7 XSRETURN(1);
2428 }
2429  
2430 /* XS fallback for writer-only accessor (set_X style) */
2431 36 XS_INTERNAL(xs_writer_fallback) {
2432 36 dXSARGS;
2433 36 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2434 36 IV idx = data->slot_idx;
2435 36 ClassMeta *meta = data->meta;
2436 36 SlotSpec *spec = meta->slots[idx];
2437 36 SV *self = ST(0);
2438 AV *av;
2439 MAGIC *mg;
2440  
2441 36 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 35 1 36
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 34 35
2442 2 croak("Not an object");
2443 }
2444 34 av = (AV*)SvRV(self);
2445  
2446 34 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 1 33 34
2447 1 croak("Writer method requires a value argument");
2448 }
2449  
2450 /* Check frozen */
2451 33 mg = get_object_magic(aTHX_ self);
2452 33 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 31 33
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
2453 1 croak("Cannot modify frozen object");
2454 }
2455  
2456 /* Check readonly */
2457 32 ▶ if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */ 32 0 32
if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */ 1 31 32
2458 1 croak("Cannot modify readonly slot '%s'", spec->name);
2459 }
2460  
2461 {
2462 31 SV *val = ST(1);
2463
2464 /* Required fields cannot be set to undef */
2465 31 ▶ if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */ 31 0 31
if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */ 2 29 31
if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */ 1 1 2
2466 1 croak("Cannot set required slot '%s' to undef", spec->name);
2467 }
2468
2469 /* Coerce + type check */
2470 30 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 30 0 30
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 17 13 30
2471 17 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 16 1 17
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 4 12 16
2472 5 val = apply_slot_coercion(aTHX_ val, spec);
2473 17 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 4 13 17
2474 2 ▶ const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered)
Branch: 1 branch
BranchTrueFalseTotal
const char *type_name = (spec->type_id == TYPE_CUSTOM && spec->registered) 2 0 2
2475 2 ? spec->registered->name
2476 6 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 2 2 4
2477 4 croak("Type constraint failed for '%s': expected %s",
2478 spec->name, type_name);
2479 }
2480 }
2481
2482 /* Trigger callback ($self, $new_value) */
2483 26 ▶ if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 26 0 26
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 3 23 26
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 1 2 3
2484 1 dSP;
2485 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
2486 1 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 1 1
2487 1 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 1 1
2488 1 PUTBACK;
2489 2 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
2490 }
2491
2492 /* In-place update */
2493 26 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 25 1 26
2494 25 SV *slot = AvARRAY(av)[idx];
2495 25 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 21 4 25
2496 21 sv_setsv(slot, val);
2497 /* Weaken reference if is_weak flag is set */
2498 21 ▶ if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */ 21 0 21
if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */ 2 19 21
if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */ 1 1 2
2499 1 sv_rvweaken(slot);
2500 }
2501 21 ST(0) = val;
2502 21 XSRETURN(1);
2503 }
2504 }
2505 {
2506 5 SV *stored = newSVsv(val);
2507 5 av_store(av, idx, stored);
2508 /* Weaken reference if is_weak flag is set */
2509 5 ▶ if (spec && spec->is_weak && SvROK(stored)) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->is_weak && SvROK(stored)) { /* # uncoverable condition right count:1 */ 5 0 5
if (spec && spec->is_weak && SvROK(stored)) { /* # uncoverable condition right count:1 */ 3 2 5
if (spec && spec->is_weak && SvROK(stored)) { /* # uncoverable condition right count:1 */ 2 1 3
2510 2 sv_rvweaken(stored);
2511 }
2512 }
2513 5 ST(0) = val;
2514 5 XSRETURN(1);
2515 }
2516 }
2517  
2518 /* Install reader-only accessor (get_X style) */
2519 29 static void install_reader(pTHX_ const char *class_name, const char *method_name, IV idx, ClassMeta *meta) {
2520 char full_name[256];
2521 CV *cv;
2522 SlotOpData *data;
2523  
2524 29 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, method_name);
2525  
2526 /* Check if method already exists */
2527 29 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2528 29 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 28 29
2529 1 return;
2530 }
2531  
2532 28 Newx(data, 1, SlotOpData);
2533 28 data->slot_idx = idx;
2534 28 data->meta = meta;
2535  
2536 28 cv = newXS(full_name, xs_reader_fallback, __FILE__);
2537 28 CvXSUBANY(cv).any_iv = PTR2IV(data);
2538 }
2539  
2540 /* Install writer-only accessor (set_X style) */
2541 30 static void install_writer(pTHX_ const char *class_name, const char *method_name, IV idx, ClassMeta *meta) {
2542 char full_name[256];
2543 CV *cv;
2544 SlotOpData *data;
2545  
2546 30 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, method_name);
2547  
2548 /* Check if method already exists */
2549 30 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2550 30 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 29 30
2551 1 return;
2552 }
2553  
2554 29 Newx(data, 1, SlotOpData);
2555 29 data->slot_idx = idx;
2556 29 data->meta = meta;
2557  
2558 29 cv = newXS(full_name, xs_writer_fallback, __FILE__);
2559 29 CvXSUBANY(cv).any_iv = PTR2IV(data);
2560 }
2561  
2562 /* Install typed accessor (with type check, triggers, etc.) */
2563 533 static void install_accessor_typed(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta) {
2564 char full_name[256];
2565 CV *cv;
2566 SV *ckobj;
2567 SlotOpData *data;
2568  
2569 533 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, prop_name);
2570  
2571 /* An accessor may already be there: a typed one from an earlier
2572 * definition keeps its CV and takes the new slot data, a plain one
2573 * (the attribute had no type until this +attr or redefinition) is
2574 * replaced, and a sub of the user's own is left alone. */
2575 533 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2576 533 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 27 506 533
2577 27 ▶ if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) {
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) { 26 1 27
if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) { 20 6 26
2578 20 data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2579 20 data->slot_idx = idx;
2580 20 data->meta = meta;
2581 22 return;
2582 }
2583 7 ▶ if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) { 6 1 7
if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) { 1 5 6
2584 2 return;
2585 }
2586 {
2587 5 HV *stash = gv_stashpv(class_name, 0);
2588 /*
2589 # uncoverable branch false
2590 # uncoverable condition right count:1 */
2591 5 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 5 0 5
2592 5 (void)hv_delete(stash, prop_name, strlen(prop_name), G_DISCARD);
2593 }
2594 }
2595 }
2596  
2597 /* Allocate persistent data for this slot */
2598 511 Newx(data, 1, SlotOpData);
2599 511 data->slot_idx = idx;
2600 511 data->meta = meta;
2601  
2602 511 cv = newXS(full_name, xs_accessor_typed_fallback, __FILE__);
2603 511 CvXSUBANY(cv).any_iv = PTR2IV(data);
2604  
2605 511 ckobj = newSViv(PTR2IV(data));
2606 511 cv_set_call_checker(cv, accessor_typed_call_checker, ckobj);
2607 }
2608  
2609 /* XS fallback for clearer method (clear_X) */
2610 9844 XS_INTERNAL(xs_clearer_fallback) {
2611 9844 dXSARGS;
2612 9844 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2613 9844 IV idx = data->slot_idx;
2614 9844 SV *self = ST(0);
2615 AV *av;
2616 MAGIC *mg;
2617  
2618 PERL_UNUSED_ARG(items);
2619  
2620 9844 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 9843 1 9844
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 9842 9843
2621 2 croak("Not an object");
2622 }
2623 9842 av = (AV*)SvRV(self);
2624  
2625 /* Check frozen */
2626 9842 mg = get_object_magic(aTHX_ self);
2627 9842 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 9840 9842
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
2628 1 croak("Cannot modify frozen object");
2629 }
2630  
2631 /* Clear the slot by setting to undef */
2632 9841 av_store(av, idx, newSV(0));
2633
2634 9841 ST(0) = self; /* Return self for chaining */
2635 9841 XSRETURN(1);
2636 }
2637  
2638 /* Install clearer method (clear_X or custom name) */
2639 41 static void install_clearer(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name) {
2640 char full_name[256];
2641 CV *cv;
2642 SlotOpData *data;
2643  
2644 41 ▶ if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 7 34 41
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 7 0 7
2645 7 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, SvPV_nolen(custom_name));
2646 } else {
2647 34 snprintf(full_name, sizeof(full_name), "%s::clear_%s", class_name, prop_name);
2648 }
2649  
2650 /* Check if method already exists */
2651 41 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2652 41 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 2 39 41
2653 2 return;
2654 }
2655  
2656 39 Newx(data, 1, SlotOpData);
2657 39 data->slot_idx = idx;
2658 39 data->meta = meta;
2659  
2660 39 cv = newXS(full_name, xs_clearer_fallback, __FILE__);
2661 39 CvXSUBANY(cv).any_iv = PTR2IV(data);
2662 }
2663  
2664 /* XS fallback for predicate method (has_X) */
2665 10244 XS_INTERNAL(xs_predicate_fallback) {
2666 10244 dXSARGS;
2667 10244 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2668 10244 IV idx = data->slot_idx;
2669 10244 SV *self = ST(0);
2670 AV *av;
2671 SV **svp;
2672  
2673 PERL_UNUSED_ARG(items);
2674  
2675 10244 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 10243 1 10244
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 10242 10243
2676 2 croak("Not an object");
2677 }
2678 10242 av = (AV*)SvRV(self);
2679  
2680 /* Check if slot has a defined value */
2681 10242 svp = av_fetch(av, idx, 0);
2682 10242 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 10241 1 10242
if (svp && SvOK(*svp)) { 6224 4017 10241
2683 6224 ST(0) = &PL_sv_yes;
2684 } else {
2685 4018 ST(0) = &PL_sv_no;
2686 }
2687 10242 XSRETURN(1);
2688 }
2689  
2690 /* Install predicate method (has_X or custom name) */
2691 28 static void install_predicate(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name) {
2692 char full_name[256];
2693 CV *cv;
2694 SlotOpData *data;
2695  
2696 28 ▶ if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 7 21 28
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 7 0 7
2697 7 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, SvPV_nolen(custom_name));
2698 } else {
2699 21 snprintf(full_name, sizeof(full_name), "%s::has_%s", class_name, prop_name);
2700 }
2701  
2702 /* Check if method already exists */
2703 28 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2704 28 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 2 26 28
2705 2 return;
2706 }
2707  
2708 26 Newx(data, 1, SlotOpData);
2709 26 data->slot_idx = idx;
2710 26 data->meta = meta;
2711  
2712 26 cv = newXS(full_name, xs_predicate_fallback, __FILE__);
2713 26 CvXSUBANY(cv).any_iv = PTR2IV(data);
2714 }
2715  
2716 /* ============================================
2717 DEMOLISH Support (zero overhead if not used)
2718 ============================================ */
2719  
2720 /* XS DESTROY wrapper that calls DEMOLISH */
2721 6 XS_INTERNAL(xs_destroy_wrapper) {
2722 6 dXSARGS;
2723 6 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
2724 6 SV *self = ST(0);
2725
2726 PERL_UNUSED_VAR(items);
2727
2728 /*
2729 # uncoverable branch false
2730 # uncoverable condition right count:1
2731 # uncoverable condition right count:2 */
2732 6 ▶ if (meta && meta->demolish_cv) {
Branch: 2 branches
BranchTrueFalseTotal
if (meta && meta->demolish_cv) { 6 0 6
if (meta && meta->demolish_cv) { 6 0 6
2733 6 dSP;
2734 6 ENTER;
2735 6 SAVETMPS;
2736 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
2737 6 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 6 6
2738 6 PUTBACK;
2739 6 call_sv((SV*)meta->demolish_cv, G_DISCARD | G_EVAL);
2740 6 SPAGAIN;
2741 /* Ignore errors in DEMOLISH - don't die during destruction */
2742 6 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 */ 6 0 6
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 */ 1 5 6
2743 2 ▶ warn("Error in DEMOLISH: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("Error in DEMOLISH: %s", SvPV_nolen(ERRSV)); 1 0 1
2744 }
2745 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 5 6
2746 6 LEAVE;
2747 }
2748
2749 6 XSRETURN_EMPTY;
2750 }
2751  
2752 /* Install DESTROY wrapper - only called if DEMOLISH exists */
2753 5 static void install_destroy_wrapper(pTHX_ const char *class_name, ClassMeta *meta) {
2754 char full_name[256];
2755 CV *cv;
2756
2757 5 snprintf(full_name, sizeof(full_name), "%s::DESTROY", class_name);
2758
2759 /* Check if DESTROY already exists - don't override user's DESTROY */
2760 5 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2761 5 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 4 5
2762 1 return; /* User has their own DESTROY, don't interfere */
2763 }
2764
2765 4 cv = newXS(full_name, xs_destroy_wrapper, __FILE__);
2766 4 CvXSUBANY(cv).any_iv = PTR2IV(meta);
2767 }
2768  
2769 /* ============================================
2770 Role Support (zero overhead if not used)
2771 ============================================ */
2772  
2773 30 static RoleMeta* get_role_meta(pTHX_ const char *role_name, STRLEN len) {
2774 SV **svp;
2775 30 ▶ if (!g_role_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_role_registry) return NULL; 3 27 30
2776 27 svp = hv_fetch(g_role_registry, role_name, len, 0);
2777 27 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 16 11 27
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 16 0 16
2778 16 return INT2PTR(RoleMeta*, SvIV(*svp));
2779 }
2780 11 return NULL;
2781 }
2782  
2783 9 static void register_role_meta(pTHX_ const char *role_name, STRLEN len, RoleMeta *meta) {
2784 9 ▶ if (!g_role_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_role_registry) { 3 6 9
2785 3 g_role_registry = newHV();
2786 }
2787 9 hv_store(g_role_registry, role_name, len, newSViv(PTR2IV(meta)), 0);
2788 9 }
2789  
2790 /* Copy a method from role stash to class stash */
2791 11 static void copy_method(pTHX_ HV *from_stash, HV *to_stash, const char *method_name) {
2792 GV *from_gv;
2793 CV *cv;
2794 char full_name[512];
2795 GV *to_gv;
2796
2797 11 from_gv = gv_fetchmeth(from_stash, method_name, strlen(method_name), 0);
2798 11 ▶ if (!from_gv || !(cv = GvCV(from_gv))) { /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!from_gv || !(cv = GvCV(from_gv))) { /* # uncoverable condition left count:2 */ 9 2 11
if (!from_gv || !(cv = GvCV(from_gv))) { /* # uncoverable condition left count:2 */ 0 9 9
2799 3 return; /* No such method in role */
2800 }
2801
2802 /* Check if target already has this method */
2803 9 to_gv = gv_fetchmeth(to_stash, method_name, strlen(method_name), 0);
2804 9 ▶ if (to_gv && GvCV(to_gv)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (to_gv && GvCV(to_gv)) { /* # uncoverable condition right count:2 */ 1 8 9
if (to_gv && GvCV(to_gv)) { /* # uncoverable condition right count:2 */ 1 0 1
2805 1 return; /* Target already has method, don't override */
2806 }
2807
2808 /* Install the CV in target stash */
2809 8 ▶ snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name);
Branch: 6 branches
BranchTrueFalseTotal
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 8 0 8
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 8 0 8
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 0 8 8
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 0 0 0
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 8 0 8
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 0 8 8
2810 8 to_gv = gv_fetchpv(full_name, GV_ADD, SVt_PVCV);
2811 /*
2812 # uncoverable branch false
2813 # uncoverable condition right count:1 */
2814 8 ▶ if (to_gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_gv) { 8 0 8
2815 /* Share the CV between role and class */
2816 8 GvCV_set(to_gv, (CV*)SvREFCNT_inc((SV*)cv));
2817 8 GvCVGEN(to_gv) = 0; /* Clear cache */
2818 }
2819 }
2820  
2821 /* Apply a role to a class */
2822 13 static void apply_role_to_class(pTHX_ ClassMeta *class_meta, RoleMeta *role_meta) {
2823 IV i;
2824 HE *entry;
2825
2826 /* Check required methods */
2827 16 ▶ for (i = 0; i < role_meta->required_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role_meta->required_count; i++) { 5 11 16
2828 5 const char *required = role_meta->required_methods[i];
2829 5 GV *gv = gv_fetchmeth(class_meta->stash, required, strlen(required), 0);
2830 5 ▶ if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */ 3 2 5
if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */ 0 3 3
2831 2 croak("Class '%s' does not implement required method '%s' from role '%s'",
2832 class_meta->class_name, required, role_meta->role_name);
2833 }
2834 }
2835
2836 /* Copy role's slots to class */
2837 35 ▶ for (i = 0; i < role_meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role_meta->slot_count; i++) { 25 10 35
2838 25 SlotSpec *role_slot = role_meta->slots[i];
2839 IV new_idx;
2840 SV **existing;
2841
2842 /* Check for slot name conflict */
2843 25 existing = hv_fetch(class_meta->prop_to_idx, role_slot->name, strlen(role_slot->name), 0);
2844 25 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 24 25
2845 1 croak("Slot conflict: '%s' already exists in class '%s' (from role '%s')",
2846 role_slot->name, class_meta->class_name, role_meta->role_name);
2847 }
2848
2849 /* Add slot to class */
2850 24 new_idx = class_meta->slot_count++;
2851 24 ▶ Renew(class_meta->slots, class_meta->slot_count, SlotSpec*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(class_meta->slots, class_meta->slot_count, SlotSpec*); 0 24 24
2852 24 ▶ Renew(class_meta->idx_to_prop, class_meta->slot_count, char*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(class_meta->idx_to_prop, class_meta->slot_count, char*); 0 24 24
2853
2854 /* Copy slot spec */
2855 24 class_meta->slots[new_idx] = role_slot; /* Share the spec */
2856 24 class_meta->idx_to_prop[new_idx] = role_slot->name;
2857 24 hv_store(class_meta->prop_to_idx, role_slot->name, strlen(role_slot->name),
2858 newSViv(new_idx), 0);
2859
2860 /* Add to arg_to_idx using init_arg if specified, otherwise property name */
2861 24 ▶ if (role_slot->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->init_arg) { 1 23 24
2862 STRLEN arg_len;
2863 1 const char *arg_name = SvPV(role_slot->init_arg, arg_len);
2864 1 hv_store(class_meta->arg_to_idx, arg_name, arg_len, newSViv(new_idx), 0);
2865 } else {
2866 23 hv_store(class_meta->arg_to_idx, role_slot->name, strlen(role_slot->name),
2867 newSViv(new_idx), 0);
2868 }
2869
2870 /* Track class-level fast-path flags for role slots */
2871 24 ▶ if (role_slot->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_type) { 10 14 24
2872 10 class_meta->has_any_types = 1;
2873 }
2874 24 ▶ if (role_slot->has_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_default) { 6 18 24
2875 6 class_meta->has_any_defaults = 1;
2876 }
2877 24 ▶ if (role_slot->has_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_trigger) { 2 22 24
2878 2 class_meta->has_any_triggers = 1;
2879 }
2880 24 ▶ if (role_slot->is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->is_required) { 1 23 24
2881 1 class_meta->has_any_required = 1;
2882 }
2883 24 ▶ if (role_slot->is_lazy) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->is_lazy) { 2 22 24
2884 2 class_meta->has_any_lazy = 1;
2885 }
2886 24 ▶ if (role_slot->has_builder) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_builder) { 2 22 24
2887 2 class_meta->has_any_builders = 1;
2888 }
2889 24 ▶ if (role_slot->is_weak) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->is_weak) { 1 23 24
2890 1 class_meta->has_any_weak = 1;
2891 }
2892
2893 /* Install accessor for this slot */
2894 24 ▶ if (role_slot->has_type || role_slot->has_trigger || role_slot->has_coerce ||
Branch: 3 branches
BranchTrueFalseTotal
if (role_slot->has_type || role_slot->has_trigger || role_slot->has_coerce || 14 10 24
if (role_slot->has_type || role_slot->has_trigger || role_slot->has_coerce || 13 1 14
if (role_slot->has_type || role_slot->has_trigger || role_slot->has_coerce || 12 1 13
2895 12 ▶ role_slot->is_readonly || role_slot->is_lazy || role_slot->is_required || role_slot->is_weak) {
Branch: 4 branches
BranchTrueFalseTotal
role_slot->is_readonly || role_slot->is_lazy || role_slot->is_required || role_slot->is_weak) { 11 1 12
role_slot->is_readonly || role_slot->is_lazy || role_slot->is_required || role_slot->is_weak) { 9 2 11
role_slot->is_readonly || role_slot->is_lazy || role_slot->is_required || role_slot->is_weak) { 8 1 9
role_slot->is_readonly || role_slot->is_lazy || role_slot->is_required || role_slot->is_weak) { 1 7 8
2896 17 install_accessor_typed(aTHX_ class_meta->class_name, role_slot->name, new_idx, class_meta);
2897 } else {
2898 7 install_accessor(aTHX_ class_meta->class_name, role_slot->name, new_idx);
2899 }
2900
2901 24 ▶ if (role_slot->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_clearer) { 1 23 24
2902 1 install_clearer(aTHX_ class_meta->class_name, role_slot->name, new_idx, class_meta, role_slot->clearer_name);
2903 }
2904 24 ▶ if (role_slot->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->has_predicate) { 1 23 24
2905 1 install_predicate(aTHX_ class_meta->class_name, role_slot->name, new_idx, class_meta, role_slot->predicate_name);
2906 }
2907 24 ▶ if (role_slot->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->reader_name) { 1 23 24
2908 2 install_reader(aTHX_ class_meta->class_name, SvPV_nolen(role_slot->reader_name), new_idx, class_meta);
2909 }
2910 24 ▶ if (role_slot->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_slot->writer_name) { 1 23 24
2911 2 install_writer(aTHX_ class_meta->class_name, SvPV_nolen(role_slot->writer_name), new_idx, class_meta);
2912 }
2913 }
2914
2915 /* Copy role's methods to class */
2916 /*
2917 # uncoverable branch false
2918 # uncoverable condition right count:1 */
2919 10 ▶ if (role_meta->stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (role_meta->stash) { 10 0 10
2920 10 hv_iterinit(role_meta->stash);
2921 22 ▶ while ((entry = hv_iternext(role_meta->stash))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(role_meta->stash))) { 12 10 22
2922 12 ▶ const char *name = HePV(entry, PL_na);
Branch: 1 branch
BranchTrueFalseTotal
const char *name = HePV(entry, PL_na); 0 12 12
2923 /* Skip special entries and slots (already handled) */
2924 12 ▶ if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) { 2 10 12
if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) { 1 1 2
2925 11 copy_method(aTHX_ role_meta->stash, class_meta->stash, name);
2926 }
2927 }
2928 }
2929
2930 /* Track consumed role */
2931 10 ▶ Renew(class_meta->consumed_roles, class_meta->role_count + 1, RoleMeta*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(class_meta->consumed_roles, class_meta->role_count + 1, RoleMeta*); 0 10 10
2932 10 class_meta->consumed_roles[class_meta->role_count++] = role_meta;
2933 10 }
2934  
2935 /* ============================================
2936 Method Modifiers (zero overhead if not used)
2937 ============================================ */
2938  
2939 /* Get or create modified method entry */
2940 17 static ModifiedMethod* get_or_create_modified_method(pTHX_ ClassMeta *meta, const char *method_name) {
2941 SV **svp;
2942 ModifiedMethod *mod;
2943 17 STRLEN name_len = strlen(method_name);
2944
2945 17 ▶ if (!meta->modified_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta->modified_methods) { 6 11 17
2946 6 meta->modified_methods = newHV();
2947 }
2948
2949 17 svp = hv_fetch(meta->modified_methods, method_name, name_len, 0);
2950 17 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 10 7 17
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 10 0 10
2951 10 return INT2PTR(ModifiedMethod*, SvIV(*svp));
2952 }
2953
2954 /* Create new modified method entry */
2955 7 Newxz(mod, 1, ModifiedMethod);
2956
2957 /* Get the original CV */
2958 {
2959 7 GV *gv = gv_fetchmeth(meta->stash, method_name, name_len, 0);
2960 7 ▶ if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 6 1 7
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 6 0 6
2961 6 mod->original_cv = GvCV(gv);
2962 6 SvREFCNT_inc((SV*)mod->original_cv);
2963 }
2964 }
2965
2966 7 hv_store(meta->modified_methods, method_name, name_len, newSViv(PTR2IV(mod)), 0);
2967 7 return mod;
2968 }
2969  
2970 /* XS wrapper for modified methods */
2971 10 XS_INTERNAL(xs_modified_method_wrapper) {
2972 10 dXSARGS;
2973 10 ModifiedMethod *mod = INT2PTR(ModifiedMethod*, CvXSUBANY(cv).any_iv);
2974 MethodModifier *m;
2975 10 int count = 0;
2976 10 I32 gimme = GIMME_V;
2977 AV *saved_args;
2978 AV *saved_results;
2979 int i;
2980
2981 /* Save original arguments for before/after chains */
2982 10 saved_args = newAV();
2983 10 sv_2mortal((SV*)saved_args);
2984 23 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 13 10 23
2985 13 av_push(saved_args, SvREFCNT_inc(ST(i)));
2986 }
2987
2988 /* Call before chain (in stack order - most recent first) */
2989 20 ▶ for (m = mod->before_chain; m; m = m->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = mod->before_chain; m; m = m->next) { 10 10 20
2990 10 dSP;
2991 10 ENTER;
2992 10 SAVETMPS;
2993 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
2994 24 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 14 10 24
2995 14 SV **svp = av_fetch(saved_args, i, 0);
2996 /*
2997 # uncoverable condition left count:1
2998 # uncoverable condition right count:2 */
2999 14 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 14 14
XPUSHs(svp ? *svp : &PL_sv_undef); 14 0 14
3000 }
3001 10 PUTBACK;
3002 10 call_sv(m->callback, G_DISCARD);
3003 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 10 10
3004 10 LEAVE;
3005 }
3006
3007 /* Save results from original/around call */
3008 10 saved_results = newAV();
3009 10 sv_2mortal((SV*)saved_results);
3010
3011 /* Call around chain (or original if no around) */
3012 10 ▶ if (mod->around_chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (mod->around_chain) { 3 7 10
3013 /* For around, we pass ($orig, $self, @args) */
3014 3 m = mod->around_chain;
3015 {
3016 3 dSP;
3017 3 ENTER;
3018 3 SAVETMPS;
3019 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
3020 3 ▶ XPUSHs(sv_2mortal(newRV_inc((SV*)mod->original_cv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV*)mod->original_cv))); 0 3 3
3021 7 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 4 3 7
3022 4 SV **svp = av_fetch(saved_args, i, 0);
3023 /*
3024 # uncoverable condition left count:1
3025 # uncoverable condition right count:2 */
3026 4 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 4 4
XPUSHs(svp ? *svp : &PL_sv_undef); 4 0 4
3027 }
3028 3 PUTBACK;
3029 3 ▶ count = call_sv(m->callback, gimme == G_ARRAY ? G_LIST : G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
count = call_sv(m->callback, gimme == G_ARRAY ? G_LIST : G_SCALAR); 1 2 3
3030 3 SPAGAIN;
3031 /* Save results before LEAVE destroys them - they're on stack in reverse */
3032 8 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 5 3 8
3033 5 av_push(saved_results, newSVsv(POPs));
3034 }
3035 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
3036 3 LEAVE;
3037 }
3038 7 ▶ } else if (mod->original_cv) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mod->original_cv) { 6 1 7
3039 /* Call original method */
3040 6 dSP;
3041 6 ENTER;
3042 6 SAVETMPS;
3043 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
3044 14 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 8 6 14
3045 8 SV **svp = av_fetch(saved_args, i, 0);
3046 /*
3047 # uncoverable condition left count:1
3048 # uncoverable condition right count:2 */
3049 8 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 8 8
XPUSHs(svp ? *svp : &PL_sv_undef); 8 0 8
3050 }
3051 6 PUTBACK;
3052 6 ▶ count = call_sv((SV*)mod->original_cv, gimme == G_ARRAY ? G_LIST : G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
count = call_sv((SV*)mod->original_cv, gimme == G_ARRAY ? G_LIST : G_SCALAR); 1 5 6
3053 6 SPAGAIN;
3054 /* Save results before LEAVE destroys them */
3055 13 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 7 6 13
3056 7 av_push(saved_results, newSVsv(POPs));
3057 }
3058 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
3059 6 LEAVE;
3060 }
3061
3062 /* Call after chain (in order of registration) */
3063 19 ▶ for (m = mod->after_chain; m; m = m->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = mod->after_chain; m; m = m->next) { 9 10 19
3064 9 dSP;
3065 9 ENTER;
3066 9 SAVETMPS;
3067 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
3068 20 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 11 9 20
3069 11 SV **svp = av_fetch(saved_args, i, 0);
3070 /*
3071 # uncoverable condition left count:1
3072 # uncoverable condition right count:2 */
3073 11 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 11 11
XPUSHs(svp ? *svp : &PL_sv_undef); 11 0 11
3074 }
3075 9 PUTBACK;
3076 9 call_sv(m->callback, G_DISCARD);
3077 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 9 9
3078 9 LEAVE;
3079 }
3080
3081 /* Put saved results back on stack (they were saved in reverse order) */
3082 {
3083 10 count = av_len(saved_results) + 1;
3084 22 ▶ for (i = count - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) { 12 10 22
3085 12 SV **svp = av_fetch(saved_results, i, 0);
3086 /* Use sv_mortalcopy to put a mortal copy on stack */
3087 12 ▶ ST(count - 1 - i) = sv_mortalcopy(svp ? *svp : &PL_sv_undef); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
ST(count - 1 - i) = sv_mortalcopy(svp ? *svp : &PL_sv_undef); /* # uncoverable condition right count:1 */ 12 0 12
3088 }
3089 }
3090
3091 10 XSRETURN(count);
3092 }
3093  
3094 /* Install the wrapper if not already done */
3095 17 static void install_modifier_wrapper(pTHX_ ClassMeta *meta, const char *method_name, ModifiedMethod *mod) {
3096 char full_name[256];
3097 CV *existing_cv;
3098
3099 17 snprintf(full_name, sizeof(full_name), "%s::%s", meta->class_name, method_name);
3100
3101 17 existing_cv = get_cvn_flags(full_name, strlen(full_name), 0);
3102
3103 /* Only install wrapper once - check if it's already our wrapper */
3104 17 ▶ if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) { 15 2 17
if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) { 9 6 15
3105 9 return; /* Already wrapped */
3106 }
3107
3108 /* Install wrapper without "Subroutine redefined" warning */
3109 {
3110 8 GV *gv = gv_fetchpv(full_name, GV_ADD, SVt_PVCV);
3111 8 CV *cv = newXS_flags(NULL, xs_modified_method_wrapper, __FILE__, NULL, 0);
3112 8 CvXSUBANY(cv).any_iv = PTR2IV(mod);
3113 /* Silently replace the CV in the GV */
3114 8 ▶ if (GvCV(gv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv)) { 7 1 8
3115 7 SvREFCNT_dec(GvCV(gv));
3116 }
3117 8 GvCV_set(gv, cv);
3118 }
3119 }
3120  
3121 /* Add a modifier to a method */
3122 17 static void add_modifier(pTHX_ ClassMeta *meta, const char *method_name, SV *callback, int type) {
3123 ModifiedMethod *mod;
3124 MethodModifier *new_mod;
3125
3126 17 mod = get_or_create_modified_method(aTHX_ meta, method_name);
3127
3128 17 Newx(new_mod, 1, MethodModifier);
3129 17 new_mod->callback = newSVsv(callback);
3130 17 new_mod->next = NULL;
3131
3132 /* Add to appropriate chain */
3133 17 switch (type) { /* # uncoverable branch true count:4 */
3134 6 case 0: /* before */
3135 6 new_mod->next = mod->before_chain;
3136 6 mod->before_chain = new_mod;
3137 6 break;
3138 8 case 1: /* after */
3139 /* Add to end of after chain */
3140 8 ▶ if (!mod->after_chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mod->after_chain) { 5 3 8
3141 5 mod->after_chain = new_mod;
3142 } else {
3143 3 MethodModifier *last = mod->after_chain;
3144 4 ▶ while (last->next) last = last->next;
Branch: 1 branch
BranchTrueFalseTotal
while (last->next) last = last->next; 1 3 4
3145 3 last->next = new_mod;
3146 }
3147 8 break;
3148 3 case 2: /* around */
3149 /* around wraps previous around/original */
3150 3 new_mod->next = mod->around_chain;
3151 3 mod->around_chain = new_mod;
3152 3 break;
3153 }
3154
3155 17 install_modifier_wrapper(aTHX_ meta, method_name, mod);
3156 17 }
3157  
3158 /* ============================================
3159 XS API Functions
3160 ============================================ */
3161  
3162 311 XS_INTERNAL(xs_define) {
3163 311 dXSARGS;
3164 STRLEN class_len;
3165 const char *class_pv;
3166 ClassMeta *meta;
3167 IV i;
3168 311 IV first_prop = 1; /* index of first property arg (after class name) */
3169  
3170 /* Multiple inheritance support */
3171 311 ClassMeta **parent_metas = NULL;
3172 311 IV parent_count = 0;
3173
3174 311 ▶ if (items < 1) croak("Usage: Object::Proto::define($class, @properties)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::define($class, @properties)"); 1 310 311
3175
3176 310 class_pv = SvPV(ST(0), class_len);
3177  
3178 /* Check for extends => 'ParentClass' or extends => ['P1','P2'] in arguments */
3179 742 ▶ for (i = 1; i < items - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items - 1; i++) { 467 275 742
3180 STRLEN klen;
3181 467 const char *kpv = SvPV(ST(i), klen);
3182 467 ▶ if (klen == 7 && memEQ(kpv, "extends", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(kpv, "extends", 7)) { 50 417 467
if (klen == 7 && memEQ(kpv, "extends", 7)) { 35 15 50
3183 35 SV *val = ST(i + 1);
3184 40 ▶ if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 7 28 35
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 6 1 7
3185 /* extends => ['Parent1', 'Parent2', ...] */
3186 6 AV *parents_av = (AV*)SvRV(val);
3187 6 IV plen = av_len(parents_av) + 1;
3188 IV p;
3189 6 ▶ Newx(parent_metas, plen, ClassMeta*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(parent_metas, plen, ClassMeta*); 0 6 6
3190 18 ▶ for (p = 0; p < plen; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < plen; p++) { 13 5 18
3191 13 SV **elem = av_fetch(parents_av, p, 0);
3192 13 ▶ if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */ 13 0 13
if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */ 11 2 13
3193 STRLEN pname_len;
3194 11 const char *pname = SvPV(*elem, pname_len);
3195 11 ClassMeta *pmeta = get_class_meta(aTHX_ pname, pname_len);
3196 11 ▶ if (!pmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmeta) { 1 10 11
3197 1 Safefree(parent_metas);
3198 1 croak("Object::Proto::define: parent class '%s' has not been defined", pname);
3199 }
3200 10 parent_metas[parent_count++] = pmeta;
3201 }
3202 }
3203 } else {
3204 /* extends => 'SingleParent' */
3205 STRLEN parent_len;
3206 29 const char *parent_pv = SvPV(val, parent_len);
3207 29 ClassMeta *pmeta = get_class_meta(aTHX_ parent_pv, parent_len);
3208 29 ▶ if (!pmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmeta) { 2 27 29
3209 2 croak("Object::Proto::define: parent class '%s' has not been defined", parent_pv);
3210 }
3211 27 Newx(parent_metas, 1, ClassMeta*);
3212 27 parent_metas[parent_count++] = pmeta;
3213 }
3214 /* Shift remaining args down to remove extends => value */
3215 {
3216 IV j;
3217 87 ▶ for (j = i; j < items - 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i; j < items - 2; j++) { 55 32 87
3218 55 ST(j) = ST(j + 2);
3219 }
3220 32 items -= 2;
3221 }
3222 32 break;
3223 }
3224 }
3225  
3226 /* Get or create class meta */
3227 307 meta = get_class_meta(aTHX_ class_pv, class_len);
3228 307 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 306 1 307
3229 306 meta = create_class_meta(aTHX_ class_pv, class_len);
3230 306 register_class_meta(aTHX_ class_pv, class_len, meta);
3231 }
3232  
3233 /* Store parent references */
3234 307 ▶ if (parent_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_count > 0) { 32 275 307
3235 32 ▶ Newx(meta->parent_classes, parent_count, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(meta->parent_classes, parent_count, char*); 0 32 32
3236 32 ▶ Newx(meta->parent_metas, parent_count, ClassMeta*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(meta->parent_metas, parent_count, ClassMeta*); 0 32 32
3237 32 meta->parent_count = parent_count;
3238 68 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 36 32 68
3239 36 STRLEN plen = strlen(parent_metas[i]->class_name);
3240 36 Newx(meta->parent_classes[i], plen + 1, char);
3241 36 Copy(parent_metas[i]->class_name, meta->parent_classes[i], plen + 1, char);
3242 36 meta->parent_metas[i] = parent_metas[i];
3243 }
3244 }
3245  
3246 /* Calculate total slots needed: all parent inherited + child own */
3247 {
3248 307 IV total_parent_slots = 0;
3249 307 IV child_props = items - 1;
3250 IV max_slots;
3251 343 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 36 307 343
3252 36 total_parent_slots += parent_metas[i]->slot_count - 1; /* -1 for prototype slot */
3253 }
3254 307 max_slots = 1 + total_parent_slots + child_props;
3255 307 ▶ Renew(meta->idx_to_prop, max_slots + 1, char*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(meta->idx_to_prop, max_slots + 1, char*); 0 307 307
3256 307 ▶ Renew(meta->slots, max_slots + 1, SlotSpec*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(meta->slots, max_slots + 1, SlotSpec*); 0 307 307
3257 1786 ▶ for (i = 0; i <= max_slots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max_slots; i++) { 1479 307 1786
3258 1479 meta->slots[i] = NULL;
3259 1479 meta->idx_to_prop[i] = NULL;
3260 }
3261 }
3262  
3263 /* Copy parent slots (if extends) - iterate all parents, first parent wins on conflict */
3264 343 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 36 307 343
3265 36 ClassMeta *pmeta = parent_metas[i];
3266 IV j;
3267 140 ▶ for (j = 1; j < pmeta->slot_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < pmeta->slot_count; j++) { 104 36 140
3268 104 SlotSpec *parent_spec = pmeta->slots[j];
3269 /*
3270 # uncoverable branch false
3271 # uncoverable condition right count:1 */
3272 104 ▶ if (parent_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_spec) { 104 0 104
3273 /* Skip if property already defined by earlier parent */
3274 104 SV **existing = hv_fetch(meta->prop_to_idx, parent_spec->name,
3275 strlen(parent_spec->name), 0);
3276 104 ▶ if (existing && SvIOK(*existing)) continue; /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (existing && SvIOK(*existing)) continue; /* # uncoverable condition right count:2 */ 4 100 104
if (existing && SvIOK(*existing)) continue; /* # uncoverable condition right count:2 */ 4 0 4
3277  
3278 100 SlotSpec *cloned = clone_slot_spec(aTHX_ parent_spec);
3279 100 IV idx = meta->slot_count++;
3280 100 meta->slots[idx] = cloned;
3281  
3282 100 ▶ if (cloned->has_type) meta->has_any_types = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_type) meta->has_any_types = 1; 60 40 100
3283 100 ▶ if (cloned->has_default) meta->has_any_defaults = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_default) meta->has_any_defaults = 1; 40 60 100
3284 100 ▶ if (cloned->has_trigger) meta->has_any_triggers = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_trigger) meta->has_any_triggers = 1; 3 97 100
3285 100 ▶ if (cloned->is_required) meta->has_any_required = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->is_required) meta->has_any_required = 1; 12 88 100
3286 100 ▶ if (cloned->is_lazy) meta->has_any_lazy = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->is_lazy) meta->has_any_lazy = 1; 2 98 100
3287 100 ▶ if (cloned->has_builder) meta->has_any_builders = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_builder) meta->has_any_builders = 1; 5 95 100
3288 100 ▶ if (cloned->is_weak) meta->has_any_weak = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->is_weak) meta->has_any_weak = 1; 1 99 100
3289  
3290 100 hv_store(meta->prop_to_idx, cloned->name, strlen(cloned->name), newSViv(idx), 0);
3291
3292 /* Add to arg_to_idx using init_arg if specified, otherwise property name */
3293 100 ▶ if (cloned->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->init_arg) { 2 98 100
3294 STRLEN arg_len;
3295 2 const char *arg_name = SvPV(cloned->init_arg, arg_len);
3296 2 hv_store(meta->arg_to_idx, arg_name, arg_len, newSViv(idx), 0);
3297 } else {
3298 98 hv_store(meta->arg_to_idx, cloned->name, strlen(cloned->name), newSViv(idx), 0);
3299 }
3300
3301 100 meta->idx_to_prop[idx] = cloned->name;
3302  
3303 100 ▶ if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) {
Branch: 7 branches
BranchTrueFalseTotal
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 40 60 100
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 38 2 40
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 36 2 38
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 35 1 36
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 34 1 35
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 33 1 34
if (cloned->has_type || cloned->has_trigger || cloned->has_coerce || cloned->is_readonly || cloned->is_lazy || cloned->is_required || cloned->is_weak) { 1 32 33
3304 68 install_accessor_typed(aTHX_ class_pv, cloned->name, idx, meta);
3305 } else {
3306 32 install_accessor(aTHX_ class_pv, cloned->name, idx);
3307 }
3308 100 ▶ if (cloned->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_clearer) { 5 95 100
3309 5 install_clearer(aTHX_ class_pv, cloned->name, idx, meta, cloned->clearer_name);
3310 }
3311 100 ▶ if (cloned->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->has_predicate) { 5 95 100
3312 5 install_predicate(aTHX_ class_pv, cloned->name, idx, meta, cloned->predicate_name);
3313 }
3314 100 ▶ if (cloned->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->reader_name) { 2 98 100
3315 4 install_reader(aTHX_ class_pv, SvPV_nolen(cloned->reader_name), idx, meta);
3316 }
3317 100 ▶ if (cloned->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (cloned->writer_name) { 2 98 100
3318 4 install_writer(aTHX_ class_pv, SvPV_nolen(cloned->writer_name), idx, meta);
3319 }
3320 }
3321 }
3322 }
3323  
3324 /* Register each child property */
3325 1067 ▶ for (i = first_prop; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = first_prop; i < items; i++) { 761 306 1067
3326 STRLEN spec_len;
3327 761 const char *spec_pv = SvPV(ST(i), spec_len);
3328 SlotSpec *spec;
3329 IV idx;
3330 SV **existing;
3331 761 U8 is_modification = 0;
3332 761 const char *real_spec_pv = spec_pv;
3333 761 STRLEN real_spec_len = spec_len;
3334  
3335 /* Check for +attr modification syntax (Moo/Moose-style) */
3336 761 ▶ if (spec_len > 0 && spec_pv[0] == '+') { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec_len > 0 && spec_pv[0] == '+') { /* # uncoverable condition right count:1 */ 761 0 761
if (spec_len > 0 && spec_pv[0] == '+') { /* # uncoverable condition right count:1 */ 29 732 761
3337 29 is_modification = 1;
3338 29 real_spec_pv = spec_pv + 1;
3339 29 real_spec_len = spec_len - 1;
3340 }
3341  
3342 /* Parse the slot spec (e.g., "name:Str:required" or just "name") */
3343 761 spec = parse_slot_spec(aTHX_ real_spec_pv, real_spec_len);
3344  
3345 /* Check if this property already exists (from parent) */
3346 761 existing = hv_fetch(meta->prop_to_idx, spec->name, strlen(spec->name), 0);
3347
3348 761 ▶ if (is_modification) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_modification) { 29 732 761
3349 /* +attr syntax: merge child modifiers onto parent spec */
3350 SlotSpec *parent_spec;
3351 SlotSpec *merged;
3352
3353 29 ▶ if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */ 28 1 29
if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */ 0 28 28
3354 1 croak("+%s: no inherited attribute '%s' to modify",
3355 spec->name, spec->name);
3356 }
3357 28 idx = SvIV(*existing);
3358 28 parent_spec = meta->slots[idx];
3359
3360 /* Merge override onto clone of parent */
3361 28 merged = merge_slot_spec(aTHX_ parent_spec, spec);
3362
3363 /* Free the override spec (we cloned what we needed) */
3364 28 Safefree(spec->name);
3365 28 Safefree(spec);
3366 28 spec = merged;
3367
3368 /* Free old parent spec */
3369 /*
3370 # uncoverable branch false
3371 # uncoverable condition right count:1 */
3372 28 ▶ if (parent_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_spec) { 28 0 28
3373 28 Safefree(parent_spec->name);
3374 28 Safefree(parent_spec);
3375 }
3376 732 ▶ } else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
} else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */ 5 727 732
} else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */ 5 0 5
3377 /* Full override: reuse same slot index */
3378 5 idx = SvIV(*existing);
3379 /* Free old spec */
3380 5 ▶ if (meta->slots[idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->slots[idx]) { 2 3 5
3381 2 Safefree(meta->slots[idx]->name);
3382 2 Safefree(meta->slots[idx]);
3383 }
3384 } else {
3385 727 idx = meta->slot_count++;
3386 }
3387  
3388 760 meta->slots[idx] = spec;
3389
3390 /* Update class-level flags for fast path checks */
3391 760 ▶ if (spec->has_type) meta->has_any_types = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) meta->has_any_types = 1; 413 347 760
3392 760 ▶ if (spec->has_default) meta->has_any_defaults = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_default) meta->has_any_defaults = 1; 103 657 760
3393 760 ▶ if (spec->has_trigger) meta->has_any_triggers = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_trigger) meta->has_any_triggers = 1; 16 744 760
3394 760 ▶ if (spec->is_required) meta->has_any_required = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_required) meta->has_any_required = 1; 43 717 760
3395 760 ▶ if (spec->has_builder) meta->has_any_builders = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_builder) meta->has_any_builders = 1; 35 725 760
3396 760 ▶ if (spec->is_weak) meta->has_any_weak = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_weak) meta->has_any_weak = 1; 14 746 760
3397  
3398 /* Store name -> idx mapping (use parsed name, not full spec) */
3399 760 hv_store(meta->prop_to_idx, spec->name, strlen(spec->name), newSViv(idx), 0);
3400
3401 /* Store arg -> idx mapping (use init_arg if specified, otherwise property name) */
3402 760 ▶ if (spec->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->init_arg) { 12 748 760
3403 STRLEN arg_len;
3404 12 const char *arg_name = SvPV(spec->init_arg, arg_len);
3405 12 hv_store(meta->arg_to_idx, arg_name, arg_len, newSViv(idx), 0);
3406 } else {
3407 748 hv_store(meta->arg_to_idx, spec->name, strlen(spec->name), newSViv(idx), 0);
3408 }
3409  
3410 /* Store idx -> name mapping */
3411 760 meta->idx_to_prop[idx] = spec->name;
3412
3413 /* Update lazy flag */
3414 760 ▶ if (spec->is_lazy) meta->has_any_lazy = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_lazy) meta->has_any_lazy = 1; 39 721 760
3415  
3416 /* Install accessor method - typed or plain depending on spec */
3417 760 ▶ if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) {
Branch: 8 branches
BranchTrueFalseTotal
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 347 413 760
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 340 7 347
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 335 5 340
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 333 2 335
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 323 10 333
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 320 3 323
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 314 6 320
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 2 312 314
3418 448 install_accessor_typed(aTHX_ class_pv, spec->name, idx, meta);
3419 } else {
3420 312 install_accessor(aTHX_ class_pv, spec->name, idx);
3421 }
3422
3423 /* Install clearer method if requested */
3424 760 ▶ if (spec->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_clearer) { 35 725 760
3425 35 install_clearer(aTHX_ class_pv, spec->name, idx, meta, spec->clearer_name);
3426 }
3427
3428 /* Install predicate method if requested */
3429 760 ▶ if (spec->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_predicate) { 22 738 760
3430 22 install_predicate(aTHX_ class_pv, spec->name, idx, meta, spec->predicate_name);
3431 }
3432
3433 /* Install custom reader method if specified */
3434 760 ▶ if (spec->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->reader_name) { 26 734 760
3435 52 install_reader(aTHX_ class_pv, SvPV_nolen(spec->reader_name), idx, meta);
3436 }
3437
3438 /* Install custom writer method if specified */
3439 760 ▶ if (spec->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->writer_name) { 27 733 760
3440 54 install_writer(aTHX_ class_pv, SvPV_nolen(spec->writer_name), idx, meta);
3441 }
3442 }
3443  
3444 /* Set up @ISA for parent classes (C3 MRO for multiple inheritance) */
3445 306 ▶ if (parent_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_count > 0) { 31 275 306
3446 31 AV *isa = get_av(Perl_form(aTHX_ "%s::ISA", class_pv), GV_ADD);
3447 66 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 35 31 66
3448 35 av_push(isa, newSVpv(parent_metas[i]->class_name, 0));
3449 }
3450 /* Notify Perl's method resolution cache that ISA changed.
3451 * Perl_mro_isa_changed_in was made a hidden (non-exported) symbol
3452 * in Perl 5.36.0. mro_method_changed_in is public since 5.10.0
3453 * and is the correct public API for invalidating the method cache
3454 * after an ISA change. */
3455 31 mro_method_changed_in(meta->stash);
3456 31 Safefree(parent_metas);
3457 }
3458  
3459 /* Install constructor */
3460 306 install_constructor(aTHX_ class_pv, meta);
3461  
3462 /* Install prototype methods as class methods */
3463 {
3464 char method_name[256];
3465 306 snprintf(method_name, sizeof(method_name), "%s::set_prototype", class_pv);
3466 306 newXS(method_name, xs_set_prototype, __FILE__);
3467 306 snprintf(method_name, sizeof(method_name), "%s::prototype", class_pv);
3468 306 newXS(method_name, xs_prototype, __FILE__);
3469 }
3470
3471 /* Check for DEMOLISH method - only set up destruction hook if class has one */
3472 {
3473 char demolish_name[256];
3474 CV *demolish_cv;
3475 306 snprintf(demolish_name, sizeof(demolish_name), "%s::DEMOLISH", class_pv);
3476 306 demolish_cv = get_cvn_flags(demolish_name, strlen(demolish_name), 0);
3477 306 ▶ if (demolish_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (demolish_cv) { 5 301 306
3478 5 meta->demolish_cv = demolish_cv;
3479 /* Install DESTROY wrapper that calls DEMOLISH */
3480 5 install_destroy_wrapper(aTHX_ class_pv, meta);
3481 }
3482 }
3483  
3484 /* Check for BUILD method - called after new() */
3485 {
3486 char build_name[256];
3487 CV *build_cv;
3488 306 snprintf(build_name, sizeof(build_name), "%s::BUILD", class_pv);
3489 306 build_cv = get_cvn_flags(build_name, strlen(build_name), 0);
3490 306 ▶ if (build_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (build_cv) { 1 305 306
3491 1 meta->build_cv = build_cv;
3492 1 meta->has_build = 1;
3493 }
3494 }
3495
3496 306 XSRETURN_EMPTY;
3497 }
3498  
3499 3015 XS_INTERNAL(xs_prototype) {
3500 3015 dXSARGS;
3501 AV *av;
3502 SV **svp;
3503
3504 3015 ▶ if (items < 1) croak("Usage: Object::Proto::prototype($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::prototype($obj)"); 1 3014 3015
3505
3506 3014 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 3013 1 3014
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 3012 3013
3507 2 croak("Not an object");
3508 }
3509 3012 av = (AV*)SvRV(ST(0));
3510 3012 svp = av_fetch(av, 0, 0);
3511 3012 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 3011 1 3012
if (svp && SvOK(*svp)) { 3009 2 3011
3512 3009 ST(0) = SvREFCNT_inc(*svp);
3513 } else {
3514 3 ST(0) = &PL_sv_undef;
3515 }
3516 3012 XSRETURN(1);
3517 }
3518  
3519 149 XS_INTERNAL(xs_set_prototype) {
3520 149 dXSARGS;
3521 AV *av;
3522 MAGIC *mg;
3523  
3524 149 ▶ if (items < 2) croak("Usage: Object::Proto::set_prototype($obj, $proto)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::set_prototype($obj, $proto)"); 1 148 149
3525  
3526 148 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 147 1 148
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 146 147
3527 2 croak("Not an object");
3528 }
3529 146 av = (AV*)SvRV(ST(0));
3530  
3531 146 mg = get_object_magic(aTHX_ ST(0));
3532 146 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 3 143 146
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 2 3
3533 1 croak("Cannot modify frozen object");
3534 }
3535  
3536 145 av_store(av, 0, newSVsv(ST(1)));
3537 145 XSRETURN_EMPTY;
3538 }
3539  
3540 /* Get the full prototype chain as an arrayref */
3541 10 XS_INTERNAL(xs_prototype_chain) {
3542 10 dXSARGS;
3543 AV *av;
3544 AV *chain;
3545 AV *visited[MAX_PROTOTYPE_DEPTH];
3546 10 int depth = 0;
3547 int i;
3548  
3549 10 ▶ if (items < 1) croak("Usage: Object::Proto::prototype_chain($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::prototype_chain($obj)"); 1 9 10
3550  
3551 9 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 8 1 9
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 7 8
3552 2 croak("Not an object");
3553 }
3554  
3555 7 chain = newAV();
3556 7 av = (AV*)SvRV(ST(0));
3557  
3558 /*
3559 # uncoverable branch true count:2
3560 # uncoverable condition right count:1 */
3561 112 ▶ while (av && depth < MAX_PROTOTYPE_DEPTH) {
Branch: 2 branches
BranchTrueFalseTotal
while (av && depth < MAX_PROTOTYPE_DEPTH) { 0 112 112
while (av && depth < MAX_PROTOTYPE_DEPTH) { 111 1 112
3562 SV **proto_svp;
3563  
3564 /* Check for circular reference */
3565 5066 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 4956 110 5066
3566 4956 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 1 4955 4956
3567 1 goto done; /* Cycle detected, stop */
3568 }
3569 }
3570 110 visited[depth] = av;
3571  
3572 /* Add this object to the chain */
3573 110 av_push(chain, newRV_inc((SV*)av));
3574  
3575 /* Follow prototype */
3576 110 proto_svp = av_fetch(av, 0, 0);
3577 /*
3578 # uncoverable branch true count:1 class:ignore_covered_err
3579 # uncoverable condition left count:1 */
3580 110 ▶ if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 0 110 110
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 4 106 110
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 1 105 106
3581 break;
3582 }
3583 105 av = (AV*)SvRV(*proto_svp);
3584 105 depth++;
3585 }
3586  
3587 6 done:
3588 7 ST(0) = sv_2mortal(newRV_noinc((SV*)chain));
3589 7 XSRETURN(1);
3590 }
3591  
3592 /* Check if object has a property in its own slots (not prototype) */
3593 14 XS_INTERNAL(xs_has_own_property) {
3594 14 dXSARGS;
3595 AV *av;
3596 SV **svp;
3597 const char *class_name;
3598 STRLEN class_len;
3599 ClassMeta *meta;
3600 const char *prop_name;
3601 STRLEN prop_len;
3602 SV **idx_sv;
3603  
3604 14 ▶ if (items < 2) croak("Usage: Object::Proto::has_own_property($obj, $property)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::has_own_property($obj, $property)"); 1 13 14
3605  
3606 13 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 12 1 13
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 11 12
3607 2 croak("Not an object");
3608 }
3609  
3610 11 av = (AV*)SvRV(ST(0));
3611 11 class_name = sv_reftype(SvRV(ST(0)), TRUE);
3612 11 class_len = strlen(class_name);
3613  
3614 11 meta = get_class_meta(aTHX_ class_name, class_len);
3615 11 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 10 11
3616 1 XSRETURN_NO;
3617 }
3618  
3619 10 prop_name = SvPV(ST(1), prop_len);
3620 10 idx_sv = hv_fetch(meta->prop_to_idx, prop_name, prop_len, 0);
3621 10 ▶ if (!idx_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_sv) { 1 9 10
3622 1 XSRETURN_NO;
3623 }
3624  
3625 /* Check if this slot has a defined value */
3626 9 svp = av_fetch(av, SvIV(*idx_sv), 0);
3627 9 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 8 1 9
if (svp && SvOK(*svp)) { 4 4 8
3628 4 XSRETURN_YES;
3629 }
3630 5 XSRETURN_NO;
3631 }
3632  
3633 /* Get the prototype depth (number of prototypes in chain) */
3634 10 XS_INTERNAL(xs_prototype_depth) {
3635 10 dXSARGS;
3636 AV *av;
3637 AV *visited[MAX_PROTOTYPE_DEPTH];
3638 10 int depth = 0;
3639 int i;
3640  
3641 10 ▶ if (items < 1) croak("Usage: Object::Proto::prototype_depth($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::prototype_depth($obj)"); 1 9 10
3642  
3643 9 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 8 1 9
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 7 8
3644 2 croak("Not an object");
3645 }
3646  
3647 7 av = (AV*)SvRV(ST(0));
3648  
3649 /*
3650 # uncoverable branch true count:2
3651 # uncoverable condition right count:1 */
3652 112 ▶ while (av && depth < MAX_PROTOTYPE_DEPTH) {
Branch: 2 branches
BranchTrueFalseTotal
while (av && depth < MAX_PROTOTYPE_DEPTH) { 0 112 112
while (av && depth < MAX_PROTOTYPE_DEPTH) { 111 1 112
3653 SV **proto_svp;
3654  
3655 /* Check for circular reference */
3656 5066 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 4956 110 5066
3657 4956 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 1 4955 4956
3658 1 goto done;
3659 }
3660 }
3661 110 visited[depth] = av;
3662  
3663 110 proto_svp = av_fetch(av, 0, 0);
3664 /*
3665 # uncoverable branch true count:1 class:ignore_covered_err
3666 # uncoverable condition left count:1 */
3667 110 ▶ if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 0 110 110
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 4 106 110
if (!proto_svp || !SvROK(*proto_svp) || SvTYPE(SvRV(*proto_svp)) != SVt_PVAV) { 1 105 106
3668 break;
3669 }
3670 105 av = (AV*)SvRV(*proto_svp);
3671 105 depth++;
3672 }
3673  
3674 6 done:
3675 7 XSRETURN_IV(depth);
3676 }
3677  
3678 416 XS_INTERNAL(xs_lock) {
3679 416 dXSARGS;
3680 MAGIC *mg;
3681
3682 416 ▶ if (items < 1) croak("Usage: Object::Proto::lock($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::lock($obj)"); 1 415 416
3683 415 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::lock");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 412 3 415
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 412 0 412
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 0 412 412
3684
3685 412 mg = get_object_magic(aTHX_ ST(0));
3686 412 ▶ if (!mg) mg = add_object_magic(aTHX_ ST(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) mg = add_object_magic(aTHX_ ST(0)); 12 400 412
3687 412 ▶ if (mg->mg_private & OBJ_FLAG_FROZEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private & OBJ_FLAG_FROZEN) { 1 411 412
3688 1 croak("Object is frozen");
3689 }
3690 411 mg->mg_private |= OBJ_FLAG_LOCKED;
3691 411 XSRETURN_EMPTY;
3692 }
3693  
3694 406 XS_INTERNAL(xs_unlock) {
3695 406 dXSARGS;
3696 MAGIC *mg;
3697
3698 406 ▶ if (items < 1) croak("Usage: Object::Proto::unlock($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::unlock($obj)"); 1 405 406
3699 405 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::unlock");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::unlock"); 404 1 405
VALIDATE_OBJECT(ST(0), "Object::Proto::unlock"); 404 0 404
VALIDATE_OBJECT(ST(0), "Object::Proto::unlock"); 0 404 404
3700
3701 404 mg = get_object_magic(aTHX_ ST(0));
3702 404 ▶ if (mg) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg) { 403 1 404
3703 403 ▶ if (mg->mg_private & OBJ_FLAG_FROZEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private & OBJ_FLAG_FROZEN) { 1 402 403
3704 1 croak("Cannot unlock frozen object");
3705 }
3706 402 mg->mg_private &= ~OBJ_FLAG_LOCKED;
3707 }
3708 403 XSRETURN_EMPTY;
3709 }
3710  
3711 18 XS_INTERNAL(xs_freeze) {
3712 18 dXSARGS;
3713 MAGIC *mg;
3714
3715 18 ▶ if (items < 1) croak("Usage: Object::Proto::freeze($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::freeze($obj)"); 1 17 18
3716 17 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::freeze");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 16 1 17
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 16 0 16
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 1 15 16
3717
3718 15 mg = get_object_magic(aTHX_ ST(0));
3719 15 ▶ if (!mg) mg = add_object_magic(aTHX_ ST(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) mg = add_object_magic(aTHX_ ST(0)); 14 1 15
3720 15 mg->mg_private |= (OBJ_FLAG_FROZEN | OBJ_FLAG_LOCKED);
3721 15 XSRETURN_EMPTY;
3722 }
3723  
3724 3012 XS_INTERNAL(xs_is_frozen) {
3725 3012 dXSARGS;
3726 MAGIC *mg;
3727
3728 3012 ▶ if (items < 1) croak("Usage: Object::Proto::is_frozen($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::is_frozen($obj)"); 1 3011 3012
3729 3011 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::is_frozen");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::is_frozen"); 3010 1 3011
VALIDATE_OBJECT(ST(0), "Object::Proto::is_frozen"); 3010 0 3010
VALIDATE_OBJECT(ST(0), "Object::Proto::is_frozen"); 0 3010 3010
3730
3731 3010 mg = get_object_magic(aTHX_ ST(0));
3732 3010 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 3007 3 3010
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 3006 1 3007
3733 3006 XSRETURN_YES;
3734 }
3735 4 XSRETURN_NO;
3736 }
3737  
3738 3420 XS_INTERNAL(xs_is_locked) {
3739 3420 dXSARGS;
3740 MAGIC *mg;
3741  
3742 3420 ▶ if (items < 1) croak("Usage: Object::Proto::is_locked($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::is_locked($obj)"); 1 3419 3420
3743 3419 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::is_locked");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::is_locked"); 3418 1 3419
VALIDATE_OBJECT(ST(0), "Object::Proto::is_locked"); 3418 0 3418
VALIDATE_OBJECT(ST(0), "Object::Proto::is_locked"); 0 3418 3418
3744  
3745 3418 mg = get_object_magic(aTHX_ ST(0));
3746 3418 ▶ if (mg && (mg->mg_private & OBJ_FLAG_LOCKED)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_LOCKED)) { 2411 1007 3418
if (mg && (mg->mg_private & OBJ_FLAG_LOCKED)) { 2409 2 2411
3747 2409 XSRETURN_YES;
3748 }
3749 1009 XSRETURN_NO;
3750 }
3751  
3752 /* ============================================
3753 Introspection API
3754 ============================================ */
3755  
3756 /* Deep clone an SV, recursing into refs.
3757 * seen_hv maps refaddr strings -> cloned SV* (handles circular refs).
3758 * Returns a mortal SV. */
3759 27545 static SV* deep_clone_sv(pTHX_ SV *src, HV *seen_hv) {
3760 SV *dst;
3761 char addr_buf[32];
3762 STRLEN addr_len;
3763 SV **cached;
3764  
3765 /* Non-references: return a plain copy */
3766 27545 ▶ if (!SvROK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) { 15279 12266 27545
3767 15279 return newSVsv(src);
3768 }
3769  
3770 /* Check seen table to break circular references */
3771 12266 addr_len = (STRLEN)sprintf(addr_buf, "%p", (void*)SvRV(src));
3772 12266 cached = hv_fetch(seen_hv, addr_buf, (I32)addr_len, 0);
3773 12266 ▶ if (cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached) { 1 12265 12266
3774 1 return SvREFCNT_inc(*cached);
3775 }
3776  
3777 12265 ▶ if (SvTYPE(SvRV(src)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(src)) == SVt_PVAV) { 12254 11 12265
3778 /* Array ref (possibly blessed) */
3779 12254 AV *src_av = (AV*)SvRV(src);
3780 12254 AV *dst_av = newAV();
3781 12254 IV i, len = av_len(src_av);
3782  
3783 12254 dst = newRV_noinc((SV*)dst_av);
3784 12254 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 6630 5624 12254
3785 6630 sv_bless(dst, SvSTASH(SvRV(src)));
3786  
3787 /* Register before recursing to handle circular refs */
3788 12254 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
3789  
3790 12254 av_extend(dst_av, len);
3791 39791 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 27537 12254 39791
3792 27537 SV **svp = av_fetch(src_av, i, 0);
3793 48432 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 27535 2 27537
if (svp && SvOK(*svp)) { 20895 6640 27535
3794 20895 SV *child = deep_clone_sv(aTHX_ *svp, seen_hv);
3795 20895 av_store(dst_av, i, child);
3796 } else {
3797 6642 av_store(dst_av, i, newSV(0));
3798 }
3799 }
3800  
3801 11 ▶ } else if (SvTYPE(SvRV(src)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(src)) == SVt_PVHV) { 7 4 11
3802 /* Hash ref (possibly blessed) */
3803 7 HV *src_hv = (HV*)SvRV(src);
3804 7 HV *dst_hv = newHV();
3805 HE *he;
3806  
3807 7 dst = newRV_noinc((SV*)dst_hv);
3808 7 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 1 6 7
3809 1 sv_bless(dst, SvSTASH(SvRV(src)));
3810  
3811 7 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
3812  
3813 7 hv_iterinit(src_hv);
3814 18 ▶ while ((he = hv_iternext(src_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src_hv))) { 11 7 18
3815 STRLEN klen;
3816 11 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 11 11
3817 11 SV *val = HeVAL(he);
3818 11 SV *copy = deep_clone_sv(aTHX_ val, seen_hv);
3819 11 hv_store(dst_hv, key, (I32)klen, copy, 0);
3820 }
3821  
3822 4 ▶ } else if (SvTYPE(SvRV(src)) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(src)) < SVt_PVAV) { 2 2 4
3823 /* Scalar ref */
3824 2 SV *inner = deep_clone_sv(aTHX_ SvRV(src), seen_hv);
3825 2 dst = newRV_noinc(inner);
3826 2 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 1 1 2
3827 1 sv_bless(dst, SvSTASH(SvRV(src)));
3828 2 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
3829  
3830 } else {
3831 /* Code refs, globs, etc. — share as-is */
3832 2 dst = newSVsv(src);
3833 2 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
3834 }
3835  
3836 12265 return dst;
3837 }
3838  
3839 /* Object::Proto::clone($obj) - deep clone an object, arrayref, hashref,
3840 * scalarref, or plain scalar */
3841 6644 XS_INTERNAL(xs_clone) {
3842 6644 dXSARGS;
3843 SV *src;
3844  
3845 6644 ▶ if (items < 1) croak("Usage: Object::Proto::clone($val) or $obj->clone()");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::clone($val) or $obj->clone()"); 1 6643 6644
3846  
3847 6643 src = ST(0);
3848  
3849 /* Plain scalar (non-ref): return a copy of the value */
3850 6643 ▶ if (!SvROK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) { 6 6637 6643
3851 6 ▶ if (SvOK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(src)) { 4 2 6
3852 4 ST(0) = sv_2mortal(newSVsv(src));
3853 } else {
3854 2 ST(0) = &PL_sv_undef;
3855 }
3856 6 XSRETURN(1);
3857 }
3858  
3859 {
3860 6637 HV *seen_hv = newHV();
3861 SV *dst;
3862  
3863 /* For blessed objects backed by an AV: strip frozen/locked magic
3864 * by cloning the underlying AV fresh (deep_clone_sv handles the
3865 * bless but the new ref carries no Object::Proto magic). */
3866 6637 dst = deep_clone_sv(aTHX_ src, seen_hv);
3867 6637 SvREFCNT_dec((SV*)seen_hv);
3868  
3869 6637 ST(0) = sv_2mortal(dst);
3870 6637 XSRETURN(1);
3871 }
3872 }
3873  
3874 /* Object::Proto::properties($class) - return property names for a class */
3875 10624 XS_INTERNAL(xs_properties) {
3876 10624 dXSARGS;
3877 STRLEN class_len;
3878 const char *class_pv;
3879 ClassMeta *meta;
3880 IV i;
3881  
3882 10624 ▶ if (items < 1) croak("Usage: Object::Proto::properties($class)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::properties($class)"); 1 10623 10624
3883  
3884 10623 class_pv = SvPV(ST(0), class_len);
3885  
3886 10623 meta = get_class_meta(aTHX_ class_pv, class_len);
3887 10623 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 4004 6619 10623
3888 /* Non-existent class: return empty list / 0 */
3889 4004 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2002 2002 4004
3890 2002 XSRETURN_EMPTY;
3891 } else {
3892 2002 XSRETURN_IV(0);
3893 }
3894 }
3895  
3896 6619 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 4618 2001 6619
3897 /* List context: return property names */
3898 4618 IV count = meta->slot_count - 1; /* -1 because slot 0 is prototype */
3899 4618 SP -= items;
3900 4618 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 4618 0 4618
EXTEND(SP, count); 0 4618 4618
3901  
3902 18481 ▶ for (i = 1; i < meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < meta->slot_count; i++) { 13863 4618 18481
3903 /*
3904 # uncoverable branch false
3905 # uncoverable condition right count:1 */
3906 13863 ▶ if (meta->idx_to_prop[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->idx_to_prop[i]) { 13863 0 13863
3907 13863 PUSHs(sv_2mortal(newSVpv(meta->idx_to_prop[i], 0)));
3908 }
3909 }
3910 4618 XSRETURN(count);
3911 } else {
3912 /* Scalar context: return count */
3913 2001 XSRETURN_IV(meta->slot_count - 1);
3914 }
3915 }
3916  
3917 /* Object::Proto::slot_info($class, $property) - return hashref with slot metadata */
3918 16849 XS_INTERNAL(xs_slot_info) {
3919 16849 dXSARGS;
3920 STRLEN class_len, prop_len;
3921 const char *class_pv, *prop_pv;
3922 ClassMeta *meta;
3923 SV **idx_svp;
3924 IV idx;
3925 SlotSpec *spec;
3926 HV *info;
3927  
3928 16849 ▶ if (items < 2) croak("Usage: Object::Proto::slot_info($class, $property)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::slot_info($class, $property)"); 1 16848 16849
3929  
3930 16848 class_pv = SvPV(ST(0), class_len);
3931 16848 ▶ prop_pv = SvPV(ST(1), prop_len);
Branch: 1 branch
BranchTrueFalseTotal
prop_pv = SvPV(ST(1), prop_len); 16848 0 16848
3932  
3933 /* Look up class meta */
3934 16848 meta = get_class_meta(aTHX_ class_pv, class_len);
3935 16848 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 2003 14845 16848
3936 2003 XSRETURN_UNDEF;
3937 }
3938  
3939 /* Look up property index - O(1) hash lookup */
3940 14845 idx_svp = hv_fetch(meta->prop_to_idx, prop_pv, prop_len, 0);
3941 14845 ▶ if (!idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_svp) { 2003 12842 14845
3942 2003 XSRETURN_UNDEF;
3943 }
3944 12842 idx = SvIV(*idx_svp);
3945  
3946 /* Build result hashref */
3947 12842 info = newHV();
3948  
3949 /* Basic info always present */
3950 12842 hv_store(info, "name", 4, newSVpv(prop_pv, prop_len), 0);
3951 12842 hv_store(info, "index", 5, newSViv(idx), 0);
3952  
3953 /* Get slot spec if available */
3954 /*
3955 # uncoverable condition right count:1
3956 # uncoverable condition right count:2 */
3957 12842 ▶ spec = (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
spec = (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL; 12842 0 12842
spec = (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL; 12842 0 12842
3958  
3959 12842 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 12842 0 12842
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 10826 2016 12842
3960 const char *type_name;
3961 10826 ▶ if (spec->type_id == TYPE_CUSTOM && spec->registered) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec->type_id == TYPE_CUSTOM && spec->registered) { /* # uncoverable condition right count:2 */ 1 10825 10826
if (spec->type_id == TYPE_CUSTOM && spec->registered) { /* # uncoverable condition right count:2 */ 1 0 1
3962 1 type_name = spec->registered->name;
3963 } else {
3964 10825 type_name = type_id_to_name(spec->type_id);
3965 }
3966 10826 hv_store(info, "type", 4, newSVpv(type_name, 0), 0);
3967 }
3968  
3969 /* Boolean flags */
3970 12842 ▶ hv_store(info, "is_required", 11, newSViv(spec ? spec->is_required : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "is_required", 11, newSViv(spec ? spec->is_required : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3971 12842 ▶ hv_store(info, "is_readonly", 11, newSViv(spec ? spec->is_readonly : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "is_readonly", 11, newSViv(spec ? spec->is_readonly : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3972 12842 ▶ hv_store(info, "is_lazy", 7, newSViv(spec ? spec->is_lazy : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "is_lazy", 7, newSViv(spec ? spec->is_lazy : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3973 12842 ▶ hv_store(info, "is_weak", 7, newSViv(spec ? spec->is_weak : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "is_weak", 7, newSViv(spec ? spec->is_weak : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3974 12842 ▶ hv_store(info, "has_default", 11, newSViv(spec ? spec->has_default : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_default", 11, newSViv(spec ? spec->has_default : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3975 12842 ▶ hv_store(info, "has_trigger", 11, newSViv(spec ? spec->has_trigger : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_trigger", 11, newSViv(spec ? spec->has_trigger : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3976 12842 ▶ hv_store(info, "has_coerce", 10, newSViv(spec ? spec->has_coerce : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_coerce", 10, newSViv(spec ? spec->has_coerce : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3977 12842 ▶ hv_store(info, "has_builder", 11, newSViv(spec ? spec->has_builder : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_builder", 11, newSViv(spec ? spec->has_builder : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3978 12842 ▶ hv_store(info, "has_clearer", 11, newSViv(spec ? spec->has_clearer : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_clearer", 11, newSViv(spec ? spec->has_clearer : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3979 12842 ▶ hv_store(info, "has_predicate", 13, newSViv(spec ? spec->has_predicate : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_predicate", 13, newSViv(spec ? spec->has_predicate : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3980 12842 ▶ hv_store(info, "has_type", 8, newSViv(spec ? spec->has_type : 0), 0); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
hv_store(info, "has_type", 8, newSViv(spec ? spec->has_type : 0), 0); /* # uncoverable condition right count:1 */ 12842 0 12842
3981  
3982 /* Default value (if present) */
3983 12842 ▶ if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 12842 0 12842
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 5206 7636 12842
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 5205 1 5206
3984 5205 hv_store(info, "default", 7, newSVsv(spec->default_sv), 0);
3985 }
3986  
3987 /* Builder method name */
3988 /*
3989 # uncoverable condition right count:1
3990 # uncoverable condition right count:3 */
3991 12842 ▶ if (spec && spec->has_builder && spec->builder_name) {
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_builder && spec->builder_name) { 12842 0 12842
if (spec && spec->has_builder && spec->builder_name) { 2 12840 12842
if (spec && spec->has_builder && spec->builder_name) { 2 0 2
3992 2 hv_store(info, "builder", 7, newSVsv(spec->builder_name), 0);
3993 }
3994  
3995 /* init_arg (if specified) */
3996 12842 ▶ if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */ 12842 0 12842
if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */ 2 12840 12842
3997 2 hv_store(info, "init_arg", 8, newSVsv(spec->init_arg), 0);
3998 }
3999  
4000 12842 ST(0) = sv_2mortal(newRV_noinc((SV*)info));
4001 12842 XSRETURN(1);
4002 }
4003  
4004 /* Object::Proto::parent($class) - return parent class name or undef */
4005 12 XS_INTERNAL(xs_parent) {
4006 12 dXSARGS;
4007 STRLEN class_len;
4008 const char *class_pv;
4009 ClassMeta *meta;
4010  
4011 12 ▶ if (items < 1) croak("Usage: Object::Proto::parent($class)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::parent($class)"); 1 11 12
4012  
4013 11 class_pv = SvPV(ST(0), class_len);
4014 11 meta = get_class_meta(aTHX_ class_pv, class_len);
4015  
4016 11 ▶ if (!meta || meta->parent_count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!meta || meta->parent_count == 0) { 10 1 11
if (!meta || meta->parent_count == 0) { 4 6 10
4017 5 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2 3 5
4018 2 XSRETURN_EMPTY;
4019 }
4020 3 XSRETURN_UNDEF;
4021 }
4022  
4023 6 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2 4 6
4024 /* List context: return all parents */
4025 IV i;
4026 2 SP -= items;
4027 2 ▶ EXTEND(SP, meta->parent_count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, meta->parent_count); 2 0 2
EXTEND(SP, meta->parent_count); 0 2 2
4028 5 ▶ for (i = 0; i < meta->parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->parent_count; i++) { 3 2 5
4029 3 PUSHs(sv_2mortal(newSVpv(meta->parent_classes[i], 0)));
4030 }
4031 2 XSRETURN(meta->parent_count);
4032 } else {
4033 /* Scalar context: return first parent */
4034 4 ST(0) = sv_2mortal(newSVpv(meta->parent_classes[0], 0));
4035 4 XSRETURN(1);
4036 }
4037 }
4038  
4039 /* Object::Proto::ancestors($class) - return list of all ancestor class names (breadth-first) */
4040 8 XS_INTERNAL(xs_ancestors) {
4041 8 dXSARGS;
4042 STRLEN class_len;
4043 const char *class_pv;
4044 ClassMeta *meta;
4045 AV *result;
4046 HV *seen;
4047 AV *queue;
4048 8 IV count = 0;
4049  
4050 8 ▶ if (items < 1) croak("Usage: Object::Proto::ancestors($class)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::ancestors($class)"); 1 7 8
4051  
4052 7 class_pv = SvPV(ST(0), class_len);
4053 7 meta = get_class_meta(aTHX_ class_pv, class_len);
4054  
4055 7 SP -= items;
4056  
4057 7 ▶ if (meta && meta->parent_count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (meta && meta->parent_count > 0) { 6 1 7
if (meta && meta->parent_count > 0) { 5 1 6
4058 IV i;
4059 5 result = newAV();
4060 5 seen = newHV();
4061 5 queue = newAV();
4062  
4063 /* Seed queue with direct parents */
4064 13 ▶ for (i = 0; i < meta->parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->parent_count; i++) { 8 5 13
4065 8 av_push(queue, newSVpv(meta->parent_metas[i]->class_name, 0));
4066 }
4067  
4068 /* BFS traversal */
4069 17 ▶ while (av_len(queue) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(queue) >= 0) { 12 5 17
4070 12 SV *cur_sv = av_shift(queue);
4071 STRLEN cur_len;
4072 12 const char *cur_name = SvPV(cur_sv, cur_len);
4073 ClassMeta *cur_meta;
4074  
4075 /* Skip if already seen */
4076 12 ▶ if (hv_exists(seen, cur_name, cur_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, cur_name, cur_len)) { 1 11 12
4077 1 SvREFCNT_dec(cur_sv);
4078 1 continue;
4079 }
4080 11 hv_store(seen, cur_name, cur_len, &PL_sv_yes, 0);
4081 11 av_push(result, cur_sv);
4082  
4083 /* Enqueue this class's parents */
4084 11 cur_meta = get_class_meta(aTHX_ cur_name, cur_len);
4085 /*
4086 # uncoverable branch false
4087 # uncoverable condition right count:1 */
4088 11 ▶ if (cur_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_meta) { 11 0 11
4089 16 ▶ for (i = 0; i < cur_meta->parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cur_meta->parent_count; i++) { 5 11 16
4090 5 const char *pname = cur_meta->parent_classes[i];
4091 5 ▶ if (!hv_exists(seen, pname, strlen(pname))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(seen, pname, strlen(pname))) { 4 1 5
4092 4 av_push(queue, newSVpv(pname, 0));
4093 }
4094 }
4095 }
4096 }
4097  
4098 5 count = av_len(result) + 1;
4099 5 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 5 0 5
EXTEND(SP, count); 0 5 5
4100 16 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 11 5 16
4101 11 SV **elem = av_fetch(result, i, 0);
4102 /*
4103 # uncoverable branch false
4104 # uncoverable condition right count:1 */
4105 11 ▶ if (elem) PUSHs(sv_2mortal(newSVsv(*elem)));
Branch: 1 branch
BranchTrueFalseTotal
if (elem) PUSHs(sv_2mortal(newSVsv(*elem))); 11 0 11
4106 }
4107  
4108 5 SvREFCNT_dec((SV*)result);
4109 5 SvREFCNT_dec((SV*)seen);
4110 5 SvREFCNT_dec((SV*)queue);
4111 }
4112  
4113 7 XSRETURN(count);
4114 }
4115  
4116 /* ============================================
4117 Global cleanup
4118 ============================================ */
4119  
4120 /* Cleanup during global destruction */
4121 70 static void object_cleanup_globals(pTHX_ void *data) {
4122 PERL_UNUSED_ARG(data);
4123  
4124 /* During global destruction, just NULL out pointers.
4125 * Perl handles SV cleanup. Trying to free them ourselves
4126 * can cause crashes due to destruction order. */
4127 /*
4128 # uncoverable branch false
4129 # uncoverable condition right count:1 */
4130 70 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 70 0 70
4131 70 g_type_registry = NULL;
4132 70 g_class_registry = NULL;
4133 70 g_func_accessor_registry = NULL;
4134 70 return;
4135 }
4136  
4137 /* Normal cleanup - not during global destruction */
4138 /* Note: Full cleanup omitted for simplicity; Perl handles SV refcounts */
4139 0 g_type_registry = NULL; /* # uncoverable statement */
4140 0 g_class_registry = NULL; /* # uncoverable statement */
4141 0 g_func_accessor_registry = NULL; /* # uncoverable statement */
4142 }
4143  
4144 /* ============================================
4145 Type Registry API
4146 ============================================ */
4147  
4148 /* C-level registration for external XS modules (called from BOOT)
4149 This is the fast path - no Perl callback overhead */
4150 4 OBJECT_API void object_register_type_xs(pTHX_ const char *name,
4151 ObjectTypeCheckFunc check,
4152 ObjectTypeCoerceFunc coerce) {
4153 RegisteredType *type;
4154 4 STRLEN name_len = strlen(name);
4155
4156 /*
4157 # uncoverable branch true
4158 # uncoverable condition left count:1 */
4159 4 ▶ if (!g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) { 0 4 4
4160 0 g_type_registry = newHV(); /* # uncoverable statement */
4161 }
4162
4163 /* Check if already registered */
4164 4 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
4165 4 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 3 4
4166 1 croak("Type '%s' is already registered", name);
4167 }
4168
4169 3 Newxz(type, 1, RegisteredType);
4170 3 Newx(type->name, name_len + 1, char);
4171 3 Copy(name, type->name, name_len, char);
4172 3 type->name[name_len] = '\0';
4173
4174 3 type->check = check; /* Direct C function pointer - no Perl overhead */
4175 3 type->coerce = coerce; /* Direct C function pointer - no Perl overhead */
4176 3 type->perl_check = NULL;
4177 3 type->perl_coerce = NULL;
4178 3 type->check_ex = NULL;
4179 3 type->coerce_ex = NULL;
4180 3 type->data = NULL;
4181
4182 3 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
4183 3 }
4184  
4185 /* C-level registration with user-data for external XS modules (called from BOOT)
4186 Allows a single pair of C functions to serve multiple dynamically-created types */
4187 3 OBJECT_API void object_register_type_xs_ex(pTHX_ const char *name,
4188 ObjectTypeCheckFuncEx check,
4189 ObjectTypeCoerceFuncEx coerce,
4190 void *data) {
4191 RegisteredType *type;
4192 3 STRLEN name_len = strlen(name);
4193
4194 /*
4195 # uncoverable branch true
4196 # uncoverable condition left count:1 */
4197 3 ▶ if (!g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) { 0 3 3
4198 0 g_type_registry = newHV(); /* # uncoverable statement */
4199 }
4200
4201 3 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
4202 3 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 2 3
4203 1 croak("Type '%s' is already registered", name);
4204 }
4205
4206 2 Newxz(type, 1, RegisteredType);
4207 2 Newx(type->name, name_len + 1, char);
4208 2 Copy(name, type->name, name_len, char);
4209 2 type->name[name_len] = '\0';
4210
4211 2 type->check = NULL;
4212 2 type->coerce = NULL;
4213 2 type->perl_check = NULL;
4214 2 type->perl_coerce = NULL;
4215 2 type->check_ex = check;
4216 2 type->coerce_ex = coerce;
4217 2 type->data = data;
4218
4219 2 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
4220 2 }
4221  
4222 /* Getter for external modules to look up a registered type */
4223 2 OBJECT_API RegisteredType* object_get_registered_type(pTHX_ const char *name) {
4224 2 STRLEN name_len = strlen(name);
4225 /*
4226 # uncoverable branch true
4227 # uncoverable condition left count:1 */
4228 2 ▶ if (!g_type_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) return NULL; 0 2 2
4229
4230 2 SV **svp = hv_fetch(g_type_registry, name, name_len, 0);
4231 2 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 1 1 2
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 1 0 1
4232 1 return INT2PTR(RegisteredType*, SvIV(*svp));
4233 }
4234 1 return NULL;
4235 }
4236  
4237 /* Object::Proto::register_type($name, $check_cb [, $coerce_cb]) */
4238 /* Object::Proto::register_simple_custom_op($xop_name) - let a custom op from
4239 * another module be used as an accessor operand without deoptimising the call.
4240 * The op must be free of side effects and leave exactly one value on the
4241 * stack; nothing here can check that, so registering is a promise. */
4242 16 XS_INTERNAL(xs_register_simple_custom_op) {
4243 16 dXSARGS;
4244 STRLEN len;
4245 const char *name;
4246 int i;
4247  
4248 16 ▶ if (items != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) 2 14 16
4249 2 croak("Usage: Object::Proto::register_simple_custom_op($xop_name)");
4250  
4251 14 name = SvPV(ST(0), len);
4252 14 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 2 12 14
4253 2 croak("register_simple_custom_op: name must not be empty");
4254  
4255 53 ▶ for (i = 0; i < g_simple_custom_op_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_simple_custom_op_count; i++) { 44 9 53
4256 44 ▶ if (strEQ(name, g_simple_custom_ops[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, g_simple_custom_ops[i])) 3 41 44
4257 3 XSRETURN_YES; /* already registered */
4258 }
4259  
4260 9 ▶ if (g_simple_custom_op_count >= OBJECT_MAX_SIMPLE_CUSTOM)
Branch: 1 branch
BranchTrueFalseTotal
if (g_simple_custom_op_count >= OBJECT_MAX_SIMPLE_CUSTOM) 1 8 9
4261 1 croak("register_simple_custom_op: registry is full (max %d)",
4262 OBJECT_MAX_SIMPLE_CUSTOM);
4263  
4264 8 g_simple_custom_ops[g_simple_custom_op_count++] = savepvn(name, len);
4265 8 XSRETURN_YES;
4266 }
4267  
4268 14 XS_INTERNAL(xs_register_type) {
4269 14 dXSARGS;
4270 STRLEN name_len;
4271 const char *name;
4272 RegisteredType *type;
4273
4274 14 ▶ if (items < 2) croak("Usage: Object::Proto::register_type($name, $check_cb [, $coerce_cb])");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::register_type($name, $check_cb [, $coerce_cb])"); 1 13 14
4275
4276 13 name = SvPV(ST(0), name_len);
4277
4278 /* Check if already registered */
4279 /*
4280 # uncoverable branch false
4281 # uncoverable condition right count:1 */
4282 13 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 13 0 13
4283 13 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
4284 13 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 2 11 13
4285 2 croak("Type '%s' is already registered", name);
4286 }
4287 } else {
4288 0 g_type_registry = newHV(); /* # uncoverable statement */
4289 }
4290
4291 11 Newxz(type, 1, RegisteredType);
4292 11 Newx(type->name, name_len + 1, char);
4293 11 Copy(name, type->name, name_len, char);
4294 11 type->name[name_len] = '\0';
4295
4296 /* Store Perl check callback */
4297 11 type->perl_check = newSVsv(ST(1));
4298 11 SvREFCNT_inc(type->perl_check);
4299
4300 /* Store Perl coerce callback if provided */
4301 11 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 4 7 11
if (items > 2 && SvOK(ST(2))) { 3 1 4
4302 3 type->perl_coerce = newSVsv(ST(2));
4303 3 SvREFCNT_inc(type->perl_coerce);
4304 }
4305
4306 11 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
4307
4308 11 XSRETURN_YES;
4309 }
4310  
4311 /* Object::Proto::has_type($name) - check if a type is registered */
4312 4012 XS_INTERNAL(xs_has_type) {
4313 4012 dXSARGS;
4314 STRLEN name_len;
4315 const char *name;
4316
4317 4012 ▶ if (items < 1) croak("Usage: Object::Proto::has_type($name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::has_type($name)"); 1 4011 4012
4318
4319 4011 name = SvPV(ST(0), name_len);
4320
4321 /* Check built-in types */
4322 4011 BuiltinTypeID builtin = parse_builtin_type(name, name_len);
4323 4011 ▶ if (builtin != TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin != TYPE_NONE) { 3003 1008 4011
4324 3003 XSRETURN_YES;
4325 }
4326
4327 /* Check registry */
4328 /*
4329 # uncoverable branch false
4330 # uncoverable condition right count:1 */
4331 1008 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 1008 0 1008
4332 1008 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
4333 1008 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 7 1001 1008
4334 7 XSRETURN_YES;
4335 }
4336 }
4337
4338 1001 XSRETURN_NO;
4339 }
4340  
4341 /* Object::Proto::list_types() - return list of registered type names */
4342 2402 XS_INTERNAL(xs_list_types) {
4343 2402 dXSARGS;
4344 2402 AV *result = newAV();
4345
4346 PERL_UNUSED_ARG(items);
4347
4348 /* Add built-in types */
4349 2402 av_push(result, newSVpvs("Any"));
4350 2402 av_push(result, newSVpvs("Defined"));
4351 2402 av_push(result, newSVpvs("Str"));
4352 2402 av_push(result, newSVpvs("Int"));
4353 2402 av_push(result, newSVpvs("Num"));
4354 2402 av_push(result, newSVpvs("Bool"));
4355 2402 av_push(result, newSVpvs("ArrayRef"));
4356 2402 av_push(result, newSVpvs("HashRef"));
4357 2402 av_push(result, newSVpvs("CodeRef"));
4358 2402 av_push(result, newSVpvs("Object"));
4359
4360 /* Add registered types */
4361 /*
4362 # uncoverable branch false
4363 # uncoverable condition right count:1 */
4364 2402 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 2402 0 2402
4365 HE *he;
4366 2402 hv_iterinit(g_type_registry);
4367 2406 ▶ while ((he = hv_iternext(g_type_registry))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(g_type_registry))) { 4 2402 2406
4368 4 av_push(result, newSVsv(hv_iterkeysv(he)));
4369 }
4370 }
4371
4372 2402 ST(0) = newRV_noinc((SV*)result);
4373 2402 sv_2mortal(ST(0));
4374 2402 XSRETURN(1);
4375 }
4376  
4377 /* ============================================
4378 Singleton support
4379 ============================================ */
4380  
4381 /* XS implementation of instance() method for singletons */
4382 8055 XS_INTERNAL(xs_singleton_instance) {
4383 8055 dXSARGS;
4384 8055 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
4385  
4386 PERL_UNUSED_ARG(items);
4387  
4388 /*
4389 # uncoverable branch true
4390 # uncoverable condition left count:1 */
4391 8055 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 0 8055 8055
4392 0 croak("Singleton metadata not found"); /* # uncoverable statement */
4393 }
4394  
4395 /* Return cached instance if it exists */
4396 8055 ▶ if (meta->singleton_instance && SvOK(meta->singleton_instance)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (meta->singleton_instance && SvOK(meta->singleton_instance)) { /* # uncoverable condition right count:2 */ 8046 9 8055
if (meta->singleton_instance && SvOK(meta->singleton_instance)) { /* # uncoverable condition right count:2 */ 8046 0 8046
4397 8046 ST(0) = meta->singleton_instance;
4398 8046 XSRETURN(1);
4399 }
4400  
4401 /* Create new instance */
4402 {
4403 9 dSP;
4404 int count;
4405 SV *obj;
4406 GV *build_gv;
4407 char full_build[256];
4408  
4409 9 ENTER;
4410 9 SAVETMPS;
4411  
4412 /* Call ClassName->new() */
4413 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
4414 9 ▶ XPUSHs(sv_2mortal(newSVpv(meta->class_name, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(meta->class_name, 0))); 0 9 9
4415 9 PUTBACK;
4416  
4417 9 count = call_method("new", G_SCALAR);
4418  
4419 9 SPAGAIN;
4420  
4421 /*
4422 # uncoverable branch true
4423 # uncoverable condition left count:1 */
4424 9 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 9 9
4425 0 croak("Singleton new() did not return object"); /* # uncoverable statement */
4426 }
4427  
4428 9 obj = POPs;
4429 9 SvREFCNT_inc(obj); /* Keep the object alive */
4430  
4431 9 PUTBACK;
4432  
4433 /* Check for BUILD method and call it */
4434 9 snprintf(full_build, sizeof(full_build), "%s::BUILD", meta->class_name);
4435 9 build_gv = gv_fetchpv(full_build, 0, SVt_PVCV);
4436 9 ▶ if (build_gv && GvCV(build_gv)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (build_gv && GvCV(build_gv)) { /* # uncoverable condition right count:1 */ 9 0 9
if (build_gv && GvCV(build_gv)) { /* # uncoverable condition right count:1 */ 5 4 9
4437 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
4438 5 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 5 5
4439 5 PUTBACK;
4440 5 call_method("BUILD", G_VOID | G_DISCARD);
4441 }
4442  
4443 /* Cache the instance */
4444 9 meta->singleton_instance = obj;
4445  
4446 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
4447 9 LEAVE;
4448  
4449 9 ST(0) = obj;
4450 9 XSRETURN(1);
4451 }
4452 }
4453  
4454 /* ============================================
4455 Role API
4456 ============================================ */
4457  
4458 /* Object::Proto::role("RoleName", @slot_specs) - define a role */
4459 11 XS_INTERNAL(xs_role) {
4460 11 dXSARGS;
4461 STRLEN role_len;
4462 const char *role_pv;
4463 RoleMeta *meta;
4464 IV i;
4465
4466 11 ▶ if (items < 1) croak("Usage: Object::Proto::role($role_name, @slot_specs)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::role($role_name, @slot_specs)"); 1 10 11
4467
4468 10 role_pv = SvPV(ST(0), role_len);
4469
4470 /* Check if role already exists */
4471 10 meta = get_role_meta(aTHX_ role_pv, role_len);
4472 10 ▶ if (meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta) { 1 9 10
4473 1 croak("Role '%s' already defined", role_pv);
4474 }
4475
4476 /* Create role meta */
4477 9 Newxz(meta, 1, RoleMeta);
4478 9 Newxz(meta->role_name, role_len + 1, char);
4479 9 Copy(role_pv, meta->role_name, role_len, char);
4480 9 meta->role_name[role_len] = '\0';
4481 9 meta->stash = gv_stashpvn(role_pv, role_len, GV_ADD);
4482
4483 /* Allocate slots array */
4484 9 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 8 1 9
4485 8 Newx(meta->slots, items - 1, SlotSpec*);
4486 8 meta->slot_count = 0;
4487
4488 31 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 23 8 31
4489 STRLEN spec_len;
4490 23 const char *spec_pv = SvPV(ST(i), spec_len);
4491 23 SlotSpec *spec = parse_slot_spec(aTHX_ spec_pv, spec_len);
4492 23 meta->slots[meta->slot_count++] = spec;
4493 }
4494 }
4495
4496 9 register_role_meta(aTHX_ role_pv, role_len, meta);
4497
4498 9 XSRETURN_EMPTY;
4499 }
4500  
4501 /* Object::Proto::requires("RoleName", @method_names) - declare required methods */
4502 4 XS_INTERNAL(xs_requires) {
4503 4 dXSARGS;
4504 STRLEN role_len;
4505 const char *role_pv;
4506 RoleMeta *meta;
4507 IV i;
4508
4509 4 ▶ if (items < 2) croak("Usage: Object::Proto::requires($role_name, @method_names)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::requires($role_name, @method_names)"); 1 3 4
4510
4511 3 role_pv = SvPV(ST(0), role_len);
4512 3 meta = get_role_meta(aTHX_ role_pv, role_len);
4513 3 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 2 3
4514 1 croak("Role '%s' not defined", role_pv);
4515 }
4516
4517 /* Add required methods */
4518 2 ▶ Renew(meta->required_methods, meta->required_count + items - 1, char*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(meta->required_methods, meta->required_count + items - 1, char*); 0 2 2
4519 4 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 2 2 4
4520 STRLEN name_len;
4521 2 const char *name_pv = SvPV(ST(i), name_len);
4522 2 Newx(meta->required_methods[meta->required_count], name_len + 1, char);
4523 2 Copy(name_pv, meta->required_methods[meta->required_count], name_len, char);
4524 2 meta->required_methods[meta->required_count][name_len] = '\0';
4525 2 meta->required_count++;
4526 }
4527
4528 2 XSRETURN_EMPTY;
4529 }
4530  
4531 /* Object::Proto::with("ClassName", @role_names) - apply roles to a class */
4532 15 XS_INTERNAL(xs_with) {
4533 15 dXSARGS;
4534 STRLEN class_len;
4535 const char *class_pv;
4536 ClassMeta *class_meta;
4537 IV i;
4538
4539 15 ▶ if (items < 2) croak("Usage: Object::Proto::with($class_name, @role_names)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::with($class_name, @role_names)"); 1 14 15
4540
4541 14 class_pv = SvPV(ST(0), class_len);
4542 14 class_meta = get_class_meta(aTHX_ class_pv, class_len);
4543 14 ▶ if (!class_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!class_meta) { 1 13 14
4544 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
4545 }
4546
4547 23 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 15 8 23
4548 STRLEN role_len;
4549 15 const char *role_pv = SvPV(ST(i), role_len);
4550 15 RoleMeta *role_meta = get_role_meta(aTHX_ role_pv, role_len);
4551
4552 15 ▶ if (!role_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!role_meta) { 3 12 15
4553 /* Auto-load the role module */
4554 3 SV *module_sv = newSVpvn(role_pv, role_len);
4555 SV *err;
4556 3 load_module(PERL_LOADMOD_NOIMPORT, module_sv, NULL);
4557 2 ▶ err = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
err = ERRSV; 2 0 2
4558 /*
4559 # uncoverable branch true
4560 # uncoverable condition left count:1 */
4561 2 ▶ if (SvTRUE(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(err)) { 0 2 2
4562 0 croak("Role '%s' not defined (failed to load: %" SVf ")", role_pv, SVfARG(err)); /* # uncoverable statement */
4563 }
4564 2 role_meta = get_role_meta(aTHX_ role_pv, role_len);
4565 }
4566 14 ▶ if (!role_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!role_meta) { 1 13 14
4567 1 croak("Role '%s' not defined", role_pv);
4568 }
4569
4570 13 apply_role_to_class(aTHX_ class_meta, role_meta);
4571 }
4572
4573 8 XSRETURN_EMPTY;
4574 }
4575  
4576 /* Object::Proto::does("ClassName" or $obj, "RoleName") - check if class/object does role */
4577 10 XS_INTERNAL(xs_does) {
4578 10 dXSARGS;
4579 ClassMeta *meta;
4580 STRLEN role_len;
4581 const char *role_pv;
4582 IV i;
4583
4584 10 ▶ if (items < 2) croak("Usage: Object::Proto::does($class_or_obj, $role_name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Object::Proto::does($class_or_obj, $role_name)"); 1 9 10
4585
4586 /* Get class meta from class name or object */
4587 9 ▶ if (SvROK(ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ST(0))) { 7 2 9
4588 /* Object - get stash name */
4589 7 HV *stash = SvSTASH(SvRV(ST(0)));
4590 7 ▶ meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash));
Branch: 12 branches
BranchTrueFalseTotal
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 7 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 0 0
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 7 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 7 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 0 0
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 7 0 7
meta = get_class_meta(aTHX_ HvNAME(stash), HvNAMELEN(stash)); 0 7 7
4591 } else {
4592 STRLEN class_len;
4593 2 const char *class_pv = SvPV(ST(0), class_len);
4594 2 meta = get_class_meta(aTHX_ class_pv, class_len);
4595 }
4596
4597 9 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 8 9
4598 1 XSRETURN_NO;
4599 }
4600
4601 8 role_pv = SvPV(ST(1), role_len);
4602
4603 /* Check if role is in consumed_roles */
4604 11 ▶ for (i = 0; i < meta->role_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->role_count; i++) { 10 1 11
4605 10 ▶ if (strEQ(meta->consumed_roles[i]->role_name, role_pv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(meta->consumed_roles[i]->role_name, role_pv)) { 7 3 10
4606 7 XSRETURN_YES;
4607 }
4608 }
4609
4610 1 XSRETURN_NO;
4611 }
4612  
4613 /* ============================================
4614 Method Modifier API
4615 ============================================ */
4616  
4617 /* Object::Proto::before("Class::method", \&callback) */
4618 11 XS_INTERNAL(xs_before) {
4619 11 dXSARGS;
4620 STRLEN full_name_len;
4621 const char *full_name;
4622 char *class_name, *method_name, *sep;
4623 ClassMeta *meta;
4624
4625 11 ▶ if (items != 2) croak("Usage: Object::Proto::before('Class::method', \\&callback)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: Object::Proto::before('Class::method', \\&callback)"); 1 10 11
4626
4627 10 full_name = SvPV(ST(0), full_name_len);
4628 10 ▶ if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 9 1 10
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 1 8 9
4629 2 croak("Second argument must be a code reference");
4630 }
4631
4632 /* Parse "Class::method" */
4633 8 sep = strstr(full_name, "::");
4634 8 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 7 8
4635 1 croak("Method name must be fully qualified (Class::method)");
4636 }
4637
4638 {
4639 7 STRLEN class_len = sep - full_name;
4640 7 Newx(class_name, class_len + 1, char);
4641 7 Copy(full_name, class_name, class_len, char);
4642 7 class_name[class_len] = '\0';
4643 7 method_name = sep + 2;
4644 }
4645
4646 7 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
4647 7 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 6 7
4648 1 Safefree(class_name);
4649 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
4650 }
4651
4652 6 add_modifier(aTHX_ meta, method_name, ST(1), 0); /* 0 = before */
4653
4654 6 Safefree(class_name);
4655 6 XSRETURN_EMPTY;
4656 }
4657  
4658 /* Object::Proto::after("Class::method", \&callback) */
4659 13 XS_INTERNAL(xs_after) {
4660 13 dXSARGS;
4661 STRLEN full_name_len;
4662 const char *full_name;
4663 char *class_name, *method_name, *sep;
4664 ClassMeta *meta;
4665
4666 13 ▶ if (items != 2) croak("Usage: Object::Proto::after('Class::method', \\&callback)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: Object::Proto::after('Class::method', \\&callback)"); 1 12 13
4667
4668 12 full_name = SvPV(ST(0), full_name_len);
4669 12 ▶ if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 11 1 12
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 1 10 11
4670 2 croak("Second argument must be a code reference");
4671 }
4672
4673 10 sep = strstr(full_name, "::");
4674 10 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 9 10
4675 1 croak("Method name must be fully qualified (Class::method)");
4676 }
4677
4678 {
4679 9 STRLEN class_len = sep - full_name;
4680 9 Newx(class_name, class_len + 1, char);
4681 9 Copy(full_name, class_name, class_len, char);
4682 9 class_name[class_len] = '\0';
4683 9 method_name = sep + 2;
4684 }
4685
4686 9 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
4687 9 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 8 9
4688 1 Safefree(class_name);
4689 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
4690 }
4691
4692 8 add_modifier(aTHX_ meta, method_name, ST(1), 1); /* 1 = after */
4693
4694 8 Safefree(class_name);
4695 8 XSRETURN_EMPTY;
4696 }
4697  
4698 /* Object::Proto::around("Class::method", \&callback) */
4699 8 XS_INTERNAL(xs_around) {
4700 8 dXSARGS;
4701 STRLEN full_name_len;
4702 const char *full_name;
4703 char *class_name, *method_name, *sep;
4704 ClassMeta *meta;
4705
4706 8 ▶ if (items != 2) croak("Usage: Object::Proto::around('Class::method', \\&callback)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: Object::Proto::around('Class::method', \\&callback)"); 1 7 8
4707
4708 7 full_name = SvPV(ST(0), full_name_len);
4709 7 ▶ if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 6 1 7
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 1 5 6
4710 2 croak("Second argument must be a code reference");
4711 }
4712
4713 5 sep = strstr(full_name, "::");
4714 5 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 4 5
4715 1 croak("Method name must be fully qualified (Class::method)");
4716 }
4717
4718 {
4719 4 STRLEN class_len = sep - full_name;
4720 4 Newx(class_name, class_len + 1, char);
4721 4 Copy(full_name, class_name, class_len, char);
4722 4 class_name[class_len] = '\0';
4723 4 method_name = sep + 2;
4724 }
4725
4726 4 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
4727 4 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 3 4
4728 1 Safefree(class_name);
4729 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
4730 }
4731
4732 3 add_modifier(aTHX_ meta, method_name, ST(1), 2); /* 2 = around */
4733
4734 3 Safefree(class_name);
4735 3 XSRETURN_EMPTY;
4736 }
4737  
4738 /* Object::Proto::singleton("Class") - marks class as singleton and installs instance() method */
4739 11 XS_INTERNAL(xs_singleton) {
4740 11 dXSARGS;
4741 STRLEN class_len;
4742 const char *class_pv;
4743 ClassMeta *meta;
4744 char full_name[256];
4745 CV *instance_cv;
4746  
4747 11 ▶ if (items < 1) croak("Usage: Object::Proto::singleton($class)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::singleton($class)"); 1 10 11
4748  
4749 10 class_pv = SvPV(ST(0), class_len);
4750  
4751 10 meta = get_class_meta(aTHX_ class_pv, class_len);
4752 10 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 9 10
4753 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
4754 }
4755  
4756 /* Mark as singleton */
4757 9 meta->is_singleton = 1;
4758 9 meta->singleton_instance = NULL;
4759  
4760 /* Install instance() class method */
4761 9 snprintf(full_name, sizeof(full_name), "%s::instance", class_pv);
4762 9 instance_cv = newXS(full_name, xs_singleton_instance, __FILE__);
4763 9 CvXSUBANY(instance_cv).any_iv = PTR2IV(meta);
4764  
4765 9 XSRETURN_EMPTY;
4766 }
4767  
4768 /* ============================================
4769 Boot
4770 ============================================ */
4771  
4772 70 XS_EXTERNAL(boot_Object__Proto) {
4773 70 dXSBOOTARGSXSAPIVERCHK;
4774 PERL_UNUSED_VAR(items);
4775  
4776 /* Register custom ops */
4777 70 XopENTRY_set(&object_get_xop, xop_name, "object_get");
4778 70 XopENTRY_set(&object_get_xop, xop_desc, "object property get");
4779 70 XopENTRY_set(&object_get_xop, xop_class, OA_UNOP);
4780 70 Perl_custom_op_register(aTHX_ pp_object_get, &object_get_xop);
4781
4782 70 XopENTRY_set(&object_set_xop, xop_name, "object_set");
4783 70 XopENTRY_set(&object_set_xop, xop_desc, "object property set");
4784 70 XopENTRY_set(&object_set_xop, xop_class, OA_BINOP);
4785 70 Perl_custom_op_register(aTHX_ pp_object_set, &object_set_xop);
4786  
4787 70 XopENTRY_set(&object_set_typed_xop, xop_name, "object_set_typed");
4788 70 XopENTRY_set(&object_set_typed_xop, xop_desc, "object property set with type check");
4789 70 XopENTRY_set(&object_set_typed_xop, xop_class, OA_BINOP);
4790 70 Perl_custom_op_register(aTHX_ pp_object_set_typed, &object_set_typed_xop);
4791  
4792 70 XopENTRY_set(&object_func_get_xop, xop_name, "object_func_get");
4793 70 XopENTRY_set(&object_func_get_xop, xop_desc, "object function-style get");
4794 70 XopENTRY_set(&object_func_get_xop, xop_class, OA_UNOP);
4795 70 Perl_custom_op_register(aTHX_ pp_object_func_get, &object_func_get_xop);
4796
4797 70 XopENTRY_set(&object_func_set_xop, xop_name, "object_func_set");
4798 70 XopENTRY_set(&object_func_set_xop, xop_desc, "object function-style set");
4799 70 XopENTRY_set(&object_func_set_xop, xop_class, OA_BINOP);
4800 70 Perl_custom_op_register(aTHX_ pp_object_func_set, &object_func_set_xop);
4801  
4802 /* Initialize registries */
4803 70 g_class_registry = newHV();
4804 70 g_type_registry = newHV();
4805  
4806 /* Install XS functions */
4807 70 newXS("Object::Proto::import", xs_import, __FILE__);
4808 70 newXS("Object::Proto::define", xs_define, __FILE__);
4809 70 newXS("Object::Proto::import_accessors", xs_import_accessors, __FILE__);
4810 70 newXS("Object::Proto::import_accessor", xs_import_accessor, __FILE__);
4811 70 newXS("Object::Proto::prototype", xs_prototype, __FILE__);
4812 70 newXS("Object::Proto::set_prototype", xs_set_prototype, __FILE__);
4813 70 newXS("Object::Proto::prototype_chain", xs_prototype_chain, __FILE__);
4814 70 newXS("Object::Proto::has_own_property", xs_has_own_property, __FILE__);
4815 70 newXS("Object::Proto::prototype_depth", xs_prototype_depth, __FILE__);
4816 70 newXS("Object::Proto::lock", xs_lock, __FILE__);
4817 70 newXS("Object::Proto::unlock", xs_unlock, __FILE__);
4818 70 newXS("Object::Proto::freeze", xs_freeze, __FILE__);
4819 70 newXS("Object::Proto::is_frozen", xs_is_frozen, __FILE__);
4820 70 newXS("Object::Proto::is_locked", xs_is_locked, __FILE__);
4821  
4822 /* Introspection API */
4823 70 newXS("Object::Proto::clone", xs_clone, __FILE__);
4824 70 newXS("Object::Proto::properties", xs_properties, __FILE__);
4825 70 newXS("Object::Proto::slot_info", xs_slot_info, __FILE__);
4826  
4827 /* Inheritance API */
4828 70 newXS("Object::Proto::parent", xs_parent, __FILE__);
4829 70 newXS("Object::Proto::ancestors", xs_ancestors, __FILE__);
4830  
4831 /* Simple custom op registry */
4832 70 newXS("Object::Proto::register_simple_custom_op", xs_register_simple_custom_op, __FILE__);
4833  
4834 /* Type registry API */
4835 70 newXS("Object::Proto::register_type", xs_register_type, __FILE__);
4836 70 newXS("Object::Proto::has_type", xs_has_type, __FILE__);
4837 70 newXS("Object::Proto::list_types", xs_list_types, __FILE__);
4838  
4839 /* Singleton support */
4840 70 newXS("Object::Proto::singleton", xs_singleton, __FILE__);
4841
4842 /* Role API */
4843 70 newXS("Object::Proto::role", xs_role, __FILE__);
4844 70 newXS("Object::Proto::requires", xs_requires, __FILE__);
4845 70 newXS("Object::Proto::with", xs_with, __FILE__);
4846 70 newXS("Object::Proto::does", xs_does, __FILE__);
4847
4848 /* Method modifier API */
4849 70 newXS("Object::Proto::before", xs_before, __FILE__);
4850 70 newXS("Object::Proto::after", xs_after, __FILE__);
4851 70 newXS("Object::Proto::around", xs_around, __FILE__);
4852  
4853 /* Register cleanup for global destruction */
4854 70 Perl_call_atexit(aTHX_ object_cleanup_globals, NULL);
4855  
4856 70 OBJECT_PROTO_XS_BOOT_EPILOG(ax);
4857 70 }
« blib/arch/Object/Proto/Install/Files.pm ↑ Summary blib/lib/Object/Proto.pm »