Sergey Skvortsov <[EMAIL PROTECTED]> writes:
>Hi.
>
>I can't fully understand why there is need to mark values returned from 
>XS-method as "mortal" if they are referenced (owned) by object itself.

There is no such need.
Marking things as mortal just arranges for an SvREFCNT_dec()
on the FREETMPS that is paired with SAVETMPS inside a ENTER/LEAVE 
pair. 

>void
>get_xs3(self)
>       SV *self;
>    PREINIT:
>       SV **pSV;
>    PPCODE:
>       pSV = av_fetch((AV*)SvRV(self), 0, 0);
>
>       ST(0) = *pSV;
>       sv_dump( ST(0) );
>       XSRETURN(1);
>
>my $arr = Getter->new(222);
>warn "\n==> XS3\n";    my $v3 = $arr->get_xs3;         Dump($v3);
>
>==> XS3
>SV = IV(0x1ab70b0) at 0x15d5c64
>   REFCNT = 1
>   FLAGS = (IOK,pIOK)
>   IV = 222
>SV = IV(0x1ab70bc) at 0x1aef620
>   REFCNT = 1
>   FLAGS = (PADBUSY,PADMY,IOK,pIOK)
>   IV = 222
>
>So we see that returned value is _always_ double-copied.
>It's marvellous.

Well it has to be copied because $v3 has its OWN sv in the 'pad'.
You can only alias SVs of variables using glob hackery
which means they aren't lexicals

  our $v4;
  *v4 = \$arr->[0];

>
>Just two questions:
>1. What is _exact_ XS-equivalent of "sub { $_[0]->[0];}" ?

There are several ways to write it but in my style it would 
be something like:

void
get_xs4(SV *self)
CODE:
 {
  if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) {
     croak("Not an ARRAY reference:%_",self);
  }
  else  {
    SV **pSV = av_fetch((AV*)SvRV(self), 0, 0);
    if (pSV) {
        ST(0) = *pSV;
        sv_dump( ST(0) );
        XSRETURN(1);
    }
    else {
        XSRETURN_UNDEF;
    } 
  }
}


>2. Is it safe use get_xs3()?

Not if there are holes in your array.

Reply via email to