1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
|
/*
XS code to test the typemap entries
Copyright (C) 2001 Tim Jenness.
All Rights Reserved
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h" /* std perl include */
#include "perl.h" /* std perl include */
#include "XSUB.h" /* XSUB include */
/* Prototypes for external functions */
FILE * xsfopen( const char * );
int xsfclose( FILE * );
int xsfprintf( FILE *, const char *);
/* Type definitions required for the XS typemaps */
typedef SV * SVREF; /* T_SVREF */
typedef int SysRet; /* T_SYSRET */
typedef int Int; /* T_INT */
typedef int intRef; /* T_PTRREF */
typedef int intObj; /* T_PTROBJ */
typedef int intRefIv; /* T_REF_IV_PTR */
typedef int intArray; /* T_ARRAY */
typedef short shortOPQ; /* T_OPAQUE */
typedef int intOpq; /* T_OPAQUEPTR */
/* A structure to test T_OPAQUEPTR */
struct t_opaqueptr {
int a;
int b;
double c;
};
typedef struct t_opaqueptr astruct;
/* Some static memory for the tests */
static I32 xst_anint;
static intRef xst_anintref;
static intObj xst_anintobj;
static intRefIv xst_anintrefiv;
static intOpq xst_anintopq;
/* Helper functions */
/* T_ARRAY - allocate some memory */
intArray * intArrayPtr( int nelem ) {
intArray * array;
Newx(array, nelem, intArray);
return array;
}
MODULE = XS::Typemap PACKAGE = XS::Typemap
PROTOTYPES: DISABLE
=head1 TYPEMAPS
Each C type is represented by an entry in the typemap file that
is responsible for converting perl variables (SV, AV, HV and CV) to
and from that type.
=over 4
=item T_SV
This simply passes the C representation of the Perl variable (an SV*)
in and out of the XS layer. This can be used if the C code wants
to deal directly with the Perl variable.
=cut
SV *
T_SV( sv )
SV * sv
CODE:
/* create a new sv for return that is a copy of the input
do not simply copy the pointer since the SV will be marked
mortal by the INPUT typemap when it is pushed back onto the stack */
RETVAL = sv_mortalcopy( sv );
/* increment the refcount since the default INPUT typemap mortalizes
by default and we don't want to decrement the ref count twice
by mistake */
SvREFCNT_inc(RETVAL);
OUTPUT:
RETVAL
=item T_SVREF
Used to pass in and return a reference to an SV.
=cut
SVREF
T_SVREF( svref )
SVREF svref
CODE:
RETVAL = svref;
OUTPUT:
RETVAL
=item T_AVREF
From the perl level this is a reference to a perl array.
From the C level this is a pointer to an AV.
=cut
AV *
T_AVREF( av )
AV * av
CODE:
RETVAL = av;
OUTPUT:
RETVAL
=item T_HVREF
From the perl level this is a reference to a perl hash.
From the C level this is a pointer to an HV.
=cut
HV *
T_HVREF( hv )
HV * hv
CODE:
RETVAL = hv;
OUTPUT:
RETVAL
=item T_CVREF
From the perl level this is a reference to a perl subroutine
(e.g. $sub = sub { 1 };). From the C level this is a pointer
to a CV.
=cut
CV *
T_CVREF( cv )
CV * cv
CODE:
RETVAL = cv;
OUTPUT:
RETVAL
=item T_SYSRET
The T_SYSRET typemap is used to process return values from system calls.
It is only meaningful when passing values from C to perl (there is
no concept of passing a system return value from Perl to C).
System calls return -1 on error (setting ERRNO with the reason)
and (usually) 0 on success. If the return value is -1 this typemap
returns C<undef>. If the return value is not -1, this typemap
translates a 0 (perl false) to "0 but true" (which
is perl true) or returns the value itself, to indicate that the
command succeeded.
The L<POSIX|POSIX> module makes extensive use of this type.
=cut
# Test a successful return
SysRet
T_SYSRET_pass()
CODE:
RETVAL = 0;
OUTPUT:
RETVAL
# Test failure
SysRet
T_SYSRET_fail()
CODE:
RETVAL = -1;
OUTPUT:
RETVAL
=item T_UV
An unsigned integer.
=cut
unsigned int
T_UV( uv )
unsigned int uv
CODE:
RETVAL = uv;
OUTPUT:
RETVAL
=item T_IV
A signed integer. This is cast to the required integer type when
passed to C and converted to an IV when passed back to Perl.
=cut
long
T_IV( iv )
long iv
CODE:
RETVAL = iv;
OUTPUT:
RETVAL
=item T_INT
A signed integer. This typemap converts the Perl value to a native
integer type (the C<int> type on the current platform). When returning
the value to perl it is processed in the same way as for T_IV.
Its behaviour is identical to using an C<int> type in XS with T_IV.
=item T_ENUM
An enum value. Used to transfer an enum component
from C. There is no reason to pass an enum value to C since
it is stored as an IV inside perl.
=cut
# The test should return the value for SVt_PVHV.
# 11 at the present time but we can't not rely on this
# for testing purposes.
svtype
T_ENUM()
CODE:
RETVAL = SVt_PVHV;
OUTPUT:
RETVAL
=item T_BOOL
A boolean type. This can be used to pass true and false values to and
from C.
=cut
bool
T_BOOL( in )
bool in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_U_INT
This is for unsigned integers. It is equivalent to using T_UV
but explicitly casts the variable to type C<unsigned int>.
The default type for C<unsigned int> is T_UV.
=item T_SHORT
Short integers. This is equivalent to T_IV but explicitly casts
the return to type C<short>. The default typemap for C<short>
is T_IV.
=item T_U_SHORT
Unsigned short integers. This is equivalent to T_UV but explicitly
casts the return to type C<unsigned short>. The default typemap for
C<unsigned short> is T_UV.
T_U_SHORT is used for type C<U16> in the standard typemap.
=cut
U16
T_U_SHORT( in )
U16 in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_LONG
Long integers. This is equivalent to T_IV but explicitly casts
the return to type C<long>. The default typemap for C<long>
is T_IV.
=item T_U_LONG
Unsigned long integers. This is equivalent to T_UV but explicitly
casts the return to type C<unsigned long>. The default typemap for
C<unsigned long> is T_UV.
T_U_LONG is used for type C<U32> in the standard typemap.
=cut
U32
T_U_LONG( in )
U32 in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_CHAR
Single 8-bit characters.
=cut
char
T_CHAR( in );
char in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_U_CHAR
An unsigned byte.
=cut
unsigned char
T_U_CHAR( in );
unsigned char in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_FLOAT
A floating point number. This typemap guarantees to return a variable
cast to a C<float>.
=cut
float
T_FLOAT( in )
float in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_NV
A Perl floating point number. Similar to T_IV and T_UV in that the
return type is cast to the requested numeric type rather than
to a specific type.
=cut
NV
T_NV( in )
NV in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_DOUBLE
A double precision floating point number. This typemap guarantees to
return a variable cast to a C<double>.
=cut
double
T_DOUBLE( in )
double in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_PV
A string (char *).
=cut
char *
T_PV( in )
char * in
CODE:
RETVAL = in;
OUTPUT:
RETVAL
=item T_PTR
A memory address (pointer). Typically associated with a C<void *>
type.
=cut
# Pass in a value. Store the value in some static memory and
# then return the pointer
void *
T_PTR_OUT( in )
int in;
CODE:
xst_anint = in;
RETVAL = &xst_anint;
OUTPUT:
RETVAL
# pass in the pointer and return the value
int
T_PTR_IN( ptr )
void * ptr
CODE:
RETVAL = *(int *)ptr;
OUTPUT:
RETVAL
=item T_PTRREF
Similar to T_PTR except that the pointer is stored in a scalar and the
reference to that scalar is returned to the caller. This can be used
to hide the actual pointer value from the programmer since it is usually
not required directly from within perl.
The typemap checks that a scalar reference is passed from perl to XS.
=cut
# Similar test to T_PTR
# Pass in a value. Store the value in some static memory and
# then return the pointer
intRef *
T_PTRREF_OUT( in )
intRef in;
CODE:
xst_anintref = in;
RETVAL = &xst_anintref;
OUTPUT:
RETVAL
# pass in the pointer and return the value
intRef
T_PTRREF_IN( ptr )
intRef * ptr
CODE:
RETVAL = *ptr;
OUTPUT:
RETVAL
=item T_PTROBJ
Similar to T_PTRREF except that the reference is blessed into a class.
This allows the pointer to be used as an object. Most commonly used to
deal with C structs. The typemap checks that the perl object passed
into the XS routine is of the correct class (or part of a subclass).
The pointer is blessed into a class that is derived from the name
of type of the pointer but with all '*' in the name replaced with
'Ptr'.
=cut
# Similar test to T_PTRREF
# Pass in a value. Store the value in some static memory and
# then return the pointer
intObj *
T_PTROBJ_OUT( in )
intObj in;
CODE:
xst_anintobj = in;
RETVAL = &xst_anintobj;
OUTPUT:
RETVAL
# pass in the pointer and return the value
MODULE = XS::Typemap PACKAGE = intObjPtr
intObj
T_PTROBJ_IN( ptr )
intObj * ptr
CODE:
RETVAL = *ptr;
OUTPUT:
RETVAL
MODULE = XS::Typemap PACKAGE = XS::Typemap
=item T_REF_IV_REF
NOT YET
=item T_REF_IV_PTR
Similar to T_PTROBJ in that the pointer is blessed into a scalar object.
The difference is that when the object is passed back into XS it must be
of the correct type (inheritance is not supported).
The pointer is blessed into a class that is derived from the name
of type of the pointer but with all '*' in the name replaced with
'Ptr'.
=cut
# Similar test to T_PTROBJ
# Pass in a value. Store the value in some static memory and
# then return the pointer
intRefIv *
T_REF_IV_PTR_OUT( in )
intRefIv in;
CODE:
xst_anintrefiv = in;
RETVAL = &xst_anintrefiv;
OUTPUT:
RETVAL
# pass in the pointer and return the value
MODULE = XS::Typemap PACKAGE = intRefIvPtr
intRefIv
T_REF_IV_PTR_IN( ptr )
intRefIv * ptr
CODE:
RETVAL = *ptr;
OUTPUT:
RETVAL
MODULE = XS::Typemap PACKAGE = XS::Typemap
=item T_PTRDESC
NOT YET
=item T_REFREF
NOT YET
=item T_REFOBJ
NOT YET
=item T_OPAQUEPTR
This can be used to store bytes in the string component of the
SV. Here the representation of the data is irrelevant to perl and the
bytes themselves are just stored in the SV. It is assumed that the C
variable is a pointer (the bytes are copied from that memory
location). If the pointer is pointing to something that is
represented by 8 bytes then those 8 bytes are stored in the SV (and
length() will report a value of 8). This entry is similar to T_OPAQUE.
In principal the unpack() command can be used to convert the bytes
back to a number (if the underlying type is known to be a number).
This entry can be used to store a C structure (the number
of bytes to be copied is calculated using the C C<sizeof> function)
and can be used as an alternative to T_PTRREF without having to worry
about a memory leak (since Perl will clean up the SV).
=cut
intOpq *
T_OPAQUEPTR_IN( val )
intOpq val
CODE:
xst_anintopq = val;
RETVAL = &xst_anintopq;
OUTPUT:
RETVAL
intOpq
T_OPAQUEPTR_OUT( ptr )
intOpq * ptr
CODE:
RETVAL = *ptr;
OUTPUT:
RETVAL
short
T_OPAQUEPTR_OUT_short( ptr )
shortOPQ * ptr
CODE:
RETVAL = *ptr;
OUTPUT:
RETVAL
# Test it with a structure
astruct *
T_OPAQUEPTR_IN_struct( a,b,c )
int a
int b
double c
PREINIT:
struct t_opaqueptr test;
CODE:
test.a = a;
test.b = b;
test.c = c;
RETVAL = &test;
OUTPUT:
RETVAL
void
T_OPAQUEPTR_OUT_struct( test )
astruct * test
PPCODE:
XPUSHs(sv_2mortal(newSViv(test->a)));
XPUSHs(sv_2mortal(newSViv(test->b)));
XPUSHs(sv_2mortal(newSVnv(test->c)));
=item T_OPAQUE
This can be used to store data from non-pointer types in the string
part of an SV. It is similar to T_OPAQUEPTR except that the
typemap retrieves the pointer directly rather than assuming it
is being supplied. For example if an integer is imported into
Perl using T_OPAQUE rather than T_IV the underlying bytes representing
the integer will be stored in the SV but the actual integer value will not
be available. i.e. The data is opaque to perl.
The data may be retrieved using the C<unpack> function if the
underlying type of the byte stream is known.
T_OPAQUE supports input and output of simple types.
T_OPAQUEPTR can be used to pass these bytes back into C if a pointer
is acceptable.
=cut
shortOPQ
T_OPAQUE_IN( val )
int val
CODE:
RETVAL = (shortOPQ)val;
OUTPUT:
RETVAL
IV
T_OPAQUE_OUT( val )
shortOPQ val
CODE:
RETVAL = (IV)val;
OUTPUT:
RETVAL
=item Implicit array
xsubpp supports a special syntax for returning
packed C arrays to perl. If the XS return type is given as
array(type, nelem)
xsubpp will copy the contents of C<nelem * sizeof(type)> bytes from
RETVAL to an SV and push it onto the stack. This is only really useful
if the number of items to be returned is known at compile time and you
don't mind having a string of bytes in your SV. Use T_ARRAY to push a
variable number of arguments onto the return stack (they won't be
packed as a single string though).
This is similar to using T_OPAQUEPTR but can be used to process more than
one element.
=cut
array(int,3)
T_OPAQUE_array( a,b,c)
int a
int b
int c
PREINIT:
int array[3];
CODE:
array[0] = a;
array[1] = b;
array[2] = c;
RETVAL = array;
OUTPUT:
RETVAL
=item T_PACKED
NOT YET
=item T_PACKEDARRAY
NOT YET
=item T_DATAUNIT
NOT YET
=item T_CALLBACK
NOT YET
=item T_ARRAY
This is used to convert the perl argument list to a C array
and for pushing the contents of a C array onto the perl
argument stack.
The usual calling signature is
@out = array_func( @in );
Any number of arguments can occur in the list before the array but
the input and output arrays must be the last elements in the list.
When used to pass a perl list to C the XS writer must provide a
function (named after the array type but with 'Ptr' substituted for
'*') to allocate the memory required to hold the list. A pointer
should be returned. It is up to the XS writer to free the memory on
exit from the function. The variable C<ix_$var> is set to the number
of elements in the new array.
When returning a C array to Perl the XS writer must provide an integer
variable called C<size_$var> containing the number of elements in the
array. This is used to determine how many elements should be pushed
onto the return argument stack. This is not required on input since
Perl knows how many arguments are on the stack when the routine is
called. Ordinarily this variable would be called C<size_RETVAL>.
Additionally, the type of each element is determined from the type of
the array. If the array uses type C<intArray *> xsubpp will
automatically work out that it contains variables of type C<int> and
use that typemap entry to perform the copy of each element. All
pointer '*' and 'Array' tags are removed from the name to determine
the subtype.
=cut
# Test passes in an integer array and returns it along with
# the number of elements
# Pass in a dummy value to test offsetting
# Problem is that xsubpp does XSRETURN(1) because we arent
# using PPCODE. This means that only the first element
# is returned. KLUGE this by using CLEANUP to return before the
# end.
intArray *
T_ARRAY( dummy, array, ... )
int dummy = 0;
intArray * array
PREINIT:
U32 size_RETVAL;
CODE:
dummy += 0; /* Fix -Wall */
size_RETVAL = ix_array;
RETVAL = array;
OUTPUT:
RETVAL
CLEANUP:
Safefree(array);
XSRETURN(size_RETVAL);
=item T_STDIO
This is used for passing perl filehandles to and from C using
C<FILE *> structures.
=cut
FILE *
T_STDIO_open( file )
const char * file
CODE:
RETVAL = xsfopen( file );
OUTPUT:
RETVAL
SysRet
T_STDIO_close( f )
PerlIO * f
PREINIT:
FILE * stream;
CODE:
/* Get the FILE* */
stream = PerlIO_findFILE( f );
/* Release the FILE* from the PerlIO system so that we do
not close the file twice */
PerlIO_releaseFILE(f,stream);
/* Must release the file before closing it */
RETVAL = xsfclose( stream );
OUTPUT:
RETVAL
int
T_STDIO_print( stream, string )
FILE * stream
const char * string
CODE:
RETVAL = xsfprintf( stream, string );
OUTPUT:
RETVAL
=item T_IN
NOT YET
=item T_INOUT
This is used for passing perl filehandles to and from C using
C<PerlIO *> structures. The file handle can used for reading and
writing.
See L<perliol> for more information on the Perl IO abstraction
layer. Perl must have been built with C<-Duseperlio>.
=item T_OUT
NOT YET
=back
=cut
|