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.8 by root, Sun Apr 27 05:59:06 2008 UTC vs.
Revision 1.13 by root, Sat Jul 19 01:38:57 2008 UTC

8 && (PERL_VERSION > (b) \ 8 && (PERL_VERSION > (b) \
9 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 9 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
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
14
15#if !PERL_VERSION_ATLEAST (5,10,0)
16# define SvPAD_OUR(dummy) 0
13#endif 17#endif
14 18
15#define res_pair(text) \ 19#define res_pair(text) \
16 do { \ 20 do { \
17 AV *av = newAV (); \ 21 AV *av = newAV (); \
20 av_push (av, sv_rvweaken (newRV_inc (sv))); \ 24 av_push (av, sv_rvweaken (newRV_inc (sv))); \
21 if (rmagical) SvRMAGICAL_off (sv); \ 25 if (rmagical) SvRMAGICAL_off (sv); \
22 av_push (about, newRV_noinc ((SV *)av)); \ 26 av_push (about, newRV_noinc ((SV *)av)); \
23 } while (0) 27 } while (0)
24 28
25#define res_gv(sigil) \ 29#define res_text(text) \
26 do { \ 30 do { \
27 AV *av = newAV (); \ 31 AV *av = newAV (); \
28 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ 32 av_push (av, newSVpv (text, 0)); \
29 HvNAME (GvSTASH (sv)), \
30 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
31 0)); \
32 av_push (about, newRV_noinc ((SV *)av)); \ 33 av_push (about, newRV_noinc ((SV *)av)); \
33 } while (0) 34 } while (0)
34 35
36#define res_gv(sigil) \
37 res_text (form ("in the global %c%s::%.*s", sigil, \
38 HvNAME (GvSTASH (sv)), \
39 GvNAMELEN (sv), \
40 GvNAME (sv) ? GvNAME (sv) : "<anonymous>"))
41
35MODULE = Devel::FindRef PACKAGE = Devel::FindRef 42MODULE = Devel::FindRef PACKAGE = Devel::FindRef
36 43
37PROTOTYPES: ENABLE 44PROTOTYPES: ENABLE
38 45
39SV * 46SV *
40ptr2ref (IV ptr) 47ptr2ref (UV ptr)
41 CODE: 48 CODE:
42 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));
43 OUTPUT: 57 OUTPUT:
44 RETVAL 58 RETVAL
45 59
46void 60void
47find_ (SV *target) 61find_ (SV *target)
48 PPCODE: 62 PPCODE:
49{ 63{
50 SV *arena, *targ; 64 SV *arena, *targ;
51 int rmagical, i; 65 U32 rmagical;
66 int i;
52 AV *about = newAV (); 67 AV *about = newAV ();
53 AV *excl = newAV (); 68 AV *excl = newAV ();
54 69
55 if (!SvROK (target)) 70 if (!SvROK (target))
56 croak ("find expects a reference to a perl value"); 71 croak ("find expects a reference to a perl value");
74 if ((rmagical = SvRMAGICAL (sv))) 89 if ((rmagical = SvRMAGICAL (sv)))
75 SvRMAGICAL_off (sv); 90 SvRMAGICAL_off (sv);
76 91
77 if (SvTYPE (sv) >= SVt_PVMG) 92 if (SvTYPE (sv) >= SVt_PVMG)
78 { 93 {
79 MAGIC *mg = SvMAGIC (sv); 94 if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv))
80 while (mg)
81 { 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 {
82 if (mg->mg_obj == targ) 106 if (mg->mg_obj == targ)
83 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));
84 108
85 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) 109 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
86 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));
87 111
88 mg = mg->mg_moremagic; 112 mg = mg->mg_moremagic;
113 }
89 } 114 }
90 } 115 }
91 116
92 if (SvROK (sv)) 117 if (SvROK (sv))
93 { 118 {
120 case SVt_PVCV: 145 case SVt_PVCV:
121 { 146 {
122 int depth = CvDEPTH (sv); 147 int depth = CvDEPTH (sv);
123 148
124 /* Anonymous subs have a padlist but zero depth */ 149 /* Anonymous subs have a padlist but zero depth */
125 if (!depth && CvPADLIST (sv)) 150 if (CvANON (sv) && !depth && CvPADLIST (sv))
126 depth = 1; 151 depth = 1;
127 152
128 if (depth) 153 if (depth)
129 { 154 {
130 AV *padlist = CvPADLIST (sv); 155 AV *padlist = CvPADLIST (sv);
133 { 158 {
134 AV *pad = (AV *)AvARRAY (padlist)[depth]; 159 AV *pad = (AV *)AvARRAY (padlist)[depth];
135 160
136 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 */
137 162
163 /* The 0th pad slot is @_ */
164 if (AvARRAY (pad)[0] == targ)
165 res_pair ("the argument array for");
166
138 for (i = AvFILLp (pad) + 1; i--; ) 167 for (i = AvFILLp (pad) + 1; --i; )
139 if (AvARRAY (pad)[i] == targ) 168 if (AvARRAY (pad)[i] == targ)
140 { 169 {
141 /* Values from constant functions are stored in the pad without any name */ 170 /* Values from constant functions are stored in the pad without any name */
142 SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i]; 171 SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i];
143 172
154 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ) 183 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ)
155 res_pair ("the constant value of"); 184 res_pair ("the constant value of");
156 185
157 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ) 186 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ)
158 res_pair ("the containing scope for"); 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>"));
159 } 199 }
160 200
161 break; 201 break;
162 202
163 case SVt_PVGV: 203 case SVt_PVGV:
175 if (rmagical) 215 if (rmagical)
176 SvRMAGICAL_on (sv); 216 SvRMAGICAL_on (sv);
177 } 217 }
178 } 218 }
179 219
220 if (targ == (SV*)PL_main_cv)
221 res_text ("the main body of the program");
222
180 EXTEND (SP, 2); 223 EXTEND (SP, 2);
181 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 224 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
182 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); 225 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
183} 226}
184 227

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines