On Aug 15, 2004, at 1:08 AM, Tassilo von Parseval wrote:

On Sat, Aug 14, 2004 at 05:50:20PM -0400 muppet wrote:

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.

I don't see why that should be so. The main difference between CODE and PPCODE is that the latter will adjust SP for you, whereas CODE leaves it untouched. Furthermore, PPCODE can't be used with an OUTPUT section. However, it's totally ok to have CODE without OUTPUT/RETVAL.

that's true. i misspoke earlier, as void return without OUTPUT is quite okay with CODE (remembered that within seconds of hitting send ;-). but the idiom of XS is to use PPCODE when you're returning more than one value. when people read your xsubs and see CODE they're going to be surprised when they find multiple values returned.



Most of the time, my XSUBs follow this scheme:

    void
    function (arg1, arg2)
        type1 arg1;
        type2 arg2;
    CODE:
    {
        ...
        EXTEND(SP, 3);
        ST(0) = ...
        ST(1) = ...
        ST(2) = ...
        XSRETURN(3);
    }

if you're going to write it that way, why use XS at all? just write the perlapi C code directly.


you could just as easily have written

void function (type1 arg1, type2 arg2)
    PPCODE:
        ...
        EXTEND (SP, 3);
        PUSHs (...);
        PUSHs (...);
        PUSHs (...);


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

For returning one boolean value, I'd always use one of XSRETURN_(YES|NO):

    if (SvIV(temp) == 10)
        XSRETURN_YES;
    XSRETURN_NO;

I find this more convenient than assigning to ST(0) plus doing an XSRETURN(1).

forgive me, but i don't understand why that's preferable to

int
....
        RETVAL = some boolean condition;
   OUTPUT:
        RETVAL

especially when wrapping c library functions returning boolean:

  /* c function */
  typedef int boolean;
  boolean foo();

  TYPEMAP
  boolean   T_BOOL

  boolean foo (class)
      C_ARGS:
        /*void*/

the T_BOOL output typemap uses boolSV(), defined in sv.h as

  #define boolSV(b)  ((b) ? &PL_sv_yes : &PL_sv_no)


which does, i think the same thing as your handwritten output code, but with less typing and more maintainability.



The XSUB would have had to be declared returning void in this case.

declaring the return type as void is what causes xsubpp not to generate a RETVAL variable on the stack, by the way, so to answer your original question, if you don't want "unused variable RETVAL", then don't declare a return type.


but, again, if you're returning one value, i don't understand why you don't want to let xsubpp and typemaps do the work for you.



--
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