initialize earlier to help with string evals
Zefram [Sun, 11 Sep 2011 18:17:14 +0000 (19:17 +0100)]
Changes
Declare.xs
lib/Devel/Declare.pm
t/early2.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 98e6e9f..cc32696 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,9 @@ Changes for Devel-Declare
 
   - Depend on B::Hooks::OP::Check version 0.19, which fixes a serious bug in
     how it interacts with other modules that hook ops.
+  - Initialize immediately upon loading the module, so that "was Devel::Declare
+    loaded soon enough" errors in string eval can be fixed by loading the
+    module earlier without having to also actually use the module earlier.
   - Adjust toke_scan_str logic to always show a positive effective length of
     string source.
   - Return undef from toke_scan_str if string was unterminated.
index 65902ff..63ee08b 100644 (file)
@@ -485,16 +485,10 @@ STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
 
 #endif /* !DD_CONST_VIA_RV2CV */
 
-static int initialized = 0;
-
-MODULE = Devel::Declare  PACKAGE = Devel::Declare
-
-PROTOTYPES: DISABLE
-
-void
-setup()
-  CODE:
-  if (!initialized++) {
+STATIC void dd_initialize(pTHX) {
+  static int initialized = 0;
+  if (!initialized) {
+    initialized = 1;
 #if DD_GROW_VIA_BLOCKHOOK
     static BHK bhk;
 #if PERL_VERSION_GE(5,13,6)
@@ -511,7 +505,22 @@ setup()
     hook_op_check(OP_CONST, dd_ck_const, NULL);
 #endif /* !DD_CONST_VIA_RV2CV */
   }
-  filter_add(dd_filter_realloc, NULL);
+}
+
+MODULE = Devel::Declare  PACKAGE = Devel::Declare
+
+PROTOTYPES: DISABLE
+
+void
+initialize()
+  CODE:
+    dd_initialize(aTHX);
+
+void
+setup()
+  CODE:
+    dd_initialize(aTHX);
+    filter_add(dd_filter_realloc, NULL);
 
 char*
 get_linestr()
index 3f3eb76..6b64ccc 100644 (file)
@@ -20,6 +20,8 @@ bootstrap Devel::Declare;
 
 @ISA = ();
 
+initialize();
+
 sub import {
   my ($class, %args) = @_;
   my $target = caller;
diff --git a/t/early2.t b/t/early2.t
new file mode 100644 (file)
index 0000000..750a6b5
--- /dev/null
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+use Devel::Declare;
+eval q[
+    BEGIN {
+       *class = sub (&) { $_[0]->() };
+       Devel::Declare->setup_for(__PACKAGE__, {
+           class => {
+               const => sub {
+                   my ($kw, $off) = @_;
+                   $off += Devel::Declare::toke_move_past_token($off);
+                   $off += Devel::Declare::toke_skipspace($off);
+                   die unless substr(Devel::Declare::get_linestr(), $off, 1) eq '{';
+                   my $l = Devel::Declare::get_linestr();
+                   substr $l, $off + 1, 0, 'pass q[injected];' . (';' x 1000);
+                   Devel::Declare::set_linestr($l);
+               },
+           },
+       });
+    }
+    class {};
+];
+is $@, "";
+
+1;