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.6 by root, Sat Dec 29 21:04:46 2007 UTC vs.
Revision 1.13 by root, Sat Jul 19 01:38:57 2008 UTC

10 10
11#if !PERL_VERSION_ATLEAST (5,8,9) 11#if !PERL_VERSION_ATLEAST (5,8,9)
12# define SVt_LAST 16 12# define SVt_LAST 16
13#endif 13#endif
14 14
15#if !PERL_VERSION_ATLEAST (5,10,0)
16# define SvPAD_OUR(dummy) 0
17#endif
18
15#define res_pair(text) \ 19#define res_pair(text) \
16 { \ 20 do { \
17 AV *av = newAV (); \ 21 AV *av = newAV (); \
18 av_push (av, newSVpv (text, 0)); \ 22 av_push (av, newSVpv (text, 0)); \
23 if (rmagical) SvRMAGICAL_on (sv); \
19 av_push (av, sv_rvweaken (newRV_inc (sv))); \ 24 av_push (av, sv_rvweaken (newRV_inc (sv))); \
25 if (rmagical) SvRMAGICAL_off (sv); \
20 av_push (about, newRV_noinc ((SV *)av)); \ 26 av_push (about, newRV_noinc ((SV *)av)); \
21 } 27 } while (0)
28
29#define res_text(text) \
30 do { \
31 AV *av = newAV (); \
32 av_push (av, newSVpv (text, 0)); \
33 av_push (about, newRV_noinc ((SV *)av)); \
34 } while (0)
22 35
23#define res_gv(sigil) \ 36#define res_gv(sigil) \
24 { \
25 AV *av = newAV (); \
26 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ 37 res_text (form ("in the global %c%s::%.*s", sigil, \
27 HvNAME (GvSTASH (sv)), \ 38 HvNAME (GvSTASH (sv)), \
39 GvNAMELEN (sv), \
28 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \ 40 GvNAME (sv) ? GvNAME (sv) : "<anonymous>"))
29 0)); \
30 av_push (about, newRV_noinc ((SV *)av)); \
31 }
32 41
33MODULE = Devel::FindRef PACKAGE = Devel::FindRef 42MODULE = Devel::FindRef PACKAGE = Devel::FindRef
34 43
35PROTOTYPES: ENABLE 44PROTOTYPES: ENABLE
36 45
37SV * 46SV *
38ptr2ref (IV ptr) 47ptr2ref (UV ptr)
39 CODE: 48 CODE:
40 RETVAL = newRV_inc (INT2PTR (SV *, ptr)); 49 RETVAL = newRV_inc (INT2PTR (SV *, ptr));
50 OUTPUT:
51 RETVAL
52
53UV
54ref2ptr (SV *rv)
55 CODE:
56 RETVAL = PTR2UV (SvRV (rv));
41 OUTPUT: 57 OUTPUT:
42 RETVAL 58 RETVAL
43 59
44void 60void
45find_ (SV *target) 61find_ (SV *target)
46 PPCODE: 62 PPCODE:
47{ 63{
48 SV *arena, *targ; 64 SV *arena, *targ;
49 int rmagical, i; 65 U32 rmagical;
66 int i;
50 AV *about = newAV (); 67 AV *about = newAV ();
51 AV *excl = newAV (); 68 AV *excl = newAV ();
52 69
53 if (!SvROK (target)) 70 if (!SvROK (target))
54 croak ("find expects a reference to a perl value"); 71 croak ("find expects a reference to a perl value");
72 if ((rmagical = SvRMAGICAL (sv))) 89 if ((rmagical = SvRMAGICAL (sv)))
73 SvRMAGICAL_off (sv); 90 SvRMAGICAL_off (sv);
74 91
75 if (SvTYPE (sv) >= SVt_PVMG) 92 if (SvTYPE (sv) >= SVt_PVMG)
76 { 93 {
77 MAGIC *mg = SvMAGIC (sv); 94 if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv))
78 while (mg)
79 { 95 {
96 /* I have no clue what this is */
97 /* maybe some placeholder for our variables for eval? */
98 /* it doesn't seem to reference anything, so we should be able to ignore it */
99 }
100 else
101 {
102 MAGIC *mg = SvMAGIC (sv);
103
104 while (mg)
105 {
80 if (mg->mg_obj == targ) 106 if (mg->mg_obj == targ)
81 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); 107 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
82 108
83 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) 109 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
84 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); 110 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
85 111
86 mg = mg->mg_moremagic; 112 mg = mg->mg_moremagic;
113 }
87 } 114 }
88 } 115 }
89 116
90 if (SvROK (sv)) 117 if (SvROK (sv))
91 { 118 {
117 144
118 case SVt_PVCV: 145 case SVt_PVCV:
119 { 146 {
120 int depth = CvDEPTH (sv); 147 int depth = CvDEPTH (sv);
121 148
149 /* Anonymous subs have a padlist but zero depth */
150 if (CvANON (sv) && !depth && CvPADLIST (sv))
151 depth = 1;
152
122 if (depth) 153 if (depth)
123 { 154 {
124 AV *padlist = CvPADLIST (sv); 155 AV *padlist = CvPADLIST (sv);
125 156
126 while (depth) 157 while (depth)
127 { 158 {
128 AV *pad = (AV *)AvARRAY (padlist)[depth]; 159 AV *pad = (AV *)AvARRAY (padlist)[depth];
129 160
130 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ 161 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */
131 162
163 /* The 0th pad slot is @_ */
164 if (AvARRAY (pad)[0] == targ)
165 res_pair ("the argument array for");
166
132 for (i = AvFILLp (pad); i--; ) 167 for (i = AvFILLp (pad) + 1; --i; )
133 if (AvARRAY (pad)[i] == targ) 168 if (AvARRAY (pad)[i] == targ)
169 {
170 /* Values from constant functions are stored in the pad without any name */
171 SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i];
172
173 if (name_sv && SvPOK (name_sv))
134 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i]))); 174 res_pair (form ("in the lexical '%s' in", SvPVX (name_sv)));
175 else
176 res_pair ("in an unnamed lexical in");
177 }
135 178
136 --depth; 179 --depth;
137 } 180 }
138 } 181 }
182
183 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ)
184 res_pair ("the constant value of");
185
186 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ)
187 res_pair ("the containing scope for");
188
189 if (sv == targ && CvANON (sv))
190 if (CvSTART (sv)
191 && CvSTART (sv)->op_type == OP_NEXTSTATE
192 && CopLINE ((COP *)CvSTART (sv)))
193 res_text (form ("the closure created at %s:%d",
194 CopFILE ((COP *)CvSTART (sv)) ? CopFILE ((COP *)CvSTART (sv)) : "<unknown>",
195 CopLINE ((COP *)CvSTART (sv))));
196 else
197 res_text (form ("the closure created somewhere in file %s (PLEASE REPORT!)",
198 CvFILE (sv) ? CvFILE (sv) : "<unknown>"));
139 } 199 }
140 200
141 break; 201 break;
142 202
143 case SVt_PVGV: 203 case SVt_PVGV:
155 if (rmagical) 215 if (rmagical)
156 SvRMAGICAL_on (sv); 216 SvRMAGICAL_on (sv);
157 } 217 }
158 } 218 }
159 219
220 if (targ == (SV*)PL_main_cv)
221 res_text ("the main body of the program");
222
160 EXTEND (SP, 2); 223 EXTEND (SP, 2);
161 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 224 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
162 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); 225 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
163} 226}
164 227

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines