/* --- Compatibility ------------------------------------------------------- */
+#ifndef PERL_UNUSED_VAR
+# define PERL_UNUSED_VAR(V)
+#endif
+
#ifndef STMT_START
# define STMT_START do
#endif
XSRETURN_UNDEF; \
} STMT_END
+typedef struct {
+ I32 cxix;
+ I32 items;
+} su_ud_unwind;
+
+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 = 0;
+
+ if (cxstack_ix > cxix)
+ dounwind(cxix);
+
+ /* Hide the level */
+ PL_stack_sp--;
+
+ gimme = GIMME_V;
+ if (cxix > 0)
+ mark = cxstack[cxix - 1].blk_oldsp;
+
+ 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_op = PL_ppaddr[OP_RETURN](aTHX);
+ *PL_markstack_ptr = mark;
+
+ fakeop.op_next = PL_op;
+ PL_op = &fakeop;
+
+ Safefree(ud);
+}
+
/* --- XS ------------------------------------------------------------------ */
+XS(XS_Scope__Upper_unwind); /* prototype to pass -Wmissing-prototypes */
+
+XS(XS_Scope__Upper_unwind) {
+#ifdef dVAR
+ dVAR; dXSARGS;
+#else
+ dXSARGS;
+#endif
+ I32 cxix;
+ su_ud_unwind *ud;
+ SV *level;
+ if (!items)
+ Perl_croak(aTHX_ "Usage: Scope::Upper::unwind(..., level)");
+ PERL_UNUSED_VAR(cv); /* -W */
+ PERL_UNUSED_VAR(ax); /* -Wall */
+ level = ST(items - 1);
+ cxix = SvOK(level) ? SvIV(level) : 0;
+ if (cxix < 0)
+ cxix = 0;
+ else if (cxix > cxstack_ix)
+ cxix = cxstack_ix;
+ cxix = cxstack_ix - cxix;
+ do {
+ PERL_CONTEXT *cx = cxstack + cxix;
+ switch (CxTYPE(cx)) {
+ case CXt_SUB:
+ 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);
+ return;
+ default:
+ break;
+ }
+ } while (--cxix >= 0);
+ croak("Can't return outside a subroutine");
+}
+
MODULE = Scope::Upper PACKAGE = Scope::Upper
PROTOTYPES: ENABLE
{
HV *stash = gv_stashpv(__PACKAGE__, 1);
newCONSTSUB(stash, "HERE", newSViv(0));
+ newXSproto("Scope::Upper::unwind", XS_Scope__Upper_unwind, file, NULL);
}
SV *
=back
+=head2 C<unwind @values, $level>
+
+Returns C<@values> I<from> the context indicated by C<$level>, i.e. from the subroutine, eval or format just above C<$level>.
+The upper level isn't coerced onto C<@values>, which is hence always evaluated in list context.
+
=head1 WORDS
=head2 C<TOP>
=head1 EXPORT
-The functions L</reap>, L</localize>, L</localize_elem> and L</localize_delete> are only exported on request, either individually or by the tags C<':funcs'> and C<':all'>.
+The functions L</reap>, L</localize>, L</localize_elem>, L</localize_delete> and L</unwind> are only exported on request, either individually or by the tags C<':funcs'> and C<':all'>.
Same goes for the words L</TOP>, L</HERE>, L</UP>, L</DOWN>, L</SUB> and L</EVAL> that are only exported on request, individually or by the tags C<':words'> and C<':all'>.
our @EXPORT = ();
our %EXPORT_TAGS = (
- funcs => [ qw/reap localize localize_elem localize_delete/ ],
+ funcs => [ qw/reap localize localize_elem localize_delete unwind/ ],
words => [ qw/TOP HERE UP DOWN SUB EVAL/ ],
);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use Test::More tests => 29;
+
+use Scope::Upper qw/unwind SUB/;
+
+my ($res, @res);
+
+# --- Void to void ------------------------------------------------------------
+
+sub {
+ $res = 1;
+ unwind(qw/a b c/ => SUB);
+ $res = 0;
+}->(0);
+ok $res, 'unwind in void context at sub to void';
+
+sub {
+ $res = 1;
+ eval {
+ unwind(qw/d e f/ => SUB);
+ };
+ $res = 0;
+}->(0);
+ok $res, 'unwind in void context at sub across eval to void';
+
+sub {
+ $res = 1;
+ for (1 .. 5) {
+ unwind qw/g h i/ => SUB;
+ }
+ $res = 0;
+}->(0);
+ok $res, 'unwind in void context at sub across loop to void';
+
+# --- Void to scalar ----------------------------------------------------------
+
+$res = sub {
+ unwind(qw/a b c/ => SUB);
+ return 'XXX';
+}->(0);
+is $res, 'c', 'unwind in void context at sub to scalar';
+
+$res = sub {
+ eval {
+ unwind qw/d e f/ => SUB;
+ };
+ return 'XXX';
+}->(0);
+is $res, 'f', 'unwind in void context at sub across eval to scalar';
+
+$res = sub {
+ for (1 .. 5) {
+ unwind qw/g h i/ => SUB;
+ }
+}->(0);
+is $res, 'i', 'unwind in void context at sub across loop to scalar';
+
+$res = sub {
+ for (6, unwind qw/j k l/ => SUB) {
+ $res = 'NO';
+ }
+ return 'XXX';
+}->(0);
+is $res, 'l', 'unwind in void context at sub across loop iterator to scalar';
+
+# --- Void to list ------------------------------------------------------------
+
+@res = sub {
+ unwind qw/a b c/ => SUB;
+ return 'XXX';
+}->(0);
+is_deeply \@res, [ qw/a b c/ ], 'unwind in void context at sub to list';
+
+@res = sub {
+ eval {
+ unwind qw/d e f/ => SUB;
+ };
+ return 'XXX';
+}->(0);
+is_deeply \@res, [ qw/d e f/ ], 'unwind in void context at sub across eval to list';
+
+@res = sub {
+ for (1 .. 5) {
+ unwind qw/g h i/ => SUB;
+ }
+}->(0);
+is_deeply \@res, [ qw/g h i/ ], 'unwind in void context at sub across loop to list';
+
+# --- Scalar to void ----------------------------------------------------------
+
+sub {
+ $res = 1;
+ my $temp = unwind(qw/a b c/ => SUB);
+ $res = 0;
+}->(0);
+ok $res, 'unwind in scalar context at sub to void';
+
+sub {
+ $res = 1;
+ my $temp = eval {
+ unwind(qw/d e f/ => SUB);
+ };
+ $res = 0;
+}->(0);
+ok $res, 'unwind in scalar context at sub across eval to void';
+
+sub {
+ $res = 1;
+ for (1 .. 5) {
+ my $temp = (unwind qw/g h i/ => SUB);
+ }
+ $res = 0;
+}->(0);
+ok $res, 'unwind in scalar context at sub across loop to void';
+
+sub {
+ $res = 1;
+ if (unwind qw/m n o/ => SUB) {
+ $res = undef;
+ }
+ $res = 0;
+}->(0);
+ok $res, 'unwind in scalar context at sub across test to void';
+
+# --- Scalar to scalar --------------------------------------------------------
+
+$res = sub {
+ 1, unwind(qw/a b c/ => SUB);
+}->(0);
+is $res, 'c', 'unwind in scalar context at sub to scalar';
+
+$res = sub {
+ eval {
+ 8, unwind qw/d e f/ => SUB;
+ };
+}->(0);
+is $res, 'f', 'unwind in scalar context at sub across eval to scalar';
+
+$res = sub {
+ if (unwind qw/m n o/ => SUB) {
+ return 'XXX';
+ }
+}->(0);
+is $res, 'o', 'unwind in scalar context at sub across test to scalar';
+
+# --- Scalar to list ----------------------------------------------------------
+
+@res = sub {
+ if (unwind qw/m n o/ => SUB) {
+ return 'XXX';
+ }
+}->(0);
+is_deeply \@res, [ qw/m n o/ ], 'unwind in scalar context at sub across test to list';
+
+# --- List to void ------------------------------------------------------------
+
+sub {
+ $res = 1;
+ my @temp = unwind(qw/a b c/ => SUB);
+ $res = 0;
+}->(0);
+ok $res, 'unwind in list context at sub to void';
+
+sub {
+ $res = 1;
+ my @temp = eval {
+ unwind(qw/d e f/ => SUB);
+ };
+ $res = 0;
+}->(0);
+ok $res, 'unwind in list context at sub across eval to void';
+
+sub {
+ $res = 1;
+ for (1 .. 5) {
+ my @temp = (unwind qw/g h i/ => SUB);
+ }
+ $res = 0;
+}->(0);
+ok $res, 'unwind in list context at sub across loop to void';
+
+sub {
+ $res = 1;
+ for (6, unwind qw/j k l/ => SUB) {
+ $res = undef;
+ }
+ $res = 0;
+}->(0);
+ok $res, 'unwind in list context at sub across test to void';
+
+# --- List to scalar ----------------------------------------------------------
+
+$res = sub {
+ my @temp = (1, unwind(qw/a b c/ => SUB));
+ return 'XXX';
+}->(0);
+is $res, 'c', 'unwind in list context at sub to scalar';
+
+$res = sub {
+ my @temp = eval {
+ 8, unwind qw/d e f/ => SUB;
+ };
+ return 'XXX';
+}->(0);
+is $res, 'f', 'unwind in list context at sub across eval to scalar';
+
+$res = sub {
+ for (1 .. 5) {
+ my @temp = (7, unwind qw/g h i/ => SUB);
+ }
+ return 'XXX';
+}->(0);
+is $res, 'i', 'unwind in list context at sub across loop to scalar';
+
+$res = sub {
+ for (6, unwind qw/j k l/ => SUB) {
+ return 'XXX';
+ }
+}->(0);
+is $res, 'l', 'unwind in list context at sub across loop iterator to scalar';
+
+# --- List to list ------------------------------------------------------------
+
+@res = sub {
+ 2, unwind qw/a b c/ => SUB;
+}->(0);
+is_deeply \@res, [ qw/a b c/ ], 'unwind in list context at sub to list';
+
+@res = sub {
+ eval {
+ 8, unwind qw/d e f/ => SUB;
+ };
+}->(0);
+is_deeply \@res, [ qw/d e f/ ], 'unwind in list context at sub across eval to list';
+
+@res = sub {
+ for (6, unwind qw/j k l/ => SUB) {
+ return 'XXX';
+ }
+}->(0);
+is_deeply \@res, [ qw/j k l/ ], 'unwind in list context at sub across loop iterator to list';