include/hyperman/hm_compat.h

filter: Statementstmt 100% 5 / 5 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.8% 8 / 9 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/hyperman.xs ↑ Summary include/hyperman/hm_abi_impl.h »
1 #ifndef HM_COMPAT_H
2 #define HM_COMPAT_H
3  
4 /* Hyperman - perl API compatibility shims.
5 *
6 * The C core is written against a modern perl API but the distribution
7 * supports older toolchains. This header backfills the handful of macros
8 * and functions the core relies on that only appeared in later perls, so
9 * hm_core.h / hm_future.h / hm_http2.h / hm_tls.h compile unchanged.
10 *
11 * Must be included after EXTERN.h / perl.h / XSUB.h (it needs the perl
12 * headers for XS, MAGIC, SvMAGIC, ERRSV, croak, etc.).
13 *
14 * Coverage (declared floor is perl 5.10):
15 * XS_INTERNAL / XS_EXTERNAL - added 5.15.4 (5.16)
16 * mg_findext - added 5.13.7 (5.14)
17 * croak_sv - added 5.13.1
18 * On 5.10-5.15 one or more of these is absent; everything the core needs
19 * beyond them (sv_magicext, hv_fetchs, ...) exists from 5.10 onward. */
20  
21 /* XSUB entry points. Before the internal/external split every XSUB used
22 * XS(name), which already supplies the pTHX_/CV* cv signature; fall back
23 * to it so the "cv" the callbacks reference stays in scope. */
24 #ifndef XS_INTERNAL
25 # define XS_INTERNAL(name) XS(name)
26 #endif
27 #ifndef XS_EXTERNAL
28 # define XS_EXTERNAL(name) XS(name)
29 #endif
30  
31 /* mg_findext: find the ext-magic slot on an SV matching a given vtbl.
32 * Trivially reconstructed from the magic chain on perls that lack it. */
33 #ifndef mg_findext
34 static MAGIC *
35 hm_mg_findext(const SV *sv, int type, const MGVTBL *vtbl)
36 {
37 if (sv) {
38 const MAGIC *mg;
39 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
40 if (mg->mg_type == type && mg->mg_virtual == vtbl)
41 return (MAGIC *)mg;
42 }
43 }
44 return NULL;
45 }
46 # define mg_findext(sv, type, vtbl) hm_mg_findext((sv), (type), (vtbl))
47 #endif
48  
49 /* croak_sv: throw an SV as the exception, preserving objects/refs (a
50 * failed Future can carry a blessed error).
51 *
52 * A reference goes through $@ and croak(NULL), which raises the current
53 * ERRSV - the only spelling that keeps a blessed exception blessed.
54 *
55 * A plain string must NOT take that route. croak_sv appends
56 * " at FILE line N.\n" to a message that does not already end in a
57 * newline; on every perl old enough to need this shim croak(NULL)
58 * raises ERRSV verbatim instead, so a string failure came out stripped
59 * of its location (0.41 FAILed t/38 on 5.10.1 and 5.12.5 with a bare
60 * 'Oopsie'). Modern perls route croak(NULL) through mess_sv and DO
61 * append, which is why no current perl can reproduce it. Hand the
62 * string to croak as an argument and croak does the appending itself,
63 * on every perl.
64 *
65 * hm_croak_sv is compiled EVERYWHERE, not only where croak_sv is
66 * missing, so that Hyperman->_croak_sv_selftest can drive it on a
67 * modern perl and t/38 can hold it to the same assertions it holds the
68 * native croak_sv to. A shim that only exists on the perls you cannot
69 * run is a shim nobody ever tests - which is how the bug above shipped. */
70  
71 /* 5.10.0 only: croak() leaves ERRSV's UTF8 flag alone, so carry it over. */
72 #if PERL_REVISION == 5 && PERL_VERSION == 10 && PERL_SUBVERSION == 0
73 # define HM_ERRSV_UTF8_FROM(sv) STMT_START { \
74 SV *e_ = ERRSV; \
75 SvFLAGS(e_) = (SvFLAGS(e_) & ~SVf_UTF8) \
76 | (SvFLAGS(sv) & SVf_UTF8); \
77 } STMT_END
78 #else
79 # define HM_ERRSV_UTF8_FROM(sv) STMT_START { } STMT_END
80 #endif
81 static void
82 4 hm_croak_sv(pTHX_ SV *sv)
83 {
84 4 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 1 3 4
85 1 ▶ sv_setsv(ERRSV, sv);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, sv); 1 0 1
86 1 croak(NULL);
87 }
88 HM_ERRSV_UTF8_FROM(sv);
89 3 croak("%" SVf, SVfARG(sv));
90 }
91 #ifndef croak_sv
92 # define croak_sv(sv) hm_croak_sv(aTHX_ (sv))
93 #endif
94  
95 #endif /* HM_COMPAT_H */
« xs/hyperman.xs ↑ Summary include/hyperman/hm_abi_impl.h »