On Wed, May 26, 2004 at 01:35:30PM -0400, muppet wrote:
> 
> Bill Moseley said:
> > I don't know how many values will be pushed onto the stack, so I can't
> > check.  There may be no returned values on the stack, as well.
> 
> then you need to do something like this:
> 
>    /* invoke perl sub */
>    n = call_pv (name, G_ARRAY);
> 
>    /* refresh local copy of stack pointer */
>    SPAGAIN;
> 
>    /* get any and all returned values off of the stack */
>    while (n-- > 0) {
>       SV * sv = POPs;  /* pop last value off of the stack */
>       /* do whatever with that value */
>    }

No, I don't want to do anything with them there.  I just want to return
them to perl.

> what's the "secondary routine"?  the code you called with call_pv()?  in that
> case they returned the values to your XS code, and you are now responsible for
> doing something with them.  are you wanting simply to pass them straight back
> to your caller?  or are you talking about using them in your xsub?

Pass them back to the caller.

So in Perl:

    my @list = Foo->return_list( 'foo', 'bar' );

Where @list contains a list of Foo::Bla objects.

Then in XS:

MODULE = Foo  PACKAGE = Foo

void
return_list( var_a, var_b )
    char *var_a
    char *var_b

    PPCODE:
        PUSHMARK(SP);
        XPUSH( (SV *)var_a );
        XPUSH( (SV *)var_b );
        PUTBACK;
        call_pv("Foo::secondary_function", G_ARRAY );
        SPAGAIN;

void
secondary_function( var_a, var_b )
    SV *var_a
    SV *var_b

    PPCODE:
        LIST long_list = get_long_list( (char *)var_a, (char *)var_b )
        while ( *long_list )
        {
            SV *object = sv_newmortal();
            sv_setref_pv( object, "Foo::Bla", (void *)*long_list );
            XPUSHs( object );
            long_list++;
        }

So "secondary_function" pushes any number of thingies onto the stack and
returns them to perl space.  Like I said, the stack is a mystery to me,
so I'm not clear how perl knows how many Foo::Bla objects are on the
stack and how many to place in @list.

That is, I wonder if I need to check the return value from call_pv (the
number of items pushed on the stack, right?) and then in "return_list"
use that number to tell perl how many items I'm returning.

Sorry, that's not very clear, but hopefully the code makes it clear what
I'm attempting.

BTW -- I also was wondering how best to check for leaks in the Perl/XS
code -- make sure all my objects are destroyed like I expect.

Thanks,

-- 
Bill Moseley
[EMAIL PROTECTED]

Reply via email to