lib/Lexical/Var.xs

filter: Statementstmt 94.2% 244 / 259 Branchbran 71.5% 133 / 186 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 377 / 445 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lexical/Sub.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #define Q_PERL_VERSION_DECIMAL(r,v,s) ((r)*1000000 + (v)*1000 + (s))
7 #define Q_PERL_DECIMAL_VERSION \
8 Q_PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
9 #define Q_PERL_VERSION_GE(r,v,s) \
10 (Q_PERL_DECIMAL_VERSION >= Q_PERL_VERSION_DECIMAL(r,v,s))
11 #define Q_PERL_VERSION_LT(r,v,s) \
12 (Q_PERL_DECIMAL_VERSION < Q_PERL_VERSION_DECIMAL(r,v,s))
13  
14 #if Q_PERL_VERSION_LT(5,7,2)
15 # undef dNOOP
16 # define dNOOP extern int Perl___notused_func(void)
17 #endif /* <5.7.2 */
18  
19 #if Q_PERL_VERSION_LT(5,37,11)
20 # undef NOOP
21 # define NOOP ((void)0)
22 #endif /* <5.37.11 */
23  
24 #ifndef PERL_UNUSED_VAR
25 # define PERL_UNUSED_VAR(x) ((void)(x))
26 #endif /* !PERL_UNUSED_VAR */
27  
28 #ifndef PERL_UNUSED_ARG
29 # define PERL_UNUSED_ARG(x) PERL_UNUSED_VAR(x)
30 #endif /* !PERL_UNUSED_ARG */
31  
32 #if Q_PERL_VERSION_GE(5,7,3)
33 # define PERL_UNUSED_THX() NOOP
34 #else /* <5.7.3 */
35 # define PERL_UNUSED_THX() ((void)(aTHX+0))
36 #endif /* <5.7.3 */
37  
38 #if Q_PERL_VERSION_LT(5,9,3)
39 # define SVt_LAST (SVt_PVIO+1)
40 #endif /* <5.9.3 */
41  
42 #ifdef SVf_PROTECT
43 # define SvREADONLY_fully_on(sv) (SvFLAGS(sv) |= SVf_READONLY|SVf_PROTECT)
44 # define SvREADONLY_fully_off(sv) (SvFLAGS(sv) &= ~(SVf_READONLY|SVf_PROTECT))
45 # define SvREADONLY_slightly_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
46 # define SvREADONLY_slightly_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
47 #else /* !SVf_PROTECT */
48 # define SvREADONLY_fully_on(sv) SvREADONLY_on(sv)
49 # define SvREADONLY_fully_off(sv) SvREADONLY_off(sv)
50 # define SvREADONLY_slightly_on(sv) SvREADONLY_on(sv)
51 # define SvREADONLY_slightly_off(sv) SvREADONLY_off(sv)
52 #endif /* !SVf_PROTECT */
53  
54 #ifndef sv_setpvs
55 # define sv_setpvs(SV, STR) sv_setpvn(SV, "" STR "", sizeof(STR)-1)
56 #endif /* !sv_setpvs */
57  
58 #ifndef gv_stashpvs
59 # define gv_stashpvs(name, flags) gv_stashpvn("" name "", sizeof(name)-1, flags)
60 #endif /* !gv_stashpvs */
61  
62 #ifndef newSV_type
63 # define newSV_type(type) THX_newSV_type(aTHX_ type)
64 static SV *THX_newSV_type(pTHX_ svtype type)
65 {
66 SV *sv = newSV(0);
67 (void) SvUPGRADE(sv, type);
68 return sv;
69 }
70 #endif /* !newSV_type */
71  
72 #ifndef PadnameIsOUR
73 # ifdef SvPAD_OUR
74 # define PadnameIsOUR(pn) SvPAD_OUR(pn)
75 # else /* !SvPAD_OUR */
76 # define PadnameIsOUR(pn) (SvFLAGS(pn) & SVpad_OUR)
77 # endif /* !SvPAD_OUR */
78 #endif /* !PadnameIsOUR */
79  
80 #ifndef PadnameIsOUR_on
81 # ifdef SvPAD_OUR_on
82 # define PadnameIsOUR_on(pn) SvPAD_OUR_on(pn)
83 # else /* !SvPAD_OUR_on */
84 # define PadnameIsOUR_on(pn) (SvFLAGS(pn) |= SVpad_OUR)
85 # endif /* !SvPAD_OUR_on */
86 #endif /* !PadnameIsOUR_on */
87  
88 #ifndef PadnameOURSTASH
89 # ifdef SvOURSTASH
90 # define PadnameOURSTASH(pn) SvOURSTASH(pn)
91 # elif defined(OURSTASH)
92 # define PadnameOURSTASH(pn) OURSTASH(pn)
93 # else /* !SvOURSTASH && !OURSTASH */
94 # define PadnameOURSTASH(pn) GvSTASH(pn)
95 # endif /* !SvOURSTASH && !OURSTASH */
96 #endif /* !PadnameOURSTASH */
97  
98 #ifndef PadnameOURSTASH_set
99 # ifdef SvOURSTASH_set
100 # define PadnameOURSTASH_set(pn, st) SvOURSTASH_set(pn, st)
101 # elif defined(OURSTASH_set)
102 # define PadnameOURSTASH_set(pn, st) OURSTASH_set(pn, st)
103 # else /* !SvOURSTASH_set && !OURSTASH_set */
104 # define PadnameOURSTASH_set(pn, st) (GvSTASH(pn) = (st))
105 # endif /* !SvOURSTASH_set && !OURSTASH_set */
106 #endif /* !PadnameOURSTASH_set */
107  
108 #ifndef PadnameIsSTATE
109 # ifdef SvPAD_STATE
110 # define PadnameIsSTATE(pn) SvPAD_STATE(pn)
111 # else /* !SvPAD_STATE */
112 # define PadnameIsSTATE(pn) 0
113 # endif /* !SvPAD_STATE */
114 #endif /* !PadnameIsSTATE */
115  
116 #ifndef PadnameIsSTATE_on
117 # ifdef SvPAD_STATE_on
118 # define PadnameIsSTATE_on(pn) SvPAD_STATE_on(pn)
119 # endif /* SvPAD_STATE_on */
120 #endif /* !PadnameIsSTATE_on */
121  
122 #ifndef PadMAX
123 # define PadlistARRAY(pl) ((PAD**)AvARRAY(pl))
124 # define PadlistNAMES(pl) (PadlistARRAY(pl)[0])
125 # define PadMAX(p) AvFILLp(p)
126 # define PadARRAY(p) AvARRAY(p)
127 typedef SV PADNAME;
128 typedef AV PADNAMELIST;
129 #endif /* !PadMAX */
130  
131 #ifndef PadnamePV
132 # define PadnamePV(pn) (SvPOK(pn) ? SvPVX(pn) : NULL)
133 #endif /* !PadnamePV */
134  
135 #ifndef PadnameLEN
136 # define PadnameLEN(pn) SvCUR(pn)
137 #endif /* !PadnameLEN */
138  
139 #ifndef PadnameOUTER
140 # define PadnameOUTER(pn) SvFAKE(pn)
141 #endif /* !PadnameOUTER */
142  
143 #if Q_PERL_VERSION_LT(5,8,1)
144 typedef AV PADLIST;
145 typedef AV PAD;
146 #endif /* <5.8.1 */
147  
148 #ifndef newPADNAMEpvn
149 # if Q_PERL_VERSION_GE(5,9,4)
150 # define SVt_PADNAME SVt_PVMG
151 # else /* <5.9.4 */
152 # define SVt_PADNAME SVt_PVGV
153 # endif /* <5.9.4 */
154 # define newPADNAMEpvn(pv, len) THX_newPADNAMEpvn(aTHX_ pv, len)
155 static PADNAME *THX_newPADNAMEpvn(pTHX_ char const *pv, STRLEN len)
156 {
157 PADNAME *name = newSV_type(SVt_PADNAME);
158 sv_setpvn(name, pv, len);
159 return name;
160 }
161 #endif /* !newPADNAMEpvn */
162  
163 #ifndef padnamelist_store
164 # define padnamelist_store av_store
165 #endif /* !padnamelist_store */
166  
167 #ifndef padnamelist_fetch
168 # define padnamelist_fetch(pnl, off) THX_padnamelist_fetch(aTHX_ pnl, off)
169 static PADNAME *THX_padnamelist_fetch(pTHX_ PADNAMELIST *pnl, PADOFFSET off)
170 {
171 SV **rp = av_fetch(pnl, off, 0);
172 return rp ? *rp : NULL;
173 }
174 #endif /* !padnamelist_fetch */
175  
176 #ifndef COP_SEQ_RANGE_LOW
177 # if Q_PERL_VERSION_GE(5,9,5)
178 # define COP_SEQ_RANGE_LOW(sv) ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow
179 # define COP_SEQ_RANGE_HIGH(sv) ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh
180 # else /* <5.9.5 */
181 # define COP_SEQ_RANGE_LOW(sv) ((U32)SvNVX(sv))
182 # define COP_SEQ_RANGE_HIGH(sv) ((U32)SvIVX(sv))
183 # endif /* <5.9.5 */
184 #endif /* !COP_SEQ_RANGE_LOW */
185  
186 #ifndef COP_SEQ_RANGE_LOW_set
187 # if Q_PERL_VERSION_GE(5,21,7)
188 # define COP_SEQ_RANGE_LOW_set(pn,val) \
189 do { (pn)->xpadn_low = (val); } while(0)
190 # define COP_SEQ_RANGE_HIGH_set(pn,val) \
191 do { (pn)->xpadn_high = (val); } while(0)
192 # elif Q_PERL_VERSION_GE(5,9,5)
193 # define COP_SEQ_RANGE_LOW_set(sv,val) \
194 do { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } while(0)
195 # define COP_SEQ_RANGE_HIGH_set(sv,val) \
196 do { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } while(0)
197 # else /* <5.9.5 */
198 # define COP_SEQ_RANGE_LOW_set(sv,val) SvNV_set(sv, val)
199 # define COP_SEQ_RANGE_HIGH_set(sv,val) SvIV_set(sv, val)
200 # endif /* <5.9.5 */
201 #endif /* !COP_SEQ_RANGE_LOW_set */
202  
203 #ifndef PadnameIN_SCOPE
204 # define PadnameIN_SCOPE(pn, seq) THX_PadnameIN_SCOPE(aTHX_ pn, seq)
205 26 static int THX_PadnameIN_SCOPE(pTHX_ PADNAME const *pn, U32 seq)
206 {
207 26 U32 lowseq = COP_SEQ_RANGE_LOW(pn);
208 26 U32 highseq = COP_SEQ_RANGE_HIGH(pn);
209 PERL_UNUSED_THX();
210 # if Q_PERL_VERSION_GE(5,13,10)
211 26 ▶ if(lowseq == PERL_PADSEQ_INTRO) {
Branch: 1 branch
BranchTrueFalseTotal
if(lowseq == PERL_PADSEQ_INTRO) { 0 26 26
212 0 return 0;
213 26 ▶ } else if(highseq == PERL_PADSEQ_INTRO) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(highseq == PERL_PADSEQ_INTRO) { 26 0 26
214 return seq > lowseq ?
215 26 ▶ (seq - lowseq) < (U32_MAX>>1) :
Branch: 1 branch
BranchTrueFalseTotal
(seq - lowseq) < (U32_MAX>>1) : 26 0 26
216 0 (lowseq - seq) > (U32_MAX>>1);
217 } else {
218 return lowseq > highseq ?
219 0 ▶ seq > lowseq || seq <= highseq :
Branch: 3 branches
BranchTrueFalseTotal
seq > lowseq || seq <= highseq : 0 0 0
seq > lowseq || seq <= highseq : 0 0 0
seq > lowseq || seq <= highseq : 0 0 0
220 0 ▶ seq > lowseq && seq <= highseq;
Branch: 2 branches
BranchTrueFalseTotal
seq > lowseq && seq <= highseq; 0 0 0
seq > lowseq && seq <= highseq; 0 0 0
221 }
222 # else /* <5.13.10 */
223 return seq > lowseq && seq <= highseq;
224 # endif /* <5.13.10 */
225 }
226 #endif /* !PadnameIN_SCOPE */
227  
228 #ifndef COP_SEQMAX_INC
229 # if Q_PERL_VERSION_GE(5,13,10)
230 # define COP_SEQMAX_INC \
231 do { \
232 PL_cop_seqmax++; \
233 if(PL_cop_seqmax == PERL_PADSEQ_INTRO) PL_cop_seqmax++; \
234 } while(0)
235 # else /* <5.13.10 */
236 # define COP_SEQMAX_INC ((void)(PL_cop_seqmax++))
237 # endif /* <5.13.10 */
238 #endif /* !COP_SEQMAX_INC */
239  
240 #ifndef SvRV_set
241 # define SvRV_set(SV, VAL) (SvRV(SV) = (VAL))
242 #endif /* !SvRV_set */
243  
244 #ifndef SVfARG
245 # define SVfARG(p) ((void *)(p))
246 #endif /* !SVfARG */
247  
248 #ifndef GV_NOTQUAL
249 # define GV_NOTQUAL 0
250 #endif /* !GV_NOTQUAL */
251  
252 #if Q_PERL_VERSION_LT(5,9,3)
253 typedef OP *(*Perl_check_t)(pTHX_ OP *);
254 #endif /* <5.9.3 */
255  
256 #if Q_PERL_VERSION_LT(5,10,1)
257 typedef unsigned Optype;
258 #endif /* <5.10.1 */
259  
260 #ifndef wrap_op_checker
261 # define wrap_op_checker(c,n,o) THX_wrap_op_checker(aTHX_ c,n,o)
262 static void THX_wrap_op_checker(pTHX_ Optype opcode,
263 Perl_check_t new_checker, Perl_check_t *old_checker_p)
264 {
265 PERL_UNUSED_THX();
266 if(*old_checker_p) return;
267 OP_REFCNT_LOCK;
268 if(!*old_checker_p) {
269 *old_checker_p = PL_check[opcode];
270 PL_check[opcode] = new_checker;
271 }
272 OP_REFCNT_UNLOCK;
273 }
274 #endif /* !wrap_op_checker */
275  
276 /*
277 * scalar classification
278 *
279 * Logic borrowed from Params::Classify.
280 */
281  
282 #define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
283  
284 #if Q_PERL_VERSION_GE(5,11,0)
285 # define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
286 #else /* <5.11.0 */
287 # define sv_is_regexp(sv) 0
288 #endif /* <5.11.0 */
289  
290 #define sv_is_string(sv) \
291 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
292 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
293  
294 #define Q_CODE_AS_STATE_IN_PAD Q_PERL_VERSION_GE(5,19,1)
295 #define Q_CODE_OUTSIDE_PAD Q_PERL_VERSION_LT(5,17,4)
296 #define Q_CODE_CLASHES_WITH_PAD (!Q_CODE_OUTSIDE_PAD && !Q_CODE_AS_STATE_IN_PAD)
297  
298 /*
299 * newOP_const_identity()
300 *
301 * This function generate op that evaluates to a fixed object identity
302 * and can also participate in constant folding.
303 *
304 * Lexical::Var generally needs to make ops that evaluate to fixed
305 * identities, that being what a name that it handles represents.
306 * Normally it can do this by means of an rv2xv op applied to a const op,
307 * where the const op holds an RV that references the object of interest.
308 * However, rv2xv can't undergo constant folding. Where the object is
309 * a readonly scalar, we'd like it to take part in constant folding.
310 * The obvious way to make it work as a constant for folding is to use a
311 * const op that directly holds the object. However, in a Perl built for
312 * ithreads, the value in a const op gets moved into the pad to achieve
313 * clonability, and in the process the value may be copied rather than the
314 * object merely rereferenced. Generally, the const op only guarantees
315 * to provide a fixed *value*, not a fixed object identity.
316 *
317 * Where a const op might not preserve object identity, we can achieve
318 * preservation by means of a customised variant of the const op. The op
319 * directly holds an RV that references the object of interest, and its
320 * variant pp function dereferences it (as rv2sv would). The pad logic
321 * operates on the op structure as normal, and may copy the RV without
322 * preserving its identity, which is OK because the RV isn't what we
323 * need to preserve. Being labelled as a const op, it is eligible for
324 * constant folding. When actually executed, it evaluates to the object
325 * of interest, providing both fixed value and fixed identity.
326 */
327  
328 #ifdef USE_ITHREADS
329 # define Q_USE_ITHREADS 1
330 #else /* !USE_ITHREADS */
331 # define Q_USE_ITHREADS 0
332 #endif /* !USE_ITHREADS */
333  
334 #define Q_CONST_COPIES Q_USE_ITHREADS
335  
336 #if Q_CONST_COPIES
337 static OP *THX_pp_const_via_ref(pTHX)
338 {
339 dSP;
340 SV *reference_sv = cSVOPx_sv(PL_op);
341 SV *referent_sv = SvRV(reference_sv);
342 XPUSHs(referent_sv);
343 RETURN;
344 }
345 #endif /* Q_CONST_COPIES */
346  
347 #define newOP_const_identity(sv) THX_newOP_const_identity(aTHX_ sv)
348 30 static OP *THX_newOP_const_identity(pTHX_ SV *sv)
349 {
350 #if Q_CONST_COPIES
351 OP *op = newSVOP(OP_CONST, 0, newRV_noinc(sv));
352 op->op_ppaddr = THX_pp_const_via_ref;
353 return op;
354 #else /* !Q_CONST_COPIES */
355 30 return newSVOP(OP_CONST, 0, sv);
356 #endif /* !Q_CONST_COPIES */
357 }
358  
359 /*
360 * %^H key names
361 */
362  
363 #define KEYPREFIX "Lexical::Var/"
364 #define KEYPREFIXLEN (sizeof(KEYPREFIX)-1)
365  
366 #define LVOURPREFIX "Lexical::Var::<LVOUR>"
367 #define LVOURPREFIXLEN (sizeof(LVOURPREFIX)-1)
368  
369 #define CHAR_IDSTART 0x01
370 #define CHAR_IDCONT 0x02
371 #define CHAR_SIGIL 0x10
372 #define CHAR_USEPAD 0x20
373  
374 static U8 const char_attr[256] = {
375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* NUL to BEL */
376 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* BS to SI */
377 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* DLE to ETB */
378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* CAN to US */
379 0x00, 0x00, 0x00, 0x00, 0x30, 0x30,
380 Q_CODE_AS_STATE_IN_PAD ? 0x30 : 0x10,
381 0x00, /* SP to ' */
382 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, /* ( to / */
383 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0 to 7 */
384 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8 to ? */
385 0x30, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* @ to G */
386 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* H to O */
387 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* P to W */
388 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03, /* X to _ */
389 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* ` to g */
390 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* h to o */
391 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, /* p to w */
392 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, /* x to DEL */
393 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
394 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
395 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
396 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
397 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
398 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
399 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
400 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
401 };
402  
403 #define name_key(sigil, name) THX_name_key(aTHX_ sigil, name)
404 24042 static SV *THX_name_key(pTHX_ char sigil, SV *name)
405 {
406 char const *p, *q, *end;
407 STRLEN len;
408 SV *key;
409 24042 p = SvPV(name, len);
410 24042 end = p + len;
411 24042 ▶ if(sigil == 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if(sigil == 'N') { 470 23572 24042
412 470 sigil = *p++;
413 470 ▶ if(!(char_attr[(U8)sigil] & CHAR_SIGIL)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!(char_attr[(U8)sigil] & CHAR_SIGIL)) return NULL; 5 465 470
414 23572 ▶ } else if(sigil == 'P') {
Branch: 1 branch
BranchTrueFalseTotal
} else if(sigil == 'P') { 23211 361 23572
415 23211 ▶ if(strnNE(p, LVOURPREFIX, LVOURPREFIXLEN)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(strnNE(p, LVOURPREFIX, LVOURPREFIXLEN)) return NULL; 23023 188 23211
416 188 p += LVOURPREFIXLEN;
417 188 sigil = *p++;
418 188 ▶ if(!(char_attr[(U8)sigil] & CHAR_SIGIL)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!(char_attr[(U8)sigil] & CHAR_SIGIL)) return NULL; 0 188 188
419 188 ▶ if(p[0] != ':' || p[1] != ':') return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if(p[0] != ':' || p[1] != ':') return NULL; 188 0 188
if(p[0] != ':' || p[1] != ':') return NULL; 0 188 188
420 188 p += 2;
421 }
422 1014 ▶ if(!(char_attr[(U8)*p] & CHAR_IDSTART)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!(char_attr[(U8)*p] & CHAR_IDSTART)) return NULL; 8 1006 1014
423 3452 ▶ for(q = p+1; q != end; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for(q = p+1; q != end; q++) { 2490 962 3452
424 2490 ▶ if(!(char_attr[(U8)*q] & CHAR_IDCONT)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!(char_attr[(U8)*q] & CHAR_IDCONT)) return NULL; 44 2446 2490
425 }
426 962 key = sv_2mortal(newSV(KEYPREFIXLEN + 1 + (end-p)));
427 962 sv_setpvs(key, KEYPREFIX "?");
428 962 SvPVX(key)[KEYPREFIXLEN] = sigil;
429 962 sv_catpvn(key, p, end-p);
430 962 return key;
431 }
432  
433 /*
434 * compiling code that uses Lexical::Var lexical variables
435 */
436  
437 #define gv_mark_multi(name) THX_gv_mark_multi(aTHX_ name)
438 25 static void THX_gv_mark_multi(pTHX_ SV *name)
439 {
440 GV *gv;
441 #ifdef gv_fetchsv
442 25 gv = gv_fetchsv(name, GV_NOADD_NOINIT|GV_NOEXPAND|GV_NOTQUAL,
443 SVt_PVGV);
444 #else /* !gv_fetchsv */
445 gv = gv_fetchpv(SvPVX(name), 0, SVt_PVGV);
446 #endif /* !gv_fetchsv */
447 25 ▶ if(gv && SvTYPE(gv) == SVt_PVGV) GvMULTI_on(gv);
Branch: 2 branches
BranchTrueFalseTotal
if(gv && SvTYPE(gv) == SVt_PVGV) GvMULTI_on(gv); 25 0 25
if(gv && SvTYPE(gv) == SVt_PVGV) GvMULTI_on(gv); 25 0 25
448 25 }
449  
450 #define Q_NEED_FAKE_REFERENT Q_PERL_VERSION_LT(5,21,4)
451  
452 #if Q_NEED_FAKE_REFERENT
453 # if Q_USE_THREADS
454 # define fakeSV_inc() newSV(0)
455 # define fakeAV_inc() ((SV*)newAV())
456 # define fakeHV_inc() ((SV*)newHV())
457 # else /* !Q_USE_THREADS */
458 static SV *fake_sv, *fake_av, *fake_hv;
459 # define fakeSV_inc() SvREFCNT_inc(fake_sv)
460 # define fakeAV_inc() SvREFCNT_inc(fake_av)
461 # define fakeHV_inc() SvREFCNT_inc(fake_hv)
462 # endif /* !Q_USE_THREADS */
463 #endif /* Q_NEED_FAKE_REFERENT */
464  
465 #define myck_rv2xv(o, sigil, THX_nxck) THX_myck_rv2xv(aTHX_ o, sigil, THX_nxck)
466 32434 static OP *THX_myck_rv2xv(pTHX_ OP *o, char sigil, OP *(*THX_nxck)(pTHX_ OP *o))
467 {
468 OP *c;
469 SV *ref, *key;
470 HE *he;
471 32434 ▶ if((o->op_flags & OPf_KIDS) && (c = cUNOPx(o)->op_first) &&
Branch: 2 branches
BranchTrueFalseTotal
if((o->op_flags & OPf_KIDS) && (c = cUNOPx(o)->op_first) && 32434 0 32434
if((o->op_flags & OPf_KIDS) && (c = cUNOPx(o)->op_first) && 32434 0 32434
472 32434 ▶ c->op_type == OP_CONST &&
Branch: 1 branch
BranchTrueFalseTotal
c->op_type == OP_CONST && 23574 8860 32434
473 23574 ▶ (c->op_private & (OPpCONST_ENTERED|OPpCONST_BARE)) &&
Branch: 1 branch
BranchTrueFalseTotal
(c->op_private & (OPpCONST_ENTERED|OPpCONST_BARE)) && 23391 183 23574
474 46782 ▶ (ref = cSVOPx(c)->op_sv) && SvPOK(ref) &&
Branch: 1 branch
BranchTrueFalseTotal
(ref = cSVOPx(c)->op_sv) && SvPOK(ref) && 23391 0 23391
475 23391 (key = name_key(sigil, ref))) {
476 334 ▶ if((he = hv_fetch_ent(GvHV(PL_hintgv), key, 0, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if((he = hv_fetch_ent(GvHV(PL_hintgv), key, 0, 0))) { 196 138 334
477 SV *hintref, *referent, *newref;
478 #if Q_NEED_FAKE_REFERENT
479 SV *fake_referent;
480 #endif /* Q_NEED_FAKE_REFERENT */
481 OP *newop;
482 U16 type, flags;
483 #if Q_PERL_VERSION_LT(5,11,2)
484 if(sigil == '&' && (c->op_private & OPpCONST_BARE))
485 croak("can't reference Lexical::Var "
486 "lexical subroutine "
487 "without & sigil on this perl");
488 #endif /* <5.11.2 */
489 196 ▶ if(sigil != 'P' || Q_PERL_VERSION_LT(5,8,0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sigil != 'P' || Q_PERL_VERSION_LT(5,8,0)) { 25 171 196
490 /*
491 * A bogus symbol lookup has already been
492 * done (by the tokeniser) based on the name
493 * we're using, to support the package-based
494 * interpretation that we're about to
495 * replace. This can cause bogus "used only
496 * once" warnings. The best we can do here
497 * is to flag the symbol as multiply-used to
498 * suppress that warning, though this is at
499 * the risk of muffling an accurate warning.
500 */
501 25 gv_mark_multi(ref);
502 }
503 /*
504 * The base checker for rv2Xv checks that the
505 * item being pointed to by the constant ref is of
506 * an appropriate type. There are two problems with
507 * this check. Firstly, it rejects GVs as a scalar
508 * target, whereas they are in fact valid. (This
509 * is in RT as bug #69456 so may be fixed.) Second,
510 * and more serious, sometimes a reference is being
511 * constructed through the wrong op type. An array
512 * indexing expression "$foo[0]" gets constructed as
513 * an rv2sv op, because of the "$" sigil, and then
514 * gets munged later. We have to detect the real
515 * intended type through the pad entry, which the
516 * tokeniser has worked out in advance, and then
517 * work through the wrong op. So it's a bit cheeky
518 * for perl to complain about the wrong type here.
519 * We work around it by making the constant ref
520 * initially point to an innocuous item to pass the
521 * type check, then changing it to the real
522 * reference later.
523 */
524 196 hintref = HeVAL(he);
525 196 ▶ if(!SvROK(hintref))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(hintref)) 0 196 196
526 0 croak("non-reference hint for Lexical::Var");
527 196 referent = SvREFCNT_inc(SvRV(hintref));
528 196 type = o->op_type;
529 196 flags = o->op_flags | (((U16)o->op_private) << 8);
530 196 ▶ if(type == OP_RV2SV && sigil == 'P' &&
Branch: 2 branches
BranchTrueFalseTotal
if(type == OP_RV2SV && sigil == 'P' && 118 78 196
if(type == OP_RV2SV && sigil == 'P' && 118 0 118
531 118 ▶ SvPVX(ref)[LVOURPREFIXLEN] == '$' &&
Branch: 1 branch
BranchTrueFalseTotal
SvPVX(ref)[LVOURPREFIXLEN] == '$' && 110 8 118
532 110 ▶ SvREADONLY(referent)) {
Branch: 1 branch
BranchTrueFalseTotal
SvREADONLY(referent)) { 30 80 110
533 30 op_free(o);
534 30 return newOP_const_identity(referent);
535 }
536 #if Q_NEED_FAKE_REFERENT
537 switch(type) {
538 case OP_RV2SV:
539 fake_referent = fakeSV_inc();
540 break;
541 case OP_RV2AV:
542 fake_referent = fakeAV_inc();
543 break;
544 case OP_RV2HV:
545 fake_referent = fakeHV_inc();
546 break;
547 default: fake_referent = NULL; break;
548 }
549 if(fake_referent) {
550 newref = newRV_noinc(fake_referent);
551 SvREFCNT_inc(newref);
552 newop = newUNOP(type, flags,
553 newSVOP(OP_CONST, 0, newref));
554 fake_referent = SvRV(newref);
555 SvREADONLY_fully_off(newref);
556 SvRV_set(newref, referent);
557 SvREADONLY_fully_on(newref);
558 SvREFCNT_dec(fake_referent);
559 SvREFCNT_dec(newref);
560 } else
561 #endif /* Q_NEED_FAKE_REFERENT */
562 {
563 166 newref = newRV_noinc(referent);
564 166 newop = newUNOP(type, flags,
565 newSVOP(OP_CONST, 0, newref));
566 }
567 166 op_free(o);
568 166 return newop;
569 138 ▶ } else if(sigil == 'P') {
Branch: 1 branch
BranchTrueFalseTotal
} else if(sigil == 'P') { 17 121 138
570 SV *newref;
571 U16 type, flags;
572 /*
573 * Not a name that we have a defined meaning for,
574 * but it has the form of the "our" hack, implying
575 * that we did put an entry in the pad for it.
576 * Munge this back to what it would have been
577 * without the pad entry. This should mainly
578 * happen due to explicit unimportation, but it
579 * might also happen if the scoping of the pad and
580 * %^H ever get out of synch.
581 */
582 17 newref = newSVpvn(SvPVX(ref)+LVOURPREFIXLEN+3,
583 SvCUR(ref)-LVOURPREFIXLEN-3);
584 17 ▶ if(SvUTF8(ref)) SvUTF8_on(newref);
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(ref)) SvUTF8_on(newref); 0 17 17
585 17 type = o->op_type;
586 17 flags = o->op_flags | (((U16)o->op_private) << 8);
587 17 op_free(o);
588 17 return newUNOP(type, flags,
589 newSVOP(OP_CONST, 0, newref));
590 }
591 }
592 32221 return THX_nxck(aTHX_ o);
593 }
594  
595 static OP *(*THX_nxck_rv2sv)(pTHX_ OP *o);
596 static OP *(*THX_nxck_rv2av)(pTHX_ OP *o);
597 static OP *(*THX_nxck_rv2hv)(pTHX_ OP *o);
598 static OP *(*THX_nxck_rv2cv)(pTHX_ OP *o);
599 static OP *(*THX_nxck_rv2gv)(pTHX_ OP *o);
600  
601 8684 static OP *THX_myck_rv2sv(pTHX_ OP *o) {
602 8684 return myck_rv2xv(o, 'P', THX_nxck_rv2sv);
603 }
604 4431 static OP *THX_myck_rv2av(pTHX_ OP *o) {
605 4431 return myck_rv2xv(o, 'P', THX_nxck_rv2av);
606 }
607 6892 static OP *THX_myck_rv2hv(pTHX_ OP *o) {
608 6892 return myck_rv2xv(o, 'P', THX_nxck_rv2hv);
609 }
610 12008 static OP *THX_myck_rv2cv(pTHX_ OP *o) {
611 12008 return myck_rv2xv(o, Q_CODE_AS_STATE_IN_PAD ? 'P' : '&',
612 THX_nxck_rv2cv);
613 }
614 419 static OP *THX_myck_rv2gv(pTHX_ OP *o) {
615 419 return myck_rv2xv(o, '*', THX_nxck_rv2gv);
616 }
617  
618 /*
619 * setting up Lexical::Var lexical names
620 */
621  
622 #if !Q_USE_THREADS
623 static HV *lvour_sv_stash, *lvour_av_stash, *lvour_hv_stash;
624 # if Q_CODE_AS_STATE_IN_PAD
625 static HV *lvour_cv_stash;
626 # endif /* Q_CODE_AS_STATE_IN_PAD */
627 #endif /* !Q_USE_THREADS */
628  
629 #define lvour_stash(sigil) THX_lvour_stash(aTHX_ sigil)
630 228 static HV *THX_lvour_stash(pTHX_ char sigil)
631 {
632 #if Q_USE_THREADS
633 if(sigil == '$' || sigil == '@' || sigil == '%' ||
634 (Q_CODE_AS_STATE_IN_PAD && sigil == '&')) {
635 char sname[LVOURPREFIXLEN+2];
636 memcpy(sname, LVOURPREFIX, LVOURPREFIXLEN);
637 sname[LVOURPREFIXLEN] = sigil;
638 sname[LVOURPREFIXLEN+1] = 0;
639 return gv_stashpvn(sname, LVOURPREFIXLEN+1, GV_ADD);
640 } else {
641 return NULL;
642 }
643 #else /* !Q_USE_THREADS */
644 PERL_UNUSED_THX();
645 # if Q_CODE_AS_STATE_IN_PAD
646 228 ▶ if(sigil == '&') return lvour_cv_stash;
Branch: 1 branch
BranchTrueFalseTotal
if(sigil == '&') return lvour_cv_stash; 30 198 228
647 # endif /* Q_CODE_AS_STATE_IN_PAD */
648 226 ▶ return sigil == '$' ? lvour_sv_stash : sigil == '@' ? lvour_av_stash :
Branch: 2 branches
BranchTrueFalseTotal
return sigil == '$' ? lvour_sv_stash : sigil == '@' ? lvour_av_stash : 57 141 198
return sigil == '$' ? lvour_sv_stash : sigil == '@' ? lvour_av_stash : 28 29 57
649 28 ▶ sigil == '%' ? lvour_hv_stash : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sigil == '%' ? lvour_hv_stash : NULL; 28 0 28
650 #endif /* !Q_USE_THREADS */
651 }
652  
653 #define padseq_intro() THX_padseq_intro(aTHX)
654 506 static U32 THX_padseq_intro(pTHX)
655 {
656 #if Q_PERL_VERSION_GE(5,13,10)
657 PERL_UNUSED_THX();
658 506 return PERL_PADSEQ_INTRO;
659 #elif Q_PERL_VERSION_GE(5,9,5)
660 PERL_UNUSED_THX();
661 return I32_MAX;
662 #elif Q_PERL_VERSION_GE(5,9,0)
663 PERL_UNUSED_THX();
664 return 999999999;
665 #elif Q_PERL_VERSION_GE(5,8,0)
666 static U32 max;
667 if(!max) {
668 SV *versv = get_sv("]", 0);
669 char *verp = SvPV_nolen(versv);
670 max = strGE(verp, "5.008009") ? I32_MAX : 999999999;
671 }
672 return max;
673 #else /* <5.8.0 */
674 PERL_UNUSED_THX();
675 return 999999999;
676 #endif /* <5.8.0 */
677 }
678  
679 #define find_compcv(vari_word) THX_find_compcv(aTHX_ vari_word)
680 659 static CV *THX_find_compcv(pTHX_ char const *vari_word)
681 {
682 CV *compcv;
683 #if Q_PERL_VERSION_GE(5,17,5)
684 659 ▶ if(!((compcv = PL_compcv) && CvPADLIST(compcv)))
Branch: 2 branches
BranchTrueFalseTotal
if(!((compcv = PL_compcv) && CvPADLIST(compcv))) 657 2 659
if(!((compcv = PL_compcv) && CvPADLIST(compcv))) 0 657 657
685 2 compcv = NULL;
686 #else /* <5.17.5 */
687 GV *compgv;
688 /*
689 * Given that we're being invoked from a BEGIN block,
690 * PL_compcv here doesn't actually point to the sub
691 * being compiled. Instead it points to the BEGIN block.
692 * The code that we want to affect is the parent of that.
693 * Along the way, better check that we are actually being
694 * invoked that way: PL_compcv may be null, indicating
695 * runtime, or it can be non-null in a couple of
696 * other situations (require, string eval).
697 */
698 if(!(PL_compcv && CvSPECIAL(PL_compcv) &&
699 (compgv = CvGV(PL_compcv)) &&
700 strEQ(GvNAME(compgv), "BEGIN") &&
701 (compcv = CvOUTSIDE(PL_compcv)) &&
702 CvPADLIST(compcv)))
703 compcv = NULL;
704 #endif /* <5.17.5 */
705 659 ▶ if(!compcv)
Branch: 1 branch
BranchTrueFalseTotal
if(!compcv) 2 657 659
706 2 croak("can't set up Lexical::Var lexical %s "
707 "outside compilation",
708 vari_word);
709 657 return compcv;
710 }
711  
712 #define setup_pad(compcv, name, referent) \
713 THX_setup_pad(aTHX_ compcv, name, referent)
714 506 static void THX_setup_pad(pTHX_ CV *compcv, char const *name, SV *referent)
715 {
716 506 PADLIST *padlist = CvPADLIST(compcv);
717 506 PADNAMELIST *padname = PadlistNAMES(padlist);
718 506 PAD *padvar = PadlistARRAY(padlist)[1];
719 PADOFFSET ouroffset;
720 PADNAME *ourname;
721 SV *ourvar;
722 #if !Q_CODE_AS_STATE_IN_PAD
723 PERL_UNUSED_ARG(referent);
724 #endif /* !Q_CODE_AS_STATE_IN_PAD */
725 506 ourname = newPADNAMEpvn(name, strlen(name));
726 506 COP_SEQ_RANGE_LOW_set(ourname, PL_cop_seqmax);
727 506 COP_SEQ_RANGE_HIGH_set(ourname, padseq_intro());
728 506 ▶ COP_SEQMAX_INC;
Branch: 2 branches
BranchTrueFalseTotal
COP_SEQMAX_INC; 0 506 506
COP_SEQMAX_INC; 0 0 0
729 #if Q_CODE_AS_STATE_IN_PAD
730 506 ▶ if(referent) {
Branch: 1 branch
BranchTrueFalseTotal
if(referent) { 292 214 506
731 292 PadnameIsSTATE_on(ourname);
732 292 ourvar = SvREFCNT_inc(referent);
733 } else
734 #endif /* Q_CODE_AS_STATE_IN_PAD */
735 {
736 214 HV *stash = lvour_stash(name[0]);
737 214 PadnameIsOUR_on(ourname);
738 214 PadnameOURSTASH_set(ourname, (HV*)SvREFCNT_inc((SV*)stash));
739 214 ourvar = newSV(0);
740 214 SvPADMY_on(ourvar);
741 }
742 506 ouroffset = PadMAX(padvar) + 1;
743 506 padnamelist_store(padname, ouroffset, ourname);
744 #ifdef PadnamelistMAXNAMED
745 506 PadnamelistMAXNAMED(padname) = ouroffset;
746 #endif /* PadnamelistMAXNAMED */
747 506 av_store(padvar, ouroffset, ourvar);
748 506 ▶ if(PL_comppad == padvar) PL_curpad = PadARRAY(padvar);
Branch: 1 branch
BranchTrueFalseTotal
if(PL_comppad == padvar) PL_curpad = PadARRAY(padvar); 0 506 506
749 506 }
750  
751 122 static int svt_scalar(svtype t)
752 {
753 122 ▶ switch(t) {
Branch: 1 branch
BranchTrueFalseTotal
switch(t) { 115 7 122
754 115 case SVt_NULL: case SVt_IV: case SVt_NV:
755 #if Q_PERL_VERSION_LT(5,11,0)
756 case SVt_RV:
757 #endif /* <5.11.0 */
758 case SVt_PV: case SVt_PVIV: case SVt_PVNV:
759 case SVt_PVMG: case SVt_PVLV: case SVt_PVGV:
760 #if Q_PERL_VERSION_GE(5,11,0)
761 case SVt_REGEXP:
762 #endif /* >=5.11.0 */
763 115 return 1;
764 7 default:
765 7 return 0;
766 }
767 }
768  
769 enum { PADLOOKUP_NOTHING, PADLOOKUP_STATE, PADLOOKUP_LVOUR, PADLOOKUP_OTHER };
770  
771 #define pad_lookup(compcv, name, value_ptr) \
772 THX_pad_lookup(aTHX_ compcv, name, value_ptr)
773 26 static int THX_pad_lookup(pTHX_ CV *compcv, char const *name, SV **value_ptr)
774 {
775 26 STRLEN namelen = strlen(name);
776 26 CV *cv = compcv;
777 26 U32 seq = PL_cop_seqmax;
778 29 ▶ for(; cv;
Branch: 1 branch
BranchTrueFalseTotal
for(; cv; 29 0 29
779 #ifdef CvOUTSIDE_SEQ
780 3 seq = CvOUTSIDE_SEQ(cv),
781 #endif /* CvOUTSIDE_SEQ */
782 3 cv = CvOUTSIDE(cv)) {
783 29 PADLIST *padlist = CvPADLIST(cv);
784 PADNAMELIST *padname;
785 PAD *pad;
786 PADOFFSET off;
787 #ifdef CvOUTSIDE_SEQ
788 29 PADOFFSET outer_off = 0;
789 #endif /* CvOUTSIDE_SEQ */
790 PADNAME *pname;
791 29 ▶ if(!padlist) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(!padlist) continue; 0 29 29
792 29 padname = PadlistNAMES(padlist);
793 29 pad = PadlistARRAY(padlist)[1];
794 #ifdef PadnamelistMAXNAMED
795 29 off = PadnamelistMAXNAMED(padname);
796 #else /* !PadnamelistMAXNAMED */
797 off = PadMAX(pad);
798 #endif /* PadnamelistMAXNAMED */
799 38 ▶ for(; off != 0; off--) {
Branch: 1 branch
BranchTrueFalseTotal
for(; off != 0; off--) { 35 3 38
800 char *pnamepv;
801 35 pname = padnamelist_fetch(padname, off);
802 35 ▶ if(!pname) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(!pname) continue; 0 35 35
803 #if Q_PERL_VERSION_LT(5,19,3)
804 if(pname == &PL_sv_undef) continue;
805 #endif /* <5.19.3 */
806 35 pnamepv = PadnamePV(pname);
807 35 ▶ if(!(pnamepv && PadnameLEN(pname) == namelen &&
Branch: 2 branches
BranchTrueFalseTotal
if(!(pnamepv && PadnameLEN(pname) == namelen && 35 0 35
if(!(pnamepv && PadnameLEN(pname) == namelen && 34 1 35
808 34 ▶ memcmp(pnamepv, name, namelen) == 0))
Branch: 1 branch
BranchTrueFalseTotal
memcmp(pnamepv, name, namelen) == 0)) 8 26 34
809 9 continue;
810 #ifdef CvOUTSIDE_SEQ
811 26 ▶ if(PadnameOUTER(pname)) {
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameOUTER(pname)) { 0 26 26
812 0 outer_off = off;
813 0 continue;
814 }
815 #endif /* CvOUTSIDE_SEQ */
816 26 ▶ if(!PadnameIN_SCOPE(pname, seq)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(!PadnameIN_SCOPE(pname, seq)) continue; 0 26 26
817 #ifdef CvOUTSIDE_SEQ
818 26 found:
819 #endif /* CvOUTSIDE_SEQ */
820 26 ▶ if(PadnameIsSTATE(pname)) {
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsSTATE(pname)) { 12 14 26
821 12 *value_ptr = *av_fetch(pad, off, 0);
822 12 return PADLOOKUP_STATE;
823 28 } else if(PadnameIsOUR(pname) &&
824 14 PadnameOURSTASH(pname) ==
825 14 lvour_stash(name[0])) {
826 8 return PADLOOKUP_LVOUR;
827 } else {
828 6 return PADLOOKUP_OTHER;
829 }
830 }
831 #ifdef CvOUTSIDE_SEQ
832 3 ▶ if(outer_off) {
Branch: 1 branch
BranchTrueFalseTotal
if(outer_off) { 0 3 3
833 0 off = outer_off;
834 0 pname = padnamelist_fetch(padname, off);
835 0 goto found;
836 }
837 #endif /* CvOUTSIDE_SEQ */
838 }
839 0 return PADLOOKUP_NOTHING;
840 }
841  
842 #define current_referent(key) THX_current_referent(aTHX_ compcv, key)
843 28 static SV *THX_current_referent(pTHX_ CV *compcv, SV *key)
844 {
845 static SV sv_other;
846 28 char *keypv = SvPVX(key);
847 28 char sigil = keypv[KEYPREFIXLEN];
848 28 ▶ if(!(sigil == '*' || (Q_CODE_OUTSIDE_PAD && sigil == '&'))) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(sigil == '*' || (Q_CODE_OUTSIDE_PAD && sigil == '&'))) { 26 2 28
849 SV *state_value;
850 int padstate =
851 26 pad_lookup(compcv, keypv+KEYPREFIXLEN, &state_value);
852 if(Q_CODE_CLASHES_WITH_PAD && sigil == '&') {
853 if(padstate != PADLOOKUP_NOTHING)
854 18 return &sv_other;
855 } else {
856 26 ▶ if(padstate == PADLOOKUP_NOTHING) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(padstate == PADLOOKUP_NOTHING) return NULL; 0 26 26
857 26 ▶ if(Q_CODE_AS_STATE_IN_PAD && sigil == '&' &&
Branch: 2 branches
BranchTrueFalseTotal
if(Q_CODE_AS_STATE_IN_PAD && sigil == '&' && 16 10 26
if(Q_CODE_AS_STATE_IN_PAD && sigil == '&' && 12 4 16
858 padstate == PADLOOKUP_STATE)
859 12 return state_value;
860 14 ▶ if(padstate != PADLOOKUP_LVOUR)
Branch: 1 branch
BranchTrueFalseTotal
if(padstate != PADLOOKUP_LVOUR) 6 8 14
861 6 return &sv_other;
862 }
863 }
864 {
865 SV *cref;
866 10 HE *he = hv_fetch_ent(GvHV(PL_hintgv), key, 0, 0);
867 10 ▶ if(!he) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!he) return NULL; 0 10 10
868 10 cref = HeVAL(he);
869 10 ▶ if(!SvROK(cref)) return &sv_other;
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(cref)) return &sv_other; 0 10 10
870 10 return SvRV(cref);
871 }
872 }
873  
874 #if Q_CODE_CLASHES_WITH_PAD
875 # define check_for_pad_clash(compcv, name) \
876 THX_check_for_pad_clash(aTHX_ compcv, name)
877 static void THX_check_for_pad_clash(pTHX_ CV *compcv, char const *name)
878 {
879 SV *state_value;
880 if(name[0] == '&' &&
881 pad_lookup(compcv, name, &state_value) !=
882 PADLOOKUP_NOTHING)
883 croak("can't shadow core lexical subroutine");
884 }
885 #else /* !Q_CODE_CLASHES_WITH_PAD */
886 # define check_for_pad_clash(compcv, name) ((void) 0)
887 #endif /* !Q_CODE_CLASHES_WITH_PAD */
888  
889 #define import(base_sigil, vari_word) THX_import(aTHX_ base_sigil, vari_word)
890 592 static void THX_import(pTHX_ char base_sigil, char const *vari_word)
891 {
892 592 dXSARGS;
893 CV *compcv;
894 int i;
895 592 SP -= items;
896 592 ▶ if(items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if(items < 1) 0 592 592
897 0 croak("too few arguments for import");
898 592 ▶ if(items == 1)
Branch: 1 branch
BranchTrueFalseTotal
if(items == 1) 4 588 592
899 4 croak("%" SVf " does no default importation", SVfARG(ST(0)));
900 588 ▶ if(!(items & 1))
Branch: 1 branch
BranchTrueFalseTotal
if(!(items & 1)) 4 584 588
901 4 croak("import list for %" SVf
902 " must alternate name and reference", SVfARG(ST(0)));
903 584 compcv = find_compcv(vari_word);
904 583 PL_hints |= HINT_LOCALIZE_HH;
905 583 gv_HVadd(PL_hintgv);
906 1071 ▶ for(i = 1; i != items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i != items; i += 2) { 586 485 1071
907 586 SV *name = ST(i), *ref = ST(i+1), *key, *val, *referent;
908 svtype rt;
909 bool rok;
910 char const *vt;
911 char sigil;
912 HE *he;
913 586 ▶ if(!sv_is_string(name))
Branch: 3 branches
BranchTrueFalseTotal
if(!sv_is_string(name)) 586 0 586
if(!sv_is_string(name)) 586 0 586
if(!sv_is_string(name)) 6 580 586
914 6 croak("%s name is not a string", vari_word);
915 580 key = name_key(base_sigil, name);
916 580 ▶ if(!key) croak("malformed %s name", vari_word);
Branch: 1 branch
BranchTrueFalseTotal
if(!key) croak("malformed %s name", vari_word); 14 566 580
917 566 sigil = SvPVX(key)[KEYPREFIXLEN];
918 566 ▶ rt = SvROK(ref) ? SvTYPE(SvRV(ref)) : SVt_LAST;
Branch: 1 branch
BranchTrueFalseTotal
rt = SvROK(ref) ? SvTYPE(SvRV(ref)) : SVt_LAST; 564 2 566
919 566 switch(sigil) {
920 122 case '$': rok = svt_scalar(rt); vt="scalar"; break;
921 43 case '@': rok = rt == SVt_PVAV; vt="array"; break;
922 42 case '%': rok = rt == SVt_PVHV; vt="hash"; break;
923 321 case '&': rok = rt == SVt_PVCV; vt="code"; break;
924 38 case '*': rok = rt == SVt_PVGV; vt="glob"; break;
925 0 default: rok = 0; vt = "wibble"; break;
926 }
927 566 ▶ if(!rok) croak("%s is not %s reference", vari_word, vt);
Branch: 1 branch
BranchTrueFalseTotal
if(!rok) croak("%s is not %s reference", vari_word, vt); 78 488 566
928 check_for_pad_clash(compcv, SvPVX(key)+KEYPREFIXLEN);
929 488 referent = SvRV(ref);
930 488 ▶ if(char_attr[(U8)sigil] & CHAR_USEPAD)
Branch: 1 branch
BranchTrueFalseTotal
if(char_attr[(U8)sigil] & CHAR_USEPAD) 464 24 488
931 464 ▶ setup_pad(compcv, SvPVX(key)+KEYPREFIXLEN,
Branch: 1 branch
BranchTrueFalseTotal
setup_pad(compcv, SvPVX(key)+KEYPREFIXLEN, 292 172 464
932 Q_CODE_AS_STATE_IN_PAD && sigil == '&' ?
933 referent : NULL);
934 488 val = newRV_inc(referent);
935 488 he = hv_store_ent(GvHV(PL_hintgv), key, val, 0);
936 488 ▶ if(he) {
Branch: 1 branch
BranchTrueFalseTotal
if(he) { 488 0 488
937 488 val = HeVAL(he);
938 488 ▶ SvSETMAGIC(val);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(val); 488 0 488
939 } else {
940 0 SvREFCNT_dec(val);
941 }
942 }
943 485 PUTBACK;
944 485 }
945  
946 #define unimport(base_sigil, vari_word) \
947 THX_unimport(aTHX_ base_sigil, vari_word)
948 79 static void THX_unimport(pTHX_ char base_sigil, char const *vari_word)
949 {
950 79 dXSARGS;
951 CV *compcv;
952 int i;
953 79 SP -= items;
954 79 ▶ if(items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if(items < 1) 0 79 79
955 0 croak("too few arguments for unimport");
956 79 ▶ if(items == 1)
Branch: 1 branch
BranchTrueFalseTotal
if(items == 1) 4 75 79
957 4 croak("%" SVf " does no default unimportation", SVfARG(ST(0)));
958 75 compcv = find_compcv(vari_word);
959 74 PL_hints |= HINT_LOCALIZE_HH;
960 74 gv_HVadd(PL_hintgv);
961 136 ▶ for(i = 1; i != items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i != items; i++) { 77 59 136
962 77 SV *name = ST(i), *ref, *key;
963 char sigil;
964 77 ▶ if(!sv_is_string(name))
Branch: 3 branches
BranchTrueFalseTotal
if(!sv_is_string(name)) 77 0 77
if(!sv_is_string(name)) 77 0 77
if(!sv_is_string(name)) 6 71 77
965 6 croak("%s name is not a string", vari_word);
966 71 key = name_key(base_sigil, name);
967 71 ▶ if(!key) croak("malformed %s name", vari_word);
Branch: 1 branch
BranchTrueFalseTotal
if(!key) croak("malformed %s name", vari_word); 9 62 71
968 62 sigil = SvPVX(key)[KEYPREFIXLEN];
969 62 ▶ if(i != items && (ref = ST(i+1), SvROK(ref))) {
Branch: 2 branches
BranchTrueFalseTotal
if(i != items && (ref = ST(i+1), SvROK(ref))) { 62 0 62
if(i != items && (ref = ST(i+1), SvROK(ref))) { 28 34 62
970 28 i++;
971 28 ▶ if(current_referent(key) != SvRV(ref))
Branch: 1 branch
BranchTrueFalseTotal
if(current_referent(key) != SvRV(ref)) 15 13 28
972 15 continue;
973 }
974 check_for_pad_clash(compcv, SvPVX(key)+KEYPREFIXLEN);
975 47 (void) hv_delete_ent(GvHV(PL_hintgv), key, G_DISCARD, 0);
976 47 ▶ if(char_attr[(U8)sigil] & CHAR_USEPAD)
Branch: 1 branch
BranchTrueFalseTotal
if(char_attr[(U8)sigil] & CHAR_USEPAD) 42 5 47
977 42 setup_pad(compcv, SvPVX(key)+KEYPREFIXLEN, NULL);
978 }
979 59 PUTBACK;
980 59 }
981  
982 MODULE = Lexical::Var PACKAGE = Lexical::Var
983  
984 PROTOTYPES: DISABLE
985  
986 BOOT:
987 #if !Q_USE_THREADS
988 # if Q_NEED_FAKE_REFERENT
989 fake_sv = newSV(0);
990 fake_av = (SV*)newAV();
991 fake_hv = (SV*)newHV();
992 # endif /* Q_NEED_FAKE_REFERENT */
993 39 lvour_sv_stash = gv_stashpvs(LVOURPREFIX "$", 1);
994 39 lvour_av_stash = gv_stashpvs(LVOURPREFIX "@", 1);
995 39 lvour_hv_stash = gv_stashpvs(LVOURPREFIX "%", 1);
996 # if Q_CODE_AS_STATE_IN_PAD
997 39 lvour_cv_stash = gv_stashpvs(LVOURPREFIX "&", 1);
998 # endif /* Q_CODE_AS_STATE_IN_PAD */
999 #endif /* !Q_USE_THREADS */
1000 39 wrap_op_checker(OP_RV2SV, THX_myck_rv2sv, &THX_nxck_rv2sv);
1001 39 wrap_op_checker(OP_RV2AV, THX_myck_rv2av, &THX_nxck_rv2av);
1002 39 wrap_op_checker(OP_RV2HV, THX_myck_rv2hv, &THX_nxck_rv2hv);
1003 39 wrap_op_checker(OP_RV2CV, THX_myck_rv2cv, &THX_nxck_rv2cv);
1004 39 wrap_op_checker(OP_RV2GV, THX_myck_rv2gv, &THX_nxck_rv2gv);
1005  
1006 void
1007 import(SV *classname, ...)
1008 PPCODE:
1009 PERL_UNUSED_VAR(classname);
1010 426 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 426 426
1011 /* the modified SP is intentionally lost here */
1012 426 import('N', "variable");
1013 347 SPAGAIN;
1014  
1015 void
1016 unimport(SV *classname, ...)
1017 PPCODE:
1018 PERL_UNUSED_VAR(classname);
1019 54 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 54 54
1020 /* the modified SP is intentionally lost here */
1021 54 unimport('N', "variable");
1022 43 SPAGAIN;
1023  
1024 MODULE = Lexical::Var PACKAGE = Lexical::Sub
1025  
1026 void
1027 import(SV *classname, ...)
1028 PPCODE:
1029 PERL_UNUSED_VAR(classname);
1030 166 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 166 166
1031 /* the modified SP is intentionally lost here */
1032 166 import('&', "subroutine");
1033 138 SPAGAIN;
1034  
1035 void
1036 unimport(SV *classname, ...)
1037 PPCODE:
1038 PERL_UNUSED_VAR(classname);
1039 25 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 25 25
1040 /* the modified SP is intentionally lost here */
1041 25 unimport('&', "subroutine");
1042 16 SPAGAIN;
↑ Summary blib/lib/Lexical/Sub.pm »