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