]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - Upper.xs
In su_unwind(), get the gimme before unwinding
[perl/modules/Scope-Upper.git] / Upper.xs
index 231acccc4dbf105c942243cdb2502a5cf28f4ea9..f89f1dc2982b15bc83324010f15b45c838915308 100644 (file)
--- a/Upper.xs
+++ b/Upper.xs
@@ -529,6 +529,8 @@ STATIC void su_unwind(pTHX_ void *ud_) {
  I32 items = ud->items - 1;
  I32 gimme, mark;
 
+ gimme = GIMME_V;
+
  if (cxstack_ix > cxix)
   dounwind(cxix);
 
@@ -538,7 +540,6 @@ STATIC void su_unwind(pTHX_ void *ud_) {
 
  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;
@@ -565,23 +566,37 @@ STATIC void su_unwind(pTHX_ void *ud_) {
 
 /* --- 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_GET_LEVEL(A)   \
+ STMT_START {             \
+  if (items > A) {        \
+   SV *lsv = ST(A);       \
+   if (SvOK(lsv))         \
+    level = SvIV(lsv);    \
+   if (level < 0)         \
+    XSRETURN(0);          \
+  }                       \
+  if (level > cxstack_ix) \
+   level = cxstack_ix;    \
+ } STMT_END
+
+#define SU_GET_CONTEXT(A, B) \
+ STMT_START {                \
+  if (items > A) {           \
+   SV *lsv = ST(B);          \
+   if (SvOK(lsv))            \
+    level = SvIV(lsv);       \
+   if (level < 0)            \
+    level = 0;               \
+   else if (level > cxix)    \
+    level = cxix;            \
+  }                          \
+ } STMT_END
 
 #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) {     \
+  I32 i, cxix = cxstack_ix, level = 0;     \
+  SU_GET_CONTEXT(0, 0);                    \
+  for (i = cxix - level; i >= 0; --i) {    \
    if (CxTYPE(&cxstack[i]) == t) {         \
     ST(0) = sv_2mortal(newSViv(cxix - i)); \
     XSRETURN(1);                           \
@@ -598,19 +613,14 @@ XS(XS_Scope__Upper_unwind) {
 #else
  dXSARGS;
 #endif
- I32 from = 0, cxix = cxstack_ix;
+ I32 cxix = cxstack_ix, level = 0;
  su_ud_unwind *ud;
- SV *level;
+
  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);
+ cxix -= level;
  do {
   PERL_CONTEXT *cx = cxstack + cxix;
   switch (CxTYPE(cx)) {
@@ -692,6 +702,61 @@ PROTOTYPE: ;$
 PPCODE:
  SU_DOPOPTOCX(CXt_EVAL);
 
+void
+CALLER(...)
+PROTOTYPE: ;$
+PREINIT:
+ I32 cxix = cxstack_ix, caller = 0, level = 0;
+PPCODE:
+ if (items) {
+  SV *csv = ST(0);
+  if (SvOK(csv))
+   caller = SvIV(csv);
+ }
+ cxix = cxstack_ix;
+ while (cxix > 0) {
+  PERL_CONTEXT *cx = cxstack + cxix--;
+  switch (CxTYPE(cx)) {
+   case CXt_SUB:
+   case CXt_EVAL:
+   case CXt_FORMAT:
+    --caller;
+    if (caller < 0)
+     goto done;
+    break;
+  }
+  ++level;
+ }
+done:
+ ST(0) = sv_2mortal(newSViv(level));
+ XSRETURN(1);
+
+void
+want_at(...)
+PROTOTYPE: ;$
+PREINIT:
+ I32 cxix = cxstack_ix, level = 0;
+PPCODE:
+ SU_GET_CONTEXT(0, 0);
+ cxix -= level;
+ 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: &;$