From: Gurusamy Sarathy Date: Tue, 14 Jul 1998 08:23:46 +0000 (+0000) Subject: fix function parameter autovivification for pseudohashes X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=74e13ce44e4508bd9e7db3e72533836d36277ca3;p=p5sagit%2Fp5-mst-13.2.git fix function parameter autovivification for pseudohashes p4raw-id: //depot/perl@1489 --- diff --git a/mg.c b/mg.c index 2b96829..09441b3 100644 --- a/mg.c +++ b/mg.c @@ -1340,10 +1340,17 @@ magic_getdefelem(SV *sv, MAGIC *mg) SV *targ = Nullsv; if (LvTARGLEN(sv)) { if (mg->mg_obj) { - HV* hv = (HV*)LvTARG(sv); - HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0); - if (he) - targ = HeVAL(he); + SV *ahv = LvTARG(sv); + if (SvTYPE(ahv) == SVt_PVHV) { + HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0); + if (he) + targ = HeVAL(he); + } + else { + SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0); + if (svp) + targ = *svp; + } } else { AV* av = (AV*)LvTARG(sv); @@ -1390,15 +1397,24 @@ void vivify_defelem(SV *sv) { dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/ - MAGIC* mg; - SV* value; + MAGIC *mg; + SV *value = Nullsv; if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y'))) return; if (mg->mg_obj) { - HV* hv = (HV*)LvTARG(sv); - HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0); - if (!he || (value = HeVAL(he)) == &sv_undef) + SV *ahv = LvTARG(sv); + if (SvTYPE(ahv) == SVt_PVHV) { + HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0); + if (he) + value = HeVAL(he); + } + else { + SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0); + if (svp) + value = *svp; + } + if (!value || value == &sv_undef) croak(no_helem, SvPV(mg->mg_obj, na)); } else { diff --git a/t/op/avhv.t b/t/op/avhv.t index 272d222..55cc992 100755 --- a/t/op/avhv.t +++ b/t/op/avhv.t @@ -17,7 +17,7 @@ sub STORESIZE { $#{$_[0]} = $_[1]+1 } package main; -print "1..10\n"; +print "1..12\n"; $sch = { 'abc' => 1, @@ -96,3 +96,15 @@ print "ok 9\n"; $avhv = [{foo=>1, bar=>2}]; print "not " unless %$avhv =~ m,^\d+/\d+,; print "ok 10\n"; + +# check if defelem magic works +sub f { + print "not " unless $_[0] eq 'a'; + $_[0] = 'b'; + print "ok 11\n"; +} +$a = [{key => 1}, 'a']; +f($a->{key}); +print "not " unless $a->[1] eq 'b'; +print "ok 12\n"; +