3 XS code to test the typemap entries
5 Copyright (C) 2001 Tim Jenness.
10 #include "EXTERN.h" /* std perl include */
11 #include "perl.h" /* std perl include */
12 #include "XSUB.h" /* XSUB include */
14 /* Prototypes for external functions */
15 FILE * xsfopen( const char * );
16 int xsfclose( FILE * );
17 int xsfprintf( FILE *, const char *);
19 /* Type definitions required for the XS typemaps */
20 typedef SV * SVREF; /* T_SVREF */
21 typedef int SysRet; /* T_SYSRET */
22 typedef int Int; /* T_INT */
23 typedef int intRef; /* T_PTRREF */
24 typedef int intObj; /* T_PTROBJ */
25 typedef int intRefIv; /* T_REF_IV_PTR */
26 typedef int intArray; /* T_ARRAY */
27 typedef short shortOPQ; /* T_OPAQUE */
28 typedef int intOpq; /* T_OPAQUEPTR */
30 /* Some static memory for the tests */
37 /* Helper functions */
39 /* T_ARRAY - allocate some memory */
40 intArray * intArrayPtr( int nelem ) {
42 New(0, array, nelem, intArray);
47 MODULE = XS::Typemap PACKAGE = XS::Typemap
53 Each C type is represented by an entry in the typemap file that
54 is responsible for converting perl variables (SV, AV, HV and CV) to
61 This simply passes the C representation of the Perl variable (an SV*)
62 in and out of the XS layer. This can be used if the C code wants
63 to deal directly with the Perl variable.
71 /* create a new sv for return that is a copy of the input
72 do not simply copy the pointer since the SV will be marked
73 mortal by the INPUT typemap when it is pushed back onto the stack */
74 RETVAL = sv_mortalcopy( sv );
75 /* increment the refcount since the default INPUT typemap mortalizes
76 by default and we don't want to decrement the ref count twice
84 Used to pass in and return a reference to an SV.
98 From the perl level this is a reference to a perl array.
99 From the C level this is a pointer to an AV.
113 From the perl level this is a reference to a perl hash.
114 From the C level this is a pointer to a HV.
128 From the perl level this is a reference to a perl subroutine
129 (e.g. $sub = sub { 1 };). From the C level this is a pointer
145 The T_SYSRET typemap is used to process return values from system calls.
146 It is only meaningful when passing values from C to perl (there is
147 no concept of passing a system return value from Perl to C).
149 System calls return -1 on error (setting ERRNO with the reason)
150 and (usually) 0 on success. If the return value is -1 this typemap
151 returns C<undef>. If the return value is not -1, this typemap
152 translates a 0 (perl false) to "0 but true" (which
153 is perl true) or returns the value itself, to indicate that the
156 The L<POSIX|POSIX> module makes extensive use of this type.
160 # Test a successful return
194 A signed integer. This is cast to the required integer type when
195 passed to C and converted to a IV when passed back to Perl.
209 A signed integer. This typemap converts the Perl value to a native
210 integer type (the C<int> type on the current platform). When returning
211 the value to perl it is processed in the same way as for T_IV.
213 Its behaviour is identical to using an C<int> type in XS with T_IV.
217 An enum value. Used to transfer an enum component
218 from C. There is no reason to pass an enum value to C since
219 it is stored as an IV inside perl.
223 # The test should return the value for SVt_PVHV.
224 # 11 at the present time but we can't not rely on this
225 # for testing purposes.
236 A boolean type. This can be used to pass true and false values to and
251 This is for unsigned integers. It is equivalent to using T_UV
252 but explicitly casts the variable to type C<unsigned int>.
253 The default type for C<unsigned int> is T_UV.
257 Short integers. This is equivalent to T_IV but explicitly casts
258 the return to type C<short>. The default typemap for C<short>
263 Unsigned short integers. This is equivalent to T_UV but explicitly
264 casts the return to type C<unsigned short>. The default typemap for
265 C<unsigned short> is T_UV.
267 T_U_SHORT is used for type C<U16> in the standard typemap.
282 Long integers. This is equivalent to T_IV but explicitly casts
283 the return to type C<long>. The default typemap for C<long>
288 Unsigned long integers. This is equivalent to T_UV but explicitly
289 casts the return to type C<unsigned long>. The default typemap for
290 C<unsigned long> is T_UV.
292 T_U_LONG is used for type C<U32> in the standard typemap.
306 Single 8-bit characters.
336 A floating point number. This typemap guarantees to return a variable
351 A Perl floating point number. Similar to T_IV and T_UV in that the
352 return type is cast to the requested numeric type rather than
367 A double precision floating point number. This typemap guarantees to
368 return a variable cast to a C<double>.
396 A memory address (pointer). Typically associated with a C<void *>
401 # Pass in a value. Store the value in some static memory and
402 # then return the pointer
413 # pass in the pointer and return the value
419 RETVAL = *(int *)ptr;
425 Similar to T_PTR except that the pointer is stored in a scalar and the
426 reference to that scalar is returned to the caller. This can be used
427 to hide the actual pointer value from the programmer since it is usually
428 not required directly from within perl.
430 The typemap checks that a scalar reference is passed from perl to XS.
434 # Similar test to T_PTR
435 # Pass in a value. Store the value in some static memory and
436 # then return the pointer
447 # pass in the pointer and return the value
461 Similar to T_PTRREF except that the reference is blessed into a class.
462 This allows the pointer to be used as an object. Most commonly used to
463 deal with C structs. The typemap checks that the perl object passed
464 into the XS routine is of the correct class (or part of a subclass).
466 The pointer is blessed into a class that is derived from the name
467 of type of the pointer but with all '*' in the name replaced with
472 # Similar test to T_PTRREF
473 # Pass in a value. Store the value in some static memory and
474 # then return the pointer
485 # pass in the pointer and return the value
487 MODULE = XS::Typemap PACKAGE = intObjPtr
497 MODULE = XS::Typemap PACKAGE = XS::Typemap
505 Similar to T_PTROBJ in that the pointer is blessed into a scalar object.
506 The difference is that when the object is passed back into XS it must be
507 of the correct type (inheritance is not supported).
509 The pointer is blessed into a class that is derived from the name
510 of type of the pointer but with all '*' in the name replaced with
515 # Similar test to T_PTROBJ
516 # Pass in a value. Store the value in some static memory and
517 # then return the pointer
520 T_REF_IV_PTR_OUT( in )
524 RETVAL = &anintrefiv;
528 # pass in the pointer and return the value
530 MODULE = XS::Typemap PACKAGE = intRefIvPtr
533 T_REF_IV_PTR_IN( ptr )
541 MODULE = XS::Typemap PACKAGE = XS::Typemap
557 This can be used to store a pointer in the string component of the
558 SV. Unlike T_PTR which stores the pointer in an IV that can be
559 printed, here the representation of the pointer is irrelevant and the
560 bytes themselves are just stored in the SV. If the pointer is
561 represented by 4 bytes then those 4 bytes are stored in the SV (and
562 length() will report a value of 4). This makes use of the fact that a
563 perl scalar can store arbritray data in its PV component.
565 In principal the unpack() command can be used to convert the pointer
571 T_OPAQUEPTR_IN( val )
580 T_OPAQUEPTR_OUT( ptr )
588 T_OPAQUEPTR_OUT_short( ptr )
597 This can be used to store pointers to non-pointer types in an SV. It
598 is similar to T_OPAQUEPTR except that the typemap retrieves the
599 pointer itself rather than assuming that it is to be given a
600 pointer. This approach hides the pointer as a byte stream in the
601 string part of the SV rather than making the actual pointer value
604 There is no reason to use T_OPAQUE to pass the data to C. Use
605 T_OPAQUEPTR to do that since once the pointer is stored in the SV
606 T_OPAQUE and T_OPAQUEPTR are identical.
614 RETVAL = (shortOPQ)val;
620 xsubpp supports a special syntax for returning
621 packed C arrays to perl. If the XS return type is given as
625 xsubpp will copy the contents of C<nelem * sizeof(type)> bytes from
626 RETVAL to an SV and push it onto the stack. This is only really useful
627 if the number of items to be returned is known at compile time and you
628 don't mind having a string of bytes in your SV. Use T_ARRAY to push a
629 variable number of arguments onto the return stack (they won't be
630 packed as a single string though).
632 This is similar to using T_OPAQUEPTR but can be used to process more than
638 T_OPAQUE_array( a,b,c)
671 This is used to convert the perl argument list to a C array
672 and for pushing the contents of a C array onto the perl
675 The usual calling signature is
677 @out = array_func( @in );
679 Any number of arguments can occur in the list before the array but
680 the input and output arrays must be the last elements in the list.
682 When used to pass a perl list to C the XS writer must provide a
683 function (named after the array type but with 'Ptr' substituted for
684 '*') to allocate the memory required to hold the list. A pointer
685 should be returned. It is up to the XS writer to free the memory on
686 exit from the function. The variable C<ix_$var> is set to the number
687 of elements in the new array.
689 When returning a C array to Perl the XS writer must provide an integer
690 variable called C<size_$var> containing the number of elements in the
691 array. This is used to determine how many elements should be pushed
692 onto the return argument stack. This is not required on input since
693 Perl knows how many arguments are on the stack when the routine is
694 called. Ordinarily this variable would be called C<size_RETVAL>.
696 Additionally, the type of each element is determined from the type of
697 the array. If the array uses type C<intArray *> xsubpp will
698 automatically work out that it contains variables of type C<int> and
699 use that typemap entry to perform the copy of each element. All
700 pointer '*' and 'Array' tags are removed from the name to determine
705 # Test passes in an integer array and returns it along with
706 # the number of elements
707 # Pass in a dummy value to test offsetting
709 # Problem is that xsubpp does XSRETURN(1) because we arent
710 # using PPCODE. This means that only the first element
711 # is returned. KLUGE this by using CLEANUP to return before the
715 T_ARRAY( dummy, array, ... )
721 size_RETVAL = ix_array;
727 XSRETURN(size_RETVAL);
732 This is used for passing perl filehandles to and from C using
733 C<FILE *> structures.
741 RETVAL = xsfopen( file );
746 T_STDIO_close( stream )
749 RETVAL = xsfclose( stream );
754 T_STDIO_print( stream, string )
758 RETVAL = xsfprintf( stream, string );
769 This is used for passing perl filehandles to and from C using
770 C<PerlIO *> structures. The file handle can used for reading and
773 See L<perliol> for more information on the Perl IO abstraction
774 layer. Perl must have been built with C<-Duseperlio>.