toke_scan_str return undef for unterminated string
[p5sagit/Devel-Declare.git] / Declare.xs
index c3a8dee..65902ff 100644 (file)
@@ -7,6 +7,12 @@
 #include <stdio.h>
 #include <string.h>
 
+#define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
+#define PERL_DECIMAL_VERSION \
+  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
+#define PERL_VERSION_GE(r,v,s) \
+  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
+
 #ifndef Newx
 # define Newx(v,n,t) New(0,v,n,t)
 #endif /* !Newx */
 #define DD_DEBUG_TRACE (dd_debug & DD_DEBUGf_TRACE)
 static int dd_debug = 0;
 
+#define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)
+
+#define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)
+
 #define LEX_NORMAL    10
 #define LEX_INTERPNORMAL   9
 
+/* please try not to have a line longer than this :) */
+
+#define DD_PREFERRED_LINESTR_SIZE 16384
+
 /* flag to trigger removal of temporary declaree sub */
 
 static int in_declare = 0;
@@ -134,7 +148,7 @@ 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) {
+  if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
     // Cribbed from toke.c
     SV * const sv = NEWSV(85,0);
 
@@ -204,61 +218,78 @@ int dd_toke_scan_ident(pTHX_ int offset) {
 }
 
 int dd_toke_scan_str(pTHX_ int offset) {
-  STRLEN remaining = sv_len(PL_linestr) - offset;
-  SV* line_copy = newSVsv(PL_linestr);
+  char* old_pvx = SvPVX(PL_linestr);
+  SV* line_copy = sv_2mortal(newSVsv(PL_linestr));
   char* base_s = SvPVX(PL_linestr) + offset;
   char* s = scan_str(base_s, FALSE, FALSE);
-  if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
-    int ret = (s - SvPVX(PL_linestr)) + remaining;
+  if(SvPVX(PL_linestr) != old_pvx)
+    croak("PL_linestr reallocated during scan_str, "
+      "Devel::Declare can't continue");
+  if (!s)
+    return 0;
+  if (s <= base_s) {
+    s += SvCUR(line_copy);
     sv_catsv(line_copy, PL_linestr);
     dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
-    SvREFCNT_dec(line_copy);
-    return ret;
   }
   return s - base_s;
 }
 
 int dd_toke_skipspace(pTHX_ int offset) {
+  char* old_pvx = SvPVX(PL_linestr);
   char* base_s = SvPVX(PL_linestr) + offset;
-  char* s = skipspace(base_s);
+  char* s = skipspace_force(base_s);
+  if(SvPVX(PL_linestr) != old_pvx)
+    croak("PL_linestr reallocated during skipspace, "
+      "Devel::Declare can't continue");
   return s - base_s;
 }
 
+static void call_done_declare(pTHX) {
+  dSP;
+
+  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("linestr: %s\n", SvPVX(PL_linestr));
+    printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+  }
+
+  ENTER;
+  SAVETMPS;
+
+  PUSHMARK(SP);
+
+  call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
+
+  FREETMPS;
+  LEAVE;
+
+  if (DD_DEBUG_TRACE) {
+    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));
+  }
+}
+
+static int dd_handle_const(pTHX_ char *name);
+
 /* replacement PL_check rv2cv entry */
 
 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
-  dSP;
   OP* kid;
   int dd_flags;
 
   PERL_UNUSED_VAR(user_data);
 
-  if (in_declare) {
-    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("linestr: %s\n", SvPVX(PL_linestr));
-      printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
-    }
-
-    ENTER;
-    SAVETMPS;
-
-    PUSHMARK(SP);
-
-    call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
-
-    FREETMPS;
-    LEAVE;
+  if (!DD_AM_LEXING)
+    return o; /* not lexing? */
 
-    if (DD_DEBUG_TRACE) {
-      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));
-    }
+  if (in_declare) {
+    call_done_declare(aTHX);
     return o;
   }
 
@@ -267,9 +298,6 @@ 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 (!DD_AM_LEXING)
-    return o; /* not lexing? */
-
   if (DD_DEBUG_TRACE) {
     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
   }
@@ -284,11 +312,40 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
   }
 
+#if DD_CONST_VIA_RV2CV
+  if (PL_expect != XOPERATOR) {
+    if (!dd_handle_const(aTHX_ GvNAME(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 (in_declare) {
+        call_done_declare(aTHX);
+      } else {
+        dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
+      }
+    }
+    return o;
+  }
+#endif /* DD_CONST_VIA_RV2CV */
+
   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
 
   return o;
 }
 
+#if DD_GROW_VIA_BLOCKHOOK
+
+static void dd_block_start(pTHX_ int full)
+{
+  PERL_UNUSED_VAR(full);
+  if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
+    (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
+}
+
+#else /* !DD_GROW_VIA_BLOCKHOOK */
+
 OP* dd_pp_entereval(pTHX) {
   dSP;
   STRLEN len;
@@ -314,7 +371,7 @@ OP* dd_pp_entereval(pTHX) {
         sv = sv_2mortal(newSVsv(sv));
       sv_catpvn(sv, "\n;", 2);
     }
-    SvGROW(sv, 8192);
+    SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
   }
   PUSHs(sv);
 #ifdef PERL_5_9_PLUS
@@ -333,14 +390,67 @@ STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
   return o;
 }
 
+#endif /* !DD_GROW_VIA_BLOCKHOOK */
+
 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 :) */
+  SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
   /* filter_del(dd_filter_realloc); */
   return count;
 }
 
+static int dd_handle_const(pTHX_ char *name) {
+  switch (PL_lex_inwhat) {
+    case OP_QR:
+    case OP_MATCH:
+    case OP_SUBST:
+    case OP_TRANS:
+    case OP_BACKTICK:
+    case OP_STRINGIFY:
+      return 0;
+      break;
+    default:
+      break;
+  }
+
+  if (strnEQ(PL_bufptr, "->", 2)) {
+    return 0;
+  }
+
+  {
+    char buf[256];
+    STRLEN len;
+    char *s = PL_bufptr;
+    STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
+
+    s = scan_word(s, buf, sizeof buf, FALSE, &len);
+    if (strnEQ(buf, name, len)) {
+      char *d;
+      SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
+      sv_catpvn(inject, buf, len);
+
+      d = peekspace(s);
+      sv_catpvn(inject, s, d - s);
+
+      if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
+        return 0;
+      }
+
+      sv_catpv(inject, d);
+      dd_set_linestr(aTHX_ SvPV_nolen(inject));
+      PL_bufptr = SvPVX(PL_linestr) + old_offset;
+      SvREFCNT_dec (inject);
+    }
+  }
+
+  dd_linestr_callback(aTHX_ "const", name);
+
+  return 1;
+}
+
+#if !DD_CONST_VIA_RV2CV
+
 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
   int dd_flags;
   char* name;
@@ -368,24 +478,13 @@ STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
   if (dd_flags == -1)
     return o;
 
-  switch (PL_lex_inwhat) {
-    case OP_QR:
-    case OP_MATCH:
-    case OP_SUBST:
-    case OP_TRANS:
-    case OP_BACKTICK:
-    case OP_STRINGIFY:
-      return o;
-      break;
-    default:
-      break;
-  }
-
-  dd_linestr_callback(aTHX_ "const", name);
+  dd_handle_const(aTHX_ name);
 
   return o;
 }
 
+#endif /* !DD_CONST_VIA_RV2CV */
+
 static int initialized = 0;
 
 MODULE = Devel::Declare  PACKAGE = Devel::Declare
@@ -396,9 +495,21 @@ void
 setup()
   CODE:
   if (!initialized++) {
-    hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
+#if DD_GROW_VIA_BLOCKHOOK
+    static BHK bhk;
+#if PERL_VERSION_GE(5,13,6)
+    BhkENTRY_set(&bhk, bhk_start, dd_block_start);
+#else /* <5.13.6 */
+    BhkENTRY_set(&bhk, start, dd_block_start);
+#endif /* <5.13.6 */
+    Perl_blockhook_register(aTHX_ &bhk);
+#else /* !DD_GROW_VIA_BLOCKHOOK */
     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
+#endif /* !DD_GROW_VIA_BLOCKHOOK */
+    hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
+#if !DD_CONST_VIA_RV2CV
     hook_op_check(OP_CONST, dd_ck_const, NULL);
+#endif /* !DD_CONST_VIA_RV2CV */
   }
   filter_add(dd_filter_realloc, NULL);
 
@@ -454,10 +565,13 @@ toke_move_past_token(int offset);
   OUTPUT:
     RETVAL
 
-int
+SV*
 toke_scan_str(int offset);
+  PREINIT:
+    int len;
   CODE:
-    RETVAL = dd_toke_scan_str(aTHX_ offset);
+    len = dd_toke_scan_str(aTHX_ offset);
+    RETVAL = len ? newSViv(len) : &PL_sv_undef;
   OUTPUT:
     RETVAL
 
@@ -488,6 +602,13 @@ set_in_declare(int value)
     in_declare = value;
 
 BOOT:
-  if (getenv ("DD_DEBUG")) {
-    dd_debug = atoi(getenv("DD_DEBUG"));
+{
+  char *endptr;
+  char *debug_str = getenv ("DD_DEBUG");
+  if (debug_str) {
+    dd_debug = strtol (debug_str, &endptr, 10);
+    if (*endptr != '\0') {
+      dd_debug = 0;
+    }
   }
+}