In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/db9306af4ddf47b54fb142041f8f950b1ec18f08?hp=f91dcd139f8093525e1a00c3803de98b1bf89cab>
- Log ----------------------------------------------------------------- commit db9306af4ddf47b54fb142041f8f950b1ec18f08 Author: Father Chrysostomos <[email protected]> Date: Thu Nov 29 09:08:08 2012 -0800 Clear method caches when unwinding local *foo=sub{} local *foo=sub{} is done in two stages: â¢Â First the local *foo localises the GP (the glob pointer, or list of slots), setting a flag on the GV. ⢠Then scalar assignment sees the flag on the GV on the LHS and loca- lises a single slot. The slot localisation only stores on the savestack a pointer into the GP struct and the old value. There is no reference to the GV. To restore a method properly, we have to have a reference to the GV when the slot localisation is undone. So in this commit I have added a new save type, SAVEt_GVSLOT. It is like SAVEt_GENERIC_SV, except it pushes the GV as well. Currently it is used only for CVs, but I will need it for HVs and maybe AVs as well. It is possible for the unwinding of the slot localisation to affect only a GV other than the one that is pushed, if glob assignments have taken place since the local *foo. So we have to check whether the pointer is inside the GP and use PL_sub_generation++ if it is not. M scope.c M scope.h M sv.c M t/mro/method_caching.t commit af5aa9e378e68e5202ada6a61d269e74cc9fe298 Author: Father Chrysostomos <[email protected]> Date: Wed Nov 28 18:05:28 2012 -0800 method_caching.t: Load test.pl at BEGIN time This stops $::TODO from producing a used-only-once warning. M t/mro/method_caching.t commit 4e52a9b69e39f85d6a5d7ac598c61ff0b00f94ee Author: Father Chrysostomos <[email protected]> Date: Wed Nov 28 18:04:01 2012 -0800 Clear method caches when unwinding local *foo=*method It was already working for those cases where *foo contained a sub before and after localisation. For those cases where *foo had no sub but localised assignment gave it one, method caches were not being reset on scope exit. case SAVEt_GP in scope.c:leave_scope needs to look at both GPs (glob pointer, or list of glob slots), both from before and after the unlo- calisation. If either has a sub, method caches need to be cleared. This does not yet fix local *foo = sub {}, but I added a to-do test for it. (This is more complicated, as localisation happens in two seperate steps, the glob slot localisation storing no pointers to the glob itself on the savestack.) M scope.c M t/mro/method_caching.t commit 3d460042b1251a4b5e3b583fa6be358554dd3bcc Author: Father Chrysostomos <[email protected]> Date: Wed Nov 28 13:46:07 2012 -0800 Fix two local *ISA bugs These are regressions from 5.8. local *ISA was not updating isa caches. local *ISA = [] was updating caches, but scope unwinding was not. Both save_gp and leave_scope/SAVEt_GP need to check whether the glob is named ISA and call mro_isa_changed_in if appropriate. M scope.c M t/mro/basic.t commit 959f7ad7c12f768f896b4dd48b5b7f41b6b087b5 Author: Father Chrysostomos <[email protected]> Date: Wed Nov 28 12:39:04 2012 -0800 Donât croak for local *DetachedStash::method save_gp was trying to call mro_method_changed_in even if the stash had been detached. This is a regression from 5.12. M scope.c M t/mro/basic.t commit 978a498e17ec54b6f1fc65f3375a62a68f321f99 Author: Father Chrysostomos <[email protected]> Date: Wed Nov 28 08:36:34 2012 -0800 Reset method caches when GPs are shared The new MRO stuff in 5.10 made PL_sub_generation++ mostly unnecessary, and almost all uses of it were replaced with mro_method_changed_in. There is only one problem: That doesnât actually work properly. After glob-to-glob assignment (*foo = *bar), both globs share the same GP (glob pointer, or list of glob slots). But there is no list of GVs associated with any GP. So there is no way, given a GV whose GP is shared, to find out what other classes might need their method caches reset. sub B::b { "b" } *A::b = *B::b; @C::ISA = "A"; print C->b, "\n"; # should print "b" eval 'sub B::b { "c" }'; print C->b, "\n"; # should print "c" __END__ $ perl5.8.9 foo b c $ perl5.10.0 foo b b And it continues up to 5.16.x. If a GP is shared, then those places where mro_method_changed_in is called after the GP has been modified must do PL_sub_generation++ instead if the GP is shared, which can be detected through its refer- ence count. M gv.h M op.c M scope.c M sv.c M t/mro/method_caching.t commit 6e1b2de7c59099adaa1866fe94957b98fabcd9c4 Author: Father Chrysostomos <[email protected]> Date: Sun Nov 25 22:15:33 2012 -0800 Fix two minor bugs with local glob assignment These are combined into one patch because it is hard to fix one with- out fixing the other. local *glob = $ref was ignoring the clobbered reference and not accounting for when updating ISA caches, resulting in two bugs: *Foo::ISA = *Bar::ISA; @Foo::ISA = "Baz"; sub Baz::ook { "Baz" } sub L::ook { "See" } warn Bar->ook; # Baz local *Foo::ISA = ["L"]; warn Bar->ook; # Baz @Baz::ISA = @Baz::ISA; # should have no effect warn Bar->ook; # See @Baz::ISA = "Foo::bar"; sub Foo::bar::ber { 'baz' } sub UNIVERSAL::ber { "black sheep" } warn Baz->ber; # baz local *Foo:: = \%Bar::; warn Baz->ber; # baz @Baz::ISA = @Baz::ISA; # should have no effect warn Baz->ber; # black sheep The dref variable in sv.c:S_glob_assign_ref holds the SV that needs to be freed. So during localisation it is NULL. When I was fixing up isa and mro bugs in perl 5.14, I misunderstood its purpose and thought it always contained the reference on the left. Since we need to have access to what was assigned over after the assignment, this commit changes dref always to hold the clobbered SV, and makes the SvREFCNT_dec conditional. M sv.c M t/mro/isa_aliases.t M t/mro/package_aliases.t commit b01425c33956a0b279a0f20ced4131027f2c40f4 Author: Father Chrysostomos <[email protected]> Date: Sun Nov 25 17:16:37 2012 -0800 scope.c:save_gp: Remove redundant code This has been redundant since ERRSV was changed to use GvSVn in com- mit f5fa9033b8. M scope.c ----------------------------------------------------------------------- Summary of changes: gv.h | 7 +++++ op.c | 6 ++-- scope.c | 57 +++++++++++++++++++++++++++++++++------------- scope.h | 1 + sv.c | 37 ++++++++++++++++++++++++----- t/mro/basic.t | 26 ++++++++++++++++++++- t/mro/isa_aliases.t | 10 +++++++- t/mro/method_caching.t | 44 ++++++++++++++++++++++++++++++++++- t/mro/package_aliases.t | 12 ++++++++- 9 files changed, 168 insertions(+), 32 deletions(-) diff --git a/gv.h b/gv.h index 8e093400..1e17f35 100644 --- a/gv.h +++ b/gv.h @@ -264,6 +264,13 @@ Return the CV from the GV. #define gv_autoload4(stash, name, len, method) \ gv_autoload_pvn(stash, name, len, !!(method)) #define newGVgen(pack) newGVgen_flags(pack, 0) +#define gv_method_changed(gv) \ + ( \ + assert_(isGV_with_GP(gv)) \ + GvREFCNT(gv) > 1 \ + ? (void)++PL_sub_generation \ + : mro_method_changed_in(GvSTASH(gv)) \ + ) #define gv_AVadd(gv) gv_add_by_type((gv), SVt_PVAV) #define gv_HVadd(gv) gv_add_by_type((gv), SVt_PVHV) diff --git a/op.c b/op.c index 0bc9021..c95c8ea 100644 --- a/op.c +++ b/op.c @@ -7548,7 +7548,7 @@ Perl_newATTRSUB_flags(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, GvCVGEN(gv) = 0; if (HvENAME_HEK(GvSTASH(gv))) /* sub Foo::bar { (shift)+1 } */ - mro_method_changed_in(GvSTASH(gv)); + gv_method_changed(gv); } } if (!CvGV(cv)) { @@ -7872,7 +7872,7 @@ Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len, GvCV_set(gv,cv); GvCVGEN(gv) = 0; if (HvENAME_HEK(GvSTASH(gv))) - mro_method_changed_in(GvSTASH(gv)); /* newXS */ + gv_method_changed(gv); /* newXS */ } } if (!name) @@ -7906,7 +7906,7 @@ Perl_newSTUB(pTHX_ GV *gv, bool fake) GvCV_set(gv, cv); GvCVGEN(gv) = 0; if (!fake && HvENAME_HEK(GvSTASH(gv))) - mro_method_changed_in(GvSTASH(gv)); + gv_method_changed(gv); CvGV_set(cv, gv); CvFILE_set_from_cop(cv, PL_curcop); CvSTASH_set(cv, PL_curstash); diff --git a/scope.c b/scope.c index f87f7ce..3d50932 100644 --- a/scope.c +++ b/scope.c @@ -285,22 +285,22 @@ Perl_save_gp(pTHX_ GV *gv, I32 empty) if (empty) { GP *gp = Perl_newGP(aTHX_ gv); - - if (GvCVu(gv)) - mro_method_changed_in(GvSTASH(gv)); /* taking a method out of circulation ("local")*/ + HV * const stash = GvSTASH(gv); + bool isa_changed = 0; + + if (stash && HvENAME(stash)) { + if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3)) + isa_changed = TRUE; + else if (GvCVu(gv)) + /* taking a method out of circulation ("local")*/ + mro_method_changed_in(stash); + } if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) { gp->gp_io = newIO(); IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START; } -#ifdef PERL_DONT_CREATE_GVSV - if (gv == PL_errgv) { - /* We could scatter this logic everywhere by changing the - definition of ERRSV from GvSV() to GvSVn(), but it seems more - efficient to do this check once here. */ - gp->gp_sv = newSV(0); - } -#endif GvGP_set(gv,gp); + if (isa_changed) mro_isa_changed_in(stash); } else { gp_ref(GvGP(gv)); @@ -783,6 +783,23 @@ Perl_leave_scope(pTHX_ I32 base) SvREFCNT_dec(sv); SvREFCNT_dec(value); break; + case SAVEt_GVSLOT: /* any slot in GV */ + value = MUTABLE_SV(SSPOPPTR); + ptr = SSPOPPTR; + gv = MUTABLE_GV(SSPOPPTR); + hv = GvSTASH(gv); + if (hv && HvENAME(hv) && ( + (value && SvTYPE(value) == SVt_PVCV) + || (*(SV **)ptr && SvTYPE(*(SV**)ptr) == SVt_PVCV) + )) + { + if ((char *)ptr < (char *)GvGP(gv) + || (char *)ptr > (char *)GvGP(gv) + sizeof(struct gp) + || GvREFCNT(gv) > 1) + PL_sub_generation++; + else mro_method_changed_in(hv); + } + goto restore_svp; case SAVEt_AV: /* array reference */ av = MUTABLE_AV(SSPOPPTR); gv = MUTABLE_GV(SSPOPPTR); @@ -865,11 +882,19 @@ Perl_leave_scope(pTHX_ I32 base) case SAVEt_GP: /* scalar reference */ ptr = SSPOPPTR; gv = MUTABLE_GV(SSPOPPTR); - gp_free(gv); - GvGP_set(gv, (GP*)ptr); - /* putting a method back into circulation ("local")*/ - if (GvCVu(gv) && (hv=GvSTASH(gv)) && HvENAME_get(hv)) - mro_method_changed_in(hv); + { + /* possibly taking a method out of circulation */ + const bool had_method = !!GvCVu(gv); + gp_free(gv); + GvGP_set(gv, (GP*)ptr); + if ((hv=GvSTASH(gv)) && HvENAME_get(hv)) { + if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3)) + mro_isa_changed_in(hv); + else if (had_method || GvCVu(gv)) + /* putting a method back into circulation ("local")*/ + gv_method_changed(gv); + } + } SvREFCNT_dec(gv); break; case SAVEt_FREESV: diff --git a/scope.h b/scope.h index 4373eac..f1d1929 100644 --- a/scope.h +++ b/scope.h @@ -59,6 +59,7 @@ #define SAVEt_GVSV 49 #define SAVEt_FREECOPHH 50 #define SAVEt_CLEARPADRANGE 51 +#define SAVEt_GVSLOT 52 #define SAVEf_SETMAGIC 1 #define SAVEf_KEEPOLDELEM 2 diff --git a/sv.c b/sv.c index d8d0ff8..35d295e 100644 --- a/sv.c +++ b/sv.c @@ -3741,7 +3741,7 @@ static void S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) { SV * const sref = SvRV(sstr); - SV *dref = NULL; + SV *dref; const int intro = GvINTRO(dstr); SV **location; U8 import_flag = 0; @@ -3787,10 +3787,25 @@ S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) GvCVGEN(dstr) = 0; /* Switch off cacheness. */ } } - SAVEGENERICSV(*location); - } - else - dref = *location; + /* SAVEt_GVSLOT takes more room on the savestack and has more + overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs + leave_scope needs access to the GV so it can reset method + caches. We must use SAVEt_GVSLOT whenever the type is + SVt_PVCV, even if the stash is anonymous, as the stash may + gain a name somehow before leave_scope. */ + if (stype == SVt_PVCV) { + /* There is no save_pushptrptrptr. Creating it for this + one call site would be overkill. So inline the ss push + routines here. */ + SSCHECK(4); + SSPUSHPTR(dstr); + SSPUSHPTR(location); + SSPUSHPTR(SvREFCNT_inc(*location)); + SSPUSHUV(SAVEt_GVSLOT); + } + else SAVEGENERICSV(*location); + } + dref = *location; if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) { CV* const cv = MUTABLE_CV(*location); if (cv) { @@ -3824,7 +3839,7 @@ S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) } GvCVGEN(dstr) = 0; /* Switch off cacheness. */ GvASSUMECV_on(dstr); - if(GvSTASH(dstr)) mro_method_changed_in(GvSTASH(dstr)); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */ + if(GvSTASH(dstr)) gv_method_changed(dstr); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */ } *location = SvREFCNT_inc_simple_NN(sref); if (import_flag && !(GvFLAGS(dstr) & import_flag) @@ -3907,7 +3922,7 @@ S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) } break; } - SvREFCNT_dec(dref); + if (!intro) SvREFCNT_dec(dref); if (SvTAINTED(sstr)) SvTAINT(dstr); return; @@ -12611,6 +12626,14 @@ Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param) ptr = POPPTR(ss,ix); TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ break; + case SAVEt_GVSLOT: /* any slot in GV */ + sv = (const SV *)POPPTR(ss,ix); + TOPPTR(nss,ix) = sv_dup_inc(sv, param); + ptr = POPPTR(ss,ix); + TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ + sv = (const SV *)POPPTR(ss,ix); + TOPPTR(nss,ix) = sv_dup_inc(sv, param); + break; case SAVEt_HV: /* hash reference */ case SAVEt_AV: /* array reference */ sv = (const SV *) POPPTR(ss,ix); diff --git a/t/mro/basic.t b/t/mro/basic.t index 10aa44c..ab34fc2 100644 --- a/t/mro/basic.t +++ b/t/mro/basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -BEGIN { require q(./test.pl); } plan(tests => 54); +BEGIN { require q(./test.pl); } plan(tests => 59); require mro; @@ -346,3 +346,27 @@ is(eval { MRO_N->testfunc() }, 123); @{*fednu::ISA} = "pyfg"; ok +fednu->isa("pyfg"), 'autovivifying @ISA via *{@ISA}'; } + +{ + sub Detached::method; + my $h = delete $::{"Detached::"}; + eval { local *Detached::method }; + is $@, "", 'localising gv-with-cv belonging to detached package'; +} + +{ + # *ISA localisation + @il::ISA = "ilsuper"; + sub ilsuper::can { "puree" } + sub il::tomatoes; + { + local *il::ISA; + is +il->can("tomatoes"), \&il::tomatoes, 'local *ISA'; + } + is "il"->can("tomatoes"), "puree", 'local *ISA unwinding'; + { + local *il::ISA = []; + is +il->can("tomatoes"), \&il::tomatoes, 'local *ISA = []'; + } + is "il"->can("tomatoes"), "puree", 'local *ISA=[] unwinding'; +} diff --git a/t/mro/isa_aliases.t b/t/mro/isa_aliases.t index abdedce..2073e5e 100644 --- a/t/mro/isa_aliases.t +++ b/t/mro/isa_aliases.t @@ -2,7 +2,7 @@ BEGIN { chdir 't'; @INC = '../lib'; require './test.pl' } -plan 12; +plan 13; @Foogh::ISA = "Bar"; *Phoogh::ISA = *Foogh::ISA; @@ -41,3 +41,11 @@ ok !Foo->isa("Bar"), '!isa when another stash has claimed the @ISA via ref-to-glob assignment'; ok !Phoo->isa("Bar"), '!isa on the stash that claimed the @ISA via ref-to-glob assignment'; + +*Fooo::ISA = *Baro::ISA; +@Fooo::ISA = "Bazo"; +sub Bazo::ook { "Baz" } +sub L::ook { "See" } +Baro->ook; +local *Fooo::ISA = ["L"]; +is 'Baro'->ook, 'See', 'localised *ISA=$ref assignment'; diff --git a/t/mro/method_caching.t b/t/mro/method_caching.t index 733193a..495e12f 100644 --- a/t/mro/method_caching.t +++ b/t/mro/method_caching.t @@ -1,6 +1,7 @@ #!./perl use strict; +no strict 'refs'; # we do a lot of this use warnings; no warnings 'redefine'; # we do a lot of this no warnings 'prototype'; # we do a lot of this @@ -10,10 +11,9 @@ BEGIN { chdir 't' if -d 't'; @INC = '../lib'; } + require './test.pl'; } -require './test.pl'; - { package MCTest::Base; sub foo { return $_[1]+1 }; @@ -35,6 +35,15 @@ my @testsubs = ( sub { is(MCTest::Derived->foo(0), 5); }, sub { sub FFF { $_[1]+7 }; local *MCTest::Base::foo = *FFF; is(MCTest::Derived->foo(0), 7); }, sub { is(MCTest::Derived->foo(0), 5); }, + sub { { local *MCTest::Base::can = sub { "tomatoes" }; + MCTest::Derived->can(0); } + is(MCTest::Derived->can("isa"), \&UNIVERSAL::isa, + 'removing method when unwinding local *method=sub{}'); }, + sub { sub peas { "peas" } + { local *MCTest::Base::can = *peas; + MCTest::Derived->can(0); } + is(MCTest::Derived->can("isa"), \&UNIVERSAL::isa, + 'removing method when unwinding local *method=*other'); }, sub { sub DDD { $_[1]+8 }; *MCTest::Base::foo = *DDD; is(MCTest::Derived->foo(0), 8); }, sub { *ASDF::asdf = sub { $_[1]+9 }; *MCTest::Base::foo = \&ASDF::asdf; is(MCTest::Derived->foo(0), 9); }, sub { undef *MCTest::Base::foo; eval { MCTest::Derived->foo(0) }; like($@, qr/locate object method/); }, @@ -57,6 +66,37 @@ my @testsubs = ( sub { *{MCTest::Base::} = *{Foo::}; eval { MCTest::Derived->foo(0) }; like($@, qr/locate object method/); }, sub { *MCTest::Derived::foo = \&MCTest::Base::foo; eval { MCTest::Derived::foo(0,0) }; ok(!$@); undef *MCTest::Derived::foo }, sub { eval 'package MCTest::Base; sub foo { $_[1]+18 }'; is(MCTest::Derived->foo(0), 18); }, + + # Redefining through a glob alias + sub { *A = *{'MCTest::Base::foo'}; eval 'sub A { $_[1]+19 }'; + is(MCTest::Derived->foo(0), 19, + 'redefining sub through glob alias via decl'); }, + sub { SKIP: { + skip_if_miniperl("no XS"); require XS::APItest; + *A = *{'MCTest::Base::foo'}; + XS::APItest::newCONSTSUB(\%main::, "A", 0, 20); + is (MCTest::Derived->foo(0), 20, + 'redefining sub through glob alias via newXS'); + } }, + sub { undef *{'MCTest::Base::foo'}; *A = *{'MCTest::Base::foo'}; + eval { no warnings 'once'; local *UNIVERSAL::foo = sub {96}; + MCTest::Derived->foo }; + ()=\&A; + eval { MCTest::Derived->foo }; + like($@, qr/Undefined subroutine/, + 'redefining sub through glob alias via stub vivification'); }, + sub { *A = *{'MCTest::Base::foo'}; + local *A = sub { 21 }; + is(MCTest::Derived->foo, 21, + 'redef sub through glob alias via local cv-to-glob assign'); }, + sub { *A = *{'MCTest::Base::foo'}; + eval 'sub MCTest::Base::foo { 22 }'; + { local *A = sub { 23 }; MCTest::Derived->foo } + is(MCTest::Derived->foo, 22, + 'redef sub through glob alias via localisation unwinding'); }, + sub { *A = *{'MCTest::Base::foo'}; *A = sub { 24 }; + is(MCTest::Derived->foo(0), 24, + 'redefining sub through glob alias via cv-to-glob assign'); }, ); plan(tests => scalar(@testsubs)); diff --git a/t/mro/package_aliases.t b/t/mro/package_aliases.t index 3bc3c8f..34aa2d6 100644 --- a/t/mro/package_aliases.t +++ b/t/mro/package_aliases.t @@ -10,7 +10,7 @@ BEGIN { use strict; use warnings; -plan(tests => 52); +plan(tests => 53); { package New; @@ -399,4 +399,12 @@ bless [], "O:"; 'isa(foo) when inheriting from "class:" after string-to-glob assignment'; } - +@Bazo::ISA = "Fooo::bar"; +sub Fooo::bar::ber { 'baz' } +sub UNIVERSAL::ber { "black sheep" } +Bazo->ber; +local *Fooo:: = \%Baro::; +{ + no warnings; + is 'Bazo'->ber, 'black sheep', 'localised *glob=$stashref assignment'; +} -- Perl5 Master Repository
