STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
const MGWIZ *w;
+ I32 cxix = 0, in_eval = 0;
#if VMG_HAS_PERL(5, 9, 5)
PERL_CONTEXT saved_cx;
- I32 cxix;
#endif
- I32 had_err, has_err, flags = G_SCALAR | G_EVAL;
int ret = 0;
- SV *svr;
+ SV *svr, *old_err = NULL;
dSP;
XPUSHs(vmg_op_info(w->opinfo));
PUTBACK;
- had_err = SvTRUE(ERRSV);
- if (had_err)
- flags |= G_KEEPERR;
+ if (SvTRUE(ERRSV)) {
+ old_err = ERRSV;
+ ERRSV = newSV(0);
+ }
+
+ if (cxstack_ix < cxstack_max) {
+ cxix = cxstack_ix + 1;
+ if (CxTYPE(cxstack + cxix) == CXt_EVAL)
+ in_eval = 1;
+ }
#if VMG_HAS_PERL(5, 9, 5)
/* This context should not be used anymore, but since we croak in places the
* core doesn't even dare to, some pointers to it may remain in the upper call
* stack. Make sure call_sv() doesn't clobber it. */
- if (cxstack_ix < cxstack_max)
- cxix = cxstack_ix + 1;
- else
- cxix = Perl_cxinc(aTHX);
saved_cx = cxstack[cxix];
#endif
- call_sv(w->cb_free, flags);
+ call_sv(w->cb_free, G_SCALAR | G_EVAL);
#if VMG_HAS_PERL(5, 9, 5)
cxstack[cxix] = saved_cx;
#endif
- has_err = SvTRUE(ERRSV);
- if (IN_PERL_COMPILETIME && !had_err && has_err) {
- if (PL_errors)
- sv_catsv(PL_errors, ERRSV);
- else
- Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
+ if (SvTRUE(ERRSV)) {
+ if (old_err) {
+ sv_setsv(old_err, ERRSV);
+ SvREFCNT_dec(ERRSV);
+ ERRSV = old_err;
+ }
+ if (IN_PERL_COMPILETIME) {
+ if (!PL_in_eval) {
+ if (PL_errors)
+ sv_catsv(PL_errors, ERRSV);
+ else
+ Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
+ SvCUR_set(ERRSV, 0);
+ }
#ifdef PL_parser
- if (PL_parser)
+ if (PL_parser)
#endif
- ++PL_error_count;
+ ++PL_error_count;
+ } else if (!in_eval)
+ croak(NULL);
+ } else {
+ if (old_err) {
+ SvREFCNT_dec(ERRSV);
+ ERRSV = old_err;
+ }
}
SPAGAIN;
-#!perl -T
+#!perl
use strict;
use warnings;
-use Test::More tests => 10 + 1;
+use Test::More tests => 14 + 1;
use Variable::Magic qw/wizard cast/;
my $wiz;
+sub expect {
+ my ($name, $where, $suffix) = @_;
+ $where = defined $where ? quotemeta $where : '\(eval \d+\)';
+ my $end = defined $suffix ? "$suffix\$" : '$';
+ qr/^\Q$name\E at $where line \d+\.$end/
+}
+
eval {
$wiz = wizard data => sub { $_[1]->() };
my $x;
cast $x, $wiz, sub { die "carrot" };
};
-like $@, qr/carrot/, 'die in data callback';
+like $@, expect('carrot', $0), 'die in data callback';
eval {
$wiz = wizard data => sub { $_[1] },
$x = 5;
};
-like $@, qr/lettuce/, 'die in set callback';
+like $@, expect('lettuce', $0), 'die in set callback';
my $res = eval {
$wiz = wizard data => sub { $_[1] },
@a;
};
-like $@, qr/potato/, 'die in len callback';
+like $@, expect('potato', $0), 'die in len callback';
eval {
$wiz = wizard data => sub { $_[1] },
cast $x, $wiz, sub { die "spinach" };
};
-like $@, qr/spinach/, 'die in free callback';
+like $@, expect('spinach', $0), 'die in free callback';
+
+eval {
+ $wiz = wizard free => sub { die 'zucchini' };
+ $@ = "";
+ {
+ my $x;
+ cast $x, $wiz;
+ }
+ die 'not reached';
+};
+
+like $@, expect('zucchini', $0),
+ 'die in free callback in block in eval with $@ unset';
+
+eval {
+ $wiz = wizard free => sub { die 'eggplant' };
+ $@ = "vuvuzela";
+ {
+ my $x;
+ cast $x, $wiz;
+ }
+ die 'not reached again';
+};
+
+like $@, expect('eggplant', $0),
+ 'die in free callback in block in eval with $@ set';
+
+eval q{BEGIN {
+ $wiz = wizard free => sub { die 'onion' };
+ my $x;
+ cast $x, $wiz;;
+}};
+
+like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
# Inspired by B::Hooks::EndOfScope
cast $x, $wiz, sub { die "pumpkin" };
}};
-like $@, qr/pumpkin/, 'die in data callback in BEGIN';
+like $@, expect('pumpkin', undef, "\nBEGIN.*"), 'die in data callback in BEGIN';
eval q{BEGIN {
$wiz = wizard data => sub { $_[1] },
cast %^H, $wiz, sub { die "macaroni" };
}};
-like $@, qr/macaroni/, 'die in free callback in BEGIN';
+like $@, expect('macaroni'), 'die in free callback at end of scope';
eval q{BEGIN {
$wiz = wizard data => sub { $_[1] },
cast @a, $wiz, sub { die "pepperoni" };
}};
-like $@, qr/pepperoni/, 'die in len callback in BEGIN';
+like $@, expect('pepperoni', undef, "\nBEGIN.*"),'die in len callback in BEGIN';
use lib 't/lib';
+
+
eval "use Variable::Magic::TestScopeEnd";
-like $@, qr/turnip/, 'die in BEGIN in require triggers hints hash destructor';
+like $@,
+ expect('turnip', 't/lib/Variable/Magic/TestScopeEnd.pm', "\nBEGIN(?s:.*)"),
+ 'die in BEGIN in require in eval string triggers hints hash destructor';
eval q{BEGIN {
Variable::Magic::TestScopeEnd::hook {
die "tomato";
}};
-like $@, qr/tomato/, 'die in BEGIN in eval triggers hints hash destructor';
+like $@, expect('tomato', undef, "\nBEGIN.*"),
+ 'die in BEGIN in eval triggers hints hash destructor';
+
+sub run_perl {
+ my $code = shift;
+
+ my $SystemRoot = $ENV{SystemRoot};
+ local %ENV;
+ $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
+
+ system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
+}
+
+SKIP:
+{
+ skip 'Capture::Tiny 0.08 is not installed' => 1
+ unless eval "use Capture::Tiny 0.08 (); 1";
+ my $code = 'use Variable::Magic qw/wizard cast/; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }';
+ my $output = Capture::Tiny::capture_merged(sub { run_perl $code });
+ skip 'Test code didn\'t run properly' => 1 unless defined $output;
+ like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
+ 'die at compile time and not in eval string';
+}