# define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
#endif
-#if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
-# ifndef PL_error_count
-# define PL_error_count PL_parser->error_count
-# endif
-#else
-# ifndef PL_error_count
-# define PL_error_count PL_Ierror_count
-# endif
-#endif
-
/* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
* enable them on 5.10 */
#if VMG_HAS_PERL(5, 10, 0)
# define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
#endif
+/* ... Bug-free mg_magical ................................................. */
+
+/* See the discussion at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html. This version is specialized to our needs. */
+
#if VMG_UVAR
-/* Bug-free mg_magical - see http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html - but specialized to our needs. */
STATIC void vmg_sv_magicuvar(pTHX_ SV *sv, const char *uf, I32 len) {
#define vmg_sv_magicuvar(S, U, L) vmg_sv_magicuvar(aTHX_ (S), (U), (L))
const MAGIC* mg;
#endif /* VMG_UVAR */
+/* ... Safe version of call_sv() ........................................... */
+
+#define VMG_SAVE_LAST_CX (!VMG_HAS_PERL(5, 8, 4) || VMG_HAS_PERL(5, 9, 5))
+
+STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, I32 destructor) {
+#define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
+ I32 ret, cxix = 0, in_eval = 0;
+#if VMG_SAVE_LAST_CX
+ PERL_CONTEXT saved_cx;
+#endif
+ SV *old_err = NULL;
+
+ if (SvTRUE(ERRSV)) {
+ old_err = ERRSV;
+ ERRSV = newSV(0);
+ }
+
+ if (cxstack_ix < cxstack_max) {
+ cxix = cxstack_ix + 1;
+ if (destructor && CxTYPE(cxstack + cxix) == CXt_EVAL)
+ in_eval = 1;
+ }
+
+#if VMG_SAVE_LAST_CX
+ /* The last popped context will be reused by call_sv(), but our callers may
+ * still need its previous value. Back it up so that it isn't clobbered. */
+ saved_cx = cxstack[cxix];
+#endif
+
+ ret = call_sv(sv, flags | G_EVAL);
+
+#if VMG_SAVE_LAST_CX
+ cxstack[cxix] = saved_cx;
+#endif
+
+ 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);
+ }
+#if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
+ if (PL_parser)
+ ++PL_parser->error_count;
+#elif defined(PL_error_count)
+ ++PL_error_count;
+#else
+ ++PL_Ierror_count;
+#endif
+ } else if (!in_eval)
+ croak(NULL);
+ } else {
+ if (old_err) {
+ SvREFCNT_dec(ERRSV);
+ ERRSV = old_err;
+ }
+ }
+
+ return ret;
+}
+
/* --- Stolen chunk of B --------------------------------------------------- */
typedef enum {
PUSHs(args[i]);
PUTBACK;
- call_sv(ctor, G_SCALAR);
+ vmg_call_sv(ctor, G_SCALAR, 0);
SPAGAIN;
nsv = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- call_sv(cb, G_SCALAR);
+ vmg_call_sv(cb, G_SCALAR, 0);
SPAGAIN;
svr = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- call_sv(w->cb_len, G_SCALAR);
+ vmg_call_sv(w->cb_len, G_SCALAR, 0);
SPAGAIN;
svr = POPs;
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;
-#endif
int ret = 0;
- SV *svr, *old_err = NULL;
+ SV *svr;
dSP;
XPUSHs(vmg_op_info(w->opinfo));
PUTBACK;
- 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. */
- saved_cx = cxstack[cxix];
-#endif
-
- call_sv(w->cb_free, G_SCALAR | G_EVAL);
-
-#if VMG_HAS_PERL(5, 9, 5)
- cxstack[cxix] = saved_cx;
-#endif
-
- 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)
-#endif
- ++PL_error_count;
- } else if (!in_eval)
- croak(NULL);
- } else {
- if (old_err) {
- SvREFCNT_dec(ERRSV);
- ERRSV = old_err;
- }
- }
+ vmg_call_sv(w->cb_free, G_SCALAR, 1);
SPAGAIN;
svr = POPs;
my $u = $b;
is_deeply(\@callers, [
- [ 'main', $0, __LINE__-2 ],
+ ([ 'main', $0, __LINE__-2 ]) x 2,
], 'caller into callback returns the right thing');
@callers = ();
$u = $b;
is_deeply(\@callers, [
- [ 'main', $0, __LINE__-2 ],
+ ([ 'main', $0, __LINE__-2 ]) x 2,
], 'caller into callback returns the right thing (second time)');
{
@callers = ();
my $u = $b;
is_deeply(\@callers, [
- [ 'main', $0, __LINE__-2 ],
+ ([ 'main', $0, __LINE__-2 ]) x 2,
], 'caller into callback into block returns the right thing');
}
eval { my $u = $b };
is($@, '', 'caller into callback doesn\'t croak');
is_deeply(\@callers, [
- ([ 'main', $0, __LINE__-3 ]) x 2,
+ ([ 'main', $0, __LINE__-3 ]) x 3,
], 'caller into callback into eval returns the right thing');