From: Vincent Pit Date: Sat, 27 Dec 2008 11:22:07 +0000 (+0100) Subject: Fix an offset of 1 with len magic on scalars X-Git-Tag: v0.27~22 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=d12adc8d9392ea952a40cb7e46df8809256e8b48;p=perl%2Fmodules%2FVariable-Magic.git Fix an offset of 1 with len magic on scalars --- diff --git a/Magic.xs b/Magic.xs index 1b63117..38b27db 100644 --- a/Magic.xs +++ b/Magic.xs @@ -512,12 +512,14 @@ STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) { STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { SV *svr; - I32 len; + I32 len, has_array; U32 ret; dSP; int count; + has_array = SvTYPE(sv) == SVt_PVAV; + ENTER; SAVETMPS; @@ -525,11 +527,11 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { EXTEND(SP, 3); PUSHs(sv_2mortal(newRV_inc(sv))); PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef); - if (SvTYPE(sv) == SVt_PVAV) { + if (has_array) { len = av_len((AV *) sv) + 1; mPUSHi(len); } else { - len = 1; + len = 0; PUSHs(&PL_sv_undef); } PUTBACK; @@ -547,7 +549,7 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { FREETMPS; LEAVE; - return ret - 1; + return has_array ? ret - 1 : ret; } STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) { diff --git a/t/22-len.t b/t/22-len.t index 8ea332c..ec74580 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,12 +3,12 @@ use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 13; use Variable::Magic qw/wizard cast VMG_COMPAT_SCALAR_LENGTH_NOLEN/; my $c = 0; -my $n = int rand 1000; +my $n = 1 + int rand 1000; my $wiz = wizard len => sub { ++$c; return $n }; is($c, 0, 'len : create wizard'); @@ -30,21 +30,26 @@ $b = scalar @a; is($c, 3, 'len : get array length 0'); is($b, 0, 'len : get array length 0 correctly'); -$c = 0; -$n = int rand 1000; -# length magic on scalars needs also get magic to be triggered. -$wiz = wizard get => sub { return 56478 }, - len => sub { ++$c; return $n }; +SKIP: { + skip 'length() no longer calls mg_len magic' => 5 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; -my $x = int rand 1000; + $c = 0; + $n = 1 + int rand 1000; + # length magic on scalars needs also get magic to be triggered. + $wiz = wizard get => sub { return 56478 }, + len => sub { ++$c; return $n }; -SKIP: { - skip 'length() no longer calls mg_len magic', 3 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; + my $x = int rand 1000; cast $x, $wiz; is($c, 0, 'len : cast on scalar'); $b = length $x; - is($c, 1, 'len : get scalar length'); - is($b, $n - 1, 'len : get scalar length correctly'); + is($c, 1, 'len : get scalar length'); + is($b, $n, 'len : get scalar length correctly'); + + $n = 0; + $b = length $x; + is($c, 2, 'len : get scalar length 0'); + is($b, $n, 'len : get scalar length 0 correctly'); }