object.c

filter: Statementstmt 99.1% 3010 / 3036 Branchbran 87.9% 2258 / 2626 (58 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.9% 5268 / 5662 (58 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 /* handles(...): how a slot's value is delegated to, decided by its type */
55 typedef enum {
56 HK_METHOD = 0, /* Object: call the method on it */
57 HK_CODE, /* CodeRef: call it */
58 HK_ARRAY, /* ArrayRef: a native array op */
59 HK_HASH /* HashRef: a native hash op */
60 } HandleKind;
61  
62 /* CodeRef modes */
63 #define HANDLE_CODE_CALL 0 /* $code->(@args) */
64 #define HANDLE_CODE_METHOD 1 /* $code->($self, @args) */
65  
66 /* One delegator, resolved against the slot's final type */
67 typedef struct {
68 SV *local; /* method installed in the class */
69 SV *remote; /* method called on the delegate, or the native op, or call/method */
70 U8 kind; /* HandleKind */
71 U8 op; /* index in the kind's op table, or HANDLE_CODE_* */
72 } ResolvedHandle;
73  
74 /* Per-slot specification - parsed from "name:Type:default(val)" */
75 typedef struct {
76 char *name;
77 BuiltinTypeID type_id; /* Built-in type or TYPE_CUSTOM */
78 RegisteredType *registered; /* For external XS types */
79 SV *default_sv; /* Default value (immutable, refcnt'd) */
80 SV *trigger_cb; /* Trigger callback */
81 SV *coerce_cb; /* Coercion callback (Perl) */
82 SV *builder_name; /* Builder method name for lazy attrs */
83 U8 is_required; /* Croak if not provided in new() */
84 U8 is_readonly; /* Croak if set after new() */
85 U8 is_lazy; /* Build on first access, not at new() */
86 U8 has_default;
87 U8 has_trigger;
88 U8 has_coerce;
89 U8 has_type;
90 U8 has_builder; /* Has builder method */
91 U8 has_clearer; /* Generate clear_X method */
92 U8 has_predicate; /* Generate has_X method */
93 U8 is_weak; /* Weaken references when stored */
94 U8 is_private; /* Accessor only callable from the owning package */
95 U8 has_checks; /* is_readonly | is_required | has_coerce | TYPE_CUSTOM — skip block when 0 */
96 SV *clearer_name; /* Custom clearer method name */
97 SV *predicate_name; /* Custom predicate method name */
98 SV *reader_name; /* Custom reader method name (get_X style) */
99 SV *writer_name; /* Custom writer method name (set_X style) */
100 SV *init_arg; /* Alternate constructor argument name */
101 char *private_owner; /* Package that declared the attribute: the only caller of a :private one */
102 char *private_also; /* A role attribute's consuming class, which may call it too; NULL otherwise */
103 AV *handles; /* handles(...) as written: local, remote pairs; local undef for role:Name,
104 remote undef for a bare name. Never changed once parsed, so clones share it */
105 ResolvedHandle *resolved; /* handles resolved when the slot joins a class */
106 IV resolved_count;
107 U8 has_handles;
108 U8 default_implicit; /* default [] or {} added by handles, not written by the user */
109 U8 inherited; /* copied unchanged from a parent: a method of the child's own
110 overrides its delegators rather than clashing with them */
111 } SlotSpec;
112  
113 /* Custom op definitions */
114 static XOP object_get_xop;
115 static XOP object_set_xop;
116 static XOP object_set_typed_xop;
117  
118 /* Per-class metadata */
119 typedef struct ClassMeta_s ClassMeta; /* Forward declaration */
120  
121 /* Method modifier chain - linked list for each type */
122 typedef struct MethodModifier_s {
123 SV *callback;
124 struct MethodModifier_s *next;
125 } MethodModifier;
126  
127 /* Modified method wrapper */
128 typedef struct {
129 CV *original_cv;
130 MethodModifier *before_chain;
131 MethodModifier *after_chain;
132 MethodModifier *around_chain;
133 } ModifiedMethod;
134  
135 /* Role metadata */
136 typedef struct {
137 char *role_name;
138 char **required_methods; /* Methods consuming class MUST have */
139 IV required_count;
140 SlotSpec **slots; /* Slots the role provides */
141 IV slot_count;
142 HV *stash; /* Role's stash for provided methods */
143 } RoleMeta;
144  
145 /* Per-class metadata */
146 struct ClassMeta_s {
147 char *class_name;
148 HV *prop_to_idx; /* property name -> slot index */
149 HV *arg_to_idx; /* constructor argument name -> slot index (init_arg or property name) */
150 char **idx_to_prop; /* slot index -> property name */
151 IV slot_count;
152 HV *stash; /* cached stash pointer */
153 /* Type system extensions */
154 SlotSpec **slots; /* Per-slot specifications, NULL if no specs */
155 U8 has_any_types; /* Quick check: any slot has type checking? */
156 U8 has_any_defaults; /* Quick check: any slot has defaults? */
157 U8 has_any_triggers; /* Quick check: any slot has triggers? */
158 U8 has_any_required; /* Quick check: any slot is required? */
159 U8 has_any_lazy; /* Quick check: any slot is lazy? */
160 U8 has_any_builders; /* Quick check: any slot has builders? */
161 U8 has_any_weak; /* Quick check: any slot has weak refs? */
162 /* Singleton support */
163 SV *singleton_instance; /* Cached singleton instance, NULL if not a singleton */
164 U8 is_singleton; /* Flag: class is a singleton */
165 /* DEMOLISH support - only set if class has DEMOLISH method */
166 CV *demolish_cv; /* Cached DEMOLISH method, NULL if none */
167 /* BUILD support - called after new() */
168 CV *build_cv; /* Cached BUILD method, NULL if none */
169 U8 has_build; /* Flag: class has BUILD method */
170 /* Role support */
171 RoleMeta **consumed_roles; /* Array of consumed roles, NULL if none */
172 IV role_count;
173 /* Method modifier registry - only allocated if modifiers are used */
174 HV *modified_methods; /* method name -> ModifiedMethod*, NULL if none */
175 /* Inheritance support */
176 char **parent_classes; /* Array of parent class names, NULL if no parents */
177 struct ClassMeta_s **parent_metas; /* Array of parent ClassMeta pointers */
178 IV parent_count; /* Number of parent classes */
179 };
180  
181 /* Global class registry */
182 static HV *g_class_registry = NULL; /* class name -> ClassMeta* */
183  
184 /* Global type registry for external plugins */
185 static HV *g_type_registry = NULL; /* type name -> RegisteredType* */
186  
187 /* Global role registry */
188 static HV *g_role_registry = NULL; /* role name -> RoleMeta* */
189  
190 /* handles(...) lookups, built at boot from the tables beside parse_handles */
191 static HV *g_reserved_methods = NULL; /* names a delegator may never take */
192 static HV *g_array_op_index = NULL; /* ArrayRef op name -> index in array_ops, aliases resolved */
193 static HV *g_hash_op_index = NULL; /* HashRef op name -> index in hash_ops */
194  
195 /* Forward declaration for FuncAccessorData */
196 typedef struct FuncAccessorData_s FuncAccessorData;
197  
198 /* Global registry for function accessor data (to avoid storing pointers in op_targ) */
199 static FuncAccessorData **g_func_accessor_registry = NULL;
200 static IV g_func_accessor_count = 0;
201 static IV g_func_accessor_capacity = 0;
202  
203 /* Forward declarations */
204 static ClassMeta* get_class_meta(pTHX_ const char *class_name, STRLEN len);
205 static void install_constructor(pTHX_ const char *class_name, ClassMeta *meta);
206 static void install_accessor(pTHX_ const char *class_name, const char *prop_name, IV idx);
207 static void install_accessor_typed(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta);
208 static void install_clearer(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name);
209 static void install_predicate(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name);
210 static void install_destroy_wrapper(pTHX_ const char *class_name, ClassMeta *meta);
211 static RoleMeta* get_role_meta(pTHX_ const char *role_name, STRLEN len);
212 static void object_check_private(pTHX_ SlotSpec *spec, ClassMeta *meta);
213 static void install_class_delegators(pTHX_ ClassMeta *meta);
214 static void check_role_slot_against_delegators(pTHX_ ClassMeta *meta, SlotSpec *slot, RoleMeta *role);
215 XS_INTERNAL(xs_prototype);
216  
217 /* What every generated method checks on each call: one flag, with the caller
218 * looked at only for a private attribute */
219 112803 OBJECT_INLINE void check_private(pTHX_ SlotSpec *spec, ClassMeta *meta) {
220 112803 ▶ if (spec->is_private) object_check_private(aTHX_ spec, meta);
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_private) object_check_private(aTHX_ spec, meta); 63 112740 112803
221 112767 }
222 XS_INTERNAL(xs_set_prototype);
223  
224 /* ============================================
225 Built-in type checking (inline)
226 ============================================ */
227  
228 4735 OBJECT_INLINE BuiltinTypeID parse_builtin_type(const char *type_str, STRLEN len) {
229 4735 ▶ if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR; 3397 1338 4735
if (len == 3 && strEQ(type_str, "Str")) return TYPE_STR; 3178 219 3397
230 1557 ▶ if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT; 219 1338 1557
if (len == 3 && strEQ(type_str, "Int")) return TYPE_INT; 132 87 219
231 1425 ▶ if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM; 87 1338 1425
if (len == 3 && strEQ(type_str, "Num")) return TYPE_NUM; 21 66 87
232 1404 ▶ if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY; 66 1338 1404
if (len == 3 && strEQ(type_str, "Any")) return TYPE_ANY; 59 7 66
233 1345 ▶ if (len == 3 && strEQ(type_str, "Ref")) return TYPE_REF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "Ref")) return TYPE_REF; 7 1338 1345
if (len == 3 && strEQ(type_str, "Ref")) return TYPE_REF; 4 3 7
234 1341 ▶ if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL; 12 1329 1341
if (len == 4 && strEQ(type_str, "Bool")) return TYPE_BOOL; 11 1 12
235 1330 ▶ if (len == 5 && strEQ(type_str, "Undef")) return TYPE_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 5 && strEQ(type_str, "Undef")) return TYPE_UNDEF; 11 1319 1330
if (len == 5 && strEQ(type_str, "Undef")) return TYPE_UNDEF; 6 5 11
236 1324 ▶ if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT; 132 1192 1324
if (len == 6 && strEQ(type_str, "Object")) return TYPE_OBJECT; 130 2 132
237 1194 ▶ if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED; 63 1131 1194
if (len == 7 && strEQ(type_str, "Defined")) return TYPE_DEFINED; 3 60 63
238 1191 ▶ if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF; 60 1131 1191
if (len == 7 && strEQ(type_str, "CodeRef")) return TYPE_CODEREF; 26 34 60
239 1165 ▶ if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF; 34 1131 1165
if (len == 7 && strEQ(type_str, "HashRef")) return TYPE_HASHREF; 29 5 34
240 1136 ▶ if (len == 7 && strEQ(type_str, "GlobRef")) return TYPE_GLOBREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && strEQ(type_str, "GlobRef")) return TYPE_GLOBREF; 5 1131 1136
if (len == 7 && strEQ(type_str, "GlobRef")) return TYPE_GLOBREF; 3 2 5
241 1133 ▶ if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF; 64 1069 1133
if (len == 8 && strEQ(type_str, "ArrayRef")) return TYPE_ARRAYREF; 63 1 64
242 1070 ▶ if (len == 9 && strEQ(type_str, "ScalarRef")) return TYPE_SCALARREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 9 && strEQ(type_str, "ScalarRef")) return TYPE_SCALARREF; 16 1054 1070
if (len == 9 && strEQ(type_str, "ScalarRef")) return TYPE_SCALARREF; 3 13 16
243 1067 ▶ if (len == 9 && strEQ(type_str, "RegexpRef")) return TYPE_REGEXPREF;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 9 && strEQ(type_str, "RegexpRef")) return TYPE_REGEXPREF; 13 1054 1067
if (len == 9 && strEQ(type_str, "RegexpRef")) return TYPE_REGEXPREF; 3 10 13
244 1064 ▶ if (len == 9 && strEQ(type_str, "ClassName")) return TYPE_CLASSNAME;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 9 && strEQ(type_str, "ClassName")) return TYPE_CLASSNAME; 10 1054 1064
if (len == 9 && strEQ(type_str, "ClassName")) return TYPE_CLASSNAME; 4 6 10
245 1060 ▶ if (len == 10 && strEQ(type_str, "FileHandle")) return TYPE_FILEHANDLE;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 10 && strEQ(type_str, "FileHandle")) return TYPE_FILEHANDLE; 8 1052 1060
if (len == 10 && strEQ(type_str, "FileHandle")) return TYPE_FILEHANDLE; 3 5 8
246 1057 ▶ if (len == 11 && strEQ(type_str, "PositiveInt")) return TYPE_POSITIVEINT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 11 && strEQ(type_str, "PositiveInt")) return TYPE_POSITIVEINT; 1032 25 1057
if (len == 11 && strEQ(type_str, "PositiveInt")) return TYPE_POSITIVEINT; 14 1018 1032
247 1043 ▶ if (len == 11 && strEQ(type_str, "NegativeInt")) return TYPE_NEGATIVEINT;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 11 && strEQ(type_str, "NegativeInt")) return TYPE_NEGATIVEINT; 1018 25 1043
if (len == 11 && strEQ(type_str, "NegativeInt")) return TYPE_NEGATIVEINT; 5 1013 1018
248 1038 ▶ if (len == 11 && strEQ(type_str, "NonEmptyStr")) return TYPE_NONEMPTYSTR;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 11 && strEQ(type_str, "NonEmptyStr")) return TYPE_NONEMPTYSTR; 1013 25 1038
if (len == 11 && strEQ(type_str, "NonEmptyStr")) return TYPE_NONEMPTYSTR; 9 1004 1013
249 1029 return TYPE_NONE; /* Unknown - could be custom */
250 }
251  
252 /* ScalarRef: a reference that ref() calls SCALAR or REF, blessed or not */
253 25 static bool check_scalarref(pTHX_ SV *val) {
254 SV *rv;
255 25 ▶ if (!SvROK(val)) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(val)) return false; 2 23 25
256 23 rv = SvRV(val);
257 23 ▶ if (SvTYPE(rv) <= SVt_PVMG) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) <= SVt_PVMG) { 11 12 23
258 #if !PERL_VERSION_GE(5,11,0)
259 if (SvRXOK(val)) return false; /* before 5.11 a regexp is a PVMG */
260 #endif
261 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
262 }
263 /* A tied element is an lvalue that ref() calls SCALAR, or REF once it
264 * has fetched a reference; substr, vec and pos lvalues are LVALUE. */
265 12 ▶ if (SvTYPE(rv) == SVt_PVLV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVLV) { 3 9 12
266 3 const char *kind = sv_reftype(rv, 0);
267 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
268 }
269 9 return false;
270 }
271  
272 /* FileHandle: an open handle - a glob (including one held in an lvalue),
273 * glob ref or IO ref whose IO is open or tied, as Scalar::Util::openhandle
274 * decides it - or an object that isa IO::Handle, which covers handle objects
275 * that are not open yet. */
276 21 static bool check_filehandle(pTHX_ SV *val) {
277 21 ▶ SV *sv = SvROK(val) ? SvRV(val) : val;
Branch: 1 branch
BranchTrueFalseTotal
SV *sv = SvROK(val) ? SvRV(val) : val; 15 6 21
278 21 IO *io = NULL;
279  
280 /* isGV_with_GP is flag && (PVGV || PVLV). The GP flag is only set on a
281 * PVGV or a PVLV, so PVGV false only ever falls to PVLV true, and PVLV
282 * is never false: neither decides the result with its false outcome.
283 # uncoverable condition right count:2
284 # uncoverable condition right count:3 */
285 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
286 13 io = GvIOp((GV *)sv);
287 8 ▶ else if (SvTYPE(sv) == SVt_PVIO)
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(sv) == SVt_PVIO) 1 7 8
288 1 io = (IO *)sv;
289  
290 /* A tied handle has no PerlIO of its own */
291 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
292 9 return true;
293  
294 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
295 }
296  
297 /* Int, and the sign of its value: -1, 0 or 1, or INT_SIGN_NONE when val
298 * is not an Int. A UV is only flagged when it is above IV_MAX, where SvIV
299 * would wrap it negative. A numeric string is signed by strtoll, which on a
300 * 32-bit perl reads past the IV range. */
301 #define INT_SIGN_NONE 2
302 11120 OBJECT_INLINE int int_sign(pTHX_ SV *val) {
303 11120 ▶ if (SvIOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) { 11024 96 11120
304 11024 ▶ if (SvIsUV(val)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(val)) return 1; 5 11019 11024
305 11019 ▶ 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 10984 11019
return SvIVX(val) > 0 ? 1 : SvIVX(val) < 0 ? -1 : 0; 18 17 35
306 }
307 96 ▶ if (SvPOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(val)) { 77 19 96
308 /* Use strtoll for fast integer parsing */
309 STRLEN len;
310 77 const char *pv = SvPV(val, len);
311 char *endp;
312 long long n;
313 113 ▶ if (len == 0) return INT_SIGN_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return INT_SIGN_NONE; 6 71 77
314 71 errno = 0;
315 71 n = strtoll(pv, &endp, 10);
316 71 ▶ 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 */ 70 1 71
if (errno == 0 && endp == pv + len && *pv != '\0') /* # uncoverable condition right count:3 */ 36 34 70
if (errno == 0 && endp == pv + len && *pv != '\0') /* # uncoverable condition right count:3 */ 36 0 36
317 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
318 }
319 54 return INT_SIGN_NONE;
320 }
321  
322 /* The GV a stash holds under name, or NULL. An entry that is not a GV is a
323 * constant or a sub stub placeholder. */
324 57 static GV *stash_gv(pTHX_ HV *stash, const char *name, I32 len) {
325 57 SV **svp = hv_fetch(stash, name, len, 0);
326 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
327 }
328  
329 /* ClassName: a non-empty string naming a loaded package - one with a
330 * defined $VERSION, a non-empty @ISA or at least one sub, as Class::Load
331 * decides it. The stash is looked up, never created, and walked bucket by
332 * bucket so the check does not reset an each() the caller has under way. */
333 43 static bool check_classname(pTHX_ SV *val) {
334 HV *stash;
335 GV *gv;
336 HE **buckets;
337 STRLEN i;
338  
339 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
340 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
341 40 stash = gv_stashsv(val, 0);
342 40 ▶ if (!stash) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return false; 10 30 40
343  
344 30 gv = stash_gv(aTHX_ stash, "VERSION", 7);
345 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
346 27 gv = stash_gv(aTHX_ stash, "ISA", 3);
347 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
348  
349 25 buckets = HvARRAY(stash);
350 25 ▶ if (buckets) {
Branch: 1 branch
BranchTrueFalseTotal
if (buckets) { 24 1 25
351 187 ▶ for (i = 0; i <= HvMAX(stash); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= HvMAX(stash); i++) { 181 6 187
352 HE *he;
353 206 ▶ for (he = buckets[i]; he; he = HeNEXT(he)) {
Branch: 1 branch
BranchTrueFalseTotal
for (he = buckets[i]; he; he = HeNEXT(he)) { 43 163 206
354 43 SV *entry = HeVAL(he);
355 43 ▶ if (SvTYPE(entry) != SVt_PVGV || GvCV((GV *)entry))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(entry) != SVt_PVGV || GvCV((GV *)entry)) 42 1 43
if (SvTYPE(entry) != SVt_PVGV || GvCV((GV *)entry)) 17 25 42
356 18 return true;
357 }
358 }
359 }
360 7 return false;
361 }
362  
363 /* Inline type check - returns true if value passes check */
364 35529 OBJECT_INLINE bool check_builtin_type(pTHX_ SV *val, BuiltinTypeID type_id) {
365 35529 switch (type_id) { /* # uncoverable branch true count:21 */
366 67 case TYPE_ANY:
367 67 return true;
368 6 case TYPE_DEFINED:
369 /* Be defensive: SvOK may not catch all defined values in older Perls */
370 /*
371 # uncoverable condition left count:2
372 # uncoverable condition left count:3
373 # uncoverable condition left count:4 */
374 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
375 15670 case TYPE_STR:
376 15670 ▶ return SvOK(val) && !SvROK(val); /* defined non-ref */
Branch: 2 branches
BranchTrueFalseTotal
return SvOK(val) && !SvROK(val); /* defined non-ref */ 15667 3 15670
return SvOK(val) && !SvROK(val); /* defined non-ref */ 15662 5 15667
377 11027 case TYPE_INT:
378 11027 return int_sign(aTHX_ val) != INT_SIGN_NONE;
379 3235 case TYPE_NUM:
380 3235 ▶ if (SvNIOK(val)) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (SvNIOK(val)) return true; 3227 8 3235
381 8 ▶ if (SvPOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(val)) { 6 2 8
382 /* Use strtod for fast number parsing */
383 STRLEN len;
384 6 const char *pv = SvPV(val, len);
385 char *endp;
386 6 ▶ if (len == 0) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return false; 1 5 6
387 5 errno = 0;
388 5 (void)strtod(pv, &endp);
389 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
390 }
391 2 return false;
392 15 case TYPE_BOOL:
393 /* Accept 0, 1, "", or boolean SVs */
394 15 ▶ if (SvIOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) { 9 6 15
395 9 IV iv = SvIV(val);
396 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
397 }
398 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
399 3079 case TYPE_ARRAYREF:
400 3079 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV; 3078 1 3079
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV; 3077 1 3078
401 2062 case TYPE_HASHREF:
402 2062 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV; 2061 1 2062
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV; 2060 1 2061
403 39 case TYPE_CODEREF:
404 39 ▶ return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV;
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV; 38 1 39
return SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV; 37 1 38
405 67 case TYPE_OBJECT:
406 67 ▶ return SvROK(val) && sv_isobject(val);
Branch: 2 branches
BranchTrueFalseTotal
return SvROK(val) && sv_isobject(val); 65 2 67
return SvROK(val) && sv_isobject(val); 62 3 65
407 19 case TYPE_REF:
408 19 return SvROK(val);
409 25 case TYPE_SCALARREF:
410 25 return check_scalarref(aTHX_ val);
411 13 case TYPE_REGEXPREF:
412 #if PERL_VERSION_GE(5,11,0)
413 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
414 #else
415 return SvROK(val) && SvRXOK(val); /* a regexp is a PVMG before 5.11 */
416 #endif
417 15 case TYPE_GLOBREF:
418 /* What ref() calls GLOB. A ref to a glob held in an lvalue
419 * vivifies it into a real glob, so the referent is a PVGV.
420 # uncoverable condition right count:3
421 # uncoverable condition left count:4
422 # uncoverable condition right count:4 */
423 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
424 21 case TYPE_FILEHANDLE:
425 21 return check_filehandle(aTHX_ val);
426 43 case TYPE_CLASSNAME:
427 43 return check_classname(aTHX_ val);
428 52 case TYPE_POSITIVEINT:
429 52 return int_sign(aTHX_ val) == 1;
430 41 case TYPE_NEGATIVEINT:
431 41 return int_sign(aTHX_ val) == -1;
432 22 case TYPE_NONEMPTYSTR:
433 /* a number always stringifies to something: only a string is empty */
434 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
435 11 case TYPE_UNDEF:
436 11 return !SvOK(val);
437 0 default: /* # uncoverable statement */
438 0 return true; /* No check or unknown */ /* # uncoverable statement */
439 }
440 }
441  
442 /* Get type name for error messages */
443 11121 static const char* type_id_to_name(BuiltinTypeID type_id) {
444 /*
445 # uncoverable branch true count:21
446 # uncoverable branch true count:22 */
447 11121 switch (type_id) {
448 2 case TYPE_ANY: return "Any";
449 4 case TYPE_DEFINED: return "Defined";
450 5631 case TYPE_STR: return "Str";
451 2637 case TYPE_INT: return "Int";
452 8 case TYPE_NUM: return "Num";
453 4 case TYPE_BOOL: return "Bool";
454 2617 case TYPE_ARRAYREF: return "ArrayRef";
455 9 case TYPE_HASHREF: return "HashRef";
456 8 case TYPE_CODEREF: return "CodeRef";
457 36 case TYPE_OBJECT: return "Object";
458 8 case TYPE_REF: return "Ref";
459 15 case TYPE_SCALARREF: return "ScalarRef";
460 10 case TYPE_REGEXPREF: return "RegexpRef";
461 10 case TYPE_GLOBREF: return "GlobRef";
462 12 case TYPE_FILEHANDLE: return "FileHandle";
463 21 case TYPE_CLASSNAME: return "ClassName";
464 36 case TYPE_POSITIVEINT: return "PositiveInt";
465 32 case TYPE_NEGATIVEINT: return "NegativeInt";
466 12 case TYPE_NONEMPTYSTR: return "NonEmptyStr";
467 9 case TYPE_UNDEF: return "Undef";
468 0 case TYPE_CUSTOM: return "custom"; /* # uncoverable statement */
469 0 default: return "unknown"; /* # uncoverable statement */
470 }
471 }
472  
473 /* Apply coercion to a value for a slot (slot-level, C-level, and Perl-registered) */
474 72 static SV* apply_slot_coercion(pTHX_ SV *val, SlotSpec *spec) {
475 /* Slot-level coerce(callback) */
476 72 ▶ if (spec->has_coerce && spec->coerce_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce && spec->coerce_cb) { 22 50 72
if (spec->has_coerce && spec->coerce_cb) { 20 2 22
477 20 dSP;
478 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
479 20 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 20 20
480 20 PUTBACK;
481 20 call_sv(spec->coerce_cb, G_SCALAR);
482 20 SPAGAIN;
483 20 val = POPs;
484 20 PUTBACK;
485 }
486 /* C-registered type coercion with user-data (fast path) */
487 72 ▶ 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 22 72
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
488 3 val = spec->registered->coerce_ex(aTHX_ val, spec->registered->data);
489 }
490 /* C-registered type coercion (fast path) */
491 72 ▶ 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 22 72
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
492 3 val = spec->registered->coerce(aTHX_ val);
493 }
494 /* Perl-registered type coercion (from register_type) */
495 72 ▶ 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 22 72
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
496 5 dSP;
497 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
498 5 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 5 5
499 5 PUTBACK;
500 5 call_sv(spec->registered->perl_coerce, G_SCALAR);
501 5 SPAGAIN;
502 5 val = POPs;
503 5 PUTBACK;
504 }
505 72 return val;
506 }
507  
508 /* Check a value against a slot's type constraint (handles both C and Perl callbacks) */
509 35580 static bool check_slot_type(pTHX_ SV *val, SlotSpec *spec) {
510 /*
511 # uncoverable branch true
512 # uncoverable condition left count:1
513 # uncoverable condition left count:2 */
514 35580 ▶ if (!spec || !spec->has_type) return true;
Branch: 2 branches
BranchTrueFalseTotal
if (!spec || !spec->has_type) return true; 35580 0 35580
if (!spec || !spec->has_type) return true; 0 35580 35580
515
516 35580 ▶ if (spec->type_id != TYPE_CUSTOM) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->type_id != TYPE_CUSTOM) { 35529 51 35580
517 35529 return check_builtin_type(aTHX_ val, spec->type_id);
518 }
519
520 /*
521 # uncoverable branch true
522 # uncoverable condition left count:1 */
523 51 ▶ if (!spec->registered) return true;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->registered) return true; 0 51 51
524
525 /* Try C function with user-data first (fast path - ~5 cycles) */
526 51 ▶ if (spec->registered->check_ex) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->check_ex) { 6 45 51
527 6 return spec->registered->check_ex(aTHX_ val, spec->registered->data);
528 }
529  
530 /* Try C function next (fast path - ~5 cycles) */
531 45 ▶ if (spec->registered->check) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->check) { 7 38 45
532 7 return spec->registered->check(aTHX_ val);
533 }
534
535 /* Fall back to Perl callback (~100 cycles) */
536 38 ▶ if (spec->registered->perl_check) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->registered->perl_check) { 33 5 38
537 33 dSP;
538 int count;
539 33 bool result = false;
540 SV *result_sv;
541 33 ENTER;
542 33 SAVETMPS;
543 33 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 33 33
544 33 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 33 33
545 33 PUTBACK;
546 33 count = call_sv(spec->registered->perl_check, G_SCALAR);
547 33 SPAGAIN;
548 /*
549 # uncoverable branch false
550 # uncoverable condition right count:1 */
551 33 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 33 0 33
552 33 result_sv = POPs;
553 33 result = SvTRUE(result_sv);
554 }
555 33 PUTBACK;
556 33 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 33 0 33
557 33 LEAVE;
558 33 return result;
559 }
560
561 5 return true;
562 }
563  
564 /* ============================================
565 Slot spec parser: "name:Type:default(val)"
566 ============================================ */
567  
568 /* ============================================
569 handles(...) - tables and parsing
570 ============================================ */
571  
572 /* The slot types handles supports and the kind of delegation each gets,
573 * indexed by handle_kind_of */
574 static const struct {
575 U8 kind;
576 const char *name;
577 } handle_kinds[] = {
578 { HK_METHOD, "Object" },
579 { HK_CODE, "CodeRef" },
580 { HK_ARRAY, "ArrayRef" },
581 { HK_HASH, "HashRef" }
582 };
583  
584 /* The handle_kinds row for a slot type, or -1 when handles does not support it */
585 254 static IV handle_kind_of(BuiltinTypeID type) {
586 254 switch (type) {
587 123 case TYPE_OBJECT: return 0;
588 28 case TYPE_CODEREF: return 1;
589 61 case TYPE_ARRAYREF: return 2;
590 22 case TYPE_HASHREF: return 3;
591 20 default: return -1;
592 }
593 }
594  
595 /* A native array or hash method. min_args and max_args count the arguments
596 * after the invocant, max_args -1 for no limit. */
597 typedef struct {
598 const char *name;
599 I8 min_args;
600 I8 max_args;
601 U8 mutates; /* changes the container: refused on a readonly slot,
602 croaks on a frozen object, fires the trigger */
603 U8 pairs; /* arguments are key/value pairs */
604 const char *alias_of; /* another name for that op */
605 } NativeOp;
606  
607 static const NativeOp array_ops[] = {
608 { "push", 1, -1, 1, 0, NULL },
609 { "pop", 0, 0, 1, 0, NULL },
610 { "shift", 0, 0, 1, 0, NULL },
611 { "unshift", 1, -1, 1, 0, NULL },
612 { "list", 0, 0, 0, 0, NULL },
613 { "count", 0, 0, 0, 0, NULL },
614 { "is_empty", 0, 0, 0, 0, NULL },
615 { "get", 1, 1, 0, 0, NULL },
616 { "set", 2, 2, 1, 0, NULL },
617 { "clear", 0, 0, 1, 0, NULL },
618 { "elements", 0, 0, 0, 0, "list" },
619 { NULL, 0, 0, 0, 0, NULL }
620 };
621  
622 /* Indices into array_ops, in its order */
623 enum { AOP_PUSH = 0, AOP_POP, AOP_SHIFT, AOP_UNSHIFT, AOP_LIST, AOP_COUNT,
624 AOP_IS_EMPTY, AOP_GET, AOP_SET, AOP_CLEAR };
625  
626 static const NativeOp hash_ops[] = {
627 { "get", 1, -1, 0, 0, NULL },
628 { "set", 2, -1, 1, 1, NULL },
629 { "exists", 1, 1, 0, 0, NULL },
630 { "defined", 1, 1, 0, 0, NULL },
631 { "delete", 1, -1, 1, 0, NULL },
632 { "keys", 0, 0, 0, 0, NULL },
633 { "values", 0, 0, 0, 0, NULL },
634 { "kv", 0, 0, 0, 0, NULL },
635 { "count", 0, 0, 0, 0, NULL },
636 { "is_empty", 0, 0, 0, 0, NULL },
637 { "clear", 0, 0, 1, 0, NULL },
638 { NULL, 0, 0, 0, 0, NULL }
639 };
640  
641 /* Indices into hash_ops, in its order */
642 enum { HOP_GET = 0, HOP_SET, HOP_EXISTS, HOP_DEFINED, HOP_DELETE, HOP_KEYS,
643 HOP_VALUES, HOP_KV, HOP_COUNT, HOP_IS_EMPTY, HOP_CLEAR };
644  
645 /* Method names a delegator may never take: Object::Proto's own, perl's
646 * special blocks and the methods of UNIVERSAL */
647 static const char *const reserved_methods[] = {
648 "new", "prototype", "set_prototype", "instance", "BUILD", "DEMOLISH", "DESTROY",
649 "AUTOLOAD", "import", "unimport", "isa", "can", "DOES", "VERSION",
650 "BEGIN", "END", "INIT", "CHECK", "UNITCHECK", NULL
651 };
652  
653 /* Whether name is reserved: one hash lookup. hash may be a hash already
654 * computed for name, as a stash entry has, or 0. */
655 317 static bool is_reserved_method(pTHX_ const char *name, STRLEN len, U32 hash) {
656 317 return hv_common_key_len(g_reserved_methods, name, (I32)len, HV_FETCH_ISEXISTS, NULL, hash) != NULL;
657 }
658  
659 /* Name -> index lookup for a table of native ops, an alias mapped straight to
660 * the op it stands for (which the table lists before it) */
661 180 static HV *build_op_index(pTHX_ const NativeOp *ops) {
662 180 HV *index = newHV();
663 IV i;
664 2160 ▶ for (i = 0; ops[i].name; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; ops[i].name; i++) { 1980 180 2160
665 3960 IV op = ops[i].alias_of
666 90 ? SvIV(*hv_fetch(index, ops[i].alias_of, strlen(ops[i].alias_of), 0))
667 1980 ▶ : i;
Branch: 1 branch
BranchTrueFalseTotal
: i; 90 1890 1980
668 1980 (void)hv_store(index, ops[i].name, strlen(ops[i].name), newSViv(op), 0);
669 }
670 180 return index;
671 }
672  
673 /* The index of the native op called name, or -1: one hash lookup */
674 164 static IV find_native_op(pTHX_ HV *index, SV *name) {
675 164 HE *he = hv_fetch_ent(index, name, 0, 0);
676 164 ▶ return he ? SvIV(HeVAL(he)) : -1;
Branch: 1 branch
BranchTrueFalseTotal
return he ? SvIV(HeVAL(he)) : -1; 162 2 164
677 }
678  
679 /* Strip leading and trailing white space from [*p, *p + *len) */
680 800 static void handles_trim(const char **p, STRLEN *len) {
681 854 ▶ while (*len && isSPACE_A(**p)) { (*p)++; (*len)--; }
Branch: 2 branches
BranchTrueFalseTotal
while (*len && isSPACE_A(**p)) { (*p)++; (*len)--; } 847 7 854
while (*len && isSPACE_A(**p)) { (*p)++; (*len)--; } 54 793 847
682 806 ▶ while (*len && isSPACE_A((*p)[*len - 1])) (*len)--;
Branch: 2 branches
BranchTrueFalseTotal
while (*len && isSPACE_A((*p)[*len - 1])) (*len)--; 799 7 806
while (*len && isSPACE_A((*p)[*len - 1])) (*len)--; 6 793 799
683 800 }
684  
685 /* A method name: [A-Za-z_]\w* */
686 394 static bool handles_name_ok(const char *p, STRLEN len) {
687 STRLEN i;
688 394 ▶ if (len == 0 || !isIDFIRST_A(*p)) return false;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 0 || !isIDFIRST_A(*p)) return false; 390 4 394
if (len == 0 || !isIDFIRST_A(*p)) return false; 1 389 390
689 1830 ▶ for (i = 1; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) { 1444 386 1830
690 1444 ▶ if (!isWORDCHAR_A(p[i])) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!isWORDCHAR_A(p[i])) return false; 3 1441 1444
691 }
692 386 return true;
693 }
694  
695 /* A package name: [A-Za-z_]\w*(::\w+)* */
696 35 static bool handles_role_ok(const char *p, STRLEN len) {
697 STRLEN i;
698 35 ▶ if (len == 0 || !isIDFIRST_A(*p)) return false;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 0 || !isIDFIRST_A(*p)) return false; 34 1 35
if (len == 0 || !isIDFIRST_A(*p)) return false; 1 33 34
699 283 ▶ for (i = 1; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) { 255 28 283
700 255 ▶ if (p[i] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] == ':') { 13 242 255
701 13 ▶ if (i + 2 >= len || p[i + 1] != ':' || !isWORDCHAR_A(p[i + 2])) return false;
Branch: 3 branches
BranchTrueFalseTotal
if (i + 2 >= len || p[i + 1] != ':' || !isWORDCHAR_A(p[i + 2])) return false; 11 2 13
if (i + 2 >= len || p[i + 1] != ':' || !isWORDCHAR_A(p[i + 2])) return false; 10 1 11
if (i + 2 >= len || p[i + 1] != ':' || !isWORDCHAR_A(p[i + 2])) return false; 1 9 10
702 9 i += 2;
703 242 ▶ } else if (!isWORDCHAR_A(p[i])) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!isWORDCHAR_A(p[i])) { 1 241 242
704 1 return false;
705 }
706 }
707 28 return true;
708 }
709  
710 /* The method names (the locals) among local, remote pairs, as a mortal hash
711 * for O(1) lookups; NULL pairs give an empty one */
712 237 static HV *handles_locals(pTHX_ AV *pairs) {
713 237 HV *locals = (HV*)sv_2mortal((SV*)newHV());
714 SSize_t i;
715 255 ▶ for (i = 0; pairs && i < av_len(pairs); i += 2) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; pairs && i < av_len(pairs); i += 2) { 33 222 255
for (i = 0; pairs && i < av_len(pairs); i += 2) { 18 15 33
716 18 SV *local = AvARRAY(pairs)[i];
717 18 ▶ if (SvOK(local)) (void)hv_store_ent(locals, local, &PL_sv_yes, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(local)) (void)hv_store_ent(locals, local, &PL_sv_yes, 0); 16 2 18
718 }
719 237 return locals;
720 }
721  
722 /* Parse the argument of handles(...) - "a, b=>c, role:Some::Role" - into
723 * the spec's local, remote pairs. A second handles(...) in the same spec adds
724 * to the first. Only the names are checked here; what they mean depends on
725 * the slot's type, which is final only when the slot joins a class. */
726 227 static void parse_handles(pTHX_ SlotSpec *spec, const char *arg, STRLEN arg_len) {
727 227 AV *pairs = (AV*)sv_2mortal((SV*)newAV());
728 HV *seen; /* the names so far, an earlier handles(...) in this spec's too */
729 227 const char *p = arg;
730 227 const char *end = arg + arg_len;
731 SSize_t i;
732  
733 {
734 227 const char *all = arg;
735 227 STRLEN all_len = arg_len;
736 227 handles_trim(&all, &all_len);
737 227 ▶ if (all_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (all_len == 0) 2 225 227
738 2 croak("handles() needs at least one method for slot '%s'", spec->name);
739 }
740 225 seen = handles_locals(aTHX_ spec->handles);
741  
742 97 for (;;) {
743 322 const char *comma = (const char *)memchr(p, ',', end - p);
744 322 const char *entry = p;
745 322 ▶ STRLEN len = (comma ? comma : end) - p;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN len = (comma ? comma : end) - p; 98 224 322
746  
747 322 handles_trim(&entry, &len);
748 350 ▶ if (len >= 5 && memEQ(entry, "role:", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 5 && memEQ(entry, "role:", 5)) { 194 128 322
if (len >= 5 && memEQ(entry, "role:", 5)) { 35 159 194
749 35 const char *role = entry + 5;
750 35 STRLEN role_len = len - 5;
751 35 handles_trim(&role, &role_len);
752 35 ▶ if (!handles_role_ok(role, role_len))
Branch: 1 branch
BranchTrueFalseTotal
if (!handles_role_ok(role, role_len)) 7 28 35
753 7 croak("Invalid role name '%.*s' in handles for slot '%s'",
754 (int)role_len, role, spec->name);
755 28 av_push(pairs, newSV(0));
756 28 av_push(pairs, newSVpvn(role, role_len));
757 } else {
758 287 const char *local = entry;
759 287 STRLEN local_len = len;
760 287 const char *arrow = ninstr(entry, entry + len, "=>", "=>" + 2);
761 287 const char *remote = NULL;
762 287 STRLEN remote_len = 0;
763  
764 287 ▶ if (arrow) {
Branch: 1 branch
BranchTrueFalseTotal
if (arrow) { 108 179 287
765 108 local_len = arrow - entry;
766 108 remote = arrow + 2;
767 108 remote_len = entry + len - remote;
768 108 handles_trim(&local, &local_len);
769 108 handles_trim(&remote, &remote_len);
770 }
771 287 ▶ if (!handles_name_ok(local, local_len))
Branch: 1 branch
BranchTrueFalseTotal
if (!handles_name_ok(local, local_len)) 5 282 287
772 5 croak("Invalid method name '%.*s' in handles for slot '%s'",
773 (int)local_len, local, spec->name);
774 282 ▶ if (arrow && !handles_name_ok(remote, remote_len))
Branch: 2 branches
BranchTrueFalseTotal
if (arrow && !handles_name_ok(remote, remote_len)) 107 175 282
if (arrow && !handles_name_ok(remote, remote_len)) 3 104 107
775 3 croak("Invalid method name '%.*s' in handles for slot '%s'",
776 (int)remote_len, remote, spec->name);
777 279 ▶ if (hv_exists(seen, local, local_len))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, local, local_len)) 3 276 279
778 3 croak("Method '%.*s' appears twice in handles for slot '%s'",
779 (int)local_len, local, spec->name);
780 276 (void)hv_store(seen, local, local_len, &PL_sv_yes, 0);
781 276 av_push(pairs, newSVpvn(local, local_len));
782 276 ▶ av_push(pairs, arrow ? newSVpvn(remote, remote_len) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pairs, arrow ? newSVpvn(remote, remote_len) : newSV(0)); 102 174 276
783 }
784  
785 304 ▶ if (!comma) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!comma) break; 207 97 304
786 97 p = comma + 1;
787 }
788  
789 207 ▶ if (!spec->handles) spec->handles = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->handles) spec->handles = newAV(); 205 2 207
790 801 ▶ for (i = 0; i <= av_len(pairs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(pairs); i++) { 594 207 801
791 594 av_push(spec->handles, SvREFCNT_inc(AvARRAY(pairs)[i]));
792 }
793 207 spec->has_handles = 1;
794 207 }
795  
796 /* Croak when a function-style modifier's '(' is never closed, before anything
797 * is allocated. The scan follows parse_slot_spec: past the name, a '(' opens
798 * an argument and parentheses nest inside it, while a ')' outside any argument
799 * is just part of a modifier's name. Once this passes, every argument scan in
800 * parse_slot_spec finds its closing ')'. */
801 1161 static void check_spec_parens(pTHX_ const char *spec_str, STRLEN len) {
802 1161 const char *p = spec_str;
803 1161 const char *end = spec_str + len;
804 1161 IV depth = 0;
805  
806 5588 ▶ while (p < end && *p != ':') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ':') p++; 5272 316 5588
while (p < end && *p != ':') p++; 4427 845 5272
807 16814 ▶ for (; p < end; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; p < end; p++) { 15653 1161 16814
808 15653 ▶ if (*p == '(') depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '(') depth++; 558 15095 15653
809 15095 ▶ else if (*p == ')' && depth > 0) depth--;
Branch: 2 branches
BranchTrueFalseTotal
else if (*p == ')' && depth > 0) depth--; 553 14542 15095
else if (*p == ')' && depth > 0) depth--; 552 1 553
810 }
811 1161 ▶ if (depth > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (depth > 0) 6 1155 1161
812 6 croak("Unterminated '(' in slot spec '%.*s'", (int)len, spec_str);
813 1155 }
814  
815 1161 static SlotSpec* parse_slot_spec(pTHX_ const char *spec_str, STRLEN len) {
816 SlotSpec *spec;
817 1161 const char *p = spec_str;
818 1161 const char *end = spec_str + len;
819 const char *name_start, *name_end;
820 STRLEN name_len;
821  
822 1161 check_spec_parens(aTHX_ spec_str, len);
823 1155 Newxz(spec, 1, SlotSpec);
824  
825 /* Parse property name (before first ':') */
826 1155 name_start = p;
827 5576 ▶ while (p < end && *p != ':') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ':') p++; 5260 316 5576
while (p < end && *p != ':') p++; 4421 839 5260
828 1155 name_end = p;
829  
830 1155 name_len = name_end - name_start;
831 1155 Newx(spec->name, name_len + 1, char);
832 1155 Copy(name_start, spec->name, name_len, char);
833 1155 spec->name[name_len] = '\0';
834
835 /* Parse modifiers after name */
836 2614 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 1480 1134 2614
837 const char *mod_start;
838 const char *arg_start;
839 const char *arg_end;
840 STRLEN mod_len;
841 STRLEN arg_len;
842 int paren_depth;
843  
844 /*
845 # uncoverable branch false
846 # uncoverable condition right count:1 */
847 1480 ▶ if (*p == ':') p++; /* Skip separator */
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ':') p++; /* Skip separator */ 1480 0 1480
848 1480 ▶ if (p >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) break; 1 1479 1480
849  
850 1479 mod_start = p;
851  
852 /* Check for function-style modifiers: default(...), trigger(...) */
853 9996 ▶ while (p < end && *p != ':' && *p != '(') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p != ':' && *p != '(') p++; 9604 392 9996
while (p < end && *p != ':' && *p != '(') p++; 9065 539 9604
while (p < end && *p != ':' && *p != '(') p++; 8517 548 9065
854  
855 1479 mod_len = p - mod_start;
856  
857 1479 ▶ if (p < end && *p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '(') { 1087 392 1479
if (p < end && *p == '(') { 548 539 1087
858 /* Function-style: default(value) or trigger(&callback) */
859 548 p++;
860 548 arg_start = p;
861 548 paren_depth = 1;
862 /* check_spec_parens has made sure the closing ')' is there */
863 5565 ▶ while (paren_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (paren_depth > 0) { 5017 548 5565
864 5017 ▶ if (*p == '(') paren_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '(') paren_depth++; 2 5015 5017
865 5015 ▶ else if (*p == ')') paren_depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == ')') paren_depth--; 550 4465 5015
866 5017 p++;
867 }
868 548 arg_end = p - 1; /* Before closing paren */
869 548 arg_len = arg_end - arg_start;
870
871 548 ▶ if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) { 438 110 548
if (mod_len == 7 && strncmp(mod_start, "default", 7) == 0) { 120 318 438
872 /* Parse default value */
873 120 spec->has_default = 1;
874 /* Simple default: copy as string and eval at runtime */
875 /* For now, support literal numbers and strings */
876 120 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 115 5 120
877 char *arg_copy;
878 115 Newx(arg_copy, arg_len + 1, char);
879 115 Copy(arg_start, arg_copy, arg_len, char);
880 115 arg_copy[arg_len] = '\0';
881
882 /* Try to parse as number */
883 115 ▶ if (arg_copy[0] >= '0' && arg_copy[0] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (arg_copy[0] >= '0' && arg_copy[0] <= '9') { 107 8 115
if (arg_copy[0] >= '0' && arg_copy[0] <= '9') { 61 46 107
884 122 ▶ if (strchr(arg_copy, '.')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(arg_copy, '.')) { 4 57 61
885 4 spec->default_sv = newSVnv(atof(arg_copy));
886 } else {
887 57 spec->default_sv = newSViv(atoi(arg_copy));
888 }
889 54 ▶ } else if (arg_copy[0] == '-' && arg_len > 1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (arg_copy[0] == '-' && arg_len > 1) { 4 50 54
} else if (arg_copy[0] == '-' && arg_len > 1) { 3 1 4
890 6 ▶ if (strchr(arg_copy, '.')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(arg_copy, '.')) { 1 2 3
891 1 spec->default_sv = newSVnv(atof(arg_copy));
892 } else {
893 2 spec->default_sv = newSViv(atoi(arg_copy));
894 }
895 51 ▶ } else if (arg_copy[0] == '\'' || arg_copy[0] == '"') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (arg_copy[0] == '\'' || arg_copy[0] == '"') { 50 1 51
} else if (arg_copy[0] == '\'' || arg_copy[0] == '"') { 1 49 50
896 /* String literal - strip quotes */
897 2 ▶ if (arg_len >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len >= 2) { 1 1 2
898 1 spec->default_sv = newSVpvn(arg_copy + 1, arg_len - 2);
899 } else {
900 1 spec->default_sv = newSVpvn("", 0);
901 }
902 49 ▶ } else if (strncmp(arg_copy, "undef", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "undef", 5) == 0) { 3 46 49
903 3 spec->default_sv = newSV(0);
904 46 ▶ } else if (strncmp(arg_copy, "[]", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "[]", 2) == 0) { 11 35 46
905 11 spec->default_sv = newRV_noinc((SV*)newAV());
906 35 ▶ } else if (strncmp(arg_copy, "{}", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(arg_copy, "{}", 2) == 0) { 7 28 35
907 7 spec->default_sv = newRV_noinc((SV*)newHV());
908 } else {
909 /* Default to string */
910 28 spec->default_sv = newSVpvn(arg_copy, arg_len);
911 }
912 115 Safefree(arg_copy);
913 }
914 428 ▶ } 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) { 318 110 428
} else if (mod_len == 7 && strncmp(mod_start, "trigger", 7) == 0) { 27 291 318
915 /* trigger(&callback) - store callback name for later resolution */
916 27 spec->has_trigger = 1;
917 /* Note: callback resolution happens at runtime in Perl layer */
918 /* For now, store as string - will be resolved in object.pm */
919 27 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 21 6 27
920 char *cb_copy;
921 21 Newx(cb_copy, arg_len + 1, char);
922 21 Copy(arg_start, cb_copy, arg_len, char);
923 21 cb_copy[arg_len] = '\0';
924 /* Store as SV for later resolution */
925 21 spec->trigger_cb = newSVpvn(cb_copy, arg_len);
926 21 Safefree(cb_copy);
927 }
928 401 ▶ } 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) { 85 316 401
} else if (mod_len == 6 && strncmp(mod_start, "coerce", 6) == 0) { 17 68 85
929 /* coerce(&callback) */
930 17 spec->has_coerce = 1;
931 17 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 15 2 17
932 char *cb_copy;
933 15 Newx(cb_copy, arg_len + 1, char);
934 15 Copy(arg_start, cb_copy, arg_len, char);
935 15 cb_copy[arg_len] = '\0';
936 15 spec->coerce_cb = newSVpvn(cb_copy, arg_len);
937 15 Safefree(cb_copy);
938 }
939 384 ▶ } 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) { 291 93 384
} else if (mod_len == 7 && strncmp(mod_start, "builder", 7) == 0) { 50 241 291
940 /* builder(method_name) - builder method, called at new() unless :lazy */
941 50 spec->has_builder = 1;
942 50 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 48 2 50
943 char *cb_copy;
944 48 Newx(cb_copy, arg_len + 1, char);
945 48 Copy(arg_start, cb_copy, arg_len, char);
946 48 cb_copy[arg_len] = '\0';
947 48 spec->builder_name = newSVpvn(cb_copy, arg_len);
948 48 Safefree(cb_copy);
949 } else {
950 /* Default builder name: _build_<property> */
951 char build_name[256];
952 2 snprintf(build_name, sizeof(build_name), "_build_%s", spec->name);
953 2 spec->builder_name = newSVpv(build_name, 0);
954 }
955 334 ▶ } 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) { 241 93 334
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 13 228 241
956 /* clearer(method_name) - custom clearer method name */
957 13 spec->has_clearer = 1;
958 13 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 12 1 13
959 char *name_copy;
960 12 Newx(name_copy, arg_len + 1, char);
961 12 Copy(arg_start, name_copy, arg_len, char);
962 12 name_copy[arg_len] = '\0';
963 12 spec->clearer_name = newSVpvn(name_copy, arg_len);
964 12 Safefree(name_copy);
965 }
966 321 ▶ } 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) { 10 311 321
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 9 1 10
967 /* predicate(method_name) - custom predicate method name */
968 9 spec->has_predicate = 1;
969 9 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 8 1 9
970 char *name_copy;
971 8 Newx(name_copy, arg_len + 1, char);
972 8 Copy(arg_start, name_copy, arg_len, char);
973 8 name_copy[arg_len] = '\0';
974 8 spec->predicate_name = newSVpvn(name_copy, arg_len);
975 8 Safefree(name_copy);
976 }
977 312 ▶ } 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) { 68 244 312
} else if (mod_len == 6 && strncmp(mod_start, "reader", 6) == 0) { 34 34 68
978 /* reader(method_name) - custom getter method name */
979 34 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 33 1 34
980 char *name_copy;
981 33 Newx(name_copy, arg_len + 1, char);
982 33 Copy(arg_start, name_copy, arg_len, char);
983 33 name_copy[arg_len] = '\0';
984 33 spec->reader_name = newSVpvn(name_copy, arg_len);
985 33 Safefree(name_copy);
986 }
987 278 ▶ } 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) { 34 244 278
} else if (mod_len == 6 && strncmp(mod_start, "writer", 6) == 0) { 33 1 34
988 /* writer(method_name) - custom setter method name */
989 33 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 32 1 33
990 char *name_copy;
991 32 Newx(name_copy, arg_len + 1, char);
992 32 Copy(arg_start, name_copy, arg_len, char);
993 32 name_copy[arg_len] = '\0';
994 32 spec->writer_name = newSVpvn(name_copy, arg_len);
995 32 Safefree(name_copy);
996 }
997 245 ▶ } else if (mod_len == 7 && strncmp(mod_start, "handles", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (mod_len == 7 && strncmp(mod_start, "handles", 7) == 0) { 228 17 245
} else if (mod_len == 7 && strncmp(mod_start, "handles", 7) == 0) { 227 1 228
998 227 parse_handles(aTHX_ spec, arg_start, arg_len);
999 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
1000 /* arg(init_arg_name) - alternate constructor argument name */
1001 14 ▶ if (arg_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg_len > 0) { 13 1 14
1002 char *name_copy;
1003 13 Newx(name_copy, arg_len + 1, char);
1004 13 Copy(arg_start, name_copy, arg_len, char);
1005 13 name_copy[arg_len] = '\0';
1006 13 spec->init_arg = newSVpvn(name_copy, arg_len);
1007 13 Safefree(name_copy);
1008 }
1009 }
1010 } else {
1011 /* Simple modifier: type name or flag */
1012 931 ▶ if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) { 143 788 931
if (mod_len == 8 && strncmp(mod_start, "required", 8) == 0) { 45 98 143
1013 45 spec->is_required = 1;
1014 886 ▶ } 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) { 98 788 886
} else if (mod_len == 8 && strncmp(mod_start, "readonly", 8) == 0) { 34 64 98
1015 34 spec->is_readonly = 1;
1016 852 ▶ } 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) { 90 762 852
} else if (mod_len == 4 && strncmp(mod_start, "lazy", 4) == 0) { 55 35 90
1017 55 spec->is_lazy = 1;
1018 797 ▶ } 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) { 35 762 797
} else if (mod_len == 4 && strncmp(mod_start, "weak", 4) == 0) { 20 15 35
1019 20 spec->is_weak = 1;
1020 777 ▶ } 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) { 108 669 777
} else if (mod_len == 7 && strncmp(mod_start, "private", 7) == 0) { 13 95 108
1021 13 spec->is_private = 1;
1022 764 ▶ } 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) { 95 669 764
} else if (mod_len == 7 && strncmp(mod_start, "clearer", 7) == 0) { 34 61 95
1023 34 spec->has_clearer = 1;
1024 /* Default clearer name: clear_<property> */
1025 730 ▶ } 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) { 26 704 730
} else if (mod_len == 9 && strncmp(mod_start, "predicate", 9) == 0) { 18 8 26
1026 18 spec->has_predicate = 1;
1027 /* Default predicate name: has_<property> */
1028 } else {
1029 /* Try as type name. A registered type takes precedence over
1030 * a built-in of the same name, so a type a user registered
1031 * keeps its meaning when a built-in of that name is added. */
1032 char *type_copy;
1033 712 SV **svp = NULL;
1034  
1035 712 Newx(type_copy, mod_len + 1, char);
1036 712 Copy(mod_start, type_copy, mod_len, char);
1037 712 type_copy[mod_len] = '\0';
1038  
1039 /*
1040 # uncoverable branch false
1041 # uncoverable condition right count:1 */
1042 712 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 712 0 712
1043 712 svp = hv_fetch(g_type_registry, type_copy, mod_len, 0);
1044 }
1045 712 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 23 689 712
1046 23 spec->registered = INT2PTR(RegisteredType*, SvIV(*svp));
1047 23 spec->type_id = TYPE_CUSTOM;
1048 23 spec->has_type = 1;
1049 } else {
1050 689 BuiltinTypeID type_id = parse_builtin_type(type_copy, mod_len);
1051 689 ▶ if (type_id != TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (type_id != TYPE_NONE) { 683 6 689
1052 683 spec->type_id = type_id;
1053 683 spec->has_type = 1;
1054 }
1055 }
1056 712 Safefree(type_copy);
1057 }
1058 }
1059 }
1060
1061 1135 spec->has_checks = spec->is_readonly | spec->is_required | spec->has_coerce
1062 1135 | (spec->type_id == TYPE_CUSTOM ? 1 : 0);
1063  
1064 1135 return spec;
1065 }
1066  
1067 /* Magic vtable for object flags */
1068 static MGVTBL object_magic_vtbl = {
1069 NULL, /* get */
1070 NULL, /* set */
1071 NULL, /* len */
1072 NULL, /* clear */
1073 NULL, /* free */
1074 NULL, /* copy */
1075 NULL, /* dup */
1076 NULL /* local */
1077 };
1078  
1079 /* Validate that an SV is a blessed array-backed object */
1080 #define VALIDATE_OBJECT(sv, funcname) \
1081 if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV || !SvOBJECT(SvRV(sv))) \
1082 croak("%s: argument is not an Object::Proto object", funcname)
1083  
1084 /* Get object magic (for flags) */
1085 43755 static MAGIC* get_object_magic(pTHX_ SV *obj) {
1086 MAGIC *mg;
1087 /*
1088 # uncoverable branch true
1089 # uncoverable condition left count:1 */
1090 43755 ▶ if (!SvROK(obj)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) return NULL; 0 43755 43755
1091 43755 mg = mg_find(SvRV(obj), PERL_MAGIC_ext);
1092 43758 ▶ while (mg) {
Branch: 1 branch
BranchTrueFalseTotal
while (mg) { 6253 37505 43758
1093 6253 ▶ if (mg->mg_virtual == &object_magic_vtbl) return mg;
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &object_magic_vtbl) return mg; 6250 3 6253
1094 3 mg = mg->mg_moremagic;
1095 }
1096 37505 return NULL;
1097 }
1098  
1099 /* A slot holding the shared undef is empty: before perl 5.20 av_fill put
1100 &PL_sv_undef in new slots rather than NULL, and it is read-only. */
1101 8298 static int slot_is_writable(SV *slot) {
1102 8298 ▶ 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 */ 8287 11 8298
return slot && slot != &PL_sv_undef; /* # uncoverable condition right count:2 */ 8287 0 8287
1103 }
1104  
1105 /* Add object magic */
1106 33 static MAGIC* add_object_magic(pTHX_ SV *obj) {
1107 MAGIC *mg;
1108 33 SV *rv = SvRV(obj);
1109 33 mg = sv_magicext(rv, NULL, PERL_MAGIC_ext, &object_magic_vtbl, NULL, 0);
1110 33 mg->mg_private = 0; /* flags */
1111 33 return mg;
1112 }
1113  
1114 /* ============================================
1115 Class definition and registration
1116 ============================================ */
1117  
1118 /* Clone a SlotSpec (deep copy for inheritance) */
1119 268 static SlotSpec* clone_slot_spec(pTHX_ const SlotSpec *src) {
1120 SlotSpec *dst;
1121 STRLEN name_len;
1122  
1123 268 Newxz(dst, 1, SlotSpec);
1124  
1125 268 name_len = strlen(src->name);
1126 268 Newx(dst->name, name_len + 1, char);
1127 268 Copy(src->name, dst->name, name_len + 1, char);
1128  
1129 268 dst->type_id = src->type_id;
1130 268 dst->registered = src->registered;
1131 268 dst->is_required = src->is_required;
1132 268 dst->is_readonly = src->is_readonly;
1133 268 dst->is_lazy = src->is_lazy;
1134 268 dst->is_weak = src->is_weak;
1135 268 dst->is_private = src->is_private;
1136 268 dst->has_default = src->has_default;
1137 268 dst->has_trigger = src->has_trigger;
1138 268 dst->has_coerce = src->has_coerce;
1139 268 dst->has_type = src->has_type;
1140 268 dst->has_builder = src->has_builder;
1141 268 dst->has_clearer = src->has_clearer;
1142 268 dst->has_predicate = src->has_predicate;
1143 268 dst->has_checks = src->has_checks; /* the inlined typed setter skips readonly/required/coerce without it */
1144  
1145 268 ▶ 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); 78 190 268
1146 268 ▶ 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); 9 259 268
1147 268 ▶ 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); 7 261 268
1148 268 ▶ 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); 9 259 268
1149 268 ▶ 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); 6 262 268
1150 268 ▶ 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 266 268
1151 268 ▶ 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); 9 259 268
1152 268 ▶ 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); 4 264 268
1153 268 ▶ 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); 4 264 268
1154 /* An inherited attribute stays private to the package that declared it */
1155 268 dst->private_owner = savepv(src->private_owner); /* savepv(NULL) is NULL */
1156 268 dst->private_also = savepv(src->private_also);
1157  
1158 /* handles as written are shared; what they resolve to, and the default
1159 * they add, are worked out again for the clone's own final spec - a
1160 * +attr may give it a builder that an inherited implicit [] would mask */
1161 268 dst->handles = (AV*)SvREFCNT_inc((SV*)src->handles);
1162 268 dst->has_handles = src->has_handles;
1163 268 ▶ if (src->default_implicit) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->default_implicit) { 20 248 268
1164 20 dst->has_default = 0;
1165 20 SvREFCNT_dec(dst->default_sv);
1166 20 dst->default_sv = NULL;
1167 }
1168  
1169 268 return dst;
1170 }
1171  
1172 /* The parent's handles with the override's added: an override entry with the
1173 * same method name replaces the parent's. Returns a new array. */
1174 12 static AV* merge_handles(pTHX_ AV *parent, AV *override) {
1175 12 AV *merged = newAV();
1176 12 HV *replaced = handles_locals(aTHX_ override);
1177 SSize_t i;
1178 28 ▶ for (i = 0; parent && i < av_len(parent); i += 2) { /* the parent may have none */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; parent && i < av_len(parent); i += 2) { /* the parent may have none */ 26 2 28
for (i = 0; parent && i < av_len(parent); i += 2) { /* the parent may have none */ 16 10 26
1179 16 SV *local = AvARRAY(parent)[i];
1180 16 ▶ if (SvOK(local) && hv_exists_ent(replaced, local, 0)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(local) && hv_exists_ent(replaced, local, 0)) continue; 15 1 16
if (SvOK(local) && hv_exists_ent(replaced, local, 0)) continue; 4 11 15
1181 12 av_push(merged, SvREFCNT_inc(local));
1182 12 av_push(merged, SvREFCNT_inc(AvARRAY(parent)[i + 1]));
1183 }
1184 42 ▶ for (i = 0; i <= av_len(override); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(override); i++) { 30 12 42
1185 30 av_push(merged, SvREFCNT_inc(AvARRAY(override)[i]));
1186 }
1187 12 return merged;
1188 }
1189  
1190 /* Merge child override modifiers onto a clone of parent spec.
1191 * Only fields that are explicitly set in override are applied.
1192 * This enables Moo/Moose-style '+attr' syntax for inheritance.
1193 * Called at define-time only - zero runtime overhead. */
1194 46 static SlotSpec* merge_slot_spec(pTHX_ const SlotSpec *parent, const SlotSpec *override) {
1195 46 SlotSpec *merged = clone_slot_spec(aTHX_ parent);
1196
1197 /* Override type if specified */
1198 46 ▶ if (override->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_type) { 4 42 46
1199 4 merged->type_id = override->type_id;
1200 4 merged->registered = override->registered;
1201 4 merged->has_type = 1;
1202 }
1203
1204 /* Override default if specified */
1205 46 ▶ if (override->has_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_default) { 10 36 46
1206 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
1207 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
1208 10 merged->has_default = 1;
1209 }
1210
1211 /* Override trigger if specified */
1212 46 ▶ if (override->has_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_trigger) { 3 43 46
1213 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
1214 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
1215 3 merged->has_trigger = 1;
1216 }
1217
1218 /* Override coerce if specified */
1219 46 ▶ if (override->has_coerce) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_coerce) { 3 43 46
1220 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
1221 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
1222 3 merged->has_coerce = 1;
1223 }
1224
1225 /* Override builder if specified */
1226 46 ▶ if (override->has_builder) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_builder) { 3 43 46
1227 3 ▶ if (merged->builder_name) SvREFCNT_dec(merged->builder_name);
Branch: 1 branch
BranchTrueFalseTotal
if (merged->builder_name) SvREFCNT_dec(merged->builder_name); 1 2 3
1228 3 ▶ 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 */ 3 0 3
1229 3 merged->has_builder = 1;
1230 }
1231
1232 /* Boolean flags - only set if explicitly enabled in override */
1233 46 ▶ if (override->is_required) merged->is_required = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_required) merged->is_required = 1; 1 45 46
1234 46 ▶ if (override->is_readonly) merged->is_readonly = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_readonly) merged->is_readonly = 1; 2 44 46
1235 46 ▶ if (override->is_lazy) merged->is_lazy = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_lazy) merged->is_lazy = 1; 1 45 46
1236 46 ▶ if (override->is_weak) merged->is_weak = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_weak) merged->is_weak = 1; 2 44 46
1237 46 ▶ if (override->is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->is_private) { 2 44 46
1238 /* Made private by the child, so the child is the one package that may call it */
1239 2 merged->is_private = 1;
1240 2 Safefree(merged->private_owner);
1241 2 Safefree(merged->private_also);
1242 2 merged->private_owner = savepv(override->private_owner);
1243 2 merged->private_also = NULL;
1244 }
1245
1246 /* Clearer/predicate */
1247 46 ▶ if (override->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_clearer) { 3 43 46
1248 3 merged->has_clearer = 1;
1249 3 ▶ if (override->clearer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->clearer_name) { 2 1 3
1250 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
1251 2 merged->clearer_name = SvREFCNT_inc(override->clearer_name);
1252 }
1253 }
1254 46 ▶ if (override->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_predicate) { 3 43 46
1255 3 merged->has_predicate = 1;
1256 3 ▶ if (override->predicate_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->predicate_name) { 2 1 3
1257 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
1258 2 merged->predicate_name = SvREFCNT_inc(override->predicate_name);
1259 }
1260 }
1261
1262 /* Reader/writer/init_arg */
1263 46 ▶ if (override->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->reader_name) { 2 44 46
1264 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
1265 2 merged->reader_name = SvREFCNT_inc(override->reader_name);
1266 }
1267 46 ▶ if (override->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->writer_name) { 2 44 46
1268 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
1269 2 merged->writer_name = SvREFCNT_inc(override->writer_name);
1270 }
1271 46 ▶ if (override->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->init_arg) { 2 44 46
1272 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
1273 2 merged->init_arg = SvREFCNT_inc(override->init_arg);
1274 }
1275
1276 /* handles: the union, the override winning for a method name */
1277 46 ▶ if (override->has_handles) {
Branch: 1 branch
BranchTrueFalseTotal
if (override->has_handles) { 12 34 46
1278 12 AV *was = merged->handles;
1279 12 merged->handles = merge_handles(aTHX_ was, override->handles);
1280 12 merged->has_handles = 1;
1281 12 SvREFCNT_dec((SV*)was);
1282 }
1283  
1284 /* Recompute has_checks flag */
1285 46 merged->has_checks = merged->is_readonly | merged->is_required | merged->has_coerce
1286 46 | (merged->type_id == TYPE_CUSTOM ? 1 : 0);
1287
1288 46 return merged;
1289 }
1290  
1291 /* ============================================
1292 handles(...) - resolving against the slot's final type
1293 ============================================ */
1294  
1295 /* A slot type's name, for messages */
1296 20 static const char *slot_type_name(const SlotSpec *spec) {
1297 20 ▶ return spec->type_id == TYPE_CUSTOM ? spec->registered->name : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
return spec->type_id == TYPE_CUSTOM ? spec->registered->name : type_id_to_name(spec->type_id); 1 19 20
1298 }
1299  
1300 /* Add one delegator to out (local, remote, op triples) */
1301 386 static void handles_push(pTHX_ AV *out, HV *seen, SV *local, SV *remote, IV op) {
1302 386 (void)hv_store_ent(seen, local, &PL_sv_yes, 0);
1303 386 av_push(out, SvREFCNT_inc(local));
1304 386 av_push(out, SvREFCNT_inc(remote));
1305 386 av_push(out, newSViv(op));
1306 386 }
1307  
1308 /* A method the role brings, delegated under its own name unless a method
1309 * given explicitly already has that name. Counted either way. */
1310 71 static void handles_push_role_method(pTHX_ AV *out, HV *seen, SV *name, IV *found) {
1311 71 (*found)++;
1312 71 ▶ if (hv_exists_ent(seen, name, 0)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(seen, name, 0)) return; 6 65 71
1313 65 handles_push(aTHX_ out, seen, name, name, 0);
1314 }
1315  
1316 /* Push onto names every method a slot gives a class: its accessor, and its
1317 * reader, writer, clearer and predicate when it has them */
1318 312 static void slot_method_names(pTHX_ const SlotSpec *spec, AV *names) {
1319 312 av_push(names, newSVpv(spec->name, 0));
1320 312 ▶ if (spec->reader_name) av_push(names, newSVsv(spec->reader_name));
Branch: 1 branch
BranchTrueFalseTotal
if (spec->reader_name) av_push(names, newSVsv(spec->reader_name)); 11 301 312
1321 312 ▶ if (spec->writer_name) av_push(names, newSVsv(spec->writer_name));
Branch: 1 branch
BranchTrueFalseTotal
if (spec->writer_name) av_push(names, newSVsv(spec->writer_name)); 3 309 312
1322 312 ▶ if (spec->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_clearer) { 20 292 312
1323 20 ▶ av_push(names, spec->clearer_name ? newSVsv(spec->clearer_name)
Branch: 1 branch
BranchTrueFalseTotal
av_push(names, spec->clearer_name ? newSVsv(spec->clearer_name) 9 11 20
1324 : newSVpvf("clear_%s", spec->name));
1325 }
1326 312 ▶ if (spec->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_predicate) { 5 307 312
1327 5 ▶ av_push(names, spec->predicate_name ? newSVsv(spec->predicate_name)
Branch: 1 branch
BranchTrueFalseTotal
av_push(names, spec->predicate_name ? newSVsv(spec->predicate_name) 2 3 5
1328 : newSVpvf("has_%s", spec->name));
1329 }
1330 312 }
1331  
1332 /* Every method within a role (D8): the methods it requires, the subs defined
1333 * in its package - not imported ones, not constants, not _private ones nor
1334 * reserved names - in sorted order, and the methods its slots give a class
1335 * that composes it, except for private slots, whose methods the delegating
1336 * class could not call. The role is loaded if it is not defined yet, as with()
1337 * does, and read as it is now. */
1338 27 static void handles_expand_role(pTHX_ SlotSpec *spec, SV *role_sv, AV *out, HV *seen) {
1339 STRLEN role_len;
1340 27 const char *role_pv = SvPV(role_sv, role_len);
1341 27 RoleMeta *role = get_role_meta(aTHX_ role_pv, role_len);
1342 27 AV *subs = (AV*)sv_2mortal((SV*)newAV());
1343 27 IV found = 0;
1344 IV i;
1345  
1346 27 ▶ if (!role) {
Branch: 1 branch
BranchTrueFalseTotal
if (!role) { 4 23 27
1347 4 load_module(PERL_LOADMOD_NOIMPORT, newSVpvn(role_pv, role_len), NULL);
1348 3 role = get_role_meta(aTHX_ role_pv, role_len);
1349 3 ▶ if (!role)
Branch: 1 branch
BranchTrueFalseTotal
if (!role) 1 2 3
1350 1 croak("Role '%s' in handles for slot '%s' is not defined", role_pv, spec->name);
1351 }
1352  
1353 59 ▶ for (i = 0; i < role->required_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role->required_count; i++) { 34 25 59
1354 34 handles_push_role_method(aTHX_ out, seen,
1355 34 sv_2mortal(newSVpv(role->required_methods[i], 0)), &found);
1356 }
1357  
1358 /* The role's package is walked bucket by bucket so that its iterator is
1359 * left alone. A package with keys has its buckets; an empty one may not. */
1360 25 ▶ if (HvTOTALKEYS(role->stash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (HvTOTALKEYS(role->stash)) { 12 13 25
1361 12 HE **buckets = HvARRAY(role->stash);
1362 STRLEN b;
1363 132 ▶ for (b = 0; b <= HvMAX(role->stash); b++) {
Branch: 1 branch
BranchTrueFalseTotal
for (b = 0; b <= HvMAX(role->stash); b++) { 120 12 132
1364 HE *he;
1365 159 ▶ for (he = buckets[b]; he; he = HeNEXT(he)) {
Branch: 1 branch
BranchTrueFalseTotal
for (he = buckets[b]; he; he = HeNEXT(he)) { 39 120 159
1366 39 SV *entry = HeVAL(he);
1367 const char *name;
1368 CV *cv;
1369 /* Not a glob: a constant or a declaration without a body */
1370 39 ▶ if (SvTYPE(entry) != SVt_PVGV) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(entry) != SVt_PVGV) continue; 1 38 39
1371 38 cv = GvCV((GV*)entry);
1372 /* No sub, or one not compiled in the role's package: an
1373 * imported one, or a constant, which belongs to no package */
1374 38 ▶ if (!cv || CvSTASH(cv) != role->stash) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!cv || CvSTASH(cv) != role->stash) continue; 29 9 38
if (!cv || CvSTASH(cv) != role->stash) continue; 10 19 29
1375 19 name = HeKEY(he);
1376 19 ▶ if (*name == '_' || is_reserved_method(aTHX_ name, HeKLEN(he), HeHASH(he))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (*name == '_' || is_reserved_method(aTHX_ name, HeKLEN(he), HeHASH(he))) continue; 15 4 19
if (*name == '_' || is_reserved_method(aTHX_ name, HeKLEN(he), HeHASH(he))) continue; 2 13 15
1377 13 av_push(subs, newSVhek(HeKEY_hek(he))); /* keeps a UTF-8 name's flag */
1378 }
1379 }
1380 }
1381 25 sortsv(AvARRAY(subs), av_len(subs) + 1, Perl_sv_cmp);
1382 38 ▶ for (i = 0; i <= av_len(subs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(subs); i++) { 13 25 38
1383 13 handles_push_role_method(aTHX_ out, seen, AvARRAY(subs)[i], &found);
1384 }
1385  
1386 42 ▶ for (i = 0; i < role->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role->slot_count; i++) { 17 25 42
1387 AV *names;
1388 IV n;
1389 17 ▶ if (role->slots[i]->is_private) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (role->slots[i]->is_private) continue; 5 12 17
1390 12 names = (AV*)sv_2mortal((SV*)newAV());
1391 12 slot_method_names(aTHX_ role->slots[i], names);
1392 36 ▶ for (n = 0; n <= av_len(names); n++) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; n <= av_len(names); n++) { 24 12 36
1393 24 handles_push_role_method(aTHX_ out, seen, AvARRAY(names)[n], &found);
1394 }
1395 }
1396  
1397 25 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 2 23 25
1398 2 croak("Role '%s' in handles for slot '%s' has no methods", role_pv, spec->name);
1399 23 }
1400  
1401 /* Add a delegator given by name: what the name maps to depends on the kind */
1402 334 static void handles_push_named(pTHX_ SlotSpec *spec, U8 kind, const char *kind_name,
1403 AV *out, HV *seen, SV *local, SV *remote) {
1404 334 ▶ SV *target = remote ? remote : local; /* a bare name maps to itself */
Branch: 1 branch
BranchTrueFalseTotal
SV *target = remote ? remote : local; /* a bare name maps to itself */ 160 174 334
1405 334 ▶ const NativeOp *ops = kind == HK_ARRAY ? array_ops : hash_ops;
Branch: 1 branch
BranchTrueFalseTotal
const NativeOp *ops = kind == HK_ARRAY ? array_ops : hash_ops; 101 233 334
1406 334 ▶ HV *index = kind == HK_ARRAY ? g_array_op_index : g_hash_op_index;
Branch: 1 branch
BranchTrueFalseTotal
HV *index = kind == HK_ARRAY ? g_array_op_index : g_hash_op_index; 101 233 334
1407 IV op;
1408  
1409 334 switch (kind) {
1410 140 case HK_METHOD:
1411 140 handles_push(aTHX_ out, seen, local, target, 0);
1412 140 return;
1413 30 case HK_CODE:
1414 /* A bare name calls the code; =>call does too, =>method passes $self */
1415 48 ▶ if (!remote || strEQ(SvPV_nolen(remote), "call")) {
Branch: 2 branches
BranchTrueFalseTotal
if (!remote || strEQ(SvPV_nolen(remote), "call")) { 18 12 30
if (!remote || strEQ(SvPV_nolen(remote), "call")) { 7 11 18
1416 19 op = HANDLE_CODE_CALL;
1417 11 ▶ } else if (strEQ(SvPV_nolen(remote), "method")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(SvPV_nolen(remote), "method")) { 10 1 11
1418 10 op = HANDLE_CODE_METHOD;
1419 } else {
1420 1 croak("CodeRef handles for slot '%s' must map to call or method, not '%s'",
1421 spec->name, SvPV_nolen(remote));
1422 }
1423 29 ▶ handles_push(aTHX_ out, seen, local,
Branch: 1 branch
BranchTrueFalseTotal
handles_push(aTHX_ out, seen, local, 10 19 29
1424 sv_2mortal(newSVpv(op == HANDLE_CODE_METHOD ? "method" : "call", 0)), op);
1425 29 return;
1426 164 default: /* HK_ARRAY, HK_HASH */
1427 164 op = find_native_op(aTHX_ index, target);
1428 164 ▶ if (op < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (op < 0) { 2 162 164
1429 2 SV *valid = sv_2mortal(newSVpvs(""));
1430 IV i;
1431 24 ▶ for (i = 0; ops[i].name; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; ops[i].name; i++) { 22 2 24
1432 22 ▶ if (ops[i].alias_of) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ops[i].alias_of) continue; 1 21 22
1433 21 ▶ sv_catpvf(valid, "%s%s", i ? ", " : "", ops[i].name);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(valid, "%s%s", i ? ", " : "", ops[i].name); 19 2 21
1434 }
1435 4 ▶ croak("Unknown %s method '%s' in handles for slot '%s' (valid: %s)",
Branch: 1 branch
BranchTrueFalseTotal
croak("Unknown %s method '%s' in handles for slot '%s' (valid: %s)", 2 0 2
1436 kind_name, SvPV_nolen(target), spec->name, SvPV_nolen(valid));
1437 }
1438 162 ▶ if (ops[op].mutates && spec->is_readonly) {
Branch: 2 branches
BranchTrueFalseTotal
if (ops[op].mutates && spec->is_readonly) { 82 80 162
if (ops[op].mutates && spec->is_readonly) { 10 72 82
1439 20 croak("Cannot delegate %s=>%s on readonly slot '%s'",
1440 SvPV_nolen(local), ops[op].name, spec->name);
1441 }
1442 152 handles_push(aTHX_ out, seen, local, sv_2mortal(newSVpv(ops[op].name, 0)), op);
1443 152 return;
1444 }
1445 }
1446  
1447 /* Resolve a slot's handles against its final type, once it is about to join a
1448 * class, and check them: the type must support handles (D1, D18), a weak slot
1449 * only method delegation (D14), a native method must exist (D15) and not change
1450 * a readonly slot (D5), a CodeRef maps to call or method (D9), a role brings
1451 * every method within it (D8). An ArrayRef or HashRef slot with no default,
1452 * builder or required gets an implicit [] or {} (D3). Croaks before the class
1453 * is touched, so a caught error leaves it as it was. */
1454 1313 static void resolve_handles(pTHX_ SlotSpec *spec) {
1455 AV *out;
1456 HV *seen;
1457 U8 kind;
1458 IV k, i, n;
1459  
1460 1313 ▶ if (!spec->has_handles) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->has_handles) return; 1058 255 1313
1461  
1462 255 ▶ if (!spec->has_type)
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->has_type) 1 254 255
1463 1 croak("handles is not supported on an untyped slot '%s'", spec->name);
1464 254 k = handle_kind_of(spec->type_id);
1465 254 ▶ if (k < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (k < 0) { 20 234 254
1466 20 const char *type = slot_type_name(spec);
1467 20 ▶ croak("handles is not supported on %s %s slot '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak("handles is not supported on %s %s slot '%s'", 4 16 20
1468 strchr("AEIOU", *type) ? "an" : "a", type, spec->name);
1469 }
1470 234 kind = handle_kinds[k].kind;
1471  
1472 234 ▶ if (spec->is_weak && kind != HK_METHOD)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_weak && kind != HK_METHOD) 5 229 234
if (spec->is_weak && kind != HK_METHOD) 4 1 5
1473 4 croak("weak slot '%s' cannot have %s handles", spec->name, handle_kinds[k].name);
1474  
1475 230 ▶ if (kind == HK_ARRAY || kind == HK_HASH) {
Branch: 2 branches
BranchTrueFalseTotal
if (kind == HK_ARRAY || kind == HK_HASH) { 171 59 230
if (kind == HK_ARRAY || kind == HK_HASH) { 21 150 171
1476 80 ▶ svtype want = kind == HK_ARRAY ? SVt_PVAV : SVt_PVHV;
Branch: 1 branch
BranchTrueFalseTotal
svtype want = kind == HK_ARRAY ? SVt_PVAV : SVt_PVHV; 59 21 80
1477 80 ▶ if (spec->has_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_default) { 7 73 80
1478 7 ▶ if (!spec->default_sv || !SvROK(spec->default_sv) || SvTYPE(SvRV(spec->default_sv)) != want)
Branch: 3 branches
BranchTrueFalseTotal
if (!spec->default_sv || !SvROK(spec->default_sv) || SvTYPE(SvRV(spec->default_sv)) != want) 6 1 7
if (!spec->default_sv || !SvROK(spec->default_sv) || SvTYPE(SvRV(spec->default_sv)) != want) 4 2 6
if (!spec->default_sv || !SvROK(spec->default_sv) || SvTYPE(SvRV(spec->default_sv)) != want) 2 2 4
1479 5 ▶ croak("The default of %s slot '%s' must be %s for handles",
Branch: 1 branch
BranchTrueFalseTotal
croak("The default of %s slot '%s' must be %s for handles", 4 1 5
1480 handle_kinds[k].name, spec->name, kind == HK_ARRAY ? "[]" : "{}");
1481 73 ▶ } else if (!spec->has_builder && !spec->is_required) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!spec->has_builder && !spec->is_required) { 67 6 73
} else if (!spec->has_builder && !spec->is_required) { 64 3 67
1482 /* Not marked in has_any_defaults (note_slot_flags), which would
1483 * change how new() reads positional arguments */
1484 64 spec->has_default = 1;
1485 64 spec->default_implicit = 1;
1486 64 ▶ spec->default_sv = newRV_noinc(kind == HK_ARRAY ? (SV*)newAV() : (SV*)newHV());
Branch: 1 branch
BranchTrueFalseTotal
spec->default_sv = newRV_noinc(kind == HK_ARRAY ? (SV*)newAV() : (SV*)newHV()); 45 19 64
1487 }
1488 }
1489  
1490 225 out = (AV*)sv_2mortal((SV*)newAV());
1491 225 seen = (HV*)sv_2mortal((SV*)newHV());
1492 225 n = av_len(spec->handles) + 1;
1493  
1494 /* Named entries first, so that a name given explicitly wins over the same
1495 * name brought by a role */
1496 575 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 363 212 575
1497 363 SV *local = AvARRAY(spec->handles)[i];
1498 363 SV *remote = AvARRAY(spec->handles)[i + 1];
1499 363 ▶ if (!SvOK(local)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(local)) continue; 29 334 363
1500 334 handles_push_named(aTHX_ spec, kind, handle_kinds[k].name, out, seen,
1501 334 ▶ local, SvOK(remote) ? remote : NULL);
Branch: 1 branch
BranchTrueFalseTotal
local, SvOK(remote) ? remote : NULL); 160 174 334
1502 }
1503 556 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 350 206 556
1504 350 ▶ if (SvOK(AvARRAY(spec->handles)[i])) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(AvARRAY(spec->handles)[i])) continue; 321 29 350
1505 29 ▶ if (kind != HK_METHOD)
Branch: 1 branch
BranchTrueFalseTotal
if (kind != HK_METHOD) 2 27 29
1506 2 croak("handles(role:...) needs an Object slot, '%s' is %s",
1507 spec->name, handle_kinds[k].name);
1508 27 handles_expand_role(aTHX_ spec, AvARRAY(spec->handles)[i + 1], out, seen);
1509 }
1510  
1511 206 n = (av_len(out) + 1) / 3;
1512 206 ▶ Newxz(spec->resolved, n, ResolvedHandle);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(spec->resolved, n, ResolvedHandle); 0 206 206
1513 591 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 385 206 591
1514 385 ResolvedHandle *h = &spec->resolved[i];
1515 385 SV *remote = AvARRAY(out)[i * 3 + 1];
1516 385 h->local = SvREFCNT_inc(AvARRAY(out)[i * 3]);
1517 /* A method name is made a shared hash key once here, so that each call
1518 * looks the method up without hashing or copying the name */
1519 385 ▶ if (kind == HK_METHOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind == HK_METHOD) { 205 180 385
1520 STRLEN len;
1521 205 const char *pv = SvPV(remote, len);
1522 205 ▶ h->remote = newSVpvn_share(pv, SvUTF8(remote) ? -(I32)len : (I32)len, 0);
Branch: 1 branch
BranchTrueFalseTotal
h->remote = newSVpvn_share(pv, SvUTF8(remote) ? -(I32)len : (I32)len, 0); 2 203 205
1523 } else {
1524 180 h->remote = SvREFCNT_inc(remote);
1525 }
1526 385 h->kind = kind;
1527 385 h->op = (U8)SvIV(AvARRAY(out)[i * 3 + 2]);
1528 }
1529 206 spec->resolved_count = n;
1530 }
1531  
1532 581 static ClassMeta* create_class_meta(pTHX_ const char *class_name, STRLEN len) {
1533 ClassMeta *meta;
1534 581 Newxz(meta, 1, ClassMeta);
1535 581 Newxz(meta->class_name, len + 1, char);
1536 581 Copy(class_name, meta->class_name, len, char);
1537 581 meta->class_name[len] = '\0';
1538 581 meta->prop_to_idx = newHV();
1539 581 meta->arg_to_idx = newHV();
1540 581 meta->idx_to_prop = NULL;
1541 581 meta->slot_count = 1; /* slot 0 reserved for prototype */
1542 581 meta->stash = gv_stashpvn(class_name, len, GV_ADD);
1543 581 return meta;
1544 }
1545  
1546 28416 static ClassMeta* get_class_meta(pTHX_ const char *class_name, STRLEN len) {
1547 SV **svp;
1548 /*
1549 # uncoverable branch true
1550 # uncoverable condition left count:1 */
1551 28416 ▶ if (!g_class_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_class_registry) return NULL; 0 28416 28416
1552 28416 svp = hv_fetch(g_class_registry, class_name, len, 0);
1553 28416 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 21814 6602 28416
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 21814 0 21814
1554 21814 return INT2PTR(ClassMeta*, SvIV(*svp));
1555 }
1556 6602 return NULL;
1557 }
1558  
1559 581 static void register_class_meta(pTHX_ const char *class_name, STRLEN len, ClassMeta *meta) {
1560 /*
1561 # uncoverable branch true
1562 # uncoverable condition left count:1 */
1563 581 ▶ if (!g_class_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_class_registry) { 0 581 581
1564 0 g_class_registry = newHV(); /* # uncoverable statement */
1565 }
1566 581 hv_store(g_class_registry, class_name, len, newSViv(PTR2IV(meta)), 0);
1567 581 }
1568  
1569 /* ============================================
1570 Prototype chain resolution
1571 ============================================ */
1572  
1573 #define MAX_PROTOTYPE_DEPTH 100
1574  
1575 /* Resolve a property through the full prototype chain.
1576 * Returns the value if found, or &PL_sv_undef if not.
1577 * Detects circular references using depth limit and pointer tracking.
1578 */
1579 45891 static SV* resolve_property_chain(pTHX_ AV *av, IV idx) {
1580 45891 int depth = 0;
1581 AV *visited[MAX_PROTOTYPE_DEPTH]; /* Simple stack-based cycle detection */
1582 int i;
1583  
1584 46058 ▶ 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 */ 46058 0 46058
while (av && depth < MAX_PROTOTYPE_DEPTH) { /* # uncoverable condition right count:1 */ 46057 1 46058
1585 /* Check for circular reference */
1586 51086 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 5031 46055 51086
1587 5031 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 2 5029 5031
1588 2 warn("Circular prototype reference detected");
1589 2 return &PL_sv_undef;
1590 }
1591 }
1592 46055 visited[depth] = av;
1593  
1594 /* Try to fetch the property at this level */
1595 46055 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 46052 3 46055
1596 46052 SV *slot = AvARRAY(av)[idx];
1597 46052 ▶ if (slot && SvOK(slot)) return slot;
Branch: 2 branches
BranchTrueFalseTotal
if (slot && SvOK(slot)) return slot; 46050 2 46052
if (slot && SvOK(slot)) return slot; 38007 8043 46050
1598 }
1599  
1600 /* Follow prototype chain (slot 0) */
1601 8048 ▶ if (AvFILLp(av) < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILLp(av) < 0) break; 1 8047 8048
1602 {
1603 8047 SV *proto_sv = AvARRAY(av)[0];
1604 /*
1605 # uncoverable branch true count:1 class:ignore_covered_err
1606 # uncoverable condition left count:1 */
1607 8047 ▶ 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; 8047 0 8047
if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break; 168 7879 8047
if (!proto_sv || !SvROK(proto_sv) || SvTYPE(SvRV(proto_sv)) != SVt_PVAV) break; 167 1 168
1608 167 av = (AV*)SvRV(proto_sv);
1609 }
1610 167 depth++;
1611 }
1612  
1613 7882 ▶ if (depth >= MAX_PROTOTYPE_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= MAX_PROTOTYPE_DEPTH) { 1 7881 7882
1614 1 warn("Prototype chain too deep (max %d levels)", MAX_PROTOTYPE_DEPTH);
1615 }
1616  
1617 7882 return &PL_sv_undef;
1618 }
1619  
1620 /* ============================================
1621 Custom OP: property accessor (get)
1622 ============================================ */
1623  
1624 10 static OP* pp_object_get(pTHX) {
1625 10 dSP;
1626 10 SV *obj = TOPs;
1627 10 IV idx = PL_op->op_targ;
1628 AV *av;
1629 SV *sv;
1630  
1631 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
1632 2 croak("Not an object");
1633 }
1634  
1635 8 av = (AV*)SvRV(obj);
1636  
1637 /* Fast path: direct slot access (common case - no prototype chain) */
1638 8 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 7 1 8
1639 7 sv = AvARRAY(av)[idx];
1640 7 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 6 1 7
if (sv && SvOK(sv)) { 2 4 6
1641 2 SETs(sv);
1642 2 RETURN;
1643 }
1644 }
1645  
1646 /* Slow path: check prototype chain only if prototype exists.
1647 Slot 0 is always allocated - use direct access instead of av_fetch(). */
1648 {
1649 6 SV *proto_sv = AvARRAY(av)[0];
1650 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
1651 2 SV *result = resolve_property_chain(aTHX_ av, idx);
1652 2 SETs(result);
1653 2 RETURN;
1654 }
1655 }
1656  
1657 4 SETs(&PL_sv_undef);
1658 4 RETURN;
1659 }
1660  
1661 /* ============================================
1662 Custom OP: property accessor (set)
1663 ============================================ */
1664  
1665 12 static OP* pp_object_set(pTHX) {
1666 12 dSP;
1667 12 SV *val = POPs;
1668 12 SV *obj = TOPs;
1669 12 IV idx = PL_op->op_targ;
1670 SV *rv;
1671 AV *av;
1672  
1673 12 rv = SvRV(obj);
1674 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
1675 2 croak("Not an object");
1676 }
1677  
1678 10 av = (AV*)rv;
1679  
1680 /* Only check magic if object has any (lazy magic - most objects don't) */
1681 10 ▶ if (SvMAGICAL(rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(rv)) { 4 6 10
1682 4 MAGIC *mg = get_object_magic(aTHX_ obj);
1683 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
1684 1 croak("Cannot modify frozen object");
1685 }
1686 }
1687  
1688 /* In-place update if slot already has an SV (avoids alloc/dealloc) */
1689 9 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 8 1 9
1690 8 SV *slot = AvARRAY(av)[idx];
1691 8 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 7 1 8
1692 7 sv_setsv(slot, val);
1693 7 SETs(val);
1694 7 RETURN;
1695 }
1696 }
1697  
1698 2 av_store(av, idx, newSVsv(val));
1699 2 SETs(val);
1700 2 RETURN;
1701 }
1702  
1703 /* ============================================
1704 Custom OP: property accessor (set) with type check
1705 Uses op_private to store type ID for inline check
1706 ============================================ */
1707  
1708 /* Helper struct to pass both idx and meta through op */
1709 typedef struct {
1710 IV slot_idx;
1711 ClassMeta *meta;
1712 } SlotOpData;
1713  
1714 /* Helper struct for function-style accessors (cross-class support) */
1715 struct FuncAccessorData_s {
1716 IV slot_idx;
1717 ClassMeta *expected_class; /* Class this accessor expects */
1718 SlotSpec *spec; /* Slot spec (for lazy support), may be NULL */
1719 IV registry_id; /* ID in g_func_accessor_registry */
1720 ClassMeta *meta; /* Class imported from: its current spec decides :private */
1721 };
1722  
1723 /* Register a FuncAccessorData and return its ID */
1724 274 static IV register_func_accessor_data(pTHX_ FuncAccessorData *data) {
1725 274 ▶ if (g_func_accessor_count >= g_func_accessor_capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_func_accessor_count >= g_func_accessor_capacity) { 17 257 274
1726 17 ▶ 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 16 17
1727 17 ▶ Renew(g_func_accessor_registry, new_capacity, FuncAccessorData*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_func_accessor_registry, new_capacity, FuncAccessorData*); 0 17 17
1728 17 g_func_accessor_capacity = new_capacity;
1729 }
1730 274 data->registry_id = g_func_accessor_count;
1731 274 g_func_accessor_registry[g_func_accessor_count] = data;
1732 274 return g_func_accessor_count++;
1733 }
1734  
1735 /* Look up FuncAccessorData by ID — inlined for hot path performance */
1736 84755 OBJECT_INLINE FuncAccessorData* get_func_accessor_data(IV id) {
1737 84755 return g_func_accessor_registry[id];
1738 }
1739  
1740 32 static OP* pp_object_set_typed(pTHX) {
1741 32 dSP;
1742 32 SV *val = POPs;
1743 32 SV *obj = TOPs;
1744 32 SlotOpData *data = INT2PTR(SlotOpData*, PL_op->op_targ);
1745 32 IV idx = data->slot_idx;
1746 32 ClassMeta *meta = data->meta;
1747 32 SlotSpec *spec = meta->slots[idx];
1748 AV *av;
1749  
1750 32 ▶ if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 31 1 32
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 1 30 31
1751 2 croak("Not an object");
1752 }
1753  
1754 30 av = (AV*)SvRV(obj);
1755  
1756 /* Check frozen/locked — only walk magic list if object has magic */
1757 30 ▶ if (SvMAGICAL(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(av)) { 4 26 30
1758 4 MAGIC *mg = get_object_magic(aTHX_ obj);
1759 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
1760 1 croak("Cannot modify frozen object");
1761 }
1762 }
1763  
1764 /* Fast-skip readonly/required/coerce when none apply */
1765 29 ▶ if (spec->has_checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_checks) { 9 20 29
1766 9 ▶ if (spec->is_readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_readonly) { 3 6 9
1767 3 croak("Cannot modify readonly slot '%s'", spec->name);
1768 }
1769 6 ▶ if (spec->is_required && !SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_required && !SvOK(val)) { 2 4 6
if (spec->is_required && !SvOK(val)) { 1 1 2
1770 1 croak("Cannot set required slot '%s' to undef", spec->name);
1771 }
1772 5 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) { 3 2 5
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) { 2 1 3
1773 4 val = apply_slot_coercion(aTHX_ val, spec);
1774 }
1775 }
1776  
1777 /* Type check using helper (handles both C and Perl callbacks) */
1778 25 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 21 4 25
1779 21 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 8 13 21
1780 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
1781 1 ? spec->registered->name
1782 9 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 7 8
1783 8 croak("Type constraint failed for '%s': expected %s",
1784 spec->name, type_name);
1785 }
1786 }
1787  
1788 /* Trigger callback ($self, $new_value) */
1789 17 ▶ if (spec->has_trigger && spec->trigger_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_trigger && spec->trigger_cb) { 3 14 17
if (spec->has_trigger && spec->trigger_cb) { 2 1 3
1790 2 dSP;
1791 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1792 2 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 2 2
1793 2 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 2 2
1794 2 PUTBACK;
1795 4 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
1796 }
1797  
1798 17 ▶ if (!spec->is_weak) {
Branch: 1 branch
BranchTrueFalseTotal
if (!spec->is_weak) { 13 4 17
1799 /* In-place update avoids newSVsv allocation (common case) */
1800 13 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 12 1 13
1801 12 SV *slot = AvARRAY(av)[idx];
1802 12 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 11 1 12
1803 11 sv_setsv(slot, val);
1804 11 SETs(val);
1805 11 RETURN;
1806 }
1807 }
1808 2 av_store(av, idx, newSVsv(val));
1809 } else {
1810 4 SV *stored = newSVsv(val);
1811 4 av_store(av, idx, stored);
1812 4 ▶ if (SvROK(stored)) sv_rvweaken(stored);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(stored)) sv_rvweaken(stored); 1 3 4
1813 }
1814 6 SETs(val);
1815 6 RETURN;
1816 }
1817  
1818 /* ============================================
1819 Call checker for accessor
1820 ============================================ */
1821  
1822 5 static OP* accessor_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
1823 5 IV idx = SvIV(ckobj);
1824 OP *pushop, *cvop, *selfop, *argop;
1825 OP *newop;
1826  
1827 PERL_UNUSED_ARG(namegv);
1828  
1829 5 pushop = cUNOPx(entersubop)->op_first;
1830 /*
1831 # uncoverable branch false
1832 # uncoverable condition right count:1 */
1833 5 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 5 0 5
1834 5 pushop = cUNOPx(pushop)->op_first;
1835 }
1836  
1837 5 ▶ selfop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
selfop = OpSIBLING(pushop); 5 0 5
1838 5 cvop = selfop;
1839 5 argop = selfop;
1840 12 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 7 5 12
1841 7 argop = cvop;
1842 7 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 7 0 7
1843 }
1844  
1845 /* Check if there's an argument after self (setter call) */
1846 5 ▶ if (argop != selfop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != selfop) { 2 3 5
1847 /* Setter: $obj->name($value) */
1848 2 ▶ OP *valop = OpSIBLING(selfop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(selfop); 2 0 2
1849
1850 /* Detach self and val */
1851 2 OpMORESIB_set(pushop, cvop);
1852 2 OpLASTSIB_set(valop, NULL);
1853 2 OpLASTSIB_set(selfop, NULL);
1854
1855 /* Create binop with self and val - use OP_NULL to avoid assertion
1856 * failures on debugging Perls (5.14-5.20), then set OP_CUSTOM */
1857 2 newop = newBINOP(OP_NULL, 0, selfop, valop);
1858 2 newop->op_type = OP_CUSTOM;
1859 2 newop->op_ppaddr = pp_object_set;
1860 2 newop->op_targ = idx;
1861
1862 2 op_free(entersubop);
1863 2 return newop;
1864 } else {
1865 /* Getter: $obj->name */
1866 3 OpMORESIB_set(pushop, cvop);
1867 3 OpLASTSIB_set(selfop, NULL);
1868
1869 /* Use OP_NULL to avoid assertion failures on debugging Perls
1870 * (5.14-5.20), then set OP_CUSTOM */
1871 3 newop = newUNOP(OP_NULL, 0, selfop);
1872 3 newop->op_type = OP_CUSTOM;
1873 3 newop->op_ppaddr = pp_object_get;
1874 3 newop->op_targ = idx;
1875
1876 3 op_free(entersubop);
1877 3 return newop;
1878 }
1879 }
1880  
1881 /* ============================================
1882 XS Fallback functions
1883 ============================================ */
1884  
1885 /* XS fallback for new (when call checker can't optimize) */
1886 5660 XS_INTERNAL(xs_object_new_fallback) {
1887 5660 dXSARGS;
1888 5660 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
1889 AV *obj_av;
1890 SV *obj_sv;
1891 SV **ary;
1892 IV i;
1893 5660 IV start_arg = 0;
1894 IV arg_count;
1895 5660 IV slot_count = meta->slot_count;
1896 5660 int is_named = 0;
1897  
1898 /* Skip class name if passed as invocant (Cat->new or new Cat).
1899 * Fast path: compare stash pointer instead of string compare. */
1900 5660 ▶ 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 */ 5659 1 5660
if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */ 5657 2 5659
if (items > 0 && SvPOK(ST(0)) && !SvROK(ST(0))) { /* # uncoverable condition right count:3 */ 5657 0 5657
1901 /* The class name is always the first arg for Cat->new() or new Cat() style */
1902 5657 start_arg = 1;
1903 }
1904  
1905 5660 arg_count = items - start_arg;
1906  
1907 /* Detect named pairs: even count and first arg is a known property/init_arg name,
1908 OR the class has defaults (unknown keys are dropped, defaults applied). */
1909 5660 ▶ 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))) { 4376 1284 5660
if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) { 4174 202 4376
if (arg_count > 0 && (arg_count % 2) == 0 && SvPOK(ST(start_arg))) { 4114 60 4174
1910 STRLEN len;
1911 4114 const char *pv = SvPV(ST(start_arg), len);
1912 4114 ▶ 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) 79 4035 4114
if (hv_exists(meta->prop_to_idx, pv, len) || hv_exists(meta->arg_to_idx, pv, len) 68 11 79
1913 68 ▶ || meta->has_any_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
|| meta->has_any_defaults) { 2 66 68
1914 4048 is_named = 1;
1915 }
1916 }
1917  
1918 /* Create array with pre-extended size and get direct pointer */
1919 5660 obj_av = newAV();
1920 5660 av_extend(obj_av, slot_count - 1);
1921 /* Fill array length so AvARRAY access is safe */
1922 5660 AvFILLp(obj_av) = slot_count - 1;
1923 5660 ary = AvARRAY(obj_av);
1924  
1925 /* Slot 0 = prototype (initially undef - read-only is fine, never written via setter) */
1926 5660 ary[0] = &PL_sv_undef;
1927  
1928 /* Fill slots with defaults or writable undef in a single pass */
1929 18102 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 12442 5660 18102
1930 12442 SlotSpec *spec = meta->slots[i];
1931 12442 ▶ 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 */ 12442 0 12442
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 2618 9824 12442
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 2602 16 2618
1932 2602 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 213 2389 2602
1933 213 ▶ if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVAV) { 130 83 213
1934 130 ary[i] = newRV_noinc((SV*)newAV());
1935 /*
1936 # uncoverable branch false
1937 # uncoverable condition right count:1 */
1938 83 ▶ } else if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(spec->default_sv)) == SVt_PVHV) { 83 0 83
1939 83 ary[i] = newRV_noinc((SV*)newHV());
1940 } else {
1941 0 ary[i] = newSVsv(spec->default_sv); /* # uncoverable statement */
1942 }
1943 } else {
1944 2389 ary[i] = newSVsv(spec->default_sv);
1945 }
1946 } else {
1947 9840 ary[i] = newSV(0);
1948 }
1949 }
1950  
1951 /* Create blessed reference NOW so triggers can do method dispatch */
1952 5660 obj_sv = newRV_noinc((SV*)obj_av);
1953 5660 sv_bless(obj_sv, meta->stash);
1954  
1955 5660 ▶ if (is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_named) { 4048 1612 5660
1956 /* Named arguments */
1957 9104 ▶ for (i = start_arg; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start_arg; i < items; i += 2) { 5084 4020 9104
1958 5084 SV *key_sv = ST(i);
1959 5084 ▶ 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 */ 5084 0 5084
1960 STRLEN key_len;
1961 5084 const char *key = SvPV(key_sv, key_len);
1962 5084 SV **idx_svp = hv_fetch(meta->arg_to_idx, key, key_len, 0);
1963 5084 ▶ if (idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx_svp) { 5079 5 5084
1964 5079 IV idx = SvIVX(*idx_svp);
1965 5079 SlotSpec *spec = meta->slots[idx];
1966
1967 /* Coerce + type check */
1968 5079 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 5079 0 5079
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 4964 115 5079
1969 4964 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 4958 6 4964
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 22 4936 4958
1970 28 val_sv = apply_slot_coercion(aTHX_ val_sv, spec);
1971 4964 ▶ if (!check_slot_type(aTHX_ val_sv, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val_sv, spec)) { 28 4936 4964
1972 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
1973 7 ? spec->registered->name
1974 35 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 7 21 28
1975 28 croak("Type constraint failed for '%s' in new(): expected %s",
1976 spec->name, type_name);
1977 }
1978 }
1979
1980 /* Trigger callback */
1981 5051 ▶ 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 */ 5051 0 5051
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 125 4926 5051
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 124 1 125
1982 124 dSP;
1983 124 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 124 124
1984 124 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 124 124
1985 124 ▶ XPUSHs(val_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val_sv); 0 124 124
1986 124 PUTBACK;
1987 248 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
1988 }
1989
1990 5051 sv_setsv(ary[idx], val_sv);
1991 }
1992 }
1993 } else {
1994 /* Positional arguments */
1995 1612 IV provided = items - start_arg;
1996 1612 ▶ if (provided > slot_count - 1) provided = slot_count - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (provided > slot_count - 1) provided = slot_count - 1; 1 1611 1612
1997  
1998 2443 ▶ for (i = 0; i < provided; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < provided; i++) { 837 1606 2443
1999 837 IV idx = i + 1;
2000 837 SV *val_sv = ST(start_arg + i);
2001 837 SlotSpec *spec = meta->slots[idx];
2002
2003 /* Coerce + type check */
2004 837 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 837 0 837
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 340 497 837
2005 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
2006 11 val_sv = apply_slot_coercion(aTHX_ val_sv, spec);
2007 340 ▶ if (!check_slot_type(aTHX_ val_sv, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val_sv, spec)) { 6 334 340
2008 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
2009 2 ? spec->registered->name
2010 8 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 2 4 6
2011 6 croak("Type constraint failed for '%s' in new(): expected %s",
2012 spec->name, type_name);
2013 }
2014 }
2015
2016 /* Trigger callback */
2017 831 ▶ 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 */ 831 0 831
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 4 827 831
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 3 1 4
2018 3 dSP;
2019 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
2020 3 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 3 3
2021 3 ▶ XPUSHs(val_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val_sv); 0 3 3
2022 3 PUTBACK;
2023 6 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
2024 }
2025
2026 831 sv_setsv(ary[idx], val_sv);
2027 }
2028 }
2029  
2030 /* Call builders for non-lazy builder slots that weren't set */
2031 5626 ▶ if (meta->has_any_builders) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_builders) { 3379 2247 5626
2032 10839 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 7462 3377 10839
2033 7462 SlotSpec *spec = meta->slots[i];
2034 7462 ▶ 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 */ 7462 0 7462
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 3405 4057 7462
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 19 3386 3405
if (spec && spec->has_builder && !spec->is_lazy && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 18 1 19
2035 /* Call builder method */
2036 18 dSP;
2037 IV count;
2038 18 ENTER;
2039 18 SAVETMPS;
2040 18 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 18 18
2041 18 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 18 18
2042 18 PUTBACK;
2043 36 count = call_method(SvPV_nolen(spec->builder_name), G_SCALAR);
2044 18 SPAGAIN;
2045 /*
2046 # uncoverable branch false
2047 # uncoverable condition right count:1 */
2048 18 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 18 0 18
2049 18 SV *built_val = POPs;
2050
2051 /* Coerce + type check the built value */
2052 18 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 16 2 18
2053 16 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 14 2 16
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 2 12 14
2054 4 built_val = apply_slot_coercion(aTHX_ built_val, spec);
2055 16 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 2 14 16
2056 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
2057 1 ? spec->registered->name
2058 3 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 1 2
2059 2 croak("Type constraint failed for '%s' in builder: expected %s",
2060 spec->name, type_name);
2061 }
2062 }
2063
2064 16 sv_setsv(ary[i], built_val);
2065 }
2066 16 PUTBACK;
2067 16 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 16 0 16
2068 16 LEAVE;
2069 }
2070 }
2071 }
2072  
2073 /* Check required slots */
2074 5624 ▶ if (meta->has_any_required) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_required) { 2306 3318 5624
2075 7261 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 4966 2295 7261
2076 4966 SlotSpec *spec = meta->slots[i];
2077 4966 ▶ 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 */ 4966 0 4966
if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 2317 2649 4966
if (spec && spec->is_required && !SvOK(ary[i])) { /* # uncoverable condition right count:1 */ 11 2306 2317
2078 11 croak("Required slot '%s' not provided in new()", spec->name);
2079 }
2080 }
2081 }
2082  
2083 /* Weaken references if any slots have is_weak */
2084 5613 ▶ if (meta->has_any_weak) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_any_weak) { 137 5476 5613
2085 1115 ▶ for (i = 1; i < slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < slot_count; i++) { 978 137 1115
2086 978 SlotSpec *spec = meta->slots[i];
2087 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
2088 3 sv_rvweaken(ary[i]);
2089 }
2090 }
2091 }
2092  
2093 /* Call BUILD if defined. Use tri-state: 0=unchecked, 1=has BUILD, 2=no BUILD.
2094 Avoids gv_fetchmeth on every construction once checked. */
2095 5613 ▶ if (meta->has_build == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_build == 0) { 371 5242 5613
2096 371 GV *gv = gv_fetchmeth(meta->stash, "BUILD", 5, 0);
2097 371 ▶ if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 12 359 371
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 12 0 12
2098 12 meta->has_build = 1;
2099 12 meta->build_cv = GvCV(gv);
2100 } else {
2101 359 meta->has_build = 2;
2102 }
2103 }
2104 5613 ▶ if (meta->has_build == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->has_build == 1) { 25 5588 5613
2105 25 dSP;
2106 25 ENTER;
2107 25 SAVETMPS;
2108 25 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 25 25
2109 25 ▶ XPUSHs(obj_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj_sv); 0 25 25
2110 25 PUTBACK;
2111 25 call_method("BUILD", G_VOID | G_DISCARD);
2112 25 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 25 0 25
2113 25 LEAVE;
2114 }
2115  
2116 5613 ST(0) = sv_2mortal(obj_sv);
2117 5613 XSRETURN(1);
2118 }
2119  
2120 /* XS fallback accessor */
2121 269 XS_INTERNAL(xs_accessor_fallback) {
2122 269 dXSARGS;
2123 269 IV idx = CvXSUBANY(cv).any_iv;
2124 269 SV *self = ST(0);
2125 AV *av;
2126 SV *rv;
2127  
2128 269 rv = SvRV(self);
2129 269 ▶ if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) { 268 1 269
if (!SvROK(self) || SvTYPE(rv) != SVt_PVAV) { 1 267 268
2130 2 croak("Not an object");
2131 }
2132 267 av = (AV*)rv;
2133  
2134 267 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 44 223 267
2135 /* Setter */
2136 44 ▶ if (SvMAGICAL(rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(rv)) { 7 37 44
2137 7 MAGIC *mg = get_object_magic(aTHX_ self);
2138 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
2139 3 croak("Cannot modify frozen object");
2140 }
2141 }
2142 /* In-place update if slot already has an SV */
2143 41 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 40 1 41
2144 40 SV *slot = AvARRAY(av)[idx];
2145 40 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 38 2 40
2146 38 sv_setsv(slot, ST(1));
2147 38 ST(0) = ST(1);
2148 38 XSRETURN(1);
2149 }
2150 }
2151 3 av_store(av, idx, newSVsv(ST(1)));
2152 3 ST(0) = ST(1);
2153 3 XSRETURN(1);
2154 } else {
2155 /* Getter - fast path: direct slot access */
2156 223 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 221 2 223
2157 221 SV *sv = AvARRAY(av)[idx];
2158 221 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 220 1 221
if (sv && SvOK(sv)) { 189 31 220
2159 189 ST(0) = sv;
2160 189 XSRETURN(1);
2161 }
2162 }
2163 /* Slow path: check prototype chain */
2164 {
2165 34 SV **proto = av_fetch(av, 0, 0);
2166 34 ▶ if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 33 1 34
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 21 12 33
if (proto && SvROK(*proto) && SvTYPE(SvRV(*proto)) == SVt_PVAV) { 20 1 21
2167 20 SV *result = resolve_property_chain(aTHX_ av, idx);
2168 20 ST(0) = result;
2169 20 XSRETURN(1);
2170 }
2171 }
2172 14 ST(0) = &PL_sv_undef;
2173 14 XSRETURN(1);
2174 }
2175 }
2176  
2177 /* ============================================
2178 Install constructor into class
2179 ============================================ */
2180  
2181 501 static void install_constructor(pTHX_ const char *class_name, ClassMeta *meta) {
2182 char full_name[256];
2183 CV *cv;
2184  
2185 501 snprintf(full_name, sizeof(full_name), "%s::new", class_name);
2186
2187 /* Create a minimal CV that will be replaced by call checker */
2188 501 cv = newXS(full_name, xs_object_new_fallback, __FILE__);
2189 501 CvXSUBANY(cv).any_iv = PTR2IV(meta);
2190 501 }
2191  
2192 /* ============================================
2193 Custom OP: fast function-style getter
2194 op_targ = registry ID, reads object from stack
2195 ============================================ */
2196 static XOP object_func_get_xop;
2197 static XOP object_func_set_xop;
2198  
2199 80336 static OP* pp_object_func_get(pTHX) {
2200 80336 dSP;
2201 80336 SV *obj = TOPs; /* peek, don't pop */
2202 80336 FuncAccessorData *data = get_func_accessor_data(PL_op->op_targ);
2203 80336 IV idx = data->slot_idx;
2204 SV *rv;
2205 AV *av;
2206 SV *sv;
2207  
2208 80336 ▶ if (!SvROK(obj)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) croak("Not an object"); 1 80335 80336
2209 80335 rv = SvRV(obj);
2210 80335 ▶ if (SvTYPE(rv) != SVt_PVAV) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) != SVt_PVAV) croak("Not an object"); 2 80333 80335
2211 80333 av = (AV*)rv;
2212  
2213 /* Validate object is of expected class (stash pointer comparison) */
2214 /*
2215 # uncoverable branch true
2216 # uncoverable condition left count:1
2217 # uncoverable condition left count:2
2218 # uncoverable condition right count:2 */
2219 80333 ▶ 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 80333 80333
if (data->expected_class && SvSTASH(rv) != data->expected_class->stash) { 0 0 0
2220 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
2221 data->expected_class->class_name, /* # uncoverable statement */
2222 HvNAME(SvSTASH(rv))); /* # uncoverable statement */
2223 }
2224  
2225 /* Direct array access — no SvOK needed (func path has no prototype chain) */
2226 80333 sv = NULL;
2227 80333 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 80332 1 80333
2228 80332 sv = AvARRAY(av)[idx];
2229 80332 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 80331 1 80332
if (sv && SvOK(sv)) { 80322 9 80331
2230 80322 SETs(sv);
2231 80322 RETURN;
2232 }
2233 }
2234  
2235 /* Lazy initialization for func accessors */
2236 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
2237 7 SlotSpec *spec = data->spec;
2238 7 SV *built_val = NULL;
2239 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
2240 2 const char *builder = SvPV_nolen(spec->builder_name);
2241 int count;
2242 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
2243 2 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 2 2
2244 2 PUTBACK;
2245 2 count = call_method(builder, G_SCALAR);
2246 2 SPAGAIN;
2247 /*
2248 # uncoverable branch false
2249 # uncoverable condition right count:1 */
2250 2 ▶ if (count > 0) built_val = newSVsv(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) built_val = newSVsv(POPs); 2 0 2
2251 0 else built_val = newSV(0); /* # uncoverable statement */
2252 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
2253 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
2254 3 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 1 3
2255 2 SV *inner = SvRV(spec->default_sv);
2256 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
2257 /*
2258 # uncoverable branch false
2259 # uncoverable condition right count:1 */
2260 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
2261 0 else built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
2262 } else {
2263 1 built_val = newSVsv(spec->default_sv);
2264 }
2265 }
2266 7 ▶ if (built_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) { 5 2 7
2267 5 av_store(av, idx, built_val);
2268 5 SETs(built_val);
2269 5 RETURN;
2270 }
2271 }
2272  
2273 6 SETs(&PL_sv_undef);
2274 6 RETURN;
2275 }
2276  
2277 4062 static OP* pp_object_func_set(pTHX) {
2278 4062 dSP;
2279 4062 SV *val = POPs; /* Pop value first */
2280 4062 SV *obj = TOPs; /* Object left on stack */
2281 4062 FuncAccessorData *data = get_func_accessor_data(PL_op->op_targ);
2282 4062 IV idx = data->slot_idx;
2283 SV *rv;
2284 AV *av;
2285  
2286 4062 ▶ if (!SvROK(obj)) croak("Not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) croak("Not an object"); 1 4061 4062
2287 4061 rv = SvRV(obj);
2288 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
2289 4060 av = (AV*)rv;
2290  
2291 /* Validate object is of expected class (stash pointer comparison) */
2292 /*
2293 # uncoverable branch true
2294 # uncoverable condition left count:1
2295 # uncoverable condition left count:2
2296 # uncoverable condition right count:2 */
2297 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
2298 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
2299 data->expected_class->class_name, /* # uncoverable statement */
2300 HvNAME(SvSTASH(rv))); /* # uncoverable statement */
2301 }
2302  
2303 /* In-place update if slot already has an SV (avoids alloc/dealloc) */
2304 4060 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 4059 1 4060
2305 4059 SV *slot = AvARRAY(av)[idx];
2306 4059 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 4058 1 4059
2307 4058 sv_setsv(slot, val);
2308 4058 SETs(val);
2309 4058 RETURN;
2310 }
2311 }
2312  
2313 2 av_store(av, idx, newSVsv(val));
2314 2 SETs(val); /* Replace object with value */
2315 2 RETURN;
2316 }
2317  
2318 /* Custom ops from other modules that are safe to use as an accessor operand:
2319 * side-effect free, and leaving exactly one value on the stack. Matched by the
2320 * name the op was registered under, so neither dist needs to know how to build
2321 * against the other. Seeded with Confold's `<:`, whose whole purpose is to mark
2322 * a value as constant. */
2323 #define OBJECT_MAX_SIMPLE_CUSTOM 8
2324 static const char *g_simple_custom_ops[OBJECT_MAX_SIMPLE_CUSTOM] = { "confold" };
2325 static int g_simple_custom_op_count = 1;
2326  
2327 1 OBJECT_INLINE bool is_simple_custom_op(pTHX_ OP *op) {
2328 const char *name;
2329 int i;
2330  
2331 1 name = XopENTRYCUSTOM(op, xop_name);
2332 /*
2333 # uncoverable branch true
2334 # uncoverable condition left count:1 */
2335 1 ▶ if (!name) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return false; 0 1 1
2336  
2337 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
2338 /*
2339 # uncoverable branch true class:ignore_covered_err
2340 # uncoverable condition left count:1 class:ignore_covered_err */
2341 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
2342 }
2343 1 return false;
2344 }
2345  
2346 /* Check if an op is "simple" (can be safely used in optimized accessor) */
2347 417 OBJECT_INLINE bool is_simple_op(pTHX_ OP *op) {
2348 /*
2349 # uncoverable branch true
2350 # uncoverable condition left count:1 */
2351 417 ▶ if (!op) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) return false; 0 417 417
2352 /* Simple ops: pad variables, constants, global variables */
2353 417 switch (op->op_type) {
2354 1 case OP_CUSTOM:
2355 1 return is_simple_custom_op(aTHX_ op);
2356 295 case OP_PADSV: /* $lexical */
2357 case OP_CONST: /* literal value */
2358 case OP_GV: /* *glob */
2359 case OP_GVSV: /* $global */
2360 case OP_AELEMFAST:/* $array[const] */
2361 #if defined(OP_AELEMFAST_LEX) && OP_AELEMFAST_LEX != OP_AELEMFAST
2362 case OP_AELEMFAST_LEX:
2363 #endif
2364 case OP_NULL: /* Often wraps simple ops */
2365 295 return true;
2366 121 default:
2367 121 return false;
2368 }
2369 }
2370  
2371 /* Call checker for function-style accessor: name($obj) or name($obj, $val) */
2372 357 static OP* func_accessor_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2373 357 IV registry_id = SvIV(ckobj);
2374 357 FuncAccessorData *data = get_func_accessor_data(registry_id);
2375 OP *pushop, *cvop, *objop, *argop;
2376 OP *newop;
2377  
2378 PERL_UNUSED_ARG(namegv);
2379  
2380 /*
2381 # uncoverable branch true
2382 # uncoverable condition left count:1 */
2383 357 ▶ if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data) { 0 357 357
2384 0 return entersubop; /* Fallback if data not found */ /* # uncoverable statement */
2385 }
2386  
2387 /* A private attribute keeps the XSUB, which checks the caller, rather
2388 * than becoming a custom op that would not. The spec is looked up in the
2389 * class, which a redefinition keeps current. */
2390 357 ▶ if (data->meta->slots[data->slot_idx]->is_private) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (data->meta->slots[data->slot_idx]->is_private) return entersubop; 8 349 357
2391  
2392 349 pushop = cUNOPx(entersubop)->op_first;
2393 /*
2394 # uncoverable branch false
2395 # uncoverable condition right count:1 */
2396 349 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 349 0 349
2397 349 pushop = cUNOPx(pushop)->op_first;
2398 }
2399  
2400 /* Walk the op tree like the method-style accessor checker */
2401 349 ▶ objop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
objop = OpSIBLING(pushop); 349 0 349
2402 349 cvop = objop;
2403 349 argop = objop;
2404 782 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 433 349 782
2405 433 argop = cvop;
2406 433 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 433 0 433
2407 }
2408  
2409 /* Check if there's an argument after obj (setter call) */
2410 349 ▶ if (argop != objop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != objop) { 83 266 349
2411 /* Setter: name($obj, $val) - optimize to custom binop */
2412 83 ▶ OP *valop = OpSIBLING(objop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(objop); 83 0 83
2413  
2414 /* Only optimize if exactly 2 args and both are simple ops */
2415 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
2416 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
2417 39 OpMORESIB_set(pushop, cvop);
2418 39 OpLASTSIB_set(valop, NULL);
2419 39 OpLASTSIB_set(objop, NULL);
2420  
2421 /* Use OP_NULL first, then set op_type to OP_CUSTOM.
2422 * This avoids assertion failures in newBINOP on older
2423 * debugging Perls (5.14-5.20) which check op class masks. */
2424 39 newop = newBINOP(OP_NULL, 0, objop, valop);
2425 39 newop->op_type = OP_CUSTOM;
2426 39 newop->op_ppaddr = pp_object_func_set;
2427 39 newop->op_targ = data->registry_id;
2428  
2429 39 op_free(entersubop);
2430 39 return newop;
2431 }
2432  
2433 /* Complex args - fall back to XS */
2434 44 return op_contextualize(entersubop, G_SCALAR);
2435 }
2436  
2437 /* Getter: name($obj) - optimize only if objop is simple */
2438 266 ▶ if (!is_simple_op(aTHX_ objop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_simple_op(aTHX_ objop)) { 79 187 266
2439 79 return entersubop;
2440 }
2441  
2442 187 OpMORESIB_set(pushop, cvop);
2443 187 OpLASTSIB_set(objop, NULL);
2444  
2445 /* Use OP_NULL first, then set op_type to OP_CUSTOM.
2446 * This avoids assertion failures in newUNOP on older
2447 * debugging Perls (5.14-5.20) which check op class masks. */
2448 187 newop = newUNOP(OP_NULL, 0, objop);
2449 187 newop->op_type = OP_CUSTOM;
2450 187 newop->op_ppaddr = pp_object_func_get;
2451 187 newop->op_targ = data->registry_id;
2452  
2453 187 op_free(entersubop);
2454 187 return newop;
2455 }
2456  
2457 /* XS fallback for function-style accessor */
2458 20384 XS_INTERNAL(xs_func_accessor_fallback) {
2459 20384 dXSARGS;
2460 20384 FuncAccessorData *data = INT2PTR(FuncAccessorData*, CvXSUBANY(cv).any_iv);
2461 20384 IV idx = data->slot_idx;
2462 20384 SV *obj = ST(0);
2463 AV *av;
2464  
2465 20384 check_private(aTHX_ data->meta->slots[idx], data->meta);
2466  
2467 20378 ▶ if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 20375 3 20378
if (!SvROK(obj) || SvTYPE(SvRV(obj)) != SVt_PVAV) { 1 20374 20375
2468 4 croak("Not an object");
2469 }
2470 20374 av = (AV*)SvRV(obj);
2471  
2472 /* Validate object is of expected class */
2473 /*
2474 # uncoverable branch true
2475 # uncoverable condition left count:1
2476 # uncoverable condition left count:2
2477 # uncoverable condition right count:2 */
2478 20374 ▶ 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 20374 20374
if (data->expected_class && SvSTASH(SvRV(obj)) != data->expected_class->stash) { 0 0 0
2479 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
2480 data->expected_class->class_name, /* # uncoverable statement */
2481 HvNAME(SvSTASH(SvRV(obj)))); /* # uncoverable statement */
2482 }
2483  
2484 20374 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 4147 16227 20374
2485 /* In-place update if slot already has an SV */
2486 4147 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 4146 1 4147
2487 4146 SV *slot = AvARRAY(av)[idx];
2488 4146 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 4145 1 4146
2489 4145 sv_setsv(slot, ST(1));
2490 4145 ST(0) = ST(1);
2491 4145 XSRETURN(1);
2492 }
2493 }
2494 2 av_store(av, idx, newSVsv(ST(1)));
2495 2 ST(0) = ST(1);
2496 } else {
2497 /* Direct array access */
2498 16227 SV *sv = NULL;
2499 16227 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 16226 1 16227
2500 16226 sv = AvARRAY(av)[idx];
2501 }
2502 16234 ▶ 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 */ 16224 3 16227
if ((!sv || !SvOK(sv)) && data->spec && data->spec->is_lazy) { /* # uncoverable condition right count:3 */ 7 16217 16224
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
2503 /* Lazy initialization */
2504 7 SlotSpec *spec = data->spec;
2505 7 SV *built_val = NULL;
2506 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
2507 1 dSP;
2508 1 const char *builder = SvPV_nolen(spec->builder_name);
2509 int count;
2510 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
2511 1 ▶ XPUSHs(ST(0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(0)); 0 1 1
2512 1 PUTBACK;
2513 1 count = call_method(builder, G_SCALAR);
2514 1 SPAGAIN;
2515 /*
2516 # uncoverable branch false
2517 # uncoverable condition right count:1 */
2518 1 ▶ if (count > 0) built_val = newSVsv(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) built_val = newSVsv(POPs); 1 0 1
2519 0 else built_val = newSV(0); /* # uncoverable statement */
2520 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
2521 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
2522 3 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 1 3
2523 2 SV *inner = SvRV(spec->default_sv);
2524 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
2525 /*
2526 # uncoverable branch false
2527 # uncoverable condition right count:1 */
2528 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
2529 0 else built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
2530 } else {
2531 1 built_val = newSVsv(spec->default_sv);
2532 }
2533 }
2534 7 ▶ if (built_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) { 4 3 7
2535 4 av_store(av, idx, built_val);
2536 4 ST(0) = built_val;
2537 } else {
2538 3 ST(0) = &PL_sv_undef;
2539 }
2540 } else {
2541 16220 ▶ ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef; 16218 2 16220
ST(0) = (sv && SvOK(sv)) ? sv : &PL_sv_undef; 16217 1 16218
2542 }
2543 }
2544 16229 XSRETURN(1);
2545 }
2546  
2547 /* Install function-style accessor in caller's namespace */
2548 274 static void install_func_accessor(pTHX_ const char *pkg, const char *prop_name, IV idx, ClassMeta *expected_class, int force, SlotSpec *spec, ClassMeta *meta) {
2549 char full_name[256];
2550 CV *cv;
2551 SV *ckobj;
2552 FuncAccessorData *data;
2553 IV registry_id;
2554  
2555 274 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, prop_name);
2556  
2557 /* Check if this accessor already exists */
2558 274 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2559 274 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 44 230 274
2560 /*
2561 # uncoverable branch true
2562 # uncoverable condition left count:1 */
2563 44 ▶ if (!force) {
Branch: 1 branch
BranchTrueFalseTotal
if (!force) { 0 44 44
2564 0 return; /* Not forced: skip to preserve user-defined subs */ /* # uncoverable statement */
2565 }
2566 /* Forced (import_accessors/import_accessor): delete existing
2567 * to avoid "Subroutine redefined" warning from newXS. */
2568 {
2569 44 HV *stash = gv_stashpvn(pkg, strlen(pkg), 0);
2570 /*
2571 # uncoverable branch false
2572 # uncoverable condition right count:1 */
2573 44 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 44 0 44
2574 44 (void)hv_delete(stash, prop_name, strlen(prop_name), G_DISCARD);
2575 }
2576 }
2577 }
2578  
2579 /* Allocate data for this accessor and register it */
2580 274 Newx(data, 1, FuncAccessorData);
2581 274 data->slot_idx = idx;
2582 274 data->expected_class = expected_class; /* NULL for same-class, set for cross-class */
2583 274 data->spec = spec; /* NULL if no spec, used for lazy support */
2584 274 data->meta = meta;
2585 274 registry_id = register_func_accessor_data(aTHX_ data);
2586  
2587 274 cv = newXS(full_name, xs_func_accessor_fallback, __FILE__);
2588 274 CvXSUBANY(cv).any_iv = PTR2IV(data); /* XS fallback still uses pointer directly */
2589  
2590 274 ckobj = newSViv(registry_id);
2591 274 cv_set_call_checker(cv, func_accessor_call_checker, ckobj);
2592 }
2593  
2594 /* Object::Proto::import_accessors("Class", "targetpkg") - import fast accessors */
2595 43 XS_INTERNAL(xs_import_accessors) {
2596 43 dXSARGS;
2597 STRLEN class_len, pkg_len, prefix_len;
2598 const char *class_pv, *pkg_pv, *prefix_pv;
2599 ClassMeta *meta;
2600 IV i;
2601  
2602 43 ▶ 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 42 43
2603  
2604 42 class_pv = SvPV(ST(0), class_len);
2605  
2606 /* Optional prefix (alias prepended to accessor names) */
2607 42 prefix_pv = NULL;
2608 42 prefix_len = 0;
2609 42 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 25 17 42
if (items > 1 && SvOK(ST(1))) { 10 15 25
2610 10 prefix_pv = SvPV(ST(1), prefix_len);
2611 10 ▶ if (prefix_len == 0) prefix_pv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (prefix_len == 0) prefix_pv = NULL; 1 9 10
2612 }
2613  
2614 42 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 21 21 42
if (items > 2 && SvOK(ST(2))) { 20 1 21
2615 40 pkg_pv = SvPV(ST(2), pkg_len);
2616 } else {
2617 /* Default to caller's package, an undef package too */
2618 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
2619 22 pkg_len = strlen(pkg_pv);
2620 }
2621  
2622 42 meta = get_class_meta(aTHX_ class_pv, class_len);
2623 42 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 41 42
2624 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
2625 }
2626  
2627 /* Install function-style accessors for each property */
2628 288 ▶ for (i = 1; i < meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < meta->slot_count; i++) { 247 41 288
2629 /*
2630 # uncoverable branch false
2631 # uncoverable condition right count:1 */
2632 247 ▶ if (meta->idx_to_prop[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->idx_to_prop[i]) { 247 0 247
2633 const char *install_name;
2634 char prefixed_name[256];
2635 247 ▶ if (prefix_pv) {
Branch: 1 branch
BranchTrueFalseTotal
if (prefix_pv) { 135 112 247
2636 135 snprintf(prefixed_name, sizeof(prefixed_name), "%s%s",
2637 135 prefix_pv, meta->idx_to_prop[i]);
2638 135 install_name = prefixed_name;
2639 } else {
2640 112 install_name = meta->idx_to_prop[i];
2641 }
2642 /* Pass NULL for same-class (skip validation), meta for cross-class */
2643 247 install_func_accessor(aTHX_ pkg_pv, install_name, i,
2644 NULL, 1,
2645 /*
2646 # uncoverable condition right count:1
2647 # uncoverable condition right count:2 */
2648 247 ▶ (meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL,
Branch: 2 branches
BranchTrueFalseTotal
(meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL, 247 0 247
(meta->slots && i < meta->slot_count) ? meta->slots[i] : NULL, 247 0 247
2649 meta);
2650 }
2651 }
2652  
2653 41 XSRETURN_EMPTY;
2654 }
2655  
2656 /* Object::Proto::import_accessor("Class", "prop", "alias") - import single accessor with alias */
2657 30 XS_INTERNAL(xs_import_accessor) {
2658 30 dXSARGS;
2659 STRLEN class_len, prop_len, alias_len, pkg_len;
2660 const char *class_pv, *prop_pv, *alias_pv, *pkg_pv;
2661 ClassMeta *meta;
2662 SV **idx_svp;
2663 IV idx;
2664  
2665 30 ▶ 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 29 30
2666  
2667 29 class_pv = SvPV(ST(0), class_len);
2668 29 ▶ prop_pv = SvPV(ST(1), prop_len);
Branch: 1 branch
BranchTrueFalseTotal
prop_pv = SvPV(ST(1), prop_len); 29 0 29
2669  
2670 /* Alias defaults to property name */
2671 29 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 27 2 29
if (items > 2 && SvOK(ST(2))) { 26 1 27
2672 52 alias_pv = SvPV(ST(2), alias_len);
2673 } else {
2674 3 alias_pv = prop_pv;
2675 }
2676  
2677 /* Package defaults to caller */
2678 29 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 12 17 29
2679 24 pkg_pv = SvPV(ST(3), pkg_len);
2680 } else {
2681 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
2682 17 pkg_len = strlen(pkg_pv);
2683 }
2684  
2685 29 meta = get_class_meta(aTHX_ class_pv, class_len);
2686 29 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 28 29
2687 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
2688 }
2689  
2690 /* Look up property index */
2691 28 idx_svp = hv_fetch(meta->prop_to_idx, prop_pv, prop_len, 0);
2692 28 ▶ if (!idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_svp) { 1 27 28
2693 1 croak("Property '%s' not defined in class '%s'", prop_pv, class_pv);
2694 }
2695 27 idx = SvIV(*idx_svp);
2696  
2697 /* Install with alias name — no class check, work with any compatible object */
2698 27 install_func_accessor(aTHX_ pkg_pv, alias_pv, idx,
2699 NULL, 1,
2700 /*
2701 # uncoverable condition right count:1
2702 # uncoverable condition right count:2 */
2703 27 ▶ (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL,
Branch: 2 branches
BranchTrueFalseTotal
(meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL, 27 0 27
(meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL, 27 0 27
2704 meta);
2705  
2706 27 XSRETURN_EMPTY;
2707 }
2708  
2709 /* Object::Proto::import() - export 'object' to caller's namespace */
2710 97 XS_INTERNAL(xs_import) {
2711 97 dXSARGS;
2712 const char *caller_pkg;
2713 SV *full_name;
2714 CV *define_cv, *before_cv, *after_cv, *around_cv;
2715 GV *gv;
2716  
2717 PERL_UNUSED_VAR(items);
2718  
2719 /* Get caller's package */
2720 97 ▶ caller_pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
caller_pkg = CopSTASHPV(PL_curcop); 97 0 97
caller_pkg = CopSTASHPV(PL_curcop); 97 0 97
caller_pkg = CopSTASHPV(PL_curcop); 97 0 97
caller_pkg = CopSTASHPV(PL_curcop); 0 97 97
caller_pkg = CopSTASHPV(PL_curcop); 0 0 0
caller_pkg = CopSTASHPV(PL_curcop); 97 0 97
caller_pkg = CopSTASHPV(PL_curcop); 0 97 97
2721  
2722 /* Get Object::Proto::define */
2723 97 define_cv = get_cv("Object::Proto::define", 0);
2724 /*
2725 # uncoverable branch true
2726 # uncoverable condition left count:1 */
2727 97 ▶ if (!define_cv) croak("Object::Proto::define not found");
Branch: 1 branch
BranchTrueFalseTotal
if (!define_cv) croak("Object::Proto::define not found"); 0 97 97
2728  
2729 /* Create fully qualified name: caller::object */
2730 97 full_name = newSVpvf("%s::object", caller_pkg);
2731  
2732 /* Export: create alias in caller's namespace */
2733 97 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2734 97 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 96 1 97
2735 96 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)define_cv));
2736 96 GvIMPORTED_CV_on(gv);
2737 }
2738 97 GvMULTI_on(gv);
2739 97 SvREFCNT_dec(full_name);
2740  
2741 /* Export before/after/around modifiers */
2742 97 before_cv = get_cv("Object::Proto::before", 0);
2743 97 after_cv = get_cv("Object::Proto::after", 0);
2744 97 around_cv = get_cv("Object::Proto::around", 0);
2745  
2746 /*
2747 # uncoverable branch false
2748 # uncoverable condition right count:1 */
2749 97 ▶ if (before_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (before_cv) { 97 0 97
2750 97 full_name = newSVpvf("%s::before", caller_pkg);
2751 97 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2752 97 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 96 1 97
2753 96 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)before_cv));
2754 96 GvIMPORTED_CV_on(gv);
2755 }
2756 97 GvMULTI_on(gv);
2757 97 SvREFCNT_dec(full_name);
2758 }
2759  
2760 /*
2761 # uncoverable branch false
2762 # uncoverable condition right count:1 */
2763 97 ▶ if (after_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (after_cv) { 97 0 97
2764 97 full_name = newSVpvf("%s::after", caller_pkg);
2765 97 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2766 97 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 96 1 97
2767 96 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)after_cv));
2768 96 GvIMPORTED_CV_on(gv);
2769 }
2770 97 GvMULTI_on(gv);
2771 97 SvREFCNT_dec(full_name);
2772 }
2773  
2774 /*
2775 # uncoverable branch false
2776 # uncoverable condition right count:1 */
2777 97 ▶ if (around_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (around_cv) { 97 0 97
2778 97 full_name = newSVpvf("%s::around", caller_pkg);
2779 97 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2780 97 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 96 1 97
2781 96 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)around_cv));
2782 96 GvIMPORTED_CV_on(gv);
2783 }
2784 97 GvMULTI_on(gv);
2785 97 SvREFCNT_dec(full_name);
2786 }
2787  
2788 /* Export role/requires/with */
2789 {
2790 static const char *names[] = { "role", "requires", "with" };
2791 int i;
2792 388 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 291 97 388
2793 291 CV *cv = get_cvn_flags(
2794 Perl_form(aTHX_ "Object::Proto::%s", names[i]),
2795 strlen("Object::Proto::") + strlen(names[i]), 0);
2796 /*
2797 # uncoverable branch false
2798 # uncoverable condition right count:1 */
2799 291 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 291 0 291
2800 291 full_name = newSVpvf("%s::%s", caller_pkg, names[i]);
2801 291 gv = gv_fetchsv(full_name, GV_ADD, SVt_PVCV);
2802 291 ▶ if (GvCV(gv) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv) == NULL) { 288 3 291
2803 288 GvCV_set(gv, (CV*)SvREFCNT_inc((SV*)cv));
2804 288 GvIMPORTED_CV_on(gv);
2805 }
2806 291 GvMULTI_on(gv);
2807 291 SvREFCNT_dec(full_name);
2808 }
2809 }
2810 }
2811  
2812 97 XSRETURN_EMPTY;
2813 }
2814  
2815 /* ============================================
2816 Install accessor into class
2817 ============================================ */
2818  
2819 406 static void install_accessor(pTHX_ const char *class_name, const char *prop_name, IV idx) {
2820 char full_name[256];
2821 CV *cv;
2822 SV *ckobj;
2823  
2824 406 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, prop_name);
2825  
2826 /* Check if accessor already exists to avoid redefinition warnings */
2827 406 cv = get_cvn_flags(full_name, strlen(full_name), 0);
2828 406 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 11 395 406
2829 11 return; /* Already defined, skip */
2830 }
2831  
2832 395 cv = newXS(full_name, xs_accessor_fallback, __FILE__);
2833 395 CvXSUBANY(cv).any_iv = idx;
2834  
2835 395 ckobj = newSViv(idx);
2836 395 cv_set_call_checker(cv, accessor_call_checker, ckobj);
2837 }
2838  
2839 /* Enforce a :private attribute. Every method Object::Proto generates for the
2840 * attribute - accessor, reader, writer, clearer, predicate and function-style
2841 * accessor - may only be called from code compiled in the package that
2842 * declared it: the class for its own attributes, the parent that declared an
2843 * inherited one, and for a role's attribute the role or the class that
2844 * composed it. In an XSUB PL_curcop is the caller's cop, so its stash names
2845 * the calling package. meta is the class the method was installed in, named
2846 * in the error. Reached through check_private, only for a private attribute.
2847 * Packages are compared by name, not stash pointer: a thread has its own copy
2848 * of each stash. */
2849 63 static void object_check_private(pTHX_ SlotSpec *spec, ClassMeta *meta) {
2850 HV *stash;
2851 const char *name;
2852 const char *caller;
2853 63 ▶ 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 */ 63 0 63
2854 /*
2855 # uncoverable condition right count:1
2856 # uncoverable condition left count:2 */
2857 63 ▶ name = stash ? HvNAME(stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
name = stash ? HvNAME(stash) : NULL; 63 0 63
name = stash ? HvNAME(stash) : NULL; 63 0 63
name = stash ? HvNAME(stash) : NULL; 63 0 63
name = stash ? HvNAME(stash) : NULL; 0 63 63
name = stash ? HvNAME(stash) : NULL; 0 0 0
name = stash ? HvNAME(stash) : NULL; 63 0 63
name = stash ? HvNAME(stash) : NULL; 0 63 63
2858 63 ▶ caller = name ? name : ""; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
caller = name ? name : ""; /* # uncoverable condition right count:1 */ 63 0 63
2859 63 ▶ if (strEQ(caller, spec->private_owner)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(caller, spec->private_owner)) return; 25 38 63
2860 38 ▶ if (spec->private_also && strEQ(caller, spec->private_also)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (spec->private_also && strEQ(caller, spec->private_also)) return; 7 31 38
if (spec->private_also && strEQ(caller, spec->private_also)) return; 2 5 7
2861 36 croak("cannot call private attribute %s on %s from %s",
2862 spec->name, meta->class_name, caller);
2863 }
2864  
2865 /* Build a lazy slot on first access: the builder's value, or a fresh copy
2866 * of the default (a new empty array or hash for [] and {}). A defined value
2867 * is type checked, then stored in the slot. Returns what was stored, or NULL
2868 * when the slot has neither a builder nor a default. */
2869 3844 static SV *slot_build_lazy(pTHX_ SV *self, AV *av, IV idx, SlotSpec *spec) {
2870 3844 SV *built_val = NULL;
2871  
2872 7680 ▶ 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 */ 3836 8 3844
if (spec->has_builder && spec->builder_name) { /* # uncoverable condition right count:2 */ 3836 0 3836
2873 /* Call builder method */
2874 3836 dSP;
2875 3836 const char *builder = SvPV_nolen(spec->builder_name);
2876 int count;
2877  
2878 3836 ENTER;
2879 3836 SAVETMPS;
2880 3836 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3836 3836
2881 3836 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 3836 3836
2882 3836 PUTBACK;
2883  
2884 3836 count = call_method(builder, G_SCALAR);
2885  
2886 3836 SPAGAIN;
2887 /*
2888 # uncoverable branch false
2889 # uncoverable condition right count:1 */
2890 3836 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 3836 0 3836
2891 /* Copy the value BEFORE FREETMPS to avoid freed scalar issue */
2892 3836 built_val = newSVsv(POPs);
2893 } else {
2894 0 built_val = newSV(0); /* undef */ /* # uncoverable statement */
2895 }
2896 3836 PUTBACK;
2897 3836 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3836 0 3836
2898 3836 LEAVE;
2899 8 ▶ } else if (spec->has_default && spec->default_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (spec->has_default && spec->default_sv) { 6 2 8
} else if (spec->has_default && spec->default_sv) { 5 1 6
2900 /* Use default value for lazy default */
2901 5 ▶ if (SvROK(spec->default_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec->default_sv)) { 2 3 5
2902 /* Clone reference types (arrays, hashes) */
2903 2 SV *inner = SvRV(spec->default_sv);
2904 2 ▶ if (SvTYPE(inner) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) == SVt_PVAV) { 1 1 2
2905 1 built_val = newRV_noinc((SV*)newAV());
2906 /*
2907 # uncoverable branch false
2908 # uncoverable condition right count:1 */
2909 1 ▶ } else if (SvTYPE(inner) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(inner) == SVt_PVHV) { 1 0 1
2910 1 built_val = newRV_noinc((SV*)newHV());
2911 } else {
2912 0 built_val = newSVsv(spec->default_sv); /* # uncoverable statement */
2913 }
2914 } else {
2915 3 built_val = newSVsv(spec->default_sv);
2916 }
2917 }
2918  
2919 3844 ▶ if (!built_val) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!built_val) return NULL; 3 3841 3844
2920  
2921 /* Type check the built value */
2922 3841 ▶ if (spec->has_type && SvOK(built_val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_type && SvOK(built_val)) { 3840 1 3841
if (spec->has_type && SvOK(built_val)) { 3836 4 3840
2923 3836 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 3 3833 3836
2924 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
2925 1 ? spec->registered->name
2926 4 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 2 3
2927 3 SvREFCNT_dec(built_val);
2928 3 croak("Type constraint failed for lazy '%s': expected %s",
2929 spec->name, type_name);
2930 }
2931 }
2932  
2933 /* Store the built value - built_val already has correct refcount from newSVsv */
2934 3838 av_store(av, idx, built_val);
2935 3838 return built_val;
2936 }
2937  
2938 72231 XS_INTERNAL(xs_accessor_typed_fallback) {
2939 72231 dXSARGS;
2940 72231 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
2941 72231 IV idx = data->slot_idx;
2942 72231 ClassMeta *meta = data->meta;
2943 72231 SlotSpec *spec = meta->slots[idx];
2944 72231 SV *self = ST(0);
2945 AV *av;
2946  
2947 72231 check_private(aTHX_ spec, meta);
2948  
2949 72206 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 72205 1 72206
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 72204 72205
2950 2 croak("Not an object");
2951 }
2952 72204 av = (AV*)SvRV(self);
2953  
2954 72204 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 26389 45815 72204
2955 /* Setter with type check */
2956 26389 SV *val = ST(1);
2957 26389 MAGIC *mg = get_object_magic(aTHX_ self);
2958 26389 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 26387 26389
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
2959 1 croak("Cannot modify frozen object");
2960 }
2961
2962 26388 ▶ if (spec->is_readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_readonly) { 9 26379 26388
2963 9 croak("Cannot modify readonly slot '%s'", spec->name);
2964 }
2965
2966 /* Required fields cannot be set to undef */
2967 26379 ▶ if (spec->is_required && !SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_required && !SvOK(val)) { 1009 25370 26379
if (spec->is_required && !SvOK(val)) { 2 1007 1009
2968 2 croak("Cannot set required slot '%s' to undef", spec->name);
2969 }
2970
2971 /* Coercion */
2972 26377 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 26370 7 26377
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 11 26359 26370
2973 18 val = apply_slot_coercion(aTHX_ val, spec);
2974  
2975 /* Type check */
2976 26377 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 26366 11 26377
2977 26366 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 174 26192 26366
2978 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
2979 4 ? spec->registered->name
2980 178 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 4 170 174
2981 174 croak("Type constraint failed for '%s': expected %s",
2982 spec->name, type_name);
2983 }
2984 }
2985  
2986 /* Trigger callback ($self, $new_value) */
2987 26203 ▶ if (spec->has_trigger && spec->trigger_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_trigger && spec->trigger_cb) { 6 26197 26203
if (spec->has_trigger && spec->trigger_cb) { 4 2 6
2988 4 dSP;
2989 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
2990 4 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 4 4
2991 4 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 4 4
2992 4 PUTBACK;
2993 8 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
2994 }
2995
2996 {
2997 26203 SV *stored = newSVsv(val);
2998 26203 av_store(av, idx, stored);
2999 /* Weaken reference if is_weak flag is set */
3000 26203 ▶ if (spec->is_weak && SvROK(stored)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_weak && SvROK(stored)) { 4 26199 26203
if (spec->is_weak && SvROK(stored)) { 3 1 4
3001 3 sv_rvweaken(stored);
3002 }
3003 }
3004 26203 ST(0) = val;
3005 26203 XSRETURN(1);
3006 } else {
3007 /* Getter - use prototype chain resolution, handle lazy */
3008 45815 SV *result = resolve_property_chain(aTHX_ av, idx);
3009
3010 /* Lazy initialization: if undef and is_lazy, build/default on first access */
3011 45815 ▶ if (spec->is_lazy && !SvOK(result)) {
Branch: 2 branches
BranchTrueFalseTotal
if (spec->is_lazy && !SvOK(result)) { 7440 38375 45815
if (spec->is_lazy && !SvOK(result)) { 3836 3604 7440
3012 3836 SV *built_val = slot_build_lazy(aTHX_ self, av, idx, spec);
3013 3833 ▶ if (built_val) result = built_val;
Branch: 1 branch
BranchTrueFalseTotal
if (built_val) result = built_val; 3831 2 3833
3014 }
3015
3016 45812 ST(0) = result;
3017 45812 XSRETURN(1);
3018 }
3019 }
3020  
3021 /* Call checker for typed accessor */
3022 31 static OP* accessor_typed_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
3023 31 SlotOpData *data = INT2PTR(SlotOpData*, SvIV(ckobj));
3024 31 IV idx = data->slot_idx;
3025 OP *pushop, *cvop, *selfop, *argop;
3026 OP *newop;
3027  
3028 PERL_UNUSED_ARG(namegv);
3029  
3030 /* Private slots keep the XSUB (which enforces the caller check) rather than
3031 * being inlined to a custom op that would bypass it. */
3032 31 ▶ if (data->meta->slots[idx]->is_private) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (data->meta->slots[idx]->is_private) return entersubop; 3 28 31
3033  
3034 28 pushop = cUNOPx(entersubop)->op_first;
3035 /*
3036 # uncoverable branch false
3037 # uncoverable condition right count:1 */
3038 28 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 28 0 28
3039 28 pushop = cUNOPx(pushop)->op_first;
3040 }
3041  
3042 28 ▶ selfop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
selfop = OpSIBLING(pushop); 28 0 28
3043 28 cvop = selfop;
3044 28 argop = selfop;
3045 82 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 54 28 82
3046 54 argop = cvop;
3047 54 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 54 0 54
3048 }
3049  
3050 /* Check if there's an argument after self (setter call) */
3051 28 ▶ if (argop != selfop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop != selfop) { 26 2 28
3052 /* Setter: $obj->name($value) - use typed setter */
3053 26 ▶ OP *valop = OpSIBLING(selfop);
Branch: 1 branch
BranchTrueFalseTotal
OP *valop = OpSIBLING(selfop); 26 0 26
3054 26 SlotSpec *spec = data->meta->slots[idx];
3055  
3056 /* When the value is already known at compile time, the type check can
3057 * be settled here instead of on every call. That is only equivalent if
3058 * a type check is the sole thing the typed setter would have done:
3059 * pp_object_set handles the frozen check and the store, but not
3060 * readonly, required, coercion, triggers or weak refs. A value that
3061 * fails falls through to the runtime path, so a type error is still
3062 * reported where it always was. */
3063 26 ▶ if (valop->op_type == OP_CONST
Branch: 1 branch
BranchTrueFalseTotal
if (valop->op_type == OP_CONST 15 11 26
3064 15 ▶ && spec->has_type
Branch: 1 branch
BranchTrueFalseTotal
&& spec->has_type 14 1 15
3065 14 ▶ && !spec->has_checks
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->has_checks 12 2 14
3066 12 ▶ && !spec->has_trigger
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->has_trigger 11 1 12
3067 11 ▶ && !spec->is_weak
Branch: 1 branch
BranchTrueFalseTotal
&& !spec->is_weak 10 1 11
3068 10 ▶ && check_slot_type(aTHX_ cSVOPx_sv(valop), spec))
Branch: 1 branch
BranchTrueFalseTotal
&& check_slot_type(aTHX_ cSVOPx_sv(valop), spec)) 5 5 10
3069 {
3070 5 OpMORESIB_set(pushop, cvop);
3071 5 OpLASTSIB_set(valop, NULL);
3072 5 OpLASTSIB_set(selfop, NULL);
3073  
3074 5 newop = newBINOP(OP_NULL, 0, selfop, valop);
3075 5 newop->op_type = OP_CUSTOM;
3076 5 newop->op_ppaddr = pp_object_set;
3077 5 newop->op_targ = idx;
3078  
3079 5 op_free(entersubop);
3080 5 return newop;
3081 }
3082  
3083 21 OpMORESIB_set(pushop, cvop);
3084 21 OpLASTSIB_set(valop, NULL);
3085 21 OpLASTSIB_set(selfop, NULL);
3086
3087 /* Use OP_NULL to avoid assertion failures on debugging Perls
3088 * (5.14-5.20), then set OP_CUSTOM */
3089 21 newop = newBINOP(OP_NULL, 0, selfop, valop);
3090 21 newop->op_type = OP_CUSTOM;
3091 21 newop->op_ppaddr = pp_object_set_typed;
3092 21 newop->op_targ = PTR2IV(data);
3093
3094 21 op_free(entersubop);
3095 21 return newop;
3096 } else {
3097 /* Getter: $obj->name - plain getter (no type check needed) */
3098 2 OpMORESIB_set(pushop, cvop);
3099 2 OpLASTSIB_set(selfop, NULL);
3100
3101 /* Use OP_NULL to avoid assertion failures on debugging Perls
3102 * (5.14-5.20), then set OP_CUSTOM */
3103 2 newop = newUNOP(OP_NULL, 0, selfop);
3104 2 newop->op_type = OP_CUSTOM;
3105 2 newop->op_ppaddr = pp_object_get;
3106 2 newop->op_targ = idx;
3107
3108 2 op_free(entersubop);
3109 2 return newop;
3110 }
3111 }
3112  
3113 /* XS fallback for reader-only accessor (get_X style) */
3114 44 XS_INTERNAL(xs_reader_fallback) {
3115 44 dXSARGS;
3116 44 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
3117 44 IV idx = data->slot_idx;
3118 44 ClassMeta *meta = data->meta;
3119 44 SlotSpec *spec = meta->slots[idx];
3120 44 SV *self = ST(0);
3121 AV *av;
3122  
3123 PERL_UNUSED_ARG(items);
3124  
3125 44 check_private(aTHX_ spec, meta);
3126  
3127 42 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 41 1 42
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 40 41
3128 2 croak("Not an object");
3129 }
3130 40 av = (AV*)SvRV(self);
3131  
3132 /* Handle lazy builder */
3133 /*
3134 # uncoverable condition right count:1
3135 # uncoverable condition right count:4 */
3136 40 ▶ 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) { 40 0 40
if (spec && spec->is_lazy && spec->has_builder && spec->builder_name) { 11 29 40
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
3137 10 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 9 1 10
3138 9 SV *slot = AvARRAY(av)[idx];
3139 9 ▶ if (!slot || !SvOK(slot)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(slot)) { 8 1 9
if (!slot || !SvOK(slot)) { 7 1 8
3140 /* Call builder method */
3141 8 dSP;
3142 IV count;
3143 8 ENTER;
3144 8 SAVETMPS;
3145 8 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 8 8
3146 8 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 8 8
3147 8 PUTBACK;
3148 16 count = call_method(SvPV_nolen(spec->builder_name), G_SCALAR);
3149 8 SPAGAIN;
3150 /*
3151 # uncoverable branch false
3152 # uncoverable condition right count:1 */
3153 8 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 8 0 8
3154 8 SV *built_val = POPs;
3155
3156 /* Type check the built value */
3157 8 ▶ if (spec->has_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) { 7 1 8
3158 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
3159 2 built_val = apply_slot_coercion(aTHX_ built_val, spec);
3160 7 ▶ if (!check_slot_type(aTHX_ built_val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ built_val, spec)) { 2 5 7
3161 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
3162 1 ? spec->registered->name
3163 3 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 1 1 2
3164 2 croak("Type constraint failed for '%s' in builder: expected %s",
3165 spec->name, type_name);
3166 }
3167 }
3168
3169 6 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 5 1 6
3170 5 sv_setsv(slot, built_val);
3171 }
3172 else {
3173 1 av_store(av, idx, newSVsv(built_val));
3174 }
3175 }
3176 6 PUTBACK;
3177 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
3178 6 LEAVE;
3179 }
3180 }
3181 }
3182  
3183 /* Getter - fast path: direct slot access */
3184 38 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 35 3 38
3185 35 SV *sv = AvARRAY(av)[idx];
3186 35 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 34 1 35
if (sv && SvOK(sv)) { 30 4 34
3187 30 ST(0) = sv;
3188 30 XSRETURN(1);
3189 }
3190 }
3191 /* Slow path: check prototype chain */
3192 {
3193 8 SV **proto = av_fetch(av, 0, 0);
3194 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
3195 1 SV *result = resolve_property_chain(aTHX_ av, idx);
3196 1 ST(0) = result;
3197 1 XSRETURN(1);
3198 }
3199 }
3200 7 ST(0) = &PL_sv_undef;
3201 7 XSRETURN(1);
3202 }
3203  
3204 /* XS fallback for writer-only accessor (set_X style) */
3205 40 XS_INTERNAL(xs_writer_fallback) {
3206 40 dXSARGS;
3207 40 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
3208 40 IV idx = data->slot_idx;
3209 40 ClassMeta *meta = data->meta;
3210 40 SlotSpec *spec = meta->slots[idx];
3211 40 SV *self = ST(0);
3212 AV *av;
3213 MAGIC *mg;
3214  
3215 40 check_private(aTHX_ spec, meta);
3216  
3217 39 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 38 1 39
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 37 38
3218 2 croak("Not an object");
3219 }
3220 37 av = (AV*)SvRV(self);
3221  
3222 37 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 1 36 37
3223 1 croak("Writer method requires a value argument");
3224 }
3225  
3226 /* Check frozen */
3227 36 mg = get_object_magic(aTHX_ self);
3228 36 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 34 36
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
3229 1 croak("Cannot modify frozen object");
3230 }
3231  
3232 /* Check readonly */
3233 35 ▶ if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */ 35 0 35
if (spec && spec->is_readonly) { /* # uncoverable condition right count:1 */ 1 34 35
3234 1 croak("Cannot modify readonly slot '%s'", spec->name);
3235 }
3236  
3237 {
3238 34 SV *val = ST(1);
3239
3240 /* Required fields cannot be set to undef */
3241 34 ▶ 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 */ 34 0 34
if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */ 2 32 34
if (spec && spec->is_required && !SvOK(val)) { /* # uncoverable condition right count:1 */ 1 1 2
3242 1 croak("Cannot set required slot '%s' to undef", spec->name);
3243 }
3244
3245 /* Coerce + type check */
3246 33 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 33 0 33
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 20 13 33
3247 20 ▶ if (spec->has_coerce || spec->type_id == TYPE_CUSTOM)
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 19 1 20
if (spec->has_coerce || spec->type_id == TYPE_CUSTOM) 4 15 19
3248 5 val = apply_slot_coercion(aTHX_ val, spec);
3249 20 ▶ if (!check_slot_type(aTHX_ val, spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!check_slot_type(aTHX_ val, spec)) { 5 15 20
3250 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
3251 2 ? spec->registered->name
3252 7 ▶ : type_id_to_name(spec->type_id);
Branch: 1 branch
BranchTrueFalseTotal
: type_id_to_name(spec->type_id); 2 3 5
3253 5 croak("Type constraint failed for '%s': expected %s",
3254 spec->name, type_name);
3255 }
3256 }
3257
3258 /* Trigger callback ($self, $new_value) */
3259 28 ▶ 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 */ 28 0 28
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 3 25 28
if (spec && spec->has_trigger && spec->trigger_cb) { /* # uncoverable condition right count:1 */ 1 2 3
3260 1 dSP;
3261 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
3262 1 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 1 1
3263 1 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 1 1
3264 1 PUTBACK;
3265 2 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
3266 }
3267
3268 /* In-place update */
3269 28 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 27 1 28
3270 27 SV *slot = AvARRAY(av)[idx];
3271 27 ▶ if (slot_is_writable(slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_is_writable(slot)) { 23 4 27
3272 23 sv_setsv(slot, val);
3273 /* Weaken reference if is_weak flag is set */
3274 23 ▶ 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 */ 23 0 23
if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */ 2 21 23
if (spec && spec->is_weak && SvROK(slot)) { /* # uncoverable condition right count:1 */ 1 1 2
3275 1 sv_rvweaken(slot);
3276 }
3277 23 ST(0) = val;
3278 23 XSRETURN(1);
3279 }
3280 }
3281 {
3282 5 SV *stored = newSVsv(val);
3283 5 av_store(av, idx, stored);
3284 /* Weaken reference if is_weak flag is set */
3285 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
3286 2 sv_rvweaken(stored);
3287 }
3288 }
3289 5 ST(0) = val;
3290 5 XSRETURN(1);
3291 }
3292 }
3293  
3294 /* Install reader-only accessor (get_X style) */
3295 36 static void install_reader(pTHX_ const char *class_name, const char *method_name, IV idx, ClassMeta *meta) {
3296 char full_name[256];
3297 CV *cv;
3298 SlotOpData *data;
3299  
3300 36 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, method_name);
3301  
3302 /* Check if method already exists */
3303 36 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3304 36 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 35 36
3305 1 return;
3306 }
3307  
3308 35 Newx(data, 1, SlotOpData);
3309 35 data->slot_idx = idx;
3310 35 data->meta = meta;
3311  
3312 35 cv = newXS(full_name, xs_reader_fallback, __FILE__);
3313 35 CvXSUBANY(cv).any_iv = PTR2IV(data);
3314 }
3315  
3316 /* Install writer-only accessor (set_X style) */
3317 33 static void install_writer(pTHX_ const char *class_name, const char *method_name, IV idx, ClassMeta *meta) {
3318 char full_name[256];
3319 CV *cv;
3320 SlotOpData *data;
3321  
3322 33 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, method_name);
3323  
3324 /* Check if method already exists */
3325 33 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3326 33 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 32 33
3327 1 return;
3328 }
3329  
3330 32 Newx(data, 1, SlotOpData);
3331 32 data->slot_idx = idx;
3332 32 data->meta = meta;
3333  
3334 32 cv = newXS(full_name, xs_writer_fallback, __FILE__);
3335 32 CvXSUBANY(cv).any_iv = PTR2IV(data);
3336 }
3337  
3338 /* Install typed accessor (with type check, triggers, etc.) */
3339 858 static void install_accessor_typed(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta) {
3340 char full_name[256];
3341 CV *cv;
3342 SV *ckobj;
3343 SlotOpData *data;
3344  
3345 858 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, prop_name);
3346  
3347 /* An accessor may already be there: a typed one from an earlier
3348 * definition keeps its CV and takes the new slot data, a plain one
3349 * (the attribute had no type until this +attr or redefinition) is
3350 * replaced, and a sub of the user's own is left alone. */
3351 858 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3352 858 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 71 787 858
3353 71 ▶ if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) {
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) { 69 2 71
if (CvISXSUB(cv) && CvXSUB(cv) == xs_accessor_typed_fallback) { 62 7 69
3354 62 data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
3355 62 data->slot_idx = idx;
3356 62 data->meta = meta;
3357 65 return;
3358 }
3359 9 ▶ if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) { 7 2 9
if (!CvISXSUB(cv) || CvXSUB(cv) != xs_accessor_fallback) { 1 6 7
3360 3 return;
3361 }
3362 {
3363 6 HV *stash = gv_stashpv(class_name, 0);
3364 /*
3365 # uncoverable branch false
3366 # uncoverable condition right count:1 */
3367 6 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 6 0 6
3368 6 (void)hv_delete(stash, prop_name, strlen(prop_name), G_DISCARD);
3369 }
3370 }
3371 }
3372  
3373 /* Allocate persistent data for this slot */
3374 793 Newx(data, 1, SlotOpData);
3375 793 data->slot_idx = idx;
3376 793 data->meta = meta;
3377  
3378 793 cv = newXS(full_name, xs_accessor_typed_fallback, __FILE__);
3379 793 CvXSUBANY(cv).any_iv = PTR2IV(data);
3380  
3381 793 ckobj = newSViv(PTR2IV(data));
3382 793 cv_set_call_checker(cv, accessor_typed_call_checker, ckobj);
3383 }
3384  
3385 /* XS fallback for clearer method (clear_X) */
3386 9857 XS_INTERNAL(xs_clearer_fallback) {
3387 9857 dXSARGS;
3388 9857 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
3389 9857 IV idx = data->slot_idx;
3390 9857 SV *self = ST(0);
3391 AV *av;
3392 MAGIC *mg;
3393  
3394 PERL_UNUSED_ARG(items);
3395  
3396 9857 check_private(aTHX_ data->meta->slots[idx], data->meta);
3397  
3398 9856 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 9855 1 9856
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 9854 9855
3399 2 croak("Not an object");
3400 }
3401 9854 av = (AV*)SvRV(self);
3402  
3403 /* Check frozen */
3404 9854 mg = get_object_magic(aTHX_ self);
3405 9854 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 2 9852 9854
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 1 2
3406 1 croak("Cannot modify frozen object");
3407 }
3408  
3409 /* Clear the slot by setting to undef */
3410 9853 av_store(av, idx, newSV(0));
3411
3412 9853 ST(0) = self; /* Return self for chaining */
3413 9853 XSRETURN(1);
3414 }
3415  
3416 /* Install clearer method (clear_X or custom name) */
3417 57 static void install_clearer(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name) {
3418 char full_name[256];
3419 CV *cv;
3420 SlotOpData *data;
3421  
3422 57 ▶ 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 */ 16 41 57
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 16 0 16
3423 16 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, SvPV_nolen(custom_name));
3424 } else {
3425 41 snprintf(full_name, sizeof(full_name), "%s::clear_%s", class_name, prop_name);
3426 }
3427  
3428 /* Check if method already exists */
3429 57 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3430 57 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 3 54 57
3431 3 return;
3432 }
3433  
3434 54 Newx(data, 1, SlotOpData);
3435 54 data->slot_idx = idx;
3436 54 data->meta = meta;
3437  
3438 54 cv = newXS(full_name, xs_clearer_fallback, __FILE__);
3439 54 CvXSUBANY(cv).any_iv = PTR2IV(data);
3440 }
3441  
3442 /* XS fallback for predicate method (has_X) */
3443 10247 XS_INTERNAL(xs_predicate_fallback) {
3444 10247 dXSARGS;
3445 10247 SlotOpData *data = INT2PTR(SlotOpData*, CvXSUBANY(cv).any_iv);
3446 10247 IV idx = data->slot_idx;
3447 10247 SV *self = ST(0);
3448 AV *av;
3449 SV **svp;
3450  
3451 PERL_UNUSED_ARG(items);
3452  
3453 10247 check_private(aTHX_ data->meta->slots[idx], data->meta);
3454  
3455 10246 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 10245 1 10246
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) { 1 10244 10245
3456 2 croak("Not an object");
3457 }
3458 10244 av = (AV*)SvRV(self);
3459  
3460 /* Check if slot has a defined value */
3461 10244 svp = av_fetch(av, idx, 0);
3462 10244 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 10243 1 10244
if (svp && SvOK(*svp)) { 6225 4018 10243
3463 6225 ST(0) = &PL_sv_yes;
3464 } else {
3465 4019 ST(0) = &PL_sv_no;
3466 }
3467 10244 XSRETURN(1);
3468 }
3469  
3470 /* Install predicate method (has_X or custom name) */
3471 31 static void install_predicate(pTHX_ const char *class_name, const char *prop_name, IV idx, ClassMeta *meta, SV *custom_name) {
3472 char full_name[256];
3473 CV *cv;
3474 SlotOpData *data;
3475  
3476 31 ▶ 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 */ 8 23 31
if (custom_name && SvOK(custom_name)) { /* # uncoverable condition right count:2 */ 8 0 8
3477 8 snprintf(full_name, sizeof(full_name), "%s::%s", class_name, SvPV_nolen(custom_name));
3478 } else {
3479 23 snprintf(full_name, sizeof(full_name), "%s::has_%s", class_name, prop_name);
3480 }
3481  
3482 /* Check if method already exists */
3483 31 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3484 31 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 2 29 31
3485 2 return;
3486 }
3487  
3488 29 Newx(data, 1, SlotOpData);
3489 29 data->slot_idx = idx;
3490 29 data->meta = meta;
3491  
3492 29 cv = newXS(full_name, xs_predicate_fallback, __FILE__);
3493 29 CvXSUBANY(cv).any_iv = PTR2IV(data);
3494 }
3495  
3496 /* ============================================
3497 DEMOLISH Support (zero overhead if not used)
3498 ============================================ */
3499  
3500 /* XS DESTROY wrapper that calls DEMOLISH */
3501 6 XS_INTERNAL(xs_destroy_wrapper) {
3502 6 dXSARGS;
3503 6 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
3504 6 SV *self = ST(0);
3505
3506 PERL_UNUSED_VAR(items);
3507
3508 /*
3509 # uncoverable branch false
3510 # uncoverable condition right count:1
3511 # uncoverable condition right count:2 */
3512 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
3513 6 dSP;
3514 6 ENTER;
3515 6 SAVETMPS;
3516 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
3517 6 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 6 6
3518 6 PUTBACK;
3519 6 call_sv((SV*)meta->demolish_cv, G_DISCARD | G_EVAL);
3520 6 SPAGAIN;
3521 /* Ignore errors in DEMOLISH - don't die during destruction */
3522 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
3523 2 ▶ warn("Error in DEMOLISH: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("Error in DEMOLISH: %s", SvPV_nolen(ERRSV)); 1 0 1
3524 }
3525 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 5 6
3526 6 LEAVE;
3527 }
3528
3529 6 XSRETURN_EMPTY;
3530 }
3531  
3532 /* Install DESTROY wrapper - only called if DEMOLISH exists */
3533 5 static void install_destroy_wrapper(pTHX_ const char *class_name, ClassMeta *meta) {
3534 char full_name[256];
3535 CV *cv;
3536
3537 5 snprintf(full_name, sizeof(full_name), "%s::DESTROY", class_name);
3538
3539 /* Check if DESTROY already exists - don't override user's DESTROY */
3540 5 cv = get_cvn_flags(full_name, strlen(full_name), 0);
3541 5 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 1 4 5
3542 1 return; /* User has their own DESTROY, don't interfere */
3543 }
3544
3545 4 cv = newXS(full_name, xs_destroy_wrapper, __FILE__);
3546 4 CvXSUBANY(cv).any_iv = PTR2IV(meta);
3547 }
3548  
3549 /* ============================================
3550 Slot registration - one path for a class's own slots, the slots it
3551 inherits and the slots a role gives it
3552 ============================================ */
3553  
3554 /* Class-level fast-path flags for one slot */
3555 1264 static void note_slot_flags(ClassMeta *meta, const SlotSpec *spec) {
3556 1264 ▶ if (spec->has_type) meta->has_any_types = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_type) meta->has_any_types = 1; 796 468 1264
3557 /* An implicit default from handles is left out: new() reads its
3558 * arguments as named pairs when the class has any default */
3559 1264 ▶ if (spec->has_default && !spec->default_implicit) meta->has_any_defaults = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (spec->has_default && !spec->default_implicit) meta->has_any_defaults = 1; 209 1055 1264
if (spec->has_default && !spec->default_implicit) meta->has_any_defaults = 1; 158 51 209
3560 1264 ▶ if (spec->has_trigger) meta->has_any_triggers = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_trigger) meta->has_any_triggers = 1; 32 1232 1264
3561 1264 ▶ if (spec->is_required) meta->has_any_required = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_required) meta->has_any_required = 1; 59 1205 1264
3562 1264 ▶ if (spec->is_lazy) meta->has_any_lazy = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_lazy) meta->has_any_lazy = 1; 60 1204 1264
3563 1264 ▶ if (spec->has_builder) meta->has_any_builders = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_builder) meta->has_any_builders = 1; 56 1208 1264
3564 1264 ▶ if (spec->is_weak) meta->has_any_weak = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (spec->is_weak) meta->has_any_weak = 1; 17 1247 1264
3565 1264 }
3566  
3567 /* Install every method the slot's spec asks for */
3568 1264 static void install_slot_methods(pTHX_ const char *class_pv, ClassMeta *meta, IV idx, SlotSpec *spec) {
3569 /* The typed accessor carries the checks, the :private one among them,
3570 * that the plain accessor leaves out */
3571 1264 ▶ if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly
Branch: 4 branches
BranchTrueFalseTotal
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly 468 796 1264
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly 458 10 468
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly 450 8 458
if (spec->has_type || spec->has_trigger || spec->has_coerce || spec->is_readonly 446 4 450
3572 446 ▶ || spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) {
Branch: 4 branches
BranchTrueFalseTotal
|| spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 433 13 446
|| spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 428 5 433
|| spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 420 8 428
|| spec->is_lazy || spec->is_required || spec->is_weak || spec->is_private) { 14 406 420
3573 858 install_accessor_typed(aTHX_ class_pv, spec->name, idx, meta);
3574 } else {
3575 406 install_accessor(aTHX_ class_pv, spec->name, idx);
3576 }
3577 1264 ▶ if (spec->has_clearer) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_clearer) { 57 1207 1264
3578 57 install_clearer(aTHX_ class_pv, spec->name, idx, meta, spec->clearer_name);
3579 }
3580 1264 ▶ if (spec->has_predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_predicate) { 31 1233 1264
3581 31 install_predicate(aTHX_ class_pv, spec->name, idx, meta, spec->predicate_name);
3582 }
3583 1264 ▶ if (spec->reader_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->reader_name) { 36 1228 1264
3584 72 install_reader(aTHX_ class_pv, SvPV_nolen(spec->reader_name), idx, meta);
3585 }
3586 1264 ▶ if (spec->writer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->writer_name) { 33 1231 1264
3587 66 install_writer(aTHX_ class_pv, SvPV_nolen(spec->writer_name), idx, meta);
3588 }
3589 /* A slot's handles(...) delegators are installed by
3590 * install_class_delegators, in a pass after every slot of the class is
3591 * in, so that a delegator can never take a name an accessor still has to
3592 * claim. */
3593 1264 }
3594  
3595 /* Put a slot into a class at idx: the property and constructor-argument
3596 * lookups, the fast-path flags and the slot's methods */
3597 1264 static void register_slot(pTHX_ const char *class_pv, ClassMeta *meta, IV idx, SlotSpec *spec) {
3598 1264 meta->slots[idx] = spec;
3599 1264 note_slot_flags(meta, spec);
3600 1264 hv_store(meta->prop_to_idx, spec->name, strlen(spec->name), newSViv(idx), 0);
3601 /* The constructor takes the init_arg if there is one, else the property name */
3602 1264 ▶ if (spec->init_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->init_arg) { 15 1249 1264
3603 STRLEN arg_len;
3604 15 const char *arg_name = SvPV(spec->init_arg, arg_len);
3605 15 hv_store(meta->arg_to_idx, arg_name, arg_len, newSViv(idx), 0);
3606 } else {
3607 1249 hv_store(meta->arg_to_idx, spec->name, strlen(spec->name), newSViv(idx), 0);
3608 }
3609 1264 meta->idx_to_prop[idx] = spec->name;
3610 1264 install_slot_methods(aTHX_ class_pv, meta, idx, spec);
3611 1264 }
3612  
3613 /* ============================================
3614 Role Support (zero overhead if not used)
3615 ============================================ */
3616  
3617 111 static RoleMeta* get_role_meta(pTHX_ const char *role_name, STRLEN len) {
3618 SV **svp;
3619 111 ▶ if (!g_role_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_role_registry) return NULL; 11 100 111
3620 100 svp = hv_fetch(g_role_registry, role_name, len, 0);
3621 100 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 67 33 100
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 67 0 67
3622 67 return INT2PTR(RoleMeta*, SvIV(*svp));
3623 }
3624 33 return NULL;
3625 }
3626  
3627 33 static void register_role_meta(pTHX_ const char *role_name, STRLEN len, RoleMeta *meta) {
3628 33 ▶ if (!g_role_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_role_registry) { 9 24 33
3629 9 g_role_registry = newHV();
3630 }
3631 33 hv_store(g_role_registry, role_name, len, newSViv(PTR2IV(meta)), 0);
3632 33 }
3633  
3634 /* Copy a method from role stash to class stash */
3635 17 static void copy_method(pTHX_ HV *from_stash, HV *to_stash, const char *method_name) {
3636 GV *from_gv;
3637 CV *cv;
3638 char full_name[512];
3639 GV *to_gv;
3640
3641 17 from_gv = gv_fetchmeth(from_stash, method_name, strlen(method_name), 0);
3642 17 ▶ 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 */ 14 3 17
if (!from_gv || !(cv = GvCV(from_gv))) { /* # uncoverable condition left count:2 */ 0 14 14
3643 4 return; /* No such method in role */
3644 }
3645
3646 /* Check if target already has this method */
3647 14 to_gv = gv_fetchmeth(to_stash, method_name, strlen(method_name), 0);
3648 14 ▶ 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 13 14
if (to_gv && GvCV(to_gv)) { /* # uncoverable condition right count:2 */ 1 0 1
3649 1 return; /* Target already has method, don't override */
3650 }
3651
3652 /* Install the CV in target stash */
3653 13 ▶ 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); 13 0 13
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 13 0 13
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 0 13 13
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); 13 0 13
snprintf(full_name, sizeof(full_name), "%s::%s", HvNAME(to_stash), method_name); 0 13 13
3654 13 to_gv = gv_fetchpv(full_name, GV_ADD, SVt_PVCV);
3655 /*
3656 # uncoverable branch false
3657 # uncoverable condition right count:1 */
3658 13 ▶ if (to_gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_gv) { 13 0 13
3659 /* Share the CV between role and class */
3660 13 GvCV_set(to_gv, (CV*)SvREFCNT_inc((SV*)cv));
3661 13 GvCVGEN(to_gv) = 0; /* Clear cache */
3662 }
3663 }
3664  
3665 /* Apply a role to a class */
3666 29 static void apply_role_to_class(pTHX_ ClassMeta *class_meta, RoleMeta *role_meta) {
3667 IV i;
3668 HE *entry;
3669
3670 /* Check required methods */
3671 36 ▶ for (i = 0; i < role_meta->required_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role_meta->required_count; i++) { 9 27 36
3672 9 const char *required = role_meta->required_methods[i];
3673 9 GV *gv = gv_fetchmeth(class_meta->stash, required, strlen(required), 0);
3674 9 ▶ if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */ 7 2 9
if (!gv || !GvCV(gv)) { /* # uncoverable condition left count:2 */ 0 7 7
3675 2 croak("Class '%s' does not implement required method '%s' from role '%s'",
3676 class_meta->class_name, required, role_meta->role_name);
3677 }
3678 }
3679
3680 /* Copy role's slots to class */
3681 65 ▶ for (i = 0; i < role_meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < role_meta->slot_count; i++) { 42 23 65
3682 42 SlotSpec *role_slot = role_meta->slots[i];
3683 SlotSpec *slot;
3684 IV new_idx;
3685 SV **existing;
3686
3687 /* Check for slot name conflict */
3688 42 existing = hv_fetch(class_meta->prop_to_idx, role_slot->name, strlen(role_slot->name), 0);
3689 42 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 41 42
3690 1 croak("Slot conflict: '%s' already exists in class '%s' (from role '%s')",
3691 role_slot->name, class_meta->class_name, role_meta->role_name);
3692 }
3693
3694 /* The class gets its own copy of the role's slot: it records the
3695 * class as the other package that may call a :private attribute,
3696 * and a +attr redefining the slot later frees this copy, not the
3697 * role's spec that other classes use too. Its handles are resolved
3698 * before the class grows, so that a croak leaves it as it was. */
3699 41 check_role_slot_against_delegators(aTHX_ class_meta, role_slot, role_meta);
3700 39 slot = clone_slot_spec(aTHX_ role_slot);
3701 39 slot->private_also = savepv(class_meta->class_name);
3702 39 resolve_handles(aTHX_ slot);
3703  
3704 /* Add slot to class */
3705 38 new_idx = class_meta->slot_count++;
3706 38 ▶ Renew(class_meta->slots, class_meta->slot_count, SlotSpec*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(class_meta->slots, class_meta->slot_count, SlotSpec*); 0 38 38
3707 38 ▶ 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 38 38
3708 38 register_slot(aTHX_ class_meta->class_name, class_meta, new_idx, slot);
3709 }
3710  
3711 /* handles(...) of the role's slots, now that their methods are in */
3712 23 install_class_delegators(aTHX_ class_meta);
3713
3714 /* Copy role's methods to class. xs_role made the role's stash with
3715 * GV_ADD, so it is always there. */
3716 22 hv_iterinit(role_meta->stash);
3717 41 ▶ while ((entry = hv_iternext(role_meta->stash))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(role_meta->stash))) { 19 22 41
3718 19 ▶ const char *name = HePV(entry, PL_na);
Branch: 1 branch
BranchTrueFalseTotal
const char *name = HePV(entry, PL_na); 0 19 19
3719 /* Skip special entries and slots (already handled) */
3720 19 ▶ if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) { 3 16 19
if (name[0] != '_' || strncmp(name, "_build_", 7) == 0) { 1 2 3
3721 17 copy_method(aTHX_ role_meta->stash, class_meta->stash, name);
3722 }
3723 }
3724
3725 /* Track consumed role */
3726 22 ▶ 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 22 22
3727 22 class_meta->consumed_roles[class_meta->role_count++] = role_meta;
3728 22 }
3729  
3730 /* ============================================
3731 Method Modifiers (zero overhead if not used)
3732 ============================================ */
3733  
3734 /* Get or create modified method entry */
3735 23 static ModifiedMethod* get_or_create_modified_method(pTHX_ ClassMeta *meta, const char *method_name) {
3736 SV **svp;
3737 ModifiedMethod *mod;
3738 23 STRLEN name_len = strlen(method_name);
3739
3740 23 ▶ if (!meta->modified_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta->modified_methods) { 10 13 23
3741 10 meta->modified_methods = newHV();
3742 }
3743
3744 23 svp = hv_fetch(meta->modified_methods, method_name, name_len, 0);
3745 23 ▶ if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 12 11 23
if (svp && SvIOK(*svp)) { /* # uncoverable condition right count:2 */ 12 0 12
3746 12 return INT2PTR(ModifiedMethod*, SvIV(*svp));
3747 }
3748
3749 /* Create new modified method entry */
3750 11 Newxz(mod, 1, ModifiedMethod);
3751
3752 /* Get the original CV */
3753 {
3754 11 GV *gv = gv_fetchmeth(meta->stash, method_name, name_len, 0);
3755 11 ▶ if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 9 2 11
if (gv && GvCV(gv)) { /* # uncoverable condition right count:2 */ 9 0 9
3756 9 mod->original_cv = GvCV(gv);
3757 9 SvREFCNT_inc((SV*)mod->original_cv);
3758 }
3759 }
3760
3761 11 hv_store(meta->modified_methods, method_name, name_len, newSViv(PTR2IV(mod)), 0);
3762 11 return mod;
3763 }
3764  
3765 /* XS wrapper for modified methods */
3766 13 XS_INTERNAL(xs_modified_method_wrapper) {
3767 13 dXSARGS;
3768 13 ModifiedMethod *mod = INT2PTR(ModifiedMethod*, CvXSUBANY(cv).any_iv);
3769 MethodModifier *m;
3770 13 int count = 0;
3771 13 I32 gimme = GIMME_V;
3772 AV *saved_args;
3773 AV *saved_results;
3774 int i;
3775
3776 /* Save original arguments for before/after chains */
3777 13 saved_args = newAV();
3778 13 sv_2mortal((SV*)saved_args);
3779 32 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 19 13 32
3780 19 av_push(saved_args, SvREFCNT_inc(ST(i)));
3781 }
3782
3783 /* Call before chain (in stack order - most recent first) */
3784 25 ▶ for (m = mod->before_chain; m; m = m->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = mod->before_chain; m; m = m->next) { 12 13 25
3785 12 dSP;
3786 12 ENTER;
3787 12 SAVETMPS;
3788 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
3789 30 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 18 12 30
3790 18 SV **svp = av_fetch(saved_args, i, 0);
3791 /*
3792 # uncoverable condition left count:1
3793 # uncoverable condition right count:2 */
3794 18 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 18 18
XPUSHs(svp ? *svp : &PL_sv_undef); 18 0 18
3795 }
3796 12 PUTBACK;
3797 12 call_sv(m->callback, G_DISCARD);
3798 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 12 12
3799 12 LEAVE;
3800 }
3801
3802 /* Save results from original/around call */
3803 13 saved_results = newAV();
3804 13 sv_2mortal((SV*)saved_results);
3805
3806 /* Call around chain (or original if no around) */
3807 13 ▶ if (mod->around_chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (mod->around_chain) { 6 7 13
3808 /* For around, we pass ($orig, $self, @args) */
3809 6 m = mod->around_chain;
3810 {
3811 6 dSP;
3812 6 ENTER;
3813 6 SAVETMPS;
3814 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
3815 6 ▶ XPUSHs(sv_2mortal(newRV_inc((SV*)mod->original_cv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV*)mod->original_cv))); 0 6 6
3816 16 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 10 6 16
3817 10 SV **svp = av_fetch(saved_args, i, 0);
3818 /*
3819 # uncoverable condition left count:1
3820 # uncoverable condition right count:2 */
3821 10 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 10 10
XPUSHs(svp ? *svp : &PL_sv_undef); 10 0 10
3822 }
3823 6 PUTBACK;
3824 6 ▶ 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 5 6
3825 6 SPAGAIN;
3826 /* Save results before LEAVE destroys them - they're on stack in reverse */
3827 14 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 8 6 14
3828 8 av_push(saved_results, newSVsv(POPs));
3829 }
3830 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
3831 6 LEAVE;
3832 }
3833 7 ▶ } else if (mod->original_cv) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mod->original_cv) { 6 1 7
3834 /* Call original method */
3835 6 dSP;
3836 6 ENTER;
3837 6 SAVETMPS;
3838 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
3839 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
3840 8 SV **svp = av_fetch(saved_args, i, 0);
3841 /*
3842 # uncoverable condition left count:1
3843 # uncoverable condition right count:2 */
3844 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
3845 }
3846 6 PUTBACK;
3847 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
3848 6 SPAGAIN;
3849 /* Save results before LEAVE destroys them */
3850 13 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 7 6 13
3851 7 av_push(saved_results, newSVsv(POPs));
3852 }
3853 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
3854 6 LEAVE;
3855 }
3856
3857 /* Call after chain (in order of registration) */
3858 24 ▶ for (m = mod->after_chain; m; m = m->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = mod->after_chain; m; m = m->next) { 11 13 24
3859 11 dSP;
3860 11 ENTER;
3861 11 SAVETMPS;
3862 11 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 11 11
3863 26 ▶ for (i = 0; i <= av_len(saved_args); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(saved_args); i++) { 15 11 26
3864 15 SV **svp = av_fetch(saved_args, i, 0);
3865 /*
3866 # uncoverable condition left count:1
3867 # uncoverable condition right count:2 */
3868 15 ▶ XPUSHs(svp ? *svp : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(svp ? *svp : &PL_sv_undef); 0 15 15
XPUSHs(svp ? *svp : &PL_sv_undef); 15 0 15
3869 }
3870 11 PUTBACK;
3871 11 call_sv(m->callback, G_DISCARD);
3872 11 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 11 11
3873 11 LEAVE;
3874 }
3875
3876 /* Put saved results back on stack (they were saved in reverse order) */
3877 {
3878 13 count = av_len(saved_results) + 1;
3879 28 ▶ for (i = count - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) { 15 13 28
3880 15 SV **svp = av_fetch(saved_results, i, 0);
3881 /* Use sv_mortalcopy to put a mortal copy on stack */
3882 15 ▶ 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 */ 15 0 15
3883 }
3884 }
3885
3886 13 XSRETURN(count);
3887 }
3888  
3889 /* Install the wrapper if not already done */
3890 23 static void install_modifier_wrapper(pTHX_ ClassMeta *meta, const char *method_name, ModifiedMethod *mod) {
3891 char full_name[256];
3892 CV *existing_cv;
3893
3894 23 snprintf(full_name, sizeof(full_name), "%s::%s", meta->class_name, method_name);
3895
3896 23 existing_cv = get_cvn_flags(full_name, strlen(full_name), 0);
3897
3898 /* Only install wrapper once - check if it's already our wrapper */
3899 23 ▶ if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) { 20 3 23
if (existing_cv && CvXSUB(existing_cv) == xs_modified_method_wrapper) { 11 9 20
3900 11 return; /* Already wrapped */
3901 }
3902
3903 /* Install wrapper without "Subroutine redefined" warning */
3904 {
3905 12 GV *gv = gv_fetchpv(full_name, GV_ADD, SVt_PVCV);
3906 12 CV *cv = newXS_flags(NULL, xs_modified_method_wrapper, __FILE__, NULL, 0);
3907 12 CvXSUBANY(cv).any_iv = PTR2IV(mod);
3908 /* Silently replace the CV in the GV */
3909 12 ▶ if (GvCV(gv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvCV(gv)) { 10 2 12
3910 10 SvREFCNT_dec(GvCV(gv));
3911 }
3912 12 GvCV_set(gv, cv);
3913 }
3914 }
3915  
3916 /* Add a modifier to a method */
3917 23 static void add_modifier(pTHX_ ClassMeta *meta, const char *method_name, SV *callback, int type) {
3918 ModifiedMethod *mod;
3919 MethodModifier *new_mod;
3920
3921 23 mod = get_or_create_modified_method(aTHX_ meta, method_name);
3922
3923 23 Newx(new_mod, 1, MethodModifier);
3924 23 new_mod->callback = newSVsv(callback);
3925 23 new_mod->next = NULL;
3926
3927 /* Add to appropriate chain */
3928 23 switch (type) { /* # uncoverable branch true count:4 */
3929 8 case 0: /* before */
3930 8 new_mod->next = mod->before_chain;
3931 8 mod->before_chain = new_mod;
3932 8 break;
3933 9 case 1: /* after */
3934 /* Add to end of after chain */
3935 9 ▶ if (!mod->after_chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mod->after_chain) { 6 3 9
3936 6 mod->after_chain = new_mod;
3937 } else {
3938 3 MethodModifier *last = mod->after_chain;
3939 4 ▶ while (last->next) last = last->next;
Branch: 1 branch
BranchTrueFalseTotal
while (last->next) last = last->next; 1 3 4
3940 3 last->next = new_mod;
3941 }
3942 9 break;
3943 6 case 2: /* around */
3944 /* around wraps previous around/original */
3945 6 new_mod->next = mod->around_chain;
3946 6 mod->around_chain = new_mod;
3947 6 break;
3948 }
3949
3950 23 install_modifier_wrapper(aTHX_ meta, method_name, mod);
3951 23 }
3952  
3953 /* ============================================
3954 handles(...) - the delegating methods
3955 ============================================ */
3956  
3957 /* How a method is called on the delegate: from perl 5.19.2 as $obj->name is,
3958 * through the shared name's cached hash */
3959 #ifdef G_METHOD_NAMED
3960 # define G_DELEGATE_METHOD G_METHOD_NAMED
3961 #else
3962 # define G_DELEGATE_METHOD G_METHOD
3963 #endif
3964  
3965 /* What a delegating method knows: its class, the slot it delegates to and
3966 * the resolved handle. Kept in CvXSUBANY, like the other generated methods. */
3967 typedef struct {
3968 ClassMeta *meta;
3969 IV slot_idx;
3970 SlotSpec *spec;
3971 ResolvedHandle *h;
3972 } DelegateData;
3973  
3974 /* The invocant of a delegating method, which must be an object */
3975 448 OBJECT_INLINE AV *delegator_self(pTHX_ I32 items, SV *self) {
3976 448 ▶ if (items < 1 || !SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) croak("Not an object");
Branch: 3 branches
BranchTrueFalseTotal
if (items < 1 || !SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) croak("Not an object"); 443 5 448
if (items < 1 || !SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) croak("Not an object"); 442 1 443
if (items < 1 || !SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) croak("Not an object"); 7 435 442
3977 435 return (AV*)SvRV(self);
3978 }
3979  
3980 /* The delegate: the slot's value, found the way the slot's getter finds it -
3981 * the object's own slot, then its prototype chain, then a lazy build.
3982 * Croaks when there is none (D3). av is the object's array. */
3983 406 OBJECT_INLINE SV *delegate_target(pTHX_ SV *self, AV *av, DelegateData *d) {
3984 406 IV idx = d->slot_idx;
3985 SV *target;
3986  
3987 /* The common case: the object's own slot holds it */
3988 406 ▶ if (idx <= AvFILLp(av)) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= AvFILLp(av)) { 405 1 406
3989 405 target = AvARRAY(av)[idx];
3990 405 ▶ if (target && SvOK(target)) return target;
Branch: 2 branches
BranchTrueFalseTotal
if (target && SvOK(target)) return target; 404 1 405
if (target && SvOK(target)) return target; 353 51 404
3991 }
3992 53 target = resolve_property_chain(aTHX_ av, idx);
3993 53 ▶ if (!SvOK(target) && d->spec->is_lazy) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(target) && d->spec->is_lazy) { 24 29 53
if (!SvOK(target) && d->spec->is_lazy) { 8 16 24
3994 8 SV *built = slot_build_lazy(aTHX_ self, av, idx, d->spec);
3995 8 ▶ if (built) target = built;
Branch: 1 branch
BranchTrueFalseTotal
if (built) target = built; 7 1 8
3996 }
3997 53 ▶ if (!SvOK(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(target)) { 20 33 53
3998 20 croak("Cannot delegate %" SVf " to %s: %s is not set",
3999 SVfARG(d->h->local), d->spec->name, d->spec->name);
4000 }
4001 33 return target;
4002 }
4003  
4004 /* $self->local(@args) is $self->slot->remote(@args). The call reuses this
4005 * XSUB's own frame: the delegate replaces $self as ST(0), the arguments stay
4006 * where they are, the caller's context passes through, and the results are
4007 * left where ours go. Nothing is copied. */
4008 97 XS_INTERNAL(xs_delegate_method) {
4009 97 dXSARGS;
4010 97 DelegateData *d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4011 97 AV *av = delegator_self(aTHX_ items, ST(0));
4012 I32 count;
4013  
4014 94 ST(0) = delegate_target(aTHX_ ST(0), av, d);
4015 /* From PL_stack_base, not a saved pointer: a lazy builder may have grown the stack */
4016 85 ▶ PUSHMARK(PL_stack_base + ax - 1);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(PL_stack_base + ax - 1); 0 85 85
4017 85 count = call_sv(d->h->remote, GIMME_V | G_DELEGATE_METHOD);
4018 80 XSRETURN(count);
4019 }
4020  
4021 /* The delegate of a CodeRef, ArrayRef or HashRef slot, found as a method
4022 * delegate is, and a reference to type, as the slot's type makes it. A value
4023 * that is not - from a prototype of another class, or written into the
4024 * object's array directly - croaks: call_sv would call a string as the name
4025 * of a sub, and the native ops would take it for a container. what is "a
4026 * CODE", "an ARRAY" or "a HASH". */
4027 312 OBJECT_INLINE SV *delegate_ref(pTHX_ SV *self, AV *av, DelegateData *d, svtype type, const char *what) {
4028 312 SV *target = delegate_target(aTHX_ self, av, d);
4029 301 ▶ if (!SvROK(target) || SvTYPE(SvRV(target)) != type) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(target) || SvTYPE(SvRV(target)) != type) { 297 4 301
if (!SvROK(target) || SvTYPE(SvRV(target)) != type) { 3 294 297
4030 7 croak("Cannot delegate %" SVf " to %s: %s is not %s reference",
4031 SVfARG(d->h->local), d->spec->name, d->spec->name, what);
4032 }
4033 294 return target;
4034 }
4035  
4036 /* $self->local(@args) is $self->slot->(@args). $self is left out by marking
4037 * the stack after it, so the arguments are passed where they are; the
4038 * results land one place up, and move down to where ours go. */
4039 51 XS_INTERNAL(xs_delegate_code_call) {
4040 51 dXSARGS;
4041 51 DelegateData *d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4042 51 AV *av = delegator_self(aTHX_ items, ST(0));
4043 49 SV *code = delegate_ref(aTHX_ ST(0), av, d, SVt_PVCV, "a CODE");
4044 I32 count, i;
4045  
4046 44 ▶ PUSHMARK(PL_stack_base + ax);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(PL_stack_base + ax); 0 44 44
4047 44 count = call_sv(code, GIMME_V);
4048 /* ST() is from PL_stack_base, so this holds if the call grew the stack */
4049 1080 ▶ for (i = 0; i < count; i++) ST(i) = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) ST(i) = ST(i + 1); 1038 42 1080
4050 42 XSRETURN(count);
4051 }
4052  
4053 /* $self->local(@args) is $self->slot->($self, @args): this XSUB's own
4054 * arguments, $self first, are passed as they are */
4055 17 XS_INTERNAL(xs_delegate_code_method) {
4056 17 dXSARGS;
4057 17 DelegateData *d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4058 17 AV *av = delegator_self(aTHX_ items, ST(0));
4059 15 SV *code = delegate_ref(aTHX_ ST(0), av, d, SVt_PVCV, "a CODE");
4060 I32 count;
4061  
4062 13 ▶ PUSHMARK(PL_stack_base + ax - 1);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(PL_stack_base + ax - 1); 0 13 13
4063 13 count = call_sv(code, GIMME_V);
4064 13 XSRETURN(count);
4065 }
4066  
4067 /* What a native delegator checks before it runs anything: the number of
4068 * arguments, and, for an op that changes the container, that the object is
4069 * not frozen (D4, D12). So a call that croaks has done nothing, not even a
4070 * lazy build. */
4071 277 OBJECT_INLINE void native_precheck(pTHX_ DelegateData *d, const NativeOp *op, I32 nargs, SV *self, AV *self_av) {
4072 277 ▶ if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) {
Branch: 5 branches
BranchTrueFalseTotal
if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) { 268 9 277
if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) { 174 94 268
if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) { 165 9 174
if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) { 29 230 259
if (nargs < op->min_args || (op->max_args >= 0 && nargs > op->max_args) || (op->pairs && nargs % 2)) { 1 28 29
4073 19 ▶ if (op->pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->pairs) { 3 16 19
4074 3 croak("Wrong number of arguments to %" SVf " (%s takes key/value pairs)",
4075 SVfARG(d->h->local), op->name);
4076 }
4077 16 ▶ if (op->max_args < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->max_args < 0) { 4 12 16
4078 4 croak("Wrong number of arguments to %" SVf " (%s takes at least %d)",
4079 SVfARG(d->h->local), op->name, (int)op->min_args);
4080 }
4081 12 croak("Wrong number of arguments to %" SVf " (%s takes %d)",
4082 SVfARG(d->h->local), op->name, (int)op->min_args);
4083 }
4084 258 ▶ if (op->mutates && SvMAGICAL(self_av)) {
Branch: 2 branches
BranchTrueFalseTotal
if (op->mutates && SvMAGICAL(self_av)) { 118 140 258
if (op->mutates && SvMAGICAL(self_av)) { 12 106 118
4085 12 MAGIC *mg = get_object_magic(aTHX_ self);
4086 12 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) croak("Cannot modify frozen object");
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) croak("Cannot modify frozen object"); 11 1 12
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) croak("Cannot modify frozen object"); 10 1 11
4087 }
4088 248 }
4089  
4090 /* After an op that changed the container: the slot's trigger, called as the
4091 * setter calls it, ($self, $new_value), with the container as the new value,
4092 * once per call (D6). The results are already in place below the stack's
4093 * top, where the call leaves them; a removed value is mortal and outlives
4094 * it, since G_DISCARD frees only the call's own temporaries. */
4095 100 static void native_trigger(pTHX_ SV *self, SV *container, SlotSpec *spec) {
4096 100 dSP;
4097 100 ▶ if (!spec->has_trigger || !spec->trigger_cb) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!spec->has_trigger || !spec->trigger_cb) return; 92 8 100
if (!spec->has_trigger || !spec->trigger_cb) return; 1 91 92
4098 91 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 91 91
4099 91 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 91 91
4100 91 ▶ XPUSHs(container);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(container); 0 91 91
4101 91 PUTBACK;
4102 182 call_method(SvPV_nolen(spec->trigger_cb), G_DISCARD);
4103 }
4104  
4105 /* $av->[key] = a copy of val, through the element as perl's own assignment
4106 * does, so that a tied array's STORE runs and nothing is left to free if it
4107 * croaks. NULL when key is before the start of the array. */
4108 1047 static SV **native_av_assign(pTHX_ AV *av, SSize_t key, SV *val) {
4109 1047 SV **svp = av_fetch(av, key, 1);
4110 1047 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 1045 2 1047
4111 1045 sv_setsv(*svp, val);
4112 1045 ▶ SvSETMAGIC(*svp);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(*svp); 5 1040 1045
4113 }
4114 1047 return svp;
4115 }
4116  
4117 /* The number of keys of a hash: kept for a plain hash, counted through the
4118 * tie for a tied one */
4119 25 static IV native_hv_count(pTHX_ HV *hv) {
4120 25 IV n = 0;
4121 /* The macros below on lines of their own: their own tests are perl's */
4122 25 ▶ if (!SvRMAGICAL(hv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvRMAGICAL(hv)) 22 3 25
4123 22 ▶ return HvUSEDKEYS(hv);
Branch: 1 branch
BranchTrueFalseTotal
return HvUSEDKEYS(hv); 0 22 22
4124 3 hv_iterinit(hv);
4125 9 ▶ while (hv_iternext(hv)) n++;
Branch: 1 branch
BranchTrueFalseTotal
while (hv_iternext(hv)) n++; 6 3 9
4126 3 return n;
4127 }
4128  
4129 /* A native ArrayRef method (phase 1 section 3): the array's own op, its
4130 * result as the builtin gives it, the trigger after a change */
4131 142 XS_INTERNAL(xs_delegate_array) {
4132 142 dXSARGS;
4133 142 ▶ dXSTARG; /* the caller's target, for a number without an allocation */
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; /* the caller's target, for a number without an allocation */ 142 0 142
4134 142 DelegateData *d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4135 142 const NativeOp *op = &array_ops[d->h->op];
4136 142 SV *self = ST(0);
4137 142 AV *self_av = delegator_self(aTHX_ items, self);
4138 SV *target;
4139 AV *av;
4140 138 I32 ret = 1;
4141 I32 i;
4142  
4143 138 native_precheck(aTHX_ d, op, items - 1, self, self_av);
4144 122 target = delegate_ref(aTHX_ self, self_av, d, SVt_PVAV, "an ARRAY");
4145 116 av = (AV*)SvRV(target);
4146 116 ▶ if (op->mutates && SvREADONLY(av)) croak_no_modify();
Branch: 2 branches
BranchTrueFalseTotal
if (op->mutates && SvREADONLY(av)) croak_no_modify(); 61 55 116
if (op->mutates && SvREADONLY(av)) croak_no_modify(); 1 60 61
4147  
4148 /* The range check before the jump table (arm 11) cannot fail: the op
4149 * is an index into array_ops, set when the handle was resolved */
4150 115 switch (d->h->op) { /* # uncoverable branch true count:11 */
4151 21 case AOP_PUSH: {
4152 21 SSize_t at = av_len(av) + 1;
4153 1050 ▶ for (i = 1; i < items; i++) native_av_assign(aTHX_ av, at++, ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) native_av_assign(aTHX_ av, at++, ST(i)); 1029 21 1050
4154 21 sv_setiv(TARG, av_len(av) + 1);
4155 21 ST(0) = TARG;
4156 21 break;
4157 }
4158 6 case AOP_UNSHIFT:
4159 6 av_unshift(av, items - 1);
4160 14 ▶ for (i = 1; i < items; i++) native_av_assign(aTHX_ av, i - 1, ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) native_av_assign(aTHX_ av, i - 1, ST(i)); 8 6 14
4161 6 sv_setiv(TARG, av_len(av) + 1);
4162 6 ST(0) = TARG;
4163 6 break;
4164 8 case AOP_POP:
4165 8 ST(0) = sv_2mortal(av_pop(av)); /* &PL_sv_undef when empty, left as it is */
4166 8 break;
4167 7 case AOP_SHIFT:
4168 7 ST(0) = sv_2mortal(av_shift(av));
4169 7 break;
4170 16 case AOP_LIST:
4171 /* The array's own elements, as @$ref gives them; its size in scalar context */
4172 16 ▶ if (GIMME_V == G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) { 12 4 16
4173 12 SV **sp_base = PL_stack_base + ax - 1;
4174 12 ret = av_len(av) + 1;
4175 12 ▶ EXTEND(sp_base, ret);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(sp_base, ret); 12 0 12
EXTEND(sp_base, ret); 0 12 12
4176 2030 ▶ for (i = 0; i < ret; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ret; i++) { 2018 12 2030
4177 2018 SV **svp = av_fetch(av, i, 0);
4178 2018 ▶ ST(i) = svp ? *svp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(i) = svp ? *svp : &PL_sv_undef; 2015 3 2018
4179 }
4180 12 break;
4181 }
4182 /* In scalar context, the count */
4183 /* FALLTHROUGH */
4184 case AOP_COUNT:
4185 22 sv_setiv(TARG, av_len(av) + 1);
4186 22 ST(0) = TARG;
4187 22 break;
4188 8 case AOP_IS_EMPTY:
4189 8 ▶ ST(0) = av_len(av) < 0 ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = av_len(av) < 0 ? &PL_sv_yes : &PL_sv_no; 3 5 8
4190 8 break;
4191 13 case AOP_GET: {
4192 13 SV **svp = av_fetch(av, SvIV(ST(1)), 0);
4193 13 ▶ ST(0) = svp ? *svp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = svp ? *svp : &PL_sv_undef; 9 4 13
4194 13 break;
4195 }
4196 10 case AOP_SET: {
4197 10 IV key = SvIV(ST(1));
4198 10 SV **svp = native_av_assign(aTHX_ av, key, ST(2));
4199 10 ▶ if (!svp) croak("Modification of non-creatable array value attempted, subscript %" IVdf, key);
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("Modification of non-creatable array value attempted, subscript %" IVdf, key); 2 8 10
4200 8 ST(0) = *svp;
4201 8 break;
4202 }
4203 8 case AOP_CLEAR:
4204 default:
4205 /* @a = () gives the empty list, or 0 in scalar context */
4206 8 av_clear(av);
4207 8 ▶ if (GIMME_V == G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) { 2 6 8
4208 2 ret = 0;
4209 } else {
4210 6 sv_setiv(TARG, 0);
4211 6 ST(0) = TARG;
4212 }
4213 8 break;
4214 }
4215  
4216 113 ▶ if (op->mutates) native_trigger(aTHX_ self, target, d->spec);
Branch: 1 branch
BranchTrueFalseTotal
if (op->mutates) native_trigger(aTHX_ self, target, d->spec); 58 55 113
4217 112 XSRETURN(ret);
4218 }
4219  
4220 /* A native HashRef method (phase 1 section 4) */
4221 141 XS_INTERNAL(xs_delegate_hash) {
4222 141 dXSARGS;
4223 141 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 141 0 141
4224 141 DelegateData *d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4225 141 const NativeOp *op = &hash_ops[d->h->op];
4226 141 SV *self = ST(0);
4227 141 AV *self_av = delegator_self(aTHX_ items, self);
4228 139 I32 nargs = items - 1;
4229 SV *target;
4230 HV *hv;
4231 139 I32 ret = 1;
4232 I32 i;
4233  
4234 139 native_precheck(aTHX_ d, op, nargs, self, self_av);
4235 126 target = delegate_ref(aTHX_ self, self_av, d, SVt_PVHV, "a HASH");
4236 121 hv = (HV*)SvRV(target);
4237  
4238 /* The range check before the jump table (arm 9) cannot fail: the op is
4239 * an index into hash_ops, set when the handle was resolved */
4240 121 switch (d->h->op) { /* # uncoverable branch true count:9 */
4241 30 case HOP_GET:
4242 case HOP_DELETE:
4243 /* Each key's value, in its place: a slice gives all of them, and
4244 * its last one in scalar context */
4245 69 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 39 30 69
4246 SV *value;
4247 39 ▶ if (d->h->op == HOP_GET) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->h->op == HOP_GET) { 23 16 39
4248 23 HE *he = hv_fetch_ent(hv, ST(i), 0, 0);
4249 23 ▶ value = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
value = he ? HeVAL(he) : NULL; 19 4 23
4250 } else {
4251 16 value = hv_delete_ent(hv, ST(i), 0, 0);
4252 }
4253 39 ▶ ST(i - 1) = value ? value : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(i - 1) = value ? value : &PL_sv_undef; 31 8 39
4254 }
4255 /* One key gives its value in any context */
4256 30 ▶ if (nargs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nargs > 1) { 5 25 30
4257 5 ▶ if (GIMME_V == G_LIST) ret = nargs;
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) ret = nargs; 3 2 5
4258 2 else ST(0) = ST(nargs - 1);
4259 }
4260 30 break;
4261 25 case HOP_SET: {
4262 /* Through each element, as $h{k} = v does: a tied hash's STORE
4263 * runs, a restricted hash croaks before anything is made */
4264 25 I32 values = nargs / 2;
4265 1055 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 1031 24 1055
4266 1031 HE *he = hv_fetch_ent(hv, ST(i), 1, 0);
4267 1030 sv_setsv(HeVAL(he), ST(i + 1));
4268 1030 ▶ SvSETMAGIC(HeVAL(he));
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(HeVAL(he)); 3 1027 1030
4269 1030 ST((i - 1) / 2) = HeVAL(he);
4270 }
4271 /* One pair gives its value; a slice assignment gives its values,
4272 * or their number in scalar context */
4273 24 ▶ if (values > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (values > 1) { 8 16 24
4274 8 ▶ if (GIMME_V == G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) { 2 6 8
4275 2 ret = values;
4276 } else {
4277 6 sv_setiv(TARG, values);
4278 6 ST(0) = TARG;
4279 }
4280 }
4281 24 break;
4282 }
4283 8 case HOP_EXISTS:
4284 8 ▶ ST(0) = hv_exists_ent(hv, ST(1), 0) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = hv_exists_ent(hv, ST(1), 0) ? &PL_sv_yes : &PL_sv_no; 5 3 8
4285 8 break;
4286 9 case HOP_DEFINED: {
4287 9 HE *he = hv_fetch_ent(hv, ST(1), 0, 0);
4288 9 ▶ SV *value = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *value = he ? HeVAL(he) : NULL; 7 2 9
4289 9 ▶ if (value)
Branch: 1 branch
BranchTrueFalseTotal
if (value) 7 2 9
4290 7 ▶ SvGETMAGIC(value); /* a tied hash's FETCH */
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(value); /* a tied hash's FETCH */ 2 5 7
SvGETMAGIC(value); /* a tied hash's FETCH */ 0 2 2
4291 9 ▶ ST(0) = value && SvOK(value) ? &PL_sv_yes : &PL_sv_no;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = value && SvOK(value) ? &PL_sv_yes : &PL_sv_no; 7 2 9
ST(0) = value && SvOK(value) ? &PL_sv_yes : &PL_sv_no; 4 3 7
4292 9 break;
4293 }
4294 26 case HOP_KEYS:
4295 case HOP_VALUES:
4296 case HOP_KV: {
4297 HE *he;
4298 /* Like keys, in any context, this starts the hash's iterator afresh */
4299 26 hv_iterinit(hv);
4300 26 ▶ if (GIMME_V != G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V != G_LIST) { 9 17 26
4301 9 sv_setiv(TARG, native_hv_count(aTHX_ hv));
4302 9 ST(0) = TARG;
4303 9 break;
4304 }
4305 17 SP = PL_stack_base + ax - 1;
4306 2053 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 2036 17 2053
4307 SV *out;
4308 2036 ▶ if (d->h->op == HOP_KEYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->h->op == HOP_KEYS) { 1013 1023 2036
4309 1013 out = hv_iterkeysv(he);
4310 1023 ▶ } else if (d->h->op == HOP_VALUES) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (d->h->op == HOP_VALUES) { 9 1014 1023
4311 9 out = hv_iterval(hv, he);
4312 } else {
4313 1014 AV *pair = newAV();
4314 1014 av_push(pair, newSVsv(hv_iterkeysv(he)));
4315 1014 av_push(pair, newSVsv(hv_iterval(hv, he)));
4316 1014 out = sv_2mortal(newRV_noinc((SV*)pair));
4317 }
4318 2036 ▶ XPUSHs(out);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(out); 0 2036 2036
4319 }
4320 17 PUTBACK;
4321 17 return;
4322 }
4323 9 case HOP_COUNT:
4324 9 sv_setiv(TARG, native_hv_count(aTHX_ hv));
4325 9 ST(0) = TARG;
4326 9 break;
4327 7 case HOP_IS_EMPTY:
4328 7 ▶ ST(0) = native_hv_count(aTHX_ hv) ? &PL_sv_no : &PL_sv_yes;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = native_hv_count(aTHX_ hv) ? &PL_sv_no : &PL_sv_yes; 4 3 7
4329 7 break;
4330 7 case HOP_CLEAR:
4331 default:
4332 7 hv_clear(hv);
4333 7 ▶ if (GIMME_V == G_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) { 2 5 7
4334 2 ret = 0;
4335 } else {
4336 5 sv_setiv(TARG, 0);
4337 5 ST(0) = TARG;
4338 }
4339 7 break;
4340 }
4341  
4342 103 ▶ if (op->mutates) native_trigger(aTHX_ self, target, d->spec);
Branch: 1 branch
BranchTrueFalseTotal
if (op->mutates) native_trigger(aTHX_ self, target, d->spec); 42 61 103
4343 103 XSRETURN(ret);
4344 }
4345  
4346 /* The XSUB a handle's kind installs */
4347 320 static XSUBADDR_t delegator_xsub(const ResolvedHandle *h) {
4348 320 switch (h->kind) {
4349 28 case HK_CODE:
4350 28 ▶ return h->op == HANDLE_CODE_METHOD ? xs_delegate_code_method : xs_delegate_code_call;
Branch: 1 branch
BranchTrueFalseTotal
return h->op == HANDLE_CODE_METHOD ? xs_delegate_code_method : xs_delegate_code_call; 9 19 28
4351 84 case HK_ARRAY:
4352 84 return xs_delegate_array;
4353 57 case HK_HASH:
4354 57 return xs_delegate_hash;
4355 151 default: /* HK_METHOD */
4356 151 return xs_delegate_method;
4357 }
4358 }
4359  
4360 /* Whether x is the XSUB of a delegating method */
4361 42 static bool xsub_is_delegator(XSUBADDR_t x) {
4362 24 ▶ return x == xs_delegate_method || x == xs_delegate_code_call || x == xs_delegate_code_method
Branch: 2 branches
BranchTrueFalseTotal
return x == xs_delegate_method || x == xs_delegate_code_call || x == xs_delegate_code_method 20 4 24
return x == xs_delegate_method || x == xs_delegate_code_call || x == xs_delegate_code_method 18 2 20
4363 66 ▶ || x == xs_delegate_array || x == xs_delegate_hash;
Branch: 3 branches
BranchTrueFalseTotal
|| x == xs_delegate_array || x == xs_delegate_hash; 24 18 42
|| x == xs_delegate_array || x == xs_delegate_hash; 16 2 18
|| x == xs_delegate_array || x == xs_delegate_hash; 2 14 16
4364 }
4365  
4366 /* The delegating method cv is, or wraps in a before/after/around modifier;
4367 * NULL when it is not one */
4368 49 static CV *delegator_cv(pTHX_ CV *cv) {
4369 49 ▶ if (CvISXSUB(cv) && CvXSUB(cv) == xs_modified_method_wrapper) {
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) && CvXSUB(cv) == xs_modified_method_wrapper) { 44 5 49
if (CvISXSUB(cv) && CvXSUB(cv) == xs_modified_method_wrapper) { 4 40 44
4370 4 ModifiedMethod *mod = INT2PTR(ModifiedMethod*, CvXSUBANY(cv).any_iv);
4371 4 cv = mod->original_cv;
4372 4 ▶ if (!cv) return NULL; /* a modifier on a method there was not */
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) return NULL; /* a modifier on a method there was not */ 1 3 4
4373 }
4374 48 ▶ return CvISXSUB(cv) && xsub_is_delegator(CvXSUB(cv)) ? cv : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return CvISXSUB(cv) && xsub_is_delegator(CvXSUB(cv)) ? cv : NULL; 42 6 48
return CvISXSUB(cv) && xsub_is_delegator(CvXSUB(cv)) ? cv : NULL; 28 14 42
4375 }
4376  
4377 /* The class's own sub called name (not one it inherits), or NULL */
4378 727 static CV *class_method_cv(pTHX_ ClassMeta *meta, SV *name) {
4379 727 SV *fqn = sv_2mortal(newSVpvf("%s::%" SVf, meta->class_name, SVfARG(name)));
4380 727 GV *gv = gv_fetchsv(fqn, 0, SVt_PVCV);
4381 727 ▶ return gv ? GvCV(gv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return gv ? GvCV(gv) : NULL; 59 668 727
4382 }
4383  
4384 /* Install one delegating method, or give the one an earlier define of the
4385 * class installed its new slot and handle */
4386 320 static void install_delegator(pTHX_ ClassMeta *meta, IV idx, SlotSpec *spec, ResolvedHandle *h) {
4387 320 CV *cv = class_method_cv(aTHX_ meta, h->local);
4388 DelegateData *d;
4389  
4390 320 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 13 307 320
4391 /* Ours, perhaps inside a modifier's wrapper: the caller has checked */
4392 13 cv = delegator_cv(aTHX_ cv);
4393 13 d = INT2PTR(DelegateData*, CvXSUBANY(cv).any_iv);
4394 13 CvXSUB(cv) = delegator_xsub(h);
4395 } else {
4396 307 SV *fqn = sv_2mortal(newSVpvf("%s::%" SVf, meta->class_name, SVfARG(h->local)));
4397 307 Newx(d, 1, DelegateData);
4398 614 cv = newXS_flags(SvPV_nolen(fqn), delegator_xsub(h), __FILE__, NULL,
4399 SvUTF8(fqn) ? SVf_UTF8 : 0);
4400 307 CvXSUBANY(cv).any_iv = PTR2IV(d);
4401 }
4402 320 d->meta = meta;
4403 320 d->slot_idx = idx;
4404 320 d->spec = spec;
4405 320 d->h = h;
4406 320 }
4407  
4408 /* Install the delegating methods of every slot of a class with handles (D7).
4409 * Run by define once all its slots are in, and by with() once a role's are,
4410 * so that no accessor is still to come whose name a delegator could take.
4411 * Each is checked before any is installed, against the methods the slots
4412 * give the class, the other delegators and the class's own subs:
4413 * - a slot the class declares (its own, a +attr, a role's) croaks on a
4414 * reserved name or a name already taken;
4415 * - a slot it only inherits gives way: a method of the class's own, or of an
4416 * earlier parent's slot, overrides the inherited delegator, as a child's
4417 * method overrides a parent's.
4418 * A delegator an earlier define of the class installed is given its new slot
4419 * rather than replaced, also when a method modifier wraps it. */
4420 548 static void install_class_delegators(pTHX_ ClassMeta *meta) {
4421 HV *taken; /* method name -> the slot that provides it */
4422 AV *names;
4423 AV *plan; /* slot index, handle index pairs to install */
4424 IV idx, i;
4425 548 bool any = false;
4426  
4427 1782 ▶ for (idx = 1; idx < meta->slot_count; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 1; idx < meta->slot_count; idx++) { 1234 548 1782
4428 /* A slot a redefinition of the class left out has no spec */
4429 1234 ▶ if (meta->slots[idx] && meta->slots[idx]->has_handles) any = true;
Branch: 2 branches
BranchTrueFalseTotal
if (meta->slots[idx] && meta->slots[idx]->has_handles) any = true; 1229 5 1234
if (meta->slots[idx] && meta->slots[idx]->has_handles) any = true; 188 1041 1229
4430 }
4431 548 ▶ if (!any) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!any) return; 384 164 548
4432  
4433 164 taken = (HV*)sv_2mortal((SV*)newHV());
4434 164 names = (AV*)sv_2mortal((SV*)newAV());
4435 164 plan = (AV*)sv_2mortal((SV*)newAV());
4436  
4437 /* The methods the slots themselves give the class */
4438 425 ▶ for (idx = 1; idx < meta->slot_count; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 1; idx < meta->slot_count; idx++) { 261 164 425
4439 261 SlotSpec *spec = meta->slots[idx];
4440 261 ▶ if (!spec) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) continue; 2 259 261
4441 259 av_clear(names);
4442 259 slot_method_names(aTHX_ spec, names);
4443 535 ▶ for (i = 0; i <= av_len(names); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(names); i++) { 276 259 535
4444 276 (void)hv_store_ent(taken, AvARRAY(names)[i], newSVpv(spec->name, 0), 0);
4445 }
4446 }
4447  
4448 /* Check every delegator before installing any */
4449 399 ▶ for (idx = 1; idx < meta->slot_count; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 1; idx < meta->slot_count; idx++) { 260 139 399
4450 260 SlotSpec *spec = meta->slots[idx];
4451 260 ▶ if (!spec || !spec->has_handles) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!spec || !spec->has_handles) continue; 258 2 260
if (!spec || !spec->has_handles) continue; 70 188 258
4452 519 ▶ for (i = 0; i < spec->resolved_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < spec->resolved_count; i++) { 356 163 519
4453 356 SV *local = spec->resolved[i].local;
4454 356 HE *he = hv_fetch_ent(taken, local, 0, 0);
4455 356 CV *cv = class_method_cv(aTHX_ meta, local);
4456 356 ▶ bool local_sub = cv && !delegator_cv(aTHX_ cv);
Branch: 2 branches
BranchTrueFalseTotal
bool local_sub = cv && !delegator_cv(aTHX_ cv); 33 323 356
bool local_sub = cv && !delegator_cv(aTHX_ cv); 20 13 33
4457  
4458 356 ▶ if (spec->inherited) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->inherited) { 54 302 356
4459 54 ▶ if (he || local_sub) continue; /* overridden */
Branch: 2 branches
BranchTrueFalseTotal
if (he || local_sub) continue; /* overridden */ 51 3 54
if (he || local_sub) continue; /* overridden */ 1 50 51
4460 302 ▶ } else if (is_reserved_method(aTHX_ SvPVX(local), SvCUR(local), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_reserved_method(aTHX_ SvPVX(local), SvCUR(local), 0)) { 6 296 302
4461 6 croak("handles for slot '%s': method '%" SVf "' is reserved",
4462 spec->name, SVfARG(local));
4463 296 ▶ } else if (he) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (he) { 13 283 296
4464 13 croak("handles for slot '%s': method '%" SVf "' is already provided by slot '%" SVf "'",
4465 spec->name, SVfARG(local), SVfARG(HeVAL(he)));
4466 283 ▶ } else if (local_sub) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (local_sub) { 6 277 283
4467 6 croak("You cannot overwrite a locally defined method (%" SVf ") with a delegation",
4468 SVfARG(local));
4469 }
4470 327 (void)hv_store_ent(taken, local, newSVpv(spec->name, 0), 0);
4471 327 av_push(plan, newSViv(idx));
4472 327 av_push(plan, newSViv(i));
4473 }
4474 }
4475  
4476 459 ▶ for (i = 0; i < av_len(plan); i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < av_len(plan); i += 2) { 320 139 459
4477 320 IV slot = SvIVX(AvARRAY(plan)[i]);
4478 320 SlotSpec *spec = meta->slots[slot];
4479 320 install_delegator(aTHX_ meta, slot, spec, &spec->resolved[SvIVX(AvARRAY(plan)[i + 1])]);
4480 }
4481 }
4482  
4483 /* with() adding a role slot whose methods would take a delegator's name:
4484 * install_accessor and the others would leave the delegator in place, so croak
4485 * before the slot joins the class */
4486 41 static void check_role_slot_against_delegators(pTHX_ ClassMeta *meta, SlotSpec *slot, RoleMeta *role) {
4487 41 AV *names = (AV*)sv_2mortal((SV*)newAV());
4488 IV i;
4489 41 slot_method_names(aTHX_ slot, names);
4490 90 ▶ for (i = 0; i <= av_len(names); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(names); i++) { 51 39 90
4491 51 CV *cv = class_method_cv(aTHX_ meta, AvARRAY(names)[i]);
4492 51 ▶ if (cv && delegator_cv(aTHX_ cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cv && delegator_cv(aTHX_ cv)) { 3 48 51
if (cv && delegator_cv(aTHX_ cv)) { 2 1 3
4493 2 croak("Slot conflict: method '%" SVf "' of slot '%s' from role '%s' is delegated in class '%s'",
4494 SVfARG(AvARRAY(names)[i]), slot->name, role->role_name, meta->class_name);
4495 }
4496 }
4497 39 }
4498  
4499 /* ============================================
4500 XS API Functions
4501 ============================================ */
4502  
4503 603 XS_INTERNAL(xs_define) {
4504 603 dXSARGS;
4505 STRLEN class_len;
4506 const char *class_pv;
4507 ClassMeta *meta;
4508 IV i;
4509 603 IV first_prop = 1; /* index of first property arg (after class name) */
4510  
4511 /* Multiple inheritance support */
4512 603 ClassMeta **parent_metas = NULL;
4513 603 IV parent_count = 0;
4514
4515 603 ▶ 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 602 603
4516
4517 602 class_pv = SvPV(ST(0), class_len);
4518  
4519 /* Check for extends => 'ParentClass' or extends => ['P1','P2'] in arguments */
4520 1111 ▶ for (i = 1; i < items - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items - 1; i++) { 584 527 1111
4521 STRLEN klen;
4522 584 const char *kpv = SvPV(ST(i), klen);
4523 584 ▶ if (klen == 7 && memEQ(kpv, "extends", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(kpv, "extends", 7)) { 92 492 584
if (klen == 7 && memEQ(kpv, "extends", 7)) { 75 17 92
4524 75 SV *val = ST(i + 1);
4525 86 ▶ if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 13 62 75
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { 12 1 13
4526 /* extends => ['Parent1', 'Parent2', ...] */
4527 12 AV *parents_av = (AV*)SvRV(val);
4528 12 IV plen = av_len(parents_av) + 1;
4529 IV p;
4530 12 ▶ Newx(parent_metas, plen, ClassMeta*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(parent_metas, plen, ClassMeta*); 0 12 12
4531 36 ▶ for (p = 0; p < plen; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < plen; p++) { 25 11 36
4532 25 SV **elem = av_fetch(parents_av, p, 0);
4533 25 ▶ if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */ 25 0 25
if (elem && SvPOK(*elem)) { /* # uncoverable condition right count:1 */ 23 2 25
4534 STRLEN pname_len;
4535 23 const char *pname = SvPV(*elem, pname_len);
4536 23 ClassMeta *pmeta = get_class_meta(aTHX_ pname, pname_len);
4537 23 ▶ if (!pmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmeta) { 1 22 23
4538 1 Safefree(parent_metas);
4539 1 croak("Object::Proto::define: parent class '%s' has not been defined", pname);
4540 }
4541 22 parent_metas[parent_count++] = pmeta;
4542 }
4543 }
4544 } else {
4545 /* extends => 'SingleParent' */
4546 STRLEN parent_len;
4547 63 const char *parent_pv = SvPV(val, parent_len);
4548 63 ClassMeta *pmeta = get_class_meta(aTHX_ parent_pv, parent_len);
4549 63 ▶ if (!pmeta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmeta) { 2 61 63
4550 2 croak("Object::Proto::define: parent class '%s' has not been defined", parent_pv);
4551 }
4552 61 Newx(parent_metas, 1, ClassMeta*);
4553 61 parent_metas[parent_count++] = pmeta;
4554 }
4555 /* Shift remaining args down to remove extends => value */
4556 {
4557 IV j;
4558 154 ▶ for (j = i; j < items - 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i; j < items - 2; j++) { 82 72 154
4559 82 ST(j) = ST(j + 2);
4560 }
4561 72 items -= 2;
4562 }
4563 72 break;
4564 }
4565 }
4566  
4567 /* Get or create class meta */
4568 599 meta = get_class_meta(aTHX_ class_pv, class_len);
4569 599 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 581 18 599
4570 581 meta = create_class_meta(aTHX_ class_pv, class_len);
4571 581 register_class_meta(aTHX_ class_pv, class_len, meta);
4572 }
4573  
4574 /* Store parent references */
4575 599 ▶ if (parent_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_count > 0) { 72 527 599
4576 72 ▶ Newx(meta->parent_classes, parent_count, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(meta->parent_classes, parent_count, char*); 0 72 72
4577 72 ▶ Newx(meta->parent_metas, parent_count, ClassMeta*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(meta->parent_metas, parent_count, ClassMeta*); 0 72 72
4578 72 meta->parent_count = parent_count;
4579 154 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 82 72 154
4580 82 STRLEN plen = strlen(parent_metas[i]->class_name);
4581 82 Newx(meta->parent_classes[i], plen + 1, char);
4582 82 Copy(parent_metas[i]->class_name, meta->parent_classes[i], plen + 1, char);
4583 82 meta->parent_metas[i] = parent_metas[i];
4584 }
4585 }
4586  
4587 /* Calculate total slots needed: all parent inherited + child own */
4588 {
4589 599 IV total_parent_slots = 0;
4590 599 IV child_props = items - 1;
4591 IV max_slots;
4592 681 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 82 599 681
4593 82 total_parent_slots += parent_metas[i]->slot_count - 1; /* -1 for prototype slot */
4594 }
4595 /* Slots are appended after the class's existing ones: a redefinition
4596 * keeps its slots where they were, so its new arrays must still reach
4597 * every index it had (slot_count is 1 for a new class) */
4598 599 max_slots = meta->slot_count + total_parent_slots + child_props;
4599 599 ▶ Renew(meta->idx_to_prop, max_slots + 1, char*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(meta->idx_to_prop, max_slots + 1, char*); 0 599 599
4600 599 ▶ Renew(meta->slots, max_slots + 1, SlotSpec*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(meta->slots, max_slots + 1, SlotSpec*); 0 599 599
4601 3139 ▶ for (i = 0; i <= max_slots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max_slots; i++) { 2540 599 3139
4602 2540 meta->slots[i] = NULL;
4603 2540 meta->idx_to_prop[i] = NULL;
4604 }
4605 }
4606  
4607 /* Copy parent slots (if extends) - iterate all parents, first parent wins on conflict */
4608 681 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 82 599 681
4609 82 ClassMeta *pmeta = parent_metas[i];
4610 IV j;
4611 269 ▶ for (j = 1; j < pmeta->slot_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < pmeta->slot_count; j++) { 187 82 269
4612 187 SlotSpec *parent_spec = pmeta->slots[j];
4613 /*
4614 # uncoverable branch false
4615 # uncoverable condition right count:1 */
4616 187 ▶ if (parent_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_spec) { 187 0 187
4617 /* Skip if property already defined by earlier parent */
4618 187 SV **existing = hv_fetch(meta->prop_to_idx, parent_spec->name,
4619 strlen(parent_spec->name), 0);
4620 187 ▶ 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 183 187
if (existing && SvIOK(*existing)) continue; /* # uncoverable condition right count:2 */ 4 0 4
4621  
4622 {
4623 183 SlotSpec *cloned = clone_slot_spec(aTHX_ parent_spec);
4624 183 cloned->inherited = 1;
4625 183 resolve_handles(aTHX_ cloned);
4626 183 register_slot(aTHX_ class_pv, meta, meta->slot_count++, cloned);
4627 }
4628 }
4629 }
4630 }
4631  
4632 /* Register each child property */
4633 1642 ▶ for (i = first_prop; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = first_prop; i < items; i++) { 1117 525 1642
4634 STRLEN spec_len;
4635 1117 const char *spec_pv = SvPV(ST(i), spec_len);
4636 SlotSpec *spec;
4637 IV idx;
4638 SV **existing;
4639 1117 U8 is_modification = 0;
4640 1117 const char *real_spec_pv = spec_pv;
4641 1117 STRLEN real_spec_len = spec_len;
4642  
4643 /* Check for +attr modification syntax (Moo/Moose-style) */
4644 1117 ▶ 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 */ 1117 0 1117
if (spec_len > 0 && spec_pv[0] == '+') { /* # uncoverable condition right count:1 */ 47 1070 1117
4645 47 is_modification = 1;
4646 47 real_spec_pv = spec_pv + 1;
4647 47 real_spec_len = spec_len - 1;
4648 }
4649  
4650 /* Parse the slot spec (e.g., "name:Str:required" or just "name") */
4651 1117 spec = parse_slot_spec(aTHX_ real_spec_pv, real_spec_len);
4652 1092 spec->private_owner = savepvn(class_pv, class_len);
4653 /* Validate handles before the class is touched, so that a croak
4654 * leaves it as it was */
4655 1092 ▶ if (!is_modification) resolve_handles(aTHX_ spec);
Branch: 1 branch
BranchTrueFalseTotal
if (!is_modification) resolve_handles(aTHX_ spec); 1045 47 1092
4656  
4657 /* Check if this property already exists (from parent) */
4658 1047 existing = hv_fetch(meta->prop_to_idx, spec->name, strlen(spec->name), 0);
4659
4660 1047 ▶ if (is_modification) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_modification) { 47 1000 1047
4661 /* +attr syntax: merge child modifiers onto parent spec */
4662 SlotSpec *parent_spec;
4663 SlotSpec *merged;
4664
4665 47 ▶ if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */ 46 1 47
if (!existing || !SvIOK(*existing)) { /* # uncoverable condition left count:2 */ 0 46 46
4666 1 croak("+%s: no inherited attribute '%s' to modify",
4667 spec->name, spec->name);
4668 }
4669 46 idx = SvIV(*existing);
4670 46 parent_spec = meta->slots[idx];
4671
4672 /* Merge override onto clone of parent */
4673 46 merged = merge_slot_spec(aTHX_ parent_spec, spec);
4674 /* Before anything is freed: a croak leaves the class as it was */
4675 46 resolve_handles(aTHX_ merged);
4676
4677 /* Free the override spec (we cloned what we needed) */
4678 43 Safefree(spec->name);
4679 43 Safefree(spec->private_owner);
4680 43 SvREFCNT_dec((SV*)spec->handles);
4681 43 Safefree(spec);
4682 43 spec = merged;
4683
4684 /* Free old parent spec */
4685 /*
4686 # uncoverable branch false
4687 # uncoverable condition right count:1 */
4688 43 ▶ if (parent_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_spec) { 43 0 43
4689 43 Safefree(parent_spec->name);
4690 43 Safefree(parent_spec->private_owner);
4691 43 Safefree(parent_spec->private_also);
4692 43 SvREFCNT_dec((SV*)parent_spec->handles);
4693 43 Safefree(parent_spec);
4694 }
4695 1000 ▶ } else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
} else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */ 36 964 1000
} else if (existing && SvIOK(*existing)) { /* # uncoverable condition right count:2 */ 36 0 36
4696 /* Full override: reuse same slot index */
4697 36 idx = SvIV(*existing);
4698 /* Free old spec */
4699 36 ▶ if (meta->slots[idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->slots[idx]) { 3 33 36
4700 3 Safefree(meta->slots[idx]->name);
4701 3 Safefree(meta->slots[idx]->private_owner);
4702 3 Safefree(meta->slots[idx]->private_also);
4703 3 SvREFCNT_dec((SV*)meta->slots[idx]->handles);
4704 3 Safefree(meta->slots[idx]);
4705 }
4706 } else {
4707 964 idx = meta->slot_count++;
4708 }
4709  
4710 1043 register_slot(aTHX_ class_pv, meta, idx, spec);
4711 }
4712  
4713 /* handles(...): now that every slot's methods are in */
4714 525 install_class_delegators(aTHX_ meta);
4715  
4716 /* Set up @ISA for parent classes (C3 MRO for multiple inheritance) */
4717 501 ▶ if (parent_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent_count > 0) { 67 434 501
4718 67 AV *isa = get_av(Perl_form(aTHX_ "%s::ISA", class_pv), GV_ADD);
4719 144 ▶ for (i = 0; i < parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parent_count; i++) { 77 67 144
4720 77 av_push(isa, newSVpv(parent_metas[i]->class_name, 0));
4721 }
4722 /* Notify Perl's method resolution cache that ISA changed.
4723 * Perl_mro_isa_changed_in was made a hidden (non-exported) symbol
4724 * in Perl 5.36.0. mro_method_changed_in is public since 5.10.0
4725 * and is the correct public API for invalidating the method cache
4726 * after an ISA change. */
4727 67 mro_method_changed_in(meta->stash);
4728 67 Safefree(parent_metas);
4729 }
4730  
4731 /* Install constructor */
4732 501 install_constructor(aTHX_ class_pv, meta);
4733  
4734 /* Install prototype methods as class methods */
4735 {
4736 char method_name[256];
4737 501 snprintf(method_name, sizeof(method_name), "%s::set_prototype", class_pv);
4738 501 newXS(method_name, xs_set_prototype, __FILE__);
4739 501 snprintf(method_name, sizeof(method_name), "%s::prototype", class_pv);
4740 501 newXS(method_name, xs_prototype, __FILE__);
4741 }
4742
4743 /* Check for DEMOLISH method - only set up destruction hook if class has one */
4744 {
4745 char demolish_name[256];
4746 CV *demolish_cv;
4747 501 snprintf(demolish_name, sizeof(demolish_name), "%s::DEMOLISH", class_pv);
4748 501 demolish_cv = get_cvn_flags(demolish_name, strlen(demolish_name), 0);
4749 501 ▶ if (demolish_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (demolish_cv) { 5 496 501
4750 5 meta->demolish_cv = demolish_cv;
4751 /* Install DESTROY wrapper that calls DEMOLISH */
4752 5 install_destroy_wrapper(aTHX_ class_pv, meta);
4753 }
4754 }
4755  
4756 /* Check for BUILD method - called after new() */
4757 {
4758 char build_name[256];
4759 CV *build_cv;
4760 501 snprintf(build_name, sizeof(build_name), "%s::BUILD", class_pv);
4761 501 build_cv = get_cvn_flags(build_name, strlen(build_name), 0);
4762 501 ▶ if (build_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (build_cv) { 1 500 501
4763 1 meta->build_cv = build_cv;
4764 1 meta->has_build = 1;
4765 }
4766 }
4767
4768 501 XSRETURN_EMPTY;
4769 }
4770  
4771 3015 XS_INTERNAL(xs_prototype) {
4772 3015 dXSARGS;
4773 AV *av;
4774 SV **svp;
4775
4776 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
4777
4778 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
4779 2 croak("Not an object");
4780 }
4781 3012 av = (AV*)SvRV(ST(0));
4782 3012 svp = av_fetch(av, 0, 0);
4783 3012 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 3011 1 3012
if (svp && SvOK(*svp)) { 3009 2 3011
4784 3009 ST(0) = SvREFCNT_inc(*svp);
4785 } else {
4786 3 ST(0) = &PL_sv_undef;
4787 }
4788 3012 XSRETURN(1);
4789 }
4790  
4791 186 XS_INTERNAL(xs_set_prototype) {
4792 186 dXSARGS;
4793 AV *av;
4794 MAGIC *mg;
4795  
4796 186 ▶ 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 185 186
4797  
4798 185 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 184 1 185
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 1 183 184
4799 2 croak("Not an object");
4800 }
4801 183 av = (AV*)SvRV(ST(0));
4802  
4803 183 mg = get_object_magic(aTHX_ ST(0));
4804 183 ▶ if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 3 180 183
if (mg && (mg->mg_private & OBJ_FLAG_FROZEN)) { 1 2 3
4805 1 croak("Cannot modify frozen object");
4806 }
4807  
4808 182 av_store(av, 0, newSVsv(ST(1)));
4809 182 XSRETURN_EMPTY;
4810 }
4811  
4812 /* Get the full prototype chain as an arrayref */
4813 10 XS_INTERNAL(xs_prototype_chain) {
4814 10 dXSARGS;
4815 AV *av;
4816 AV *chain;
4817 AV *visited[MAX_PROTOTYPE_DEPTH];
4818 10 int depth = 0;
4819 int i;
4820  
4821 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
4822  
4823 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
4824 2 croak("Not an object");
4825 }
4826  
4827 7 chain = newAV();
4828 7 av = (AV*)SvRV(ST(0));
4829  
4830 /*
4831 # uncoverable branch true count:2
4832 # uncoverable condition right count:1 */
4833 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
4834 SV **proto_svp;
4835  
4836 /* Check for circular reference */
4837 5066 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 4956 110 5066
4838 4956 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 1 4955 4956
4839 1 goto done; /* Cycle detected, stop */
4840 }
4841 }
4842 110 visited[depth] = av;
4843  
4844 /* Add this object to the chain */
4845 110 av_push(chain, newRV_inc((SV*)av));
4846  
4847 /* Follow prototype */
4848 110 proto_svp = av_fetch(av, 0, 0);
4849 /*
4850 # uncoverable branch true count:1 class:ignore_covered_err
4851 # uncoverable condition left count:1 */
4852 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
4853 break;
4854 }
4855 105 av = (AV*)SvRV(*proto_svp);
4856 105 depth++;
4857 }
4858  
4859 6 done:
4860 7 ST(0) = sv_2mortal(newRV_noinc((SV*)chain));
4861 7 XSRETURN(1);
4862 }
4863  
4864 /* Check if object has a property in its own slots (not prototype) */
4865 14 XS_INTERNAL(xs_has_own_property) {
4866 14 dXSARGS;
4867 AV *av;
4868 SV **svp;
4869 const char *class_name;
4870 STRLEN class_len;
4871 ClassMeta *meta;
4872 const char *prop_name;
4873 STRLEN prop_len;
4874 SV **idx_sv;
4875  
4876 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
4877  
4878 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
4879 2 croak("Not an object");
4880 }
4881  
4882 11 av = (AV*)SvRV(ST(0));
4883 11 class_name = sv_reftype(SvRV(ST(0)), TRUE);
4884 11 class_len = strlen(class_name);
4885  
4886 11 meta = get_class_meta(aTHX_ class_name, class_len);
4887 11 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 10 11
4888 1 XSRETURN_NO;
4889 }
4890  
4891 10 prop_name = SvPV(ST(1), prop_len);
4892 10 idx_sv = hv_fetch(meta->prop_to_idx, prop_name, prop_len, 0);
4893 10 ▶ if (!idx_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_sv) { 1 9 10
4894 1 XSRETURN_NO;
4895 }
4896  
4897 /* Check if this slot has a defined value */
4898 9 svp = av_fetch(av, SvIV(*idx_sv), 0);
4899 9 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 8 1 9
if (svp && SvOK(*svp)) { 4 4 8
4900 4 XSRETURN_YES;
4901 }
4902 5 XSRETURN_NO;
4903 }
4904  
4905 /* Get the prototype depth (number of prototypes in chain) */
4906 10 XS_INTERNAL(xs_prototype_depth) {
4907 10 dXSARGS;
4908 AV *av;
4909 AV *visited[MAX_PROTOTYPE_DEPTH];
4910 10 int depth = 0;
4911 int i;
4912  
4913 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
4914  
4915 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
4916 2 croak("Not an object");
4917 }
4918  
4919 7 av = (AV*)SvRV(ST(0));
4920  
4921 /*
4922 # uncoverable branch true count:2
4923 # uncoverable condition right count:1 */
4924 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
4925 SV **proto_svp;
4926  
4927 /* Check for circular reference */
4928 5066 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 4956 110 5066
4929 4956 ▶ if (visited[i] == av) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[i] == av) { 1 4955 4956
4930 1 goto done;
4931 }
4932 }
4933 110 visited[depth] = av;
4934  
4935 110 proto_svp = av_fetch(av, 0, 0);
4936 /*
4937 # uncoverable branch true count:1 class:ignore_covered_err
4938 # uncoverable condition left count:1 */
4939 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
4940 break;
4941 }
4942 105 av = (AV*)SvRV(*proto_svp);
4943 105 depth++;
4944 }
4945  
4946 6 done:
4947 7 XSRETURN_IV(depth);
4948 }
4949  
4950 417 XS_INTERNAL(xs_lock) {
4951 417 dXSARGS;
4952 MAGIC *mg;
4953
4954 417 ▶ if (items < 1) croak("Usage: Object::Proto::lock($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::lock($obj)"); 1 416 417
4955 416 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::lock");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 413 3 416
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 413 0 413
VALIDATE_OBJECT(ST(0), "Object::Proto::lock"); 0 413 413
4956
4957 413 mg = get_object_magic(aTHX_ ST(0));
4958 413 ▶ if (!mg) mg = add_object_magic(aTHX_ ST(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) mg = add_object_magic(aTHX_ ST(0)); 13 400 413
4959 413 ▶ if (mg->mg_private & OBJ_FLAG_FROZEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private & OBJ_FLAG_FROZEN) { 1 412 413
4960 1 croak("Object is frozen");
4961 }
4962 412 mg->mg_private |= OBJ_FLAG_LOCKED;
4963 412 XSRETURN_EMPTY;
4964 }
4965  
4966 406 XS_INTERNAL(xs_unlock) {
4967 406 dXSARGS;
4968 MAGIC *mg;
4969
4970 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
4971 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
4972
4973 404 mg = get_object_magic(aTHX_ ST(0));
4974 404 ▶ if (mg) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg) { 403 1 404
4975 403 ▶ if (mg->mg_private & OBJ_FLAG_FROZEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private & OBJ_FLAG_FROZEN) { 1 402 403
4976 1 croak("Cannot unlock frozen object");
4977 }
4978 402 mg->mg_private &= ~OBJ_FLAG_LOCKED;
4979 }
4980 403 XSRETURN_EMPTY;
4981 }
4982  
4983 24 XS_INTERNAL(xs_freeze) {
4984 24 dXSARGS;
4985 MAGIC *mg;
4986
4987 24 ▶ if (items < 1) croak("Usage: Object::Proto::freeze($obj)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::freeze($obj)"); 1 23 24
4988 23 ▶ VALIDATE_OBJECT(ST(0), "Object::Proto::freeze");
Branch: 3 branches
BranchTrueFalseTotal
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 22 1 23
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 22 0 22
VALIDATE_OBJECT(ST(0), "Object::Proto::freeze"); 1 21 22
4989
4990 21 mg = get_object_magic(aTHX_ ST(0));
4991 21 ▶ if (!mg) mg = add_object_magic(aTHX_ ST(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) mg = add_object_magic(aTHX_ ST(0)); 20 1 21
4992 21 mg->mg_private |= (OBJ_FLAG_FROZEN | OBJ_FLAG_LOCKED);
4993 21 XSRETURN_EMPTY;
4994 }
4995  
4996 3012 XS_INTERNAL(xs_is_frozen) {
4997 3012 dXSARGS;
4998 MAGIC *mg;
4999
5000 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
5001 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
5002
5003 3010 mg = get_object_magic(aTHX_ ST(0));
5004 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
5005 3006 XSRETURN_YES;
5006 }
5007 4 XSRETURN_NO;
5008 }
5009  
5010 3420 XS_INTERNAL(xs_is_locked) {
5011 3420 dXSARGS;
5012 MAGIC *mg;
5013  
5014 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
5015 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
5016  
5017 3418 mg = get_object_magic(aTHX_ ST(0));
5018 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
5019 2409 XSRETURN_YES;
5020 }
5021 1009 XSRETURN_NO;
5022 }
5023  
5024 /* ============================================
5025 Introspection API
5026 ============================================ */
5027  
5028 /* Deep clone an SV, recursing into refs.
5029 * seen_hv maps refaddr strings -> cloned SV* (handles circular refs).
5030 * Returns a mortal SV. */
5031 27545 static SV* deep_clone_sv(pTHX_ SV *src, HV *seen_hv) {
5032 SV *dst;
5033 char addr_buf[32];
5034 STRLEN addr_len;
5035 SV **cached;
5036  
5037 /* Non-references: return a plain copy */
5038 27545 ▶ if (!SvROK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) { 15279 12266 27545
5039 15279 return newSVsv(src);
5040 }
5041  
5042 /* Check seen table to break circular references */
5043 12266 addr_len = (STRLEN)sprintf(addr_buf, "%p", (void*)SvRV(src));
5044 12266 cached = hv_fetch(seen_hv, addr_buf, (I32)addr_len, 0);
5045 12266 ▶ if (cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached) { 1 12265 12266
5046 1 return SvREFCNT_inc(*cached);
5047 }
5048  
5049 12265 ▶ if (SvTYPE(SvRV(src)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(src)) == SVt_PVAV) { 12254 11 12265
5050 /* Array ref (possibly blessed) */
5051 12254 AV *src_av = (AV*)SvRV(src);
5052 12254 AV *dst_av = newAV();
5053 12254 IV i, len = av_len(src_av);
5054  
5055 12254 dst = newRV_noinc((SV*)dst_av);
5056 12254 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 6630 5624 12254
5057 6630 sv_bless(dst, SvSTASH(SvRV(src)));
5058  
5059 /* Register before recursing to handle circular refs */
5060 12254 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
5061  
5062 12254 av_extend(dst_av, len);
5063 39791 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 27537 12254 39791
5064 27537 SV **svp = av_fetch(src_av, i, 0);
5065 48432 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 27535 2 27537
if (svp && SvOK(*svp)) { 20895 6640 27535
5066 20895 SV *child = deep_clone_sv(aTHX_ *svp, seen_hv);
5067 20895 av_store(dst_av, i, child);
5068 } else {
5069 6642 av_store(dst_av, i, newSV(0));
5070 }
5071 }
5072  
5073 11 ▶ } else if (SvTYPE(SvRV(src)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(src)) == SVt_PVHV) { 7 4 11
5074 /* Hash ref (possibly blessed) */
5075 7 HV *src_hv = (HV*)SvRV(src);
5076 7 HV *dst_hv = newHV();
5077 HE *he;
5078  
5079 7 dst = newRV_noinc((SV*)dst_hv);
5080 7 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 1 6 7
5081 1 sv_bless(dst, SvSTASH(SvRV(src)));
5082  
5083 7 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
5084  
5085 7 hv_iterinit(src_hv);
5086 18 ▶ while ((he = hv_iternext(src_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src_hv))) { 11 7 18
5087 STRLEN klen;
5088 11 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 11 11
5089 11 SV *val = HeVAL(he);
5090 11 SV *copy = deep_clone_sv(aTHX_ val, seen_hv);
5091 11 hv_store(dst_hv, key, (I32)klen, copy, 0);
5092 }
5093  
5094 4 ▶ } else if (SvTYPE(SvRV(src)) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(SvRV(src)) < SVt_PVAV) { 2 2 4
5095 /* Scalar ref */
5096 2 SV *inner = deep_clone_sv(aTHX_ SvRV(src), seen_hv);
5097 2 dst = newRV_noinc(inner);
5098 2 ▶ if (SvOBJECT(SvRV(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(SvRV(src))) 1 1 2
5099 1 sv_bless(dst, SvSTASH(SvRV(src)));
5100 2 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
5101  
5102 } else {
5103 /* Code refs, globs, etc. — share as-is */
5104 2 dst = newSVsv(src);
5105 2 hv_store(seen_hv, addr_buf, (I32)addr_len, SvREFCNT_inc(dst), 0);
5106 }
5107  
5108 12265 return dst;
5109 }
5110  
5111 /* Object::Proto::clone($obj) - deep clone an object, arrayref, hashref,
5112 * scalarref, or plain scalar */
5113 6644 XS_INTERNAL(xs_clone) {
5114 6644 dXSARGS;
5115 SV *src;
5116  
5117 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
5118  
5119 6643 src = ST(0);
5120  
5121 /* Plain scalar (non-ref): return a copy of the value */
5122 6643 ▶ if (!SvROK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) { 6 6637 6643
5123 6 ▶ if (SvOK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(src)) { 4 2 6
5124 4 ST(0) = sv_2mortal(newSVsv(src));
5125 } else {
5126 2 ST(0) = &PL_sv_undef;
5127 }
5128 6 XSRETURN(1);
5129 }
5130  
5131 {
5132 6637 HV *seen_hv = newHV();
5133 SV *dst;
5134  
5135 /* For blessed objects backed by an AV: strip frozen/locked magic
5136 * by cloning the underlying AV fresh (deep_clone_sv handles the
5137 * bless but the new ref carries no Object::Proto magic). */
5138 6637 dst = deep_clone_sv(aTHX_ src, seen_hv);
5139 6637 SvREFCNT_dec((SV*)seen_hv);
5140  
5141 6637 ST(0) = sv_2mortal(dst);
5142 6637 XSRETURN(1);
5143 }
5144 }
5145  
5146 /* Object::Proto::properties($class) - return property names for a class */
5147 10625 XS_INTERNAL(xs_properties) {
5148 10625 dXSARGS;
5149 STRLEN class_len;
5150 const char *class_pv;
5151 ClassMeta *meta;
5152 IV i;
5153  
5154 10625 ▶ if (items < 1) croak("Usage: Object::Proto::properties($class)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Object::Proto::properties($class)"); 1 10624 10625
5155  
5156 10624 class_pv = SvPV(ST(0), class_len);
5157  
5158 10624 meta = get_class_meta(aTHX_ class_pv, class_len);
5159 10624 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 4004 6620 10624
5160 /* Non-existent class: return empty list / 0 */
5161 4004 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2002 2002 4004
5162 2002 XSRETURN_EMPTY;
5163 } else {
5164 2002 XSRETURN_IV(0);
5165 }
5166 }
5167  
5168 6620 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 4619 2001 6620
5169 /* List context: return property names */
5170 4619 IV count = meta->slot_count - 1; /* -1 because slot 0 is prototype */
5171 4619 SP -= items;
5172 4619 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 4619 0 4619
EXTEND(SP, count); 0 4619 4619
5173  
5174 18483 ▶ for (i = 1; i < meta->slot_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < meta->slot_count; i++) { 13864 4619 18483
5175 /*
5176 # uncoverable branch false
5177 # uncoverable condition right count:1 */
5178 13864 ▶ if (meta->idx_to_prop[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta->idx_to_prop[i]) { 13864 0 13864
5179 13864 PUSHs(sv_2mortal(newSVpv(meta->idx_to_prop[i], 0)));
5180 }
5181 }
5182 4619 XSRETURN(count);
5183 } else {
5184 /* Scalar context: return count */
5185 2001 XSRETURN_IV(meta->slot_count - 1);
5186 }
5187 }
5188  
5189 /* Object::Proto::slot_info($class, $property) - return hashref with slot metadata */
5190 16921 XS_INTERNAL(xs_slot_info) {
5191 16921 dXSARGS;
5192 STRLEN class_len, prop_len;
5193 const char *class_pv, *prop_pv;
5194 ClassMeta *meta;
5195 SV **idx_svp;
5196 IV idx;
5197 SlotSpec *spec;
5198 HV *info;
5199  
5200 16921 ▶ 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 16920 16921
5201  
5202 16920 class_pv = SvPV(ST(0), class_len);
5203 16920 ▶ prop_pv = SvPV(ST(1), prop_len);
Branch: 1 branch
BranchTrueFalseTotal
prop_pv = SvPV(ST(1), prop_len); 16920 0 16920
5204  
5205 /* Look up class meta */
5206 16920 meta = get_class_meta(aTHX_ class_pv, class_len);
5207 16920 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 2003 14917 16920
5208 2003 XSRETURN_UNDEF;
5209 }
5210  
5211 /* Look up property index - O(1) hash lookup */
5212 14917 idx_svp = hv_fetch(meta->prop_to_idx, prop_pv, prop_len, 0);
5213 14917 ▶ if (!idx_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx_svp) { 2003 12914 14917
5214 2003 XSRETURN_UNDEF;
5215 }
5216 12914 idx = SvIV(*idx_svp);
5217  
5218 /* Build result hashref */
5219 12914 info = newHV();
5220  
5221 /* Basic info always present */
5222 12914 hv_store(info, "name", 4, newSVpv(prop_pv, prop_len), 0);
5223 12914 hv_store(info, "index", 5, newSViv(idx), 0);
5224  
5225 /* Get slot spec if available */
5226 /*
5227 # uncoverable condition right count:1
5228 # uncoverable condition right count:2 */
5229 12914 ▶ 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; 12914 0 12914
spec = (meta->slots && idx < meta->slot_count) ? meta->slots[idx] : NULL; 12914 0 12914
5230  
5231 12914 ▶ if (spec && spec->has_type) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 12914 0 12914
if (spec && spec->has_type) { /* # uncoverable condition right count:1 */ 10895 2019 12914
5232 const char *type_name;
5233 10895 ▶ 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 10893 10895
if (spec->type_id == TYPE_CUSTOM && spec->registered) { /* # uncoverable condition right count:2 */ 2 0 2
5234 2 type_name = spec->registered->name;
5235 } else {
5236 10893 type_name = type_id_to_name(spec->type_id);
5237 }
5238 10895 hv_store(info, "type", 4, newSVpv(type_name, 0), 0);
5239 }
5240  
5241 /* Boolean flags */
5242 12914 ▶ 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 */ 12914 0 12914
5243 12914 ▶ 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 */ 12914 0 12914
5244 12914 ▶ 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 */ 12914 0 12914
5245 12914 ▶ 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 */ 12914 0 12914
5246 12914 ▶ 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 */ 12914 0 12914
5247 12914 ▶ 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 */ 12914 0 12914
5248 12914 ▶ 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 */ 12914 0 12914
5249 12914 ▶ 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 */ 12914 0 12914
5250 12914 ▶ 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 */ 12914 0 12914
5251 12914 ▶ 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 */ 12914 0 12914
5252 12914 ▶ 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 */ 12914 0 12914
5253  
5254 /* Default value (if present) */
5255 12914 ▶ 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 */ 12914 0 12914
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 5224 7690 12914
if (spec && spec->has_default && spec->default_sv) { /* # uncoverable condition right count:1 */ 5223 1 5224
5256 5223 hv_store(info, "default", 7, newSVsv(spec->default_sv), 0);
5257 }
5258  
5259 /* Builder method name */
5260 /*
5261 # uncoverable condition right count:1
5262 # uncoverable condition right count:3 */
5263 12914 ▶ if (spec && spec->has_builder && spec->builder_name) {
Branch: 3 branches
BranchTrueFalseTotal
if (spec && spec->has_builder && spec->builder_name) { 12914 0 12914
if (spec && spec->has_builder && spec->builder_name) { 4 12910 12914
if (spec && spec->has_builder && spec->builder_name) { 4 0 4
5264 4 hv_store(info, "builder", 7, newSVsv(spec->builder_name), 0);
5265 }
5266  
5267 /* init_arg (if specified) */
5268 12914 ▶ if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */ 12914 0 12914
if (spec && spec->init_arg) { /* # uncoverable condition right count:1 */ 2 12912 12914
5269 2 hv_store(info, "init_arg", 8, newSVsv(spec->init_arg), 0);
5270 }
5271  
5272 /* handles, and the default they added. Every slot of a defined class has
5273 * its spec, so spec needs no test here. */
5274 12914 hv_store(info, "has_handles", 11, newSViv(spec->has_handles), 0);
5275 12914 ▶ if (spec->default_implicit) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->default_implicit) { 16 12898 12914
5276 16 hv_store(info, "default_implicit", 16, newSViv(1), 0);
5277 }
5278 12914 ▶ if (spec->has_handles) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->has_handles) { 57 12857 12914
5279 57 HV *handles = newHV();
5280 IV h;
5281 230 ▶ for (h = 0; h < spec->resolved_count; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < spec->resolved_count; h++) { 173 57 230
5282 173 (void)hv_store_ent(handles, spec->resolved[h].local, newSVsv(spec->resolved[h].remote), 0);
5283 }
5284 57 hv_store(info, "handles", 7, newRV_noinc((SV*)handles), 0);
5285 }
5286  
5287 12914 ST(0) = sv_2mortal(newRV_noinc((SV*)info));
5288 12914 XSRETURN(1);
5289 }
5290  
5291 /* Object::Proto::parent($class) - return parent class name or undef */
5292 12 XS_INTERNAL(xs_parent) {
5293 12 dXSARGS;
5294 STRLEN class_len;
5295 const char *class_pv;
5296 ClassMeta *meta;
5297  
5298 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
5299  
5300 11 class_pv = SvPV(ST(0), class_len);
5301 11 meta = get_class_meta(aTHX_ class_pv, class_len);
5302  
5303 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
5304 5 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2 3 5
5305 2 XSRETURN_EMPTY;
5306 }
5307 3 XSRETURN_UNDEF;
5308 }
5309  
5310 6 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2 4 6
5311 /* List context: return all parents */
5312 IV i;
5313 2 SP -= items;
5314 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
5315 5 ▶ for (i = 0; i < meta->parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->parent_count; i++) { 3 2 5
5316 3 PUSHs(sv_2mortal(newSVpv(meta->parent_classes[i], 0)));
5317 }
5318 2 XSRETURN(meta->parent_count);
5319 } else {
5320 /* Scalar context: return first parent */
5321 4 ST(0) = sv_2mortal(newSVpv(meta->parent_classes[0], 0));
5322 4 XSRETURN(1);
5323 }
5324 }
5325  
5326 /* Object::Proto::ancestors($class) - return list of all ancestor class names (breadth-first) */
5327 8 XS_INTERNAL(xs_ancestors) {
5328 8 dXSARGS;
5329 STRLEN class_len;
5330 const char *class_pv;
5331 ClassMeta *meta;
5332 AV *result;
5333 HV *seen;
5334 AV *queue;
5335 8 IV count = 0;
5336  
5337 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
5338  
5339 7 class_pv = SvPV(ST(0), class_len);
5340 7 meta = get_class_meta(aTHX_ class_pv, class_len);
5341  
5342 7 SP -= items;
5343  
5344 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
5345 IV i;
5346 5 result = newAV();
5347 5 seen = newHV();
5348 5 queue = newAV();
5349  
5350 /* Seed queue with direct parents */
5351 13 ▶ for (i = 0; i < meta->parent_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->parent_count; i++) { 8 5 13
5352 8 av_push(queue, newSVpv(meta->parent_metas[i]->class_name, 0));
5353 }
5354  
5355 /* BFS traversal */
5356 17 ▶ while (av_len(queue) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(queue) >= 0) { 12 5 17
5357 12 SV *cur_sv = av_shift(queue);
5358 STRLEN cur_len;
5359 12 const char *cur_name = SvPV(cur_sv, cur_len);
5360 ClassMeta *cur_meta;
5361  
5362 /* Skip if already seen */
5363 12 ▶ if (hv_exists(seen, cur_name, cur_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, cur_name, cur_len)) { 1 11 12
5364 1 SvREFCNT_dec(cur_sv);
5365 1 continue;
5366 }
5367 11 hv_store(seen, cur_name, cur_len, &PL_sv_yes, 0);
5368 11 av_push(result, cur_sv);
5369  
5370 /* Enqueue this class's parents */
5371 11 cur_meta = get_class_meta(aTHX_ cur_name, cur_len);
5372 /*
5373 # uncoverable branch false
5374 # uncoverable condition right count:1 */
5375 11 ▶ if (cur_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_meta) { 11 0 11
5376 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
5377 5 const char *pname = cur_meta->parent_classes[i];
5378 5 ▶ if (!hv_exists(seen, pname, strlen(pname))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(seen, pname, strlen(pname))) { 4 1 5
5379 4 av_push(queue, newSVpv(pname, 0));
5380 }
5381 }
5382 }
5383 }
5384  
5385 5 count = av_len(result) + 1;
5386 5 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 5 0 5
EXTEND(SP, count); 0 5 5
5387 16 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 11 5 16
5388 11 SV **elem = av_fetch(result, i, 0);
5389 /*
5390 # uncoverable branch false
5391 # uncoverable condition right count:1 */
5392 11 ▶ if (elem) PUSHs(sv_2mortal(newSVsv(*elem)));
Branch: 1 branch
BranchTrueFalseTotal
if (elem) PUSHs(sv_2mortal(newSVsv(*elem))); 11 0 11
5393 }
5394  
5395 5 SvREFCNT_dec((SV*)result);
5396 5 SvREFCNT_dec((SV*)seen);
5397 5 SvREFCNT_dec((SV*)queue);
5398 }
5399  
5400 7 XSRETURN(count);
5401 }
5402  
5403 /* ============================================
5404 Global cleanup
5405 ============================================ */
5406  
5407 /* Cleanup during global destruction */
5408 90 static void object_cleanup_globals(pTHX_ void *data) {
5409 PERL_UNUSED_ARG(data);
5410  
5411 /* During global destruction, just NULL out pointers.
5412 * Perl handles SV cleanup. Trying to free them ourselves
5413 * can cause crashes due to destruction order. */
5414 /*
5415 # uncoverable branch false
5416 # uncoverable condition right count:1 */
5417 90 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 90 0 90
5418 90 g_type_registry = NULL;
5419 90 g_class_registry = NULL;
5420 90 g_func_accessor_registry = NULL;
5421 90 return;
5422 }
5423  
5424 /* Normal cleanup - not during global destruction */
5425 /* Note: Full cleanup omitted for simplicity; Perl handles SV refcounts */
5426 0 g_type_registry = NULL; /* # uncoverable statement */
5427 0 g_class_registry = NULL; /* # uncoverable statement */
5428 0 g_func_accessor_registry = NULL; /* # uncoverable statement */
5429 }
5430  
5431 /* ============================================
5432 Type Registry API
5433 ============================================ */
5434  
5435 /* C-level registration for external XS modules (called from BOOT)
5436 This is the fast path - no Perl callback overhead */
5437 4 OBJECT_API void object_register_type_xs(pTHX_ const char *name,
5438 ObjectTypeCheckFunc check,
5439 ObjectTypeCoerceFunc coerce) {
5440 RegisteredType *type;
5441 4 STRLEN name_len = strlen(name);
5442
5443 /*
5444 # uncoverable branch true
5445 # uncoverable condition left count:1 */
5446 4 ▶ if (!g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) { 0 4 4
5447 0 g_type_registry = newHV(); /* # uncoverable statement */
5448 }
5449
5450 /* Check if already registered */
5451 4 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
5452 4 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 3 4
5453 1 croak("Type '%s' is already registered", name);
5454 }
5455
5456 3 Newxz(type, 1, RegisteredType);
5457 3 Newx(type->name, name_len + 1, char);
5458 3 Copy(name, type->name, name_len, char);
5459 3 type->name[name_len] = '\0';
5460
5461 3 type->check = check; /* Direct C function pointer - no Perl overhead */
5462 3 type->coerce = coerce; /* Direct C function pointer - no Perl overhead */
5463 3 type->perl_check = NULL;
5464 3 type->perl_coerce = NULL;
5465 3 type->check_ex = NULL;
5466 3 type->coerce_ex = NULL;
5467 3 type->data = NULL;
5468
5469 3 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
5470 3 }
5471  
5472 /* C-level registration with user-data for external XS modules (called from BOOT)
5473 Allows a single pair of C functions to serve multiple dynamically-created types */
5474 3 OBJECT_API void object_register_type_xs_ex(pTHX_ const char *name,
5475 ObjectTypeCheckFuncEx check,
5476 ObjectTypeCoerceFuncEx coerce,
5477 void *data) {
5478 RegisteredType *type;
5479 3 STRLEN name_len = strlen(name);
5480
5481 /*
5482 # uncoverable branch true
5483 # uncoverable condition left count:1 */
5484 3 ▶ if (!g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) { 0 3 3
5485 0 g_type_registry = newHV(); /* # uncoverable statement */
5486 }
5487
5488 3 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
5489 3 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 1 2 3
5490 1 croak("Type '%s' is already registered", name);
5491 }
5492
5493 2 Newxz(type, 1, RegisteredType);
5494 2 Newx(type->name, name_len + 1, char);
5495 2 Copy(name, type->name, name_len, char);
5496 2 type->name[name_len] = '\0';
5497
5498 2 type->check = NULL;
5499 2 type->coerce = NULL;
5500 2 type->perl_check = NULL;
5501 2 type->perl_coerce = NULL;
5502 2 type->check_ex = check;
5503 2 type->coerce_ex = coerce;
5504 2 type->data = data;
5505
5506 2 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
5507 2 }
5508  
5509 /* Getter for external modules to look up a registered type */
5510 2 OBJECT_API RegisteredType* object_get_registered_type(pTHX_ const char *name) {
5511 2 STRLEN name_len = strlen(name);
5512 /*
5513 # uncoverable branch true
5514 # uncoverable condition left count:1 */
5515 2 ▶ if (!g_type_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_type_registry) return NULL; 0 2 2
5516
5517 2 SV **svp = hv_fetch(g_type_registry, name, name_len, 0);
5518 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
5519 1 return INT2PTR(RegisteredType*, SvIV(*svp));
5520 }
5521 1 return NULL;
5522 }
5523  
5524 /* Object::Proto::register_type($name, $check_cb [, $coerce_cb]) */
5525 /* Object::Proto::register_simple_custom_op($xop_name) - let a custom op from
5526 * another module be used as an accessor operand without deoptimising the call.
5527 * The op must be free of side effects and leave exactly one value on the
5528 * stack; nothing here can check that, so registering is a promise. */
5529 16 XS_INTERNAL(xs_register_simple_custom_op) {
5530 16 dXSARGS;
5531 STRLEN len;
5532 const char *name;
5533 int i;
5534  
5535 16 ▶ if (items != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) 2 14 16
5536 2 croak("Usage: Object::Proto::register_simple_custom_op($xop_name)");
5537  
5538 14 name = SvPV(ST(0), len);
5539 14 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 2 12 14
5540 2 croak("register_simple_custom_op: name must not be empty");
5541  
5542 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
5543 44 ▶ if (strEQ(name, g_simple_custom_ops[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, g_simple_custom_ops[i])) 3 41 44
5544 3 XSRETURN_YES; /* already registered */
5545 }
5546  
5547 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
5548 1 croak("register_simple_custom_op: registry is full (max %d)",
5549 OBJECT_MAX_SIMPLE_CUSTOM);
5550  
5551 8 g_simple_custom_ops[g_simple_custom_op_count++] = savepvn(name, len);
5552 8 XSRETURN_YES;
5553 }
5554  
5555 19 XS_INTERNAL(xs_register_type) {
5556 19 dXSARGS;
5557 STRLEN name_len;
5558 const char *name;
5559 RegisteredType *type;
5560
5561 19 ▶ 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 18 19
5562
5563 18 name = SvPV(ST(0), name_len);
5564
5565 /* Check if already registered */
5566 /*
5567 # uncoverable branch false
5568 # uncoverable condition right count:1 */
5569 18 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 18 0 18
5570 18 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
5571 18 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 3 15 18
5572 3 croak("Type '%s' is already registered", name);
5573 }
5574 } else {
5575 0 g_type_registry = newHV(); /* # uncoverable statement */
5576 }
5577
5578 15 Newxz(type, 1, RegisteredType);
5579 15 Newx(type->name, name_len + 1, char);
5580 15 Copy(name, type->name, name_len, char);
5581 15 type->name[name_len] = '\0';
5582
5583 /* Store Perl check callback */
5584 15 type->perl_check = newSVsv(ST(1));
5585 15 SvREFCNT_inc(type->perl_check);
5586
5587 /* Store Perl coerce callback if provided */
5588 15 ▶ if (items > 2 && SvOK(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) { 5 10 15
if (items > 2 && SvOK(ST(2))) { 4 1 5
5589 4 type->perl_coerce = newSVsv(ST(2));
5590 4 SvREFCNT_inc(type->perl_coerce);
5591 }
5592
5593 15 hv_store(g_type_registry, name, name_len, newSViv(PTR2IV(type)), 0);
5594
5595 15 XSRETURN_YES;
5596 }
5597  
5598 /* Object::Proto::has_type($name) - check if a type is registered */
5599 4039 XS_INTERNAL(xs_has_type) {
5600 4039 dXSARGS;
5601 STRLEN name_len;
5602 const char *name;
5603
5604 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
5605
5606 4038 name = SvPV(ST(0), name_len);
5607
5608 /* Check built-in types */
5609 4038 BuiltinTypeID builtin = parse_builtin_type(name, name_len);
5610 4038 ▶ if (builtin != TYPE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin != TYPE_NONE) { 3017 1021 4038
5611 3017 XSRETURN_YES;
5612 }
5613
5614 /* Check registry */
5615 /*
5616 # uncoverable branch false
5617 # uncoverable condition right count:1 */
5618 1021 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 1021 0 1021
5619 1021 SV **existing = hv_fetch(g_type_registry, name, name_len, 0);
5620 1021 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 4 1017 1021
5621 4 XSRETURN_YES;
5622 }
5623 }
5624
5625 1017 XSRETURN_NO;
5626 }
5627  
5628 /* Object::Proto::list_types() - return list of registered type names */
5629 2405 XS_INTERNAL(xs_list_types) {
5630 2405 dXSARGS;
5631 2405 AV *result = newAV();
5632
5633 PERL_UNUSED_ARG(items);
5634
5635 /* Add built-in types */
5636 2405 av_push(result, newSVpvs("Any"));
5637 2405 av_push(result, newSVpvs("Defined"));
5638 2405 av_push(result, newSVpvs("Str"));
5639 2405 av_push(result, newSVpvs("Int"));
5640 2405 av_push(result, newSVpvs("Num"));
5641 2405 av_push(result, newSVpvs("Bool"));
5642 2405 av_push(result, newSVpvs("ArrayRef"));
5643 2405 av_push(result, newSVpvs("HashRef"));
5644 2405 av_push(result, newSVpvs("CodeRef"));
5645 2405 av_push(result, newSVpvs("Object"));
5646 2405 av_push(result, newSVpvs("Ref"));
5647 2405 av_push(result, newSVpvs("ScalarRef"));
5648 2405 av_push(result, newSVpvs("RegexpRef"));
5649 2405 av_push(result, newSVpvs("GlobRef"));
5650 2405 av_push(result, newSVpvs("FileHandle"));
5651 2405 av_push(result, newSVpvs("ClassName"));
5652 2405 av_push(result, newSVpvs("PositiveInt"));
5653 2405 av_push(result, newSVpvs("NegativeInt"));
5654 2405 av_push(result, newSVpvs("NonEmptyStr"));
5655 2405 av_push(result, newSVpvs("Undef"));
5656
5657 /* Add registered types */
5658 /*
5659 # uncoverable branch false
5660 # uncoverable condition right count:1 */
5661 2405 ▶ if (g_type_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_type_registry) { 2405 0 2405
5662 HE *he;
5663 2405 hv_iterinit(g_type_registry);
5664 2413 ▶ while ((he = hv_iternext(g_type_registry))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(g_type_registry))) { 8 2405 2413
5665 /* A registered type that shadows a built-in is listed once */
5666 I32 key_len;
5667 8 const char *key = hv_iterkey(he, &key_len);
5668 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
5669 6 continue;
5670 2 av_push(result, newSVsv(hv_iterkeysv(he)));
5671 }
5672 }
5673
5674 2405 ST(0) = newRV_noinc((SV*)result);
5675 2405 sv_2mortal(ST(0));
5676 2405 XSRETURN(1);
5677 }
5678  
5679 /* ============================================
5680 Singleton support
5681 ============================================ */
5682  
5683 /* XS implementation of instance() method for singletons */
5684 8055 XS_INTERNAL(xs_singleton_instance) {
5685 8055 dXSARGS;
5686 8055 ClassMeta *meta = INT2PTR(ClassMeta*, CvXSUBANY(cv).any_iv);
5687  
5688 PERL_UNUSED_ARG(items);
5689  
5690 /*
5691 # uncoverable branch true
5692 # uncoverable condition left count:1 */
5693 8055 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 0 8055 8055
5694 0 croak("Singleton metadata not found"); /* # uncoverable statement */
5695 }
5696  
5697 /* Return cached instance if it exists */
5698 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
5699 8046 ST(0) = meta->singleton_instance;
5700 8046 XSRETURN(1);
5701 }
5702  
5703 /* Create new instance */
5704 {
5705 9 dSP;
5706 int count;
5707 SV *obj;
5708 GV *build_gv;
5709 char full_build[256];
5710  
5711 9 ENTER;
5712 9 SAVETMPS;
5713  
5714 /* Call ClassName->new() */
5715 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
5716 9 ▶ XPUSHs(sv_2mortal(newSVpv(meta->class_name, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(meta->class_name, 0))); 0 9 9
5717 9 PUTBACK;
5718  
5719 9 count = call_method("new", G_SCALAR);
5720  
5721 9 SPAGAIN;
5722  
5723 /*
5724 # uncoverable branch true
5725 # uncoverable condition left count:1 */
5726 9 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 9 9
5727 0 croak("Singleton new() did not return object"); /* # uncoverable statement */
5728 }
5729  
5730 9 obj = POPs;
5731 9 SvREFCNT_inc(obj); /* Keep the object alive */
5732  
5733 9 PUTBACK;
5734  
5735 /* Check for BUILD method and call it */
5736 9 snprintf(full_build, sizeof(full_build), "%s::BUILD", meta->class_name);
5737 9 build_gv = gv_fetchpv(full_build, 0, SVt_PVCV);
5738 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
5739 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
5740 5 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 5 5
5741 5 PUTBACK;
5742 5 call_method("BUILD", G_VOID | G_DISCARD);
5743 }
5744  
5745 /* Cache the instance */
5746 9 meta->singleton_instance = obj;
5747  
5748 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
5749 9 LEAVE;
5750  
5751 9 ST(0) = obj;
5752 9 XSRETURN(1);
5753 }
5754 }
5755  
5756 /* ============================================
5757 Role API
5758 ============================================ */
5759  
5760 /* Object::Proto::role("RoleName", @slot_specs) - define a role */
5761 36 XS_INTERNAL(xs_role) {
5762 36 dXSARGS;
5763 STRLEN role_len;
5764 const char *role_pv;
5765 RoleMeta *meta;
5766 IV i;
5767
5768 36 ▶ 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 35 36
5769
5770 35 role_pv = SvPV(ST(0), role_len);
5771
5772 /* Check if role already exists */
5773 35 meta = get_role_meta(aTHX_ role_pv, role_len);
5774 35 ▶ if (meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta) { 1 34 35
5775 1 croak("Role '%s' already defined", role_pv);
5776 }
5777
5778 /* Create role meta */
5779 34 Newxz(meta, 1, RoleMeta);
5780 34 Newxz(meta->role_name, role_len + 1, char);
5781 34 Copy(role_pv, meta->role_name, role_len, char);
5782 34 meta->role_name[role_len] = '\0';
5783 34 meta->stash = gv_stashpvn(role_pv, role_len, GV_ADD);
5784
5785 /* Allocate slots array */
5786 34 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 23 11 34
5787 23 Newx(meta->slots, items - 1, SlotSpec*);
5788 23 meta->slot_count = 0;
5789
5790 66 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 44 22 66
5791 STRLEN spec_len;
5792 44 const char *spec_pv = SvPV(ST(i), spec_len);
5793 44 SlotSpec *spec = parse_slot_spec(aTHX_ spec_pv, spec_len);
5794 43 spec->private_owner = savepvn(role_pv, role_len);
5795 43 meta->slots[meta->slot_count++] = spec;
5796 }
5797 }
5798
5799 33 register_role_meta(aTHX_ role_pv, role_len, meta);
5800
5801 33 XSRETURN_EMPTY;
5802 }
5803  
5804 /* Object::Proto::requires("RoleName", @method_names) - declare required methods */
5805 14 XS_INTERNAL(xs_requires) {
5806 14 dXSARGS;
5807 STRLEN role_len;
5808 const char *role_pv;
5809 RoleMeta *meta;
5810 IV i;
5811
5812 14 ▶ 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 13 14
5813
5814 13 role_pv = SvPV(ST(0), role_len);
5815 13 meta = get_role_meta(aTHX_ role_pv, role_len);
5816 13 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 12 13
5817 1 croak("Role '%s' not defined", role_pv);
5818 }
5819
5820 /* Add required methods */
5821 12 ▶ 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 12 12
5822 29 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 17 12 29
5823 STRLEN name_len;
5824 17 const char *name_pv = SvPV(ST(i), name_len);
5825 17 Newx(meta->required_methods[meta->required_count], name_len + 1, char);
5826 17 Copy(name_pv, meta->required_methods[meta->required_count], name_len, char);
5827 17 meta->required_methods[meta->required_count][name_len] = '\0';
5828 17 meta->required_count++;
5829 }
5830
5831 12 XSRETURN_EMPTY;
5832 }
5833  
5834 /* Object::Proto::with("ClassName", @role_names) - apply roles to a class */
5835 31 XS_INTERNAL(xs_with) {
5836 31 dXSARGS;
5837 STRLEN class_len;
5838 const char *class_pv;
5839 ClassMeta *class_meta;
5840 IV i;
5841
5842 31 ▶ 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 30 31
5843
5844 30 class_pv = SvPV(ST(0), class_len);
5845 30 class_meta = get_class_meta(aTHX_ class_pv, class_len);
5846 30 ▶ if (!class_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!class_meta) { 1 29 30
5847 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
5848 }
5849
5850 51 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 31 20 51
5851 STRLEN role_len;
5852 31 const char *role_pv = SvPV(ST(i), role_len);
5853 31 RoleMeta *role_meta = get_role_meta(aTHX_ role_pv, role_len);
5854
5855 31 ▶ if (!role_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!role_meta) { 3 28 31
5856 /* Auto-load the role module */
5857 3 SV *module_sv = newSVpvn(role_pv, role_len);
5858 SV *err;
5859 3 load_module(PERL_LOADMOD_NOIMPORT, module_sv, NULL);
5860 2 ▶ err = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
err = ERRSV; 2 0 2
5861 /*
5862 # uncoverable branch true
5863 # uncoverable condition left count:1 */
5864 2 ▶ if (SvTRUE(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(err)) { 0 2 2
5865 0 croak("Role '%s' not defined (failed to load: %" SVf ")", role_pv, SVfARG(err)); /* # uncoverable statement */
5866 }
5867 2 role_meta = get_role_meta(aTHX_ role_pv, role_len);
5868 }
5869 30 ▶ if (!role_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!role_meta) { 1 29 30
5870 1 croak("Role '%s' not defined", role_pv);
5871 }
5872
5873 29 apply_role_to_class(aTHX_ class_meta, role_meta);
5874 }
5875
5876 20 XSRETURN_EMPTY;
5877 }
5878  
5879 /* Object::Proto::does("ClassName" or $obj, "RoleName") - check if class/object does role */
5880 11 XS_INTERNAL(xs_does) {
5881 11 dXSARGS;
5882 ClassMeta *meta;
5883 STRLEN role_len;
5884 const char *role_pv;
5885 IV i;
5886
5887 11 ▶ 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 10 11
5888
5889 /* Get class meta from class name or object */
5890 10 ▶ if (SvROK(ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ST(0))) { 7 3 10
5891 /* Object - get stash name */
5892 7 HV *stash = SvSTASH(SvRV(ST(0)));
5893 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
5894 } else {
5895 STRLEN class_len;
5896 3 const char *class_pv = SvPV(ST(0), class_len);
5897 3 meta = get_class_meta(aTHX_ class_pv, class_len);
5898 }
5899
5900 10 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 9 10
5901 1 XSRETURN_NO;
5902 }
5903
5904 9 role_pv = SvPV(ST(1), role_len);
5905
5906 /* Check if role is in consumed_roles */
5907 12 ▶ for (i = 0; i < meta->role_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < meta->role_count; i++) { 11 1 12
5908 11 ▶ if (strEQ(meta->consumed_roles[i]->role_name, role_pv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(meta->consumed_roles[i]->role_name, role_pv)) { 8 3 11
5909 8 XSRETURN_YES;
5910 }
5911 }
5912
5913 1 XSRETURN_NO;
5914 }
5915  
5916 /* ============================================
5917 Method Modifier API
5918 ============================================ */
5919  
5920 /* Object::Proto::before("Class::method", \&callback) */
5921 13 XS_INTERNAL(xs_before) {
5922 13 dXSARGS;
5923 STRLEN full_name_len;
5924 const char *full_name;
5925 char *class_name, *method_name, *sep;
5926 ClassMeta *meta;
5927
5928 13 ▶ 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 12 13
5929
5930 12 full_name = SvPV(ST(0), full_name_len);
5931 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
5932 2 croak("Second argument must be a code reference");
5933 }
5934
5935 /* Parse "Class::method" */
5936 10 sep = strstr(full_name, "::");
5937 10 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 9 10
5938 1 croak("Method name must be fully qualified (Class::method)");
5939 }
5940
5941 {
5942 9 STRLEN class_len = sep - full_name;
5943 9 Newx(class_name, class_len + 1, char);
5944 9 Copy(full_name, class_name, class_len, char);
5945 9 class_name[class_len] = '\0';
5946 9 method_name = sep + 2;
5947 }
5948
5949 9 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
5950 9 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 8 9
5951 1 Safefree(class_name);
5952 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
5953 }
5954
5955 8 add_modifier(aTHX_ meta, method_name, ST(1), 0); /* 0 = before */
5956
5957 8 Safefree(class_name);
5958 8 XSRETURN_EMPTY;
5959 }
5960  
5961 /* Object::Proto::after("Class::method", \&callback) */
5962 14 XS_INTERNAL(xs_after) {
5963 14 dXSARGS;
5964 STRLEN full_name_len;
5965 const char *full_name;
5966 char *class_name, *method_name, *sep;
5967 ClassMeta *meta;
5968
5969 14 ▶ 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 13 14
5970
5971 13 full_name = SvPV(ST(0), full_name_len);
5972 13 ▶ if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 12 1 13
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV) { 1 11 12
5973 2 croak("Second argument must be a code reference");
5974 }
5975
5976 11 sep = strstr(full_name, "::");
5977 11 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 10 11
5978 1 croak("Method name must be fully qualified (Class::method)");
5979 }
5980
5981 {
5982 10 STRLEN class_len = sep - full_name;
5983 10 Newx(class_name, class_len + 1, char);
5984 10 Copy(full_name, class_name, class_len, char);
5985 10 class_name[class_len] = '\0';
5986 10 method_name = sep + 2;
5987 }
5988
5989 10 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
5990 10 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 9 10
5991 1 Safefree(class_name);
5992 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
5993 }
5994
5995 9 add_modifier(aTHX_ meta, method_name, ST(1), 1); /* 1 = after */
5996
5997 9 Safefree(class_name);
5998 9 XSRETURN_EMPTY;
5999 }
6000  
6001 /* Object::Proto::around("Class::method", \&callback) */
6002 11 XS_INTERNAL(xs_around) {
6003 11 dXSARGS;
6004 STRLEN full_name_len;
6005 const char *full_name;
6006 char *class_name, *method_name, *sep;
6007 ClassMeta *meta;
6008
6009 11 ▶ 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 10 11
6010
6011 10 full_name = SvPV(ST(0), full_name_len);
6012 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
6013 2 croak("Second argument must be a code reference");
6014 }
6015
6016 8 sep = strstr(full_name, "::");
6017 8 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 1 7 8
6018 1 croak("Method name must be fully qualified (Class::method)");
6019 }
6020
6021 {
6022 7 STRLEN class_len = sep - full_name;
6023 7 Newx(class_name, class_len + 1, char);
6024 7 Copy(full_name, class_name, class_len, char);
6025 7 class_name[class_len] = '\0';
6026 7 method_name = sep + 2;
6027 }
6028
6029 7 meta = get_class_meta(aTHX_ class_name, strlen(class_name));
6030 7 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 6 7
6031 1 Safefree(class_name);
6032 1 croak("Class '%.*s' not defined with Object::Proto::define", (int)(sep - full_name), full_name);
6033 }
6034
6035 6 add_modifier(aTHX_ meta, method_name, ST(1), 2); /* 2 = around */
6036
6037 6 Safefree(class_name);
6038 6 XSRETURN_EMPTY;
6039 }
6040  
6041 /* Object::Proto::singleton("Class") - marks class as singleton and installs instance() method */
6042 11 XS_INTERNAL(xs_singleton) {
6043 11 dXSARGS;
6044 STRLEN class_len;
6045 const char *class_pv;
6046 ClassMeta *meta;
6047 char full_name[256];
6048 CV *instance_cv;
6049  
6050 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
6051  
6052 10 class_pv = SvPV(ST(0), class_len);
6053  
6054 10 meta = get_class_meta(aTHX_ class_pv, class_len);
6055 10 ▶ if (!meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (!meta) { 1 9 10
6056 1 croak("Class '%s' not defined with Object::Proto::define", class_pv);
6057 }
6058  
6059 /* Mark as singleton */
6060 9 meta->is_singleton = 1;
6061 9 meta->singleton_instance = NULL;
6062  
6063 /* Install instance() class method */
6064 9 snprintf(full_name, sizeof(full_name), "%s::instance", class_pv);
6065 9 instance_cv = newXS(full_name, xs_singleton_instance, __FILE__);
6066 9 CvXSUBANY(instance_cv).any_iv = PTR2IV(meta);
6067  
6068 9 XSRETURN_EMPTY;
6069 }
6070  
6071 /* ============================================
6072 Boot
6073 ============================================ */
6074  
6075 90 XS_EXTERNAL(boot_Object__Proto) {
6076 90 dXSBOOTARGSXSAPIVERCHK;
6077 PERL_UNUSED_VAR(items);
6078  
6079 /* Register custom ops */
6080 90 XopENTRY_set(&object_get_xop, xop_name, "object_get");
6081 90 XopENTRY_set(&object_get_xop, xop_desc, "object property get");
6082 90 XopENTRY_set(&object_get_xop, xop_class, OA_UNOP);
6083 90 Perl_custom_op_register(aTHX_ pp_object_get, &object_get_xop);
6084
6085 90 XopENTRY_set(&object_set_xop, xop_name, "object_set");
6086 90 XopENTRY_set(&object_set_xop, xop_desc, "object property set");
6087 90 XopENTRY_set(&object_set_xop, xop_class, OA_BINOP);
6088 90 Perl_custom_op_register(aTHX_ pp_object_set, &object_set_xop);
6089  
6090 90 XopENTRY_set(&object_set_typed_xop, xop_name, "object_set_typed");
6091 90 XopENTRY_set(&object_set_typed_xop, xop_desc, "object property set with type check");
6092 90 XopENTRY_set(&object_set_typed_xop, xop_class, OA_BINOP);
6093 90 Perl_custom_op_register(aTHX_ pp_object_set_typed, &object_set_typed_xop);
6094  
6095 90 XopENTRY_set(&object_func_get_xop, xop_name, "object_func_get");
6096 90 XopENTRY_set(&object_func_get_xop, xop_desc, "object function-style get");
6097 90 XopENTRY_set(&object_func_get_xop, xop_class, OA_UNOP);
6098 90 Perl_custom_op_register(aTHX_ pp_object_func_get, &object_func_get_xop);
6099
6100 90 XopENTRY_set(&object_func_set_xop, xop_name, "object_func_set");
6101 90 XopENTRY_set(&object_func_set_xop, xop_desc, "object function-style set");
6102 90 XopENTRY_set(&object_func_set_xop, xop_class, OA_BINOP);
6103 90 Perl_custom_op_register(aTHX_ pp_object_func_set, &object_func_set_xop);
6104  
6105 /* Initialize registries */
6106 90 g_class_registry = newHV();
6107 90 g_type_registry = newHV();
6108  
6109 /* handles(...) lookups */
6110 {
6111 const char *const *r;
6112 90 g_reserved_methods = newHV();
6113 1800 ▶ for (r = reserved_methods; *r; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = reserved_methods; *r; r++) { 1710 90 1800
6114 1710 (void)hv_store(g_reserved_methods, *r, strlen(*r), &PL_sv_yes, 0);
6115 }
6116 90 g_array_op_index = build_op_index(aTHX_ array_ops);
6117 90 g_hash_op_index = build_op_index(aTHX_ hash_ops);
6118 }
6119  
6120 /* Install XS functions */
6121 90 newXS("Object::Proto::import", xs_import, __FILE__);
6122 90 newXS("Object::Proto::define", xs_define, __FILE__);
6123 90 newXS("Object::Proto::import_accessors", xs_import_accessors, __FILE__);
6124 90 newXS("Object::Proto::import_accessor", xs_import_accessor, __FILE__);
6125 90 newXS("Object::Proto::prototype", xs_prototype, __FILE__);
6126 90 newXS("Object::Proto::set_prototype", xs_set_prototype, __FILE__);
6127 90 newXS("Object::Proto::prototype_chain", xs_prototype_chain, __FILE__);
6128 90 newXS("Object::Proto::has_own_property", xs_has_own_property, __FILE__);
6129 90 newXS("Object::Proto::prototype_depth", xs_prototype_depth, __FILE__);
6130 90 newXS("Object::Proto::lock", xs_lock, __FILE__);
6131 90 newXS("Object::Proto::unlock", xs_unlock, __FILE__);
6132 90 newXS("Object::Proto::freeze", xs_freeze, __FILE__);
6133 90 newXS("Object::Proto::is_frozen", xs_is_frozen, __FILE__);
6134 90 newXS("Object::Proto::is_locked", xs_is_locked, __FILE__);
6135  
6136 /* Introspection API */
6137 90 newXS("Object::Proto::clone", xs_clone, __FILE__);
6138 90 newXS("Object::Proto::properties", xs_properties, __FILE__);
6139 90 newXS("Object::Proto::slot_info", xs_slot_info, __FILE__);
6140  
6141 /* Inheritance API */
6142 90 newXS("Object::Proto::parent", xs_parent, __FILE__);
6143 90 newXS("Object::Proto::ancestors", xs_ancestors, __FILE__);
6144  
6145 /* Simple custom op registry */
6146 90 newXS("Object::Proto::register_simple_custom_op", xs_register_simple_custom_op, __FILE__);
6147  
6148 /* Type registry API */
6149 90 newXS("Object::Proto::register_type", xs_register_type, __FILE__);
6150 90 newXS("Object::Proto::has_type", xs_has_type, __FILE__);
6151 90 newXS("Object::Proto::list_types", xs_list_types, __FILE__);
6152  
6153 /* Singleton support */
6154 90 newXS("Object::Proto::singleton", xs_singleton, __FILE__);
6155
6156 /* Role API */
6157 90 newXS("Object::Proto::role", xs_role, __FILE__);
6158 90 newXS("Object::Proto::requires", xs_requires, __FILE__);
6159 90 newXS("Object::Proto::with", xs_with, __FILE__);
6160 90 newXS("Object::Proto::does", xs_does, __FILE__);
6161
6162 /* Method modifier API */
6163 90 newXS("Object::Proto::before", xs_before, __FILE__);
6164 90 newXS("Object::Proto::after", xs_after, __FILE__);
6165 90 newXS("Object::Proto::around", xs_around, __FILE__);
6166  
6167 /* Register cleanup for global destruction */
6168 90 Perl_call_atexit(aTHX_ object_cleanup_globals, NULL);
6169  
6170 90 OBJECT_PROTO_XS_BOOT_EPILOG(ax);
6171 90 }
« blib/arch/Object/Proto/Install/Files.pm ↑ Summary blib/lib/Object/Proto.pm »