]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - Upper.xs
Introduce SCOPE()
[perl/modules/Scope-Upper.git] / Upper.xs
index 231acccc4dbf105c942243cdb2502a5cf28f4ea9..cd093dea83fdb52c8771d94f461f5da65598ff53 100644 (file)
--- a/Upper.xs
+++ b/Upper.xs
 
 #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
 
+/* --- Threads and multiplicity -------------------------------------------- */
+
+#ifndef NOOP
+# define NOOP
+#endif
+
+#ifndef dNOOP
+# define dNOOP
+#endif
+
+#ifndef SU_MULTIPLICITY
+# if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
+#  define SU_MULTIPLICITY 1
+# else
+#  define SU_MULTIPLICITY 0
+# endif
+#endif
+#if SU_MULTIPLICITY && !defined(tTHX)
+# define tTHX PerlInterpreter*
+#endif
+
+#if SU_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
+# define SU_THREADSAFE 1
+# ifndef MY_CXT_CLONE
+#  define MY_CXT_CLONE \
+    dMY_CXT_SV;                                                      \
+    my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
+    Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
+    sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
+# endif
+#else
+# define SU_THREADSAFE 0
+# undef  dMY_CXT
+# define dMY_CXT      dNOOP
+# undef  MY_CXT
+# define MY_CXT       su_globaldata
+# undef  START_MY_CXT
+# define START_MY_CXT STATIC my_cxt_t MY_CXT;
+# undef  MY_CXT_INIT
+# define MY_CXT_INIT  NOOP
+# undef  MY_CXT_CLONE
+# define MY_CXT_CLONE NOOP
+#endif
+
 /* --- Stack manipulations ------------------------------------------------- */
 
 #ifndef SvCANEXISTDELETE
@@ -445,22 +489,22 @@ STATIC void su_pop(pTHX_ void *ud) {
 
 /* --- Initialize the stack and the action userdata ------------------------ */
 
-STATIC I32 su_init(pTHX_ I32 level, void *ud, I32 size) {
+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;
 
  LEAVE;
 
- if (level <= 0) {
+ if (cxix >= cxstack_ix) {
   SU_UD_HANDLER(ud)(aTHX_ ud);
   goto done;
  }
 
- SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for level %d\n", ud, level));
+ SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix));
 
- for (i = 0; i < level; ++i) {
-  PERL_CONTEXT *cx = &cxstack[cxstack_ix - i];
+ for (i = cxstack_ix; i > cxix; --i) {
+  PERL_CONTEXT *cx = cxstack + i;
   switch (CxTYPE(cx)) {
 #if SU_HAS_PERL(5, 11, 0)
    case CXt_LOOP_FOR:
@@ -515,19 +559,30 @@ done:
  return depth;
 }
 
-/* --- Unwind stack -------------------------------------------------------- */
+/* --- Global data --------------------------------------------------------- */
+
+#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
 
 typedef struct {
  I32 cxix;
  I32 items;
-} su_ud_unwind;
+ SV  **savesp;
+ OP  fakeop;
+} my_cxt_t;
+
+START_MY_CXT
+
+/* --- Unwind stack -------------------------------------------------------- */
 
 STATIC void su_unwind(pTHX_ void *ud_) {
- su_ud_unwind *ud = (su_ud_unwind *) ud_;
- OP fakeop;
- I32 cxix  = ud->cxix;
- I32 items = ud->items - 1;
- I32 gimme, mark;
+ dMY_CXT;
+ I32 cxix    = MY_CXT.cxix;
+ I32 items   = MY_CXT.items - 1;
+ SV **savesp = MY_CXT.savesp;
+ I32 mark;
+
+ if (savesp)
+  PL_stack_sp = savesp;
 
  if (cxstack_ix > cxix)
   dounwind(cxix);
@@ -537,19 +592,13 @@ STATIC void su_unwind(pTHX_ void *ud_) {
   PL_stack_sp--;
 
  mark = PL_markstack[cxstack[cxix].blk_oldmarksp];
-
- gimme = GIMME_V;
- if (gimme == G_SCALAR) {
-  *PL_markstack_ptr = PL_stack_sp - PL_stack_base;
-  PL_stack_sp += items;
- } else {
-  *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
- }
+ *PL_markstack_ptr = PL_stack_sp - PL_stack_base - items;
 
  SU_D({
+  I32 gimme = GIMME_V;
   PerlIO_printf(Perl_debug_log,
                 "%p: cx=%d gimme=%s items=%d sp=%d oldmark=%d mark=%d\n",
-                ud, cxix,
+                &MY_CXT, cxix,
                 gimme == G_VOID ? "void" : gimme == G_ARRAY ? "list" : "scalar",
                 items, PL_stack_sp - PL_stack_base, *PL_markstack_ptr, mark);
  });
@@ -557,37 +606,64 @@ STATIC void su_unwind(pTHX_ void *ud_) {
  PL_op = PL_ppaddr[OP_RETURN](aTHX);
  *PL_markstack_ptr = mark;
 
- fakeop.op_next = PL_op;
- PL_op = &fakeop;
-
- Safefree(ud);
+ MY_CXT.fakeop.op_next = PL_op;
+ PL_op = &(MY_CXT.fakeop);
 }
 
 /* --- XS ------------------------------------------------------------------ */
 
-#define SU_GET_LEVEL(A)  \
- if (items > A) {        \
-  SV *lsv = ST(A);       \
-  if (SvOK(lsv))         \
-   level = SvUV(lsv);    \
-  if (level < 0)         \
-   XSRETURN(0);          \
- }                       \
- if (level > cxstack_ix) \
-  level = cxstack_ix;
-
-#define SU_DOPOPTOCX(t)                    \
- STMT_START {                              \
-  I32 i, cxix = cxstack_ix, from = 0;      \
-  if (items)                               \
-   from = SvIV(ST(0));                     \
-  for (i = cxix - from; i >= 0; --i) {     \
-   if (CxTYPE(&cxstack[i]) == t) {         \
-    ST(0) = sv_2mortal(newSViv(cxix - i)); \
-    XSRETURN(1);                           \
-   }                                       \
-  }                                        \
-  XSRETURN_UNDEF;                          \
+#if SU_HAS_PERL(5, 8, 9)
+# define SU_SKIP_DB_MAX 2
+#else
+# define SU_SKIP_DB_MAX 3
+#endif
+
+/* Skip context sequences of 1 to SU_SKIP_DB_MAX (included) block contexts
+ * followed by a DB sub */
+
+#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); \
+ } STMT_END
+
+#define SU_GET_CONTEXT(A, B)   \
+ STMT_START {                  \
+  if (items > A) {             \
+   SV *csv = ST(B);            \
+   if (SvOK(csv))              \
+    cxix = SvIV(csv);          \
+   if (cxix < 0)               \
+    cxix = 0;                  \
+   else if (cxix > cxstack_ix) \
+    cxix = cxstack_ix;         \
+  } else {                     \
+   cxix = cxstack_ix;          \
+   if (PL_DBsub)               \
+    SU_SKIP_DB(cxix);          \
+  }                            \
+ } STMT_END
+
+#define SU_GET_LEVEL(A, B) \
+ STMT_START {              \
+  if (items > 0) {         \
+   SV *lsv = ST(B);        \
+   if (SvOK(lsv))          \
+    level = SvIV(lsv);     \
+   if (level < 0)          \
+    level = 0;             \
+  } else                   \
+   level = 0;              \
  } STMT_END
 
 XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
@@ -598,32 +674,33 @@ XS(XS_Scope__Upper_unwind) {
 #else
  dXSARGS;
 #endif
I32 from = 0, cxix = cxstack_ix;
su_ud_unwind *ud;
- SV *level;
dMY_CXT;
I32 cxix;
+
  PERL_UNUSED_VAR(cv); /* -W */
  PERL_UNUSED_VAR(ax); /* -Wall */
- if (items) {
-  from = SvIV(ST(items - 1));
-  if (from < 0)
-   from = 0;
-  else if (from > cxix)
-   from = cxix;
- }
- cxix -= from;
+
+ SU_GET_CONTEXT(0, items - 1);
  do {
   PERL_CONTEXT *cx = cxstack + cxix;
   switch (CxTYPE(cx)) {
    case CXt_SUB:
+    if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
+     continue;
    case CXt_EVAL:
    case CXt_FORMAT:
-    /* pp_entersub will try to sanitize the stack - screw that, we're insane */
-    if (GIMME_V == G_SCALAR)
-     PL_stack_sp = PL_stack_base + TOPMARK + 1;
-    Newx(ud, 1, su_ud_unwind);
-    ud->cxix  = cxix;
-    ud->items = items;
-    SAVEDESTRUCTOR_X(su_unwind, ud);
+    MY_CXT.cxix  = cxix;
+    MY_CXT.items = items;
+    /* pp_entersub will want to sanitize the stack after returning from there
+     * Screw that, we're insane */
+    if (GIMME_V == G_SCALAR) {
+     MY_CXT.savesp = PL_stack_sp;
+     /* dXSARGS calls POPMARK, so we need to match PL_markstack_ptr[1] */
+     PL_stack_sp = PL_stack_base + PL_markstack_ptr[1] + 1;
+    } else {
+     MY_CXT.savesp = NULL;
+    }
+    SAVEDESTRUCTOR_X(su_unwind, NULL);
     return;
    default:
     break;
@@ -638,82 +715,183 @@ PROTOTYPES: ENABLE
 
 BOOT:
 {
- HV *stash = gv_stashpv(__PACKAGE__, 1);
- newCONSTSUB(stash, "HERE", newSViv(0));
+ HV *stash;
+ MY_CXT_INIT;
+ stash = gv_stashpv(__PACKAGE__, 1);
+ newCONSTSUB(stash, "TOP", newSViv(0));
  newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
 }
 
-SV *
-TOP()
-PROTOTYPE:
+void
+CLONE(...)
+PROTOTYPE: DISABLE
 CODE:
- RETVAL = newSViv(cxstack_ix);
-OUTPUT:
- RETVAL
+#if SU_THREADSAFE
+ MY_CXT_CLONE;
+#endif /* SU_THREADSAFE */
 
 SV *
-UP(...)
-PROTOTYPE: ;$
+HERE()
+PROTOTYPE:
 PREINIT:
- I32 i = 0;
  I32 cxix = cxstack_ix;
 CODE:
- if (items)
-  i = SvIV(ST(0));
- if (++i > cxix)
-  i = cxix;
- RETVAL = newSViv(i);
+ if (PL_DBsub)
+  SU_SKIP_DB(cxix);
+ RETVAL = newSViv(cxix);
 OUTPUT:
  RETVAL
 
 SV *
-DOWN(...)
+UP(...)
 PROTOTYPE: ;$
 PREINIT:
- I32 i = 0;
+ I32 cxix;
 CODE:
- if (items)
-  i = SvIV(ST(0));
- if (--i < 0)
-  i = 0;
- RETVAL = newSViv(i);
+ SU_GET_CONTEXT(0, 0);
+ if (--cxix < 0)
+  cxix = 0;
+ if (PL_DBsub)
+  SU_SKIP_DB(cxix);
+ RETVAL = newSViv(cxix);
 OUTPUT:
  RETVAL
 
 void
 SUB(...)
 PROTOTYPE: ;$
+PREINIT:
+ I32 cxix;
 PPCODE:
- SU_DOPOPTOCX(CXt_SUB);
+ SU_GET_CONTEXT(0, 0);
+ for (; cxix >= 0; --cxix) {
+  PERL_CONTEXT *cx = cxstack + cxix;
+  switch (CxTYPE(cx)) {
+   default:
+    continue;
+   case CXt_SUB:
+    if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
+     continue;
+    ST(0) = sv_2mortal(newSViv(cxix));
+    XSRETURN(1);
+  }
+ }
+ XSRETURN_UNDEF;
 
 void
 EVAL(...)
 PROTOTYPE: ;$
+PREINIT:
+ I32 cxix;
+PPCODE:
+ SU_GET_CONTEXT(0, 0);
+ for (; cxix >= 0; --cxix) {
+  PERL_CONTEXT *cx = cxstack + cxix;
+  switch (CxTYPE(cx)) {
+   default:
+    continue;
+   case CXt_EVAL:
+    ST(0) = sv_2mortal(newSViv(cxix));
+    XSRETURN(1);
+  }
+ }
+ XSRETURN_UNDEF;
+
+void
+SCOPE(...)
+PROTOTYPE: ;$
+PREINIT:
+ I32 cxix, level;
 PPCODE:
- SU_DOPOPTOCX(CXt_EVAL);
+ 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)) {
+   case CXt_SUB:
+    if (PL_DBsub && cx->blk_sub.cv == GvCV(PL_DBsub))
+     continue;
+   case CXt_EVAL:
+   case CXt_FORMAT:
+    if (--level < 0)
+     goto done;
+    break;
+  }
+ }
+done:
+ ST(0) = sv_2mortal(newSViv(cxix));
+ XSRETURN(1);
+
+void
+want_at(...)
+PROTOTYPE: ;$
+PREINIT:
+ I32 cxix;
+PPCODE:
+ SU_GET_CONTEXT(0, 0);
+ while (cxix > 0) {
+  PERL_CONTEXT *cx = cxstack + cxix--;
+  switch (CxTYPE(cx)) {
+   case CXt_SUB:
+   case CXt_EVAL:
+   case CXt_FORMAT: {
+    I32 gimme = cx->blk_gimme;
+    switch (gimme) {
+     case G_VOID:   XSRETURN_UNDEF; break;
+     case G_SCALAR: XSRETURN_NO;    break;
+     case G_ARRAY:  XSRETURN_YES;   break;
+    }
+    break;
+   }
+  }
+ }
+ XSRETURN_UNDEF;
 
 void
 reap(SV *hook, ...)
 PROTOTYPE: &;$
 PREINIT:
- I32 level = 0;
+ I32 cxix;
  su_ud_reap *ud;
 CODE:
- SU_GET_LEVEL(1);
+ SU_GET_CONTEXT(1, 1);
  Newx(ud, 1, su_ud_reap);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_reap;
  ud->cb = newSVsv(hook);
- su_init(level, ud, 3);
+ su_init(cxix, ud, 3);
 
 void
 localize(SV *sv, SV *val, ...)
 PROTOTYPE: $$;$
 PREINIT:
- I32 level = 0;
+ I32 cxix;
  su_ud_localize *ud;
 CODE:
- SU_GET_LEVEL(2);
+ SU_GET_CONTEXT(2, 2);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
@@ -721,16 +899,16 @@ CODE:
  ud->sv   = sv;
  ud->val  = newSVsv(val);
  ud->elem = NULL;
- su_init(level, ud, 3);
+ su_init(cxix, ud, 3);
 
 void
 localize_elem(SV *sv, SV *elem, SV *val, ...)
 PROTOTYPE: $$$;$
 PREINIT:
- I32 level = 0;
+ I32 cxix;
  su_ud_localize *ud;
 CODE:
- SU_GET_LEVEL(3);
+ SU_GET_CONTEXT(3, 3);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
@@ -739,16 +917,16 @@ CODE:
  ud->val  = newSVsv(val);
  SvREFCNT_inc(elem);
  ud->elem = elem;
- su_init(level, ud, 4);
+ su_init(cxix, ud, 4);
 
 void
 localize_delete(SV *sv, SV *elem, ...)
 PROTOTYPE: $$;$
 PREINIT:
- I32 level = 0;
+ I32 cxix;
  su_ud_localize *ud;
 CODE:
- SU_GET_LEVEL(2);
+ SU_GET_CONTEXT(2, 2);
  Newx(ud, 1, su_ud_localize);
  SU_UD_ORIGIN(ud)  = NULL;
  SU_UD_HANDLER(ud) = su_localize;
@@ -757,4 +935,4 @@ CODE:
  ud->val  = NULL;
  SvREFCNT_inc(elem);
  ud->elem = elem;
- su_init(level, ud, 4);
+ su_init(cxix, ud, 4);