--- Devel-FindRef/FindRef.xs 2008/04/26 03:15:28 1.7 +++ Devel-FindRef/FindRef.xs 2008/12/01 13:22:43 1.14 @@ -12,43 +12,44 @@ # define SVt_LAST 16 #endif +#if !PERL_VERSION_ATLEAST (5,10,0) +# define SvPAD_OUR(dummy) 0 +#endif + #define res_pair(text) \ - { \ + do { \ AV *av = newAV (); \ av_push (av, newSVpv (text, 0)); \ if (rmagical) SvRMAGICAL_on (sv); \ av_push (av, sv_rvweaken (newRV_inc (sv))); \ if (rmagical) SvRMAGICAL_off (sv); \ av_push (about, newRV_noinc ((SV *)av)); \ - } + } while (0) -#define res_gv(sigil) \ - { \ +#define res_text(text) \ + do { \ AV *av = newAV (); \ - av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ - HvNAME (GvSTASH (sv)), \ - GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : ""), \ - 0)); \ + av_push (av, newSVpv (text, 0)); \ av_push (about, newRV_noinc ((SV *)av)); \ - } + } while (0) + +#define res_gv(sigil) \ + res_text (form ("in the global %c%s::%.*s", sigil, \ + HvNAME (GvSTASH (sv)), \ + GvNAMELEN (sv), \ + GvNAME (sv) ? GvNAME (sv) : "")) MODULE = Devel::FindRef PACKAGE = Devel::FindRef PROTOTYPES: ENABLE -SV * -ptr2ref (IV ptr) - CODE: - RETVAL = newRV_inc (INT2PTR (SV *, ptr)); - OUTPUT: - RETVAL - void find_ (SV *target) PPCODE: { SV *arena, *targ; - int rmagical, i; + U32 rmagical; + int i; AV *about = newAV (); AV *excl = newAV (); @@ -76,20 +77,30 @@ if (SvTYPE (sv) >= SVt_PVMG) { - MAGIC *mg = SvMAGIC (sv); - while (mg) + if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) + { + /* I have no clue what this is */ + /* maybe some placeholder for our variables for eval? */ + /* it doesn't seem to reference anything, so we should be able to ignore it */ + } + else { - if (mg->mg_obj == targ) - res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); + MAGIC *mg = SvMAGIC (sv); + + while (mg) + { + if (mg->mg_obj == targ) + res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); - if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) - res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); + if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) + res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); - mg = mg->mg_moremagic; + mg = mg->mg_moremagic; + } } } - if (SvROK (sv)) + if (SvROK (sv) && !SvWEAKREF (sv)) { if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv)) res_pair ("referenced by"); @@ -121,7 +132,8 @@ { int depth = CvDEPTH (sv); - if (!depth && CvPADLIST(sv)) + /* Anonymous subs have a padlist but zero depth */ + if (CvANON (sv) && !depth && CvPADLIST (sv)) depth = 1; if (depth) @@ -134,16 +146,42 @@ av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ - for (i = AvFILLp (pad) + 1; i--; ) + /* The 0th pad slot is @_ */ + if (AvARRAY (pad)[0] == targ) + res_pair ("the argument array for"); + + for (i = AvFILLp (pad) + 1; --i; ) if (AvARRAY (pad)[i] == targ) - res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i]))); + { + /* Values from constant functions are stored in the pad without any name */ + SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i]; + + if (name_sv && SvPOK (name_sv)) + res_pair (form ("in the lexical '%s' in", SvPVX (name_sv))); + else + res_pair ("in an unnamed lexical in"); + } --depth; } } - if ((SV*)CvOUTSIDE(sv) == targ) + if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ) + res_pair ("the constant value of"); + + if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ) res_pair ("the containing scope for"); + + if (sv == targ && CvANON (sv)) + if (CvSTART (sv) + && CvSTART (sv)->op_type == OP_NEXTSTATE + && CopLINE ((COP *)CvSTART (sv))) + res_text (form ("the closure created at %s:%d", + CopFILE ((COP *)CvSTART (sv)) ? CopFILE ((COP *)CvSTART (sv)) : "", + CopLINE ((COP *)CvSTART (sv)))); + else + res_text (form ("the closure created somewhere in file %s (PLEASE REPORT!)", + CvFILE (sv) ? CvFILE (sv) : "")); } break; @@ -165,8 +203,31 @@ } } + if (targ == (SV*)PL_main_cv) + res_text ("the main body of the program"); + EXTEND (SP, 2); PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); } +SV * +ptr2ref (UV ptr) + CODE: + RETVAL = newRV_inc (INT2PTR (SV *, ptr)); + OUTPUT: + RETVAL + +UV +ref2ptr (SV *rv) + CODE: + RETVAL = PTR2UV (SvRV (rv)); + OUTPUT: + RETVAL + +U32 +_refcnt (SV *rv) + CODE: + RETVAL = SvREFCNT (SvRV (rv)); + OUTPUT: + RETVAL