Fix bug id 20020427.004 on %^H.

Add a regression test for %^H.
Change the sort pragma implementation to use a
global variable instead of %^H.

p4raw-id: //depot/perl@16286
This commit is contained in:
Rafael Garcia-Suarez 2002-04-30 19:03:34 +00:00
parent e8c86ba6ca
commit 045ac3170c
6 changed files with 67 additions and 24 deletions

View File

@ -2291,6 +2291,7 @@ t/comp/colon.t See if colons are parsed correctly
t/comp/cpp.aux main file for cpp.t
t/comp/cpp.t See if C preprocessor works
t/comp/decl.t See if declarations work
t/comp/hints.t See if %^H works
t/comp/multiline.t See if multiline strings work
t/comp/package.t See if packages work
t/comp/proto.t See if function prototypes work

View File

@ -1,8 +1,13 @@
package sort;
our $VERSION = '1.00';
our $VERSION = '1.01';
$sort::hint_bits = 0x00020000; # HINT_LOCALIZE_HH, really...
# Currently the hints for pp_sort are stored in the global variable
# $sort::hints. An improvement would be to store them in $^H{SORT} and have
# this information available somewhere in the listop OP_SORT, to allow lexical
# scoping of this pragma. -- rgs 2002-04-30
our $hints = 0;
$sort::quicksort_bit = 0x00000001;
$sort::mergesort_bit = 0x00000002;
@ -17,18 +22,17 @@ sub import {
require Carp;
Carp::croak("sort pragma requires arguments");
}
$^H |= $sort::hint_bits;
local $_;
no warnings 'uninitialized'; # $^H{SORT} bitops would warn
no warnings 'uninitialized'; # bitops would warn
while ($_ = shift(@_)) {
if (/^_q(?:uick)?sort$/) {
$^H{SORT} &= ~$sort::sort_bits;
$^H{SORT} |= $sort::quicksort_bit;
$hints &= ~$sort::sort_bits;
$hints |= $sort::quicksort_bit;
} elsif ($_ eq '_mergesort') {
$^H{SORT} &= ~$sort::sort_bits;
$^H{SORT} |= $sort::mergesort_bit;
$hints &= ~$sort::sort_bits;
$hints |= $sort::mergesort_bit;
} elsif ($_ eq 'stable') {
$^H{SORT} |= $sort::stable_bit;
$hints |= $sort::stable_bit;
} else {
require Carp;
Carp::croak("sort: unknown subpragma '$_'");
@ -38,10 +42,10 @@ sub import {
sub current {
my @sort;
if ($^H{SORT}) {
push @sort, 'quicksort' if $^H{SORT} & $sort::quicksort_bit;
push @sort, 'mergesort' if $^H{SORT} & $sort::mergesort_bit;
push @sort, 'stable' if $^H{SORT} & $sort::stable_bit;
if ($hints) {
push @sort, 'quicksort' if $hints & $sort::quicksort_bit;
push @sort, 'mergesort' if $hints & $sort::mergesort_bit;
push @sort, 'stable' if $hints & $sort::stable_bit;
}
push @sort, 'mergesort' unless @sort;
join(' ', @sort);

View File

@ -136,9 +136,8 @@ main(0);
# XXX We're using this eval "..." trick to force recompilation,
# to ensure that the correct pragma is enabled when main() is run.
# Currently 'use sort' modifies $^H{SORT} at compile-time, but
# pp_sort() fetches its value at run-time : thus the lexical scoping
# of %^H is of no utility.
# Currently 'use sort' modifies $sort::hints at compile-time, but
# pp_sort() fetches its value at run-time.
# The order of those evals is important.
eval q{

View File

@ -34,10 +34,9 @@ static I32 amagic_cmp_locale(pTHX_ SV *a, SV *b);
#define sv_cmp_static Perl_sv_cmp
#define sv_cmp_locale_static Perl_sv_cmp_locale
#define SORTHINTS(hintsvp) \
((PL_hintgv && \
(hintsvp = hv_fetch(GvHV(PL_hintgv), "SORT", 4, FALSE))) ? \
(I32)SvIV(*hintsvp) : 0)
#define SORTHINTS(hintsv) \
(((hintsv) = GvSV(gv_fetchpv("sort::hints", GV_ADDMULTI, SVt_IV))), \
(SvIOK(hintsv) ? ((I32)SvIV(hintsv)) : 0))
#ifndef SMALLSORT
#define SMALLSORT (200)
@ -1304,9 +1303,9 @@ cmpindir(pTHX_ gptr a, gptr b)
STATIC void
S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp)
{
SV **hintsvp;
SV *hintsv;
if (SORTHINTS(hintsvp) & HINT_SORT_STABLE) {
if (SORTHINTS(hintsv) & HINT_SORT_STABLE) {
register gptr **pp, *q;
register size_t n, j, i;
gptr *small[SMALLSORT], **indir, tmp;
@ -1391,7 +1390,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
{
void (*sortsvp)(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) =
S_mergesortsv;
SV **hintsvp;
SV *hintsv;
I32 hints;
/* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used
@ -1399,7 +1398,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
errors related to picking the correct sort() function, try recompiling
this file without optimiziation. -- A.D. 4/2002.
*/
hints = SORTHINTS(hintsvp);
hints = SORTHINTS(hintsv);
if (hints & HINT_SORT_QUICKSORT) {
sortsvp = S_qsortsv;
}

View File

@ -959,6 +959,10 @@ Perl_leave_scope(pTHX_ I32 base)
PL_op = (OP*)SSPOPPTR;
break;
case SAVEt_HINTS:
if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) {
SvREFCNT_dec((SV*)GvHV(PL_hintgv));
GvHV(PL_hintgv) = NULL;
}
*(I32*)&PL_hints = (I32)SSPOPINT;
break;
case SAVEt_COMPPAD:

36
t/comp/hints.t Normal file
View File

@ -0,0 +1,36 @@
#!./perl -w
BEGIN { print "1..7\n"; }
BEGIN {
print "not " if exists $^H{foo};
print "ok 1 - \$^H{foo} doesn't exist initially\n";
}
{
# simulate a pragma -- don't forget HINT_LOCALIZE_HH
BEGIN { $^H |= 0x00020000; $^H{foo} = "a"; }
BEGIN {
print "not " if $^H{foo} ne "a";
print "ok 2 - \$^H{foo} is now 'a'\n";
}
{
BEGIN { $^H |= 0x00020000; $^H{foo} = "b"; }
BEGIN {
print "not " if $^H{foo} ne "b";
print "ok 3 - \$^H{foo} is now 'b'\n";
}
}
BEGIN {
print "not " if $^H{foo} ne "a";
print "ok 4 - \$H^{foo} restored to 'a'\n";
}
CHECK {
print "not " if exists $^H{foo};
print "ok 6 - \$^H{foo} doesn't exist when compilation complete\n";
}
print "not " if exists $^H{foo};
print "ok 7 - \$^H{foo} doesn't exist at runtime\n";
}
BEGIN {
print "not " if exists $^H{foo};
print "ok 5 - \$^H{foo} doesn't exist while finishing compilation\n";
}