Commit | Line | Data |
a0d0e21e |
1 | =head1 NAME |
2 | |
8e07c86e |
3 | perlxs - XS language reference manual |
a0d0e21e |
4 | |
5 | =head1 DESCRIPTION |
6 | |
7 | =head2 Introduction |
8 | |
9 | XS is a language used to create an extension interface |
10 | between Perl and some C library which one wishes to use with |
11 | Perl. The XS interface is combined with the library to |
12 | create a new library which can be linked to Perl. An B<XSUB> |
13 | is a function in the XS language and is the core component |
14 | of the Perl application interface. |
15 | |
16 | The XS compiler is called B<xsubpp>. This compiler will embed |
17 | the constructs necessary to let an XSUB, which is really a C |
18 | function in disguise, manipulate Perl values and creates the |
19 | glue necessary to let Perl access the XSUB. The compiler |
20 | uses B<typemaps> to determine how to map C function parameters |
21 | and variables to Perl values. The default typemap handles |
22 | many common C types. A supplement typemap must be created |
23 | to handle special structures and types for the library being |
24 | linked. |
25 | |
cb1a09d0 |
26 | See L<perlxstut> for a tutorial on the whole extension creation process. |
8e07c86e |
27 | |
28 | =head2 On The Road |
29 | |
a5f75d66 |
30 | Many of the examples which follow will concentrate on creating an interface |
31 | between Perl and the ONC+ RPC bind library functions. The rpcb_gettime() |
32 | function is used to demonstrate many features of the XS language. This |
33 | function has two parameters; the first is an input parameter and the second |
34 | is an output parameter. The function also returns a status value. |
a0d0e21e |
35 | |
36 | bool_t rpcb_gettime(const char *host, time_t *timep); |
37 | |
38 | From C this function will be called with the following |
39 | statements. |
40 | |
41 | #include <rpc/rpc.h> |
42 | bool_t status; |
43 | time_t timep; |
44 | status = rpcb_gettime( "localhost", &timep ); |
45 | |
46 | If an XSUB is created to offer a direct translation between this function |
47 | and Perl, then this XSUB will be used from Perl with the following code. |
48 | The $status and $timep variables will contain the output of the function. |
49 | |
50 | use RPC; |
51 | $status = rpcb_gettime( "localhost", $timep ); |
52 | |
53 | The following XS file shows an XS subroutine, or XSUB, which |
54 | demonstrates one possible interface to the rpcb_gettime() |
55 | function. This XSUB represents a direct translation between |
56 | C and Perl and so preserves the interface even from Perl. |
57 | This XSUB will be invoked from Perl with the usage shown |
58 | above. Note that the first three #include statements, for |
59 | C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the |
60 | beginning of an XS file. This approach and others will be |
61 | expanded later in this document. |
62 | |
63 | #include "EXTERN.h" |
64 | #include "perl.h" |
65 | #include "XSUB.h" |
66 | #include <rpc/rpc.h> |
67 | |
68 | MODULE = RPC PACKAGE = RPC |
69 | |
70 | bool_t |
71 | rpcb_gettime(host,timep) |
8e07c86e |
72 | char *host |
73 | time_t &timep |
a0d0e21e |
74 | OUTPUT: |
75 | timep |
76 | |
77 | Any extension to Perl, including those containing XSUBs, |
78 | should have a Perl module to serve as the bootstrap which |
79 | pulls the extension into Perl. This module will export the |
80 | extension's functions and variables to the Perl program and |
81 | will cause the extension's XSUBs to be linked into Perl. |
82 | The following module will be used for most of the examples |
83 | in this document and should be used from Perl with the C<use> |
84 | command as shown earlier. Perl modules are explained in |
85 | more detail later in this document. |
86 | |
87 | package RPC; |
88 | |
89 | require Exporter; |
90 | require DynaLoader; |
91 | @ISA = qw(Exporter DynaLoader); |
92 | @EXPORT = qw( rpcb_gettime ); |
93 | |
94 | bootstrap RPC; |
95 | 1; |
96 | |
97 | Throughout this document a variety of interfaces to the rpcb_gettime() |
98 | XSUB will be explored. The XSUBs will take their parameters in different |
99 | orders or will take different numbers of parameters. In each case the |
100 | XSUB is an abstraction between Perl and the real C rpcb_gettime() |
101 | function, and the XSUB must always ensure that the real rpcb_gettime() |
102 | function is called with the correct parameters. This abstraction will |
103 | allow the programmer to create a more Perl-like interface to the C |
104 | function. |
105 | |
106 | =head2 The Anatomy of an XSUB |
107 | |
8e07c86e |
108 | The following XSUB allows a Perl program to access a C library function |
109 | called sin(). The XSUB will imitate the C function which takes a single |
110 | argument and returns a single value. |
a0d0e21e |
111 | |
112 | double |
113 | sin(x) |
8e07c86e |
114 | double x |
a0d0e21e |
115 | |
8e07c86e |
116 | When using C pointers the indirection operator C<*> should be considered |
117 | part of the type and the address operator C<&> should be considered part of |
118 | the variable, as is demonstrated in the rpcb_gettime() function above. See |
119 | the section on typemaps for more about handling qualifiers and unary |
120 | operators in C types. |
a0d0e21e |
121 | |
a0d0e21e |
122 | The function name and the return type must be placed on |
123 | separate lines. |
124 | |
125 | INCORRECT CORRECT |
126 | |
127 | double sin(x) double |
8e07c86e |
128 | double x sin(x) |
129 | double x |
a0d0e21e |
130 | |
c07a80fd |
131 | The function body may be indented or left-adjusted. The following example |
132 | shows a function with its body left-adjusted. Most examples in this |
133 | document will indent the body. |
134 | |
135 | CORRECT |
136 | |
137 | double |
138 | sin(x) |
139 | double x |
140 | |
a0d0e21e |
141 | =head2 The Argument Stack |
142 | |
143 | The argument stack is used to store the values which are |
144 | sent as parameters to the XSUB and to store the XSUB's |
145 | return value. In reality all Perl functions keep their |
146 | values on this stack at the same time, each limited to its |
147 | own range of positions on the stack. In this document the |
148 | first position on that stack which belongs to the active |
149 | function will be referred to as position 0 for that function. |
150 | |
8e07c86e |
151 | XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x> |
152 | refers to a position in this XSUB's part of the stack. Position 0 for that |
a0d0e21e |
153 | function would be known to the XSUB as ST(0). The XSUB's incoming |
154 | parameters and outgoing return values always begin at ST(0). For many |
155 | simple cases the B<xsubpp> compiler will generate the code necessary to |
156 | handle the argument stack by embedding code fragments found in the |
157 | typemaps. In more complex cases the programmer must supply the code. |
158 | |
159 | =head2 The RETVAL Variable |
160 | |
161 | The RETVAL variable is a magic variable which always matches |
162 | the return type of the C library function. The B<xsubpp> compiler will |
163 | supply this variable in each XSUB and by default will use it to hold the |
164 | return value of the C library function being called. In simple cases the |
165 | value of RETVAL will be placed in ST(0) of the argument stack where it can |
166 | be received by Perl as the return value of the XSUB. |
167 | |
168 | If the XSUB has a return type of C<void> then the compiler will |
169 | not supply a RETVAL variable for that function. When using |
170 | the PPCODE: directive the RETVAL variable may not be needed. |
171 | |
172 | =head2 The MODULE Keyword |
173 | |
174 | The MODULE keyword is used to start the XS code and to |
175 | specify the package of the functions which are being |
176 | defined. All text preceding the first MODULE keyword is |
177 | considered C code and is passed through to the output |
178 | untouched. Every XS module will have a bootstrap function |
179 | which is used to hook the XSUBs into Perl. The package name |
180 | of this bootstrap function will match the value of the last |
181 | MODULE statement in the XS source files. The value of |
182 | MODULE should always remain constant within the same XS |
183 | file, though this is not required. |
184 | |
185 | The following example will start the XS code and will place |
186 | all functions in a package named RPC. |
187 | |
188 | MODULE = RPC |
189 | |
190 | =head2 The PACKAGE Keyword |
191 | |
192 | When functions within an XS source file must be separated into packages |
193 | the PACKAGE keyword should be used. This keyword is used with the MODULE |
194 | keyword and must follow immediately after it when used. |
195 | |
196 | MODULE = RPC PACKAGE = RPC |
197 | |
198 | [ XS code in package RPC ] |
199 | |
200 | MODULE = RPC PACKAGE = RPCB |
201 | |
202 | [ XS code in package RPCB ] |
203 | |
204 | MODULE = RPC PACKAGE = RPC |
205 | |
206 | [ XS code in package RPC ] |
207 | |
208 | Although this keyword is optional and in some cases provides redundant |
209 | information it should always be used. This keyword will ensure that the |
210 | XSUBs appear in the desired package. |
211 | |
212 | =head2 The PREFIX Keyword |
213 | |
214 | The PREFIX keyword designates prefixes which should be |
215 | removed from the Perl function names. If the C function is |
216 | C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will |
217 | see this function as C<gettime()>. |
218 | |
219 | This keyword should follow the PACKAGE keyword when used. |
220 | If PACKAGE is not used then PREFIX should follow the MODULE |
221 | keyword. |
222 | |
223 | MODULE = RPC PREFIX = rpc_ |
224 | |
225 | MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_ |
226 | |
227 | =head2 The OUTPUT: Keyword |
228 | |
229 | The OUTPUT: keyword indicates that certain function parameters should be |
230 | updated (new values made visible to Perl) when the XSUB terminates or that |
231 | certain values should be returned to the calling Perl function. For |
232 | simple functions, such as the sin() function above, the RETVAL variable is |
233 | automatically designated as an output value. In more complex functions |
234 | the B<xsubpp> compiler will need help to determine which variables are output |
235 | variables. |
236 | |
237 | This keyword will normally be used to complement the CODE: keyword. |
238 | The RETVAL variable is not recognized as an output variable when the |
239 | CODE: keyword is present. The OUTPUT: keyword is used in this |
240 | situation to tell the compiler that RETVAL really is an output |
241 | variable. |
242 | |
243 | The OUTPUT: keyword can also be used to indicate that function parameters |
244 | are output variables. This may be necessary when a parameter has been |
245 | modified within the function and the programmer would like the update to |
8e07c86e |
246 | be seen by Perl. |
a0d0e21e |
247 | |
248 | bool_t |
249 | rpcb_gettime(host,timep) |
8e07c86e |
250 | char *host |
251 | time_t &timep |
a0d0e21e |
252 | OUTPUT: |
253 | timep |
254 | |
255 | The OUTPUT: keyword will also allow an output parameter to |
256 | be mapped to a matching piece of code rather than to a |
257 | typemap. |
258 | |
259 | bool_t |
260 | rpcb_gettime(host,timep) |
8e07c86e |
261 | char *host |
262 | time_t &timep |
a0d0e21e |
263 | OUTPUT: |
8e07c86e |
264 | timep sv_setnv(ST(1), (double)timep); |
a0d0e21e |
265 | |
266 | =head2 The CODE: Keyword |
267 | |
268 | This keyword is used in more complicated XSUBs which require |
269 | special handling for the C function. The RETVAL variable is |
270 | available but will not be returned unless it is specified |
271 | under the OUTPUT: keyword. |
272 | |
273 | The following XSUB is for a C function which requires special handling of |
274 | its parameters. The Perl usage is given first. |
275 | |
276 | $status = rpcb_gettime( "localhost", $timep ); |
277 | |
278 | The XSUB follows. |
279 | |
d1b91892 |
280 | bool_t |
281 | rpcb_gettime(host,timep) |
8e07c86e |
282 | char *host |
283 | time_t timep |
a0d0e21e |
284 | CODE: |
285 | RETVAL = rpcb_gettime( host, &timep ); |
286 | OUTPUT: |
287 | timep |
288 | RETVAL |
289 | |
c07a80fd |
290 | =head2 The INIT: Keyword |
291 | |
292 | The INIT: keyword allows initialization to be inserted into the XSUB before |
293 | the compiler generates the call to the C function. Unlike the CODE: keyword |
294 | above, this keyword does not affect the way the compiler handles RETVAL. |
295 | |
296 | bool_t |
297 | rpcb_gettime(host,timep) |
298 | char *host |
299 | time_t &timep |
300 | INIT: |
301 | printf("# Host is %s\n", host ); |
302 | OUTPUT: |
303 | timep |
a0d0e21e |
304 | |
305 | =head2 The NO_INIT Keyword |
306 | |
307 | The NO_INIT keyword is used to indicate that a function |
d1b91892 |
308 | parameter is being used as only an output value. The B<xsubpp> |
a0d0e21e |
309 | compiler will normally generate code to read the values of |
310 | all function parameters from the argument stack and assign |
311 | them to C variables upon entry to the function. NO_INIT |
312 | will tell the compiler that some parameters will be used for |
313 | output rather than for input and that they will be handled |
314 | before the function terminates. |
315 | |
316 | The following example shows a variation of the rpcb_gettime() function. |
d1b91892 |
317 | This function uses the timep variable as only an output variable and does |
a0d0e21e |
318 | not care about its initial contents. |
319 | |
320 | bool_t |
321 | rpcb_gettime(host,timep) |
8e07c86e |
322 | char *host |
323 | time_t &timep = NO_INIT |
a0d0e21e |
324 | OUTPUT: |
325 | timep |
326 | |
327 | =head2 Initializing Function Parameters |
328 | |
329 | Function parameters are normally initialized with their |
330 | values from the argument stack. The typemaps contain the |
331 | code segments which are used to transfer the Perl values to |
332 | the C parameters. The programmer, however, is allowed to |
333 | override the typemaps and supply alternate initialization |
334 | code. |
335 | |
336 | The following code demonstrates how to supply initialization code for |
337 | function parameters. The initialization code is eval'd by the compiler |
338 | before it is added to the output so anything which should be interpreted |
339 | literally, such as double quotes, must be protected with backslashes. |
340 | |
341 | bool_t |
342 | rpcb_gettime(host,timep) |
8e07c86e |
343 | char *host = (char *)SvPV(ST(0),na); |
344 | time_t &timep = 0; |
a0d0e21e |
345 | OUTPUT: |
346 | timep |
347 | |
348 | This should not be used to supply default values for parameters. One |
349 | would normally use this when a function parameter must be processed by |
350 | another library function before it can be used. Default parameters are |
351 | covered in the next section. |
352 | |
353 | =head2 Default Parameter Values |
354 | |
355 | Default values can be specified for function parameters by |
356 | placing an assignment statement in the parameter list. The |
357 | default value may be a number or a string. Defaults should |
358 | always be used on the right-most parameters only. |
359 | |
360 | To allow the XSUB for rpcb_gettime() to have a default host |
361 | value the parameters to the XSUB could be rearranged. The |
362 | XSUB will then call the real rpcb_gettime() function with |
363 | the parameters in the correct order. Perl will call this |
364 | XSUB with either of the following statements. |
365 | |
366 | $status = rpcb_gettime( $timep, $host ); |
367 | |
368 | $status = rpcb_gettime( $timep ); |
369 | |
370 | The XSUB will look like the code which follows. A CODE: |
371 | block is used to call the real rpcb_gettime() function with |
372 | the parameters in the correct order for that function. |
373 | |
374 | bool_t |
375 | rpcb_gettime(timep,host="localhost") |
8e07c86e |
376 | char *host |
377 | time_t timep = NO_INIT |
a0d0e21e |
378 | CODE: |
379 | RETVAL = rpcb_gettime( host, &timep ); |
380 | OUTPUT: |
381 | timep |
382 | RETVAL |
383 | |
c07a80fd |
384 | =head2 The PREINIT: Keyword |
385 | |
386 | The PREINIT: keyword allows extra variables to be declared before the |
387 | typemaps are expanded. If a variable is declared in a CODE: block then that |
388 | variable will follow any typemap code. This may result in a C syntax |
389 | error. To force the variable to be declared before the typemap code, place |
390 | it into a PREINIT: block. The PREINIT: keyword may be used one or more |
391 | times within an XSUB. |
392 | |
393 | The following examples are equivalent, but if the code is using complex |
394 | typemaps then the first example is safer. |
395 | |
396 | bool_t |
397 | rpcb_gettime(timep) |
398 | time_t timep = NO_INIT |
399 | PREINIT: |
400 | char *host = "localhost"; |
401 | CODE: |
402 | RETVAL = rpcb_gettime( host, &timep ); |
403 | OUTPUT: |
404 | timep |
405 | RETVAL |
406 | |
407 | A correct, but error-prone example. |
408 | |
409 | bool_t |
410 | rpcb_gettime(timep) |
411 | time_t timep = NO_INIT |
412 | CODE: |
413 | char *host = "localhost"; |
414 | RETVAL = rpcb_gettime( host, &timep ); |
415 | OUTPUT: |
416 | timep |
417 | RETVAL |
418 | |
419 | =head2 The INPUT: Keyword |
420 | |
421 | The XSUB's parameters are usually evaluated immediately after entering the |
422 | XSUB. The INPUT: keyword can be used to force those parameters to be |
423 | evaluated a little later. The INPUT: keyword can be used multiple times |
424 | within an XSUB and can be used to list one or more input variables. This |
425 | keyword is used with the PREINIT: keyword. |
426 | |
427 | The following example shows how the input parameter C<timep> can be |
428 | evaluated late, after a PREINIT. |
429 | |
430 | bool_t |
431 | rpcb_gettime(host,timep) |
432 | char *host |
433 | PREINIT: |
434 | time_t tt; |
435 | INPUT: |
436 | time_t timep |
437 | CODE: |
438 | RETVAL = rpcb_gettime( host, &tt ); |
439 | timep = tt; |
440 | OUTPUT: |
441 | timep |
442 | RETVAL |
443 | |
444 | The next example shows each input parameter evaluated late. |
445 | |
446 | bool_t |
447 | rpcb_gettime(host,timep) |
448 | PREINIT: |
449 | time_t tt; |
450 | INPUT: |
451 | char *host |
452 | PREINIT: |
453 | char *h; |
454 | INPUT: |
455 | time_t timep |
456 | CODE: |
457 | h = host; |
458 | RETVAL = rpcb_gettime( h, &tt ); |
459 | timep = tt; |
460 | OUTPUT: |
461 | timep |
462 | RETVAL |
463 | |
a0d0e21e |
464 | =head2 Variable-length Parameter Lists |
465 | |
466 | XSUBs can have variable-length parameter lists by specifying an ellipsis |
467 | C<(...)> in the parameter list. This use of the ellipsis is similar to that |
468 | found in ANSI C. The programmer is able to determine the number of |
469 | arguments passed to the XSUB by examining the C<items> variable which the |
470 | B<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can |
471 | create an XSUB which accepts a list of parameters of unknown length. |
472 | |
473 | The I<host> parameter for the rpcb_gettime() XSUB can be |
474 | optional so the ellipsis can be used to indicate that the |
475 | XSUB will take a variable number of parameters. Perl should |
d1b91892 |
476 | be able to call this XSUB with either of the following statements. |
a0d0e21e |
477 | |
478 | $status = rpcb_gettime( $timep, $host ); |
479 | |
480 | $status = rpcb_gettime( $timep ); |
481 | |
482 | The XS code, with ellipsis, follows. |
483 | |
484 | bool_t |
485 | rpcb_gettime(timep, ...) |
8e07c86e |
486 | time_t timep = NO_INIT |
c07a80fd |
487 | PREINIT: |
a0d0e21e |
488 | char *host = "localhost"; |
c07a80fd |
489 | CODE: |
490 | if( items > 1 ) |
491 | host = (char *)SvPV(ST(1), na); |
492 | RETVAL = rpcb_gettime( host, &timep ); |
a0d0e21e |
493 | OUTPUT: |
494 | timep |
495 | RETVAL |
496 | |
497 | =head2 The PPCODE: Keyword |
498 | |
499 | The PPCODE: keyword is an alternate form of the CODE: keyword and is used |
500 | to tell the B<xsubpp> compiler that the programmer is supplying the code to |
d1b91892 |
501 | control the argument stack for the XSUBs return values. Occasionally one |
a0d0e21e |
502 | will want an XSUB to return a list of values rather than a single value. |
503 | In these cases one must use PPCODE: and then explicitly push the list of |
504 | values on the stack. The PPCODE: and CODE: keywords are not used |
505 | together within the same XSUB. |
506 | |
507 | The following XSUB will call the C rpcb_gettime() function |
508 | and will return its two output values, timep and status, to |
509 | Perl as a single list. |
510 | |
d1b91892 |
511 | void |
512 | rpcb_gettime(host) |
8e07c86e |
513 | char *host |
c07a80fd |
514 | PREINIT: |
a0d0e21e |
515 | time_t timep; |
516 | bool_t status; |
c07a80fd |
517 | PPCODE: |
a0d0e21e |
518 | status = rpcb_gettime( host, &timep ); |
519 | EXTEND(sp, 2); |
cb1a09d0 |
520 | PUSHs(sv_2mortal(newSViv(status))); |
521 | PUSHs(sv_2mortal(newSViv(timep))); |
a0d0e21e |
522 | |
523 | Notice that the programmer must supply the C code necessary |
524 | to have the real rpcb_gettime() function called and to have |
525 | the return values properly placed on the argument stack. |
526 | |
527 | The C<void> return type for this function tells the B<xsubpp> compiler that |
528 | the RETVAL variable is not needed or used and that it should not be created. |
529 | In most scenarios the void return type should be used with the PPCODE: |
530 | directive. |
531 | |
532 | The EXTEND() macro is used to make room on the argument |
533 | stack for 2 return values. The PPCODE: directive causes the |
534 | B<xsubpp> compiler to create a stack pointer called C<sp>, and it |
535 | is this pointer which is being used in the EXTEND() macro. |
536 | The values are then pushed onto the stack with the PUSHs() |
537 | macro. |
538 | |
539 | Now the rpcb_gettime() function can be used from Perl with |
540 | the following statement. |
541 | |
542 | ($status, $timep) = rpcb_gettime("localhost"); |
543 | |
544 | =head2 Returning Undef And Empty Lists |
545 | |
d1b91892 |
546 | Occasionally the programmer will want to simply return |
a0d0e21e |
547 | C<undef> or an empty list if a function fails rather than a |
548 | separate status value. The rpcb_gettime() function offers |
549 | just this situation. If the function succeeds we would like |
550 | to have it return the time and if it fails we would like to |
551 | have undef returned. In the following Perl code the value |
552 | of $timep will either be undef or it will be a valid time. |
553 | |
554 | $timep = rpcb_gettime( "localhost" ); |
555 | |
556 | The following XSUB uses the C<void> return type to disable the generation of |
557 | the RETVAL variable and uses a CODE: block to indicate to the compiler |
558 | that the programmer has supplied all the necessary code. The |
559 | sv_newmortal() call will initialize the return value to undef, making that |
560 | the default return value. |
561 | |
562 | void |
563 | rpcb_gettime(host) |
564 | char * host |
c07a80fd |
565 | PREINIT: |
a0d0e21e |
566 | time_t timep; |
567 | bool_t x; |
c07a80fd |
568 | CODE: |
a0d0e21e |
569 | ST(0) = sv_newmortal(); |
570 | if( rpcb_gettime( host, &timep ) ) |
571 | sv_setnv( ST(0), (double)timep); |
a0d0e21e |
572 | |
573 | The next example demonstrates how one would place an explicit undef in the |
574 | return value, should the need arise. |
575 | |
576 | void |
577 | rpcb_gettime(host) |
578 | char * host |
c07a80fd |
579 | PREINIT: |
a0d0e21e |
580 | time_t timep; |
581 | bool_t x; |
c07a80fd |
582 | CODE: |
a0d0e21e |
583 | ST(0) = sv_newmortal(); |
584 | if( rpcb_gettime( host, &timep ) ){ |
585 | sv_setnv( ST(0), (double)timep); |
586 | } |
587 | else{ |
588 | ST(0) = &sv_undef; |
589 | } |
a0d0e21e |
590 | |
591 | To return an empty list one must use a PPCODE: block and |
592 | then not push return values on the stack. |
593 | |
594 | void |
595 | rpcb_gettime(host) |
8e07c86e |
596 | char *host |
c07a80fd |
597 | PREINIT: |
a0d0e21e |
598 | time_t timep; |
c07a80fd |
599 | PPCODE: |
a0d0e21e |
600 | if( rpcb_gettime( host, &timep ) ) |
cb1a09d0 |
601 | PUSHs(sv_2mortal(newSViv(timep))); |
a0d0e21e |
602 | else{ |
603 | /* Nothing pushed on stack, so an empty */ |
604 | /* list is implicitly returned. */ |
605 | } |
a0d0e21e |
606 | |
f27cfbbe |
607 | Some people may be inclined to include an explicit C<return> in the above |
608 | XSUB, rather than letting control fall through to the end. In those |
609 | situations C<XSRETURN_EMPTY> should be used, instead. This will ensure that |
610 | the XSUB stack is properly adjusted. Consult L<perlguts/"API LISTING"> for |
611 | other C<XSRETURN> macros. |
612 | |
4633a7c4 |
613 | =head2 The REQUIRE: Keyword |
614 | |
615 | The REQUIRE: keyword is used to indicate the minimum version of the |
616 | B<xsubpp> compiler needed to compile the XS module. An XS module which |
617 | contains the following statement will only compile with B<xsubpp> version |
618 | 1.922 or greater: |
619 | |
620 | REQUIRE: 1.922 |
621 | |
a0d0e21e |
622 | =head2 The CLEANUP: Keyword |
623 | |
624 | This keyword can be used when an XSUB requires special cleanup procedures |
625 | before it terminates. When the CLEANUP: keyword is used it must follow |
626 | any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB. The |
627 | code specified for the cleanup block will be added as the last statements |
628 | in the XSUB. |
629 | |
630 | =head2 The BOOT: Keyword |
631 | |
632 | The BOOT: keyword is used to add code to the extension's bootstrap |
633 | function. The bootstrap function is generated by the B<xsubpp> compiler and |
634 | normally holds the statements necessary to register any XSUBs with Perl. |
635 | With the BOOT: keyword the programmer can tell the compiler to add extra |
636 | statements to the bootstrap function. |
637 | |
638 | This keyword may be used any time after the first MODULE keyword and should |
639 | appear on a line by itself. The first blank line after the keyword will |
640 | terminate the code block. |
641 | |
642 | BOOT: |
643 | # The following message will be printed when the |
644 | # bootstrap function executes. |
645 | printf("Hello from the bootstrap!\n"); |
646 | |
c07a80fd |
647 | =head2 The VERSIONCHECK: Keyword |
648 | |
649 | The VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and |
650 | C<-noversioncheck> options. This keyword overrides the commandline |
651 | options. Version checking is enabled by default. When version checking is |
652 | enabled the XS module will attempt to verify that its version matches the |
653 | version of the PM module. |
654 | |
655 | To enable version checking: |
656 | |
657 | VERSIONCHECK: ENABLE |
658 | |
659 | To disable version checking: |
660 | |
661 | VERSIONCHECK: DISABLE |
662 | |
663 | =head2 The PROTOTYPES: Keyword |
664 | |
665 | The PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and |
666 | C<-noprototypes> options. This keyword overrides the commandline options. |
667 | Prototypes are enabled by default. When prototypes are enabled XSUBs will |
668 | be given Perl prototypes. This keyword may be used multiple times in an XS |
669 | module to enable and disable prototypes for different parts of the module. |
670 | |
671 | To enable prototypes: |
672 | |
673 | PROTOTYPES: ENABLE |
674 | |
675 | To disable prototypes: |
676 | |
677 | PROTOTYPES: DISABLE |
678 | |
679 | =head2 The PROTOTYPE: Keyword |
680 | |
681 | This keyword is similar to the PROTOTYPES: keyword above but can be used to |
682 | force B<xsubpp> to use a specific prototype for the XSUB. This keyword |
683 | overrides all other prototype options and keywords but affects only the |
684 | current XSUB. Consult L<perlsub/Prototypes> for information about Perl |
685 | prototypes. |
686 | |
687 | bool_t |
688 | rpcb_gettime(timep, ...) |
689 | time_t timep = NO_INIT |
690 | PROTOTYPE: $;$ |
691 | PREINIT: |
692 | char *host = "localhost"; |
693 | CODE: |
694 | if( items > 1 ) |
695 | host = (char *)SvPV(ST(1), na); |
696 | RETVAL = rpcb_gettime( host, &timep ); |
697 | OUTPUT: |
698 | timep |
699 | RETVAL |
700 | |
701 | =head2 The ALIAS: Keyword |
702 | |
703 | The ALIAS: keyword allows an XSUB to have two more more unique Perl names |
704 | and to know which of those names was used when it was invoked. The Perl |
705 | names may be fully-qualified with package names. Each alias is given an |
706 | index. The compiler will setup a variable called C<ix> which contain the |
707 | index of the alias which was used. When the XSUB is called with its |
708 | declared name C<ix> will be 0. |
709 | |
710 | The following example will create aliases C<FOO::gettime()> and |
711 | C<BAR::getit()> for this function. |
712 | |
713 | bool_t |
714 | rpcb_gettime(host,timep) |
715 | char *host |
716 | time_t &timep |
717 | ALIAS: |
718 | FOO::gettime = 1 |
719 | BAR::getit = 2 |
720 | INIT: |
721 | printf("# ix = %d\n", ix ); |
722 | OUTPUT: |
723 | timep |
724 | |
725 | =head2 The INCLUDE: Keyword |
726 | |
727 | This keyword can be used to pull other files into the XS module. The other |
728 | files may have XS code. INCLUDE: can also be used to run a command to |
729 | generate the XS code to be pulled into the module. |
730 | |
731 | The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function: |
732 | |
733 | bool_t |
734 | rpcb_gettime(host,timep) |
735 | char *host |
736 | time_t &timep |
737 | OUTPUT: |
738 | timep |
739 | |
740 | The XS module can use INCLUDE: to pull that file into it. |
741 | |
742 | INCLUDE: Rpcb1.xsh |
743 | |
744 | If the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then |
745 | the compiler will interpret the parameters as a command. |
746 | |
747 | INCLUDE: cat Rpcb1.xsh | |
748 | |
749 | =head2 The CASE: Keyword |
750 | |
751 | The CASE: keyword allows an XSUB to have multiple distinct parts with each |
752 | part acting as a virtual XSUB. CASE: is greedy and if it is used then all |
753 | other XS keywords must be contained within a CASE:. This means nothing may |
754 | precede the first CASE: in the XSUB and anything following the last CASE: is |
755 | included in that case. |
756 | |
757 | A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS: |
758 | variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable |
759 | (see L<"Variable-length Parameter Lists">). The last CASE: becomes the |
760 | B<default> case if it is not associated with a conditional. The following |
761 | example shows CASE switched via C<ix> with a function C<rpcb_gettime()> |
762 | having an alias C<x_gettime()>. When the function is called as |
b772cb6e |
763 | C<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>, |
764 | but when the function is called as C<x_gettime()> its parameters are |
c07a80fd |
765 | reversed, C<(time_t *timep, char *host)>. |
766 | |
767 | long |
768 | rpcb_gettime(a,b) |
769 | CASE: ix == 1 |
770 | ALIAS: |
771 | x_gettime = 1 |
772 | INPUT: |
773 | # 'a' is timep, 'b' is host |
774 | char *b |
775 | time_t a = NO_INIT |
776 | CODE: |
777 | RETVAL = rpcb_gettime( b, &a ); |
778 | OUTPUT: |
779 | a |
780 | RETVAL |
781 | CASE: |
782 | # 'a' is host, 'b' is timep |
783 | char *a |
784 | time_t &b = NO_INIT |
785 | OUTPUT: |
786 | b |
787 | RETVAL |
788 | |
789 | That function can be called with either of the following statements. Note |
790 | the different argument lists. |
791 | |
792 | $status = rpcb_gettime( $host, $timep ); |
793 | |
794 | $status = x_gettime( $timep, $host ); |
795 | |
796 | =head2 The & Unary Operator |
797 | |
798 | The & unary operator is used to tell the compiler that it should dereference |
799 | the object when it calls the C function. This is used when a CODE: block is |
800 | not used and the object is a not a pointer type (the object is an C<int> or |
801 | C<long> but not a C<int*> or C<long*>). |
802 | |
803 | The following XSUB will generate incorrect C code. The xsubpp compiler will |
804 | turn this into code which calls C<rpcb_gettime()> with parameters C<(char |
805 | *host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep> |
806 | parameter to be of type C<time_t*> rather than C<time_t>. |
807 | |
808 | bool_t |
809 | rpcb_gettime(host,timep) |
810 | char *host |
811 | time_t timep |
812 | OUTPUT: |
813 | timep |
814 | |
815 | That problem is corrected by using the C<&> operator. The xsubpp compiler |
816 | will now turn this into code which calls C<rpcb_gettime()> correctly with |
817 | parameters C<(char *host, time_t *timep)>. It does this by carrying the |
818 | C<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>. |
819 | |
820 | bool_t |
821 | rpcb_gettime(host,timep) |
822 | char *host |
823 | time_t &timep |
824 | OUTPUT: |
825 | timep |
826 | |
a0d0e21e |
827 | =head2 Inserting Comments and C Preprocessor Directives |
828 | |
f27cfbbe |
829 | C preprocessor directives are allowed within BOOT:, PREINIT: INIT:, |
830 | CODE:, PPCODE: and CLEANUP: blocks, as well as outside the functions. |
831 | Comments are allowed anywhere after the MODULE keyword. The compiler |
832 | will pass the preprocessor directives through untouched and will remove |
833 | the commented lines. |
b772cb6e |
834 | |
f27cfbbe |
835 | Comments can be added to XSUBs by placing a C<#> as the first |
836 | non-whitespace of a line. Care should be taken to avoid making the |
837 | comment look like a C preprocessor directive, lest it be interpreted as |
838 | such. The simplest way to prevent this is to put whitespace in front of |
839 | the C<#>. |
840 | |
f27cfbbe |
841 | If you use preprocessor directives to choose one of two |
842 | versions of a function, use |
843 | |
844 | #if ... version1 |
845 | #else /* ... version2 */ |
846 | #endif |
847 | |
848 | and not |
849 | |
850 | #if ... version1 |
851 | #endif |
852 | #if ... version2 |
853 | #endif |
854 | |
855 | because otherwise xsubpp will believe that you made a duplicate |
856 | definition of the function. Also, put a blank line before the |
857 | #else/#endif so it will not be seen as part of the function body. |
a0d0e21e |
858 | |
859 | =head2 Using XS With C++ |
860 | |
861 | If a function is defined as a C++ method then it will assume |
862 | its first argument is an object pointer. The object pointer |
863 | will be stored in a variable called THIS. The object should |
864 | have been created by C++ with the new() function and should |
cb1a09d0 |
865 | be blessed by Perl with the sv_setref_pv() macro. The |
866 | blessing of the object by Perl can be handled by a typemap. An example |
867 | typemap is shown at the end of this section. |
a0d0e21e |
868 | |
869 | If the method is defined as static it will call the C++ |
870 | function using the class::method() syntax. If the method is not static |
f27cfbbe |
871 | the function will be called using the THIS-E<gt>method() syntax. |
a0d0e21e |
872 | |
cb1a09d0 |
873 | The next examples will use the following C++ class. |
a0d0e21e |
874 | |
a5f75d66 |
875 | class color { |
cb1a09d0 |
876 | public: |
a5f75d66 |
877 | color(); |
878 | ~color(); |
cb1a09d0 |
879 | int blue(); |
880 | void set_blue( int ); |
881 | |
882 | private: |
883 | int c_blue; |
884 | }; |
885 | |
886 | The XSUBs for the blue() and set_blue() methods are defined with the class |
887 | name but the parameter for the object (THIS, or "self") is implicit and is |
888 | not listed. |
889 | |
890 | int |
891 | color::blue() |
a0d0e21e |
892 | |
893 | void |
cb1a09d0 |
894 | color::set_blue( val ) |
895 | int val |
a0d0e21e |
896 | |
cb1a09d0 |
897 | Both functions will expect an object as the first parameter. The xsubpp |
898 | compiler will call that object C<THIS> and will use it to call the specified |
899 | method. So in the C++ code the blue() and set_blue() methods will be called |
900 | in the following manner. |
a0d0e21e |
901 | |
cb1a09d0 |
902 | RETVAL = THIS->blue(); |
a0d0e21e |
903 | |
cb1a09d0 |
904 | THIS->set_blue( val ); |
a0d0e21e |
905 | |
cb1a09d0 |
906 | If the function's name is B<DESTROY> then the C++ C<delete> function will be |
907 | called and C<THIS> will be given as its parameter. |
a0d0e21e |
908 | |
d1b91892 |
909 | void |
cb1a09d0 |
910 | color::DESTROY() |
911 | |
912 | The C++ code will call C<delete>. |
913 | |
914 | delete THIS; |
a0d0e21e |
915 | |
cb1a09d0 |
916 | If the function's name is B<new> then the C++ C<new> function will be called |
917 | to create a dynamic C++ object. The XSUB will expect the class name, which |
918 | will be kept in a variable called C<CLASS>, to be given as the first |
919 | argument. |
a0d0e21e |
920 | |
cb1a09d0 |
921 | color * |
922 | color::new() |
a0d0e21e |
923 | |
cb1a09d0 |
924 | The C++ code will call C<new>. |
a0d0e21e |
925 | |
cb1a09d0 |
926 | RETVAL = new color(); |
927 | |
928 | The following is an example of a typemap that could be used for this C++ |
929 | example. |
930 | |
931 | TYPEMAP |
932 | color * O_OBJECT |
933 | |
934 | OUTPUT |
935 | # The Perl object is blessed into 'CLASS', which should be a |
936 | # char* having the name of the package for the blessing. |
937 | O_OBJECT |
938 | sv_setref_pv( $arg, CLASS, (void*)$var ); |
c07a80fd |
939 | |
cb1a09d0 |
940 | INPUT |
941 | O_OBJECT |
942 | if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) ) |
943 | $var = ($type)SvIV((SV*)SvRV( $arg )); |
944 | else{ |
945 | warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" ); |
946 | XSRETURN_UNDEF; |
947 | } |
a0d0e21e |
948 | |
d1b91892 |
949 | =head2 Interface Strategy |
a0d0e21e |
950 | |
951 | When designing an interface between Perl and a C library a straight |
952 | translation from C to XS is often sufficient. The interface will often be |
953 | very C-like and occasionally nonintuitive, especially when the C function |
954 | modifies one of its parameters. In cases where the programmer wishes to |
955 | create a more Perl-like interface the following strategy may help to |
956 | identify the more critical parts of the interface. |
957 | |
958 | Identify the C functions which modify their parameters. The XSUBs for |
959 | these functions may be able to return lists to Perl, or may be |
960 | candidates to return undef or an empty list in case of failure. |
961 | |
d1b91892 |
962 | Identify which values are used by only the C and XSUB functions |
a0d0e21e |
963 | themselves. If Perl does not need to access the contents of the value |
964 | then it may not be necessary to provide a translation for that value |
965 | from C to Perl. |
966 | |
967 | Identify the pointers in the C function parameter lists and return |
968 | values. Some pointers can be handled in XS with the & unary operator on |
969 | the variable name while others will require the use of the * operator on |
970 | the type name. In general it is easier to work with the & operator. |
971 | |
972 | Identify the structures used by the C functions. In many |
973 | cases it may be helpful to use the T_PTROBJ typemap for |
974 | these structures so they can be manipulated by Perl as |
975 | blessed objects. |
976 | |
a0d0e21e |
977 | =head2 Perl Objects And C Structures |
978 | |
979 | When dealing with C structures one should select either |
980 | B<T_PTROBJ> or B<T_PTRREF> for the XS type. Both types are |
981 | designed to handle pointers to complex objects. The |
982 | T_PTRREF type will allow the Perl object to be unblessed |
983 | while the T_PTROBJ type requires that the object be blessed. |
984 | By using T_PTROBJ one can achieve a form of type-checking |
d1b91892 |
985 | because the XSUB will attempt to verify that the Perl object |
a0d0e21e |
986 | is of the expected type. |
987 | |
988 | The following XS code shows the getnetconfigent() function which is used |
8e07c86e |
989 | with ONC+ TIRPC. The getnetconfigent() function will return a pointer to a |
a0d0e21e |
990 | C structure and has the C prototype shown below. The example will |
991 | demonstrate how the C pointer will become a Perl reference. Perl will |
992 | consider this reference to be a pointer to a blessed object and will |
993 | attempt to call a destructor for the object. A destructor will be |
994 | provided in the XS source to free the memory used by getnetconfigent(). |
995 | Destructors in XS can be created by specifying an XSUB function whose name |
996 | ends with the word B<DESTROY>. XS destructors can be used to free memory |
997 | which may have been malloc'd by another XSUB. |
998 | |
999 | struct netconfig *getnetconfigent(const char *netid); |
1000 | |
1001 | A C<typedef> will be created for C<struct netconfig>. The Perl |
1002 | object will be blessed in a class matching the name of the C |
1003 | type, with the tag C<Ptr> appended, and the name should not |
1004 | have embedded spaces if it will be a Perl package name. The |
1005 | destructor will be placed in a class corresponding to the |
1006 | class of the object and the PREFIX keyword will be used to |
1007 | trim the name to the word DESTROY as Perl will expect. |
1008 | |
1009 | typedef struct netconfig Netconfig; |
1010 | |
1011 | MODULE = RPC PACKAGE = RPC |
1012 | |
1013 | Netconfig * |
1014 | getnetconfigent(netid) |
8e07c86e |
1015 | char *netid |
a0d0e21e |
1016 | |
1017 | MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_ |
1018 | |
1019 | void |
1020 | rpcb_DESTROY(netconf) |
8e07c86e |
1021 | Netconfig *netconf |
a0d0e21e |
1022 | CODE: |
1023 | printf("Now in NetconfigPtr::DESTROY\n"); |
1024 | free( netconf ); |
1025 | |
1026 | This example requires the following typemap entry. Consult the typemap |
1027 | section for more information about adding new typemaps for an extension. |
1028 | |
1029 | TYPEMAP |
1030 | Netconfig * T_PTROBJ |
1031 | |
1032 | This example will be used with the following Perl statements. |
1033 | |
1034 | use RPC; |
1035 | $netconf = getnetconfigent("udp"); |
1036 | |
1037 | When Perl destroys the object referenced by $netconf it will send the |
1038 | object to the supplied XSUB DESTROY function. Perl cannot determine, and |
1039 | does not care, that this object is a C struct and not a Perl object. In |
1040 | this sense, there is no difference between the object created by the |
1041 | getnetconfigent() XSUB and an object created by a normal Perl subroutine. |
1042 | |
a0d0e21e |
1043 | =head2 The Typemap |
1044 | |
1045 | The typemap is a collection of code fragments which are used by the B<xsubpp> |
1046 | compiler to map C function parameters and values to Perl values. The |
1047 | typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and |
1048 | C<OUTPUT>. The INPUT section tells the compiler how to translate Perl values |
1049 | into variables of certain C types. The OUTPUT section tells the compiler |
1050 | how to translate the values from certain C types into values Perl can |
1051 | understand. The TYPEMAP section tells the compiler which of the INPUT and |
1052 | OUTPUT code fragments should be used to map a given C type to a Perl value. |
1053 | Each of the sections of the typemap must be preceded by one of the TYPEMAP, |
1054 | INPUT, or OUTPUT keywords. |
1055 | |
1056 | The default typemap in the C<ext> directory of the Perl source contains many |
1057 | useful types which can be used by Perl extensions. Some extensions define |
1058 | additional typemaps which they keep in their own directory. These |
1059 | additional typemaps may reference INPUT and OUTPUT maps in the main |
1060 | typemap. The B<xsubpp> compiler will allow the extension's own typemap to |
1061 | override any mappings which are in the default typemap. |
1062 | |
1063 | Most extensions which require a custom typemap will need only the TYPEMAP |
1064 | section of the typemap file. The custom typemap used in the |
1065 | getnetconfigent() example shown earlier demonstrates what may be the typical |
1066 | use of extension typemaps. That typemap is used to equate a C structure |
1067 | with the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown |
1068 | here. Note that the C type is separated from the XS type with a tab and |
1069 | that the C unary operator C<*> is considered to be a part of the C type name. |
1070 | |
1071 | TYPEMAP |
1072 | Netconfig *<tab>T_PTROBJ |
1073 | |
1074 | =head1 EXAMPLES |
1075 | |
1076 | File C<RPC.xs>: Interface to some ONC+ RPC bind library functions. |
1077 | |
1078 | #include "EXTERN.h" |
1079 | #include "perl.h" |
1080 | #include "XSUB.h" |
1081 | |
1082 | #include <rpc/rpc.h> |
1083 | |
1084 | typedef struct netconfig Netconfig; |
1085 | |
1086 | MODULE = RPC PACKAGE = RPC |
1087 | |
1088 | void |
1089 | rpcb_gettime(host="localhost") |
8e07c86e |
1090 | char *host |
c07a80fd |
1091 | PREINIT: |
a0d0e21e |
1092 | time_t timep; |
c07a80fd |
1093 | CODE: |
a0d0e21e |
1094 | ST(0) = sv_newmortal(); |
1095 | if( rpcb_gettime( host, &timep ) ) |
1096 | sv_setnv( ST(0), (double)timep ); |
a0d0e21e |
1097 | |
1098 | Netconfig * |
1099 | getnetconfigent(netid="udp") |
8e07c86e |
1100 | char *netid |
a0d0e21e |
1101 | |
1102 | MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_ |
1103 | |
1104 | void |
1105 | rpcb_DESTROY(netconf) |
8e07c86e |
1106 | Netconfig *netconf |
a0d0e21e |
1107 | CODE: |
1108 | printf("NetconfigPtr::DESTROY\n"); |
1109 | free( netconf ); |
1110 | |
1111 | File C<typemap>: Custom typemap for RPC.xs. |
1112 | |
1113 | TYPEMAP |
1114 | Netconfig * T_PTROBJ |
1115 | |
1116 | File C<RPC.pm>: Perl module for the RPC extension. |
1117 | |
1118 | package RPC; |
1119 | |
1120 | require Exporter; |
1121 | require DynaLoader; |
1122 | @ISA = qw(Exporter DynaLoader); |
1123 | @EXPORT = qw(rpcb_gettime getnetconfigent); |
1124 | |
1125 | bootstrap RPC; |
1126 | 1; |
1127 | |
1128 | File C<rpctest.pl>: Perl test program for the RPC extension. |
1129 | |
1130 | use RPC; |
1131 | |
1132 | $netconf = getnetconfigent(); |
1133 | $a = rpcb_gettime(); |
1134 | print "time = $a\n"; |
1135 | print "netconf = $netconf\n"; |
1136 | |
1137 | $netconf = getnetconfigent("tcp"); |
1138 | $a = rpcb_gettime("poplar"); |
1139 | print "time = $a\n"; |
1140 | print "netconf = $netconf\n"; |
1141 | |
1142 | |
c07a80fd |
1143 | =head1 XS VERSION |
1144 | |
f27cfbbe |
1145 | This document covers features supported by C<xsubpp> 1.935. |
c07a80fd |
1146 | |
a0d0e21e |
1147 | =head1 AUTHOR |
1148 | |
d1b91892 |
1149 | Dean Roehrich F<E<lt>roehrich@cray.comE<gt>> |
b772cb6e |
1150 | Jul 8, 1996 |