added DD::Context::Simple, which packages the synopsis (or method_no_semi.t) for...
[p5sagit/Devel-Declare.git] / Declare.xs
index 34652c9..41488e5 100644 (file)
@@ -1,5 +1,3 @@
-#define PERL_CORE
-#define PERL_NO_GET_CONTEXT
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
 # define Newx(v,n,t) New(0,v,n,t)
 #endif /* !Newx */
 
-#if 0
-#define DD_DEBUG
-#endif
-
-#ifdef DD_DEBUG
-#define DD_DEBUG_S printf("Buffer: %s\n", s);
-#else
-#define DD_DEBUG_S
-#endif
+static int dd_debug = 0;
 
 #define LEX_NORMAL    10
 #define LEX_INTERPNORMAL   9
 
 static int in_declare = 0;
 
+/* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
+   is a lookup into it - so if anything else we can use to tell, so we
+   need to be a bit more careful if PL_parser exists */
+
+#define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
+
+#ifdef PL_parser
+#define DD_HAVE_PARSER PL_parser
+#define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
+#define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
+#else
+#define DD_HAVE_PARSER 1
+#define DD_HAVE_LEX_STUFF PL_lex_stuff
+#define DD_AM_LEXING DD_AM_LEXING_CHECK
+#endif
+
 /* thing that decides whether we're dealing with a declarator */
 
 int dd_is_declarator(pTHX_ char* name) {
@@ -104,10 +110,11 @@ void dd_set_linestr(pTHX_ char* new_value) {
   int new_len = strlen(new_value);
   char* old_linestr = SvPVX(PL_linestr);
 
-  SvGROW(PL_linestr, strlen(new_value));
+  if (SvLEN(PL_linestr) < new_len) {
+    croak("forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
+  }
 
-  if (SvPVX(PL_linestr) != old_linestr)
-    Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
+  SvGROW(PL_linestr, new_len);
 
   memcpy(SvPVX(PL_linestr), new_value, new_len+1);
 
@@ -117,11 +124,12 @@ void dd_set_linestr(pTHX_ char* new_value) {
 }
 
 char* dd_get_lex_stuff(pTHX) {
-  return (PL_lex_stuff ? SvPVX(PL_lex_stuff) : "");
+  return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
 }
 
 char* dd_clear_lex_stuff(pTHX) {
-  PL_lex_stuff = Nullsv;
+  if (DD_HAVE_PARSER)
+    PL_lex_stuff = (SV*)NULL;
 }
 
 char* dd_get_curstash_name(pTHX) {
@@ -186,21 +194,21 @@ 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
+    if (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));
+    }
     call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
-#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
+    if (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));
+    }
     return o;
   }
 
@@ -209,25 +217,22 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   if (kid->op_type != OP_GV) /* not a GV so ignore */
     return o;
 
-  if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
+  if (!DD_AM_LEXING)
     return o; /* not lexing? */
 
-#ifdef DD_DEBUG
-  printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
-#endif
+  if (dd_debug) {
+    printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
+  }
 
   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
 
   if (dd_flags == -1)
     return o;
 
-#ifdef DD_DEBUG
-  printf("dd_flags are: %i\n", dd_flags);
-#endif
-
-#ifdef DD_DEBUG
-  printf("PL_tokenbuf: %s\n", PL_tokenbuf);
-#endif
+  if (dd_debug) {
+    printf("dd_flags are: %i\n", dd_flags);
+    printf("PL_tokenbuf: %s\n", PL_tokenbuf);
+  }
 
   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
 
@@ -242,9 +247,9 @@ OP* dd_pp_entereval(pTHX) {
   STRLEN len;
   const char* s;
   if (SvPOK(sv)) {
-#ifdef DD_DEBUG
-    printf("mangling eval sv\n");
-#endif
+    if (dd_debug) {
+      printf("mangling eval sv\n");
+    }
     if (SvREADONLY(sv))
       sv = sv_2mortal(newSVsv(sv));
     s = SvPVX(sv);
@@ -287,7 +292,7 @@ STATIC OP *dd_ck_const(pTHX_ OP *o) {
   /* if this is set, we just grabbed a delimited string or something,
      not a bareword, so NO TOUCHY */
 
-  if (PL_lex_stuff)
+  if (DD_HAVE_LEX_STUFF)
     return o;
 
   /* don't try and look this up if it's not a string const */
@@ -402,3 +407,8 @@ void
 set_in_declare(int value)
   CODE:
     in_declare = value;
+
+BOOT:
+  if (getenv ("DD_DEBUG")) {
+    dd_debug = 1;
+  }