extra test, forget where it came from
[p5sagit/Devel-Declare.git] / Declare.xs
index 15ff50c..450e81b 100644 (file)
@@ -1,4 +1,3 @@
-#define PERL_IN_TOKE_C
 #define PERL_CORE
 #define PERL_NO_GET_CONTEXT
 #include "EXTERN.h"
@@ -9,12 +8,14 @@
 #include <stdio.h>
 #include <string.h>
 
+#define DD_HAS_TRAITS
 #if 0
 #define DD_DEBUG
 #endif
 
 #define DD_HANDLE_NAME 1
 #define DD_HANDLE_PROTO 2
+#define DD_HANDLE_PACKAGE 8
 
 #ifdef DD_DEBUG
 #define DD_DEBUG_S printf("Buffer: %s\n", s);
@@ -42,7 +43,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   char* save_s;
   char tmpbuf[sizeof PL_tokenbuf];
   char found_name[sizeof PL_tokenbuf];
-  char* found_proto = NULL;
+  char* found_proto = NULL, *found_traits = NULL;
   STRLEN len = 0;
   HV *stash;
   HV* is_declarator;
@@ -50,7 +51,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   HV* is_declarator_pack_hash;
   SV** is_declarator_flag_ref;
   int dd_flags;
-  char* cb_args[5];
+  char* cb_args[6];
   dSP; /* define stack pointer for later call stuff */
   char* retstr;
   STRLEN n_a; /* for POPpx */
@@ -59,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;
   }
 
@@ -135,9 +150,9 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
 
     DD_DEBUG_S
 
-    /* 0 in arg 4 is allow_package - not trying that yet :) */
+    /* arg 4 is allow_package */
 
-    s = scan_word(s, tmpbuf, sizeof tmpbuf, 0, &len);
+    s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
 
     DD_DEBUG_S
 
@@ -156,6 +171,23 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
     if (*s == '(') { /* found a prototype-ish thing */
       save_s = s;
       s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
+#ifdef DD_HAS_TRAITS
+      {
+          char *traitstart = s = skipspace(s);
+
+          while (*s && *s != '{') ++s;
+          if (*s) {
+              int tlen = s - traitstart;
+              Newx(found_traits, tlen+1, char);
+              Copy(traitstart, found_traits, tlen, char);
+              found_traits[tlen] = 0;
+#ifdef DD_DEBUG
+              printf("found traits..... (%s)\n", found_traits);
+#endif
+          }
+      }
+#endif
+      
       if (SvPOK(PL_lex_stuff)) {
 #ifdef DD_DEBUG
         printf("Found proto %s\n", SvPVX(PL_lex_stuff));
@@ -184,9 +216,11 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
 #endif
   cb_args[0] = HvNAME(stash);
   cb_args[1] = GvNAME(kGVOP_gv);
-  cb_args[2] = found_name;
-  cb_args[3] = found_proto;
-  cb_args[4] = NULL;
+  cb_args[2] = HvNAME(PL_curstash);
+  cb_args[3] = found_name;
+  cb_args[4] = found_proto;
+  cb_args[5] = found_traits;
+  cb_args[6] = NULL;
 
   if (len && found_proto)
     in_declare = 2;
@@ -207,18 +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);
 #endif
-      SvGROW(PL_linestr, strlen(retstr));
+      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 {
@@ -232,7 +275,7 @@ STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
   SV** to_inject_ref;
   int i, pad_inject_list_last;
 
-  o = dd_old_ck_lineseq(o);
+  o = dd_old_ck_lineseq(aTHX_ o);
 
   pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
   if (!pad_inject_list)
@@ -258,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
@@ -270,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);