Skip to content

Commit 6a7025f

Browse files
committed
perlxs.pod: update OVERLOAD, add T_PTROBJ
First, add a new subsection =head3 T_PTROBJ and opaque handles to the TYPEMAPs section explaining how this typemap can be used to map between Perl objects and C library handles. It provides a fully-worked example of wrapping a simple arithmetic library. Then completely rewrite the =head3 The OVERLOAD: Keyword section. In particular, it now refers to the new T_PTROBJ example and shows how it can be extended to use overloading.
1 parent 2864ffc commit 6a7025f

File tree

1 file changed

+303
-39
lines changed

1 file changed

+303
-39
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 303 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,8 @@ result in these declarations in the C code:
21252125

21262126
With the appropriate XS typemap entries and C typedefs, this can be used
21272127
to assist in declaring XSUBs which are passed and return Perl objects.
2128+
See L</T_PTROBJ and opaque handles> for an example of this using
2129+
the common C<T_PTROBJ> typemap type.
21282130

21292131
=head3 XSUB Parameter Placeholders
21302132

@@ -3397,46 +3399,161 @@ prototype string, including the empty prototype.
33973399

33983400
=head3 The OVERLOAD: Keyword
33993401

3400-
Instead of writing an overloaded interface using pure Perl, you
3401-
can also use the OVERLOAD keyword to define additional Perl names
3402-
for your functions (like the ALIAS: keyword above). However, the
3403-
overloaded functions must be defined in such a way as to accept the number
3404-
of parameters supplied by perl's overload system. For most overload
3405-
methods, it will be three parameters; for the C<nomethod> function it will
3406-
be four. However, the bitwise operators C<&>, C<|>, C<^>, and C<~> may be
3407-
called with three I<or> five arguments (see L<overload>).
3408-
3409-
If any
3410-
function has the OVERLOAD: keyword, several additional lines
3411-
will be defined in the c file generated by xsubpp in order to
3412-
register with the overload magic.
3413-
3414-
Since blessed objects are actually stored as RV's, it is useful
3415-
to use the typemap features to preprocess parameters and extract
3416-
the actual SV stored within the blessed RV. See the sample for
3417-
T_PTROBJ_SPECIAL in L<perlxstypemap>.
3418-
3419-
To use the OVERLOAD: keyword, create an XS function which takes
3420-
three input parameters (or use the C-style '...' definition) like
3421-
this:
3402+
MODULE = Foo PACKAGE = Foo::Bar
34223403

3423-
SV *
3424-
cmp (lobj, robj, swap)
3425-
My_Module_obj lobj
3426-
My_Module_obj robj
3427-
IV swap
3428-
OVERLOAD: cmp <=>
3429-
{ /* function defined here */}
3430-
3431-
In this case, the function will overload both of the three way
3432-
comparison operators. For all overload operations using non-alpha
3433-
characters, you must type the parameter without quoting, separating
3434-
multiple overloads with whitespace. Note that "" (the stringify
3435-
overload) should be entered as \"\" (i.e. escaped).
3436-
3437-
Since, as mentioned above, bitwise operators may take extra arguments, you
3438-
may want to use something like C<(lobj, robj, swap, ...)> (with
3439-
literal C<...>) as your parameter list.
3404+
SV*
3405+
subtract(SV* a, SV* b, bool swap)
3406+
OVERLOAD: - -=
3407+
CODE:
3408+
...
3409+
3410+
The C<OVERLOAD> keyword allows you to declare that this XSUB acts as an
3411+
overload method for the specified operators in the current package. The
3412+
example above is approximately equivalent to this Perl code:
3413+
3414+
package Foo::Bar;
3415+
3416+
sub subtract { ... }
3417+
3418+
use overload
3419+
'-' => \&subtract,
3420+
'-=' => \&subtract;
3421+
3422+
The rest of the line following the keyword, plus any further lines until
3423+
the next keyword, are interpreted as a space-separated list of overloaded
3424+
operators. There is no check that they are valid operator names. The names
3425+
and symbols will eventually end up within double-quoted strings
3426+
in the C file, so double-quotes need to be escaped; in particular:
3427+
3428+
OVERLOAD: \"\"
3429+
3430+
This could be regarded as a bug.
3431+
3432+
XSUBs used for overload methods are invoked with the same arguments as
3433+
Perl subroutines would be: for example, an overloaded binary operator will
3434+
trigger a call to the XSUB method with the first argument being an
3435+
overloaded object representing one of the two operands of the binary
3436+
operator; the second being the other operand (which may or may not be an
3437+
object); and third, a swap flag. See L<overload> for the full details
3438+
of how these functions will be called, with what arguments. Note that
3439+
swap can in fact be undef in addition to false, to indicate an assign
3440+
overload such as C<+=>.
3441+
3442+
Bitwise operator methods sometimes take extra arguments: in
3443+
particular under C<use feature 'bitwise'>. So you may want to use an
3444+
ellipsis (something like C<(lobj, robj, swap, ...)>) to skip them.
3445+
3446+
The net effect of the C<OVERLOAD> keyword is to add some extra code to
3447+
the boot XSUB to register this XSUB as the handler for the specified
3448+
overload actions, in the same way that C<use overload> does for Perl
3449+
methods.
3450+
3451+
See also the file-scoped L<FALLBACK|/The FALLBACK: Keyword> keyword for
3452+
details of how to set the fallback behaviour for the current package.
3453+
3454+
Note that C<OVERLOAD> shouldn't be mixed with the L<ALIAS|/The ALIAS:
3455+
Keyword> keyword; the value of C<ix> will be undefined for any overload
3456+
method call.
3457+
3458+
The L</T_PTROBJ and opaque handles> section contains a fully-worked
3459+
example of using the C<T_PTROBJ> typemap to wrap a simple arithmetic
3460+
library. The result of that wrapper allows you to write Perl code such as
3461+
3462+
my $i2 = My::Num->new(2);
3463+
my $i7 = My::Num->new(7);
3464+
my $i13 = My::Num->new(13);
3465+
3466+
my $x = $i13->add($i7)->divide($i2);
3467+
printf "val=%d\n", $x->val();
3468+
3469+
Using overloading, we would like to be able to write those last two lines
3470+
more simply as:
3471+
3472+
my $x = ($i13 + $i7)/$i2;
3473+
printf "val=%d\n", $x;
3474+
3475+
The following additions and modifications to that example XS code show how
3476+
to add overloading:
3477+
3478+
FALLBACK: UNDEF
3479+
3480+
int
3481+
mynum_val(My::Num x, ...)
3482+
OVERLOAD: 0+
3483+
3484+
My::Num
3485+
mynum_add(My::Num x, My::Num y, bool swap)
3486+
OVERLOAD: +
3487+
C_ARGS: x, y
3488+
INIT:
3489+
if (swap) {
3490+
mynum* tmp = x; x = y; y = tmp;
3491+
}
3492+
3493+
# and three similar XSUBs for mynum_subtract, mynum_multiply,
3494+
# mynum_divide
3495+
3496+
The C<FALLBACK> line isn't actually necessary as this is the default
3497+
anyway, but is included to remind you that the keyword can be used.
3498+
3499+
Overloading is added to the C<mynum_val()> method so that it automatically
3500+
returns the value of an object when used in a numeric context (such as for
3501+
the C<printf> above). The ellipsis is added to ignore the extra two
3502+
arguments passed to an overload method.
3503+
3504+
The original C<mynum_add()> method which, via aliasing, handled all four
3505+
of the arithmetic operations, is now split into four separate XSUBs, since
3506+
C<ALIAS> and C<OVERLOAD> doesn't mix.
3507+
3508+
The main change to each arithmetic XSUB part from adding the C<OVERLOAD>
3509+
keyword, is that there is an extra C<swap> parameter. There's no real need
3510+
to use it for addition and multiplication, but it is important for the
3511+
non-commutative subtraction and division operations.
3512+
3513+
That example uses the C<T_PTROBJ> typemap to process the second argument,
3514+
which in the most general usage may not be an object. For example the
3515+
second and third of these lines will croak with an C<Expected foo to be of
3516+
type My::Num, got scalar> error:
3517+
3518+
$i13 + My::Num->new(7);
3519+
$i13 + 7;
3520+
$i13 + "7";
3521+
3522+
If it is necessary to handle this, then you may need to create your own
3523+
typemap: for example, something similar to C<T_PTROBJ>, but with an INPUT
3524+
template along the lines of:
3525+
3526+
T_MYNUM
3527+
SV *sv = $arg;
3528+
SvGETMAGIC(sv);
3529+
if (!SvROK(sv)) {
3530+
sv = sv_newmortal();
3531+
sv_setref_pv(sv, "$ntype", mynum_new(SvIV($arg));
3532+
}
3533+
....
3534+
3535+
Finally, although not directly related to XS, the following could be added
3536+
to F<Num.pm> to allow integer literals to be used directly:
3537+
3538+
sub import {
3539+
overload::constant integer =>
3540+
sub {
3541+
my $str = shift;
3542+
return My::Num->new($str);
3543+
};
3544+
}
3545+
3546+
which then allows these lines:
3547+
3548+
my $i2 = My::Num->new(2);
3549+
my $i7 = My::Num->new(7);
3550+
my $i13 = My::Num->new(13);
3551+
3552+
to be rewritten more cleanly as:
3553+
3554+
my $i2 = 2;
3555+
my $i7 = 7;
3556+
my $i13 = 13;
34403557

34413558
=head3 The ATTRS: Keyword
34423559

@@ -3644,6 +3761,153 @@ the different argument lists.
36443761

36453762
XXX TBC
36463763

3764+
=head3 T_PTROBJ and opaque handles
3765+
3766+
A common interface arrangement for C libraries is that some sort of
3767+
I<create> function creates and returns a handle, which is a pointer to
3768+
some opaque data. Other function calls are then passed that handle as an
3769+
argument, until finally some sort of destroy function frees the handle and
3770+
its data. The C<T_PTROBJ> typemap is one common method for mapping Perl
3771+
objects to such C library handles. Behind the scenes, it uses blessed
3772+
scalar objects with the scalar's integer value set to the address of the
3773+
handle. The C<INPUT> code template of the C<T_PTROBJ> typemap retrieves the
3774+
pointer from the scalar object referred to by a passed RV argument, while
3775+
the C<OUTPUT> template creates a new blessed RV-to-SV with the handle
3776+
address stored in it.
3777+
3778+
For the purposes of an example, we'll create here a minimal example C
3779+
library called C<mynum>, which we'll then proceed to wrap using XS. This
3780+
library just stores an integer in its opaque data. In real life you would
3781+
be wrapping an existing library which stores something more interesting,
3782+
such as a complex number or a multiple precision integer.
3783+
3784+
The following sample library code might go in the initial 'C' part of the
3785+
XS file:
3786+
3787+
typedef struct { int i; } mynum;
3788+
3789+
mynum* mynum_new(int i)
3790+
{
3791+
mynum* x = (mynum*)malloc(sizeof(mynum));
3792+
x->i = i;
3793+
return x;
3794+
}
3795+
3796+
void mynum_destroy (mynum *x)
3797+
{ free((void*)x); }
3798+
3799+
int mynum_val (mynum *x)
3800+
{ return x->i; }
3801+
3802+
mynum* mynum_add (mynum *x, mynum *y)
3803+
{ return mynum_new(x->i + y->i); }
3804+
3805+
mynum* mynum_subtract (mynum *x, mynum *y)
3806+
{ return mynum_new(x->i - y->i); }
3807+
3808+
mynum* mynum_multiply (mynum *x, mynum *y)
3809+
{ return mynum_new(x->i * y->i); }
3810+
3811+
mynum* mynum_divide (mynum *x, mynum *y)
3812+
{ return mynum_new(x->i / y->i); }
3813+
3814+
The C<mynum> struct holds the opaque handle data. The C<mynum_new()>
3815+
function creates a numeric value and returns a handle to it. The other
3816+
functions then take such handles as arguments, including a destroy
3817+
function to free a handle's data.
3818+
3819+
The following XS code shows an example of how this library might be
3820+
wrapped and be made accessible from Perl via C<My::Num> objects:
3821+
3822+
typedef mynum *My__Num;
3823+
3824+
MODULE = My::Num PACKAGE = My::Num PREFIX = mynum_
3825+
3826+
PROTOTYPES: DISABLE
3827+
3828+
TYPEMAP: <<EOF
3829+
My::Num T_PTROBJ
3830+
EOF
3831+
3832+
My::Num
3833+
mynum_new(class, int i)
3834+
C_ARGS: i
3835+
3836+
void
3837+
DESTROY(My::Num x)
3838+
CODE:
3839+
mynum_destroy(x);
3840+
3841+
int
3842+
mynum_val(My::Num x)
3843+
3844+
My::Num
3845+
mynum_add(My::Num x, My::Num y)
3846+
ALIAS: subtract = 1
3847+
multiply = 2
3848+
divide = 3
3849+
CODE:
3850+
switch (ix) {
3851+
case 0: RETVAL = mynum_add(x, y); break;
3852+
case 1: RETVAL = mynum_subtract(x, y); break;
3853+
case 2: RETVAL = mynum_multiply(x, y); break;
3854+
case 3: RETVAL = mynum_divide(x, y); break;
3855+
}
3856+
OUTPUT:
3857+
RETVAL
3858+
3859+
The XSUBs in this example are mostly declared with parameter and return
3860+
types of C<My::Num> which, as explained in L</Fully-qualified type names
3861+
and Perl objects>, is looked up as-is in the typemap, but has C<s/:/_/g>
3862+
applied to the type name to convert it to the C<My__Num> C type when used
3863+
in the declaration of the XSUB's auto variables.
3864+
3865+
Going through this code in order: while still in the 'C' half of the XS
3866+
file, we add a typedef which says that the C<My__Num> C type is equivalent
3867+
to a pointer to a handle from that arithmetic library.
3868+
3869+
Next, the C<MODULE> line includes a C<mynum_> prefix, which means that
3870+
the names of the XSUBs in the Perl namespace will be C<My::Num::new()> etc
3871+
rather than C<My::Num::mynum_new()>.
3872+
3873+
Then a C<TYPEMAP> declaration is used to map the C<My::Num> pseudo-type to
3874+
the C<T_PTROBJ> XS type.
3875+
3876+
Next comes the C<new()> class method. This will be called from perl as
3877+
C<< My::Num->new(99); >> for example. Its first parameter will be the
3878+
class name, which we don't use here, and the second parameter is the value
3879+
to initialise the object to. The XSUB autocalls the library C<mynum_new()>
3880+
function with just the C<i> value. This returns a handle, which the
3881+
C<T_PTROBJ> C<OUTPUT> map converts into a blessed scalar ref containing
3882+
the handle.
3883+
3884+
Next, the C<DESTROY()> method is just a thin wrapper around
3885+
C<mynum_destroy()>, while C<val()> returns the integer value of the
3886+
object.
3887+
3888+
Finally, four binary functions are defined, sharing the same XSUB body via
3889+
aliases.
3890+
3891+
This XS module might be accessed from Perl using code like this:
3892+
3893+
use My::Num;
3894+
3895+
my $i2 = My::Num->new(2);
3896+
my $i7 = My::Num->new(7);
3897+
my $i13 = My::Num->new(13);
3898+
3899+
my $x = $i13->add($i7)->divide($i2);
3900+
printf "val=%d\n", $x->val(); # prints "val=10"
3901+
3902+
See L</The OVERLOAD: Keyword> for an example of how to extend this using
3903+
overloading so that the expression could be written more simply as
3904+
C<($i13 + $i7)/$i2>.
3905+
3906+
Note that, as a very special case, the XS compiler translates the XS
3907+
typemap name using C<s/OBJ$/REF/> when looking up INPUT typemap entries
3908+
for an XSUB named C<DESTROY>. So for such subs, the C<T_PTRREF> typemap
3909+
entry will be used instead.
3910+
36473911
=head2 Using XS With C++
36483912

36493913
If an XSUB name contains C<::>, it is considered to be a C++ method.

0 commit comments

Comments
 (0)