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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.