in reply to XS: how to manually handle input argument stack?

Look again at perlxstut, perlx, and perlguts. Each describe the ST() macro, where ST(0) represents the first element on the argument stack, ST(1) the second, and so on. The count of how many items are on the stack is held in items, which is available to all XSUBs.

In perlxstut, the "Typemap" example is probably the first exposure to param passing on the stack.

It's probably easiest to think about known XS modules that take variable length param lists, and view the source to get an idea of how they do it. The Inline::C-Cookbook also discusses Inline::C's stack macros, which are really just convenience layers over basic XS concepts. Therefore, the discussion in Inline::C and Inline::C-Cookbook's PODs is useful

All of the "guts" and "XS" documentation is dense and tightly coupled enough that each document relies on your having fully grasped all of the other documents before its useful. One has to read each of them several times, and then still be ready to dive in again when specific questions arise. ;)

Update: You don't have to look much farther than the source for List::Util to find the min() function, which accepts a variable length list of args:

void min(...) PROTOTYPE: @ ALIAS: min = 0 max = 1 CODE: { int index; NV retval; SV *retsv; int magic; if(!items) XSRETURN_UNDEF; retsv = ST(0); magic = SvAMAGIC(retsv); if(!magic) retval = slu_sv_value(retsv); for(index = 1 ; index < items ; index++) { SV *stacksv = ST(index); /* The rest of the code has been omitted, as we've seen the relevant p +art. */

Notice how items already exists. At first the XSUB tests if there are items on the stack, and returns undef if not. Then it grabs the first item at ST(0), and next starts a loop over the remaining items to compare them and find the minimum. items holds the count, and ST(index) refers to individual elements on the stack.

With XS, one of the best ways to learn is to learn from prior art. But you can't take shortcuts. If there's a macro being used in some example code that you don't understand, you had better dig in and research it, because it will be the one that bites you if you blindly cargo-cult it, or that burns you if you obliviously omit it.


Dave

Replies are listed 'Best First'.
Re^2: XS: how to manually handle input argument stack?
by llancet (Friar) on Mar 08, 2014 at 05:36 UTC
    Yes, I do know the macros for perl stack operation. What I want to know is how to declare the argument list in XS function declaration, which will make perl not to croak that you passed incorrect number of arguments.

      Are you looking for (...)? My update that included the example from List::Util probably hit just as you were posting this followup, and for that I apologize. If the List::Util::min code doesn't demonstrate what you're after, let us know.


      Dave

        Yes, that is what I want. I forgot the standard libraries, and just kept searching on Cairo. Thanks for a lot!
Re^2: XS: how to manually handle input argument stack?
by llancet (Friar) on Sep 05, 2014 at 03:03 UTC
    Another question: why there are PROTOTYPE: @? Is that mandatory?