| 1 |
|
| /*--------------------------------------------------------------------------- |
| 2 |
|
| * Module::Generic - Generic.xs |
| 3 |
|
| * Version v0.2.0 |
| 4 |
|
| * Copyright(c) 2026 DEGUEST Pte. Ltd. |
| 5 |
|
| * Author: Jacques Deguest <jack@deguest.jp> |
| 6 |
|
| * Created 2026/03/07 |
| 7 |
|
| * Modified 2026/06/02 |
| 8 |
|
| * |
| 9 |
|
| * XS implementations of the most frequently called type-inspection helpers. |
| 10 |
|
| * + integration of a class loading check with _is_class_loaded |
| 11 |
|
| * Moving them here eliminates Perl dispatch overhead and the temporary |
| 12 |
|
| * allocations created by the Perl call stack on every invocation. |
| 13 |
|
| * |
| 14 |
|
| * XS bindings for libmagic, loaded dynamically at runtime via dlopen(3). |
| 15 |
|
| * |
| 16 |
|
| * Design goals: |
| 17 |
|
| * - No magic.h required at build time |
| 18 |
|
| * - No libmagic-dev / file-devel required at build time |
| 19 |
|
| * - Only libmagic.so.1 (the binary runtime package) needed at runtime |
| 20 |
|
| * - All symbols resolved via dlopen + dlsym in the BOOT block |
| 21 |
|
| * - If dlopen fails (libmagic absent), BOOT does NOT croak — instead it |
| 22 |
|
| * sets $Module::Generic::File::Magic::BACKEND to "json" so the Perl |
| 23 |
|
| * layer falls back to the pure-Perl JSON backend |
| 24 |
|
| * - Optional symbols (magic_getflags, magic_version, magic_compile, |
| 25 |
|
| * magic_check, magic_list) silently degrade to undef if absent |
| 26 |
|
| * |
| 27 |
|
| * The magic_t cookie is stored and passed as a Perl IV (integer large |
| 28 |
|
| * enough to hold a pointer on both ILP32 and LP64 platforms). |
| 29 |
|
| * |
| 30 |
|
| * NOTE on MODULE vs PACKAGE: |
| 31 |
|
| * MODULE = Module::Generic so that XSLoader::load("Module::Generic") in |
| 32 |
|
| * Magic.pm resolves the bootstrap symbol boot_Module__Generic correctly, |
| 33 |
|
| * matching the Generic.so produced by EUMM with NAME = "Module::Generic". |
| 34 |
|
| * PACKAGE = Module::Generic::File::Magic installs all XS functions in the |
| 35 |
|
| * correct namespace. This pattern is also used by Time::HiRes and POSIX. |
| 36 |
|
| *---------------------------------------------------------------------------*/ |
| 37 |
|
| /* |
| 38 |
|
| * Portions of the class-loading detection logic are derived from |
| 39 |
|
| * Class::Load::XS by Matt S. Trout and contributors. |
| 40 |
|
| * |
| 41 |
|
| * Copyright (c) the Class::Load::XS authors. |
| 42 |
|
| * Distributed under the same terms as Perl itself. |
| 43 |
|
| * |
| 44 |
|
| * Adapted for Module::Generic by Jacques Deguest. |
| 45 |
|
| *---------------------------------------------------------------------------*/ |
| 46 |
|
| #include "EXTERN.h" |
| 47 |
|
| #include "perl.h" |
| 48 |
|
| #include "XSUB.h" |
| 49 |
|
| |
| 50 |
|
| #include <dlfcn.h> |
| 51 |
|
| #include <stddef.h> |
| 52 |
|
| #include <string.h> |
| 53 |
|
| |
| 54 |
|
| /* |
| 55 |
|
| * Opaque handle type. |
| 56 |
|
| * libmagic defines magic_t as struct magic_set* internally; we treat it |
| 57 |
|
| * as void* since we never dereference it. |
| 58 |
|
| */ |
| 59 |
|
| typedef void* magic_t; |
| 60 |
|
| |
| 61 |
|
| /* |
| 62 |
|
| * Function pointer typedefs: mirrors <magic.h> without including it. |
| 63 |
|
| */ |
| 64 |
|
| typedef magic_t (*fn_magic_open) ( int flags ); |
| 65 |
|
| typedef void (*fn_magic_close) ( magic_t cookie ); |
| 66 |
|
| typedef int (*fn_magic_load) ( magic_t cookie, const char* filename ); |
| 67 |
|
| typedef const char* (*fn_magic_file) ( magic_t cookie, const char* filename ); |
| 68 |
|
| typedef const char* (*fn_magic_buffer) ( magic_t cookie, const void* buffer, size_t length ); |
| 69 |
|
| typedef const char* (*fn_magic_descriptor) ( magic_t cookie, int fd ); |
| 70 |
|
| typedef const char* (*fn_magic_error) ( magic_t cookie ); |
| 71 |
|
| typedef int (*fn_magic_setflags) ( magic_t cookie, int flags ); |
| 72 |
|
| typedef int (*fn_magic_getflags) ( magic_t cookie ); /* optional */ |
| 73 |
|
| typedef int (*fn_magic_version) ( void ); /* optional */ |
| 74 |
|
| typedef int (*fn_magic_compile) ( magic_t cookie, const char* filename ); /* optional */ |
| 75 |
|
| typedef int (*fn_magic_check) ( magic_t cookie, const char* filename ); /* optional */ |
| 76 |
|
| typedef int (*fn_magic_list) ( magic_t cookie, const char* filename ); /* optional */ |
| 77 |
|
| |
| 78 |
|
| /* |
| 79 |
|
| * Module-level state. |
| 80 |
|
| * Populated once in BOOT; never freed (library lives for the process). |
| 81 |
|
| */ |
| 82 |
|
| static void* _libhandle = NULL; |
| 83 |
|
| |
| 84 |
|
| /* Required; set only when dlopen succeeds */ |
| 85 |
|
| static fn_magic_open _fn_open = NULL; |
| 86 |
|
| static fn_magic_close _fn_close = NULL; |
| 87 |
|
| static fn_magic_load _fn_load = NULL; |
| 88 |
|
| static fn_magic_file _fn_file = NULL; |
| 89 |
|
| static fn_magic_buffer _fn_buffer = NULL; |
| 90 |
|
| static fn_magic_descriptor _fn_descriptor = NULL; |
| 91 |
|
| static fn_magic_error _fn_error = NULL; |
| 92 |
|
| static fn_magic_setflags _fn_setflags = NULL; |
| 93 |
|
| |
| 94 |
|
| /* Optional; NULL when absent; callers check before use */ |
| 95 |
|
| static fn_magic_getflags _fn_getflags = NULL; |
| 96 |
|
| static fn_magic_version _fn_version = NULL; |
| 97 |
|
| static fn_magic_compile _fn_compile = NULL; |
| 98 |
|
| static fn_magic_check _fn_check = NULL; |
| 99 |
|
| static fn_magic_list _fn_list = NULL; |
| 100 |
|
| |
| 101 |
|
| /* |
| 102 |
|
| * _resolve( handle, name ) |
| 103 |
|
| * Calls dlsym; returns the symbol or NULL on failure. |
| 104 |
|
| * Used for required symbols; the BOOT block checks the return value. |
| 105 |
|
| */ |
| 106 |
|
| static void* |
| 107 |
0 |
| _resolve( void* handle, const char* name ) |
| 108 |
|
| { |
| 109 |
0 |
| return( dlsym( handle, name ) ); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* |
| 113 |
|
| * _set_backend( pkg, value ) |
| 114 |
|
| * Sets $Module::Generic::File::Magic::BACKEND to the given string value. |
| 115 |
|
| */ |
| 116 |
|
| static void |
| 117 |
33 |
| _set_backend( pTHX_ const char* value ) |
| 118 |
|
| { |
| 119 |
|
| /* Use GV_ADD | GV_ADDMULTI to mark the symbol as "seen multiple times", |
| 120 |
|
| * suppressing the "used only once" warning from DynaLoader. */ |
| 121 |
33 |
| SV* sv = get_sv( "Module::Generic::File::Magic::BACKEND", GV_ADD | GV_ADDMULTI ); |
| 122 |
33 |
| sv_setpv( sv, value ); |
| 123 |
33 |
| SvPOK_on( sv ); |
| 124 |
33 |
| } |
| 125 |
|
| |
| 126 |
|
| /* mg_get_sv_arg: safely fetch argument n from the XS stack. |
| 127 |
|
| * Returns &PL_sv_undef when n is beyond the actual argument count, preventing the |
| 128 |
|
| * "uninitialized value in subroutine entry" warning that xsubpp-declared SV* parameters |
| 129 |
|
| * generate when undef is passed. */ |
| 130 |
|
| static SV* |
| 131 |
5543 |
| mg_get_sv_arg( I32 n, I32 count, SV** sp ) |
| 132 |
|
| { |
| 133 |
|
| SV* sv; |
| 134 |
5543 |
▶ | if( n >= count ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( n >= count ) |
0 |
5543 |
5543 |
|
| 135 |
0 |
| return( &PL_sv_undef ); |
| 136 |
5543 |
| sv = sp[n]; |
| 137 |
|
| /* For tied scalars, SvGETMAGIC alone does not update SvROK. |
| 138 |
|
| * We need a full copy via sv_mortalcopy which triggers FETCH and returns a new SV |
| 139 |
|
| * reflecting the actual underlying value. |
| 140 |
|
| * This mirrors what Scalar::Util::reftype() does internally. */ |
| 141 |
5543 |
▶ | if( SvGMAGICAL( sv ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( SvGMAGICAL( sv ) ) |
0 |
5543 |
5543 |
|
| 142 |
0 |
| sv = sv_mortalcopy( sv ); |
| 143 |
5543 |
| return( sv ); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* |
| 147 |
|
| * mg_is_valid_class_name( $class ) |
| 148 |
|
| * |
| 149 |
|
| * Validates that the supplied class name is syntactically valid without autovivifying |
| 150 |
|
| * any stash. |
| 151 |
|
| * |
| 152 |
|
| * Accepted format: |
| 153 |
|
| * |
| 154 |
|
| * Foo |
| 155 |
|
| * Foo::Bar |
| 156 |
|
| * Foo::Bar::Baz |
| 157 |
|
| * |
| 158 |
|
| * Rejected: |
| 159 |
|
| * |
| 160 |
|
| * "" |
| 161 |
|
| * ::Foo |
| 162 |
|
| * Foo: |
| 163 |
|
| * Foo:::Bar |
| 164 |
|
| * Foo-Bar |
| 165 |
|
| * Foo Bar |
| 166 |
|
| * |
| 167 |
|
| * On success, returns TRUE and provides direct access to the underlying |
| 168 |
|
| * class name string and length. |
| 169 |
|
| * |
| 170 |
|
| * This performs only syntactic validation; it does not check whether the |
| 171 |
|
| * package actually exists. |
| 172 |
|
| */ |
| 173 |
|
| static bool |
| 174 |
88 |
| mg_is_valid_class_name( pTHX_ SV* klass, const char** namep, STRLEN* lenp ) |
| 175 |
|
| { |
| 176 |
|
| const char* s; |
| 177 |
|
| STRLEN len; |
| 178 |
|
| STRLEN i; |
| 179 |
88 |
| int need_ident = 1; |
| 180 |
|
| |
| 181 |
88 |
▶ | if( !klass ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !klass ) |
0 |
88 |
88 |
|
| 182 |
|
| { |
| 183 |
0 |
| return( FALSE ); |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
88 |
| SvGETMAGIC( klass ); |
| 187 |
|
| |
| 188 |
88 |
▶ | if( !SvOK( klass ) || SvROK( klass ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( !SvOK( klass ) || SvROK( klass ) ) |
88 |
0 |
88 |
| if( !SvOK( klass ) || SvROK( klass ) ) |
0 |
88 |
88 |
|
| 189 |
|
| { |
| 190 |
0 |
| return( FALSE ); |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
88 |
| s = SvPV( klass, len ); |
| 194 |
|
| |
| 195 |
88 |
▶ | if( len == 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( len == 0 ) |
0 |
88 |
88 |
|
| 196 |
|
| { |
| 197 |
0 |
| return( FALSE ); |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
1804 |
▶ | for( i = 0; i < len; i++ ) |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; i < len; i++ ) |
1716 |
88 |
1804 |
|
| 201 |
|
| { |
| 202 |
1716 |
| unsigned char c = (unsigned char)s[i]; |
| 203 |
|
| |
| 204 |
1716 |
▶ | if( need_ident ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( need_ident ) |
253 |
1463 |
1716 |
|
| 205 |
|
| { |
| 206 |
253 |
▶ | if( !( ( c >= 'A' && c <= 'Z' ) || |
Branch: 4 branches
| Branch | True | False | Total |
| if( !( ( c >= 'A' && c <= 'Z' ) || |
253 |
0 |
253 |
| if( !( ( c >= 'A' && c <= 'Z' ) || |
9 |
244 |
253 |
| if( !( ( c >= 'A' && c <= 'Z' ) || |
9 |
0 |
9 |
| if( !( ( c >= 'A' && c <= 'Z' ) || |
0 |
0 |
0 |
|
| 207 |
9 |
▶ | ( c >= 'a' && c <= 'z' ) || |
Branch: 1 branch
| Branch | True | False | Total |
| ( c >= 'a' && c <= 'z' ) || |
0 |
9 |
9 |
|
| 208 |
|
| c == '_' ) ) |
| 209 |
|
| { |
| 210 |
0 |
| return( FALSE ); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
253 |
| need_ident = 0; |
| 214 |
|
| } |
| 215 |
1463 |
▶ | else if( c == ':' ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if( c == ':' ) |
165 |
1298 |
1463 |
|
| 216 |
|
| { |
| 217 |
165 |
▶ | if( i + 1 >= len || s[i + 1] != ':' ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( i + 1 >= len || s[i + 1] != ':' ) |
165 |
0 |
165 |
| if( i + 1 >= len || s[i + 1] != ':' ) |
0 |
165 |
165 |
|
| 218 |
|
| { |
| 219 |
0 |
| return( FALSE ); |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
165 |
| i++; |
| 223 |
165 |
| need_ident = 1; |
| 224 |
|
| } |
| 225 |
1298 |
▶ | else if( !( ( c >= 'A' && c <= 'Z' ) || |
Branch: 4 branches
| Branch | True | False | Total |
| else if( !( ( c >= 'A' && c <= 'Z' ) || |
1293 |
5 |
1298 |
| else if( !( ( c >= 'A' && c <= 'Z' ) || |
1241 |
52 |
1293 |
| else if( !( ( c >= 'A' && c <= 'Z' ) || |
1241 |
5 |
1246 |
| else if( !( ( c >= 'A' && c <= 'Z' ) || |
0 |
0 |
0 |
|
| 226 |
1246 |
▶ | ( c >= 'a' && c <= 'z' ) || |
Branch: 2 branches
| Branch | True | False | Total |
| ( c >= 'a' && c <= 'z' ) || |
0 |
1241 |
1241 |
| ( c >= 'a' && c <= 'z' ) || |
5 |
0 |
5 |
|
| 227 |
5 |
▶ | ( c >= '0' && c <= '9' ) || |
Branch: 1 branch
| Branch | True | False | Total |
| ( c >= '0' && c <= '9' ) || |
0 |
5 |
5 |
|
| 228 |
|
| c == '_' ) ) |
| 229 |
|
| { |
| 230 |
0 |
| return( FALSE ); |
| 231 |
|
| } |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
88 |
▶ | if( need_ident ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( need_ident ) |
0 |
88 |
88 |
|
| 235 |
|
| { |
| 236 |
0 |
| return( FALSE ); |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
88 |
| *namep = s; |
| 240 |
88 |
| *lenp = len; |
| 241 |
|
| |
| 242 |
88 |
| return( TRUE ); |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| /* |
| 246 |
|
| * mg_inc_has_class( $class ) |
| 247 |
|
| * |
| 248 |
|
| * Checks whether a class appears in %INC. |
| 249 |
|
| * |
| 250 |
|
| * The class name is converted to its corresponding .pm path: |
| 251 |
|
| * |
| 252 |
|
| * Foo::Bar -> Foo/Bar.pm |
| 253 |
|
| * |
| 254 |
|
| * and looked up directly in Perl's global %INC hash. |
| 255 |
|
| * |
| 256 |
|
| * This is the most reliable indication that a module file has been loaded through |
| 257 |
|
| * require/use. |
| 258 |
|
| * |
| 259 |
|
| * Returns TRUE if the module path exists in %INC, FALSE otherwise. |
| 260 |
|
| */ |
| 261 |
|
| static bool |
| 262 |
88 |
| mg_inc_has_class( pTHX_ const char* name, STRLEN len ) |
| 263 |
|
| { |
| 264 |
|
| HV* inc; |
| 265 |
|
| char* pm; |
| 266 |
|
| STRLEN i; |
| 267 |
88 |
| STRLEN j = 0; |
| 268 |
|
| bool found; |
| 269 |
|
| |
| 270 |
88 |
| inc = GvHV( PL_incgv ); |
| 271 |
|
| |
| 272 |
88 |
▶ | if( !inc ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !inc ) |
0 |
88 |
88 |
|
| 273 |
|
| { |
| 274 |
0 |
| return( FALSE ); |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
88 |
| Newx( pm, len + 4, char ); |
| 278 |
|
| |
| 279 |
1804 |
▶ | for( i = 0; i < len; i++ ) |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; i < len; i++ ) |
1716 |
88 |
1804 |
|
| 280 |
|
| { |
| 281 |
1716 |
▶ | if( name[i] == ':' && i + 1 < len && name[i + 1] == ':' ) |
Branch: 3 branches
| Branch | True | False | Total |
| if( name[i] == ':' && i + 1 < len && name[i + 1] == ':' ) |
165 |
1551 |
1716 |
| if( name[i] == ':' && i + 1 < len && name[i + 1] == ':' ) |
165 |
0 |
165 |
| if( name[i] == ':' && i + 1 < len && name[i + 1] == ':' ) |
165 |
0 |
165 |
|
| 282 |
|
| { |
| 283 |
165 |
| pm[j++] = '/'; |
| 284 |
165 |
| i++; |
| 285 |
|
| } |
| 286 |
|
| else |
| 287 |
|
| { |
| 288 |
1551 |
| pm[j++] = name[i]; |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
88 |
| pm[j++] = '.'; |
| 293 |
88 |
| pm[j++] = 'p'; |
| 294 |
88 |
| pm[j++] = 'm'; |
| 295 |
88 |
| pm[j] = '\0'; |
| 296 |
|
| |
| 297 |
88 |
| found = hv_exists( inc, pm, (I32)j ) ? TRUE : FALSE; |
| 298 |
|
| |
| 299 |
88 |
| Safefree( pm ); |
| 300 |
|
| |
| 301 |
88 |
| return( found ); |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| /* |
| 305 |
|
| * mg_stash_has_sub( $stash ) |
| 306 |
|
| * |
| 307 |
|
| * Iterates through a package stash and looks for at least one CODE entry. |
| 308 |
|
| * |
| 309 |
|
| * This mirrors part of the detection strategy used by Class::Load::XS. |
| 310 |
|
| * |
| 311 |
|
| * IMPORTANT: |
| 312 |
|
| * |
| 313 |
|
| * The presence of a CODE entry does not necessarily mean that a module file has been |
| 314 |
|
| * loaded. Perl may create stubs, forward declarations, or partially populated package |
| 315 |
|
| * symbols during compilation. |
| 316 |
|
| * |
| 317 |
|
| * Consequently, this helper should be considered heuristic only and must not be treated |
| 318 |
|
| * as equivalent to a positive %INC lookup. |
| 319 |
|
| * |
| 320 |
|
| * Returns TRUE if at least one CV is found in the stash. |
| 321 |
|
| */ |
| 322 |
|
| static bool |
| 323 |
0 |
| mg_stash_has_sub( pTHX_ HV* stash ) |
| 324 |
|
| { |
| 325 |
|
| HE* he; |
| 326 |
|
| |
| 327 |
0 |
| hv_iterinit( stash ); |
| 328 |
|
| |
| 329 |
0 |
▶ | while( ( he = hv_iternext( stash ) ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| while( ( he = hv_iternext( stash ) ) ) |
0 |
0 |
0 |
|
| 330 |
|
| { |
| 331 |
0 |
| GV* gv = (GV*)HeVAL( he ); |
| 332 |
|
| STRLEN keylen; |
| 333 |
0 |
▶ | const char* key = HePV( he, keylen ); |
Branch: 1 branch
| Branch | True | False | Total |
| const char* key = HePV( he, keylen ); |
0 |
0 |
0 |
|
| 334 |
0 |
| SV* cv = NULL; |
| 335 |
|
| |
| 336 |
0 |
▶ | if( isGV( gv ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( isGV( gv ) ) |
0 |
0 |
0 |
|
| 337 |
|
| { |
| 338 |
0 |
▶ | cv = (SV*)GvCVu( gv ); |
Branch: 1 branch
| Branch | True | False | Total |
| cv = (SV*)GvCVu( gv ); |
0 |
0 |
0 |
|
| 339 |
|
| } |
| 340 |
|
| else |
| 341 |
|
| { |
| 342 |
0 |
| gv_init( gv, stash, key, keylen, GV_ADDMULTI ); |
| 343 |
0 |
| cv = (SV*)GvCV( gv ); |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
0 |
▶ | if( cv ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( cv ) |
0 |
0 |
0 |
|
| 347 |
|
| { |
| 348 |
0 |
| return( TRUE ); |
| 349 |
|
| } |
| 350 |
|
| } |
| 351 |
|
| |
| 352 |
0 |
| return( FALSE ); |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
|
| /* |
| 356 |
|
| * mg_stash_has_loaded_markers( $stash ) |
| 357 |
|
| * |
| 358 |
|
| * Checks whether a package stash contains strong indicators that the package has |
| 359 |
|
| * been initialised. |
| 360 |
|
| * |
| 361 |
|
| * Current markers: |
| 362 |
|
| * |
| 363 |
|
| * $VERSION |
| 364 |
|
| * @ISA |
| 365 |
|
| * |
| 366 |
|
| * These are commonly populated by fully loaded Perl modules and provide a safer |
| 367 |
|
| * fallback than simply detecting the existence of a package stash. |
| 368 |
|
| * |
| 369 |
|
| * Unlike %INC, this remains heuristic because a package may manually populate these |
| 370 |
|
| * symbols without having been loaded from a .pm file. |
| 371 |
|
| * |
| 372 |
|
| * Returns TRUE if any recognised marker is found. |
| 373 |
|
| */ |
| 374 |
|
| /* |
| 375 |
|
| * NOTE: |
| 376 |
|
| * |
| 377 |
|
| * We intentionally do NOT treat the mere presence of CODE symbols as proof that a |
| 378 |
|
| * module has been loaded. Perl may create stubs and package entries during |
| 379 |
|
| * compilation long before a corresponding require() succeeds. |
| 380 |
|
| * |
| 381 |
|
| * This avoids false positives such as: |
| 382 |
|
| * |
| 383 |
|
| * MIME::Base64::encode_base64(...) |
| 384 |
|
| * |
| 385 |
|
| * where the symbol may exist but MIME::Base64 has not yet been loaded. |
| 386 |
|
| */ |
| 387 |
|
| static bool |
| 388 |
29 |
| mg_stash_has_loaded_markers( pTHX_ HV* stash ) |
| 389 |
|
| { |
| 390 |
|
| SV** svp; |
| 391 |
|
| GV* gv; |
| 392 |
|
| SV* sv; |
| 393 |
|
| AV* av; |
| 394 |
|
| |
| 395 |
29 |
| svp = hv_fetchs( stash, "VERSION", 0 ); |
| 396 |
|
| |
| 397 |
29 |
▶ | if( svp && *svp && isGV( *svp ) ) |
Branch: 3 branches
| Branch | True | False | Total |
| if( svp && *svp && isGV( *svp ) ) |
0 |
29 |
29 |
| if( svp && *svp && isGV( *svp ) ) |
0 |
0 |
0 |
| if( svp && *svp && isGV( *svp ) ) |
0 |
0 |
0 |
|
| 398 |
|
| { |
| 399 |
0 |
| gv = (GV*)*svp; |
| 400 |
0 |
| sv = GvSV( gv ); |
| 401 |
|
| |
| 402 |
0 |
▶ | if( sv ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( sv ) |
0 |
0 |
0 |
|
| 403 |
|
| { |
| 404 |
0 |
▶ | if( SvROK( sv ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( SvROK( sv ) ) |
0 |
0 |
0 |
|
| 405 |
|
| { |
| 406 |
0 |
▶ | if( sv_isobject( sv ) || SvOK( SvRV( sv ) ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( sv_isobject( sv ) || SvOK( SvRV( sv ) ) ) |
0 |
0 |
0 |
| if( sv_isobject( sv ) || SvOK( SvRV( sv ) ) ) |
0 |
0 |
0 |
|
| 407 |
|
| { |
| 408 |
0 |
| return( TRUE ); |
| 409 |
|
| } |
| 410 |
|
| } |
| 411 |
0 |
▶ | else if( SvOK( sv ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if( SvOK( sv ) ) |
0 |
0 |
0 |
|
| 412 |
|
| { |
| 413 |
0 |
| return( TRUE ); |
| 414 |
|
| } |
| 415 |
|
| } |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
29 |
| svp = hv_fetchs( stash, "ISA", 0 ); |
| 419 |
|
| |
| 420 |
29 |
▶ | if( svp && *svp && isGV( *svp ) ) |
Branch: 3 branches
| Branch | True | False | Total |
| if( svp && *svp && isGV( *svp ) ) |
7 |
22 |
29 |
| if( svp && *svp && isGV( *svp ) ) |
7 |
0 |
7 |
| if( svp && *svp && isGV( *svp ) ) |
7 |
0 |
7 |
|
| 421 |
|
| { |
| 422 |
7 |
| gv = (GV*)*svp; |
| 423 |
7 |
| av = GvAV( gv ); |
| 424 |
|
| |
| 425 |
7 |
▶ | if( av && av_len( av ) != -1 ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( av && av_len( av ) != -1 ) |
7 |
0 |
7 |
| if( av && av_len( av ) != -1 ) |
7 |
0 |
7 |
|
| 426 |
|
| { |
| 427 |
7 |
| return( TRUE ); |
| 428 |
|
| } |
| 429 |
|
| } |
| 430 |
|
| |
| 431 |
22 |
| return( FALSE ); |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| /* |
| 435 |
|
| * XS MODULE |
| 436 |
|
| */ |
| 437 |
|
| |
| 438 |
|
| /*--------------------------------------------------------------------------- |
| 439 |
|
| * Module::Generic::File::Magic - Magic.xs |
| 440 |
|
| *---------------------------------------------------------------------------*/ |
| 441 |
|
| MODULE = Module::Generic PACKAGE = Module::Generic::File::Magic |
| 442 |
|
| |
| 443 |
|
| PROTOTYPES: DISABLE |
| 444 |
|
| |
| 445 |
|
| #--------------------------------------------------------------------------- |
| 446 |
|
| # BOOT block |
| 447 |
|
| # Executed once when Perl's DynaLoader loads this .so. |
| 448 |
|
| # |
| 449 |
|
| # Tries candidate sonames in order. If none loads, sets $BACKEND = "json" |
| 450 |
|
| # and returns, but no croak. The Perl layer detects this and uses the |
| 451 |
|
| # pure-Perl JSON backend. |
| 452 |
|
| # |
| 453 |
|
| # If dlopen succeeds but a required symbol is missing (corrupted or extremely |
| 454 |
|
| # old libmagic), we do croak, because that is a genuine installation problem |
| 455 |
|
| # that the user must fix. |
| 456 |
|
| #--------------------------------------------------------------------------- |
| 457 |
|
| BOOT: |
| 458 |
|
| { |
| 459 |
|
| static const char* candidates[] = { |
| 460 |
|
| "libmagic.so.1", |
| 461 |
|
| "libmagic.so", |
| 462 |
|
| "libmagic.1.dylib", |
| 463 |
|
| "libmagic.dylib", |
| 464 |
|
| NULL |
| 465 |
|
| }; |
| 466 |
|
| int i; |
| 467 |
|
| void* sym; |
| 468 |
|
| |
| 469 |
165 |
▶ | for( i = 0; candidates[i] != NULL; i++ ) |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 0; candidates[i] != NULL; i++ ) |
132 |
33 |
165 |
|
| 470 |
|
| { |
| 471 |
132 |
| _libhandle = dlopen( candidates[i], RTLD_LAZY | RTLD_GLOBAL ); |
| 472 |
132 |
▶ | if( _libhandle != NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( _libhandle != NULL ) |
0 |
132 |
132 |
|
| 473 |
0 |
| break; |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
33 |
▶ | if( _libhandle == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( _libhandle == NULL ) |
33 |
0 |
33 |
|
| 477 |
|
| { |
| 478 |
|
| /* libmagic not installed: degrade gracefully to JSON backend */ |
| 479 |
33 |
| _set_backend( aTHX_ "json" ); |
| 480 |
|
| } |
| 481 |
|
| else |
| 482 |
|
| { |
| 483 |
|
| /* Resolve required symbols — croak on failure (corrupt install) */ |
| 484 |
0 |
| _fn_open = (fn_magic_open) _resolve( _libhandle, "magic_open" ); |
| 485 |
0 |
| _fn_close = (fn_magic_close) _resolve( _libhandle, "magic_close" ); |
| 486 |
0 |
| _fn_load = (fn_magic_load) _resolve( _libhandle, "magic_load" ); |
| 487 |
0 |
| _fn_file = (fn_magic_file) _resolve( _libhandle, "magic_file" ); |
| 488 |
0 |
| _fn_buffer = (fn_magic_buffer) _resolve( _libhandle, "magic_buffer" ); |
| 489 |
0 |
| _fn_descriptor = (fn_magic_descriptor) _resolve( _libhandle, "magic_descriptor" ); |
| 490 |
0 |
| _fn_error = (fn_magic_error) _resolve( _libhandle, "magic_error" ); |
| 491 |
0 |
| _fn_setflags = (fn_magic_setflags) _resolve( _libhandle, "magic_setflags" ); |
| 492 |
|
| |
| 493 |
|
| /* Verify all required symbols resolved */ |
| 494 |
0 |
▶ | if( !_fn_open || !_fn_close || !_fn_load || !_fn_file |
Branch: 4 branches
| Branch | True | False | Total |
| if( !_fn_open || !_fn_close || !_fn_load || !_fn_file |
0 |
0 |
0 |
| if( !_fn_open || !_fn_close || !_fn_load || !_fn_file |
0 |
0 |
0 |
| if( !_fn_open || !_fn_close || !_fn_load || !_fn_file |
0 |
0 |
0 |
| if( !_fn_open || !_fn_close || !_fn_load || !_fn_file |
0 |
0 |
0 |
|
| 495 |
0 |
▶ | || !_fn_buffer || !_fn_descriptor || !_fn_error || !_fn_setflags ) |
Branch: 4 branches
| Branch | True | False | Total |
| || !_fn_buffer || !_fn_descriptor || !_fn_error || !_fn_setflags ) |
0 |
0 |
0 |
| || !_fn_buffer || !_fn_descriptor || !_fn_error || !_fn_setflags ) |
0 |
0 |
0 |
| || !_fn_buffer || !_fn_descriptor || !_fn_error || !_fn_setflags ) |
0 |
0 |
0 |
| || !_fn_buffer || !_fn_descriptor || !_fn_error || !_fn_setflags ) |
0 |
0 |
0 |
|
| 496 |
|
| { |
| 497 |
0 |
| Perl_croak( aTHX_ |
| 498 |
|
| "Module::Generic::File::Magic: one or more required libmagic " |
| 499 |
|
| "symbols could not be resolved. Your libmagic installation " |
| 500 |
|
| "may be corrupt.\ndlopen error: %s\n", |
| 501 |
|
| dlerror() |
| 502 |
|
| ); |
| 503 |
|
| } |
| 504 |
|
| |
| 505 |
|
| /* Resolve optional symbols; NULL is fine */ |
| 506 |
0 |
| _fn_getflags = (fn_magic_getflags) dlsym( _libhandle, "magic_getflags" ); |
| 507 |
0 |
| _fn_version = (fn_magic_version) dlsym( _libhandle, "magic_version" ); |
| 508 |
0 |
| _fn_compile = (fn_magic_compile) dlsym( _libhandle, "magic_compile" ); |
| 509 |
0 |
| _fn_check = (fn_magic_check) dlsym( _libhandle, "magic_check" ); |
| 510 |
0 |
| _fn_list = (fn_magic_list) dlsym( _libhandle, "magic_list" ); |
| 511 |
|
| |
| 512 |
0 |
| _set_backend( aTHX_ "xs" ); |
| 513 |
|
| } |
| 514 |
|
| } |
| 515 |
|
| |
| 516 |
|
| # Backend predicate |
| 517 |
|
| |
| 518 |
|
| # magic_backend() -> "xs" | "json" |
| 519 |
|
| const char* |
| 520 |
|
| magic_backend() |
| 521 |
|
| CODE: |
| 522 |
0 |
▶ | RETVAL = ( _libhandle != NULL ) ? "xs" : "json"; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _libhandle != NULL ) ? "xs" : "json"; |
0 |
0 |
0 |
|
| 523 |
|
| OUTPUT: |
| 524 |
|
| RETVAL |
| 525 |
|
| |
| 526 |
|
| # XS backend functions |
| 527 |
|
| # All return undef / 0 when libmagic is absent; gated by magic_backend() in the Perl layer. |
| 528 |
|
| |
| 529 |
|
| IV |
| 530 |
|
| magic_open( flags ) |
| 531 |
|
| int flags |
| 532 |
|
| CODE: |
| 533 |
0 |
▶ | RETVAL = _fn_open ? (IV)( _fn_open( flags ) ) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = _fn_open ? (IV)( _fn_open( flags ) ) : 0; |
0 |
0 |
0 |
| RETVAL = _fn_open ? (IV)( _fn_open( flags ) ) : 0; |
0 |
0 |
0 |
|
| 534 |
|
| OUTPUT: |
| 535 |
|
| RETVAL |
| 536 |
|
| |
| 537 |
|
| void |
| 538 |
|
| magic_close( cookie ) |
| 539 |
|
| IV cookie |
| 540 |
|
| CODE: |
| 541 |
0 |
▶ | if( _fn_close && cookie ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( _fn_close && cookie ) |
0 |
0 |
0 |
| if( _fn_close && cookie ) |
0 |
0 |
0 |
|
| 542 |
0 |
| _fn_close( (magic_t)(cookie) ); |
| 543 |
|
| |
| 544 |
|
| int |
| 545 |
|
| magic_load( cookie, filename ) |
| 546 |
|
| IV cookie |
| 547 |
|
| SV* filename |
| 548 |
|
| CODE: |
| 549 |
0 |
▶ | if( !_fn_load ) { RETVAL = -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_load ) { RETVAL = -1; } |
0 |
0 |
0 |
|
| 550 |
|
| else |
| 551 |
|
| { |
| 552 |
0 |
▶ | const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
0 |
0 |
0 |
|
| 553 |
0 |
| RETVAL = _fn_load( (magic_t)(cookie), path ); |
| 554 |
|
| } |
| 555 |
|
| OUTPUT: |
| 556 |
|
| RETVAL |
| 557 |
|
| |
| 558 |
|
| SV* |
| 559 |
|
| magic_file( cookie, filename ) |
| 560 |
|
| IV cookie |
| 561 |
|
| const char* filename |
| 562 |
|
| CODE: |
| 563 |
0 |
▶ | if( !_fn_file ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_file ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 564 |
|
| else |
| 565 |
|
| { |
| 566 |
0 |
| const char* result = _fn_file( (magic_t)(cookie), filename ); |
| 567 |
0 |
▶ | RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 568 |
|
| } |
| 569 |
|
| OUTPUT: |
| 570 |
|
| RETVAL |
| 571 |
|
| |
| 572 |
|
| SV* |
| 573 |
|
| magic_buffer( cookie, buffer ) |
| 574 |
|
| IV cookie |
| 575 |
|
| SV* buffer |
| 576 |
|
| CODE: |
| 577 |
0 |
▶ | if( !_fn_buffer ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_buffer ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 578 |
|
| else |
| 579 |
|
| { |
| 580 |
|
| STRLEN len; |
| 581 |
0 |
| const char* ptr = SvPVbyte( buffer, len ); |
| 582 |
0 |
| const char* result = _fn_buffer( (magic_t)(cookie), (const void*)ptr, (size_t)len ); |
| 583 |
0 |
▶ | RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 584 |
|
| } |
| 585 |
|
| OUTPUT: |
| 586 |
|
| RETVAL |
| 587 |
|
| |
| 588 |
|
| SV* |
| 589 |
|
| magic_descriptor( cookie, fd ) |
| 590 |
|
| IV cookie |
| 591 |
|
| int fd |
| 592 |
|
| CODE: |
| 593 |
0 |
▶ | if( !_fn_descriptor ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_descriptor ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 594 |
|
| else |
| 595 |
|
| { |
| 596 |
0 |
| const char* result = _fn_descriptor( (magic_t)(cookie), fd ); |
| 597 |
0 |
▶ | RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = result ? newSVpv( result, 0 ) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 598 |
|
| } |
| 599 |
|
| OUTPUT: |
| 600 |
|
| RETVAL |
| 601 |
|
| |
| 602 |
|
| SV* |
| 603 |
|
| magic_error( cookie ) |
| 604 |
|
| IV cookie |
| 605 |
|
| CODE: |
| 606 |
0 |
▶ | if( !_fn_error ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_error ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 607 |
|
| else |
| 608 |
|
| { |
| 609 |
0 |
| const char* msg = _fn_error( (magic_t)(cookie) ); |
| 610 |
0 |
▶ | RETVAL = msg ? newSVpv( msg, 0 ) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = msg ? newSVpv( msg, 0 ) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 611 |
|
| } |
| 612 |
|
| OUTPUT: |
| 613 |
|
| RETVAL |
| 614 |
|
| |
| 615 |
|
| int |
| 616 |
|
| magic_setflags( cookie, flags ) |
| 617 |
|
| IV cookie |
| 618 |
|
| int flags |
| 619 |
|
| CODE: |
| 620 |
0 |
▶ | RETVAL = _fn_setflags ? _fn_setflags( (magic_t)(cookie), flags ) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = _fn_setflags ? _fn_setflags( (magic_t)(cookie), flags ) : -1; |
0 |
0 |
0 |
| RETVAL = _fn_setflags ? _fn_setflags( (magic_t)(cookie), flags ) : -1; |
0 |
0 |
0 |
|
| 621 |
|
| OUTPUT: |
| 622 |
|
| RETVAL |
| 623 |
|
| |
| 624 |
|
| SV* |
| 625 |
|
| magic_getflags( cookie ) |
| 626 |
|
| IV cookie |
| 627 |
|
| CODE: |
| 628 |
0 |
▶ | RETVAL = ( _fn_getflags && cookie ) |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_getflags && cookie ) |
0 |
0 |
0 |
|
| 629 |
0 |
| ? newSViv( _fn_getflags( (magic_t)(cookie) ) ) |
| 630 |
0 |
▶ | : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| : &PL_sv_undef; |
0 |
0 |
0 |
|
| 631 |
|
| OUTPUT: |
| 632 |
|
| RETVAL |
| 633 |
|
| |
| 634 |
|
| SV* |
| 635 |
|
| magic_version() |
| 636 |
|
| CODE: |
| 637 |
0 |
▶ | RETVAL = _fn_version ? newSViv( _fn_version() ) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = _fn_version ? newSViv( _fn_version() ) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 638 |
|
| OUTPUT: |
| 639 |
|
| RETVAL |
| 640 |
|
| |
| 641 |
|
| SV* |
| 642 |
|
| magic_compile( cookie, filename ) |
| 643 |
|
| IV cookie |
| 644 |
|
| SV* filename |
| 645 |
|
| CODE: |
| 646 |
0 |
▶ | if( !_fn_compile ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_compile ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 647 |
|
| else |
| 648 |
|
| { |
| 649 |
0 |
▶ | const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
0 |
0 |
0 |
|
| 650 |
0 |
| RETVAL = newSViv( _fn_compile( (magic_t)(cookie), path ) ); |
| 651 |
|
| } |
| 652 |
|
| OUTPUT: |
| 653 |
|
| RETVAL |
| 654 |
|
| |
| 655 |
|
| SV* |
| 656 |
|
| magic_check( cookie, filename ) |
| 657 |
|
| IV cookie |
| 658 |
|
| SV* filename |
| 659 |
|
| CODE: |
| 660 |
0 |
▶ | if( !_fn_check ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_check ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 661 |
|
| else |
| 662 |
|
| { |
| 663 |
0 |
▶ | const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
0 |
0 |
0 |
|
| 664 |
0 |
| RETVAL = newSViv( _fn_check( (magic_t)(cookie), path ) ); |
| 665 |
|
| } |
| 666 |
|
| OUTPUT: |
| 667 |
|
| RETVAL |
| 668 |
|
| |
| 669 |
|
| SV* |
| 670 |
|
| magic_list( cookie, filename ) |
| 671 |
|
| IV cookie |
| 672 |
|
| SV* filename |
| 673 |
|
| CODE: |
| 674 |
0 |
▶ | if( !_fn_list ) { RETVAL = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if( !_fn_list ) { RETVAL = &PL_sv_undef; } |
0 |
0 |
0 |
|
| 675 |
|
| else |
| 676 |
|
| { |
| 677 |
0 |
▶ | const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* path = SvOK( filename ) ? SvPVbyte_nolen( filename ) : NULL; |
0 |
0 |
0 |
|
| 678 |
0 |
| RETVAL = newSViv( _fn_list( (magic_t)(cookie), path ) ); |
| 679 |
|
| } |
| 680 |
|
| OUTPUT: |
| 681 |
|
| RETVAL |
| 682 |
|
| |
| 683 |
|
| # Availability predicates for optional symbols |
| 684 |
|
| |
| 685 |
|
| int |
| 686 |
|
| magic_has_getflags() |
| 687 |
|
| CODE: |
| 688 |
0 |
▶ | RETVAL = ( _fn_getflags != NULL ) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_getflags != NULL ) ? 1 : 0; |
0 |
0 |
0 |
|
| 689 |
|
| OUTPUT: |
| 690 |
|
| RETVAL |
| 691 |
|
| |
| 692 |
|
| int |
| 693 |
|
| magic_has_version() |
| 694 |
|
| CODE: |
| 695 |
0 |
▶ | RETVAL = ( _fn_version != NULL ) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_version != NULL ) ? 1 : 0; |
0 |
0 |
0 |
|
| 696 |
|
| OUTPUT: |
| 697 |
|
| RETVAL |
| 698 |
|
| |
| 699 |
|
| int |
| 700 |
|
| magic_has_compile() |
| 701 |
|
| CODE: |
| 702 |
0 |
▶ | RETVAL = ( _fn_compile != NULL ) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_compile != NULL ) ? 1 : 0; |
0 |
0 |
0 |
|
| 703 |
|
| OUTPUT: |
| 704 |
|
| RETVAL |
| 705 |
|
| |
| 706 |
|
| int |
| 707 |
|
| magic_has_check() |
| 708 |
|
| CODE: |
| 709 |
0 |
▶ | RETVAL = ( _fn_check != NULL ) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_check != NULL ) ? 1 : 0; |
0 |
0 |
0 |
|
| 710 |
|
| OUTPUT: |
| 711 |
|
| RETVAL |
| 712 |
|
| |
| 713 |
|
| int |
| 714 |
|
| magic_has_list() |
| 715 |
|
| CODE: |
| 716 |
0 |
▶ | RETVAL = ( _fn_list != NULL ) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ( _fn_list != NULL ) ? 1 : 0; |
0 |
0 |
0 |
|
| 717 |
|
| OUTPUT: |
| 718 |
|
| RETVAL |
| 719 |
|
| |
| 720 |
|
| # --------------------------------------------------------------------------- |
| 721 |
|
| # Module::Generic utility methods |
| 722 |
|
| # |
| 723 |
|
| # XS implementations of the most frequently called type-inspection helpers. |
| 724 |
|
| # Moving them here eliminates Perl dispatch overhead and the temporary |
| 725 |
|
| # allocations created by the Perl call stack on every invocation. |
| 726 |
|
| # |
| 727 |
|
| # These are installed in Module::Generic and inherited by all subclasses via |
| 728 |
|
| # the normal @ISA mechanism; no changes needed in subclasses. |
| 729 |
|
| # --------------------------------------------------------------------------- |
| 730 |
|
| |
| 731 |
|
| MODULE = Module::Generic PACKAGE = Module::Generic |
| 732 |
|
| |
| 733 |
|
| PROTOTYPES: DISABLE |
| 734 |
|
| |
| 735 |
|
| # --------------------------------------------------------------------------- |
| 736 |
|
| # _get_args_as_array( $self, @args ) |
| 737 |
|
| # |
| 738 |
|
| # If called with a single arrayref argument, returns it directly. |
| 739 |
|
| # Otherwise wraps all arguments in a new arrayref. |
| 740 |
|
| # Returns [] if no arguments provided. |
| 741 |
|
| # --------------------------------------------------------------------------- |
| 742 |
|
| |
| 743 |
|
| SV* |
| 744 |
|
| _get_args_as_array( self, ... ) |
| 745 |
|
| SV* self |
| 746 |
|
| PREINIT: |
| 747 |
|
| AV* av; |
| 748 |
|
| I32 i; |
| 749 |
|
| CODE: |
| 750 |
2698 |
▶ | if( items <= 1 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( items <= 1 ) |
2676 |
22 |
2698 |
|
| 751 |
|
| { |
| 752 |
|
| /* No args: return [] */ |
| 753 |
2676 |
| RETVAL = newRV_noinc( (SV*)newAV() ); |
| 754 |
|
| } |
| 755 |
22 |
▶ | else if( items == 2 && |
Branch: 1 branch
| Branch | True | False | Total |
| else if( items == 2 && |
16 |
6 |
22 |
|
| 756 |
16 |
▶ | SvROK( ST(1) ) && |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK( ST(1) ) && |
9 |
7 |
16 |
|
| 757 |
9 |
▶ | SvTYPE( SvRV( ST(1) ) ) == SVt_PVAV ) |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( ST(1) ) ) == SVt_PVAV ) |
8 |
1 |
9 |
|
| 758 |
|
| { |
| 759 |
|
| /* Single arrayref argument: return it directly. |
| 760 |
|
| * No SvOK check: mirrors _is_array which does not require SvOK, |
| 761 |
|
| * and blessed arrayrefs may not have scalar-value flags set. */ |
| 762 |
8 |
| RETVAL = SvREFCNT_inc( ST(1) ); |
| 763 |
|
| } |
| 764 |
|
| else |
| 765 |
|
| { |
| 766 |
|
| /* Multiple args (or single non-arrayref): wrap in new arrayref. |
| 767 |
|
| * Use newSVsv() to copy each SV rather than SvREFCNT_inc() so that |
| 768 |
|
| * the array holds independent mutable copies, not read-only aliases |
| 769 |
|
| * to the Perl call stack, thus mirroring Perl's [ @_ ] behaviour. */ |
| 770 |
14 |
| av = newAV(); |
| 771 |
14 |
| av_extend( av, items - 2 ); |
| 772 |
40 |
▶ | for( i = 1; i < items; i++ ) |
Branch: 1 branch
| Branch | True | False | Total |
| for( i = 1; i < items; i++ ) |
26 |
14 |
40 |
|
| 773 |
|
| { |
| 774 |
26 |
| av_store( av, i - 1, newSVsv( ST(i) ) ); |
| 775 |
|
| } |
| 776 |
14 |
| RETVAL = newRV_noinc( (SV*)av ); |
| 777 |
|
| } |
| 778 |
|
| OUTPUT: |
| 779 |
|
| RETVAL |
| 780 |
|
| |
| 781 |
|
| # --------------------------------------------------------------------------- |
| 782 |
|
| # _is_array( $self, $val ) |
| 783 |
|
| # |
| 784 |
|
| # Returns true if reftype($val) eq 'ARRAY'. |
| 785 |
|
| # Uses SvTYPE() directly; one integer comparison, no string allocation. |
| 786 |
|
| # --------------------------------------------------------------------------- |
| 787 |
|
| |
| 788 |
|
| int |
| 789 |
|
| _is_array( self, ... ) |
| 790 |
|
| SV* self |
| 791 |
|
| CODE: |
| 792 |
|
| { |
| 793 |
1524 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 794 |
1524 |
▶ | RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVAV ) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVAV ) ? 1 : 0; |
61 |
1463 |
1524 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVAV ) ? 1 : 0; |
52 |
9 |
61 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVAV ) ? 1 : 0; |
1475 |
49 |
1524 |
|
| 795 |
|
| } |
| 796 |
|
| OUTPUT: |
| 797 |
|
| RETVAL |
| 798 |
|
| |
| 799 |
|
| # --------------------------------------------------------------------------- |
| 800 |
|
| # _is_class_loaded_xs( $self, $class ) |
| 801 |
|
| # |
| 802 |
|
| # XS implementation of Module::Generic::_is_class_loaded. |
| 803 |
|
| # |
| 804 |
|
| # Detection order: |
| 805 |
|
| # |
| 806 |
|
| # 1. Validate class name syntax. |
| 807 |
|
| # 2. Check %INC for the corresponding .pm file. |
| 808 |
|
| # 3. Check the package stash for loading markers. |
| 809 |
|
| # |
| 810 |
|
| # Unlike gv_stashpv(..., GV_ADD), this implementation never autovivifies |
| 811 |
|
| # namespaces and therefore cannot accidentally create packages while |
| 812 |
|
| # testing for their existence. |
| 813 |
|
| # |
| 814 |
|
| # Returns: |
| 815 |
|
| # |
| 816 |
|
| # 1 Class appears loaded. |
| 817 |
|
| # 0 Class does not appear loaded. |
| 818 |
|
| # |
| 819 |
|
| # This function is intended as a fast XS replacement for the Perl-level |
| 820 |
|
| # implementation and follows the same semantics as closely as possible. |
| 821 |
|
| # --------------------------------------------------------------------------- |
| 822 |
|
| |
| 823 |
|
| int |
| 824 |
|
| _is_class_loaded_xs( self, ... ) |
| 825 |
|
| SV* self |
| 826 |
|
| PREINIT: |
| 827 |
|
| SV* klass; |
| 828 |
|
| const char* name; |
| 829 |
|
| STRLEN len; |
| 830 |
|
| HV* stash; |
| 831 |
|
| CODE: |
| 832 |
88 |
▶ | if( items < 2 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( items < 2 ) |
0 |
88 |
88 |
|
| 833 |
|
| { |
| 834 |
0 |
| RETVAL = 0; |
| 835 |
|
| } |
| 836 |
|
| else |
| 837 |
|
| { |
| 838 |
88 |
| klass = mg_get_sv_arg( 1, items, &ST(0) ); |
| 839 |
|
| |
| 840 |
88 |
▶ | if( !mg_is_valid_class_name( aTHX_ klass, &name, &len ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !mg_is_valid_class_name( aTHX_ klass, &name, &len ) ) |
0 |
88 |
88 |
|
| 841 |
|
| { |
| 842 |
0 |
| RETVAL = 0; |
| 843 |
|
| } |
| 844 |
88 |
▶ | else if( mg_inc_has_class( aTHX_ name, len ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if( mg_inc_has_class( aTHX_ name, len ) ) |
0 |
88 |
88 |
|
| 845 |
|
| { |
| 846 |
0 |
| RETVAL = 1; |
| 847 |
|
| } |
| 848 |
|
| else |
| 849 |
|
| { |
| 850 |
88 |
| stash = gv_stashpvn( name, (U32)len, 0 ); |
| 851 |
|
| |
| 852 |
88 |
▶ | if( stash && mg_stash_has_loaded_markers( aTHX_ stash ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( stash && mg_stash_has_loaded_markers( aTHX_ stash ) ) |
29 |
59 |
88 |
| if( stash && mg_stash_has_loaded_markers( aTHX_ stash ) ) |
7 |
22 |
29 |
|
| 853 |
|
| { |
| 854 |
7 |
| RETVAL = 1; |
| 855 |
|
| } |
| 856 |
|
| else |
| 857 |
|
| { |
| 858 |
81 |
| RETVAL = 0; |
| 859 |
|
| } |
| 860 |
|
| } |
| 861 |
|
| } |
| 862 |
|
| OUTPUT: |
| 863 |
|
| RETVAL |
| 864 |
|
| |
| 865 |
|
| # --------------------------------------------------------------------------- |
| 866 |
|
| # _is_code( $self, $val ) |
| 867 |
|
| # |
| 868 |
|
| # Returns true if $val is a CODE reference (blessed or not). |
| 869 |
|
| # Uses ref() semantics: blessed coderefs also return true. |
| 870 |
|
| # --------------------------------------------------------------------------- |
| 871 |
|
| |
| 872 |
|
| int |
| 873 |
|
| _is_code( self, ... ) |
| 874 |
|
| SV* self |
| 875 |
|
| CODE: |
| 876 |
|
| { |
| 877 |
8 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 878 |
|
| /* SvROK alone (without SvOK) handles blessed coderefs correctly. |
| 879 |
|
| * SvOK fails on blessed refs because they lack scalar-value flags. |
| 880 |
|
| * Mirrors Scalar::Util::reftype($val) eq 'CODE'. */ |
| 881 |
8 |
▶ | RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVCV ) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVCV ) ? 1 : 0; |
6 |
2 |
8 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVCV ) ? 1 : 0; |
4 |
2 |
6 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVCV ) ? 1 : 0; |
0 |
8 |
8 |
|
| 882 |
|
| } |
| 883 |
|
| OUTPUT: |
| 884 |
|
| RETVAL |
| 885 |
|
| |
| 886 |
|
| # --------------------------------------------------------------------------- |
| 887 |
|
| # _is_glob( $self, $val ) |
| 888 |
|
| # |
| 889 |
|
| # Returns true if reftype($val) eq 'GLOB'. |
| 890 |
|
| # --------------------------------------------------------------------------- |
| 891 |
|
| |
| 892 |
|
| int |
| 893 |
|
| _is_glob( self, ... ) |
| 894 |
|
| SV* self |
| 895 |
|
| CODE: |
| 896 |
|
| { |
| 897 |
45 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 898 |
|
| /* Mirrors Scalar::Util::reftype($val) eq 'GLOB'. */ |
| 899 |
45 |
▶ | RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVGV ) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVGV ) ? 1 : 0; |
4 |
41 |
45 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVGV ) ? 1 : 0; |
2 |
2 |
4 |
| RETVAL = ( SvROK( val ) && SvTYPE( SvRV( val ) ) == SVt_PVGV ) ? 1 : 0; |
32 |
13 |
45 |
|
| 900 |
|
| } |
| 901 |
|
| OUTPUT: |
| 902 |
|
| RETVAL |
| 903 |
|
| |
| 904 |
|
| # --------------------------------------------------------------------------- |
| 905 |
|
| # _is_hash( $self, $val, $strict? ) |
| 906 |
|
| # |
| 907 |
|
| # Returns true if reftype($val) eq 'HASH'. |
| 908 |
|
| # Optional third argument 'strict': if defined and eq 'strict', only unblessed |
| 909 |
|
| # hashrefs return true (mirrors ref($val) eq 'HASH'). |
| 910 |
|
| # --------------------------------------------------------------------------- |
| 911 |
|
| |
| 912 |
|
| int |
| 913 |
|
| _is_hash( self, ... ) |
| 914 |
|
| SV* self |
| 915 |
|
| PREINIT: |
| 916 |
|
| SV* rv; |
| 917 |
|
| int strict; |
| 918 |
|
| STRLEN len; |
| 919 |
|
| const char* opt; |
| 920 |
|
| CODE: |
| 921 |
|
| { |
| 922 |
394 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 923 |
394 |
▶ | if( !SvROK( val ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !SvROK( val ) ) |
2 |
392 |
394 |
|
| 924 |
|
| { |
| 925 |
2 |
| RETVAL = 0; |
| 926 |
|
| } |
| 927 |
|
| else |
| 928 |
|
| { |
| 929 |
392 |
| rv = SvRV( val ); |
| 930 |
392 |
▶ | if( SvTYPE( rv ) != SVt_PVHV ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( SvTYPE( rv ) != SVt_PVHV ) |
2 |
390 |
392 |
|
| 931 |
|
| { |
| 932 |
2 |
| RETVAL = 0; |
| 933 |
|
| } |
| 934 |
|
| else |
| 935 |
|
| { |
| 936 |
|
| /* Strict mode is requested only when the option is the string |
| 937 |
|
| 'strict' or its alias 'native', mirroring ref($val) eq 'HASH' |
| 938 |
|
| (unblessed only). Any other value, or no option at all, falls back |
| 939 |
|
| to reftype($val) eq 'HASH' (blessed or unblessed). Both keywords are |
| 940 |
|
| six bytes, so the length is checked first to avoid a needless compare. */ |
| 941 |
390 |
| strict = 0; |
| 942 |
390 |
▶ | if( items > 2 && SvOK( ST(2) ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( items > 2 && SvOK( ST(2) ) ) |
18 |
372 |
390 |
| if( items > 2 && SvOK( ST(2) ) ) |
18 |
0 |
18 |
|
| 943 |
|
| { |
| 944 |
18 |
| opt = SvPV( ST(2), len ); |
| 945 |
18 |
▶ | if( len == 6 && ( memEQ( opt, "strict", 6 ) || memEQ( opt, "native", 6 ) ) ) |
Branch: 3 branches
| Branch | True | False | Total |
| if( len == 6 && ( memEQ( opt, "strict", 6 ) || memEQ( opt, "native", 6 ) ) ) |
18 |
0 |
18 |
| if( len == 6 && ( memEQ( opt, "strict", 6 ) || memEQ( opt, "native", 6 ) ) ) |
0 |
18 |
18 |
| if( len == 6 && ( memEQ( opt, "strict", 6 ) || memEQ( opt, "native", 6 ) ) ) |
0 |
0 |
0 |
|
| 946 |
|
| { |
| 947 |
18 |
| strict = 1; |
| 948 |
|
| } |
| 949 |
|
| } |
| 950 |
390 |
▶ | RETVAL = strict ? ( SvOBJECT( rv ) ? 0 : 1 ) : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = strict ? ( SvOBJECT( rv ) ? 0 : 1 ) : 1; |
18 |
372 |
390 |
| RETVAL = strict ? ( SvOBJECT( rv ) ? 0 : 1 ) : 1; |
17 |
1 |
18 |
|
| 951 |
|
| } |
| 952 |
|
| } |
| 953 |
|
| } |
| 954 |
|
| OUTPUT: |
| 955 |
|
| RETVAL |
| 956 |
|
| |
| 957 |
|
| # --------------------------------------------------------------------------- |
| 958 |
|
| # _is_integer( $self, $val ) |
| 959 |
|
| # |
| 960 |
|
| # Returns true if $val is a plain scalar matching /^[+-]?\d+$/. |
| 961 |
|
| # We use the Perl regex engine via pregexec for correctness and Unicode |
| 962 |
|
| # safety, but avoid the overhead of compiling the pattern each call by using |
| 963 |
|
| # a static compiled regexp. |
| 964 |
|
| # --------------------------------------------------------------------------- |
| 965 |
|
| |
| 966 |
|
| int |
| 967 |
|
| _is_integer( self, ... ) |
| 968 |
|
| SV* self |
| 969 |
|
| PREINIT: |
| 970 |
|
| STRLEN len; |
| 971 |
|
| const char* s; |
| 972 |
|
| const char* p; |
| 973 |
|
| const char* end; |
| 974 |
|
| CODE: |
| 975 |
|
| { |
| 976 |
35 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 977 |
35 |
▶ | if( !SvOK( val ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !SvOK( val ) ) |
1 |
34 |
35 |
|
| 978 |
|
| { |
| 979 |
1 |
| RETVAL = 0; |
| 980 |
|
| } |
| 981 |
|
| else |
| 982 |
|
| { |
| 983 |
|
| /* Use SvPV macro rather than sv_2pv_flags directly. On perl < 5.18 |
| 984 |
|
| * (at least 5.16 confirmed), calling sv_2pv_flags on a pure SVt_PV scalar |
| 985 |
|
| * returns an empty string instead of the actual PV. The SvPV macro has a |
| 986 |
|
| * fast path that reads SvPVX/SvCUR directly when the POK flag is set, |
| 987 |
|
| * avoiding the buggy code path entirely. */ |
| 988 |
34 |
| s = SvPV( val, len ); |
| 989 |
34 |
▶ | if( len == 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( len == 0 ) |
1 |
33 |
34 |
|
| 990 |
|
| { |
| 991 |
1 |
| RETVAL = 0; |
| 992 |
|
| } |
| 993 |
|
| else |
| 994 |
|
| { |
| 995 |
33 |
| p = s; |
| 996 |
33 |
| end = s + len; |
| 997 |
33 |
▶ | if( *p == '+' || *p == '-' ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( *p == '+' || *p == '-' ) |
32 |
1 |
33 |
| if( *p == '+' || *p == '-' ) |
2 |
30 |
32 |
|
| 998 |
3 |
| p++; |
| 999 |
33 |
▶ | if( p >= end ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( p >= end ) |
0 |
33 |
33 |
|
| 1000 |
|
| { |
| 1001 |
0 |
| RETVAL = 0; |
| 1002 |
|
| } |
| 1003 |
|
| else |
| 1004 |
|
| { |
| 1005 |
33 |
| RETVAL = 1; |
| 1006 |
74 |
▶ | while( p < end ) |
Branch: 1 branch
| Branch | True | False | Total |
| while( p < end ) |
45 |
29 |
74 |
|
| 1007 |
|
| { |
| 1008 |
45 |
▶ | if( *p < '0' || *p > '9' ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( *p < '0' || *p > '9' ) |
44 |
1 |
45 |
| if( *p < '0' || *p > '9' ) |
3 |
41 |
44 |
|
| 1009 |
|
| { |
| 1010 |
4 |
| RETVAL = 0; |
| 1011 |
4 |
| break; |
| 1012 |
|
| } |
| 1013 |
41 |
| p++; |
| 1014 |
|
| } |
| 1015 |
|
| } |
| 1016 |
|
| } |
| 1017 |
|
| } |
| 1018 |
|
| } |
| 1019 |
|
| OUTPUT: |
| 1020 |
|
| RETVAL |
| 1021 |
|
| |
| 1022 |
|
| # --------------------------------------------------------------------------- |
| 1023 |
|
| # _is_number( $self, $val ) |
| 1024 |
|
| # |
| 1025 |
|
| # Returns true only if $val carries actual numeric flags (SVf_IOK or SVf_NOK), |
| 1026 |
|
| # i.e. it was produced as a number, not merely looks like one. |
| 1027 |
|
| # This is exactly what the Perl implementation does via B::svref_2object, but |
| 1028 |
|
| # without the overhead of loading B or calling into it. |
| 1029 |
|
| # --------------------------------------------------------------------------- |
| 1030 |
|
| |
| 1031 |
|
| int |
| 1032 |
|
| _is_number( self, ... ) |
| 1033 |
|
| SV* self |
| 1034 |
|
| CODE: |
| 1035 |
|
| { |
| 1036 |
31 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 1037 |
|
| /* Mirrors: !defined($v) -> 0, ref($v) -> 0 |
| 1038 |
|
| * SvOK: correct here since val is a plain scalar, not a ref. |
| 1039 |
|
| * SVf_IOK/SVf_NOK: mirrors B::SVf_IOK()|B::SVf_NOK() flags check. */ |
| 1040 |
31 |
▶ | if( !SvOK( val ) || SvROK( val ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( !SvOK( val ) || SvROK( val ) ) |
29 |
2 |
31 |
| if( !SvOK( val ) || SvROK( val ) ) |
1 |
28 |
29 |
|
| 1041 |
|
| { |
| 1042 |
3 |
| RETVAL = 0; |
| 1043 |
|
| } |
| 1044 |
|
| else |
| 1045 |
|
| { |
| 1046 |
|
| /* SVf_IOK: integer value is valid; SVf_NOK: float value is valid */ |
| 1047 |
28 |
| RETVAL = ( SvFLAGS( val ) & ( SVf_IOK | SVf_NOK ) ) ? 1 : 0; |
| 1048 |
|
| } |
| 1049 |
|
| } |
| 1050 |
|
| OUTPUT: |
| 1051 |
|
| RETVAL |
| 1052 |
|
| |
| 1053 |
|
| # --------------------------------------------------------------------------- |
| 1054 |
|
| # _is_object( $self, $val ) |
| 1055 |
|
| # |
| 1056 |
|
| # Returns true if $val is a blessed reference, which is equivalent to |
| 1057 |
|
| # Scalar::Util::blessed($val), but without the Perl dispatch overhead. |
| 1058 |
|
| # --------------------------------------------------------------------------- |
| 1059 |
|
| |
| 1060 |
|
| int |
| 1061 |
|
| _is_object( self, ... ) |
| 1062 |
|
| SV* self |
| 1063 |
|
| CODE: |
| 1064 |
|
| { |
| 1065 |
3395 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 1066 |
|
| /* Mirrors Scalar::Util::blessed($val) in boolean context: 1 if blessed, 0 otherwise. */ |
| 1067 |
3395 |
▶ | RETVAL = ( SvROK( val ) && SvOBJECT( SvRV( val ) ) ) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| RETVAL = ( SvROK( val ) && SvOBJECT( SvRV( val ) ) ) ? 1 : 0; |
3190 |
205 |
3395 |
| RETVAL = ( SvROK( val ) && SvOBJECT( SvRV( val ) ) ) ? 1 : 0; |
3141 |
49 |
3190 |
| RETVAL = ( SvROK( val ) && SvOBJECT( SvRV( val ) ) ) ? 1 : 0; |
3067 |
328 |
3395 |
|
| 1068 |
|
| } |
| 1069 |
|
| OUTPUT: |
| 1070 |
|
| RETVAL |
| 1071 |
|
| |
| 1072 |
|
| # --------------------------------------------------------------------------- |
| 1073 |
|
| # _is_overloaded( $self, $val ) |
| 1074 |
|
| # |
| 1075 |
|
| # Returns true if $val is a blessed object that has any overloading. |
| 1076 |
|
| # Equivalent to overload::Overloaded($val). |
| 1077 |
|
| # HvAMAGIC(stash) checks SVf_AMAGIC (0x10000000) on the stash HV, which Perl |
| 1078 |
|
| # sets automatically when a package installs overload methods. |
| 1079 |
|
| # --------------------------------------------------------------------------- |
| 1080 |
|
| |
| 1081 |
|
| int |
| 1082 |
|
| _is_overloaded( self, ... ) |
| 1083 |
|
| SV* self |
| 1084 |
|
| PREINIT: |
| 1085 |
|
| HV* stash; |
| 1086 |
|
| GV* gv; |
| 1087 |
|
| CODE: |
| 1088 |
|
| { |
| 1089 |
4 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 1090 |
4 |
▶ | if( !SvROK( val ) || !SvOBJECT( SvRV( val ) ) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( !SvROK( val ) || !SvOBJECT( SvRV( val ) ) ) |
3 |
1 |
4 |
| if( !SvROK( val ) || !SvOBJECT( SvRV( val ) ) ) |
1 |
2 |
3 |
|
| 1091 |
|
| { |
| 1092 |
2 |
| RETVAL = 0; |
| 1093 |
|
| } |
| 1094 |
|
| else |
| 1095 |
|
| { |
| 1096 |
2 |
| stash = SvSTASH( SvRV( val ) ); |
| 1097 |
2 |
▶ | if( !stash ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !stash ) |
0 |
2 |
2 |
|
| 1098 |
|
| { |
| 1099 |
0 |
| RETVAL = 0; |
| 1100 |
|
| } |
| 1101 |
|
| else |
| 1102 |
|
| { |
| 1103 |
|
| /* gv_fetchmeth_pvn was added in perl 5.15.4 (became public in 5.16). |
| 1104 |
|
| * On older perls, fall back to gv_fetchmeth, which has been available |
| 1105 |
|
| * since perl 5.0 but lacks UTF-8 and flag support. For looking up the |
| 1106 |
|
| * "()" overload marker, the simpler API is sufficient. */ |
| 1107 |
|
| #if PERL_VERSION >= 16 |
| 1108 |
2 |
| gv = gv_fetchmeth_pvn( stash, "()", 2, -1, 0 ); |
| 1109 |
|
| #else |
| 1110 |
|
| gv = gv_fetchmeth( stash, "()", 2, -1 ); |
| 1111 |
|
| #endif |
| 1112 |
2 |
▶ | RETVAL = ( gv && isGV( gv ) ) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = ( gv && isGV( gv ) ) ? 1 : 0; |
1 |
1 |
2 |
| RETVAL = ( gv && isGV( gv ) ) ? 1 : 0; |
1 |
0 |
1 |
|
| 1113 |
|
| } |
| 1114 |
|
| } |
| 1115 |
|
| } |
| 1116 |
|
| OUTPUT: |
| 1117 |
|
| RETVAL |
| 1118 |
|
| |
| 1119 |
|
| # --------------------------------------------------------------------------- |
| 1120 |
|
| # _is_scalar( $self, $val ) |
| 1121 |
|
| # |
| 1122 |
|
| # Returns true if reftype($val) eq 'SCALAR' or 'REF'. |
| 1123 |
|
| # A reference to a plain scalar has type SVt_PVMG or lower in older perls, |
| 1124 |
|
| # and SVt_IV/SVt_PV on newer ones. The reliable test is: it's a ref, it's |
| 1125 |
|
| # not AV/HV/CV/GV/FM/IO, and it's not overloaded (which would be PVMG+BLESS). |
| 1126 |
|
| # We mirror Scalar::Util::reftype: returns true for \$scalar and \$ref. |
| 1127 |
|
| # --------------------------------------------------------------------------- |
| 1128 |
|
| |
| 1129 |
|
| int |
| 1130 |
|
| _is_scalar( self, ... ) |
| 1131 |
|
| SV* self |
| 1132 |
|
| CODE: |
| 1133 |
|
| { |
| 1134 |
13 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 1135 |
|
| /* Mirrors Scalar::Util::reftype($val) eq 'SCALAR'. |
| 1136 |
|
| * reftype returns 'SCALAR' when the referent is a plain scalar, |
| 1137 |
|
| * and 'REF' when the referent is itself a reference. |
| 1138 |
|
| * The distinction in XS: SvROK(SvRV(val)) is true for REF, false for SCALAR. */ |
| 1139 |
24 |
▶ | RETVAL = ( SvROK( val ) && !SvROK( SvRV( val ) ) && |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = ( SvROK( val ) && !SvROK( SvRV( val ) ) && |
10 |
1 |
11 |
| RETVAL = ( SvROK( val ) && !SvROK( SvRV( val ) ) && |
2 |
11 |
13 |
|
| 1140 |
10 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVAV && |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVAV && |
9 |
1 |
10 |
|
| 1141 |
9 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVHV && |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVHV && |
8 |
1 |
9 |
|
| 1142 |
8 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVCV && |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVCV && |
7 |
1 |
8 |
|
| 1143 |
7 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVGV && |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVGV && |
7 |
0 |
7 |
|
| 1144 |
7 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVFM && |
Branch: 1 branch
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVFM && |
7 |
0 |
7 |
|
| 1145 |
24 |
▶ | SvTYPE( SvRV( val ) ) != SVt_PVIO ) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| SvTYPE( SvRV( val ) ) != SVt_PVIO ) ? 1 : 0; |
11 |
2 |
13 |
| SvTYPE( SvRV( val ) ) != SVt_PVIO ) ? 1 : 0; |
7 |
0 |
7 |
|
| 1146 |
|
| } |
| 1147 |
|
| OUTPUT: |
| 1148 |
|
| RETVAL |
| 1149 |
|
| |
| 1150 |
|
| # --------------------------------------------------------------------------- |
| 1151 |
|
| # _obj2h( $self ) |
| 1152 |
|
| # |
| 1153 |
|
| # Returns the underlying hash for an object, for direct field access. |
| 1154 |
|
| # Hot path (blessed HASH ref) is a single SvREFCNT_inc; effectively free. |
| 1155 |
|
| # |
| 1156 |
|
| # Mirrors the Perl original: |
| 1157 |
|
| # HASH ref -> return $self as-is |
| 1158 |
|
| # GLOB ref -> return reference to its hash slot |
| 1159 |
|
| # non-ref -> build minimal { debug, verbose, error } hash blessed into $self |
| 1160 |
|
| # other -> return {} |
| 1161 |
|
| # --------------------------------------------------------------------------- |
| 1162 |
|
| |
| 1163 |
|
| SV* |
| 1164 |
|
| _obj2h( self ) |
| 1165 |
|
| SV* self |
| 1166 |
|
| PREINIT: |
| 1167 |
|
| SV* rv; |
| 1168 |
|
| U32 type; |
| 1169 |
|
| CODE: |
| 1170 |
123574 |
▶ | if( !SvROK( self ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !SvROK( self ) ) |
974 |
122600 |
123574 |
|
| 1171 |
|
| { |
| 1172 |
|
| /* Package->method call: mirrors the Perl non-ref branch. |
| 1173 |
|
| * Builds { debug => $Pkg::DEBUG, verbose => $Pkg::VERBOSE, error => $Pkg::ERROR } |
| 1174 |
|
| * blessed into the package. Uses get_sv() with the fully-qualified name |
| 1175 |
|
| * to mirror ${ "${class}::VAR" } semantics. */ |
| 1176 |
974 |
| HV* hash = newHV(); |
| 1177 |
|
| STRLEN pkg_len; |
| 1178 |
974 |
| char* pkg = SvPV( self, pkg_len ); |
| 1179 |
974 |
| HV* stash = gv_stashpvn( pkg, (U32)pkg_len, GV_ADD ); |
| 1180 |
|
| SV* tmp; |
| 1181 |
|
| char varname[512]; |
| 1182 |
|
| |
| 1183 |
|
| /* $Package::DEBUG */ |
| 1184 |
974 |
| snprintf( varname, sizeof(varname), "%.*s::DEBUG", (int)pkg_len, pkg ); |
| 1185 |
974 |
| tmp = get_sv( varname, 0 ); |
| 1186 |
974 |
▶ | hv_stores( hash, "debug", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores( hash, "debug", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
973 |
1 |
974 |
|
| 1187 |
|
| |
| 1188 |
|
| /* $Package::VERBOSE */ |
| 1189 |
974 |
| snprintf( varname, sizeof(varname), "%.*s::VERBOSE", (int)pkg_len, pkg ); |
| 1190 |
974 |
| tmp = get_sv( varname, 0 ); |
| 1191 |
974 |
▶ | hv_stores( hash, "verbose", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores( hash, "verbose", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
23 |
951 |
974 |
|
| 1192 |
|
| |
| 1193 |
|
| /* $Package::ERROR */ |
| 1194 |
974 |
| snprintf( varname, sizeof(varname), "%.*s::ERROR", (int)pkg_len, pkg ); |
| 1195 |
974 |
| tmp = get_sv( varname, 0 ); |
| 1196 |
974 |
▶ | hv_stores( hash, "error", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores( hash, "error", tmp ? SvREFCNT_inc(tmp) : newSViv(0) ); |
23 |
951 |
974 |
|
| 1197 |
|
| |
| 1198 |
974 |
| RETVAL = sv_bless( newRV_noinc( (SV*)hash ), stash ); |
| 1199 |
|
| } |
| 1200 |
|
| else |
| 1201 |
|
| { |
| 1202 |
122600 |
| rv = SvRV( self ); |
| 1203 |
122600 |
| type = SvTYPE( rv ); |
| 1204 |
|
| |
| 1205 |
122600 |
▶ | if( type == SVt_PVHV ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( type == SVt_PVHV ) |
115287 |
7313 |
122600 |
|
| 1206 |
|
| { |
| 1207 |
|
| /* Blessed or plain HASH — the common case, return self directly */ |
| 1208 |
115287 |
| RETVAL = SvREFCNT_inc( self ); |
| 1209 |
|
| } |
| 1210 |
7313 |
▶ | else if( type == SVt_PVGV ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if( type == SVt_PVGV ) |
7313 |
0 |
7313 |
|
| 1211 |
|
| { |
| 1212 |
|
| /* GLOB — return a reference to its hash slot */ |
| 1213 |
7313 |
| HV* hv = GvHV( (GV*)rv ); |
| 1214 |
7313 |
▶ | if( !hv ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !hv ) |
0 |
7313 |
7313 |
|
| 1215 |
|
| { |
| 1216 |
0 |
| hv = newHV(); |
| 1217 |
0 |
| GvHV( (GV*)rv ) = hv; |
| 1218 |
|
| } |
| 1219 |
7313 |
| RETVAL = newRV_inc( (SV*)hv ); |
| 1220 |
|
| } |
| 1221 |
|
| else |
| 1222 |
|
| { |
| 1223 |
|
| /* Anything else — return an empty hashref */ |
| 1224 |
0 |
| RETVAL = newRV_noinc( (SV*)newHV() ); |
| 1225 |
|
| } |
| 1226 |
|
| } |
| 1227 |
|
| OUTPUT: |
| 1228 |
|
| RETVAL |
| 1229 |
|
| |
| 1230 |
|
| # --------------------------------------------------------------------------- |
| 1231 |
|
| # _refaddr( $self, $val ) |
| 1232 |
|
| # |
| 1233 |
|
| # Returns the memory address of the referent of $val as an unsigned integer, |
| 1234 |
|
| # equivalent to Scalar::Util::refaddr($val). |
| 1235 |
|
| # --------------------------------------------------------------------------- |
| 1236 |
|
| |
| 1237 |
|
| SV* |
| 1238 |
|
| _refaddr( self, ... ) |
| 1239 |
|
| SV* self |
| 1240 |
|
| CODE: |
| 1241 |
|
| { |
| 1242 |
6 |
| SV* val = mg_get_sv_arg( 1, items, &ST(0) ); |
| 1243 |
|
| /* Mirrors Scalar::Util::refaddr($val): undef for non-refs, memory address of |
| 1244 |
|
| * the referent for refs. */ |
| 1245 |
6 |
▶ | if( !SvROK( val ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( !SvROK( val ) ) |
2 |
4 |
6 |
|
| 1246 |
|
| { |
| 1247 |
2 |
| RETVAL = &PL_sv_undef; |
| 1248 |
|
| } |
| 1249 |
|
| else |
| 1250 |
|
| { |
| 1251 |
4 |
| RETVAL = newSVuv( PTR2UV( SvRV( val ) ) ); |
| 1252 |
|
| } |
| 1253 |
|
| } |
| 1254 |
|
| OUTPUT: |
| 1255 |
|
| RETVAL |
| 1256 |
|
| |