XS.xs

filter: Statementstmt 79.1% 361 / 456 Branchbran 56.6% 171 / 302 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.1% 532 / 758 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Package/Stash/XS.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #ifndef gv_fetchsv
8 #define gv_fetchsv(n,f,t) gv_fetchpv(SvPV_nolen(n), f, t)
9 #endif
10  
11 #ifndef mro_method_changed_in
12 #define mro_method_changed_in(x) PL_sub_generation++
13 #endif
14  
15 #ifndef HvENAME
16 #define HvENAME HvNAME
17 #endif
18  
19 #ifndef hv_name_set
20 #define hv_name_set(stash, name, namelen, flags) \
21 (HvNAME(stash) = savepvn(name, namelen))
22 #endif
23  
24 #ifdef newSVhek
25 #define newSVhe(he) newSVhek(HeKEY_hek(he))
26 #else
27 #define newSVhe(he) newSVpv(HePV(he, PL_na), 0)
28 #endif
29  
30 #ifndef GvCV_set
31 #define GvCV_set(gv, cv) (GvCV(gv) = (CV*)(cv))
32 #endif
33  
34 #ifndef MUTABLE_PTR
35 #define MUTABLE_PTR(p) ((void *) (p))
36 #endif
37  
38 #ifndef MUTABLE_SV
39 #define MUTABLE_SV(p) ((SV *)MUTABLE_PTR(p))
40 #endif
41  
42 #ifndef SVT_SCALAR
43 #define SVT_SCALAR(svt) (svt <= SVt_PVLV)
44 #endif
45  
46 #ifndef SVT_ARRAY
47 #define SVT_ARRAY(svt) (svt == SVt_PVAV)
48 #endif
49  
50 #ifndef SVT_HASH
51 #define SVT_HASH(svt) (svt == SVt_PVHV)
52 #endif
53  
54 #ifndef SVT_CODE
55 #define SVT_CODE(svt) (svt == SVt_PVCV)
56 #endif
57  
58 #ifndef SVT_IO
59 #define SVT_IO(svt) (svt == SVt_PVIO)
60 #endif
61  
62 #ifndef SVT_FORMAT
63 #define SVT_FORMAT(svt) (svt == SVt_PVFM)
64 #endif
65  
66 /* HACK: scalar slots are always populated on perl < 5.10, so treat undef
67 * as nonexistent. this is consistent with the previous behavior of the pure
68 * perl version of this module (since this is the behavior that perl sees
69 * in all versions */
70 #if PERL_VERSION < 10
71 #define GvSVOK(g) (GvSV(g) && SvTYPE(GvSV(g)) != SVt_NULL)
72 #else
73 #define GvSVOK(g) GvSV(g)
74 #endif
75  
76 #define GvAVOK(g) GvAV(g)
77 #define GvHVOK(g) GvHV(g)
78 #define GvCVOK(g) GvCVu(g) /* XXX: should this really be GvCVu? or GvCV? */
79 #define GvIOOK(g) GvIO(g)
80  
81 /* see above - don't let scalar slots become unpopulated, this breaks
82 * assumptions in core */
83 #if PERL_VERSION < 10
84 #define GvSetSV(g,v) do { \
85 SV *_v = (SV*)(v); \
86 SvREFCNT_dec(GvSV(g)); \
87 if ((GvSV(g) = _v ? _v : newSV(0))) \
88 GvIMPORTED_SV_on(g); \
89 } while (0)
90 #else
91 #define GvSetSV(g,v) do { \
92 SvREFCNT_dec(GvSV(g)); \
93 if ((GvSV(g) = (SV*)(v))) \
94 GvIMPORTED_SV_on(g); \
95 } while (0)
96 #endif
97  
98 #define GvSetAV(g,v) do { \
99 SvREFCNT_dec(GvAV(g)); \
100 if ((GvAV(g) = (AV*)(v))) \
101 GvIMPORTED_AV_on(g); \
102 } while (0)
103 #define GvSetHV(g,v) do { \
104 SvREFCNT_dec(GvHV(g)); \
105 if ((GvHV(g) = (HV*)(v))) \
106 GvIMPORTED_HV_on(g); \
107 } while (0)
108 #define GvSetCV(g,v) do { \
109 SvREFCNT_dec(GvCV(g)); \
110 if ((GvCV_set(g, (CV*)(v)))) { \
111 GvIMPORTED_CV_on(g); \
112 GvASSUMECV_on(g); \
113 } \
114 GvCVGEN(g) = 0; \
115 mro_method_changed_in(GvSTASH(g)); \
116 } while (0)
117 #define GvSetIO(g,v) do { \
118 SvREFCNT_dec(GvIO(g)); \
119 GvIOp(g) = (IO*)(v); \
120 } while (0)
121  
122 typedef enum {
123 VAR_NONE = 0,
124 VAR_SCALAR,
125 VAR_ARRAY,
126 VAR_HASH,
127 VAR_CODE,
128 VAR_IO,
129 VAR_GLOB, /* TODO: unimplemented */
130 VAR_FORMAT /* TODO: unimplemented */
131 } vartype_t;
132  
133 typedef struct {
134 vartype_t type;
135 SV *name;
136 } varspec_t;
137  
138 static U32 name_hash, namespace_hash, type_hash;
139 static SV *name_key, *namespace_key, *type_key;
140 static REGEXP *valid_module_regex;
141  
142 5 static const char *vartype_to_string(vartype_t type)
143 {
144 5 switch (type) {
145 3 case VAR_SCALAR:
146 3 return "SCALAR";
147 1 case VAR_ARRAY:
148 1 return "ARRAY";
149 0 case VAR_HASH:
150 0 return "HASH";
151 0 case VAR_CODE:
152 0 return "CODE";
153 1 case VAR_IO:
154 1 return "IO";
155 0 default:
156 0 return "unknown";
157 }
158 }
159  
160 351 static vartype_t string_to_vartype(char *vartype)
161 {
162 351 ▶ if (strEQ(vartype, "SCALAR")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(vartype, "SCALAR")) { 2 349 351
163 2 return VAR_SCALAR;
164 }
165 349 ▶ else if (strEQ(vartype, "ARRAY")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(vartype, "ARRAY")) { 1 348 349
166 1 return VAR_ARRAY;
167 }
168 348 ▶ else if (strEQ(vartype, "HASH")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(vartype, "HASH")) { 3 345 348
169 3 return VAR_HASH;
170 }
171 345 ▶ else if (strEQ(vartype, "CODE")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(vartype, "CODE")) { 345 0 345
172 345 return VAR_CODE;
173 }
174 0 ▶ else if (strEQ(vartype, "IO")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(vartype, "IO")) { 0 0 0
175 0 return VAR_IO;
176 }
177 else {
178 0 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO', not '%s'", vartype);
179 }
180 }
181  
182 404 static int _is_valid_module_name(SV *package)
183 {
184 char *buf;
185 STRLEN len;
186 SV *sv;
187  
188 404 buf = SvPV(package, len);
189  
190 /* whee cargo cult */
191 404 sv = sv_newmortal();
192 404 sv_upgrade(sv, SVt_PV);
193 404 SvREADONLY_on(sv);
194 404 SvLEN(sv) = 0;
195 404 SvUTF8_on(sv);
196 404 SvPVX(sv) = buf;
197 404 SvCUR_set(sv, len);
198 404 SvPOK_on(sv);
199  
200 404 return pregexec(valid_module_regex, buf, buf + len, buf, 1, sv, 1);
201 }
202  
203 590 static void _deconstruct_variable_name(SV *variable, varspec_t *varspec)
204 {
205 char *varpv;
206  
207 590 ▶ if (!SvCUR(variable))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(variable)) 0 590 590
208 0 croak("You must pass a variable name");
209  
210 590 varspec->name = sv_2mortal(newSVsv(variable));
211  
212 590 varpv = SvPV_nolen(varspec->name);
213 590 switch (varpv[0]) {
214 48 case '$':
215 48 varspec->type = VAR_SCALAR;
216 48 sv_chop(varspec->name, &varpv[1]);
217 48 break;
218 39 case '@':
219 39 varspec->type = VAR_ARRAY;
220 39 sv_chop(varspec->name, &varpv[1]);
221 39 break;
222 35 case '%':
223 35 varspec->type = VAR_HASH;
224 35 sv_chop(varspec->name, &varpv[1]);
225 35 break;
226 448 case '&':
227 448 varspec->type = VAR_CODE;
228 448 sv_chop(varspec->name, &varpv[1]);
229 448 break;
230 20 default:
231 20 varspec->type = VAR_IO;
232 20 break;
233 }
234 590 }
235  
236 0 static void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
237 {
238 HE *val;
239  
240 0 val = hv_fetch_ent(variable, name_key, 0, name_hash);
241 0 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 0 0
242 0 croak("The 'name' key is required in variable specs");
243  
244 0 varspec->name = sv_2mortal(newSVsv(HeVAL(val)));
245  
246 0 val = hv_fetch_ent(variable, type_key, 0, type_hash);
247 0 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 0 0
248 0 croak("The 'type' key is required in variable specs");
249  
250 0 varspec->type = string_to_vartype(SvPV_nolen(HeVAL(val)));
251 0 }
252  
253 590 static void _check_varspec_is_valid(varspec_t *varspec)
254 {
255 1180 ▶ if (strstr(SvPV_nolen(varspec->name), "::")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strstr(SvPV_nolen(varspec->name), "::")) { 3 587 590
256 3 croak("Variable names may not contain ::");
257 }
258 587 }
259  
260 239 static int _valid_for_type(SV *value, vartype_t type)
261 {
262 239 ▶ svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
Branch: 1 branch
BranchTrueFalseTotal
svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL; 235 4 239
263  
264 239 switch (type) {
265 15 case VAR_SCALAR:
266 /* XXX is a glob a scalar? assigning a glob to the scalar slot seems
267 * to work here, but in pure perl i'm pretty sure it goes to the EGV
268 * slot, which seems more correct to me. just disable it for now
269 * i guess */
270 15 ▶ return SVT_SCALAR(sv_type) && sv_type != SVt_PVGV;
Branch: 2 branches
BranchTrueFalseTotal
return SVT_SCALAR(sv_type) && sv_type != SVt_PVGV; 13 2 15
return SVT_SCALAR(sv_type) && sv_type != SVt_PVGV; 12 1 13
271 4 case VAR_ARRAY:
272 4 return SVT_ARRAY(sv_type);
273 6 case VAR_HASH:
274 6 return SVT_HASH(sv_type);
275 212 case VAR_CODE:
276 212 return SVT_CODE(sv_type);
277 2 case VAR_IO:
278 2 return SVT_IO(sv_type);
279 0 default:
280 0 return 0;
281 }
282 }
283  
284 936 static HV *_get_namespace(SV *self)
285 {
286 936 dSP;
287 SV *ret;
288  
289 936 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 936 936
290 936 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 936 936
291 936 PUTBACK;
292  
293 936 call_method("namespace", G_SCALAR);
294  
295 936 SPAGAIN;
296 936 ret = POPs;
297 936 PUTBACK;
298  
299 936 return (HV*)SvRV(ret);
300 }
301  
302 411 static SV *_get_name(SV *self)
303 {
304 411 dSP;
305 SV *ret;
306  
307 411 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 411 411
308 411 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 411 411
309 411 PUTBACK;
310  
311 411 call_method("name", G_SCALAR);
312  
313 411 SPAGAIN;
314 411 ret = POPs;
315 411 PUTBACK;
316  
317 411 return ret;
318 }
319  
320 238 static void _real_gv_init(GV *gv, HV *stash, SV *name)
321 {
322 char *name_pv;
323 STRLEN name_len;
324  
325 238 name_pv = SvPV(name, name_len);
326 238 ▶ if (!HvENAME(stash)) {
Branch: 7 branches
BranchTrueFalseTotal
if (!HvENAME(stash)) { 237 1 238
if (!HvENAME(stash)) { 237 0 237
if (!HvENAME(stash)) { 237 0 237
if (!HvENAME(stash)) { 0 237 237
if (!HvENAME(stash)) { 0 237 237
if (!HvENAME(stash)) { 237 0 237
if (!HvENAME(stash)) { 0 237 237
327 1 hv_name_set(stash, "__ANON__", 8, 0);
328 }
329 238 gv_init(gv, stash, name_pv, name_len, 1);
330  
331 /* XXX: copied and pasted from gv_fetchpvn_flags and such */
332 /* ignoring the stuff for CORE:: and main:: for now, and also
333 * ignoring the GvMULTI_on bits, since we pass 1 to gv_init above */
334 238 switch (name_pv[0]) {
335 3 case 'I':
336 3 ▶ if (strEQ(&name_pv[1], "SA")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(&name_pv[1], "SA")) { 3 0 3
337 AV *av;
338  
339 3 ▶ av = GvAVn(gv);
Branch: 1 branch
BranchTrueFalseTotal
av = GvAVn(gv); 0 3 3
340 3 sv_magic(MUTABLE_SV(av), MUTABLE_SV(gv), PERL_MAGIC_isa,
341 NULL, 0);
342 }
343 3 break;
344 0 case 'O':
345 0 ▶ if (strEQ(&name_pv[1], "VERLOAD")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(&name_pv[1], "VERLOAD")) { 0 0 0
346 HV *hv;
347  
348 0 ▶ hv = GvHVn(gv);
Branch: 1 branch
BranchTrueFalseTotal
hv = GvHVn(gv); 0 0 0
349 0 hv_magic(hv, NULL, PERL_MAGIC_overload);
350 }
351 0 break;
352 235 default:
353 235 break;
354 }
355 238 }
356  
357 10 static void _expand_glob(SV *self, SV *varname, HE *entry, HV *namespace,
358 int lval)
359 {
360 GV *glob;
361  
362 10 ▶ if (entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry) { 10 0 10
363 10 glob = (GV*)HeVAL(entry);
364 10 ▶ if (isGV(glob)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isGV(glob)) { 0 10 10
365 0 croak("_expand_glob called on stash slot with expanded glob: %"SVf,
366 varname);
367 }
368 else {
369 10 SvREFCNT_inc(glob);
370 10 _real_gv_init(glob, namespace, varname);
371 10 ▶ if (HeVAL(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeVAL(entry)) { 10 0 10
372 10 SvREFCNT_dec(HeVAL(entry));
373 }
374 10 HeVAL(entry) = (SV*)glob;
375 }
376 }
377 else {
378 0 croak("_expand_glob called on nonexistent stash slot");
379 }
380 10 }
381  
382 5 static SV *_undef_for_type(vartype_t type)
383 {
384 5 switch (type) {
385 2 case VAR_SCALAR:
386 2 return newSV(0);
387 break;
388 1 case VAR_ARRAY:
389 1 return (SV*)newAV();
390 break;
391 1 case VAR_HASH:
392 1 return (SV*)newHV();
393 break;
394 0 case VAR_CODE:
395 0 croak("Don't know how to vivify CODE variables");
396 1 case VAR_IO:
397 1 return (SV*)newIO();
398 break;
399 0 default:
400 0 croak("Unknown type in vivification");
401 }
402 }
403  
404 239 static void _add_symbol_entry(SV *self, varspec_t variable, SV *initial,
405 HE *entry, HV *namespace)
406 {
407 GV *glob;
408 SV *val;
409  
410 239 ▶ if (entry && isGV(HeVAL(entry))) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry && isGV(HeVAL(entry))) { 239 0 239
if (entry && isGV(HeVAL(entry))) { 11 228 239
411 11 glob = (GV*)HeVAL(entry);
412 }
413 228 ▶ else if (entry) {
Branch: 1 branch
BranchTrueFalseTotal
else if (entry) { 228 0 228
414 228 glob = (GV*)newSV(0);
415 228 _real_gv_init(glob, namespace, variable.name);
416 228 ▶ if (HeVAL(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeVAL(entry)) { 228 0 228
417 228 SvREFCNT_dec(HeVAL(entry));
418 }
419 228 HeVAL(entry) = (SV*)glob;
420 }
421 else {
422 0 croak("invalid entry passed to _add_symbol_entry");
423 }
424  
425 239 ▶ if (!initial) {
Branch: 1 branch
BranchTrueFalseTotal
if (!initial) { 5 234 239
426 5 val = _undef_for_type(variable.type);
427 }
428 234 ▶ else if (SvROK(initial)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(initial)) { 230 4 234
429 230 val = SvRV(initial);
430 230 SvREFCNT_inc_simple_void_NN(val);
431 }
432 else {
433 4 val = newSVsv(initial);
434 }
435  
436 239 switch (variable.type) {
437 14 case VAR_SCALAR:
438 14 ▶ GvSetSV(glob, val);
Branch: 1 branch
BranchTrueFalseTotal
GvSetSV(glob, val); 14 0 14
439 14 break;
440 4 case VAR_ARRAY:
441 4 ▶ GvSetAV(glob, val);
Branch: 1 branch
BranchTrueFalseTotal
GvSetAV(glob, val); 4 0 4
442 4 break;
443 7 case VAR_HASH:
444 7 ▶ GvSetHV(glob, val);
Branch: 1 branch
BranchTrueFalseTotal
GvSetHV(glob, val); 7 0 7
445 7 break;
446 212 case VAR_CODE:
447 212 ▶ GvSetCV(glob, val);
Branch: 1 branch
BranchTrueFalseTotal
GvSetCV(glob, val); 212 0 212
448 212 break;
449 2 case VAR_IO:
450 2 ▶ GvSetIO(glob, val);
Branch: 4 branches
BranchTrueFalseTotal
GvSetIO(glob, val); 2 0 2
GvSetIO(glob, val); 0 2 2
GvSetIO(glob, val); 0 0 0
GvSetIO(glob, val); 2 0 2
451 2 break;
452 0 default:
453 0 croak("Unknown variable type in add_symbol");
454 break;
455 }
456 239 }
457  
458 235 static void _add_symbol(SV *self, varspec_t variable, SV *initial)
459 {
460 HV *namespace;
461 HE *entry;
462  
463 235 namespace = _get_namespace(self);
464 235 entry = hv_fetch_ent(namespace, variable.name, 1, 0);
465  
466 235 _add_symbol_entry(self, variable, initial, entry, namespace);
467 235 }
468  
469 16 static int _slot_exists(GV *glob, vartype_t type)
470 {
471 16 switch (type) {
472 4 case VAR_SCALAR:
473 4 return GvSVOK(glob) ? 1 : 0;
474 break;
475 9 case VAR_ARRAY:
476 9 return GvAVOK(glob) ? 1 : 0;
477 break;
478 2 case VAR_HASH:
479 2 return GvHVOK(glob) ? 1 : 0;
480 break;
481 0 case VAR_CODE:
482 0 croak("Don't know how to vivify CODE variables");
483 1 case VAR_IO:
484 1 ▶ return GvIOOK(glob) ? 1 : 0;
Branch: 5 branches
BranchTrueFalseTotal
return GvIOOK(glob) ? 1 : 0; 1 0 1
return GvIOOK(glob) ? 1 : 0; 0 1 1
return GvIOOK(glob) ? 1 : 0; 0 0 0
return GvIOOK(glob) ? 1 : 0; 1 0 1
return GvIOOK(glob) ? 1 : 0; 0 1 1
485 break;
486 0 default:
487 0 croak("Unknown type in vivification");
488 }
489  
490 return 0;
491 }
492  
493 288 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
494 {
495 HV *namespace;
496 HE *entry;
497 GV *glob;
498  
499 288 namespace = _get_namespace(self);
500 288 ▶ entry = hv_fetch_ent(namespace, variable->name,
Branch: 2 branches
BranchTrueFalseTotal
entry = hv_fetch_ent(namespace, variable->name, 16 272 288
entry = hv_fetch_ent(namespace, variable->name, 7 9 16
501 vivify && !hv_exists_ent(namespace, variable->name, 0),
502 0);
503 288 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 7 281 288
504 7 return NULL;
505  
506 281 glob = (GV*)(HeVAL(entry));
507 281 ▶ if (!isGV(glob))
Branch: 1 branch
BranchTrueFalseTotal
if (!isGV(glob)) 10 271 281
508 10 _expand_glob(self, variable->name, entry, namespace, vivify);
509  
510 281 ▶ if (vivify && !_slot_exists(glob, variable->type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (vivify && !_slot_exists(glob, variable->type)) { 16 265 281
if (vivify && !_slot_exists(glob, variable->type)) { 4 12 16
511 4 _add_symbol_entry(self, *variable, NULL, entry, namespace);
512 }
513  
514 281 switch (variable->type) {
515 16 case VAR_SCALAR:
516 16 return GvSV(glob);
517 20 case VAR_ARRAY:
518 20 return (SV*)GvAV(glob);
519 16 case VAR_HASH:
520 16 return (SV*)GvHV(glob);
521 218 case VAR_CODE:
522 218 return (SV*)GvCV(glob);
523 11 case VAR_IO:
524 11 ▶ return (SV*)GvIO(glob);
Branch: 4 branches
BranchTrueFalseTotal
return (SV*)GvIO(glob); 11 0 11
return (SV*)GvIO(glob); 0 11 11
return (SV*)GvIO(glob); 0 0 0
return (SV*)GvIO(glob); 11 0 11
525 0 default:
526 0 return NULL;
527 }
528 }
529  
530 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
531  
532 PROTOTYPES: DISABLE
533  
534 SV*
535 new(class, package)
536 SV *class
537 SV *package
538 PREINIT:
539 HV *instance;
540 CODE:
541 407 ▶ if (SvPOK(package)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(package)) { 404 3 407
542 404 ▶ if (!_is_valid_module_name(package))
Branch: 1 branch
BranchTrueFalseTotal
if (!_is_valid_module_name(package)) 5 399 404
543 5 croak("%s is not a module name", SvPV_nolen(package));
544  
545 399 instance = newHV();
546  
547 399 ▶ if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) { 0 399 399
548 0 SvREFCNT_dec(package);
549 0 SvREFCNT_dec(instance);
550 0 croak("Couldn't initialize the 'name' key, hv_store failed");
551 }
552 }
553 3 ▶ else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) { 2 1 3
else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) { 1 1 2
554 #if PERL_VERSION < 10
555 croak("The XS implementation of Package::Stash does not support "
556 "anonymous stashes before perl 5.10");
557 #else
558 1 instance = newHV();
559  
560 1 ▶ if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) { 0 1 1
561 0 SvREFCNT_dec(package);
562 0 SvREFCNT_dec(instance);
563 0 croak("Couldn't initialize the 'namespace' key, hv_store failed");
564 }
565 #endif
566 }
567 else {
568 2 croak("Package::Stash->new must be passed the name of the package to access");
569 }
570  
571 400 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
572 OUTPUT:
573 RETVAL
574  
575 SV*
576 name(self)
577 SV *self
578 PREINIT:
579 HE *slot;
580 CODE:
581 412 ▶ if (!sv_isobject(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) 1 411 412
582 1 croak("Can't call name as a class method");
583 411 ▶ if ((slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash))) { 411 0 411
584 411 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
585 }
586 else {
587 0 croak("Can't get the name of an anonymous package");
588 }
589 OUTPUT:
590 RETVAL
591  
592 SV*
593 namespace(self)
594 SV *self
595 PREINIT:
596 HE *slot;
597 SV *package_name;
598 CODE:
599 933 ▶ if (!sv_isobject(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(self)) 0 933 933
600 0 croak("Can't call namespace as a class method");
601 #if PERL_VERSION < 10
602 package_name = _get_name(self);
603 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
604 #else
605 933 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
606 933 ▶ if (slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot) { 536 397 933
607 536 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
608 }
609 else {
610 HV *namespace;
611 SV *nsref;
612  
613 397 package_name = _get_name(self);
614 397 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
615 397 nsref = newRV_inc((SV*)namespace);
616 397 sv_rvweaken(nsref);
617 397 ▶ if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) { 0 397 397
618 0 SvREFCNT_dec(nsref);
619 0 SvREFCNT_dec(self);
620 0 croak("Couldn't initialize the 'namespace' key, hv_store failed");
621 }
622 397 RETVAL = SvREFCNT_inc_simple_NN(nsref);
623 }
624 #endif
625 OUTPUT:
626 RETVAL
627  
628 void
629 add_symbol(self, variable, initial=NULL, ...)
630 SV *self
631 varspec_t variable
632 SV *initial
633 CODE:
634 240 ▶ if (initial && !_valid_for_type(initial, variable.type))
Branch: 2 branches
BranchTrueFalseTotal
if (initial && !_valid_for_type(initial, variable.type)) 239 1 240
if (initial && !_valid_for_type(initial, variable.type)) 5 234 239
635 10 croak("%s is not of type %s",
636 SvPV_nolen(initial), vartype_to_string(variable.type));
637  
638 235 ▶ if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
Branch: 3 branches
BranchTrueFalseTotal
if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) { 234 1 235
if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) { 14 220 234
if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) { 14 0 14
639 int i;
640 14 char *filename = NULL;
641 14 I32 first_line_num = -1, last_line_num = -1;
642 SV *dbval, *name;
643 HV *dbsub;
644  
645 14 ▶ if ((items - 3) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2) 0 14 14
646 0 croak("add_symbol: Odd number of elements in %%opts");
647  
648 17 ▶ for (i = 3; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i < items; i += 2) { 3 14 17
649 char *key;
650 3 key = SvPV_nolen(ST(i));
651 3 ▶ if (strEQ(key, "filename")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "filename")) { 1 2 3
652 1 ▶ if (!SvPOK(ST(i + 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(ST(i + 1))) 0 1 1
653 0 croak("add_symbol: filename must be a string");
654 2 filename = SvPV_nolen(ST(i + 1));
655 }
656 2 ▶ else if (strEQ(key, "first_line_num")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "first_line_num")) { 1 1 2
657 1 ▶ if (!SvIOK(ST(i + 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(ST(i + 1))) 0 1 1
658 0 croak("add_symbol: first_line_num must be an integer");
659 1 first_line_num = SvIV(ST(i + 1));
660 }
661 1 ▶ else if (strEQ(key, "last_line_num")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "last_line_num")) { 1 0 1
662 1 ▶ if (!SvIOK(ST(i + 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(ST(i + 1))) 0 1 1
663 0 croak("add_symbol: last_line_num must be an integer");
664 1 last_line_num = SvIV(ST(i + 1));
665 }
666 }
667  
668 14 ▶ if (!filename || first_line_num == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (!filename || first_line_num == -1) { 1 13 14
if (!filename || first_line_num == -1) { 0 1 1
669 13 ▶ if (!filename)
Branch: 1 branch
BranchTrueFalseTotal
if (!filename) 13 0 13
670 13 ▶ filename = CopFILE(PL_curcop);
Branch: 1 branch
BranchTrueFalseTotal
filename = CopFILE(PL_curcop); 13 0 13
671 13 ▶ if (first_line_num == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (first_line_num == -1) 13 0 13
672 13 first_line_num = PL_curcop->cop_line;
673 }
674  
675 14 ▶ if (last_line_num == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (last_line_num == -1) 13 1 14
676 13 last_line_num = first_line_num;
677  
678 14 name = newSVsv(_get_name(self));
679 14 sv_catpvs(name, "::");
680 14 sv_catsv(name, variable.name);
681  
682 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
683 14 dbsub = get_hv("DB::sub", 1);
684 14 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
685 14 ▶ if (!hv_store_ent(dbsub, name, dbval, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store_ent(dbsub, name, dbval, 0)) { 0 14 14
686 0 warn("Failed to update $DB::sub for subroutine %s",
687 SvPV_nolen(name));
688 0 SvREFCNT_dec(dbval);
689 }
690  
691 14 SvREFCNT_dec(name);
692 }
693  
694 235 _add_symbol(self, variable, initial);
695  
696 void
697 remove_glob(self, name)
698 SV *self
699 SV *name
700 CODE:
701 0 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
702  
703 int
704 has_symbol(self, variable)
705 SV *self
706 varspec_t variable
707 PREINIT:
708 HV *namespace;
709 HE *entry;
710 SV *val;
711 CODE:
712 53 namespace = _get_namespace(self);
713 53 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
714 53 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 3 50 53
715 3 XSRETURN_UNDEF;
716  
717 50 val = HeVAL(entry);
718 50 ▶ if (isGV(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isGV(val)) { 50 0 50
719 50 GV *glob = (GV*)val;
720 50 switch (variable.type) {
721 14 case VAR_SCALAR:
722 14 RETVAL = GvSVOK(glob) ? 1 : 0;
723 14 break;
724 10 case VAR_ARRAY:
725 10 RETVAL = GvAVOK(glob) ? 1 : 0;
726 10 break;
727 8 case VAR_HASH:
728 8 RETVAL = GvHVOK(glob) ? 1 : 0;
729 8 break;
730 13 case VAR_CODE:
731 13 ▶ RETVAL = GvCVOK(glob) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = GvCVOK(glob) ? 1 : 0; 13 0 13
RETVAL = GvCVOK(glob) ? 1 : 0; 7 6 13
732 13 break;
733 5 case VAR_IO:
734 5 ▶ RETVAL = GvIOOK(glob) ? 1 : 0;
Branch: 5 branches
BranchTrueFalseTotal
RETVAL = GvIOOK(glob) ? 1 : 0; 5 0 5
RETVAL = GvIOOK(glob) ? 1 : 0; 0 5 5
RETVAL = GvIOOK(glob) ? 1 : 0; 0 0 0
RETVAL = GvIOOK(glob) ? 1 : 0; 5 0 5
RETVAL = GvIOOK(glob) ? 1 : 0; 4 1 5
735 5 break;
736 0 default:
737 0 croak("Unknown variable type in has_symbol");
738 }
739 }
740 else {
741 0 RETVAL = (variable.type == VAR_CODE);
742 }
743 OUTPUT:
744 RETVAL
745  
746 SV*
747 get_symbol(self, variable)
748 SV *self
749 varspec_t variable
750 PREINIT:
751 SV *val;
752 CODE:
753 272 val = _get_symbol(self, &variable, 0);
754 272 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 7 265 272
755 7 XSRETURN_UNDEF;
756 265 RETVAL = newRV_inc(val);
757 OUTPUT:
758 RETVAL
759  
760 SV*
761 get_or_add_symbol(self, variable)
762 SV *self
763 varspec_t variable
764 PREINIT:
765 SV *val;
766 CODE:
767 16 val = _get_symbol(self, &variable, 1);
768 16 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 16 16
769 0 XSRETURN_UNDEF;
770 16 RETVAL = newRV_inc(val);
771 OUTPUT:
772 RETVAL
773  
774 void
775 remove_symbol(self, variable)
776 SV *self
777 varspec_t variable
778 PREINIT:
779 HV *namespace;
780 HE *entry;
781 SV *val;
782 CODE:
783 6 namespace = _get_namespace(self);
784 6 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
785 6 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 0 6 6
786 0 XSRETURN_EMPTY;
787  
788 6 val = HeVAL(entry);
789 6 ▶ if (isGV(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isGV(val)) { 6 0 6
790 6 GV *glob = (GV*)val;
791 6 switch (variable.type) {
792 1 case VAR_SCALAR:
793 1 ▶ GvSetSV(glob, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GvSetSV(glob, NULL); 0 1 1
794 1 break;
795 0 case VAR_ARRAY:
796 0 ▶ GvSetAV(glob, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GvSetAV(glob, NULL); 0 0 0
797 0 break;
798 1 case VAR_HASH:
799 1 ▶ GvSetHV(glob, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GvSetHV(glob, NULL); 0 1 1
800 1 break;
801 3 case VAR_CODE:
802 3 ▶ GvSetCV(glob, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GvSetCV(glob, NULL); 0 3 3
803 3 break;
804 1 case VAR_IO:
805 1 ▶ GvSetIO(glob, NULL);
Branch: 4 branches
BranchTrueFalseTotal
GvSetIO(glob, NULL); 1 0 1
GvSetIO(glob, NULL); 0 1 1
GvSetIO(glob, NULL); 0 0 0
GvSetIO(glob, NULL); 1 0 1
806 1 break;
807 0 default:
808 0 croak("Unknown variable type in remove_symbol");
809 break;
810 }
811 }
812 else {
813 0 ▶ if (variable.type == VAR_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (variable.type == VAR_CODE) { 0 0 0
814 0 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
815 }
816 }
817  
818 void
819 list_all_symbols(self, vartype=VAR_NONE)
820 SV *self
821 vartype_t vartype
822 PPCODE:
823 351 PUTBACK;
824 351 ▶ if (vartype == VAR_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (vartype == VAR_NONE) { 2 349 351
825 HV *namespace;
826 HE *entry;
827 int keys;
828  
829 2 namespace = _get_namespace(self);
830 2 SPAGAIN;
831 2 keys = hv_iterinit(namespace);
832 2 ▶ EXTEND(SP, keys);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, keys); 2 0 2
EXTEND(SP, keys); 0 2 2
833 15 ▶ while ((entry = hv_iternext(namespace))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(namespace))) { 13 2 15
834 #if PERL_VERSION < 10
835 char *pv;
836 STRLEN len;
837 pv = HePV(entry, len);
838 if (strnEQ(entry, "::ISA::CACHE::", len)) {
839 continue;
840 }
841 #endif
842 13 mPUSHs(newSVhe(entry));
843 }
844 }
845 else {
846 HV *namespace;
847 SV *val;
848 char *key;
849 I32 len;
850  
851 349 namespace = _get_namespace(self);
852 349 SPAGAIN;
853 349 hv_iterinit(namespace);
854 16250 ▶ while ((val = hv_iternextsv(namespace, &key, &len))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((val = hv_iternextsv(namespace, &key, &len))) { 15901 349 16250
855 15901 GV *gv = (GV*)val;
856 #if PERL_VERSION < 10
857 if (vartype == VAR_SCALAR && strnEQ(key, "::ISA::CACHE::", len)) {
858 continue;
859 }
860 #endif
861 15901 ▶ if (isGV(gv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isGV(gv)) { 14929 972 15901
862 14929 switch (vartype) {
863 10 case VAR_SCALAR:
864 10 ▶ if (GvSVOK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (GvSVOK(val)) 1 9 10
865 1 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 0 1 1
866 10 break;
867 7 case VAR_ARRAY:
868 7 ▶ if (GvAVOK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (GvAVOK(val)) 2 5 7
869 2 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 0 2 2
870 7 break;
871 15 case VAR_HASH:
872 15 ▶ if (GvHVOK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (GvHVOK(val)) 3 12 15
873 3 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 0 3 3
874 15 break;
875 14897 case VAR_CODE:
876 14897 ▶ if (GvCVOK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (GvCVOK(val)) 14539 358 14897
if (GvCVOK(val)) 12531 2008 14539
877 12531 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 1 12530 12531
878 14897 break;
879 0 case VAR_IO:
880 0 ▶ if (GvIOOK(val))
Branch: 5 branches
BranchTrueFalseTotal
if (GvIOOK(val)) 0 0 0
if (GvIOOK(val)) 0 0 0
if (GvIOOK(val)) 0 0 0
if (GvIOOK(val)) 0 0 0
if (GvIOOK(val)) 0 0 0
881 0 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 0 0 0
882 0 break;
883 0 default:
884 0 croak("Unknown variable type in list_all_symbols");
885 }
886 }
887 972 ▶ else if (vartype == VAR_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (vartype == VAR_CODE) { 972 0 972
888 972 ▶ mXPUSHp(key, len);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(key, len); 0 972 972
889 }
890 }
891 }
892  
893 void
894 get_all_symbols(self, vartype=VAR_NONE)
895 SV *self
896 vartype_t vartype
897 PREINIT:
898 HV *namespace, *ret;
899 HE *entry;
900 PPCODE:
901 3 PUTBACK;
902 3 namespace = _get_namespace(self);
903 3 SPAGAIN;
904 3 ret = newHV();
905  
906 3 hv_iterinit(namespace);
907 23 ▶ while ((entry = hv_iternext(namespace))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(namespace))) { 20 3 23
908 20 GV *gv = (GV*)hv_iterval(namespace, entry);
909 char *key;
910 I32 len;
911  
912 20 key = hv_iterkey(entry, &len);
913 #if PERL_VERSION < 10
914 if ((vartype == VAR_SCALAR || vartype == VAR_NONE)
915 && strnEQ(key, "::ISA::CACHE::", len)) {
916 continue;
917 }
918 #endif
919  
920 20 ▶ if (!isGV(gv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isGV(gv)) { 0 20 20
921 0 SV *keysv = newSVpvn(key, len);
922 0 _expand_glob(self, keysv, entry, namespace, 0);
923 0 SvREFCNT_dec(keysv);
924 }
925  
926 20 switch (vartype) {
927 0 case VAR_SCALAR:
928 0 ▶ if (GvSVOK(gv))
Branch: 1 branch
BranchTrueFalseTotal
if (GvSVOK(gv)) 0 0 0
929 0 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
930 0 break;
931 0 case VAR_ARRAY:
932 0 ▶ if (GvAVOK(gv))
Branch: 1 branch
BranchTrueFalseTotal
if (GvAVOK(gv)) 0 0 0
933 0 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
934 0 break;
935 8 case VAR_HASH:
936 8 ▶ if (GvHVOK(gv))
Branch: 1 branch
BranchTrueFalseTotal
if (GvHVOK(gv)) 2 6 8
937 2 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
938 8 break;
939 6 case VAR_CODE:
940 6 ▶ if (GvCVOK(gv))
Branch: 2 branches
BranchTrueFalseTotal
if (GvCVOK(gv)) 6 0 6
if (GvCVOK(gv)) 2 4 6
941 2 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
942 6 break;
943 0 case VAR_IO:
944 0 ▶ if (GvIOOK(gv))
Branch: 5 branches
BranchTrueFalseTotal
if (GvIOOK(gv)) 0 0 0
if (GvIOOK(gv)) 0 0 0
if (GvIOOK(gv)) 0 0 0
if (GvIOOK(gv)) 0 0 0
if (GvIOOK(gv)) 0 0 0
945 0 ▶ hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
Branch: 4 branches
BranchTrueFalseTotal
hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0); 0 0 0
hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0); 0 0 0
hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0); 0 0 0
hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0); 0 0 0
946 0 break;
947 6 case VAR_NONE:
948 6 hv_store(ret, key, len, SvREFCNT_inc_simple_NN((SV*)gv), 0);
949 6 break;
950 0 default:
951 0 croak("Unknown variable type in get_all_symbols");
952 }
953 }
954  
955 3 mPUSHs(newRV_noinc((SV*)ret));
956  
957 BOOT:
958 {
959 18 const char *vmre = "\\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\\z";
960 #if (PERL_VERSION < 9) || ((PERL_VERSION == 9) && (PERL_SUBVERSION < 5))
961 PMOP fakepmop;
962  
963 fakepmop.op_pmflags = 0;
964 valid_module_regex = pregcomp(vmre, vmre + strlen(vmre), &fakepmop);
965 #else
966 SV *re;
967  
968 18 re = newSVpv(vmre, 0);
969 18 valid_module_regex = pregcomp(re, 0);
970 #endif
971  
972 18 name_key = newSVpvs("name");
973 18 PERL_HASH(name_hash, "name", 4);
974  
975 18 namespace_key = newSVpvs("namespace");
976 18 PERL_HASH(namespace_hash, "namespace", 9);
977  
978 18 type_key = newSVpvs("type");
979 18 PERL_HASH(type_hash, "type", 4);
980 }
↑ Summary blib/lib/Package/Stash/XS.pm »