Skip to content

Commit 7188b4a

Browse files
committed
perlxs.pod: update INTERFACE, INTERFACE_MACRO
Rewrite these sections: =head3 The INTERFACE: Keyword =head3 The INTERFACE_MACRO: Keyword also demote the second to be a head4 child of the first. Then expand the T_PTROBJ example to use INTERFACE as an alternative to ALIAS.
1 parent 815fe4e commit 7188b4a

File tree

1 file changed

+119
-71
lines changed

1 file changed

+119
-71
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 119 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3750,84 +3750,119 @@ be used together with either of C<INTERFACE> or C<ATTRS>.
37503750

37513751
=head3 The INTERFACE: Keyword
37523752

3753-
XXX this keyword can appear anywhere within the init part of an XSUB.
3753+
MODULE = Foo::Bar PACKAGE = Foo::Bar PREFIX = foobar_
37543754

3755-
This keyword declares the current XSUB as a keeper of the given
3756-
calling signature. If some text follows this keyword, it is
3757-
considered as a list of functions which have this signature, and
3758-
should be attached to the current XSUB.
3759-
3760-
For example, if you have 4 C functions multiply(), divide(), add(),
3761-
subtract() all having the signature:
3762-
3763-
symbolic f(symbolic, symbolic);
3764-
3765-
you can make them all to use the same XSUB using this:
3766-
3767-
symbolic
3768-
interface_s_ss(arg1, arg2)
3769-
symbolic arg1
3770-
symbolic arg2
3771-
INTERFACE:
3772-
multiply divide
3773-
add subtract
3774-
3775-
(This is the complete XSUB code for 4 Perl functions!) Four generated
3776-
Perl function share names with corresponding C functions.
3777-
3778-
The advantage of this approach comparing to ALIAS: keyword is that there
3779-
is no need to code a switch statement, each Perl function (which shares
3780-
the same XSUB) knows which C function it should call. Additionally, one
3781-
can attach an extra function remainder() at runtime by using
3755+
int
3756+
arith(int a, int b)
3757+
INTERFACE: foobar_add foobar_subtract
3758+
foobar_divide foobar_multiply
3759+
3760+
This keyword can appear anywhere within the L<initialisation part|/The
3761+
XSUB Init Part> of an XSUB.
3762+
3763+
This keyword provides similar functionality to C<ALIAS>, but is intended
3764+
for XSUBs which use autocall. It allows a single XSUB to have multiple
3765+
names in the Perl namespace which, when invoked, will call the correct
3766+
wrapped C library function.
3767+
3768+
In the example above there is a single C XSUB function created (called
3769+
C<XS_Foo__Bar_arith>), plus four CVs in the Perl namespace called
3770+
C<Foo::Bar::add> etc. Calling C<Foo::Bar::add()> from Perl invokes
3771+
C<XS_Foo__Bar_arith()> with some indication of which C function to call,
3772+
which is then autocalled. C<ALIAS> achieves this by storing an index value
3773+
in each CV and making it available via the C<ix> variable, while
3774+
C<INTERFACE> currently achieves this by storing a C function pointer in
3775+
each CV. So the C<Foo::Bar::add()> CV holds a pointer to the
3776+
C<foobar_add()> C function. The action of the XSUB is to extract the
3777+
parameter values from the passed arguments and the function pointer from
3778+
the CV, then call the underlying C function.
3779+
3780+
Note that storing a function pointer in the CV is an implementation detail
3781+
which could change in the future. See L</The INTERFACE_MACRO: Keyword> for
3782+
details of how to customise the setting and retrieving of this value in
3783+
the CV.
3784+
3785+
The rest of the line following the C<INTERFACE> keyword, plus any further
3786+
lines until the next keyword, are assumed to contain zero or more
3787+
interface names, separated by white space (or commas).
3788+
3789+
An interface name is always used as-is for the name of the wrapped C
3790+
function. If the name contains a package separator, then it will be
3791+
used as-is to generate the Perl name; otherwise any prefix is stripped and
3792+
the current package name is prepended. The following shows how a few such
3793+
interface names would be processed (assuming the current PACKAGE and
3794+
PREFIX are C<Foo::bar> and C<foobar_>):
3795+
3796+
Interface name Perl function name C function name
3797+
-------------- ------------------ ----------------
3798+
abc Foo::Bar::abc abc
3799+
foobar_abc Foo::Bar::abc foobar_abc
3800+
X::Y::foobar_def X::Y::foobar_def X::Y::foobar_def
3801+
3802+
Unlike C<ALIAS>, the XSUB name is used only as the name of the generated C
3803+
function; in the example above, it doesn't cause a Perl function called
3804+
C<arith()> to be created.
3805+
3806+
See L</T_PTROBJ and opaque handles> for a complete example using
3807+
C<INTERFACE> with the C<T_PTROBJ> typemap. But note that before Perl
3808+
5.44.0 (F<ExtUtils::ParseXS> 3.60), C<INTERFACE> would not work properly
3809+
on XSUBs used with Perlish return types (as used by C<T_PTROBJ>), such as
37823810

3783-
CV *mycv = newXSproto("Symbolic::remainder",
3784-
XS_Symbolic_interface_s_ss, __FILE__, "$$");
3785-
XSINTERFACE_FUNC_SET(mycv, remainder);
3811+
Foo::Bar
3812+
foo(...)
3813+
....
37863814

3787-
say, from another XSUB. (This example supposes that there was no
3788-
INTERFACE_MACRO: section, otherwise one needs to use something else instead of
3789-
C<XSINTERFACE_FUNC_SET>, see the next section.)
3815+
This has mostly been fixed in 5.44.0 onwards, but may generate invalid C
3816+
code (in particular, invalid function pointer casts) for XSUBs having a
3817+
C<C_ARGS> keyword, unless the value of C<C_ARGS> is a simple list of
3818+
parameter names.
37903819

3791-
=head3 The INTERFACE_MACRO: Keyword
3820+
Note that C<INTERFACE> should not be used together with either of C<ALIAS>
3821+
or C<ATTRS>.
37923822

3793-
XXX this keyword can appear anywhere within the input and init parts of an
3794-
XSUB.
3823+
=head4 The INTERFACE_MACRO: Keyword
37953824

3796-
This keyword allows one to define an INTERFACE using a different way
3797-
to extract a function pointer from an XSUB. The text which follows
3798-
this keyword should give the name of macros which would extract/set a
3799-
function pointer. The extractor macro is given return type, C<CV*>,
3800-
and C<XSANY.any_dptr> for this C<CV*>. The setter macro is given cv,
3801-
and the function pointer.
3802-
3803-
The default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>.
3804-
An INTERFACE keyword with an empty list of functions can be omitted if
3805-
INTERFACE_MACRO keyword is used.
3806-
3807-
Suppose that in the previous example functions pointers for
3808-
multiply(), divide(), add(), subtract() are kept in a global C array
3809-
C<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>,
3810-
C<subtract_off>. Then one can use
3811-
3812-
#define XSINTERFACE_FUNC_BYOFFSET(ret, cv, f) \
3813-
((XSINTERFACE_CVT_ANON(ret))fp[CvXSUBANY(cv).any_i32])
3814-
#define XSINTERFACE_FUNC_BYOFFSET_set(cv, f) \
3825+
int
3826+
arith(int a, int b)
3827+
INTERFACE: add subtract divide multiply
3828+
INTERFACE_MACRO: MY_FUNC_GET
3829+
MY_FUNC_SET
3830+
3831+
Note that this keyword is deprecated since it assumes a particular
3832+
implementation for the C<INTERFACE> keyword, which might change in future.
3833+
3834+
This keyword can appear anywhere within the L<input|/The XSUB Input Part>
3835+
or L<initialisation|/The XSUB Init Part> parts of an XSUB.
3836+
3837+
By default, the C code generated by the C<INTERFACE> keyword plants calls
3838+
to two macros, C<XSINTERFACE_FUNC_SET> and C<XSINTERFACE_FUNC>, which are
3839+
used respectively to set (at boot time) a field in the CV to the address
3840+
of the C function pointer to use, and to retrieve (at run time) that value
3841+
from the CV.
3842+
3843+
The C<INTERFACE_MACRO> macro allows you to override the names of the two
3844+
macros to be used for this purpose. The rest of the line following the
3845+
C<INTERFACE_MACRO> keyword, plus any further lines until the next keyword,
3846+
should contain (in total) two words which are taken to be macro names.
3847+
3848+
The get macro takes three parameters: the return type of the function, the
3849+
CV which holds the function's pointer value, and the field within the CV
3850+
which has the pointer value. It should return a C function pointer. The
3851+
setter macro has two parameters: the CV, and the function pointer.
3852+
3853+
Suppose that in the example above, pointers to the C<multiply()>,
3854+
C<divide()>, C<add()> and C<subtract()> functions are kept in a global C
3855+
array called C<arith_ptrs[]> with offsets specified by the enum values
3856+
C<multiply_off>, C<divide_off>, C<add_off> and C<subtract_off>. Then one
3857+
could use:
3858+
3859+
#define MY_FUNC_GET(ret, cv, f) \
3860+
((XSINTERFACE_CVT_ANON(ret))arith_ptrs[CvXSUBANY(cv).any_i32])
3861+
#define MY_FUNC_SET(cv, f) \
38153862
CvXSUBANY(cv).any_i32 = CAT2(f, _off)
38163863

3817-
in C section,
3818-
3819-
symbolic
3820-
interface_s_ss(arg1, arg2)
3821-
symbolic arg1
3822-
symbolic arg2
3823-
INTERFACE_MACRO:
3824-
XSINTERFACE_FUNC_BYOFFSET
3825-
XSINTERFACE_FUNC_BYOFFSET_set
3826-
INTERFACE:
3827-
multiply divide
3828-
add subtract
3829-
3830-
in XSUB section.
3864+
to store an array index in the CV, rather than storing the actual function
3865+
pointer.
38313866

38323867
=head3 The CASE: Keyword
38333868

@@ -4005,7 +4040,20 @@ C<mynum_destroy()>, while C<val()> returns the integer value of the
40054040
object.
40064041

40074042
Finally, four binary functions are defined, sharing the same XSUB body via
4008-
aliases.
4043+
aliases. As an alternative, the code for the main XSUB could simplified
4044+
using the L<INTERFACE|/The INTERFACE: Keyword> keyword rather than
4045+
using aliasing:
4046+
4047+
My::Num
4048+
arithmetic_interface(My::Num x, My::Num y)
4049+
INTERFACE:
4050+
mynum_add
4051+
mynum_subtract
4052+
mynum_multiply
4053+
mynum_divide
4054+
4055+
but note that C<INTERFACE> only supports Perlish return types such
4056+
as C<My::Num> from Perl 5.44.0 (F<ExtUtils::ParseXS> 3.60) onwards.
40094057

40104058
This XS module might be accessed from Perl using code like this:
40114059

0 commit comments

Comments
 (0)