Legba.xs

filter: Statementstmt 92.8% 587 / 632 Branchbran 60.9% 268 / 440 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.7% 855 / 1072 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Legba.pm »
1 /*
2 * Legba.xs - Ultra-fast global slot storage for Perl
3 *
4 * Named after Papa Legba, the Vodou gatekeeper of crossroads.
5 *
6 * Architecture ported from Ancient/slot (slot.c):
7 * - Plain SV** array with realloc; no PVX-buffer trick
8 * - Slot index stored in op_targ (standard OP field), not embedded as SV*
9 * in a custom struct — no dangling pointer risk on registry resize
10 * - Reactive watchers (optional, zero overhead without them)
11 *
12 * Legba extensions over Ancient/slot:
13 * - lock/freeze access control per slot
14 * - Dedicated SV* per slot (mutated via sv_setsv) so _slot_ptr remains
15 * stable across value changes
16 * - _slot_ptr / _make_get_op / _make_set_op for external op building
17 * - _registry introspection
18 */
19  
20 #define PERL_NO_GET_CONTEXT
21 #include "EXTERN.h"
22 #include "perl.h"
23 #include "XSUB.h"
24 #include "ppport.h"
25  
26 /* ============================================
27 Compatibility macros
28 ============================================ */
29  
30 #ifndef PERL_VERSION_GE
31 # define PERL_VERSION_GE(r,v,s) \
32 (PERL_REVISION > (r) || (PERL_REVISION == (r) && \
33 (PERL_VERSION > (v) || (PERL_VERSION == (v) && PERL_SUBVERSION >= (s)))))
34 #endif
35  
36 #ifndef OpHAS_SIBLING
37 # define OpHAS_SIBLING(o) ((o)->op_sibling != NULL)
38 #endif
39 #ifndef OpSIBLING
40 # define OpSIBLING(o) ((o)->op_sibling)
41 #endif
42 #ifndef OpMORESIB_set
43 # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib))
44 #endif
45 #ifndef OpLASTSIB_set
46 # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL)
47 #endif
48 #ifndef SvREFCNT_inc_simple_NN
49 # define SvREFCNT_inc_simple_NN(sv) SvREFCNT_inc(sv)
50 #endif
51 #ifndef SvREFCNT_dec_NN
52 # define SvREFCNT_dec_NN(sv) SvREFCNT_dec(sv)
53 #endif
54  
55 /* PADNAMEf_CONST - compile-time constant flag (0x40 unused in standard perl) */
56 #ifndef PADNAMEf_CONST
57 # define PADNAMEf_CONST 0x40
58 #endif
59  
60 #if !PERL_VERSION_GE(5,18,0)
61 typedef SV PADNAME;
62 # define PadnamelistMAX(pn) (AvFILLp(pn))
63 # define PadnamelistARRAY(pn) ((PADNAME**)AvARRAY(pn))
64 # define PadnameFLAGS(pn) (SvFLAGS(pn))
65 # undef PADNAMEf_CONST
66 # define PADNAMEf_CONST 0
67 #elif !PERL_VERSION_GE(5,22,0)
68 # ifndef PadnameFLAGS
69 # define PadnameFLAGS(pn) (SvFLAGS((SV*)(pn)))
70 # endif
71 #endif
72  
73 /* cv_set_call_checker - 5.14+ only */
74 #if !PERL_VERSION_GE(5,14,0)
75 # define cv_set_call_checker(cv, checker, ckobj) /* no-op on pre-5.14 */
76 #endif
77  
78 /* XOP API - 5.14+ */
79 #if PERL_VERSION_GE(5,14,0)
80 # define LEGBA_HAS_XOP 1
81 #else
82 # define LEGBA_HAS_XOP 0
83 # ifndef XOP_DEFINED_BY_COMPAT
84 # define XOP_DEFINED_BY_COMPAT 1
85 typedef struct { const char *xop_name; const char *xop_desc; } XOP;
86 # endif
87 # ifndef XopENTRY_set
88 # define XopENTRY_set(xop, field, value) do { (xop)->field = (value); } while(0)
89 # endif
90 # ifdef PERL_IMPLICIT_CONTEXT
91 # define Perl_custom_op_register(ctx, ppfunc, xop) \
92 legba_compat_reg_xop((ctx), (Perl_ppaddr_t)(ppfunc), (xop)->xop_name, (xop)->xop_desc)
93 # else
94 # define Perl_custom_op_register(ppfunc, xop) \
95 legba_compat_reg_xop(aTHX_ (Perl_ppaddr_t)(ppfunc), (xop)->xop_name, (xop)->xop_desc)
96 # endif
97 static void legba_compat_reg_xop(pTHX_ Perl_ppaddr_t ppfunc, const char *name, const char *desc) {
98 if (!PL_custom_op_names) PL_custom_op_names = newHV();
99 if (!PL_custom_op_descs) PL_custom_op_descs = newHV();
100 hv_store(PL_custom_op_names, (char*)&ppfunc, sizeof(ppfunc), newSVpv(name, 0), 0);
101 hv_store(PL_custom_op_descs, (char*)&ppfunc, sizeof(ppfunc), newSVpv(desc, 0), 0);
102 }
103 #endif /* LEGBA_HAS_XOP */
104  
105 #ifndef dXSBOOTARGSXSAPIVERCHK
106 # define dXSBOOTARGSXSAPIVERCHK dXSARGS
107 #endif
108 #if !PERL_VERSION_GE(5,22,0)
109 # ifndef Perl_xs_boot_epilog
110 # ifdef PERL_IMPLICIT_CONTEXT
111 # define Perl_xs_boot_epilog(ctx, ax) XSRETURN_YES
112 # else
113 # define Perl_xs_boot_epilog(ax) XSRETURN_YES
114 # endif
115 # endif
116 #endif
117 #ifndef XS_EXTERNAL
118 # define XS_EXTERNAL(name) XS(name)
119 #endif
120  
121 /* ============================================
122 Globals
123 ============================================ */
124  
125 /*
126 * Each g_slots[i] is a dedicated SV* allocated once at slot creation and
127 * mutated via sv_setsv for value changes. The pointer itself never changes,
128 * so PTR2UV(g_slots[i]) is stable — required for _slot_ptr compatibility.
129 */
130 static SV **g_slots = NULL;
131 static IV g_slots_size = 0;
132 static IV g_slots_count= 0;
133 static char *g_has_watchers= NULL;
134  
135 /* Per-slot access-control flags (lock / freeze) */
136 static UV *slot_flags = NULL;
137 #define SLOT_FLAG_LOCKED 0x1
138 #define SLOT_FLAG_FROZEN 0x2
139  
140 /* Name <-> index mappings */
141 static HV *g_slot_index = NULL; /* slot name -> IV index */
142 static HV *g_slot_names = NULL; /* "N" (idx) -> slot name */
143 static HV *g_watchers = NULL; /* slot name -> AV of callbacks */
144  
145 /* Custom op type descriptors */
146 #if LEGBA_HAS_XOP
147 static XOP legba_get_xop;
148 static XOP legba_set_xop;
149 static XOP legba_watch_xop;
150 static XOP legba_unwatch_xop;
151 static XOP legba_unwatch_one_xop;
152 static XOP legba_clear_xop;
153 #endif
154  
155 /* Forward declaration */
156 static void fire_watchers(pTHX_ IV idx, SV *new_val);
157  
158 /* ============================================
159 Custom op pp functions
160 ============================================ */
161  
162 10403 static OP* pp_slot_get(pTHX) {
163 10403 dSP;
164 10403 IV idx = PL_op->op_targ;
165 #ifdef DEBUGGING
166 EXTEND(SP, 1);
167 #endif
168 10403 PUSHs(g_slots[idx]);
169 10403 PUTBACK;
170 10403 return NORMAL;
171 }
172  
173 11494 static OP* pp_slot_set(pTHX) {
174 11494 dSP;
175 11494 IV idx = PL_op->op_targ;
176 11494 SV *new_val = TOPs;
177 11494 ▶ if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) { 6 11488 11494
178 6 ▶ croak("Attempt to set %s slot",
Branch: 1 branch
BranchTrueFalseTotal
croak("Attempt to set %s slot", 3 3 6
179 (slot_flags[idx] & SLOT_FLAG_FROZEN) ? "frozen" : "locked");
180 }
181 11488 sv_setsv(g_slots[idx], new_val);
182 11488 ▶ if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]);
Branch: 1 branch
BranchTrueFalseTotal
if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]); 5 11483 11488
183 11488 SETs(g_slots[idx]);
184 11488 PUTBACK;
185 11488 return NORMAL;
186 }
187  
188 6 static OP* pp_slot_watch(pTHX) {
189 6 dSP;
190 6 IV idx = PL_op->op_targ;
191 6 SV *callback = POPs;
192 char key[32];
193 6 int klen = snprintf(key, sizeof(key), "%ld", (long)idx);
194 6 SV **name_svp = hv_fetch(g_slot_names, key, klen, 0);
195 6 ▶ if (name_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_svp) { 6 0 6
196 STRLEN name_len;
197 6 const char *name = SvPV(*name_svp, name_len);
198 6 SV **existing = hv_fetch(g_watchers, name, name_len, 0);
199 AV *callbacks;
200 6 ▶ if (existing && SvROK(*existing)) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing && SvROK(*existing)) { 1 5 6
if (existing && SvROK(*existing)) { 1 0 1
201 1 callbacks = (AV*)SvRV(*existing);
202 } else {
203 5 callbacks = newAV();
204 5 hv_store(g_watchers, name, name_len, newRV_noinc((SV*)callbacks), 0);
205 }
206 6 av_push(callbacks, SvREFCNT_inc(callback));
207 6 g_has_watchers[idx] = 1;
208 }
209 6 RETURN;
210 }
211  
212 3 static OP* pp_slot_unwatch(pTHX) {
213 3 IV idx = PL_op->op_targ;
214 char key[32];
215 3 int klen = snprintf(key, sizeof(key), "%ld", (long)idx);
216 3 SV **name_svp = hv_fetch(g_slot_names, key, klen, 0);
217 3 ▶ if (name_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_svp) { 3 0 3
218 STRLEN name_len;
219 3 const char *name = SvPV(*name_svp, name_len);
220 3 hv_delete(g_watchers, name, name_len, G_DISCARD);
221 3 g_has_watchers[idx] = 0;
222 }
223 3 return NORMAL;
224 }
225  
226 0 static OP* pp_slot_unwatch_one(pTHX) {
227 0 dSP;
228 0 IV idx = PL_op->op_targ;
229 0 SV *callback = POPs;
230 char key[32];
231 0 int klen = snprintf(key, sizeof(key), "%ld", (long)idx);
232 0 SV **name_svp = hv_fetch(g_slot_names, key, klen, 0);
233 0 ▶ if (name_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_svp) { 0 0 0
234 STRLEN name_len;
235 0 const char *name = SvPV(*name_svp, name_len);
236 0 SV **existing = hv_fetch(g_watchers, name, name_len, 0);
237 0 ▶ if (existing && SvROK(*existing)) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing && SvROK(*existing)) { 0 0 0
if (existing && SvROK(*existing)) { 0 0 0
238 0 AV *callbacks = (AV*)SvRV(*existing);
239 0 SSize_t i, len = av_len(callbacks);
240 0 ▶ for (i = len; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i >= 0; i--) { 0 0 0
241 0 SV **cb = av_fetch(callbacks, i, 0);
242 0 ▶ if (cb && SvRV(*cb) == SvRV(callback))
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvRV(*cb) == SvRV(callback)) 0 0 0
if (cb && SvRV(*cb) == SvRV(callback)) 0 0 0
243 0 av_delete(callbacks, i, G_DISCARD);
244 }
245 0 ▶ if (av_len(callbacks) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(callbacks) < 0) 0 0 0
246 0 g_has_watchers[idx] = 0;
247 }
248 }
249 0 RETURN;
250 }
251  
252 3 static OP* pp_slot_clear(pTHX) {
253 3 IV idx = PL_op->op_targ;
254 char key[32];
255 3 int klen = snprintf(key, sizeof(key), "%ld", (long)idx);
256 3 SV **name_svp = hv_fetch(g_slot_names, key, klen, 0);
257 3 sv_setsv(g_slots[idx], &PL_sv_undef);
258 3 ▶ if (name_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_svp) { 3 0 3
259 STRLEN name_len;
260 3 const char *name = SvPV(*name_svp, name_len);
261 3 hv_delete(g_watchers, name, name_len, G_DISCARD);
262 }
263 3 g_has_watchers[idx] = 0;
264 3 return NORMAL;
265 }
266  
267 /* ============================================
268 Call checkers
269 ============================================ */
270  
271 /* Installed on each imported accessor CV.
272 * 0-arg call => getter custom op; 1-arg call => setter custom op. */
273 169 static OP* slot_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
274 169 CV *cv = (CV*)ckobj;
275 169 IV idx = CvXSUBANY(cv).any_iv;
276 OP *pushop, *cvop, *argop;
277 PERL_UNUSED_ARG(namegv);
278  
279 169 pushop = cUNOPx(entersubop)->op_first;
280 169 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 169 0 169
281 169 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 169 0 169
282 169 cvop = argop;
283 250 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 81 0 81
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 81 169 250
284  
285 169 ▶ if (argop == cvop) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop == cvop) { 88 81 169
286 /* Getter: no arguments */
287 88 OP *newop = newOP(OP_CUSTOM, 0);
288 88 newop->op_ppaddr = pp_slot_get;
289 88 newop->op_targ = idx;
290 88 op_free(entersubop);
291 88 return newop;
292 81 ▶ } else if (OpSIBLING(argop) == cvop) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (OpSIBLING(argop) == cvop) { 81 0 81
} else if (OpSIBLING(argop) == cvop) { 81 0 81
293 /* Setter: single argument */
294 81 OP *arg = argop;
295 OP *newop;
296 81 OpMORESIB_set(pushop, cvop);
297 81 OpLASTSIB_set(arg, NULL);
298 81 op_contextualize(arg, G_SCALAR);
299 81 newop = newUNOP(OP_NULL, 0, arg);
300 81 newop->op_type = OP_CUSTOM;
301 81 newop->op_ppaddr = pp_slot_set;
302 81 newop->op_targ = idx;
303 81 op_free(entersubop);
304 81 return newop;
305 }
306 0 return entersubop;
307 }
308  
309 /* Shared helper: extract constant string namesv from single-arg call */
310 32 static SV* get_const_namesv_1arg(pTHX_ OP *entersubop) {
311 OP *pushop, *cvop, *argop;
312 32 pushop = cUNOPx(entersubop)->op_first;
313 32 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 32 0 32
314 32 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 32 0 32
315 32 cvop = argop;
316 64 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 32 0 32
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 32 32 64
317 32 ▶ if (argop == cvop || OpSIBLING(argop) != cvop) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (argop == cvop || OpSIBLING(argop) != cvop) return NULL; 32 0 32
if (argop == cvop || OpSIBLING(argop) != cvop) return NULL; 32 0 32
if (argop == cvop || OpSIBLING(argop) != cvop) return NULL; 0 32 32
318 32 ▶ if (argop->op_type == OP_CONST)
Branch: 1 branch
BranchTrueFalseTotal
if (argop->op_type == OP_CONST) 29 3 32
319 29 return cSVOPx(argop)->op_sv;
320 3 ▶ if (argop->op_type == OP_PADSV) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop->op_type == OP_PADSV) { 3 0 3
321 3 PADOFFSET po = argop->op_targ;
322 3 ▶ if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) { 3 0 3
if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) { 3 0 3
323 3 PADNAME *pn = PadnamelistARRAY(PL_comppad_name)[po];
324 3 ▶ if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) {
Branch: 3 branches
BranchTrueFalseTotal
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 3 0 3
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 0 3 3
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 0 0 0
325 0 SV **svp = av_fetch(PL_comppad, po, 0);
326 0 ▶ if (svp && SvPOK(*svp)) return *svp;
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) return *svp; 0 0 0
if (svp && SvPOK(*svp)) return *svp; 0 0 0
327 }
328 }
329 }
330 3 return NULL;
331 }
332  
333 /* get('constant') / _get('constant') => getter custom op */
334 32 static OP* slot_get_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
335 SV *namesv;
336 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
337 32 namesv = get_const_namesv_1arg(aTHX_ entersubop);
338 32 ▶ if (namesv && SvPOK(namesv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (namesv && SvPOK(namesv)) { 29 3 32
if (namesv && SvPOK(namesv)) { 29 0 29
339 STRLEN name_len;
340 29 const char *name = SvPV(namesv, name_len);
341 29 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
342 29 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 14 15 29
343 14 OP *newop = newOP(OP_CUSTOM, 0);
344 14 newop->op_ppaddr = pp_slot_get;
345 14 newop->op_targ = SvIV(*svp);
346 14 op_free(entersubop);
347 14 return newop;
348 }
349 }
350 18 return entersubop;
351 }
352  
353 /* set('constant', $val) / _set('constant', $val) => setter custom op */
354 44 static OP* slot_set_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
355 OP *pushop, *cvop, *argop, *valop;
356 44 SV *namesv = NULL;
357 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
358  
359 44 pushop = cUNOPx(entersubop)->op_first;
360 44 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 44 0 44
361 44 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 44 0 44
362 44 cvop = argop;
363 132 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 88 0 88
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 88 44 132
364 44 ▶ valop = OpSIBLING(argop);
Branch: 1 branch
BranchTrueFalseTotal
valop = OpSIBLING(argop); 44 0 44
365 44 ▶ if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop)
Branch: 5 branches
BranchTrueFalseTotal
if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop) 44 0 44
if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop) 44 0 44
if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop) 44 0 44
if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop) 44 0 44
if (argop == cvop || !valop || valop == cvop || OpSIBLING(valop) != cvop) 0 44 44
366 0 return entersubop;
367  
368 44 ▶ if (argop->op_type == OP_CONST) {
Branch: 1 branch
BranchTrueFalseTotal
if (argop->op_type == OP_CONST) { 42 2 44
369 42 namesv = cSVOPx(argop)->op_sv;
370 2 ▶ } else if (argop->op_type == OP_PADSV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (argop->op_type == OP_PADSV) { 2 0 2
371 2 PADOFFSET po = argop->op_targ;
372 2 ▶ if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) { 2 0 2
if (PL_comppad_name && po < (PADOFFSET)(PadnamelistMAX(PL_comppad_name) + 1)) { 2 0 2
373 2 PADNAME *pn = PadnamelistARRAY(PL_comppad_name)[po];
374 2 ▶ if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) {
Branch: 3 branches
BranchTrueFalseTotal
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 2 0 2
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 0 2 2
if (pn && (PadnameFLAGS(pn) & PADNAMEf_CONST) && PL_comppad) { 0 0 0
375 0 SV **svp = av_fetch(PL_comppad, po, 0);
376 0 ▶ if (svp && SvPOK(*svp)) namesv = *svp;
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) namesv = *svp; 0 0 0
if (svp && SvPOK(*svp)) namesv = *svp; 0 0 0
377 }
378 }
379 }
380 44 ▶ if (namesv && SvPOK(namesv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (namesv && SvPOK(namesv)) { 42 2 44
if (namesv && SvPOK(namesv)) { 42 0 42
381 STRLEN name_len;
382 42 const char *name = SvPV(namesv, name_len);
383 42 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
384 42 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 15 27 42
385 OP *newop;
386 15 OpMORESIB_set(pushop, cvop);
387 15 OpLASTSIB_set(valop, NULL);
388 15 op_contextualize(valop, G_SCALAR);
389 15 newop = newUNOP(OP_NULL, 0, valop);
390 15 newop->op_type = OP_CUSTOM;
391 15 newop->op_ppaddr = pp_slot_set;
392 15 newop->op_targ = SvIV(*svp);
393 15 op_free(entersubop);
394 15 return newop;
395 }
396 }
397 29 return entersubop;
398 }
399  
400 /* index('constant') => OP_CONST (fully constant-folded) */
401 14 static OP* slot_index_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
402 OP *pushop, *cvop, *argop;
403 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
404 14 pushop = cUNOPx(entersubop)->op_first;
405 14 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 14 0 14
406 14 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 14 0 14
407 14 cvop = argop;
408 28 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 14 0 14
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 14 14 28
409 14 ▶ if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) {
Branch: 4 branches
BranchTrueFalseTotal
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 14 0 14
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 14 0 14
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 14 0 14
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 13 1 14
410 13 SV *namesv = cSVOPx(argop)->op_sv;
411 13 ▶ if (SvPOK(namesv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(namesv)) { 13 0 13
412 STRLEN name_len;
413 13 const char *name = SvPV(namesv, name_len);
414 13 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
415 13 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 6 7 13
416 6 OP *newop = newSVOP(OP_CONST, 0, newSViv(SvIV(*svp)));
417 6 op_free(entersubop);
418 6 return newop;
419 }
420 }
421 }
422 8 return entersubop;
423 }
424  
425 /* watch('constant', $cb) => watch custom op */
426 15 static OP* slot_watch_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
427 OP *pushop, *cvop, *argop, *cbop;
428 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
429 15 pushop = cUNOPx(entersubop)->op_first;
430 15 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 15 0 15
431 15 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 15 0 15
432 15 cvop = argop;
433 45 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 30 0 30
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 30 15 45
434 15 ▶ cbop = OpSIBLING(argop);
Branch: 1 branch
BranchTrueFalseTotal
cbop = OpSIBLING(argop); 15 0 15
435 15 ▶ if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop
Branch: 5 branches
BranchTrueFalseTotal
if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop 15 0 15
if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop 15 0 15
if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop 15 0 15
if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop 15 0 15
if (argop != cvop && cbop && cbop != cvop && OpSIBLING(cbop) == cvop 15 0 15
436 15 ▶ && argop->op_type == OP_CONST) {
Branch: 1 branch
BranchTrueFalseTotal
&& argop->op_type == OP_CONST) { 15 0 15
437 15 SV *namesv = cSVOPx(argop)->op_sv;
438 15 ▶ if (SvPOK(namesv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(namesv)) { 15 0 15
439 STRLEN name_len;
440 15 const char *name = SvPV(namesv, name_len);
441 15 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
442 15 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 6 9 15
443 OP *newop;
444 6 OpMORESIB_set(pushop, cvop);
445 6 OpLASTSIB_set(cbop, NULL);
446 6 newop = newUNOP(OP_NULL, 0, cbop);
447 6 newop->op_type = OP_CUSTOM;
448 6 newop->op_ppaddr = pp_slot_watch;
449 6 newop->op_targ = SvIV(*svp);
450 6 op_free(entersubop);
451 6 return newop;
452 }
453 }
454 }
455 9 return entersubop;
456 }
457  
458 /* unwatch('constant') or unwatch('constant', $cb) */
459 5 static OP* slot_unwatch_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
460 OP *pushop, *cvop, *argop, *cbop;
461 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
462 5 pushop = cUNOPx(entersubop)->op_first;
463 5 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 5 0 5
464 5 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 5 0 5
465 5 cvop = argop;
466 11 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 6 0 6
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 6 5 11
467 5 ▶ if (argop != cvop && argop->op_type == OP_CONST) {
Branch: 2 branches
BranchTrueFalseTotal
if (argop != cvop && argop->op_type == OP_CONST) { 5 0 5
if (argop != cvop && argop->op_type == OP_CONST) { 5 0 5
468 5 SV *namesv = cSVOPx(argop)->op_sv;
469 5 ▶ if (SvPOK(namesv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(namesv)) { 5 0 5
470 STRLEN name_len;
471 5 const char *name = SvPV(namesv, name_len);
472 5 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
473 5 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 3 2 5
474 3 IV idx = SvIV(*svp);
475 3 ▶ cbop = OpSIBLING(argop);
Branch: 1 branch
BranchTrueFalseTotal
cbop = OpSIBLING(argop); 3 0 3
476 3 ▶ if (cbop == cvop) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbop == cvop) { 3 0 3
477 3 OP *newop = newOP(OP_CUSTOM, 0);
478 3 newop->op_ppaddr = pp_slot_unwatch;
479 3 newop->op_targ = idx;
480 3 op_free(entersubop);
481 3 return newop;
482 0 ▶ } else if (OpSIBLING(cbop) == cvop) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (OpSIBLING(cbop) == cvop) { 0 0 0
} else if (OpSIBLING(cbop) == cvop) { 0 0 0
483 OP *newop;
484 0 OpMORESIB_set(pushop, cvop);
485 0 OpLASTSIB_set(cbop, NULL);
486 0 newop = newUNOP(OP_NULL, 0, cbop);
487 0 newop->op_type = OP_CUSTOM;
488 0 newop->op_ppaddr = pp_slot_unwatch_one;
489 0 newop->op_targ = idx;
490 0 op_free(entersubop);
491 0 return newop;
492 }
493 }
494 }
495 }
496 2 return entersubop;
497 }
498  
499 /* clear('constant') => clear custom op */
500 8 static OP* slot_clear_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
501 OP *pushop, *cvop, *argop;
502 PERL_UNUSED_ARG(namegv); PERL_UNUSED_ARG(ckobj);
503 8 pushop = cUNOPx(entersubop)->op_first;
504 8 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 8 0 8
505 8 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 8 0 8
506 8 cvop = argop;
507 17 ▶ while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop);
Branch: 2 branches
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 9 0 9
while (OpHAS_SIBLING(cvop)) cvop = OpSIBLING(cvop); 9 8 17
508 8 ▶ if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) {
Branch: 4 branches
BranchTrueFalseTotal
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 8 0 8
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 8 0 8
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 7 1 8
if (argop != cvop && OpSIBLING(argop) == cvop && argop->op_type == OP_CONST) { 7 0 7
509 7 SV *namesv = cSVOPx(argop)->op_sv;
510 7 ▶ if (SvPOK(namesv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(namesv)) { 7 0 7
511 STRLEN name_len;
512 7 const char *name = SvPV(namesv, name_len);
513 7 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
514 7 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 3 4 7
515 3 OP *newop = newOP(OP_CUSTOM, 0);
516 3 newop->op_ppaddr = pp_slot_clear;
517 3 newop->op_targ = SvIV(*svp);
518 3 op_free(entersubop);
519 3 return newop;
520 }
521 }
522 }
523 5 return entersubop;
524 }
525  
526 /* ============================================
527 XS slot accessor (fallback for unoptimized calls)
528 ============================================ */
529  
530 300 static XS(xs_slot_accessor) {
531 300 dXSARGS;
532 300 IV idx = CvXSUBANY(cv).any_iv;
533 300 ▶ if (items) {
Branch: 1 branch
BranchTrueFalseTotal
if (items) { 150 150 300
534 150 UV flags = slot_flags[idx];
535 150 ▶ if (flags & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) { 0 150 150
536 0 ▶ croak("Attempt to set %s slot",
Branch: 1 branch
BranchTrueFalseTotal
croak("Attempt to set %s slot", 0 0 0
537 (flags & SLOT_FLAG_FROZEN) ? "frozen" : "locked");
538 }
539 150 sv_setsv(g_slots[idx], ST(0));
540 150 ▶ if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]);
Branch: 1 branch
BranchTrueFalseTotal
if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]); 0 150 150
541 150 ST(0) = g_slots[idx];
542 150 XSRETURN(1);
543 }
544 150 ST(0) = g_slots[idx];
545 150 XSRETURN(1);
546 }
547  
548 /* ============================================
549 Watchers
550 ============================================ */
551  
552 1023 static void fire_watchers(pTHX_ IV idx, SV *new_val) {
553 char key[32];
554 1023 int klen = snprintf(key, sizeof(key), "%ld", (long)idx);
555 1023 SV **name_sv = hv_fetch(g_slot_names, key, klen, 0);
556 1023 ▶ if (!name_sv || !SvOK(*name_sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!name_sv || !SvOK(*name_sv)) return; 1023 0 1023
if (!name_sv || !SvOK(*name_sv)) return; 0 1023 1023
557 {
558 STRLEN name_len;
559 1023 const char *name = SvPV(*name_sv, name_len);
560 1023 SV **svp = hv_fetch(g_watchers, name, name_len, 0);
561 1023 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 1023 0 1023
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 1023 0 1023
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 1023 0 1023
562 1023 AV *callbacks = (AV*)SvRV(*svp);
563 1023 SSize_t i, len = av_len(callbacks);
564 2053 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 1031 1022 2053
565 1031 SV **cb = av_fetch(callbacks, i, 0);
566 1031 ▶ if (cb && SvROK(*cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvROK(*cb)) { 1029 2 1031
if (cb && SvROK(*cb)) { 1029 0 1029
567 1029 dSP;
568 1029 ENTER; SAVETMPS;
569 1029 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1029 1029
570 1029 ▶ mXPUSHs(newSVpvn(name, name_len));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvn(name, name_len)); 0 1029 1029
571 1029 ▶ XPUSHs(new_val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(new_val); 0 1029 1029
572 1029 PUTBACK;
573 1029 call_sv(*cb, G_DISCARD);
574 1028 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1028 0 1028
575 }
576 }
577 }
578 }
579 }
580  
581 /* ============================================
582 Slot management helpers
583 ============================================ */
584  
585 492 static void ensure_slot_capacity(pTHX_ IV needed) {
586 492 ▶ if (needed >= g_slots_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed >= g_slots_size) { 10 482 492
587 10 ▶ IV new_size = g_slots_size ? g_slots_size * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
IV new_size = g_slots_size ? g_slots_size * 2 : 16; 10 0 10
588 IV i;
589 10 ▶ while (new_size <= needed) new_size *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_size <= needed) new_size *= 2; 0 10 10
590 10 ▶ Renew(g_slots, new_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_slots, new_size, SV*); 0 10 10
591 10 Renew(g_has_watchers,new_size, char);
592 10 ▶ Renew(slot_flags, new_size, UV);
Branch: 1 branch
BranchTrueFalseTotal
Renew(slot_flags, new_size, UV); 0 10 10
593 538 ▶ for (i = g_slots_size; i < new_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = g_slots_size; i < new_size; i++) { 528 10 538
594 528 g_slots[i] = newSV(0); /* dedicated SV; pointer never replaced */
595 528 g_has_watchers[i] = 0;
596 528 slot_flags[i] = 0;
597 }
598 10 g_slots_size = new_size;
599 }
600 492 }
601  
602 492 static IV create_slot(pTHX_ const char *name, STRLEN name_len) {
603 492 IV idx = g_slots_count++;
604 char key[32];
605 int klen;
606 492 ensure_slot_capacity(aTHX_ idx);
607 492 hv_store(g_slot_index, name, name_len, newSViv(idx), 0);
608 492 klen = snprintf(key, sizeof(key), "%ld", (long)idx);
609 492 hv_store(g_slot_names, key, klen, newSVpvn(name, name_len), 0);
610 492 return idx;
611 }
612  
613 1160 static IV get_or_create_slot_idx(pTHX_ const char *name, STRLEN len) {
614 1160 SV **svp = hv_fetch(g_slot_index, name, len, 0);
615 1160 ▶ return svp ? SvIV(*svp) : create_slot(aTHX_ name, len);
Branch: 1 branch
BranchTrueFalseTotal
return svp ? SvIV(*svp) : create_slot(aTHX_ name, len); 1136 24 1160
616 }
617  
618 223 static void install_accessor(pTHX_ const char *pkg, const char *name, STRLEN name_len, IV idx) {
619 char full[512];
620 CV *cv;
621 223 snprintf(full, sizeof(full), "%s::%s", pkg, name);
622 223 cv = newXS(full, xs_slot_accessor, __FILE__);
623 223 CvXSUBANY(cv).any_iv = idx;
624 223 cv_set_call_checker(cv, slot_call_checker, (SV*)cv);
625 223 }
626  
627 /* ============================================
628 XS functions
629 ============================================ */
630  
631 113 static XS(xs_import) {
632 113 dXSARGS;
633 113 ▶ const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop));
Branch: 6 branches
BranchTrueFalseTotal
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 113 0 113
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 113 0 113
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 0 113 113
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 0 0 0
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 113 0 113
const char *pkg = HvNAME((HV*)CopSTASH(PL_curcop)); 0 113 113
634 int i;
635 113 ▶ if (!pkg) pkg = "main";
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg) pkg = "main"; 0 113 113
636 338 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 225 113 338
637 STRLEN name_len;
638 225 const char *name = SvPV(ST(i), name_len);
639 225 SV **existing = hv_fetch(g_slot_index, name, name_len, 0);
640 225 ▶ IV idx = existing ? SvIV(*existing) : create_slot(aTHX_ name, name_len);
Branch: 1 branch
BranchTrueFalseTotal
IV idx = existing ? SvIV(*existing) : create_slot(aTHX_ name, name_len); 12 213 225
641 /* Skip if already installed as our accessor in this package */
642 {
643 225 HV *stash = gv_stashpv(pkg, GV_ADD);
644 225 SV **gvp = hv_fetch(stash, name, name_len, 0);
645 225 ▶ if (gvp && isGV(*gvp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gvp && isGV(*gvp)) { 2 223 225
if (gvp && isGV(*gvp)) { 2 0 2
646 2 CV *ecv = GvCV((GV*)*gvp);
647 2 ▶ if (ecv && CvISXSUB(ecv) && CvXSUB(ecv) == xs_slot_accessor)
Branch: 3 branches
BranchTrueFalseTotal
if (ecv && CvISXSUB(ecv) && CvXSUB(ecv) == xs_slot_accessor) 2 0 2
if (ecv && CvISXSUB(ecv) && CvXSUB(ecv) == xs_slot_accessor) 2 0 2
if (ecv && CvISXSUB(ecv) && CvXSUB(ecv) == xs_slot_accessor) 2 0 2
648 2 continue;
649 }
650 }
651 223 install_accessor(aTHX_ pkg, name, name_len, idx);
652 }
653 113 XSRETURN_EMPTY;
654 }
655  
656 46 static XS(xs_add) {
657 46 dXSARGS;
658 int i;
659 302 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 256 46 302
660 STRLEN name_len;
661 256 const char *name = SvPV(ST(i), name_len);
662 256 ▶ if (!hv_fetch(g_slot_index, name, name_len, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetch(g_slot_index, name, name_len, 0)) 255 1 256
663 255 create_slot(aTHX_ name, name_len);
664 }
665 46 XSRETURN_EMPTY;
666 }
667  
668 20 static XS(xs_watch) {
669 20 dXSARGS;
670 STRLEN name_len;
671 const char *name;
672 SV *callback;
673 SV **existing;
674 AV *callbacks;
675 SV **idx_sv;
676 20 ▶ if (items < 2) croak("Usage: Legba::watch($name, $callback)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Legba::watch($name, $callback)"); 0 20 20
677 20 name = SvPV(ST(0), name_len);
678 20 callback = ST(1);
679 20 existing = hv_fetch(g_watchers, name, name_len, 0);
680 20 ▶ if (existing && SvROK(*existing)) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing && SvROK(*existing)) { 5 15 20
if (existing && SvROK(*existing)) { 5 0 5
681 5 callbacks = (AV*)SvRV(*existing);
682 } else {
683 15 callbacks = newAV();
684 15 hv_store(g_watchers, name, name_len, newRV_noinc((SV*)callbacks), 0);
685 }
686 20 av_push(callbacks, SvREFCNT_inc(callback));
687 20 idx_sv = hv_fetch(g_slot_index, name, name_len, 0);
688 20 ▶ if (idx_sv) g_has_watchers[SvIV(*idx_sv)] = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (idx_sv) g_has_watchers[SvIV(*idx_sv)] = 1; 20 0 20
689 20 XSRETURN_EMPTY;
690 }
691  
692 4 static XS(xs_unwatch) {
693 4 dXSARGS;
694 STRLEN name_len;
695 const char *name;
696 SV **idx_sv;
697 4 int clear_flag = 0;
698 4 ▶ if (items < 1) croak("Usage: Legba::unwatch($name [, $callback])");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Legba::unwatch($name [, $callback])"); 0 4 4
699 4 name = SvPV(ST(0), name_len);
700 4 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 2 2 4
701 2 hv_delete(g_watchers, name, name_len, G_DISCARD);
702 2 clear_flag = 1;
703 } else {
704 2 SV *callback = ST(1);
705 2 SV **existing = hv_fetch(g_watchers, name, name_len, 0);
706 2 ▶ if (existing && SvROK(*existing)) {
Branch: 2 branches
BranchTrueFalseTotal
if (existing && SvROK(*existing)) { 2 0 2
if (existing && SvROK(*existing)) { 2 0 2
707 2 AV *callbacks = (AV*)SvRV(*existing);
708 2 SSize_t i, len = av_len(callbacks);
709 7 ▶ for (i = len; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i >= 0; i--) { 5 2 7
710 5 SV **cb = av_fetch(callbacks, i, 0);
711 5 ▶ if (cb && SvRV(*cb) == SvRV(callback))
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvRV(*cb) == SvRV(callback)) 5 0 5
if (cb && SvRV(*cb) == SvRV(callback)) 2 3 5
712 2 av_delete(callbacks, i, G_DISCARD);
713 }
714 2 ▶ if (av_len(callbacks) < 0) clear_flag = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(callbacks) < 0) clear_flag = 1; 0 2 2
715 }
716 }
717 4 ▶ if (clear_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (clear_flag) { 2 2 4
718 2 idx_sv = hv_fetch(g_slot_index, name, name_len, 0);
719 2 ▶ if (idx_sv) g_has_watchers[SvIV(*idx_sv)] = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (idx_sv) g_has_watchers[SvIV(*idx_sv)] = 0; 2 0 2
720 }
721 4 XSRETURN_EMPTY;
722 }
723  
724 18 static XS(xs_index) {
725 18 dXSARGS;
726 STRLEN name_len; const char *name; SV **svp;
727 18 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 18 18
728 18 name = SvPV(ST(0), name_len);
729 18 svp = hv_fetch(g_slot_index, name, name_len, 0);
730 18 ▶ if (svp) { ST(0) = *svp; XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { ST(0) = *svp; XSRETURN(1); } 17 1 18
731 1 XSRETURN_UNDEF;
732 }
733  
734 6 static XS(xs_get_by_idx) {
735 6 dXSARGS;
736 IV idx;
737 6 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 6 6
738 6 idx = SvIV(ST(0));
739 6 ▶ if (idx >= 0 && idx < g_slots_count) { ST(0) = g_slots[idx]; XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (idx >= 0 && idx < g_slots_count) { ST(0) = g_slots[idx]; XSRETURN(1); } 6 0 6
if (idx >= 0 && idx < g_slots_count) { ST(0) = g_slots[idx]; XSRETURN(1); } 5 1 6
740 1 XSRETURN_UNDEF;
741 }
742  
743 10008 static XS(xs_set_by_idx) {
744 10008 dXSARGS;
745 IV idx;
746 10008 ▶ if (items < 2) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) XSRETURN_EMPTY; 0 10008 10008
747 10008 idx = SvIV(ST(0));
748 10008 ▶ if (idx >= 0 && idx < g_slots_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx >= 0 && idx < g_slots_count) { 10008 0 10008
if (idx >= 0 && idx < g_slots_count) { 10008 0 10008
749 10008 UV flags = slot_flags[idx];
750 10008 ▶ if (flags & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN))
Branch: 1 branch
BranchTrueFalseTotal
if (flags & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) 2 10006 10008
751 2 ▶ croak("Attempt to set %s slot",
Branch: 1 branch
BranchTrueFalseTotal
croak("Attempt to set %s slot", 1 1 2
752 (flags & SLOT_FLAG_FROZEN) ? "frozen" : "locked");
753 10006 sv_setsv(g_slots[idx], ST(1));
754 10006 ▶ if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]);
Branch: 1 branch
BranchTrueFalseTotal
if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]); 1 10005 10006
755 10006 ST(0) = g_slots[idx];
756 10006 XSRETURN(1);
757 }
758 0 XSRETURN_EMPTY;
759 }
760  
761 101 static XS(xs_get) {
762 101 dXSARGS;
763 STRLEN name_len; const char *name; SV **svp;
764 101 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 101 101
765 101 name = SvPV(ST(0), name_len);
766 101 svp = hv_fetch(g_slot_index, name, name_len, 0);
767 101 ▶ if (svp) { ST(0) = g_slots[SvIV(*svp)]; XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { ST(0) = g_slots[SvIV(*svp)]; XSRETURN(1); } 99 2 101
768 2 XSRETURN_UNDEF;
769 }
770  
771 /* _set / set: create slot if missing; respects lock/freeze */
772 1131 static XS(xs_set) {
773 1131 dXSARGS;
774 STRLEN name_len; const char *name;
775 IV idx;
776 1131 ▶ if (items < 2) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) XSRETURN_EMPTY; 0 1131 1131
777 1131 name = SvPV(ST(0), name_len);
778 1131 idx = get_or_create_slot_idx(aTHX_ name, name_len);
779 1131 ▶ if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN))
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) 2 1129 1131
780 2 ▶ croak("Attempt to set %s slot '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak("Attempt to set %s slot '%s'", 1 1 2
781 (slot_flags[idx] & SLOT_FLAG_FROZEN) ? "frozen" : "locked", name);
782 1129 sv_setsv(g_slots[idx], ST(1));
783 1129 ▶ if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]);
Branch: 1 branch
BranchTrueFalseTotal
if (g_has_watchers[idx]) fire_watchers(aTHX_ idx, g_slots[idx]); 1017 112 1129
784 1128 ST(0) = g_slots[idx];
785 1128 XSRETURN(1);
786 }
787  
788 2 static XS(xs_slots) {
789 2 dXSARGS;
790 HE *entry;
791 PERL_UNUSED_VAR(items);
792 2 SP -= items;
793 2 hv_iterinit(g_slot_index);
794 50 ▶ while ((entry = hv_iternext(g_slot_index)))
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(g_slot_index))) 48 2 50
795 48 ▶ XPUSHs(hv_iterkeysv(entry));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(hv_iterkeysv(entry)); 0 48 48
796 2 PUTBACK;
797 2 return;
798 }
799  
800 26 static XS(xs_exists) {
801 26 dXSARGS;
802 STRLEN name_len; const char *name;
803 26 ▶ if (items != 1) croak("Usage: Legba::exists($name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: Legba::exists($name)"); 0 26 26
804 26 name = SvPV(ST(0), name_len);
805 26 ▶ if (hv_exists(g_slot_index, name, name_len)) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(g_slot_index, name, name_len)) XSRETURN_YES; 19 7 26
806 7 XSRETURN_NO;
807 }
808  
809 /* clear($name, ...) - clear value + watchers, skips locked/frozen */
810 6 static XS(xs_clear_named) {
811 6 dXSARGS;
812 int i;
813 13 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 7 6 13
814 STRLEN name_len;
815 7 const char *name = SvPV(ST(i), name_len);
816 7 SV **svp = hv_fetch(g_slot_index, name, name_len, 0);
817 7 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 7 0 7
818 7 IV idx = SvIV(*svp);
819 7 ▶ if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) continue; 2 5 7
820 5 sv_setsv(g_slots[idx], &PL_sv_undef);
821 5 hv_delete(g_watchers, name, name_len, G_DISCARD);
822 5 g_has_watchers[idx] = 0;
823 }
824 }
825 6 XSRETURN_EMPTY;
826 }
827  
828 2 static XS(xs_clear_by_idx) {
829 2 dXSARGS;
830 int i;
831 5 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 3 2 5
832 3 IV idx = SvIV(ST(i));
833 3 ▶ if (idx >= 0 && idx < g_slots_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx >= 0 && idx < g_slots_count) { 3 0 3
if (idx >= 0 && idx < g_slots_count) { 3 0 3
834 char key[32]; int klen; SV **name_sv;
835 3 sv_setsv(g_slots[idx], &PL_sv_undef);
836 3 klen = snprintf(key, sizeof(key), "%ld", (long)idx);
837 3 name_sv = hv_fetch(g_slot_names, key, klen, 0);
838 3 ▶ if (name_sv && SvOK(*name_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name_sv && SvOK(*name_sv)) { 3 0 3
if (name_sv && SvOK(*name_sv)) { 3 0 3
839 STRLEN name_len;
840 3 const char *name = SvPV(*name_sv, name_len);
841 3 hv_delete(g_watchers, name, name_len, G_DISCARD);
842 }
843 3 g_has_watchers[idx] = 0;
844 }
845 }
846 2 XSRETURN_EMPTY;
847 }
848  
849 /* _delete($name) - set to undef, slot still exists; respects lock/freeze */
850 5 static XS(xs_delete) {
851 5 dXSARGS;
852 STRLEN name_len; const char *name; SV **svp;
853 5 ▶ if (items < 1) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_EMPTY; 0 5 5
854 5 name = SvPV(ST(0), name_len);
855 5 svp = hv_fetch(g_slot_index, name, name_len, 0);
856 5 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 5 0 5
857 5 IV idx = SvIV(*svp);
858 5 ▶ if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN))
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)) 2 3 5
859 2 ▶ croak("Attempt to delete %s slot '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak("Attempt to delete %s slot '%s'", 1 1 2
860 (slot_flags[idx] & SLOT_FLAG_FROZEN) ? "frozen" : "locked", name);
861 3 sv_setsv(g_slots[idx], &PL_sv_undef);
862 }
863 3 XSRETURN_EMPTY;
864 }
865  
866 /* _keys() - list all slot names */
867 3 static XS(xs_keys) {
868 3 dXSARGS;
869 HE *entry;
870 PERL_UNUSED_VAR(items);
871 3 SP -= items;
872 3 hv_iterinit(g_slot_index);
873 48 ▶ while ((entry = hv_iternext(g_slot_index))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(g_slot_index))) { 45 3 48
874 45 I32 klen; char *key = hv_iterkey(entry, &klen);
875 45 ▶ mXPUSHp(key, klen);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, klen); 0 45 45
876 }
877 3 PUTBACK;
878 3 return;
879 }
880  
881 /* _clear() - clear all non-locked/frozen slot values (preserves slots/watchers) */
882 6 static XS(xs_clear_all) {
883 6 dXSARGS;
884 IV i;
885 PERL_UNUSED_VAR(items);
886 148 ▶ for (i = 0; i < g_slots_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_slots_count; i++) { 142 6 148
887 142 ▶ if (!(slot_flags[i] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(slot_flags[i] & (SLOT_FLAG_LOCKED | SLOT_FLAG_FROZEN))) 141 1 142
888 141 sv_setsv(g_slots[i], &PL_sv_undef);
889 }
890 6 XSRETURN_EMPTY;
891 }
892  
893 /* _install_accessor($pkg, $slot_name) */
894 0 static XS(xs_install_accessor_fn) {
895 0 dXSARGS;
896 STRLEN pkg_len, name_len;
897 const char *pkg_name, *name;
898 IV idx;
899 0 ▶ if (items < 2) croak("Usage: Legba::_install_accessor($pkg, $slot_name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Usage: Legba::_install_accessor($pkg, $slot_name)"); 0 0 0
900 0 pkg_name = SvPV(ST(0), pkg_len);
901 0 ▶ name = SvPV(ST(1), name_len);
Branch: 1 branch
BranchTrueFalseTotal
name = SvPV(ST(1), name_len); 0 0 0
902 0 idx = get_or_create_slot_idx(aTHX_ name, name_len);
903 0 install_accessor(aTHX_ pkg_name, name, name_len, idx);
904 0 XSRETURN_EMPTY;
905 }
906  
907 /* _slot_ptr($name) - UV of the dedicated SV* (stable across value changes) */
908 11 static XS(xs_slot_ptr) {
909 11 dXSARGS;
910 STRLEN name_len; const char *name; IV idx;
911 11 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 11 11
912 11 name = SvPV(ST(0), name_len);
913 11 idx = get_or_create_slot_idx(aTHX_ name, name_len);
914 11 ST(0) = sv_2mortal(newSVuv(PTR2UV(g_slots[idx])));
915 11 XSRETURN(1);
916 }
917  
918 /* _registry() - hashref of slot_name => index for introspection */
919 4 static XS(xs_registry) {
920 4 dXSARGS;
921 PERL_UNUSED_VAR(items);
922 4 ST(0) = sv_2mortal(newRV_inc((SV*)g_slot_index));
923 4 XSRETURN(1);
924 }
925  
926 /* _make_get_op($name) - allocate a getter OP, return address as UV */
927 15 static XS(xs_make_get_op) {
928 15 dXSARGS;
929 STRLEN name_len; const char *name; IV idx; OP *op;
930 15 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 15 15
931 15 name = SvPV(ST(0), name_len);
932 15 idx = get_or_create_slot_idx(aTHX_ name, name_len);
933 15 op = newOP(OP_CUSTOM, 0);
934 15 op->op_ppaddr = pp_slot_get;
935 15 op->op_targ = idx;
936 15 ST(0) = sv_2mortal(newSVuv(PTR2UV(op)));
937 15 XSRETURN(1);
938 }
939  
940 /* _make_set_op($name) - allocate a setter OP, return address as UV */
941 3 static XS(xs_make_set_op) {
942 3 dXSARGS;
943 STRLEN name_len; const char *name; IV idx; OP *op;
944 3 ▶ if (items < 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_UNDEF; 0 3 3
945 3 name = SvPV(ST(0), name_len);
946 3 idx = get_or_create_slot_idx(aTHX_ name, name_len);
947 /* Setter needs an operand child; use a null op as placeholder */
948 3 op = newUNOP(OP_NULL, 0, newOP(OP_NULL, 0));
949 3 op->op_type = OP_CUSTOM;
950 3 op->op_ppaddr = pp_slot_set;
951 3 op->op_targ = idx;
952 3 ST(0) = sv_2mortal(newSVuv(PTR2UV(op)));
953 3 XSRETURN(1);
954 }
955  
956 /* Lock / freeze */
957 7 static XS(xs_lock) {
958 7 dXSARGS;
959 STRLEN len; const char *n; SV **svp; IV idx;
960 7 ▶ if (items < 1) croak("Usage: Legba::_lock($name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Legba::_lock($name)"); 0 7 7
961 7 n = SvPV(ST(0), len);
962 7 svp = hv_fetch(g_slot_index, n, len, 0);
963 7 ▶ if (!svp) croak("Cannot lock non-existent slot '%s'", n);
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("Cannot lock non-existent slot '%s'", n); 1 6 7
964 6 idx = SvIV(*svp);
965 6 ▶ if (slot_flags[idx] & SLOT_FLAG_FROZEN) croak("Cannot lock frozen slot '%s'", n);
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & SLOT_FLAG_FROZEN) croak("Cannot lock frozen slot '%s'", n); 1 5 6
966 5 slot_flags[idx] |= SLOT_FLAG_LOCKED;
967 5 XSRETURN_EMPTY;
968 }
969  
970 6 static XS(xs_unlock) {
971 6 dXSARGS;
972 STRLEN len; const char *n; SV **svp; IV idx;
973 6 ▶ if (items < 1) croak("Usage: Legba::_unlock($name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Legba::_unlock($name)"); 0 6 6
974 6 n = SvPV(ST(0), len);
975 6 svp = hv_fetch(g_slot_index, n, len, 0);
976 6 ▶ if (!svp) croak("Cannot unlock non-existent slot '%s'", n);
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("Cannot unlock non-existent slot '%s'", n); 0 6 6
977 6 idx = SvIV(*svp);
978 6 ▶ if (slot_flags[idx] & SLOT_FLAG_FROZEN) croak("Cannot unlock frozen slot '%s'", n);
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[idx] & SLOT_FLAG_FROZEN) croak("Cannot unlock frozen slot '%s'", n); 1 5 6
979 5 slot_flags[idx] &= ~SLOT_FLAG_LOCKED;
980 5 XSRETURN_EMPTY;
981 }
982  
983 6 static XS(xs_freeze) {
984 6 dXSARGS;
985 STRLEN len; const char *n; SV **svp; IV idx;
986 6 ▶ if (items < 1) croak("Usage: Legba::_freeze($name)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: Legba::_freeze($name)"); 0 6 6
987 6 n = SvPV(ST(0), len);
988 6 svp = hv_fetch(g_slot_index, n, len, 0);
989 6 ▶ if (!svp) croak("Cannot freeze non-existent slot '%s'", n);
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("Cannot freeze non-existent slot '%s'", n); 1 5 6
990 5 idx = SvIV(*svp);
991 5 slot_flags[idx] |= SLOT_FLAG_FROZEN;
992 5 slot_flags[idx] &= ~SLOT_FLAG_LOCKED; /* frozen supersedes locked */
993 5 XSRETURN_EMPTY;
994 }
995  
996 3 static XS(xs_is_locked) {
997 3 dXSARGS;
998 STRLEN len; const char *n; SV **svp;
999 3 ▶ if (items < 1) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_NO; 0 3 3
1000 3 n = SvPV(ST(0), len);
1001 3 svp = hv_fetch(g_slot_index, n, len, 0);
1002 3 ▶ if (!svp) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) XSRETURN_NO; 1 2 3
1003 2 ▶ if (slot_flags[SvIV(*svp)] & SLOT_FLAG_LOCKED) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[SvIV(*svp)] & SLOT_FLAG_LOCKED) XSRETURN_YES; 1 1 2
1004 1 XSRETURN_NO;
1005 }
1006  
1007 8 static XS(xs_is_frozen) {
1008 8 dXSARGS;
1009 STRLEN len; const char *n; SV **svp;
1010 8 ▶ if (items < 1) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) XSRETURN_NO; 0 8 8
1011 8 n = SvPV(ST(0), len);
1012 8 svp = hv_fetch(g_slot_index, n, len, 0);
1013 8 ▶ if (!svp) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) XSRETURN_NO; 1 7 8
1014 7 ▶ if (slot_flags[SvIV(*svp)] & SLOT_FLAG_FROZEN) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (slot_flags[SvIV(*svp)] & SLOT_FLAG_FROZEN) XSRETURN_YES; 7 0 7
1015 0 XSRETURN_NO;
1016 }
1017  
1018 /* ============================================
1019 MODULE / BOOT
1020 ============================================ */
1021  
1022 MODULE = Legba PACKAGE = Legba
1023  
1024 PROTOTYPES: DISABLE
1025  
1026 BOOT:
1027 {
1028 #if LEGBA_HAS_XOP
1029 13 XopENTRY_set(&legba_get_xop, xop_name, "legba_get");
1030 13 XopENTRY_set(&legba_get_xop, xop_desc, "Legba slot getter");
1031 13 XopENTRY_set(&legba_get_xop, xop_class, OA_BASEOP);
1032 13 Perl_custom_op_register(aTHX_ pp_slot_get, &legba_get_xop);
1033  
1034 13 XopENTRY_set(&legba_set_xop, xop_name, "legba_set");
1035 13 XopENTRY_set(&legba_set_xop, xop_desc, "Legba slot setter");
1036 13 XopENTRY_set(&legba_set_xop, xop_class, OA_UNOP);
1037 13 Perl_custom_op_register(aTHX_ pp_slot_set, &legba_set_xop);
1038  
1039 13 XopENTRY_set(&legba_watch_xop, xop_name, "legba_watch");
1040 13 XopENTRY_set(&legba_watch_xop, xop_desc, "Legba slot watcher registration");
1041 13 XopENTRY_set(&legba_watch_xop, xop_class, OA_UNOP);
1042 13 Perl_custom_op_register(aTHX_ pp_slot_watch, &legba_watch_xop);
1043  
1044 13 XopENTRY_set(&legba_unwatch_xop, xop_name, "legba_unwatch");
1045 13 XopENTRY_set(&legba_unwatch_xop, xop_desc, "Legba slot unwatch all");
1046 13 XopENTRY_set(&legba_unwatch_xop, xop_class, OA_BASEOP);
1047 13 Perl_custom_op_register(aTHX_ pp_slot_unwatch, &legba_unwatch_xop);
1048  
1049 13 XopENTRY_set(&legba_unwatch_one_xop, xop_name, "legba_unwatch_one");
1050 13 XopENTRY_set(&legba_unwatch_one_xop, xop_desc, "Legba slot unwatch specific");
1051 13 XopENTRY_set(&legba_unwatch_one_xop, xop_class, OA_UNOP);
1052 13 Perl_custom_op_register(aTHX_ pp_slot_unwatch_one, &legba_unwatch_one_xop);
1053  
1054 13 XopENTRY_set(&legba_clear_xop, xop_name, "legba_clear");
1055 13 XopENTRY_set(&legba_clear_xop, xop_desc, "Legba slot clear");
1056 13 XopENTRY_set(&legba_clear_xop, xop_class, OA_BASEOP);
1057 13 Perl_custom_op_register(aTHX_ pp_slot_clear, &legba_clear_xop);
1058 #endif
1059  
1060 /* Initialise globals */
1061 13 g_slot_index = newHV();
1062 13 g_slot_names = newHV();
1063 13 g_watchers = newHV();
1064  
1065 13 g_slots_size = 16;
1066 13 ▶ Newx(g_slots, g_slots_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(g_slots, g_slots_size, SV*); 0 13 13
1067 13 Newxz(g_has_watchers,g_slots_size, char);
1068 13 ▶ Newxz(slot_flags, g_slots_size, UV);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(slot_flags, g_slots_size, UV); 0 13 13
1069 {
1070 IV i;
1071 221 ▶ for (i = 0; i < g_slots_size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_slots_size; i++) 208 13 221
1072 208 g_slots[i] = newSV(0); /* dedicated SV — pointer is stable */
1073 }
1074  
1075 /* import */
1076 13 newXS("Legba::import", xs_import, __FILE__);
1077  
1078 /* New API (from Ancient/slot) */
1079 13 newXS("Legba::add", xs_add, __FILE__);
1080 13 newXS("Legba::get_by_idx", xs_get_by_idx, __FILE__);
1081 13 newXS("Legba::set_by_idx", xs_set_by_idx, __FILE__);
1082 13 newXS("Legba::slots", xs_slots, __FILE__);
1083 13 newXS("Legba::exists", xs_exists, __FILE__);
1084 13 newXS("Legba::clear_by_idx", xs_clear_by_idx, __FILE__);
1085 {
1086 13 CV *cv = newXS("Legba::get", xs_get, __FILE__);
1087 13 cv_set_call_checker(cv, slot_get_call_checker, (SV*)cv);
1088 }
1089 {
1090 13 CV *cv = newXS("Legba::set", xs_set, __FILE__);
1091 13 cv_set_call_checker(cv, slot_set_call_checker, (SV*)cv);
1092 }
1093 {
1094 13 CV *cv = newXS("Legba::index", xs_index, __FILE__);
1095 13 cv_set_call_checker(cv, slot_index_call_checker, (SV*)cv);
1096 }
1097 {
1098 13 CV *cv = newXS("Legba::watch", xs_watch, __FILE__);
1099 13 cv_set_call_checker(cv, slot_watch_call_checker, (SV*)cv);
1100 }
1101 {
1102 13 CV *cv = newXS("Legba::unwatch", xs_unwatch, __FILE__);
1103 13 cv_set_call_checker(cv, slot_unwatch_call_checker, (SV*)cv);
1104 }
1105 {
1106 13 CV *cv = newXS("Legba::clear", xs_clear_named, __FILE__);
1107 13 cv_set_call_checker(cv, slot_clear_call_checker, (SV*)cv);
1108 }
1109  
1110 /* Backward-compatible API (_get, _set, _exists, _delete, _keys, _clear,
1111 _lock, _unlock, _freeze, _is_locked, _is_frozen, _install_accessor,
1112 _slot_ptr, _registry, _make_get_op, _make_set_op) */
1113 {
1114 13 CV *cv = newXS("Legba::_get", xs_get, __FILE__);
1115 13 cv_set_call_checker(cv, slot_get_call_checker, (SV*)cv);
1116 }
1117 {
1118 13 CV *cv = newXS("Legba::_set", xs_set, __FILE__);
1119 13 cv_set_call_checker(cv, slot_set_call_checker, (SV*)cv);
1120 }
1121 13 newXS("Legba::_exists", xs_exists, __FILE__);
1122 13 newXS("Legba::_delete", xs_delete, __FILE__);
1123 13 newXS("Legba::_keys", xs_keys, __FILE__);
1124 13 newXS("Legba::_clear", xs_clear_all, __FILE__);
1125 13 newXS("Legba::_lock", xs_lock, __FILE__);
1126 13 newXS("Legba::_unlock", xs_unlock, __FILE__);
1127 13 newXS("Legba::_freeze", xs_freeze, __FILE__);
1128 13 newXS("Legba::_is_locked", xs_is_locked, __FILE__);
1129 13 newXS("Legba::_is_frozen", xs_is_frozen, __FILE__);
1130 13 newXS("Legba::_install_accessor", xs_install_accessor_fn,__FILE__);
1131 13 newXS("Legba::_slot_ptr", xs_slot_ptr, __FILE__);
1132 13 newXS("Legba::_registry", xs_registry, __FILE__);
1133 13 newXS("Legba::_make_get_op", xs_make_get_op, __FILE__);
1134 13 newXS("Legba::_make_set_op", xs_make_set_op, __FILE__);
1135 }
↑ Summary blib/lib/Legba.pm »