On Aug 14, 2004, at 3:28 AM, Tels wrote:

FastCalc.c: In function `XS_Math__BigInt__FastCalc__is_ten':
FastCalc.c:480: warning: unused variable `class'
FastCalc.c:482: warning: unused variable `RETVAL'

you've used CODE without setting RETVAL. if you're not going to use RETVAL, don't use CODE. see below.



Here is the XS function in question:

SV *
_is_ten(class, x)
  SV*   class
  SV*   x
  INIT:
    AV* a;
    SV* temp;

CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
if ( av_len(a) != 0)
{
ST(0) = &PL_sv_no;
XSRETURN(1); /* len != 1, can't be '10' */
}
temp = *av_fetch(a, 0, 0); /* fetch first element */
ST(0) = boolSV((SvIV(temp) == 10));

if all you want to do is return one item, then CODE, RETVAL, and OUTPUT are the right way to do it.


you could much more easily use int for a bool than an SV.

i don't understand why you want to make the caller pass a reference to a one-element array, like $something = MyClass->_is_ten ([$foo]), but i will presume you have a good reason. ;-)

i would've written this as

# note the lack of a type specification for class.
int
_is_ten(class, SV * x)
    INIT:
        AV* a;
    CODE:
        a = (AV*)SvRV(x);
        if (av_len(a) != 0)
                RETVAL = 0;
        else
                RETVAL = (SvIV(*av_fetch(a, 0, 0)) == 10);
    OUTPUT:
        RETVAL



* "class" is ignored. I know this. Can I somehow flag this?

if you don't specify a type for an argument, code will not be generated to pull it off the stack, and thus you won't have an unused var.



* I read in perldoc perlxs about PPCODE RETVAL etc but I am confused. Which
combination is the proper one? I tried void as return value in combination
with PPCODE and CODE, as well as SV* and assigning to RETVAL. Some
combination (I think the RETVAL one) triggers segfaults straighaway, while
the other (void and PPCODE or CODE) seems (seems!) to work. And the current
one emits a warning. So, which one is the right combination to use in this
situation?

use PPCODE when you need to return more than one element. when using PPCODE, the xsub's return type must be void, and you use EXTEND(), PUSHs(), etc to manipulate the stack.


use CODE when you just need to return one item from the function. the OUTPUT section causes xsubpp to generate code based on the typemap associated with the return type.


* I have some routines which take only one, optional argument, "class".
Unfortunately, XS code generated is always with a required argument,meaning
I cannot call f.i. _one() from Perl, instead it must be _one($dummy). Is
there a way to make arguments optional to XS routines just like for Perl?

supply a default value for the argument.

int
something (SV * class_or_object=NULL)
    ....


--
The door is locked. I tried to open it, but the lock is harder to pick than a broken nose.
-- Sensei, on 'I, Ninja'




Reply via email to