ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
(Generate patch)

Comparing Devel-FindRef/FindRef.xs (file contents):
Revision 1.2 by root, Thu Jan 11 23:05:52 2007 UTC vs.
Revision 1.6 by root, Sat Dec 29 21:04:46 2007 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#define PERL_VERSION_ATLEAST(a,b,c) \
6 (PERL_REVISION > (a) \
7 || (PERL_REVISION == (a) \
8 && (PERL_VERSION > (b) \
9 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
10
11#if !PERL_VERSION_ATLEAST (5,8,9)
12# define SVt_LAST 16
13#endif
14
5#define res_pair(text) \ 15#define res_pair(text) \
6 { \ 16 { \
7 AV *av = newAV (); \ 17 AV *av = newAV (); \
8 av_push (av, newSVpv (text, 0)); \ 18 av_push (av, newSVpv (text, 0)); \
9 av_push (av, newRV_inc (sv)); \ 19 av_push (av, sv_rvweaken (newRV_inc (sv))); \
10 av_push (about, newRV_noinc ((SV *)av)); \ 20 av_push (about, newRV_noinc ((SV *)av)); \
11 } 21 }
12 22
13#define res_gv(sigil) \ 23#define res_gv(sigil) \
14 { \ 24 { \
15 AV *av = newAV (); \ 25 AV *av = newAV (); \
16 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ 26 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \
17 HvNAME (GvSTASH (sv)), \ 27 HvNAME (GvSTASH (sv)), \
18 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \ 28 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
19 0)); \ 29 0)); \
20 av_push (about, newRV_noinc ((SV *)av)); \ 30 av_push (about, newRV_noinc ((SV *)av)); \
21 } 31 }
22 32
23MODULE = Devel::FindRef PACKAGE = Devel::FindRef 33MODULE = Devel::FindRef PACKAGE = Devel::FindRef
24 34
25PROTOTYPES: ENABLE 35PROTOTYPES: ENABLE
39 int rmagical, i; 49 int rmagical, i;
40 AV *about = newAV (); 50 AV *about = newAV ();
41 AV *excl = newAV (); 51 AV *excl = newAV ();
42 52
43 if (!SvROK (target)) 53 if (!SvROK (target))
44 croak ("about expects a reference to a perl value"); 54 croak ("find expects a reference to a perl value");
45 55
46 targ = SvRV (target); 56 targ = SvRV (target);
47 57
48 for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) 58 for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena))
49 { 59 {
67 MAGIC *mg = SvMAGIC (sv); 77 MAGIC *mg = SvMAGIC (sv);
68 while (mg) 78 while (mg)
69 { 79 {
70 if (mg->mg_obj == targ) 80 if (mg->mg_obj == targ)
71 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); 81 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
82
72 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) 83 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
73 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); 84 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
74 85
75 mg = mg->mg_moremagic; 86 mg = mg->mg_moremagic;
76 } 87 }
77 } 88 }
78 89
79 switch (SvTYPE (sv)) 90 if (SvROK (sv))
80 { 91 {
81 case SVt_RV:
82 if (sv != target && SvRV (sv) == targ) 92 if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv))
83 res_pair ("referenced by"); 93 res_pair ("referenced by");
94 }
95 else
96 switch (SvTYPE (sv))
84 break; 97 {
98 case SVt_PVAV:
99 if (AvREAL (sv))
100 for (i = AvFILLp (sv) + 1; i--; )
101 if (AvARRAY (sv)[i] == targ)
102 res_pair (form ("in array element %d of", i));
85 103
86 case SVt_PVAV:
87 if (AvREAL (sv))
88 for (i = AvFILLp (sv) + 1; i--; )
89 if (AvARRAY (sv)[i] == targ)
90 res_pair (form ("in array element %d of", i));
91 break; 104 break;
92 105
93 case SVt_PVHV: 106 case SVt_PVHV:
94 if (hv_iterinit ((HV *)sv)) 107 if (hv_iterinit ((HV *)sv))
108 {
109 HE *he;
110
111 while ((he = hv_iternext ((HV *)sv)))
112 if (HeVAL (he) == targ)
113 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he)));
114 }
115
116 break;
117
118 case SVt_PVCV:
95 { 119 {
120 int depth = CvDEPTH (sv);
121
122 if (depth)
96 HE *he; 123 {
97 while ((he = hv_iternext ((HV *)sv))) 124 AV *padlist = CvPADLIST (sv);
125
126 while (depth)
127 {
128 AV *pad = (AV *)AvARRAY (padlist)[depth];
129
130 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */
131
132 for (i = AvFILLp (pad); i--; )
98 if (HeVAL (he) == targ) 133 if (AvARRAY (pad)[i] == targ)
99 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he))); 134 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
135
136 --depth;
137 }
138 }
100 } 139 }
101 break;
102 140
141 break;
142
103 case SVt_PVCV: 143 case SVt_PVGV:
104 {
105 int depth = CvDEPTH (sv);
106 if (depth) 144 if (GvGP (sv))
107 { 145 {
108 AV *padlist = CvPADLIST (sv); 146 if (GvSV (sv) == (SV *)targ) res_gv ('$');
109 while (depth) 147 if (GvAV (sv) == (AV *)targ) res_gv ('@');
148 if (GvHV (sv) == (HV *)targ) res_gv ('%');
149 if (GvCV (sv) == (CV *)targ) res_gv ('&');
110 { 150 }
111 AV *pad = (AV *)AvARRAY (padlist)[depth];
112 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */
113 for (i = AvFILLp (pad); i--; )
114 if (AvARRAY (pad)[i] == targ)
115 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
116 151
117 --depth;
118 }
119 }
120 }
121 break; 152 break;
122
123 case SVt_PVGV:
124 if (GvGP (sv))
125 {
126 if (GvSV (sv) == targ)
127 res_gv ('$');
128 if (GvAV (sv) == (AV *)targ)
129 res_gv ('@');
130 if (GvHV (sv) == (HV *)targ)
131 res_gv ('%');
132 if (GvCV (sv) == (CV *)targ)
133 res_gv ('&');
134 }
135 break;
136 } 153 }
137 154
138 if (rmagical) 155 if (rmagical)
139 SvRMAGICAL_off (sv); 156 SvRMAGICAL_on (sv);
140 } 157 }
141 } 158 }
142 159
143 EXTEND (SP, 2); 160 EXTEND (SP, 2);
144 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 161 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines