2 XS code to test the typemap entries
4 Copyright (C) 2001 Tim Jenness.
9 #define PERL_NO_GET_CONTEXT
11 #include "EXTERN.h" /* std perl include */
12 #include "perl.h" /* std perl include */
13 #include "XSUB.h" /* XSUB include */
15 /* Prototypes for external functions */
16 FILE * xsfopen( const char * );
17 int xsfclose( FILE * );
18 int xsfprintf( FILE *, const char *);
20 /* Type definitions required for the XS typemaps */
21 typedef SV * SVREF; /* T_SVREF */
22 typedef int SysRet; /* T_SYSRET */
23 typedef int Int; /* T_INT */
24 typedef int intRef; /* T_PTRREF */
25 typedef int intObj; /* T_PTROBJ */
26 typedef int intRefIv; /* T_REF_IV_PTR */
27 typedef int intArray; /* T_ARRAY */
28 typedef short shortOPQ; /* T_OPAQUE */
29 typedef int intOpq; /* T_OPAQUEPTR */
31 /* A structure to test T_OPAQUEPTR */
38 typedef struct t_opaqueptr astruct;
40 /* Some static memory for the tests */
42 static intRef xst_anintref;
43 static intObj xst_anintobj;
44 static intRefIv xst_anintrefiv;
45 static intOpq xst_anintopq;
47 /* A different type to refer to for testing the different
48 * AV*, HV*, etc typemaps */
52 typedef SVREF SVREF_FIXED;
54 /* Helper functions */
56 /* T_ARRAY - allocate some memory */
57 intArray * intArrayPtr( int nelem ) {
59 Newx(array, nelem, intArray);
64 MODULE = XS::Typemap PACKAGE = XS::Typemap
70 Each C type is represented by an entry in the typemap file that
71 is responsible for converting perl variables (SV, AV, HV and CV) to
78 This simply passes the C representation of the Perl variable (an SV*)
79 in and out of the XS layer. This can be used if the C code wants
80 to deal directly with the Perl variable.
88 /* create a new sv for return that is a copy of the input
89 do not simply copy the pointer since the SV will be marked
90 mortal by the INPUT typemap when it is pushed back onto the stack */
91 RETVAL = sv_mortalcopy( sv );
92 /* increment the refcount since the default INPUT typemap mortalizes
93 by default and we don't want to decrement the ref count twice
101 Used to pass in and return a reference to an SV.
103 Note that this typemap does not decrement the reference count
104 when returning the reference to an SV*.
105 See also: T_SVREF_REFCOUNT_FIXED
119 Used to pass in and return a reference to an SV.
121 variant of T_SVREF that decrements the refcount appropriately
122 when returning a reference to an SV*. Introduced in perl 5.15.4.
127 T_SVREF_REFCOUNT_FIXED( svref )
137 From the perl level this is a reference to a perl array.
138 From the C level this is a pointer to an AV.
140 Note that this typemap does not decrement the reference count
141 when returning an AV*. See also: T_AVREF_REFCOUNT_FIXED
153 =item T_AVREF_REFCOUNT_FIXED
155 From the perl level this is a reference to a perl array.
156 From the C level this is a pointer to an AV. This is a fixed
157 variant of T_AVREF that decrements the refcount appropriately
158 when returning an AV*. Introduced in perl 5.15.4.
163 T_AVREF_REFCOUNT_FIXED( av )
173 From the perl level this is a reference to a perl hash.
174 From the C level this is a pointer to an HV.
176 Note that this typemap does not decrement the reference count
177 when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
189 =item T_HVREF_REFCOUNT_FIXED
191 From the perl level this is a reference to a perl hash.
192 From the C level this is a pointer to an HV. This is a fixed
193 variant of T_HVREF that decrements the refcount appropriately
194 when returning an HV*. Introduced in perl 5.15.4.
199 T_HVREF_REFCOUNT_FIXED( hv )
210 From the perl level this is a reference to a perl subroutine
211 (e.g. $sub = sub { 1 };). From the C level this is a pointer
214 Note that this typemap does not decrement the reference count
215 when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
227 =item T_CVREF_REFCOUNT_FIXED
229 From the perl level this is a reference to a perl subroutine
230 (e.g. $sub = sub { 1 };). From the C level this is a pointer
234 variant of T_HVREF that decrements the refcount appropriately
235 when returning an HV*. Introduced in perl 5.15.4.
240 T_CVREF_REFCOUNT_FIXED( cv )
250 The T_SYSRET typemap is used to process return values from system calls.
251 It is only meaningful when passing values from C to perl (there is
252 no concept of passing a system return value from Perl to C).
254 System calls return -1 on error (setting ERRNO with the reason)
255 and (usually) 0 on success. If the return value is -1 this typemap
256 returns C<undef>. If the return value is not -1, this typemap
257 translates a 0 (perl false) to "0 but true" (which
258 is perl true) or returns the value itself, to indicate that the
261 The L<POSIX|POSIX> module makes extensive use of this type.
265 # Test a successful return
299 A signed integer. This is cast to the required integer type when
300 passed to C and converted to an IV when passed back to Perl.
314 A signed integer. This typemap converts the Perl value to a native
315 integer type (the C<int> type on the current platform). When returning
316 the value to perl it is processed in the same way as for T_IV.
318 Its behaviour is identical to using an C<int> type in XS with T_IV.
322 An enum value. Used to transfer an enum component
323 from C. There is no reason to pass an enum value to C since
324 it is stored as an IV inside perl.
328 # The test should return the value for SVt_PVHV.
329 # 11 at the present time but we can't not rely on this
330 # for testing purposes.
341 A boolean type. This can be used to pass true and false values to and
356 This is for unsigned integers. It is equivalent to using T_UV
357 but explicitly casts the variable to type C<unsigned int>.
358 The default type for C<unsigned int> is T_UV.
362 Short integers. This is equivalent to T_IV but explicitly casts
363 the return to type C<short>. The default typemap for C<short>
368 Unsigned short integers. This is equivalent to T_UV but explicitly
369 casts the return to type C<unsigned short>. The default typemap for
370 C<unsigned short> is T_UV.
372 T_U_SHORT is used for type C<U16> in the standard typemap.
387 Long integers. This is equivalent to T_IV but explicitly casts
388 the return to type C<long>. The default typemap for C<long>
393 Unsigned long integers. This is equivalent to T_UV but explicitly
394 casts the return to type C<unsigned long>. The default typemap for
395 C<unsigned long> is T_UV.
397 T_U_LONG is used for type C<U32> in the standard typemap.
411 Single 8-bit characters.
441 A floating point number. This typemap guarantees to return a variable
456 A Perl floating point number. Similar to T_IV and T_UV in that the
457 return type is cast to the requested numeric type rather than
472 A double precision floating point number. This typemap guarantees to
473 return a variable cast to a C<double>.
501 A memory address (pointer). Typically associated with a C<void *>
506 # Pass in a value. Store the value in some static memory and
507 # then return the pointer
518 # pass in the pointer and return the value
524 RETVAL = *(int *)ptr;
530 Similar to T_PTR except that the pointer is stored in a scalar and the
531 reference to that scalar is returned to the caller. This can be used
532 to hide the actual pointer value from the programmer since it is usually
533 not required directly from within perl.
535 The typemap checks that a scalar reference is passed from perl to XS.
539 # Similar test to T_PTR
540 # Pass in a value. Store the value in some static memory and
541 # then return the pointer
548 RETVAL = &xst_anintref;
552 # pass in the pointer and return the value
566 Similar to T_PTRREF except that the reference is blessed into a class.
567 This allows the pointer to be used as an object. Most commonly used to
568 deal with C structs. The typemap checks that the perl object passed
569 into the XS routine is of the correct class (or part of a subclass).
571 The pointer is blessed into a class that is derived from the name
572 of type of the pointer but with all '*' in the name replaced with
577 # Similar test to T_PTRREF
578 # Pass in a value. Store the value in some static memory and
579 # then return the pointer
586 RETVAL = &xst_anintobj;
590 # pass in the pointer and return the value
592 MODULE = XS::Typemap PACKAGE = intObjPtr
602 MODULE = XS::Typemap PACKAGE = XS::Typemap
610 Similar to T_PTROBJ in that the pointer is blessed into a scalar object.
611 The difference is that when the object is passed back into XS it must be
612 of the correct type (inheritance is not supported).
614 The pointer is blessed into a class that is derived from the name
615 of type of the pointer but with all '*' in the name replaced with
620 # Similar test to T_PTROBJ
621 # Pass in a value. Store the value in some static memory and
622 # then return the pointer
625 T_REF_IV_PTR_OUT( in )
629 RETVAL = &xst_anintrefiv;
633 # pass in the pointer and return the value
635 MODULE = XS::Typemap PACKAGE = intRefIvPtr
638 T_REF_IV_PTR_IN( ptr )
646 MODULE = XS::Typemap PACKAGE = XS::Typemap
662 This can be used to store bytes in the string component of the
663 SV. Here the representation of the data is irrelevant to perl and the
664 bytes themselves are just stored in the SV. It is assumed that the C
665 variable is a pointer (the bytes are copied from that memory
666 location). If the pointer is pointing to something that is
667 represented by 8 bytes then those 8 bytes are stored in the SV (and
668 length() will report a value of 8). This entry is similar to T_OPAQUE.
670 In principal the unpack() command can be used to convert the bytes
671 back to a number (if the underlying type is known to be a number).
673 This entry can be used to store a C structure (the number
674 of bytes to be copied is calculated using the C C<sizeof> function)
675 and can be used as an alternative to T_PTRREF without having to worry
676 about a memory leak (since Perl will clean up the SV).
681 T_OPAQUEPTR_IN( val )
685 RETVAL = &xst_anintopq;
690 T_OPAQUEPTR_OUT( ptr )
698 T_OPAQUEPTR_OUT_short( ptr )
705 # Test it with a structure
707 T_OPAQUEPTR_IN_struct( a,b,c )
712 struct t_opaqueptr test;
722 T_OPAQUEPTR_OUT_struct( test )
725 XPUSHs(sv_2mortal(newSViv(test->a)));
726 XPUSHs(sv_2mortal(newSViv(test->b)));
727 XPUSHs(sv_2mortal(newSVnv(test->c)));
732 This can be used to store data from non-pointer types in the string
733 part of an SV. It is similar to T_OPAQUEPTR except that the
734 typemap retrieves the pointer directly rather than assuming it
735 is being supplied. For example if an integer is imported into
736 Perl using T_OPAQUE rather than T_IV the underlying bytes representing
737 the integer will be stored in the SV but the actual integer value will not
738 be available. i.e. The data is opaque to perl.
740 The data may be retrieved using the C<unpack> function if the
741 underlying type of the byte stream is known.
743 T_OPAQUE supports input and output of simple types.
744 T_OPAQUEPTR can be used to pass these bytes back into C if a pointer
753 RETVAL = (shortOPQ)val;
767 xsubpp supports a special syntax for returning
768 packed C arrays to perl. If the XS return type is given as
772 xsubpp will copy the contents of C<nelem * sizeof(type)> bytes from
773 RETVAL to an SV and push it onto the stack. This is only really useful
774 if the number of items to be returned is known at compile time and you
775 don't mind having a string of bytes in your SV. Use T_ARRAY to push a
776 variable number of arguments onto the return stack (they won't be
777 packed as a single string though).
779 This is similar to using T_OPAQUEPTR but can be used to process more than
785 T_OPAQUE_array( a,b,c)
818 This is used to convert the perl argument list to a C array
819 and for pushing the contents of a C array onto the perl
822 The usual calling signature is
824 @out = array_func( @in );
826 Any number of arguments can occur in the list before the array but
827 the input and output arrays must be the last elements in the list.
829 When used to pass a perl list to C the XS writer must provide a
830 function (named after the array type but with 'Ptr' substituted for
831 '*') to allocate the memory required to hold the list. A pointer
832 should be returned. It is up to the XS writer to free the memory on
833 exit from the function. The variable C<ix_$var> is set to the number
834 of elements in the new array.
836 When returning a C array to Perl the XS writer must provide an integer
837 variable called C<size_$var> containing the number of elements in the
838 array. This is used to determine how many elements should be pushed
839 onto the return argument stack. This is not required on input since
840 Perl knows how many arguments are on the stack when the routine is
841 called. Ordinarily this variable would be called C<size_RETVAL>.
843 Additionally, the type of each element is determined from the type of
844 the array. If the array uses type C<intArray *> xsubpp will
845 automatically work out that it contains variables of type C<int> and
846 use that typemap entry to perform the copy of each element. All
847 pointer '*' and 'Array' tags are removed from the name to determine
852 # Test passes in an integer array and returns it along with
853 # the number of elements
854 # Pass in a dummy value to test offsetting
856 # Problem is that xsubpp does XSRETURN(1) because we arent
857 # using PPCODE. This means that only the first element
858 # is returned. KLUGE this by using CLEANUP to return before the
862 T_ARRAY( dummy, array, ... )
868 dummy += 0; /* Fix -Wall */
869 size_RETVAL = ix_array;
875 XSRETURN(size_RETVAL);
880 This is used for passing perl filehandles to and from C using
881 C<FILE *> structures.
889 RETVAL = xsfopen( file );
900 stream = PerlIO_findFILE( f );
901 /* Release the FILE* from the PerlIO system so that we do
902 not close the file twice */
903 PerlIO_releaseFILE(f,stream);
904 /* Must release the file before closing it */
905 RETVAL = xsfclose( stream );
910 T_STDIO_print( stream, string )
914 RETVAL = xsfprintf( stream, string );
925 This is used for passing perl filehandles to and from C using
926 C<PerlIO *> structures. The file handle can used for reading and
929 See L<perliol> for more information on the Perl IO abstraction
930 layer. Perl must have been built with C<-Duseperlio>.