missed an aTHX_
[p5sagit/Devel-Declare.git] / Declare.xs
index db6437b..450e81b 100644 (file)
@@ -60,8 +60,22 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
 
   if (in_declare) {
     cb_args[0] = NULL;
+#ifdef DD_DEBUG
+    printf("Deconstructing declare\n");
+    printf("PL_bufptr: %s\n", PL_bufptr);
+    printf("bufend at: %i\n", PL_bufend - PL_bufptr);
+    printf("linestr: %s\n", SvPVX(PL_linestr));
+    printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+#endif
     call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
     in_declare--;
+#ifdef DD_DEBUG
+    printf("PL_bufptr: %s\n", PL_bufptr);
+    printf("bufend at: %i\n", 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));
+#endif
     return o;
   }
 
@@ -227,21 +241,27 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
     retstr = POPpx;
     PUTBACK;
     if (retstr && strlen(retstr)) {
+      const char* old_start = SvPVX(PL_linestr);
+      int start_diff;
       const int old_len = SvCUR(PL_linestr);
 #ifdef DD_DEBUG
       printf("Got string %s\n", retstr);
-      printf("retstr len: %d, old_len %d\n", strlen(retstr), old_len);
 #endif
       SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
+      if (start_diff = SvPVX(PL_linestr) - old_start) {
+        Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
+      }
       memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
       memmove(s, retstr, strlen(retstr));
       SvCUR_set(PL_linestr, old_len + strlen(retstr));
       PL_bufend += strlen(retstr);
 #ifdef DD_DEBUG
   printf("cur buf: %s\n", s);
+  printf("PL_bufptr: %s\n", PL_bufptr);
   printf("bufend at: %i\n", PL_bufend - s);
   printf("linestr: %s\n", SvPVX(PL_linestr));
   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+  printf("tokenbuf now: %s\n", PL_tokenbuf);
 #endif
     }
   } else {
@@ -281,6 +301,14 @@ STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
   return o;
 }
 
+static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
+{
+  const I32 count = FILTER_READ(idx+1, sv, maxlen);
+  SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
+  /* filter_del(dd_filter_realloc); */
+  return count;
+}
+
 static int initialized = 0;
 
 MODULE = Devel::Declare  PACKAGE = Devel::Declare
@@ -293,15 +321,5 @@ setup()
   if (!initialized++) {
     dd_old_ck_rv2cv = PL_check[OP_RV2CV];
     PL_check[OP_RV2CV] = dd_ck_rv2cv;
-    dd_old_ck_lineseq = PL_check[OP_LINESEQ];
-    PL_check[OP_LINESEQ] = dd_ck_lineseq;
-  }
-
-void
-teardown()
-  CODE:
-  /* ensure we only uninit when number of teardown calls matches 
-     number of setup calls */
-  if (initialized && !--initialized) {
-    PL_check[OP_RV2CV] = dd_old_ck_rv2cv;
   }
+  filter_add(dd_filter_realloc, NULL);