ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
Revision: 1.1
Committed: Thu Jan 11 22:30:34 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #define res_pair(text) \
6 { \
7 AV *av = newAV (); \
8 av_push (av, newSVpv (text, 0)); \
9 av_push (av, newRV_inc (sv)); \
10 av_push (about, newRV_noinc ((SV *)av)); \
11 }
12
13 #define res_gv(sigil) \
14 { \
15 AV *av = newAV (); \
16 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \
17 HvNAME (GvSTASH (sv)), \
18 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
19 0)); \
20 av_push (about, newRV_noinc ((SV *)av)); \
21 }
22
23 MODULE = Devel::FindRef PACKAGE = Devel::FindRef
24
25 PROTOTYPES: ENABLE
26
27 SV *
28 ptr2ref (IV ptr)
29 CODE:
30 RETVAL = newRV_inc (INT2PTR (SV *, ptr));
31 OUTPUT:
32 RETVAL
33
34 void
35 find_ (SV *target)
36 PPCODE:
37 {
38 SV *arena, *targ;
39 int rmagical, i;
40 AV *about = newAV ();
41 AV *excl = newAV ();
42
43 if (!SvROK (target))
44 croak ("about expects a reference to a perl value");
45
46 targ = SvRV (target);
47
48 for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena))
49 {
50 UV idx = SvREFCNT (arena);
51
52 /* Remember that the zeroth slot is used as the pointer onwards, so don't
53 include it. */
54 while (--idx > 0)
55 {
56 SV *sv = &arena [idx];
57
58 if ((rmagical = SvRMAGICAL (sv)))
59 SvRMAGICAL_off (sv);
60
61 //TODO: magic
62
63 switch (SvTYPE (sv))
64 {
65 case SVt_RV:
66 if (sv != target && SvRV (sv) == targ)
67 res_pair ("referenced by");
68 break;
69
70 case SVt_PVAV:
71 if (AvREAL (sv))
72 for (i = AvFILLp (sv) + 1; i--; )
73 if (AvARRAY (sv)[i] == targ)
74 res_pair (form ("in array element %d of", i));
75 break;
76
77 case SVt_PVHV:
78 if (hv_iterinit ((HV *)sv))
79 {
80 HE *he;
81 while ((he = hv_iternext ((HV *)sv)))
82 if (HeVAL (he) == targ)
83 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he)));
84 }
85 break;
86
87 case SVt_PVGV:
88 if (GvGP (sv))
89 {
90 if (GvSV (sv) == targ)
91 res_gv ('$');
92 if (GvAV (sv) == (AV *)targ)
93 res_gv ('@');
94 if (GvHV (sv) == (HV *)targ)
95 res_gv ('%');
96 if (GvCV (sv) == (CV *)targ)
97 res_gv ('&');
98 }
99 break;
100
101 case SVt_PVCV:
102 {
103 int depth = CvDEPTH (sv);
104 if (depth)
105 {
106 AV *padlist = CvPADLIST (sv);
107 while (depth)
108 {
109 AV *pad = (AV *)AvARRAY (padlist)[depth];
110 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */
111 for (i = AvFILLp (pad); i--; )
112 if (AvARRAY (pad)[i] == targ)
113 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
114
115 --depth;
116 }
117 }
118 }
119 break;
120 }
121
122 if (rmagical)
123 SvRMAGICAL_off (sv);
124 }
125 }
126
127 EXTEND (SP, 2);
128 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
129 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
130 }
131