@@ -3702,84 +3702,119 @@ be used together with either of C<INTERFACE> or C<ATTR>.
37023702
37033703=head3 The INTERFACE: Keyword
37043704
3705- XXX this keyword can appear anywhere within the init part of an XSUB.
3705+ MODULE = Foo::Bar PACKAGE = Foo::Bar PREFIX = foobar_
37063706
3707- This keyword declares the current XSUB as a keeper of the given
3708- calling signature. If some text follows this keyword, it is
3709- considered as a list of functions which have this signature, and
3710- should be attached to the current XSUB.
3711-
3712- For example, if you have 4 C functions multiply(), divide(), add(),
3713- subtract() all having the signature:
3714-
3715- symbolic f(symbolic, symbolic);
3716-
3717- you can make them all to use the same XSUB using this:
3718-
3719- symbolic
3720- interface_s_ss(arg1, arg2)
3721- symbolic arg1
3722- symbolic arg2
3723- INTERFACE:
3724- multiply divide
3725- add subtract
3726-
3727- (This is the complete XSUB code for 4 Perl functions!) Four generated
3728- Perl function share names with corresponding C functions.
3729-
3730- The advantage of this approach comparing to ALIAS: keyword is that there
3731- is no need to code a switch statement, each Perl function (which shares
3732- the same XSUB) knows which C function it should call. Additionally, one
3733- can attach an extra function remainder() at runtime by using
3707+ int
3708+ arith(int a, int b)
3709+ INTERFACE: foobar_add foobar_subtract
3710+ foobar_divide foobar_multiply
3711+
3712+ This keyword can appear anywhere within the L<initialisation part|/The
3713+ XSUB Init Part> of an XSUB.
3714+
3715+ This keyword provides similar functionality to C<ALIAS>, but is intended
3716+ for XSUBs which use autocall. It allows a single XSUB to have multiple
3717+ names in the Perl namespace which, when invoked, will call the correct
3718+ wrapped C library function.
3719+
3720+ In the example above there is a single C XSUB function created (called
3721+ C<XS_Foo__Bar_arith>), plus four CVs in the Perl namespace called
3722+ C<Foo::Bar::add> etc. Calling C<Foo::Bar::add()> from Perl invokes
3723+ C<XS_Foo__Bar_arith()> with some indication of which C function to call,
3724+ which is then autocalled. C<ALIAS> achieves this by storing an index value
3725+ in each CV and making it available via the C<ix> variable, while
3726+ C<INTERFACE> currently achieves this by storing a C function pointer in
3727+ each CV. So the C<Foo::Bar::add()> CV holds a pointer to the
3728+ C<foobar_add()> C function. The action of the XSUB is to extract the
3729+ parameter values from the passed arguments and the function pointer from
3730+ the CV, then call the underlying C function.
3731+
3732+ Note that storing a function pointer in the CV is an implementation detail
3733+ which could change in the future. See L</The INTERFACE_MACRO: Keyword> for
3734+ details of how to customise the setting and retrieving of this value in
3735+ the CV.
3736+
3737+ The rest of the line following the C<INTERFACE> keyword, plus any further
3738+ lines until the next keyword, are assumed to contain zero or more
3739+ interface names, separated by white space (or commas).
3740+
3741+ An interface name is always used as-is for the name of the wrapped C
3742+ function. If the name contains a package separator, then it will be
3743+ used as-is to generate the Perl name; otherwise any prefix is stripped and
3744+ the current package name is prepended. The following shows how a few such
3745+ interface names would be processed (assuming the current PACKAGE and
3746+ PREFIX are C<Foo::bar> and C<foobar_>):
3747+
3748+ Interface name Perl function name C function name
3749+ -------------- ------------------ ----------------
3750+ abc Foo::Bar::abc abc
3751+ foobar_abc Foo::Bar::abc foobar_abc
3752+ X::Y::foobar_def X::Y::foobar_def X::Y::foobar_def
3753+
3754+ Unlike C<ALIAS>, the XSUB name is used only as the name of the generated C
3755+ function; in the example above, it doesn't cause a Perl function called
3756+ C<arith()> to be created.
3757+
3758+ See L</T_PTROBJ and opaque handles> for a complete example using
3759+ C<INTERFACE> with the C<T_PTROBJ> typemap. But note that before Perl
3760+ 5.44.0 (F<ExtUtils::ParseXS> 3.60), C<INTERFACE> would not work properly
3761+ on XSUBs used with Perlish return types (as used by C<T_PTROBJ>), such as
37343762
3735- CV *mycv = newXSproto("Symbolic::remainder",
3736- XS_Symbolic_interface_s_ss, __FILE__, "$$");
3737- XSINTERFACE_FUNC_SET(mycv, remainder);
3763+ Foo::Bar
3764+ foo(...)
3765+ ....
37383766
3739- say, from another XSUB. (This example supposes that there was no
3740- INTERFACE_MACRO: section, otherwise one needs to use something else instead of
3741- C<XSINTERFACE_FUNC_SET>, see the next section.)
3767+ This has mostly been fixed in 5.44.0 onwards, but may generate invalid C
3768+ code (in particular, invalid function pointer casts) for XSUBs having a
3769+ C<C_ARGS> keyword, unless the value of C<C_ARGS> is a simple list of
3770+ parameter names.
37423771
3743- =head3 The INTERFACE_MACRO: Keyword
3772+ Note that C<INTERFACE> should not be used together with either of C<ALIAS>
3773+ or C<ATTR>.
37443774
3745- XXX this keyword can appear anywhere within the input and init parts of an
3746- XSUB.
3775+ =head4 The INTERFACE_MACRO: Keyword
37473776
3748- This keyword allows one to define an INTERFACE using a different way
3749- to extract a function pointer from an XSUB. The text which follows
3750- this keyword should give the name of macros which would extract/set a
3751- function pointer. The extractor macro is given return type, C<CV*>,
3752- and C<XSANY.any_dptr> for this C<CV*>. The setter macro is given cv,
3753- and the function pointer.
3754-
3755- The default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>.
3756- An INTERFACE keyword with an empty list of functions can be omitted if
3757- INTERFACE_MACRO keyword is used.
3758-
3759- Suppose that in the previous example functions pointers for
3760- multiply(), divide(), add(), subtract() are kept in a global C array
3761- C<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>,
3762- C<subtract_off>. Then one can use
3763-
3764- #define XSINTERFACE_FUNC_BYOFFSET(ret, cv, f) \
3765- ((XSINTERFACE_CVT_ANON(ret))fp[CvXSUBANY(cv).any_i32])
3766- #define XSINTERFACE_FUNC_BYOFFSET_set(cv, f) \
3777+ int
3778+ arith(int a, int b)
3779+ INTERFACE: add subtract divide multiply
3780+ INTERFACE_MACRO: MY_FUNC_GET
3781+ MY_FUNC_SET
3782+
3783+ Note that this keyword is deprecated as it assumes a particular
3784+ implementation for the C<INTERFACE> keyword: which might change in future.
3785+
3786+ This keyword can appear anywhere within the L<input|/The XSUB Input Part>
3787+ or L<initialisation|/The XSUB Init Part> parts of an XSUB.
3788+
3789+ By default the C code generated by the C<INTERFACE> keyword plants calls
3790+ to two macros, C<XSINTERFACE_FUNC_SET> and C<XSINTERFACE_FUNC>, which are
3791+ used respectively to set (at boot time) a field in the CV to the address
3792+ of the C function pointer to use, and to retrieve (at run time) that value
3793+ from the CV.
3794+
3795+ The C<INTERFACE_MACRO> macro allows you to override the names of the two
3796+ macros to be used for this purpose. The rest of the line following the
3797+ C<INTERFACE_MACRO> keyword, plus any further lines until the next keyword,
3798+ should contain (in total) two words which are taken to be macro names.
3799+
3800+ The get macro takes three parameters: the return type of the function, the
3801+ CV which holds the function's pointer value, and the field within the CV
3802+ which has the pointer value. It should return a C function pointer. The
3803+ setter macro has two parameters: the CV, and the function pointer.
3804+
3805+ Suppose that in the example above, pointers to the C<multiply()>,
3806+ C<divide()>, C<add()> and C<subtract()> functions are kept in a global C
3807+ array called C<arith_ptrs[]> with offsets specified by the enum values
3808+ C<multiply_off>, C<divide_off>, C<add_off> and C<subtract_off>. Then one
3809+ could use:
3810+
3811+ #define MY_FUNC_GET(ret, cv, f) \
3812+ ((XSINTERFACE_CVT_ANON(ret))arith_ptrs[CvXSUBANY(cv).any_i32])
3813+ #define MY_FUNC_SET(cv, f) \
37673814 CvXSUBANY(cv).any_i32 = CAT2(f, _off)
37683815
3769- in C section,
3770-
3771- symbolic
3772- interface_s_ss(arg1, arg2)
3773- symbolic arg1
3774- symbolic arg2
3775- INTERFACE_MACRO:
3776- XSINTERFACE_FUNC_BYOFFSET
3777- XSINTERFACE_FUNC_BYOFFSET_set
3778- INTERFACE:
3779- multiply divide
3780- add subtract
3781-
3782- in XSUB section.
3816+ to store an array index in the CV, rather than storing the actual function
3817+ pointer.
37833818
37843819=head3 The CASE: Keyword
37853820
@@ -3957,7 +3992,20 @@ C<mynum_destroy()>, while C<val()> returns the integer value of the
39573992object.
39583993
39593994Finally, four binary functions are defined, sharing the same XSUB body via
3960- aliases.
3995+ aliases. As an alternative, the code for the main XSUB could simplified
3996+ using the L<INTERFACE|/The INTERFACE: Keyword> keyword rather than
3997+ using aliasing:
3998+
3999+ My::Num
4000+ arthmetic_interface(My::Num x, My::Num y)
4001+ INTERFACE:
4002+ mynum_add
4003+ mynum_subtract
4004+ mynum_multiply
4005+ mynum_divide
4006+
4007+ but note that C<INTERFACE> only supports Perlish return types such
4008+ as C<My::Num> from Perl 5.44.0 (F<ExtUtils::ParseXS> 3.60) onwards.
39614009
39624010This XS module might be accessed from Perl using code like this:
39634011
0 commit comments