]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - Magic.xs
Exception propagation fixes
[perl/modules/Variable-Magic.git] / Magic.xs
index 3ec0b6fed0db8342fc010d9d91d51631bbbce645..0cfce8c5c5ff06839f1d9079c3315dfd37e87929 100644 (file)
--- a/Magic.xs
+++ b/Magic.xs
@@ -104,6 +104,10 @@ STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
 # define SvRV_const(sv) SvRV((SV *) sv)
 #endif
 
+#ifndef SvREFCNT_inc_simple_void
+# define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
+#endif
+
 #ifndef mPUSHu
 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
 #endif
@@ -142,8 +146,8 @@ STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
 # endif
 #endif
 
-/* uvar magic and Hash::Util::FieldHash were commited with 28419, but only
- * enable it on 5.10 */
+/* 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_UVAR 1
 #else
@@ -171,7 +175,7 @@ STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
 #endif
 
 /* Applied to dev-5.11 as 34908 */
-#if VMG_HAS_PERL_MAINT(5, 11, 0, 34908)
+#if VMG_HAS_PERL_MAINT(5, 11, 0, 34908) || VMG_HAS_PERL(5, 12, 0)
 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
 #else
 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
@@ -184,7 +188,7 @@ STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
 #endif
 
-#if VMG_HAS_PERL_MAINT(5, 11, 0, 32969)
+#if VMG_HAS_PERL_MAINT(5, 11, 0, 32969) || VMG_HAS_PERL(5, 12, 0)
 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
 #else
 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
@@ -633,9 +637,9 @@ STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
  SPAGAIN;
  nsv = POPs;
 #if VMG_HAS_PERL(5, 8, 3)
- SvREFCNT_inc(nsv);    /* Or it will be destroyed in FREETMPS */
+ SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
 #else
- nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
+ nsv = sv_newref(nsv);          /* Workaround some bug in SvREFCNT_inc() */
 #endif
  PUTBACK;
 
@@ -1018,7 +1022,7 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
  svr = POPs;
  ret = SvOK(svr) ? (U32) SvUV(svr) : len;
  if (t == SVt_PVAV)
-   --ret;
+  --ret;
  PUTBACK;
 
  FREETMPS;
@@ -1034,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;
 
@@ -1052,9 +1055,9 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
  w = vmg_wizard_mgwiz(mg->mg_ptr);
 
  /* So that it survives the temp cleanup below */
- SvREFCNT_inc(sv);
+ SvREFCNT_inc_simple_void(sv);
 
-#if !VMG_HAS_PERL_MAINT(5, 11, 0, 32686)
+#if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
  /* The previous magic tokens were freed but the magic chain wasn't updated, so
   * if you access the sv from the callback the old deleted magics will trigger
   * and cause memory misreads. Change 32686 solved it that way : */
@@ -1072,30 +1075,56 @@ 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)
-  ++PL_error_count;
+ 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;
+  }
+ }
 
  SPAGAIN;
  svr = POPs;
@@ -1117,7 +1146,7 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
 
 #if MGf_COPY
 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
-# if VMG_HAS_PERL_MAINT(5, 11, 0, 33256)
+# if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
   I32 keylen
 # else
   int keylen