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
In reply to Re: XS: how to manually handle input argument stack?
by davido
in thread XS: how to manually handle input argument stack?
by llancet
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |