include/fetch/ft_json.h

filter: Statementstmt 92.5% 25 / 27 Branchbran 50.0% 14 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.9% 39 / 55 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/abi.xs ↑ Summary include/fetch/backend_epoll.c »
1 #ifndef FT_JSON_H
2 #define FT_JSON_H
3  
4 /* JSON for the `json =>` request option (encode) and Fetch::Response->json
5 * (decode), through File::Raw::JSON's C ABI (frj_abi.h) - decoded/encoded
6 * entirely in C, with no per-call Perl dispatch and no Cpanel::JSON::XS /
7 * JSON::PP dependency. Encoding is canonical UTF-8 (allow_nonref); true/false
8 * decode to File::Raw::JSON::Boolean, null to undef. The versioned ABI table is
9 * resolved lazily on first use. Needs the Perl API (perl.h, via the .xs). */
10  
11 #include "frj_abi.h" /* reached through ExtUtils::Depends */
12  
13 static const frj_abi *FT_FRJ = NULL; /* resolved on first JSON op */
14 static int FT_FRJ_TRIED = 0;
15  
16 /* Resolve File::Raw::JSON's ABI table once. It is a hard dependency (loaded by
17 * Fetch.pm), so a failure to resolve is fatal rather than a silent fallback. */
18 2 static const frj_abi *ft_frj(pTHX) {
19 2 ▶ if (!FT_FRJ && !FT_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!FT_FRJ && !FT_FRJ_TRIED) { 1 1 2
if (!FT_FRJ && !FT_FRJ_TRIED) { 1 0 1
20 1 dSP; int count; IV p = 0;
21 1 FT_FRJ_TRIED = 1;
22 1 eval_pv("require File::Raw::JSON;", FALSE);
23 1 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 1 0 1
if (!SvTRUE(ERRSV)) { 1 0 1
24 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 1 1
25 1 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
26 1 SPAGAIN;
27 1 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
28 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
29 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
30 1 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 1 0 1
31 1 const frj_abi *a = INT2PTR(const frj_abi *, p);
32 1 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) FT_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) FT_FRJ = a; 1 0 1
if (a && a->abi_version >= FRJ_ABI_VERSION) FT_FRJ = a; 1 0 1
33 }
34 }
35 }
36 2 ▶ if (!FT_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!FT_FRJ) 0 2 2
37 0 croak("Fetch: JSON needs File::Raw::JSON with a compatible C ABI "
38 "(FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
39 2 return FT_FRJ;
40 }
41  
42 /* $data -> canonical UTF-8 JSON bytes (SV +1, owned). Croaks on a bad shape. */
43 1 static SV *ft_json_encode(pTHX_ SV *data) {
44 1 const frj_abi *J = ft_frj(aTHX);
45 frj_opts o;
46 1 J->opts_init(&o);
47 1 o.canonical = 1; /* was ->utf8->allow_nonref->canonical */
48 1 return J->encode(aTHX_ data, &o);
49 }
50  
51 /* JSON bytes -> Perl data (SV +1, owned). Croaks on malformed JSON. */
52 1 static SV *ft_json_decode(pTHX_ SV *bytes) {
53 1 const frj_abi *J = ft_frj(aTHX);
54 STRLEN len;
55 1 const char *pv = SvPV(bytes, len);
56 1 return J->decode(aTHX_ pv, len, NULL); /* NULL opts => defaults (utf8 on) */
57 }
58  
59 #endif /* FT_JSON_H */
« xs/abi.xs ↑ Summary include/fetch/backend_epoll.c »