include/pq_frj.h

filter: Statementstmt 93.5% 29 / 31 Branchbran 55.2% 21 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.4% 50 / 69 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_dbi.h ↑ Summary include/pq_queue.h »
1 #ifndef PQ_FRJ_H
2 #define PQ_FRJ_H
3  
4 /* The File::Raw::JSON C ABI - the codec for a job's args, result and notes.
5 *
6 * Resolved lazily on first use from File::Raw::JSON::_abi_ptr, the consumer
7 * pattern every dist in this ecosystem uses. File::Raw::JSON is a hard
8 * PREREQ and the gate is `==` (frj's table is not documented as append-only
9 * the way hm_abi's is), so a mismatch is a startup-environment error and
10 * croaks rather than degrading.
11 *
12 * Include after pq_compat.h and frj_abi.h. */
13  
14 static const frj_abi *PQ_FRJ = NULL;
15 static int PQ_FRJ_TRIED = 0;
16  
17 2308 static const frj_abi *pq_frj(pTHX) {
18 2308 ▶ if (!PQ_FRJ && !PQ_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!PQ_FRJ && !PQ_FRJ_TRIED) { 67 2241 2308
if (!PQ_FRJ && !PQ_FRJ_TRIED) { 67 0 67
19 67 dSP; int count; IV p = 0;
20 67 PQ_FRJ_TRIED = 1;
21 67 eval_pv("require File::Raw::JSON;", FALSE);
22 /* The require runs arbitrary Perl. If that grew the value stack it
23 * was reallocated, and the SP captured by dSP above now points into
24 * the freed block - which the PUTBACK below would publish as
25 * PL_stack_sp. */
26 67 SPAGAIN;
27 67 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 67 0 67
if (!SvTRUE(ERRSV)) { 67 0 67
28 67 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 67 67
29 67 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
30 67 SPAGAIN;
31 67 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 67 0 67
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 67 0 67
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 67 0 67
32 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
33 67 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 67 0 67
34 67 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 67 0 67
35 67 const frj_abi *a = INT2PTR(const frj_abi *, p);
36 67 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) PQ_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) PQ_FRJ = a; 67 0 67
if (a && a->abi_version >= FRJ_ABI_VERSION) PQ_FRJ = a; 67 0 67
37 }
38 }
39 }
40 2308 ▶ if (!PQ_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!PQ_FRJ) 0 2308 2308
41 0 croak("Punk::Queue needs File::Raw::JSON with a compatible C ABI "
42 "(FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
43 2308 return PQ_FRJ;
44 }
45  
46 /* Encode a Perl value to JSON. Returns +1, caller owns. `fallback` is
47 * emitted when the value is undef, which is how an absent args list becomes
48 * "[]" and absent notes become "{}" without a branch at every call site.
49 *
50 * frj returns UTF-8 *bytes* with the UTF8 flag off. That is exactly wrong
51 * for a DBI bind: DBD::Pg upgrades an unflagged string as if it were
52 * latin-1 on the way to a UTF-8 database, double-encoding every multibyte
53 * character, while DBD::SQLite stores the bytes verbatim - so the same
54 * payload would round-trip differently per backend, which is the one thing
55 * the conformance suite exists to forbid. sv_utf8_decode turns the bytes
56 * into the character string they spell (validating as it goes; frj output
57 * is guaranteed well-formed), after which every driver agrees on what is
58 * being stored. */
59 2123 static SV *pq_json_encode(pTHX_ SV *value, const char *fallback) {
60 const frj_abi *A;
61 SV *out;
62 2123 ▶ if (!value || !SvOK(value)) return newSVpv(fallback, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (!value || !SvOK(value)) return newSVpv(fallback, 0); 830 1293 2123
if (!value || !SvOK(value)) return newSVpv(fallback, 0); 0 830 830
63 830 A = pq_frj(aTHX);
64 830 out = A->encode(aTHX_ value, NULL);
65 830 (void)sv_utf8_decode(out);
66 830 return out;
67 }
68  
69 /* Decode JSON bytes to a Perl value. Returns +1, caller owns. An undef or
70 * empty column decodes as undef rather than croaking: a NULL result column
71 * is normal for a job that has not finished. Malformed stored JSON is a
72 * real error and is allowed to croak. */
73 1875 static SV *pq_json_decode(pTHX_ SV *bytes) {
74 const frj_abi *A;
75 const char *p;
76 STRLEN len;
77 1875 ▶ if (!bytes || !SvOK(bytes)) return newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!bytes || !SvOK(bytes)) return newSV(0); 1875 0 1875
if (!bytes || !SvOK(bytes)) return newSV(0); 397 1478 1875
78 1478 p = SvPV_const(bytes, len);
79 1478 ▶ if (!len) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return newSV(0); 0 1478 1478
80 1478 A = pq_frj(aTHX);
81 1478 return A->decode(aTHX_ p, len, NULL);
82 }
83  
84 #endif /* PQ_FRJ_H */
« include/pq_dbi.h ↑ Summary include/pq_queue.h »