| 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 |
439 |
| static SV *pk_require_src(pTHX_ const char *class) { |
| 127 |
439 |
| STRLEN len = strlen(class), i = 0; |
| 128 |
439 |
| int ok = len > 0; |
| 129 |
801 |
▶ | while (ok && i < len) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (ok && i < len) { |
800 |
1 |
801 |
| while (ok && i < len) { |
800 |
0 |
800 |
|
| 130 |
800 |
| STRLEN start = i; |
| 131 |
800 |
▶ | if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } |
2 |
798 |
800 |
| if (!(isALPHA((U8)class[i]) || class[i] == '_')) { ok = 0; break; } |
2 |
0 |
2 |
|
| 132 |
5534 |
▶ | while (i < len && isWORDCHAR((U8)class[i])) i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < len && isWORDCHAR((U8)class[i])) i++; |
5106 |
428 |
5534 |
| while (i < len && isWORDCHAR((U8)class[i])) i++; |
4736 |
370 |
5106 |
|
| 133 |
798 |
▶ | if (i == start) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == start) { ok = 0; break; } |
0 |
798 |
798 |
|
| 134 |
798 |
▶ | if (i == len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == len) break; |
428 |
370 |
798 |
|
| 135 |
370 |
▶ | if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' |
364 |
6 |
370 |
| if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' |
364 |
0 |
364 |
| if (!(class[i] == ':' && i + 1 < len && class[i + 1] == ':' |
363 |
1 |
364 |
|
| 136 |
370 |
▶ | && i + 2 < len)) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| && i + 2 < len)) { ok = 0; break; } |
1 |
362 |
363 |
|
| 137 |
362 |
| i += 2; |
| 138 |
|
| } |
| 139 |
439 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
11 |
428 |
439 |
|
| 140 |
11 |
| croak("Punk: '%s' is not a class name, so it will not be loaded", |
| 141 |
|
| class); |
| 142 |
428 |
| return sv_2mortal(newSVpvf("require %s; 1;", class)); |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| #endif /* PUNK_COMPAT_H */ |