[Bast-commits] r3553 - in trunk/Devel-Declare: . lib/Devel t

matthewt at dev.catalyst.perl.org matthewt at dev.catalyst.perl.org
Sun Jul 1 17:58:39 GMT 2007


Author: matthewt
Date: 2007-07-01 17:58:39 +0100 (Sun, 01 Jul 2007)
New Revision: 3553

Added:
   trunk/Devel-Declare/t/padstuff.t
Modified:
   trunk/Devel-Declare/Declare.xs
   trunk/Devel-Declare/lib/Devel/Declare.pm
   trunk/Devel-Declare/t/simple.t
Log:
pad stuffing by source injection

Modified: trunk/Devel-Declare/Declare.xs
===================================================================
--- trunk/Devel-Declare/Declare.xs	2007-06-29 13:48:53 UTC (rev 3552)
+++ trunk/Devel-Declare/Declare.xs	2007-07-01 16:58:39 UTC (rev 3553)
@@ -9,7 +9,9 @@
 #include <stdio.h>
 #include <string.h>
 
-#define DD_DEBUG 0
+#if 0
+#define DD_DEBUG
+#endif
 
 #define DD_HANDLE_NAME 1
 #define DD_HANDLE_PROTO 2
@@ -26,7 +28,7 @@
 /* placeholders for PL_check entries we wrap */
 
 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
-STATIC OP *(*dd_old_ck_nextstate)(pTHX_ OP *op);
+STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op);
 
 /* flag to trigger removal of temporary declaree sub */
 
@@ -49,6 +51,9 @@
   SV** is_declarator_flag_ref;
   int dd_flags;
   char* cb_args[5];
+  dSP; /* define stack pointer for later call stuff */
+  char* retstr;
+  STRLEN n_a; /* for POPpx */
 
   o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
 
@@ -156,7 +161,10 @@
         printf("Found proto %s\n", SvPVX(PL_lex_stuff));
 #endif
         found_proto = SvPVX(PL_lex_stuff);
-        *save_s++ = '=';
+        if (len) /* foo name () => foo name  X, only foo parsed so works */
+          *save_s++ = ' ';
+        else /* foo () => foo =X, TOKEN('&') won't handle foo X */
+          *save_s++ = '=';
         *save_s++ = 'X';
         while (save_s < s) {
           *save_s++ = ' ';
@@ -168,26 +176,85 @@
     }
   }
 
-  if (len || found_proto) {
-    if (!len)
-      found_name[0] = 0;
+  if (!len)
+    found_name[0] = 0;
+
 #ifdef DD_DEBUG
-  printf("Calling init_declare");
+  printf("Calling init_declare\n");
 #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[0] = HvNAME(stash);
+  cb_args[1] = GvNAME(kGVOP_gv);
+  cb_args[2] = found_name;
+  cb_args[3] = found_proto;
+  cb_args[4] = NULL;
+
+  if (len && found_proto)
+    in_declare = 2;
+  else if (len || found_proto)
+    in_declare = 1;
+  if (found_proto)
+    PL_lex_stuff = Nullsv;
+  s = skipspace(s);
+#ifdef DD_DEBUG
+  printf("cur buf: %s\n", s);
+  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));
+#endif
+  if (*s++ == '{') {
+    call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
+    SPAGAIN;
+    retstr = POPpx;
+    PUTBACK;
+    if (retstr && strlen(retstr)) {
+#ifdef DD_DEBUG
+      printf("Got string %s\n", retstr);
+#endif
+      SvGROW(PL_linestr, strlen(retstr));
+      memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
+      memmove(s, retstr, strlen(retstr));
+      PL_bufend += strlen(retstr);
+#ifdef DD_DEBUG
+  printf("cur buf: %s\n", s);
+  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));
+#endif
+    }
+  } else {
     call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
-    if (len && found_proto)
-      in_declare = 2;
-    else
-      in_declare = 1;
-    if (found_proto)
-      PL_lex_stuff = Nullsv;
   }
+  return o;
+}
 
+STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
+  AV* pad_inject_list;
+  SV** to_inject_ref;
+  int i, pad_inject_list_last;
+
+  o = dd_old_ck_lineseq(o);
+
+  pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
+  if (!pad_inject_list)
+    return o;
+
+  pad_inject_list_last = av_len(pad_inject_list);
+
+  if (pad_inject_list_last == -1)
+    return o;
+
+  for (i = 0; i <= pad_inject_list_last; i++) {
+    to_inject_ref = av_fetch(pad_inject_list, i, FALSE);
+    if (to_inject_ref && SvPOK(*to_inject_ref)) {
+#ifdef DD_DEBUG
+  printf("Injecting %s into pad\n", SvPVX(*to_inject_ref));
+#endif
+      allocmy(SvPVX(*to_inject_ref));
+    }
+  }
+
+  av_clear(pad_inject_list);
+
   return o;
 }
 
@@ -203,6 +270,8 @@
   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

Modified: trunk/Devel-Declare/lib/Devel/Declare.pm
===================================================================
--- trunk/Devel-Declare/lib/Devel/Declare.pm	2007-06-29 13:48:53 UTC (rev 3552)
+++ trunk/Devel-Declare/lib/Devel/Declare.pm	2007-07-01 16:58:39 UTC (rev 3553)
@@ -10,8 +10,9 @@
 
 use constant DECLARE_NAME => 1;
 use constant DECLARE_PROTO => 2;
+use constant DECLARE_NONE => 4;
 
-use vars qw(%declarators %declarator_handlers);
+use vars qw(%declarators %declarator_handlers @next_pad_inject);
 use base qw(DynaLoader);
 
 bootstrap Devel::Declare;
@@ -21,8 +22,8 @@
   my $target = caller;
   if (@_ == 1) { # "use Devel::Declare;"
     no strict 'refs';
-    foreach my $name (qw(DECLARE_NAME DECLARE_PROTO)) {
-      *{"${target}::${name}"} = *{"${name}"};
+    foreach my $name (qw(NAME PROTO NONE)) {
+      *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"};
     }
   } else {
     $class->setup_for($target => \%args);
@@ -67,9 +68,10 @@
 
 sub init_declare {
   my ($pack, $use, $name, $proto) = @_;
-  my ($name_h, $XX_h) = $declarator_handlers{$pack}{$use}->(
-                            $pack, $use, $name, $proto
-                        );
+  my ($name_h, $XX_h, $extra_code)
+       = $declarator_handlers{$pack}{$use}->(
+           $pack, $use, $name, $proto, defined(wantarray)
+         );
   ($temp_pack, $temp_name, $temp_save) = ($pack, [], []);
   if ($name) {
     push(@$temp_name, $name);
@@ -87,6 +89,11 @@
     no warnings 'prototype';
     *{"${pack}::X"} = $XX_h;
   }
+  if (defined wantarray) {
+    return $extra_code || '0;';
+  } else {
+    return;
+  }
 }
 
 sub done_declare {
@@ -101,6 +108,10 @@
   }
 }
 
+sub inject_into_next_pad {
+  shift; @next_pad_inject = @_;
+}
+
 =head1 NAME
 
 Devel::Declare - 

Added: trunk/Devel-Declare/t/padstuff.t
===================================================================
--- trunk/Devel-Declare/t/padstuff.t	                        (rev 0)
+++ trunk/Devel-Declare/t/padstuff.t	2007-07-01 16:58:39 UTC (rev 3553)
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+use Test::More 'no_plan';
+
+sub action (&) { return shift; }
+
+sub handle_action {
+  return (undef, undef, 'my ($self, $c) = (shift, shift);');
+}
+
+use Devel::Declare;
+use Devel::Declare action => [ DECLARE_NONE, \&handle_action ];
+
+my $args;
+
+my $a = action {
+  $args = join(', ', $self, $c);
+};
+
+$a->("SELF", "CONTEXT");
+
+is($args, "SELF, CONTEXT", "args passed ok");

Modified: trunk/Devel-Declare/t/simple.t
===================================================================
--- trunk/Devel-Declare/t/simple.t	2007-06-29 13:48:53 UTC (rev 3552)
+++ trunk/Devel-Declare/t/simple.t	2007-07-01 16:58:39 UTC (rev 3553)
@@ -24,7 +24,7 @@
 method # blather
   baz
   # whee
-{
+{ # fweet
   $args2 = join(', ', @_);
 };
 




More information about the Bast-commits mailing list