Skip to content

Commit d8dcfe7

Browse files
committed
perlxs.pod: update default, length, ellipis params
Rewrite (and retitle) these three subsections: =head3 Default Parameter Values =head3 The C<length(NAME)> Keyword =head3 Variable-length Parameter Lists
1 parent 9717f25 commit d8dcfe7

File tree

2 files changed

+145
-64
lines changed

2 files changed

+145
-64
lines changed

XSUB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ must be called prior to setup the C<MARK> variable.
4444
4545
=for apidoc Amn|Stack_off_t|items
4646
Variable which is setup by C<xsubpp> to indicate the number of
47-
items on the stack. See L<perlxs/"Variable-length Parameter Lists">.
47+
items on the stack. See L<perlxs/"Ellipsis: variable-length parameter lists">.
4848
4949
=for apidoc Amn|I32|ix
5050
Variable which is setup by C<xsubpp> to indicate which of an

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 144 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,93 +2350,174 @@ which could be called from perl as:
23502350

23512351
=head3 Default Parameter Values
23522352

2353-
Default values for XSUB arguments can be specified by placing an
2354-
assignment statement in the parameter list. The default value may
2355-
be a number, a string or the special string C<NO_INIT>. Defaults should
2356-
always be used on the right-most parameters only.
2353+
int
2354+
foo(int i, char *s = "abc")
23572355

2358-
To allow the XSUB for rpcb_gettime() to have a default host
2359-
value the parameters to the XSUB could be rearranged. The
2360-
XSUB will then call the real rpcb_gettime() function with
2361-
the parameters in the correct order. This XSUB can be called
2362-
from Perl with either of the following statements:
2356+
int
2357+
bar(int i, int j = i + ')', char *s = "abc,)")
23632358

2364-
$status = rpcb_gettime($timep, $host);
2359+
int
2360+
baz(int i, char *s = NO_INIT)
2361+
2362+
Optional parameters can be indicated by appending C<= C_expression> to the
2363+
parameter declaration. The C expression will be evaluated if not enough
2364+
arguments are supplied. Parameters with default values should come after
2365+
any mandatory parameters (although this is currently not enforced by the
2366+
XS compiler). The value can be any valid compile-time or run-time C
2367+
expression (but see below), including the values of any parameters
2368+
declared to its left. The special value C<NO_INIT> indicates that the
2369+
parameter is kept uninitialised if there isn't a corresponding argument.
2370+
2371+
The XS parser's handling of default expressions is rather simplistic. It
2372+
just wants to extract parameter declarations (including any optional
2373+
trailing default value) from a comma-separated list, but it doesn't
2374+
understand C syntax. It can handle commas and closing parentheses within a
2375+
quoted string, but currently not an escaped quote such as C<'\''> or
2376+
C<"\"">. Neither can it handle balanced parentheses such as C<int j =
2377+
(i+1)>.
2378+
2379+
Due to an implementation flaw, default value expressions are currently
2380+
evalled in double-quoted context during parsing, in a similar fashion to
2381+
typemap templates. So for example C<char *s = "$arg"> is expanded to
2382+
C<char *s = "ST(0)"> or similar. This behaviour may be fixed at some
2383+
point; in the meantime, it is best to avoid the C<$> and C<@> characters
2384+
within default value expressions.
2385+
2386+
=head3 The C<length(param_name)> pseudo-parameter
23652387

2366-
$status = rpcb_gettime($timep);
2388+
int
2389+
foo(char *s, int length(s))
23672390

2368-
The XSUB will look like the code which follows. A CODE:
2369-
block is used to call the real rpcb_gettime() function with
2370-
the parameters in the correct order for that function.
2391+
It is common for a C function to take a string pointer and length as two
2392+
arguments, while in Perl, string-valued SVs combine both the string and
2393+
length in a single value. To simplify generating the autocall code in such
2394+
situations, the C<length(foo)> pseudo-parameter acts as the length of the
2395+
parameter C<foo>. It doesn't consume an argument or appear in the XSUB's
2396+
usage message, but it I<is> passed to the autocalled C function. For
2397+
example, this XS:
23712398

2372-
bool_t
2373-
rpcb_gettime(timep, host="localhost")
2374-
char *host
2375-
time_t timep = NO_INIT
2376-
CODE:
2377-
RETVAL = rpcb_gettime(host, &timep);
2378-
OUTPUT:
2379-
timep
2380-
RETVAL
2399+
void
2400+
foo(char *s, short length(s), int t)
23812401

2382-
=head3 The C<length(NAME)> Keyword
2402+
translates to something similar to this C code:
23832403

2384-
If one of the input arguments to the C function is the length of a string
2385-
argument C<NAME>, one can substitute the name of the length-argument by
2386-
C<length(NAME)> in the XSUB declaration. This argument must be omitted when
2387-
the generated Perl function is called. E.g.,
2404+
if (items != 2)
2405+
croak_xs_usage(cv, "s, t");
23882406

2389-
void
2390-
dump_chars(char *s, short l)
23912407
{
2392-
short n = 0;
2393-
while (n < l) {
2394-
printf("s[%d] = \"\\%#03o\"\n", n, (int)s[n]);
2395-
n++;
2396-
}
2408+
STRLEN STRLEN_length_of_s;
2409+
short XSauto_length_of_s;
2410+
char * s = (char *)SvPV(ST(0), STRLEN_length_of_s);
2411+
int t = (int)SvIV(ST(1));
2412+
2413+
XSauto_length_of_s = STRLEN_length_of_s;
2414+
foo(s, XSauto_length_of_s, t);
23972415
}
23982416

2399-
MODULE = x PACKAGE = x
2417+
and might be called from Perl as:
24002418

2401-
void dump_chars(char *s, short length(s))
2419+
foo("abcd", 9999);
24022420

2403-
should be called as C<dump_chars($string)>.
2421+
The exact C code generated will vary over releases, but the important
2422+
things to note are:
24042423

2405-
This directive is supported with ANSI-type function declarations only.
2424+
=over
24062425

2407-
=head3 Variable-length Parameter Lists
2426+
=item *
2427+
2428+
The auto variable C<XSauto_length_of_foo> will be declared with the
2429+
specified type and will be passed to any autocall function, but it won't
2430+
appear in the usage message. This variable is available for use in C<CODE>
2431+
blocks and similar.
24082432

2409-
XSUBs can have variable-length parameter lists by specifying an ellipsis
2410-
C<(...)> in the parameter list. This use of the ellipsis is similar to that
2411-
found in ANSI C. The programmer is able to determine the number of
2412-
arguments passed to the XSUB by examining the C<items> variable which the
2413-
B<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can
2414-
create an XSUB which accepts a list of parameters of unknown length.
2433+
=item *
24152434

2416-
The I<host> parameter for the rpcb_gettime() XSUB can be
2417-
optional so the ellipsis can be used to indicate that the
2418-
XSUB will take a variable number of parameters. Perl should
2419-
be able to call this XSUB with either of the following statements.
2435+
The auto variable C<STRLEN_length_of_s> is used in addition to allow
2436+
conversion between the type expected by C<SvPV()> and the type declared
2437+
for the length pseudo-parameter.
24202438

2421-
$status = rpcb_gettime($timep, $host);
2439+
=item *
24222440

2423-
$status = rpcb_gettime($timep);
2441+
A length parameter can appear anywhere in the signature, even before the
2442+
string parameter of the same name; but its position in any autocall
2443+
matches its position in the signature.
24242444

2425-
The XS code, with ellipsis, follows.
2445+
=item *
24262446

2427-
bool_t
2428-
rpcb_gettime(timep, ...)
2429-
time_t timep = NO_INIT
2430-
PREINIT:
2431-
char *host = "localhost";
2447+
Each length parameter must match another parameter of the same name. That
2448+
parameter must be a string type (something which maps to the C<T_PV>
2449+
typemap type).
2450+
2451+
=back
2452+
2453+
=head3 Ellipsis: variable-length parameter lists
2454+
2455+
int
2456+
foo(char *s, ...)
2457+
2458+
An XSUB can have a variable-length parameter list by specifying an
2459+
ellipsis as the last parameter, similar to C function declarations. Its
2460+
main effect is to disable the error check for too many parameters. Any
2461+
declared parameters will still be processed as normal, but the programmer
2462+
will have to access any extra arguments manually, making use of the
2463+
C<ST(n)> macro to access the nth item on the stack (counting from 0), and
2464+
the C<items> variable, which indicates the total number of passed
2465+
arguments, including any fixed arguments.
2466+
2467+
Note that currently XS doesn't provide any mechanism to autocall
2468+
variable-length C functions, so the ellipsis should only be used on XSUBs
2469+
which have a body.
2470+
2471+
For example, consider this Perl subroutine which returns the sum of all
2472+
of its arguments which are within a specified range:
2473+
2474+
sub minmax_sum {
2475+
my $min = shift;
2476+
my $max = shift;
2477+
my $RETVAL = 0;
2478+
$RETVAL += $_ for grep { $min <= $_ && $_ <= $max } @_;
2479+
return $RETVAL;
2480+
}
2481+
2482+
This XSUB provides equivalent functionality:
2483+
2484+
int
2485+
minmax_sum(int min, int max, ...)
24322486
CODE:
2433-
if (items > 1)
2434-
host = (char *)SvPVbyte_nolen(ST(1));
2435-
RETVAL = rpcb_gettime(host, &timep);
2487+
{
2488+
int i = 2; /* skip the two fixed arguments */
2489+
RETVAL = 0;
2490+
2491+
for (; i < items; i++) {
2492+
int val = (int)SvIV(ST(i));
2493+
if (min <= val && val <= max)
2494+
RETVAL += val;
2495+
}
2496+
}
24362497
OUTPUT:
2437-
timep
24382498
RETVAL
24392499

2500+
It is possible to write an XSUB which both accepts and returns a list. For
2501+
example, this XSUB does the equivalent of the Perl C<map { $_*3 } ...>
2502+
2503+
void
2504+
triple(...)
2505+
PPCODE:
2506+
SP += items;
2507+
{
2508+
int i;
2509+
for (i = 0; i < items; i++) {
2510+
int val = (int)SvIV(ST(i));
2511+
ST(i) = sv_2mortal(newSViv(val*3));
2512+
}
2513+
}
2514+
2515+
Note that the L<PPCODE|/The PPCODE: Keyword> keyword, in comparison to
2516+
C<CODE>, resets the local copy of the argument stack pointer, and relies
2517+
on the coder to place any return values on the stack. The example above
2518+
reclaims the passed arguments by setting C<SP> back to the top of the
2519+
stack, then replaces the items on the stack one by one.
2520+
24402521
=head2 The XSUB Input Part
24412522

24422523
XXX TBC
@@ -3247,7 +3328,7 @@ included in that case.
32473328

32483329
A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS:
32493330
variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable
3250-
(see L<"Variable-length Parameter Lists">). The last CASE: becomes the
3331+
(see L<"Ellipsis: variable-length parameter lists">). The last CASE: becomes the
32513332
B<default> case if it is not associated with a conditional. The following
32523333
example shows CASE switched via C<ix> with a function C<rpcb_gettime()>
32533334
having an alias C<x_gettime()>. When the function is called as

0 commit comments

Comments
 (0)