B::Hooks::OP::Check::Install::Files is not indexed
[p5sagit/Devel-Declare.git] / Declare.xs
index 5869056..9b84045 100644 (file)
@@ -1,3 +1,4 @@
+#define PERL_NO_GET_CONTEXT 1
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
@@ -63,6 +64,7 @@ int dd_is_declarator(pTHX_ char* name) {
   HV* is_declarator_pack_hash;
   SV** is_declarator_flag_ref;
   int dd_flags;
+  char* curstash_name;
 
   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
 
@@ -71,11 +73,12 @@ int dd_is_declarator(pTHX_ char* name) {
 
   /* $declarators{$current_package_name} */
 
-  if (!HvNAME(PL_curstash))
+  curstash_name = HvNAME(PL_curstash);
+  if (!curstash_name)
     return -1;
 
-  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
-                             strlen(HvNAME(PL_curstash)), FALSE);
+  is_declarator_pack_ref = hv_fetch(is_declarator, curstash_name,
+                             strlen(curstash_name), FALSE);
 
   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
     return -1; /* not a hashref */
@@ -149,14 +152,17 @@ void dd_set_linestr(pTHX_ char* new_value) {
   PL_bufend = SvPVX(PL_linestr) + new_len;
 
   if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
-    // Cribbed from toke.c
-    SV * const sv = NEWSV(85,0);
-
-    sv_upgrade(sv, SVt_PVMG);
-    sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
-    (void)SvIOK_on(sv);
-    SvIV_set(sv, 0);
-    av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
+    /* Cribbed from toke.c */
+    AV *fileav = CopFILEAV(&PL_compiling);
+    if (fileav) {
+      SV * const sv = NEWSV(85,0);
+
+      sv_upgrade(sv, SVt_PVMG);
+      sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
+      (void)SvIOK_on(sv);
+      SvIV_set(sv, 0);
+      av_store(fileav,(I32)CopLINE(&PL_compiling),sv);
+    }
   }
 }
 
@@ -251,9 +257,9 @@ static void call_done_declare(pTHX) {
   if (DD_DEBUG_TRACE) {
     printf("Deconstructing declare\n");
     printf("PL_bufptr: %s\n", PL_bufptr);
-    printf("bufend at: %i\n", PL_bufend - PL_bufptr);
+    printf("bufend at: %i\n", (int)(PL_bufend - PL_bufptr));
     printf("linestr: %s\n", SvPVX(PL_linestr));
-    printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+    printf("linestr len: %i\n", (int)(PL_bufend - SvPVX(PL_linestr)));
   }
 
   ENTER;
@@ -268,20 +274,35 @@ static void call_done_declare(pTHX) {
 
   if (DD_DEBUG_TRACE) {
     printf("PL_bufptr: %s\n", PL_bufptr);
-    printf("bufend at: %i\n", PL_bufend - PL_bufptr);
+    printf("bufend at: %i\n", (int)(PL_bufend - PL_bufptr));
     printf("linestr: %s\n", SvPVX(PL_linestr));
-    printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
-    printf("actual len: %i\n", strlen(PL_bufptr));
+    printf("linestr len: %i\n", (int)(PL_bufend - SvPVX(PL_linestr)));
+    printf("actual len: %i\n", (int)strlen(PL_bufptr));
   }
 }
 
 static int dd_handle_const(pTHX_ char *name);
 
+#ifdef CV_NAME_NOTQUAL /* 5.21.5 */
+# define Gv_or_CvNAME(g) (isGV(g) \
+               ? GvNAME(g) \
+               : SvPV_nolen(cv_name((CV *)SvRV(g), NULL, CV_NAME_NOTQUAL)))
+#elif defined(CvNAMED) /* 5.21.4 */
+# define Gv_or_CvNAME(g) (isGV(g) \
+                            ? GvNAME(g) \
+                            : CvNAMED(SvRV(g)) \
+                                ? HEK_KEY(CvNAME_HEK((CV *)SvRV(g))) \
+                                : GvNAME(CvGV(SvRV(g))))
+#else
+# define Gv_or_CvNAME(g) GvNAME(g)
+#endif
+
 /* replacement PL_check rv2cv entry */
 
 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
   OP* kid;
   int dd_flags;
+  char *gvname;
 
   PERL_UNUSED_VAR(user_data);
 
@@ -298,11 +319,17 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
   if (kid->op_type != OP_GV) /* not a GV so ignore */
     return o;
 
+  if (!isGV(kGVOP_gv)
+   && (!SvROK(kGVOP_gv) || SvTYPE(SvRV(kGVOP_gv)) != SVt_PVCV))
+    return o;
+
+  gvname = Gv_or_CvNAME(kGVOP_gv);
+
   if (DD_DEBUG_TRACE) {
-    printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
+    printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), gvname);
   }
 
-  dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
+  dd_flags = dd_is_declarator(aTHX_ gvname);
 
   if (dd_flags == -1)
     return o;
@@ -314,23 +341,23 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
 
 #if DD_CONST_VIA_RV2CV
   if (PL_expect != XOPERATOR) {
-    if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
+    if (!dd_handle_const(aTHX_ Gv_or_CvNAME(kGVOP_gv)))
       return o;
     CopLINE(PL_curcop) = PL_copline;
     /* The parser behaviour that we're simulating depends on what comes
        after the declarator. */
-    if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
+    if (*skipspace(PL_bufptr + strlen(gvname)) != '(') {
       if (in_declare) {
         call_done_declare(aTHX);
       } else {
-        dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
+        dd_linestr_callback(aTHX_ "rv2cv", gvname);
       }
     }
     return o;
   }
 #endif /* DD_CONST_VIA_RV2CV */
 
-  dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
+  dd_linestr_callback(aTHX_ "rv2cv", gvname);
 
   return o;
 }
@@ -352,7 +379,7 @@ OP* dd_pp_entereval(pTHX) {
   const char* s;
   SV *sv;
 #ifdef PERL_5_9_PLUS
-  SV *saved_hh;
+  SV *saved_hh = NULL;
   if (PL_op->op_private & OPpEVAL_HAS_HH) {
     saved_hh = POPs;
   }
@@ -394,9 +421,23 @@ STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
 
 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
 {
+  SV *filter_datasv;
   const I32 count = FILTER_READ(idx+1, sv, maxlen);
   SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
-  /* filter_del(dd_filter_realloc); */
+  /* Filters can only be deleted in the correct order (reverse of the
+     order in which they were added).  Insisting on deleting the filter
+     here would break if another filter were added after ours and is
+     still around.  Not deleting the filter at all would break if another
+     filter were added earlier and attempts to delete itself later.
+     We can play nicely to the maximum possible extent by deleting our
+     filter iff it is currently deletable (i.e., it is on the top of
+     the filter stack).  Can still run into trouble in more complex
+     situations, but can't avoid that. */
+  if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0 &&
+      (filter_datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters))) &&
+      IoANY(filter_datasv) == FPTR2DPTR(void *, dd_filter_realloc)) {
+    filter_del(dd_filter_realloc);
+  }
   return count;
 }