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 /* Helper functions */
49 /* T_ARRAY - allocate some memory */
50 intArray * intArrayPtr( int nelem ) {
52 Newx(array, nelem, intArray);
57 MODULE = XS::Typemap PACKAGE = XS::Typemap
63 Each C type is represented by an entry in the typemap file that
64 is responsible for converting perl variables (SV, AV, HV and CV) to
71 This simply passes the C representation of the Perl variable (an SV*)
72 in and out of the XS layer. This can be used if the C code wants
73 to deal directly with the Perl variable.
81 /* create a new sv for return that is a copy of the input
82 do not simply copy the pointer since the SV will be marked
83 mortal by the INPUT typemap when it is pushed back onto the stack */
84 RETVAL = sv_mortalcopy( sv );
85 /* increment the refcount since the default INPUT typemap mortalizes
86 by default and we don't want to decrement the ref count twice
94 Used to pass in and return a reference to an SV.
108 From the perl level this is a reference to a perl array.
109 From the C level this is a pointer to an AV.
123 From the perl level this is a reference to a perl hash.
124 From the C level this is a pointer to an HV.
138 From the perl level this is a reference to a perl subroutine
139 (e.g. $sub = sub { 1 };). From the C level this is a pointer
155 The T_SYSRET typemap is used to process return values from system calls.
156 It is only meaningful when passing values from C to perl (there is
157 no concept of passing a system return value from Perl to C).
159 System calls return -1 on error (setting ERRNO with the reason)
160 and (usually) 0 on success. If the return value is -1 this typemap
161 returns C<undef>. If the return value is not -1, this typemap
162 translates a 0 (perl false) to "0 but true" (which
163 is perl true) or returns the value itself, to indicate that the
166 The L<POSIX|POSIX> module makes extensive use of this type.
170 # Test a successful return
204 A signed integer. This is cast to the required integer type when
205 passed to C and converted to an IV when passed back to Perl.
219 A signed integer. This typemap converts the Perl value to a native
220 integer type (the C<int> type on the current platform). When returning
221 the value to perl it is processed in the same way as for T_IV.
223 Its behaviour is identical to using an C<int> type in XS with T_IV.
227 An enum value. Used to transfer an enum component
228 from C. There is no reason to pass an enum value to C since
229 it is stored as an IV inside perl.
233 # The test should return the value for SVt_PVHV.
234 # 11 at the present time but we can't not rely on this
235 # for testing purposes.
246 A boolean type. This can be used to pass true and false values to and
261 This is for unsigned integers. It is equivalent to using T_UV
262 but explicitly casts the variable to type C<unsigned int>.
263 The default type for C<unsigned int> is T_UV.
267 Short integers. This is equivalent to T_IV but explicitly casts
268 the return to type C<short>. The default typemap for C<short>
273 Unsigned short integers. This is equivalent to T_UV but explicitly
274 casts the return to type C<unsigned short>. The default typemap for
275 C<unsigned short> is T_UV.
277 T_U_SHORT is used for type C<U16> in the standard typemap.
292 Long integers. This is equivalent to T_IV but explicitly casts
293 the return to type C<long>. The default typemap for C<long>
298 Unsigned long integers. This is equivalent to T_UV but explicitly
299 casts the return to type C<unsigned long>. The default typemap for
300 C<unsigned long> is T_UV.
302 T_U_LONG is used for type C<U32> in the standard typemap.
316 Single 8-bit characters.
346 A floating point number. This typemap guarantees to return a variable
361 A Perl floating point number. Similar to T_IV and T_UV in that the
362 return type is cast to the requested numeric type rather than
377 A double precision floating point number. This typemap guarantees to
378 return a variable cast to a C<double>.
406 A memory address (pointer). Typically associated with a C<void *>
411 # Pass in a value. Store the value in some static memory and
412 # then return the pointer
423 # pass in the pointer and return the value
429 RETVAL = *(int *)ptr;
435 Similar to T_PTR except that the pointer is stored in a scalar and the
436 reference to that scalar is returned to the caller. This can be used
437 to hide the actual pointer value from the programmer since it is usually
438 not required directly from within perl.
440 The typemap checks that a scalar reference is passed from perl to XS.
444 # Similar test to T_PTR
445 # Pass in a value. Store the value in some static memory and
446 # then return the pointer
453 RETVAL = &xst_anintref;
457 # pass in the pointer and return the value
471 Similar to T_PTRREF except that the reference is blessed into a class.
472 This allows the pointer to be used as an object. Most commonly used to
473 deal with C structs. The typemap checks that the perl object passed
474 into the XS routine is of the correct class (or part of a subclass).
476 The pointer is blessed into a class that is derived from the name
477 of type of the pointer but with all '*' in the name replaced with
482 # Similar test to T_PTRREF
483 # Pass in a value. Store the value in some static memory and
484 # then return the pointer
491 RETVAL = &xst_anintobj;
495 # pass in the pointer and return the value
497 MODULE = XS::Typemap PACKAGE = intObjPtr
507 MODULE = XS::Typemap PACKAGE = XS::Typemap
515 Similar to T_PTROBJ in that the pointer is blessed into a scalar object.
516 The difference is that when the object is passed back into XS it must be
517 of the correct type (inheritance is not supported).
519 The pointer is blessed into a class that is derived from the name
520 of type of the pointer but with all '*' in the name replaced with
525 # Similar test to T_PTROBJ
526 # Pass in a value. Store the value in some static memory and
527 # then return the pointer
530 T_REF_IV_PTR_OUT( in )
534 RETVAL = &xst_anintrefiv;
538 # pass in the pointer and return the value
540 MODULE = XS::Typemap PACKAGE = intRefIvPtr
543 T_REF_IV_PTR_IN( ptr )
551 MODULE = XS::Typemap PACKAGE = XS::Typemap
567 This can be used to store bytes in the string component of the
568 SV. Here the representation of the data is irrelevant to perl and the
569 bytes themselves are just stored in the SV. It is assumed that the C
570 variable is a pointer (the bytes are copied from that memory
571 location). If the pointer is pointing to something that is
572 represented by 8 bytes then those 8 bytes are stored in the SV (and
573 length() will report a value of 8). This entry is similar to T_OPAQUE.
575 In principal the unpack() command can be used to convert the bytes
576 back to a number (if the underlying type is known to be a number).
578 This entry can be used to store a C structure (the number
579 of bytes to be copied is calculated using the C C<sizeof> function)
580 and can be used as an alternative to T_PTRREF without having to worry
581 about a memory leak (since Perl will clean up the SV).
586 T_OPAQUEPTR_IN( val )
590 RETVAL = &xst_anintopq;
595 T_OPAQUEPTR_OUT( ptr )
603 T_OPAQUEPTR_OUT_short( ptr )
610 # Test it with a structure
612 T_OPAQUEPTR_IN_struct( a,b,c )
617 struct t_opaqueptr test;
627 T_OPAQUEPTR_OUT_struct( test )
630 XPUSHs(sv_2mortal(newSViv(test->a)));
631 XPUSHs(sv_2mortal(newSViv(test->b)));
632 XPUSHs(sv_2mortal(newSVnv(test->c)));
637 This can be used to store data from non-pointer types in the string
638 part of an SV. It is similar to T_OPAQUEPTR except that the
639 typemap retrieves the pointer directly rather than assuming it
640 is being supplied. For example if an integer is imported into
641 Perl using T_OPAQUE rather than T_IV the underlying bytes representing
642 the integer will be stored in the SV but the actual integer value will not
643 be available. i.e. The data is opaque to perl.
645 The data may be retrieved using the C<unpack> function if the
646 underlying type of the byte stream is known.
648 T_OPAQUE supports input and output of simple types.
649 T_OPAQUEPTR can be used to pass these bytes back into C if a pointer
658 RETVAL = (shortOPQ)val;
672 xsubpp supports a special syntax for returning
673 packed C arrays to perl. If the XS return type is given as
677 xsubpp will copy the contents of C<nelem * sizeof(type)> bytes from
678 RETVAL to an SV and push it onto the stack. This is only really useful
679 if the number of items to be returned is known at compile time and you
680 don't mind having a string of bytes in your SV. Use T_ARRAY to push a
681 variable number of arguments onto the return stack (they won't be
682 packed as a single string though).
684 This is similar to using T_OPAQUEPTR but can be used to process more than
690 T_OPAQUE_array( a,b,c)
723 This is used to convert the perl argument list to a C array
724 and for pushing the contents of a C array onto the perl
727 The usual calling signature is
729 @out = array_func( @in );
731 Any number of arguments can occur in the list before the array but
732 the input and output arrays must be the last elements in the list.
734 When used to pass a perl list to C the XS writer must provide a
735 function (named after the array type but with 'Ptr' substituted for
736 '*') to allocate the memory required to hold the list. A pointer
737 should be returned. It is up to the XS writer to free the memory on
738 exit from the function. The variable C<ix_$var> is set to the number
739 of elements in the new array.
741 When returning a C array to Perl the XS writer must provide an integer
742 variable called C<size_$var> containing the number of elements in the
743 array. This is used to determine how many elements should be pushed
744 onto the return argument stack. This is not required on input since
745 Perl knows how many arguments are on the stack when the routine is
746 called. Ordinarily this variable would be called C<size_RETVAL>.
748 Additionally, the type of each element is determined from the type of
749 the array. If the array uses type C<intArray *> xsubpp will
750 automatically work out that it contains variables of type C<int> and
751 use that typemap entry to perform the copy of each element. All
752 pointer '*' and 'Array' tags are removed from the name to determine
757 # Test passes in an integer array and returns it along with
758 # the number of elements
759 # Pass in a dummy value to test offsetting
761 # Problem is that xsubpp does XSRETURN(1) because we arent
762 # using PPCODE. This means that only the first element
763 # is returned. KLUGE this by using CLEANUP to return before the
767 T_ARRAY( dummy, array, ... )
773 dummy += 0; /* Fix -Wall */
774 size_RETVAL = ix_array;
780 XSRETURN(size_RETVAL);
785 This is used for passing perl filehandles to and from C using
786 C<FILE *> structures.
794 RETVAL = xsfopen( file );
805 stream = PerlIO_findFILE( f );
806 /* Release the FILE* from the PerlIO system so that we do
807 not close the file twice */
808 PerlIO_releaseFILE(f,stream);
809 /* Must release the file before closing it */
810 RETVAL = xsfclose( stream );
815 T_STDIO_print( stream, string )
819 RETVAL = xsfprintf( stream, string );
830 This is used for passing perl filehandles to and from C using
831 C<PerlIO *> structures. The file handle can used for reading and
834 See L<perliol> for more information on the Perl IO abstraction
835 layer. Perl must have been built with C<-Duseperlio>.