@@ -3183,74 +3183,154 @@ it. It is documented here for completeness.
31833183
31843184=head2 The XSUB Output Part
31853185
3186- XXX TBC
3186+ Following an XSUB's code part, any results may be post-processed and
3187+ returned. Two keywords in particular support this: L<POSTCALL|/The
3188+ POSTCALL: Keyword>, which allows for a block of code to be added after
3189+ any autocall in order to post-process return values from the call, and
3190+ L<OUTPUT|/The OUTPUT: Keyword>, which tells the parser to generate code to
3191+ return the value of C<RETVAL> or to update the values of one or more
3192+ passed arguments.
3193+
3194+ These two optional keywords should each only be used once at most, and in
3195+ that order; but due to a parsing bug (kept for backwards compatibility),
3196+ they can appear in either order any number of times. But don't do that.
31873197
3188- XXX NB: the keywords described in L<XSUB Generic Keywords> and L<Sharing
3198+ Note that the keywords described in L<XSUB Generic Keywords> and L<Sharing
31893199XSUB bodies> may also appear in this part.
31903200
31913201=head3 The POSTCALL: Keyword
31923202
3193- This keyword can be used when an XSUB requires special procedures
3194- executed after the C subroutine call is performed. When the POSTCALL:
3195- keyword is used it must precede OUTPUT: and CLEANUP: blocks which are
3196- present in the XSUB.
3203+ The C<POSTCALL> keyword allows a block of code to be inserted directly after
3204+ any autocall or C<CODE>/C<PPCODE> code block (although it's really only of
3205+ use with autocall). It's typically used for cleaning up the return value
3206+ from the autocall. For example these two XSUBs are equivalent:
31973207
3198- See an example in L<"An XSUB Declaration">.
3199-
3200- The POSTCALL: block does not make a lot of sense when the C subroutine
3201- call is supplied by user by providing either CODE: or PPCODE: section.
3208+ int
3209+ foo(int a)
3210+ POSTCALL:
3211+ if (RETVAL < 0)
3212+ RETVAL = 0
32023213
3214+ int
3215+ foo(int a)
3216+ CODE:
3217+ RETVAL = foo(a);
3218+ if (RETVAL < 0)
3219+ RETVAL = 0
3220+ OUTPUT:
3221+ RETVAL
32033222
32043223=head3 The OUTPUT: Keyword
32053224
3206- The OUTPUT: keyword indicates that certain function parameters should be
3207- updated (new values made visible to Perl) when the XSUB terminates or that
3208- certain values should be returned to the calling Perl function. For
3209- simple functions which have no CODE: or PPCODE: section,
3210- such as the sin() function above, the RETVAL variable is
3211- automatically designated as an output value. For more complex functions
3212- the B<xsubpp> compiler will need help to determine which variables are output
3213- variables.
3214-
3215- This keyword will normally be used to complement the CODE: keyword.
3216- The RETVAL variable is not recognized as an output variable when the
3217- CODE: keyword is present. The OUTPUT: keyword is used in this
3218- situation to tell the compiler that RETVAL really is an output
3219- variable.
3220-
3221- The OUTPUT: keyword can also be used to indicate that function parameters
3222- are output variables. This may be necessary when a parameter has been
3223- modified within the function and the programmer would like the update to
3224- be seen by Perl.
3225+ # Common usage:
32253226
3226- bool_t
3227- rpcb_gettime(host, timep)
3228- char *host
3229- time_t &timep
3227+ OUTPUT:
3228+ RETVAL
3229+
3230+ # Rare usage:
3231+
3232+ OUTPUT:
3233+ arg0
3234+ SETMAGIC: DISABLE
3235+ arg1
3236+ SETMAGIC: ENABLE
3237+ arg2 sv_setfoo(ST[2], arg2)
3238+
3239+
3240+ The C<OUTPUT> keyword can be used to indicate that the value of RETVAL
3241+ should be returned to the caller on the stack, and/or that the values of
3242+ certain passed Perl arguments should be updated with the current values of
3243+ the corresponding parameter variables. Each non-blank line of the
3244+ C<OUTPUT> block should contain the name of one variable, with optional
3245+ setting code, or a C<SETMAGIC:> keyword with a value of C<ENABLE> or
3246+ C<DISABLE>.
3247+
3248+ The common usage is to list just the C<RETVAL> variable:
3249+
3250+ int
3251+ foo()
3252+ CODE:
3253+ RETVAL = ...;
3254+ OUTPUT:
3255+ RETVAL
3256+
3257+ It is needed for XSUBs containing a C<CODE> block to tell the XS compiler
3258+ to generate C code which will return the value of C<RETVAL> to the caller.
3259+ For autocall XSUBs, this is done automatically without the need for the
3260+ C<OUTPUT> keyword.
3261+
3262+ The second usage of C<OUTPUT> is to specify parameters to be updated; this
3263+ usage has been almost completely replaced by using the
3264+ L<OUT|/"Updating and returning parameter values: the IN_OUT etc keywords">
3265+ parameter modifier. For example these two XSUBs have identical behaviours,
3266+ but the second is the preferred form:
3267+
3268+ int
3269+ foo1(a)
3270+ INPUT:
3271+ int &a
3272+ OUTPUT:
3273+ a
3274+
3275+ int
3276+ foo2(IN_OUT int a)
3277+
3278+ They both cause output C code similar to this to be planted (with the
3279+ first part derived from a typemap):
3280+
3281+ sv_setiv(ST(0), (IV)a);
3282+ SvSETMAGIC(ST(0));
3283+
3284+ which updates the value of the passed SV with the current value of C<a>,
3285+ and then calls the SV's I<set> magic, if any: which will, for example,
3286+ cause a tied variable to have its C<STORE()> method called.
3287+
3288+ You can skip the planting of the C<SvSETMAGIC()> magic call with
3289+ C<SETMAGIC: DISABLE>; in the example at the start of this section, C<arg0>
3290+ and C<arg2> will have set magic, while C<arg1> won't. The C<SETMAGIC>
3291+ setting remains in force until another C<SETMAGIC>, or notionally until
3292+ the end of the current C<OUTPUT> block. In fact the current setting will
3293+ carry over into any further C<OUTPUT> declarations within in the same
3294+ XSUB, or since Perl 5.40.0, only into any declarations within the same
3295+ case C<CASE> branch.
3296+
3297+ The current setting of C<SETMAGIC> is ignored for C<RETVAL>, which is
3298+ usually setting the value of a fresh temporary SV which won't have any
3299+ attached magic anyway.
3300+
3301+ Finally, it is possible to override the typemap entry used to set the
3302+ value of the temporary SV or passed argument from the C<RETVAL> or other
3303+ variables. Normally, in an XSUB like:
3304+
3305+ int
3306+ foo(int abc)
32303307 OUTPUT:
3231- timep
3308+ abc
32323309
3233- The OUTPUT: keyword will also allow an output parameter to
3234- be mapped to a matching piece of code rather than to a
3235- typemap.
32363310
3237- bool_t
3238- rpcb_gettime(host, timep)
3239- char *host
3240- time_t &timep
3311+ the C<int> type (via a two-stage lookup in the system typemap) will yield
3312+ this output typemap entry:
3313+
3314+ sv_setiv($arg, (IV)$var);
3315+
3316+ which, after variable expansion, may yield
3317+
3318+ sv_setiv(ST(0), (IV)abc);
3319+
3320+ or similar. This can be overridden; for example
3321+
3322+ int
3323+ foo(int abc)
32413324 OUTPUT:
3242- timep sv_setnv(ST(1), (double)timep);
3243-
3244- B<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
3245- OUTPUT section of the XSUB, except RETVAL. This is the usually desired
3246- behavior, as it takes care of properly invoking 'set' magic on output
3247- parameters (needed for hash or array element parameters that must be
3248- created if they didn't exist). If for some reason, this behavior is
3249- not desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
3250- to disable it for the remainder of the parameters in the OUTPUT section.
3251- Likewise, C<SETMAGIC: ENABLE> can be used to reenable it for the
3252- remainder of the OUTPUT section. See L<perlguts> for more details
3253- about 'set' magic.
3325+ abc my_setiv(ST(0), (IV)abc);
3326+
3327+ But importantly, unlike the similar syntax in C<INPUT> lines, the override
3328+ text is I<not> variable expanded. It is thus tricky to ensure that the
3329+ right arguments are used (such as C<ST(0)>). Basically this feature has a
3330+ design flaw and should probably be avoided. Since 5.16.0 it's been
3331+ possible to have locally defined typemaps using the L<TYPEMAP|/The
3332+ TYPEMAP: Keyword> keyword which is probably a better way to modify how
3333+ values are returned.
32543334
32553335=head2 The XSUB Cleanup Part
32563336
0 commit comments