This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / pp_sort.c
index 82913ca..62cafd0 100644 (file)
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -1543,14 +1543,8 @@ PP(pp_sort)
 
            if (is_xsub)
                PL_sortcop = (OP*)cv;
-           else {
+           else
                PL_sortcop = CvSTART(cv);
-               SAVEVPTR(CvROOT(cv)->op_ppaddr);
-               CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
-
-               SAVECOMPPAD();
-               PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
-            }
        }
     }
     else {
@@ -1575,6 +1569,10 @@ PP(pp_sort)
            }
        }
        else {
+           if (SvREADONLY(av))
+               Perl_croak(aTHX_ PL_no_modify);
+           else
+               SvREADONLY_on(av);
            p1 = p2 = AvARRAY(av);
            sorting_av = 1;
        }
@@ -1646,13 +1644,12 @@ PP(pp_sort)
            CATCH_SET(TRUE);
            PUSHSTACKi(PERLSI_SORT);
            if (!hasargs && !is_xsub) {
-               if (PL_sortstash != stash || !PL_firstgv || !PL_secondgv) {
-                   SAVESPTR(PL_firstgv);
-                   SAVESPTR(PL_secondgv);
-                   PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
-                   PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
-                   PL_sortstash = stash;
-               }
+               SAVESPTR(PL_firstgv);
+               SAVESPTR(PL_secondgv);
+               SAVESPTR(PL_sortstash);
+               PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
+               PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
+               PL_sortstash = stash;
 #ifdef USE_5005THREADS
                sv_lock((SV *)PL_firstgv);
                sv_lock((SV *)PL_secondgv);
@@ -1666,25 +1663,41 @@ PP(pp_sort)
                cx->cx_type = CXt_SUB;
                cx->blk_gimme = G_SCALAR;
                PUSHSUB(cx);
-           }
-           PL_sortcxix = cxstack_ix;
+               if (!is_xsub) {
+                   AV* padlist = CvPADLIST(cv);
+
+                   if (++CvDEPTH(cv) >= 2) {
+                       PERL_STACK_OVERFLOW_CHECK();
+                       pad_push(padlist, CvDEPTH(cv), 1);
+                   }
+                   SAVECOMPPAD();
+                   PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
 
-           if (hasargs && !is_xsub) {
-               /* This is mostly copied from pp_entersub */
-               AV *av = (AV*)PAD_SVl(0);
+                   if (hasargs) {
+                       /* This is mostly copied from pp_entersub */
+                       AV *av = (AV*)PAD_SVl(0);
 
 #ifndef USE_5005THREADS
-               cx->blk_sub.savearray = GvAV(PL_defgv);
-               GvAV(PL_defgv) = (AV*)SvREFCNT_inc_simple(av);
+                       cx->blk_sub.savearray = GvAV(PL_defgv);
+                       GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
 #endif /* USE_5005THREADS */
-               CX_CURPAD_SAVE(cx->blk_sub);
-               cx->blk_sub.argarray = av;
+                       CX_CURPAD_SAVE(cx->blk_sub);
+                       cx->blk_sub.argarray = av;
+                   }
+
+               }
            }
+           cx->cx_type |= CXp_MULTICALL;
            
            start = p1 - max;
            sortsvp(aTHX_ start, max,
                    is_xsub ? sortcv_xsub : hasargs ? sortcv_stacked : sortcv);
 
+           if (!(flags & OPf_SPECIAL)) {
+               LEAVESUB(cv);
+               if (!is_xsub)
+                   CvDEPTH(cv)--;
+           }
            POPBLOCK(cx,PL_curpm);
            PL_stack_sp = newsp;
            POPSTACK;
@@ -1713,7 +1726,9 @@ PP(pp_sort)
            }
        }
     }
-    if (av && !sorting_av) {
+    if (sorting_av)
+       SvREADONLY_off(av);
+    else if (av && !sorting_av) {
        /* simulate pp_aassign of tied AV */
        SV** const base = ORIGMARK+1;
        for (i=0; i < max; i++) {