| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Devel::CallChecker - custom op checking attached to subroutines | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | # to generate header prior to XS compilation | ||
| 8 | |||
| 9 | perl -MDevel::CallChecker=callchecker0_h \ | ||
| 10 | -e 'print callchecker0_h' > callchecker0.h | ||
| 11 | |||
| 12 | # in Perl part of module | ||
| 13 | |||
| 14 | use Devel::CallChecker; | ||
| 15 | |||
| 16 | /* in XS */ | ||
| 17 | |||
| 18 | #include "callchecker0.h" | ||
| 19 | |||
| 20 | cv_get_call_checker(cv, &ckfun, &ckobj); | ||
| 21 | static OP *my_ckfun(pTHX_ OP *o, GV *namegv, SV *ckobj); | ||
| 22 | cv_set_call_checker(cv, my_ckfun, ckobj); | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This module makes some new features of the Perl 5.14.0 C API available | ||
| 27 | to XS modules running on older versions of Perl. The features are | ||
| 28 | centred around the function C<cv_set_call_checker>, which allows XS | ||
| 29 | code to attach a magical annotation to a Perl subroutine, resulting in | ||
| 30 | resolvable calls to that subroutine being mutated at compile time by | ||
| 31 | arbitrary C code. This module makes C<cv_set_call_checker> and several | ||
| 32 | supporting functions available. (It is possible to achieve the effect | ||
| 33 | of C<cv_set_call_checker> from XS code on much earlier Perl versions, | ||
| 34 | but it is painful to achieve without the centralised facility.) | ||
| 35 | |||
| 36 | This module provides the implementation of the functions at runtime (on | ||
| 37 | Perls where they are not provided by the core). It also, at compile time, | ||
| 38 | supplies the C header file and link library which provide access to the | ||
| 39 | functions. In normal use, L</callchecker0_h> and L</callchecker_linkable> | ||
| 40 | should be called at build time (not authoring time) for the module that | ||
| 41 | wishes to use the C functions. | ||
| 42 | |||
| 43 | The purpose of this module is specifically to provide the Perl 5.14.0 | ||
| 44 | version of the C<cv_set_call_checker> API to earlier Perl versions where | ||
| 45 | the core doesn't have C<cv_set_call_checker> at all. This module does not | ||
| 46 | attempt to backport later refinements of the C<cv_set_call_checker> API. | ||
| 47 | Thus an XS module that uses this module can be sure of having at least | ||
| 48 | the Perl 5.14.0 version of C<cv_set_call_checker> available, regardless | ||
| 49 | of which Perl version it is running on, but cannot be sure of having | ||
| 50 | any more refined version of the API available. Such a module will have | ||
| 51 | access to the core's version of the API as normal on Perl versions where | ||
| 52 | the core supplies it, and is free to use the ordinary mechanisms of Perl | ||
| 53 | version portability to manage the differences between versions of the API. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | package Devel::CallChecker; | ||
| 58 | |||
| 59 | 3 | ▶ | { use 5.006; } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 60 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 61 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 62 | |||
| 63 | 3 | our $VERSION = "0.010"; | |
| 64 | |||
| 65 | 3 | ▶ | use parent "Exporter"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | 3 | our @EXPORT_OK = qw(callchecker0_h callchecker_linkable); | |
| 67 | |||
| 68 | { | ||
| 69 | 3 | require DynaLoader; | |
| 70 | 3 | local our @ISA = qw(DynaLoader); | |
| 71 | 3 | ▶ | local *dl_load_flags = sub { 1 }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 72 | 3 | __PACKAGE__->bootstrap($VERSION); | |
| 73 | } | ||
| 74 | |||
| 75 | =head1 CONSTANTS | ||
| 76 | |||
| 77 | =over | ||
| 78 | |||
| 79 | =item callchecker0_h | ||
| 80 | |||
| 81 | Content of a C header file, intended to be named "C<callchecker0.h>". | ||
| 82 | It is to be included in XS code, and C<perl.h> must be included first. | ||
| 83 | When the XS module is loaded at runtime, the C<Devel::CallChecker> | ||
| 84 | module must be loaded first. This will result in the Perl API functions | ||
| 85 | C<rv2cv_op_cv>, C<ck_entersub_args_list>, C<ck_entersub_args_proto>, | ||
| 86 | C<ck_entersub_args_proto_or_list>, C<cv_get_call_checker>, and | ||
| 87 | C<cv_set_call_checker>, as defined below and in the Perl 5.14.0 API, | ||
| 88 | being available to the XS code. | ||
| 89 | |||
| 90 | =item callchecker_linkable | ||
| 91 | |||
| 92 | List of names of files that must be used as additional objects when | ||
| 93 | linking an XS module that uses the C functions supplied by this module. | ||
| 94 | This list will be empty on many platforms. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | 2 | ▶ | sub callchecker_linkable() { |
|
Subroutine
callchecker_linkable: called CC=1 SCAR=0.0
Pod
documented
| |||
| 99 | 2 | require DynaLoader::Functions; | |
| 100 | 2 | DynaLoader::Functions->VERSION(0.001); | |
| 101 | 2 | return DynaLoader::Functions::linkable_for_module(__PACKAGE__); | |
| 102 | } | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =head1 C FUNCTIONS | ||
| 107 | |||
| 108 | =over | ||
| 109 | |||
| 110 | =item rv2cv_op_cv | ||
| 111 | |||
| 112 | Examines an op, which is expected to identify a subroutine at runtime, | ||
| 113 | and attempts to determine at compile time which subroutine it identifies. | ||
| 114 | This is normally used during Perl compilation to determine whether | ||
| 115 | a prototype can be applied to a function call. I<cvop> is the op | ||
| 116 | being considered, normally an C<rv2cv> op. A pointer to the identified | ||
| 117 | subroutine is returned, if it could be determined statically, and a null | ||
| 118 | pointer is returned if it was not possible to determine statically. | ||
| 119 | |||
| 120 | Whether the subroutine is statically identifiable is determined in | ||
| 121 | accordance with the prevailing standards of the Perl version being used. | ||
| 122 | The same criteria are used that the core uses to determine whether to | ||
| 123 | apply a prototype to a subroutine call. From version 5.11.2 onwards, the | ||
| 124 | subroutine can be determined if the RV that the C<rv2cv> is to operate | ||
| 125 | on is provided by a suitable C<gv> or C<const> op. Prior to 5.11.2, | ||
| 126 | only a C<gv> op will do. A C<gv> op is suitable if the GV's CV slot | ||
| 127 | is populated. A C<const> op is suitable if the constant value is | ||
| 128 | an RV pointing to a CV. Details of this process may change in future | ||
| 129 | versions of Perl. | ||
| 130 | |||
| 131 | If the C<rv2cv> op has the C<OPpENTERSUB_AMPER> flag set then no attempt | ||
| 132 | is made to identify the subroutine statically: this flag is used to | ||
| 133 | suppress compile-time magic on a subroutine call, forcing it to use | ||
| 134 | default runtime behaviour. | ||
| 135 | |||
| 136 | If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling | ||
| 137 | of a GV reference is modified. If a GV was examined and its CV slot was | ||
| 138 | found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set. | ||
| 139 | If the op is not optimised away, and the CV slot is later populated with | ||
| 140 | a subroutine having a prototype, that flag eventually triggers the warning | ||
| 141 | "called too early to check prototype". | ||
| 142 | |||
| 143 | If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead | ||
| 144 | of returning a pointer to the subroutine it returns a pointer to the | ||
| 145 | GV giving the most appropriate name for the subroutine in this context. | ||
| 146 | Normally this is just the C<CvGV> of the subroutine, but for an anonymous | ||
| 147 | (C<CvANON>) subroutine that is referenced through a GV it will be the | ||
| 148 | referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned. | ||
| 149 | A null pointer is returned as usual if there is no statically-determinable | ||
| 150 | subroutine. | ||
| 151 | |||
| 152 | CV *rv2cv_op_cv(OP *cvop, U32 flags) | ||
| 153 | |||
| 154 | =item cv_get_call_checker | ||
| 155 | |||
| 156 | Retrieves the function that will be used to fix up a call to I<cv>. | ||
| 157 | Specifically, the function is applied to an C<entersub> op tree for a | ||
| 158 | subroutine call, not marked with C<&>, where the callee can be identified | ||
| 159 | at compile time as I<cv>. | ||
| 160 | |||
| 161 | The C-level function pointer is returned in I<*ckfun_p>, and an SV | ||
| 162 | argument for it is returned in I<*ckobj_p>. The function is intended | ||
| 163 | to be called in this manner: | ||
| 164 | |||
| 165 | entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p)); | ||
| 166 | |||
| 167 | In this call, I<entersubop> is a pointer to the C<entersub> op, | ||
| 168 | which may be replaced by the check function, and I<namegv> is a GV | ||
| 169 | supplying the name that should be used by the check function to refer | ||
| 170 | to the callee of the C<entersub> op if it needs to emit any diagnostics. | ||
| 171 | It is permitted to apply the check function in non-standard situations, | ||
| 172 | such as to a call to a different subroutine or to a method call. | ||
| 173 | |||
| 174 | By default, the function is | ||
| 175 | L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>, | ||
| 176 | and the SV parameter is I<cv> itself. This implements standard | ||
| 177 | prototype processing. It can be changed, for a particular subroutine, | ||
| 178 | by L</cv_set_call_checker>. | ||
| 179 | |||
| 180 | void cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, | ||
| 181 | SV **ckobj_p) | ||
| 182 | |||
| 183 | =item cv_set_call_checker | ||
| 184 | |||
| 185 | Sets the function that will be used to fix up a call to I<cv>. | ||
| 186 | Specifically, the function is applied to an C<entersub> op tree for a | ||
| 187 | subroutine call, not marked with C<&>, where the callee can be identified | ||
| 188 | at compile time as I<cv>. | ||
| 189 | |||
| 190 | The C-level function pointer is supplied in I<ckfun>, and an SV argument | ||
| 191 | for it is supplied in I<ckobj>. The function is intended to be called | ||
| 192 | in this manner: | ||
| 193 | |||
| 194 | entersubop = ckfun(aTHX_ entersubop, namegv, ckobj); | ||
| 195 | |||
| 196 | In this call, I<entersubop> is a pointer to the C<entersub> op, | ||
| 197 | which may be replaced by the check function, and I<namegv> is a GV | ||
| 198 | supplying the name that should be used by the check function to refer | ||
| 199 | to the callee of the C<entersub> op if it needs to emit any diagnostics. | ||
| 200 | It is permitted to apply the check function in non-standard situations, | ||
| 201 | such as to a call to a different subroutine or to a method call. | ||
| 202 | |||
| 203 | The current setting for a particular CV can be retrieved by | ||
| 204 | L</cv_get_call_checker>. | ||
| 205 | |||
| 206 | void cv_set_call_checker(CV *cv, Perl_call_checker ckfun, | ||
| 207 | SV *ckobj) | ||
| 208 | |||
| 209 | =item ck_entersub_args_list | ||
| 210 | |||
| 211 | Performs the default fixup of the arguments part of an C<entersub> | ||
| 212 | op tree. This consists of applying list context to each of the | ||
| 213 | argument ops. This is the standard treatment used on a call marked | ||
| 214 | with C<&>, or a method call, or a call through a subroutine reference, | ||
| 215 | or any other call where the callee can't be identified at compile time, | ||
| 216 | or a call where the callee has no prototype. | ||
| 217 | |||
| 218 | OP *ck_entersub_args_list(OP *entersubop) | ||
| 219 | |||
| 220 | =item ck_entersub_args_proto | ||
| 221 | |||
| 222 | Performs the fixup of the arguments part of an C<entersub> op tree | ||
| 223 | based on a subroutine prototype. This makes various modifications to | ||
| 224 | the argument ops, from applying context up to inserting C<refgen> ops, | ||
| 225 | and checking the number and syntactic types of arguments, as directed by | ||
| 226 | the prototype. This is the standard treatment used on a subroutine call, | ||
| 227 | not marked with C<&>, where the callee can be identified at compile time | ||
| 228 | and has a prototype. | ||
| 229 | |||
| 230 | I<protosv> supplies the subroutine prototype to be applied to the call. | ||
| 231 | It may be a normal defined scalar, of which the string value will be used. | ||
| 232 | Alternatively, for convenience, it may be a subroutine object (a C<CV*> | ||
| 233 | that has been cast to C<SV*>) which has a prototype. The prototype | ||
| 234 | supplied, in whichever form, does not need to match the actual callee | ||
| 235 | referenced by the op tree. | ||
| 236 | |||
| 237 | If the argument ops disagree with the prototype, for example by having | ||
| 238 | an unacceptable number of arguments, a valid op tree is returned anyway. | ||
| 239 | The error is reflected in the parser state, normally resulting in a single | ||
| 240 | exception at the top level of parsing which covers all the compilation | ||
| 241 | errors that occurred. In the error message, the callee is referred to | ||
| 242 | by the name defined by the I<namegv> parameter. | ||
| 243 | |||
| 244 | OP *ck_entersub_args_proto(OP *entersubop, GV *namegv, | ||
| 245 | SV *protosv) | ||
| 246 | |||
| 247 | =item ck_entersub_args_proto_or_list | ||
| 248 | |||
| 249 | Performs the fixup of the arguments part of an C<entersub> op tree either | ||
| 250 | based on a subroutine prototype or using default list-context processing. | ||
| 251 | This is the standard treatment used on a subroutine call, not marked | ||
| 252 | with C<&>, where the callee can be identified at compile time. | ||
| 253 | |||
| 254 | I<protosv> supplies the subroutine prototype to be applied to the call, | ||
| 255 | or indicates that there is no prototype. It may be a normal scalar, | ||
| 256 | in which case if it is defined then the string value will be used | ||
| 257 | as a prototype, and if it is undefined then there is no prototype. | ||
| 258 | Alternatively, for convenience, it may be a subroutine object (a C<CV*> | ||
| 259 | that has been cast to C<SV*>), of which the prototype will be used if it | ||
| 260 | has one. The prototype (or lack thereof) supplied, in whichever form, | ||
| 261 | does not need to match the actual callee referenced by the op tree. | ||
| 262 | |||
| 263 | If the argument ops disagree with the prototype, for example by having | ||
| 264 | an unacceptable number of arguments, a valid op tree is returned anyway. | ||
| 265 | The error is reflected in the parser state, normally resulting in a single | ||
| 266 | exception at the top level of parsing which covers all the compilation | ||
| 267 | errors that occurred. In the error message, the callee is referred to | ||
| 268 | by the name defined by the I<namegv> parameter. | ||
| 269 | |||
| 270 | OP *ck_entersub_args_proto_or_list(OP *entersubop, GV *namegv, | ||
| 271 | SV *protosv) | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =head1 SEE ALSO | ||
| 276 | |||
| 277 | L<B::CallChecker>, | ||
| 278 | L<Devel::CallParser>, | ||
| 279 | L<perlapi/cv_set_call_checker> | ||
| 280 | |||
| 281 | =head1 AUTHOR | ||
| 282 | |||
| 283 | Andrew Main (Zefram) <zefram@fysh.org> | ||
| 284 | |||
| 285 | =head1 COPYRIGHT | ||
| 286 | |||
| 287 | Copyright (C) 2011, 2012, 2013, 2015, 2017, 2023 | ||
| 288 | Andrew Main (Zefram) <zefram@fysh.org> | ||
| 289 | |||
| 290 | =head1 LICENSE | ||
| 291 | |||
| 292 | This module is free software; you can redistribute it and/or modify it | ||
| 293 | under the same terms as Perl itself. | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | 3 | 1; | |