object.c

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