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. 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); } When in doubt, it's sometimes a good idea to look at the c-file that is generated from the xs-file. > >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. 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). The XSUB would have had to be declared returning void in this case. > >* 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. The very same holds true for CODE when not using OUTPUT. Tassilo -- $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval