Skip to content

Commit 7fd59c0

Browse files
committed
perlxs.pod: update: init part, INIT sections
Add text for the new '=head2 The XSUB Init Part' section, and rewrite the existing entry for the INIT keyword.
1 parent fd4933d commit 7fd59c0

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,39 +2792,37 @@ shown here. Even better, just don't use it at all.
27922792

27932793
=head2 The XSUB Init Part
27942794

2795-
XXX TBC
2796-
2797-
XXX NB: the keywords described in L<XSUB Generic Keywords> and L<Sharing
2798-
XSUB bodies> may also appear in this part, plus C<C_ARGS> C<INTERFACE> and
2799-
C<INTERFACE_MACRO>.
2795+
Following an XSUB's input part, an optional init part follows. This
2796+
consists solely of the C<INIT> keyword described below, plus the keywords
2797+
described in L<XSUB Generic Keywords> and L<Sharing XSUB bodies>, plus the
2798+
L<C_ARGS|/The C_ARGS: Keyword>, L<INTERFACE|/The INTERFACE: Keyword> and
2799+
L<INTERFACE_MACRO|/The INTERFACE_MACRO: Keyword> keywords.
28002800

28012801
=head3 The INIT: Keyword
28022802

2803-
The INIT: keyword allows initialization to be inserted into the XSUB before
2804-
the compiler generates the call to the C function. Unlike the CODE: keyword
2805-
above, this keyword does not affect the way the compiler handles RETVAL.
2803+
The C<INIT> keyword allows arbitrary initialisation code to inserted after
2804+
any variable declarations (and their initialisations), but before the main
2805+
body of code. It is primarily intended for use when the main body is an
2806+
autocall to a C function. For example these two XSUBs are equivalent:
28062807

2807-
bool_t
2808-
rpcb_gettime(host, timep)
2809-
char *host
2810-
time_t &timep
2808+
int
2809+
foo(int i)
28112810
INIT:
2812-
printf("# Host is %s\n", host);
2813-
OUTPUT:
2814-
timep
2815-
2816-
Another use for the INIT: section is to check for preconditions before
2817-
making a call to the C function:
2811+
if (i < 0)
2812+
XSRETURN_UNDEF;
28182813

2819-
long long
2820-
lldiv(a, b)
2821-
long long a
2822-
long long b
2823-
INIT:
2824-
if (a == 0 && b == 0)
2814+
int
2815+
foo(int i)
2816+
CODE:
2817+
if (i < 0)
28252818
XSRETURN_UNDEF;
2826-
if (b == 0)
2827-
croak("lldiv: cannot divide by 0");
2819+
RETVAL = foo(i);
2820+
OUTPUT:
2821+
RETVAL
2822+
2823+
Any lines following C<INIT> until the next keyword (except POD and XS
2824+
comments) are copied out as-is to the C code file. Multiple C<INIT>
2825+
keywords are allowed.
28282826

28292827
=head2 The XSUB Code Part
28302828

0 commit comments

Comments
 (0)