include/punk/punk_compat.h

filter: Statementstmt 100% 15 / 15 Branchbran 88.4% 22 / 26 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.6% 37 / 41 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/csrf.xs ↑ Summary include/punk/punk_entropy.h »
1 #ifndef PUNK_COMPAT_H
2 #define PUNK_COMPAT_H
3  
4 /* Perl-version portability shims for the C core, so Punk builds on every perl
5 * it claims (5.10.0+). Include after EXTERN.h / perl.h / XSUB.h and before any
6 * other punk/ header, so the definitions are in scope everywhere below.
7 *
8 * Each shim is the standard definition of the thing it stands in for; on a
9 * perl new enough to have the real one, none of this is compiled. */
10  
11 /* XS_INTERNAL / XS_EXTERNAL (and XSPROTO) arrived in perl's XSUB.h at 5.16.
12 * The C-closure callbacks (punk_static.h, punk_oamount.h, punk_model.h,
13 * punk_compile.h, punk_sse.h, punk_future.h) are declared XS_INTERNAL, so
14 * without these they fail with "XS_INTERNAL undeclared" / "cv undeclared". */
15 #ifndef XSPROTO
16 # define XSPROTO(name) void name(pTHX_ CV *cv)
17 #endif
18 #ifndef XS_INTERNAL
19 # define XS_INTERNAL(name) STATIC XSPROTO(name)
20 #endif
21 #ifndef XS_EXTERNAL
22 # define XS_EXTERNAL(name) XSPROTO(name)
23 #endif
24  
25 /* mg_findext (5.14) and croak_sv (5.13.1). punk_static.h uses mg_findext to
26 * reach the magic carrying a closure's captures; the view tier, the websocket
27 * handshake and Punk::Future croak_sv an error SV they were handed. */
28 #if PERL_REVISION == 5 && PERL_VERSION < 14
29  
30 static MAGIC *Punk_mg_findext(SV *sv, int type, const MGVTBL *vtbl) {
31 if (sv) {
32 MAGIC *mg;
33 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
34 if (mg->mg_type == type && (const MGVTBL *)mg->mg_virtual == vtbl)
35 return mg;
36 }
37 return NULL;
38 }
39 # define mg_findext(sv, type, vtbl) Punk_mg_findext((sv), (type), (vtbl))
40  
41 /* croak_sv takes two routes and needs both. A reference goes through $@ and
42 * croak(NULL), which raises ERRSV as it stands - the only spelling that keeps
43 * a blessed error blessed, and both punk_wshandshake.h (rethrowing a handler's
44 * die) and Punk::Future (failed with whatever the caller passed) can hold one.
45 * Anything else is handed to croak as an argument, so croak appends
46 * " at FILE line N.\n" when the message has no trailing newline, exactly as
47 * the core croak_sv does. Raising a string through croak(NULL) instead would
48 * lose that location, because on the perls that select this shim croak(NULL)
49 * takes ERRSV verbatim rather than passing it through mess_sv. */
50 static void Punk_croak_sv(pTHX_ SV *sv) {
51 if (SvROK(sv)) {
52 sv_setsv(ERRSV, sv);
53 Perl_croak(aTHX_ NULL);
54 }
55 Perl_croak(aTHX_ "%" SVf, SVfARG(sv));
56 }
57 # define croak_sv(sv) Punk_croak_sv(aTHX_ (sv))
58  
59 #endif
60  
61 /* isWORDCHAR is the 5.14 spelling of what older perls call isALNUM. Both mean
62 * [0-9A-Za-z_] here: every caller (route methods, extension sniffs, the mount
63 * path scanner) is testing a single ASCII byte. */
64 #ifndef isWORDCHAR
65 # define isWORDCHAR(c) isALNUM(c)
66 #endif
67  
68 /* foldEQ is 5.13.2; before that the same function is spelled ibcmp, with the
69 * opposite sense (ibcmp returns 0 when the strings match case-insensitively).
70 * xs/response.xs uses it to compare a header name. */
71 #ifndef foldEQ
72 # define foldEQ(a, b, len) (!ibcmp((a), (b), (len)))
73 #endif
74  
75 /* Perl_isinfnan is 5.22, a function rather than a macro, so only the version
76 * can say whether it is there. x - x is NaN exactly when x is an infinity or
77 * a NaN. punk_plural.h, the i18n count, and the cache and idempotency lock
78 * waits use it to refuse a number that is not finite. */
79 #if PERL_REVISION == 5 && PERL_VERSION < 22
80 static int Punk_isinfnan(NV x) {
81 NV d = x - x;
82 return d != d;
83 }
84 # define Perl_isinfnan(x) Punk_isinfnan(x)
85 #endif
86  
87 /* mPUSHs / mXPUSHs are 5.10.1, one point release above the floor this dist
88 * declares. Perl's own definitions. */
89 #ifndef mPUSHs
90 # define mPUSHs(s) PUSHs(sv_2mortal(s))
91 #endif
92 #ifndef mXPUSHs
93 # define mXPUSHs(s) XPUSHs(sv_2mortal(s))
94 #endif
95  
96 /* A true value for a set-membership slot, that the container may own.
97 *
98 * &PL_sv_yes must never be stored bare: hv_store and friends take over a
99 * reference, and the matching decrement when the hash is freed lands on an
100 * immortal that nobody incremented. From perl 5.20 the immortals shrug that
101 * off; before it the refcount really does walk down to zero and the next
102 * thing to touch yes/no/undef segfaults. Taking the reference first costs
103 * nothing - it is still the same singleton, with no allocation - and keeps
104 * the books straight on every perl. See t/0012-immortal-refcount.t. */
105 #define PUNK_SET_TRUE SvREFCNT_inc_simple_NN(&PL_sv_yes)
106  
107 /* A temporaries frame for a callback the event loop dispatches straight
108 * into C.
109 *
110 * Hyperman calls a C watcher, a C timer and a stream callback with no
111 * ENTER/SAVETMPS of its own, and its loop has no frame per tick. So a mortal
112 * made in one of those callbacks before anything opens a frame lands on the
113 * loop's own, which is released when the worker exits: every received
114 * websocket message stayed allocated for the life of the process. Every
115 * callback handed to the loop opens its own frame with these, whatever the
116 * server does about it. */
117 #define PK_LOOP_ENTER ENTER; SAVETMPS
118 #define PK_LOOP_LEAVE FREETMPS; LEAVE
119  
120 /* The source of `require CLASS`, for the places that load a class named in
121 * a keyword or in punk.yml - a plugin, a view engine, a controller, a model.
122 *
123 * Mortal, and only for a name that IS one: words joined by `::`. The
124 * statement is built by putting the name into Perl source, and a name with
125 * anything else in it was run as whatever it said. */
126 562 static SV *pk_require_src(pTHX_ const char *class) {
127 562 STRLEN len = strlen(class), i = 0;
128 562 int ok = len > 0;
129 1007 ▶ while (ok && i < len) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (ok && i < len) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1006 1 1007
while (ok && i < len) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1006 0 1006
130 1006 STRLEN start = i;
131 1006 ▶ if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 1004 1006
if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
132 7053 ▶ while (i < len && isWORDCHAR((U8)class[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && isWORDCHAR((U8)class[i])) i++; 6502 551 7053
while (i < len && isWORDCHAR((U8)class[i])) i++; 6049 453 6502
133 /*
134 # uncoverable branch true
135 # uncoverable condition left count:1 class:ignore_covered_err */
136 1004 ▶ if (i == start) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (i == start) { ok = 0; break; } 0 1004 1004
137 1004 ▶ if (i == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) break; 551 453 1004
138 453 ▶ if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' /* # uncoverable condition left count:2 class:ignore_covered_err */ 447 6 453
if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' /* # uncoverable condition left count:2 class:ignore_covered_err */ 447 0 447
if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' /* # uncoverable condition left count:2 class:ignore_covered_err */ 446 1 447
139 453 ▶ && i + 2 < len)) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
&& i + 2 < len)) { ok = 0; break; } 1 445 446
140 445 i += 2;
141 }
142 562 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 11 551 562
143 11 croak("Punk: '%s' is not a class name, so it will not be loaded",
144 class);
145 551 return sv_2mortal(newSVpvf("require %s; 1;", class));
146 }
147  
148 #endif /* PUNK_COMPAT_H */
« xs/csrf.xs ↑ Summary include/punk/punk_entropy.h »