]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - Upper.xs
Be stricter about what localize_elem() accepts
[perl/modules/Scope-Upper.git] / Upper.xs
index 73bf6e776b6b1336da7a6f3be59c81679869f6a8..5bf3b496797c67bf67de422a6679e78e27382f3d 100644 (file)
--- a/Upper.xs
+++ b/Upper.xs
 # define SvPV_nolen_const(S) SvPV_nolen(S)
 #endif
 
+#ifndef SvREFCNT_inc_simple_void
+# define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
+#endif
+
 #ifndef HvNAME_get
 # define HvNAME_get(H) HvNAME(H)
 #endif
 # define MY_CXT_CLONE NOOP
 #endif
 
+/* --- Global data --------------------------------------------------------- */
+
+#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
+
+typedef struct {
+ int stack_placeholder;
+ I32 cxix;
+ I32 items;
+ SV  **savesp;
+ OP  fakeop;
+} my_cxt_t;
+
+START_MY_CXT
+
 /* --- Stack manipulations ------------------------------------------------- */
 
+#define SU_SAVE_DESTRUCTOR_SIZE 3
+#define SU_SAVE_INT_SIZE        3
+
 #ifndef SvCANEXISTDELETE
 # define SvCANEXISTDELETE(sv) \
   (!SvRMAGICAL(sv)            \
@@ -137,7 +158,6 @@ STATIC I32 su_av_key2idx(pTHX_ AV *av, I32 key) {
  if (SvRMAGICAL(av)) {
   const MAGIC * const tied_magic = mg_find((SV *) av, PERL_MAGIC_tied);
   if (tied_magic) {
-   int adjust_index = 1;
    SV * const * const negative_indices_glob =
                     hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *) (av), tied_magic))),
                              NEGATIVE_INDICES_VAR, 16, 0);
@@ -177,7 +197,7 @@ STATIC void su_save_adelete(pTHX_ AV *av, I32 idx) {
  Newx(ud, 1, su_ud_adelete);
  ud->av  = av;
  ud->idx = idx;
- SvREFCNT_inc(av);
+ SvREFCNT_inc_simple_void(av);
 
  SAVEDESTRUCTOR_X(su_adelete, ud);
 }
@@ -248,7 +268,7 @@ STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
  if (val) { /* local $x{$keysv} = $val; */
   SvSetMagicSV(*svp, val);
  } else {   /* local $x{$keysv}; delete $x{$keysv}; */
-  hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
+  (void)hv_delete_ent(hv, keysv, G_DISCARD, HeHASH(he));
  }
 }
 
@@ -256,11 +276,13 @@ STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) {
 
 typedef struct {
  I32 depth;
+ I32 pad;
  I32 *origin;
  void (*handler)(pTHX_ void *);
 } su_ud_common;
 
 #define SU_UD_DEPTH(U)   (((su_ud_common *) (U))->depth)
+#define SU_UD_PAD(U)     (((su_ud_common *) (U))->pad)
 #define SU_UD_ORIGIN(U)  (((su_ud_common *) (U))->origin)
 #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler)
 
@@ -278,39 +300,42 @@ typedef struct {
 
 STATIC void su_call(pTHX_ void *ud_) {
  su_ud_reap *ud = (su_ud_reap *) ud_;
-#if SU_HAS_PERL(5, 10, 0)
- I32 dieing = PL_op->op_type == OP_DIE;
+#if SU_HAS_PERL(5, 9, 5)
+ PERL_CONTEXT saved_cx;
+ I32 cxix;
 #endif
 
  dSP;
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: @@@ call at %d (save is %d)\n",
-                                     ud, PL_scopestack_ix, PL_savestack_ix));
+ SU_D({
+  PerlIO_printf(Perl_debug_log,
+                "%p: @@@ call\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
+                 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
+ });
+
  ENTER;
  SAVETMPS;
 
  PUSHMARK(SP);
  PUTBACK;
 
- /* If cxstack_ix isn't incremented there, the eval context will be overwritten
-  * when the new sub scope will be created in call_sv. */
+ /* If the recently popped context isn't saved there, it will be overwritten by
+  * the sub scope from call_sv, although it's still needed in our caller. */
 
-#if SU_HAS_PERL(5, 10, 0)
- if (dieing)
-  if (cxstack_ix < cxstack_max)
-   ++cxstack_ix;
-  else
  cxstack_ix = Perl_cxinc(aTHX);
+#if SU_HAS_PERL(5, 9, 5)
+ if (cxstack_ix < cxstack_max)
+  cxix = cxstack_ix + 1;
+ else
+  cxix = Perl_cxinc(aTHX);
saved_cx = cxstack[cxix];
 #endif
 
  call_sv(ud->cb, G_VOID);
 
-#if SU_HAS_PERL(5, 10, 0)
- if (dieing && cxstack_ix > 0)
-  --cxstack_ix;
+#if SU_HAS_PERL(5, 9, 5)
+ cxstack[cxix] = saved_cx;
 #endif
 
- SPAGAIN;
  PUTBACK;
 
  FREETMPS;
@@ -322,40 +347,36 @@ STATIC void su_call(pTHX_ void *ud_) {
 
 STATIC void su_reap(pTHX_ void *ud) {
 #define su_reap(U) su_reap(aTHX_ (U))
- SU_D(PerlIO_printf(Perl_debug_log, "%p: === reap at %d (save is %d)\n",
-                                     ud, PL_scopestack_ix, PL_savestack_ix));
+ SU_D({
+  PerlIO_printf(Perl_debug_log,
+                "%p: === reap\n%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
+                 ud, ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
+ });
+
  SAVEDESTRUCTOR_X(su_call, ud);
- SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
-                                     ud, PL_savestack_ix,
-                                         PL_scopestack[PL_scopestack_ix]));
 }
 
 /* ... Localize & localize array/hash element .............................. */
 
 typedef struct {
  su_ud_common ci;
- SV *sv;
- SV *val;
- SV *elem;
+ SV    *sv;
+ SV    *val;
+ SV    *elem;
+ svtype type;
 } su_ud_localize;
 
-STATIC void su_localize(pTHX_ void *ud_) {
-#define su_localize(U) su_localize(aTHX_ (U))
- su_ud_localize *ud = (su_ud_localize *) ud_;
- SV *sv   = ud->sv;
- SV *val  = ud->val;
- SV *elem = ud->elem;
- GV *gv;
+STATIC void su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) {
+#define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E))
  UV deref = 0;
  svtype t = SVt_NULL;
 
+ SvREFCNT_inc_simple_void(sv);
+
  if (SvTYPE(sv) >= SVt_PVGV) {
-  gv = (GV *) sv;
-  if (!val) {               /* local *x; */
+  if (!val || !SvROK(val)) { /* local *x; or local *x = $val; */
    t = SVt_PVGV;
-  } else if (!SvROK(val)) { /* local *x = $val; */
-   goto assign;
-  } else {                  /* local *x = \$val; */
+  } else {                   /* local *x = \$val; */
    t = SvTYPE(SvRV(val));
    deref = 1;
   }
@@ -385,14 +406,55 @@ STATIC void su_localize(pTHX_ void *ud_) {
     t = SvTYPE(val);
    }
   }
-  gv = gv_fetchpvn_flags(s, l, GV_ADDMULTI, SVt_PVGV);
+  SvREFCNT_dec(sv);
+  sv = newSVpvn(s, l);
+ }
+
+ switch (t) {
+  case SVt_PVAV:
+  case SVt_PVHV:
+  case SVt_PVCV:
+  case SVt_PVGV:
+   deref = 0;
+  default:
+   break;
+ }
+ /* When deref is set, val isn't NULL */
+
+ ud->sv   = sv;
+ ud->val  = val ? newSVsv(deref ? SvRV(val) : val) : NULL;
+ ud->elem = SvREFCNT_inc(elem);
+ ud->type = t;
+}
+
+STATIC void su_localize(pTHX_ void *ud_) {
+#define su_localize(U) su_localize(aTHX_ (U))
+ su_ud_localize *ud = (su_ud_localize *) ud_;
+ SV *sv   = ud->sv;
+ SV *val  = ud->val;
+ SV *elem = ud->elem;
+ svtype t = ud->type;
+ GV *gv;
+
+ if (SvTYPE(sv) >= SVt_PVGV) {
+  gv = (GV *) sv;
+ } else {
+#ifdef gv_fetchsv
+  gv = gv_fetchsv(sv, GV_ADDMULTI, t);
+#else
+  STRLEN len;
+  const char *name = SvPV_const(sv, len);
+  gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t);
+#endif
  }
 
  SU_D({
-  SV *z = newSV_type(t);
-  PerlIO_printf(Perl_debug_log, "%p: === localize a %s at %d (save is %d)\n",
-                                 ud, sv_reftype(z, 0),
-                                     PL_scopestack_ix, PL_savestack_ix);
+  SV *z = newSV(0);
+  SvUPGRADE(z, t);
+  PerlIO_printf(Perl_debug_log, "%p: === localize a %s\n",ud, sv_reftype(z, 0));
+  PerlIO_printf(Perl_debug_log,
+                "%p: depth=%2d scope_ix=%2d save_ix=%2d\n",
+                 ud, SU_UD_DEPTH(ud), PL_scopestack_ix, PL_savestack_ix);
   SvREFCNT_dec(z);
  });
 
@@ -421,17 +483,9 @@ STATIC void su_localize(pTHX_ void *ud_) {
    break;
   default:
    gv = (GV *) save_scalar(gv);
-maybe_deref:
-   if (deref) /* val != NULL */
-    val = SvRV(val);
    break;
  }
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: savestack is now at %d, base at %d\n",
-                                     ud, PL_savestack_ix,
-                                         PL_scopestack[PL_scopestack_ix]));
-
-assign:
  if (val)
   SvSetMagicSV((SV *) gv, val);
 
@@ -457,55 +511,106 @@ STATIC void su_pop(pTHX_ void *ud) {
  I32 depth, base, mark, *origin;
  depth = SU_UD_DEPTH(ud);
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: --- pop %s at %d from %d to %d [%d]\n",
-                                     ud, SU_CXNAME,
-                                         PL_scopestack_ix, PL_savestack_ix,
-                                         PL_scopestack[PL_scopestack_ix],
-                                         depth));
+ SU_D(
+  PerlIO_printf(Perl_debug_log,
+   "%p: --- pop a %s\n"
+   "%p: leave scope     at depth=%2d scope_ix=%2d cur_top=%2d cur_base=%2d\n",
+    ud, SU_CXNAME,
+    ud, depth, PL_scopestack_ix,PL_savestack_ix,PL_scopestack[PL_scopestack_ix])
+ );
 
  origin = SU_UD_ORIGIN(ud);
  mark   = origin[depth];
  base   = origin[depth - 1];
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: clean from %d down to %d\n",
-                                     ud, mark, base));
+ SU_D(PerlIO_printf(Perl_debug_log,
+                    "%p: original scope was %*c top=%2d     base=%2d\n",
+                     ud,                24, ' ',    mark,        base));
 
  if (base < mark) {
+  SU_D(PerlIO_printf(Perl_debug_log, "%p: clear leftovers\n", ud));
   PL_savestack_ix = mark;
   leave_scope(base);
  }
  PL_savestack_ix = base;
- if (--depth > 0) {
-  SU_UD_DEPTH(ud) = depth;
-  SU_D(PerlIO_printf(Perl_debug_log, "%p: save new destructor at %d [%d]\n",
-                                      ud, PL_savestack_ix, depth));
+
+ SU_UD_DEPTH(ud) = --depth;
+
+ if (depth > 0) {
+  I32 i = 1, pad;
+
+  if (pad = SU_UD_PAD(ud)) {
+   dMY_CXT;
+   do {
+    save_int(&MY_CXT.stack_placeholder);
+   } while (--pad);
+  }
   SAVEDESTRUCTOR_X(su_pop, ud);
-  SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
-                                      ud, PL_savestack_ix, depth));
+
+  /* Skip depths corresponding to scopes for which leave_scope() might not be
+   * called. */
+  while (depth > 1 && PL_scopestack_ix >= i) {
+   I32 j = PL_scopestack[PL_scopestack_ix - i];
+
+   if (j < PL_savestack_ix)
+    break;
+
+   SU_D(PerlIO_printf(Perl_debug_log,
+    "%p: skip scope%*cat depth=%2d scope_ix=%2d new_top=%2d >= cur_base=%2d\n",
+     ud,           6, ' ',   depth, PL_scopestack_ix - i, j, PL_savestack_ix));
+
+   SU_UD_DEPTH(ud) = --depth;
+
+   ++i;
+  }
+
+  SU_D(PerlIO_printf(Perl_debug_log,
+          "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
+           ud,                       depth, PL_scopestack_ix, PL_savestack_ix));
  } else {
   SU_UD_HANDLER(ud)(aTHX_ ud);
  }
+
+ SU_D(PerlIO_printf(Perl_debug_log,
+                    "%p: --- end pop: cur_top=%2d == cur_base=%2d\n",
+                     ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix]));
 }
 
 /* --- Initialize the stack and the action userdata ------------------------ */
 
 STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) {
 #define su_init(L, U, S) su_init(aTHX_ (L), (U), (S))
- I32 i, depth = 0, *origin;
- I32 cur, last, step;
+ I32 i, depth = 1, pad, offset, *origin;
 
LEAVE;
SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
 
- if (cxix >= cxstack_ix) {
-  SU_UD_HANDLER(ud)(aTHX_ ud);
-  goto done;
+ if (size <= SU_SAVE_DESTRUCTOR_SIZE)
+  pad = 0;
+ else {
+  I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE;
+  pad = extra / SU_SAVE_INT_SIZE + ((extra % SU_SAVE_INT_SIZE) ? 1 : 0);
  }
+ offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_INT_SIZE * pad;
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
+ SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n",
+                                     ud,    size,   pad,   offset));
 
  for (i = cxstack_ix; i > cxix; --i) {
   PERL_CONTEXT *cx = cxstack + i;
   switch (CxTYPE(cx)) {
+#if SU_HAS_PERL(5, 10, 0)
+   case CXt_BLOCK:
+    SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is block\n", ud, i));
+    /* Given and when blocks are actually followed by a simple block, so skip
+     * it if needed. */
+    if (cxix > 0) { /* Implies i > 0 */
+     PERL_CONTEXT *next = cx - 1;
+     if (CxTYPE(next) == CXt_GIVEN || CxTYPE(next) == CXt_WHEN)
+      --cxix;
+    }
+    depth++;
+    break;
+#endif
 #if SU_HAS_PERL(5, 11, 0)
    case CXt_LOOP_FOR:
    case CXt_LOOP_PLAIN:
@@ -518,12 +623,12 @@ STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) {
     depth += 2;
     break;
    default:
-    SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is normal\n", ud, i));
+    SU_D(PerlIO_printf(Perl_debug_log, "%p: cx %d is other\n", ud, i));
     depth++;
     break;
   }
  }
- SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth));
+ SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth));
 
  Newx(origin, depth + 1, I32);
  origin[0] = PL_scopestack[PL_scopestack_ix - depth];
@@ -531,47 +636,45 @@ STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) {
  for (i = depth - 1; i >= 1; --i) {
   I32 j = PL_scopestack_ix - i;
   origin[depth - i] = PL_scopestack[j];
-  PL_scopestack[j] += 3;
+  PL_scopestack[j] += offset;
  }
  origin[depth] = PL_savestack_ix;
 
- SU_D({
-  PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
-                depth, 0, PL_scopestack_ix - 1, PL_savestack_ix, origin[depth]);
-  for (i = depth - 1; i >= 0; --i) {
-   I32 x = PL_scopestack_ix  - depth + i;
-   PerlIO_printf(Perl_debug_log, "%p: d=%d s=%d x=%d c=%d o=%d\n", ud,
-                                  i, depth - i, x, PL_scopestack[x], origin[i]);
-  }
- });
-
  SU_UD_ORIGIN(ud) = origin;
  SU_UD_DEPTH(ud)  = depth;
-
- SU_D(PerlIO_printf(Perl_debug_log, "%p: set original destructor at %d [%d]\n",
-                                     ud, PL_savestack_ix, depth));
-
+ SU_UD_PAD(ud)    = pad;
+
+ /* Make sure the first destructor fires by pushing enough fake slots on the
+  * stack. */
+ if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
+                                       <= PL_scopestack[PL_scopestack_ix - 1]) {
+  dMY_CXT;
+  do {
+   SU_D(PerlIO_printf(Perl_debug_log,
+                  "%p: push a fake slot      at scope_ix=%2d  save_ix=%2d\n",
+                   ud,                      PL_scopestack_ix, PL_savestack_ix));
+   save_int(&MY_CXT.stack_placeholder);
+  } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE
+                                        <= PL_scopestack[PL_scopestack_ix - 1]);
+ }
+ SU_D(PerlIO_printf(Perl_debug_log,
+                  "%p: push first destructor at scope_ix=%2d  save_ix=%2d\n",
+                   ud,                      PL_scopestack_ix, PL_savestack_ix));
  SAVEDESTRUCTOR_X(su_pop, ud);
 
-done:
- ENTER;
+ SU_D({
+  for (i = 0; i <= depth; ++i) {
+   I32 j = PL_scopestack_ix  - i;
+   PerlIO_printf(Perl_debug_log,
+                 "%p: depth=%2d scope_ix=%2d saved_floor=%2d new_floor=%2d\n",
+                  ud,        i, j, origin[depth - i],
+                                   i == 0 ? PL_savestack_ix : PL_scopestack[j]);
+  }
+ });
 
  return depth;
 }
 
-/* --- Global data --------------------------------------------------------- */
-
-#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
-
-typedef struct {
- I32 cxix;
- I32 items;
- SV  **savesp;
- OP  fakeop;
-} my_cxt_t;
-
-START_MY_CXT
-
 /* --- Unwind stack -------------------------------------------------------- */
 
 STATIC void su_unwind(pTHX_ void *ud_) {
@@ -581,6 +684,8 @@ STATIC void su_unwind(pTHX_ void *ud_) {
  SV **savesp = MY_CXT.savesp;
  I32 mark;
 
+ PERL_UNUSED_VAR(ud_);
+
  if (savesp)
   PL_stack_sp = savesp;
 
@@ -623,34 +728,53 @@ STATIC void su_unwind(pTHX_ void *ud_) {
 
 #define SU_SKIP_DB(C) \
  STMT_START {         \
-  I32 i = 1;          \
-  PERL_CONTEXT *cx = cxstack + (C); \
-  do {                              \
-   if (CxTYPE(cx) == CXt_BLOCK && (C) >= i) { \
-    --cx;                                     \
-    if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.cv == GvCV(PL_DBsub)) { \
-     (C) -= i + 1;                \
-     break;                       \
-    }                             \
-   } else                         \
-    break;                        \
-  } while (++i <= SU_SKIP_DB_MAX); \
+  I32 skipped = 0;    \
+  PERL_CONTEXT *base = cxstack;      \
+  PERL_CONTEXT *cx   = base + (C);   \
+  while (cx >= base && (C) > skipped && CxTYPE(cx) == CXt_BLOCK) \
+   --cx, ++skipped;                  \
+  if (cx >= base && (C) > skipped) { \
+   switch (CxTYPE(cx)) {  \
+    case CXt_SUB:         \
+     if (skipped <= SU_SKIP_DB_MAX && cx->blk_sub.cv == GvCV(PL_DBsub)) \
+      (C) -= skipped + 1; \
+      break;              \
+    default:              \
+     break;               \
+   }                      \
+  }                       \
  } STMT_END
 
 #define SU_GET_CONTEXT(A, B)   \
  STMT_START {                  \
   if (items > A) {             \
    SV *csv = ST(B);            \
-   if (SvOK(csv))              \
-    cxix = SvIV(csv);          \
+   if (!SvOK(csv))             \
+    goto default_cx;           \
+   cxix = SvIV(csv);           \
    if (cxix < 0)               \
     cxix = 0;                  \
    else if (cxix > cxstack_ix) \
     cxix = cxstack_ix;         \
-  } else                       \
+  } else {                     \
+default_cx:                    \
    cxix = cxstack_ix;          \
-  if (PL_DBsub)                \
-   SU_SKIP_DB(cxix);           \
+   if (PL_DBsub)               \
+    SU_SKIP_DB(cxix);          \
+  }                            \
+ } STMT_END
+
+#define SU_GET_LEVEL(A, B) \
+ STMT_START {              \
+  level = 0;               \
+  if (items > 0) {         \
+   SV *lsv = ST(B);        \
+   if (SvOK(lsv)) {        \
+    level = SvIV(lsv);     \
+    if (level < 0)         \
+     level = 0;            \
+   }                       \
+  }                        \
  } STMT_END
 
 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
@@ -703,18 +827,28 @@ PROTOTYPES: ENABLE
 BOOT:
 {
  HV *stash;
+
  MY_CXT_INIT;
+ MY_CXT.stack_placeholder = 0;
+
  stash = gv_stashpv(__PACKAGE__, 1);
- newCONSTSUB(stash, "TOP", newSViv(0));
+ newCONSTSUB(stash, "TOP",           newSViv(0));
+ newCONSTSUB(stash, "SU_THREADSAFE", newSVuv(SU_THREADSAFE));
+
  newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
 }
 
+#if SU_THREADSAFE
+
 void
 CLONE(...)
 PROTOTYPE: DISABLE
 CODE:
-#if SU_THREADSAFE
- MY_CXT_CLONE;
+ PERL_UNUSED_VAR(items);
+ {
+  MY_CXT_CLONE;
+ }
+
 #endif /* SU_THREADSAFE */
 
 SV *
@@ -744,19 +878,6 @@ CODE:
 OUTPUT:
  RETVAL
 
-SV *
-DOWN(...)
-PROTOTYPE: ;$
-PREINIT:
- I32 cxix;
-CODE:
- SU_GET_CONTEXT(0, 0);
- if (++cxix > cxstack_ix)
-  cxix = cxstack_ix;
- RETVAL = newSViv(cxix);
-OUTPUT:
- RETVAL
-
 void
 SUB(...)
 PROTOTYPE: ;$
@@ -798,18 +919,36 @@ PPCODE:
  XSRETURN_UNDEF;
 
 void
-CALLER(...)
+SCOPE(...)
 PROTOTYPE: ;$
 PREINIT:
- I32 cxix, caller = 0;
+ I32 cxix, level;
 PPCODE:
- if (items) {
-  SV *csv = ST(0);
-  if (SvOK(csv))
-   caller = SvIV(csv);
-  if (caller < 0)
-   caller = 0;
+ SU_GET_LEVEL(0, 0);
+ cxix = cxstack_ix;
+ if (PL_DBsub) {
+  SU_SKIP_DB(cxix);
+  while (cxix > 0) {
+   if (--level < 0)
+    break;
+   --cxix;
+   SU_SKIP_DB(cxix);
+  }
+ } else {
+  cxix -= level;
+  if (cxix < 0)
+   cxix = 0;
  }
+ ST(0) = sv_2mortal(newSViv(cxix));
+ XSRETURN(1);
+
+void
+CALLER(...)
+PROTOTYPE: ;$
+PREINIT:
+ I32 cxix, level;
+PPCODE:
+ SU_GET_LEVEL(0, 0);
  for (cxix = cxstack_ix; cxix > 0; --cxix) {
   PERL_CONTEXT *cx = cxstack + cxix;
   switch (CxTYPE(cx)) {
@@ -818,8 +957,7 @@ PPCODE:
      continue;
    case CXt_EVAL:
    case CXt_FORMAT:
-    --caller;
-    if (caller < 0)
+    if (--level < 0)
      goto done;
     break;
   }
@@ -872,17 +1010,19 @@ localize(SV *sv, SV *val, ...)
 PROTOTYPE: $$;$
 PREINIT:
  I32 cxix;
+ I32 size = 3;
  su_ud_localize *ud;
 CODE:
  SU_GET_CONTEXT(2, 2);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
- SvREFCNT_inc(sv);
- ud->sv   = sv;
- ud->val  = newSVsv(val);
- ud->elem = NULL;
- su_init(cxix, ud, 3);
+ su_ud_localize_init(ud, sv, val, NULL);
+#if !SU_HAS_PERL(5, 8, 9)
+ if (ud->type >= SVt_PVGV)
+  size = 6;
+#endif
+ su_init(cxix, ud, size);
 
 void
 localize_elem(SV *sv, SV *elem, SV *val, ...)
@@ -891,15 +1031,17 @@ PREINIT:
  I32 cxix;
  su_ud_localize *ud;
 CODE:
+ if (SvTYPE(sv) >= SVt_PVGV)
+  croak("Can't infer the element localization type from a glob and the value");
  SU_GET_CONTEXT(3, 3);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
SvREFCNT_inc(sv);
- ud->sv   = sv;
ud->val  = newSVsv(val);
SvREFCNT_inc(elem);
- ud->elem = elem;
su_ud_localize_init(ud, sv, val, elem);
+ if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) {
 Safefree(ud);
 croak("Can't localize an element of something that isn't an array or a hash");
+ }
  su_init(cxix, ud, 4);
 
 void
@@ -907,15 +1049,16 @@ localize_delete(SV *sv, SV *elem, ...)
 PROTOTYPE: $$;$
 PREINIT:
  I32 cxix;
+ I32 size = 4;
  su_ud_localize *ud;
 CODE:
  SU_GET_CONTEXT(2, 2);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
SvREFCNT_inc(sv);
- ud->sv   = sv;
- ud->val  = NULL;
SvREFCNT_inc(elem);
- ud->elem = elem;
- su_init(cxix, ud, 4);
su_ud_localize_init(ud, sv, NULL, elem);
+#if !SU_HAS_PERL(5, 8, 9)
+ if (ud->type >= SVt_PVGV)
 size = 6;
+#endif
+ su_init(cxix, ud, size);