]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - Magic.xs
Exception propagation fixes
[perl/modules/Variable-Magic.git] / Magic.xs
index ccce4416ba2ad68a50a4e1ff5c1497c2acef9c6f..0cfce8c5c5ff06839f1d9079c3315dfd37e87929 100644 (file)
--- a/Magic.xs
+++ b/Magic.xs
@@ -1038,13 +1038,12 @@ STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
 
 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;
 
@@ -1076,37 +1075,55 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
   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;