Skip to content

Commit d11731b

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 b26efeb commit d11731b

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
@@ -2162,6 +2162,8 @@ result in these declarations in the C code:
21622162

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

21662168
=head3 XSUB Parameter Placeholders
21672169

@@ -3436,46 +3438,161 @@ prototype string, including the empty prototype.
34363438

34373439
=head3 The OVERLOAD: Keyword
34383440

3439-
Instead of writing an overloaded interface using pure Perl, you
3440-
can also use the OVERLOAD keyword to define additional Perl names
3441-
for your functions (like the ALIAS: keyword above). However, the
3442-
overloaded functions must be defined in such a way as to accept the number
3443-
of parameters supplied by perl's overload system. For most overload
3444-
methods, it will be three parameters; for the C<nomethod> function it will
3445-
be four. However, the bitwise operators C<&>, C<|>, C<^>, and C<~> may be
3446-
called with three I<or> five arguments (see L<overload>).
3447-
3448-
If any
3449-
function has the OVERLOAD: keyword, several additional lines
3450-
will be defined in the c file generated by xsubpp in order to
3451-
register with the overload magic.
3452-
3453-
Since blessed objects are actually stored as RV's, it is useful
3454-
to use the typemap features to preprocess parameters and extract
3455-
the actual SV stored within the blessed RV. See the sample for
3456-
T_PTROBJ_SPECIAL in L<perlxstypemap>.
3457-
3458-
To use the OVERLOAD: keyword, create an XS function which takes
3459-
three input parameters (or use the C-style '...' definition) like
3460-
this:
3441+
MODULE = Foo PACKAGE = Foo::Bar
34613442

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

34803597
=head3 The ATTRS: Keyword
34813598

@@ -3683,6 +3800,153 @@ the different argument lists.
36833800

36843801
XXX TBC
36853802

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

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

0 commit comments

Comments
 (0)