include/pmcp/pmcp_abi.h

filter: Statementstmt 100% 35 / 35 Branchbran 83.3% 20 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.2% 55 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_rand.h ↑ Summary include/pmcp/pmcp_proto.h »
1 /* pmcp_abi.h - the two C ABIs this dist calls, resolved once at BOOT.
2 *
3 * Needs: pmcp_clos.h, frj_abi.h, jsf_abi.h.
4 * Gives: PMCP_FRJ (File::Raw::JSON) and PMCP_JSF (JSON::Schema::Fast), both
5 * non-NULL once BOOT has returned, so no later header checks for NULL.
6 *
7 * The house _abi_ptr pattern (memory: reference_abi_pattern). The pointer is
8 * read back as a UV, never an IV: a library mapped above the sign bit gives a
9 * negative IV, which is real on illumos and on 32-bit builds. The version gate
10 * is >=, never ==, because the tables are append-only and an equality check
11 * turns every provider release into a breaking change.
12 *
13 * Unlike Maat, which falls back when a table is missing, this dist has no slow
14 * path: every message is decoded by frj and every schema is compiled by jsf. So
15 * BOOT croaks, naming the provider, the ABI it needs and what it found.
16 */
17  
18 #ifndef PMCP_ABI_H
19 #define PMCP_ABI_H
20  
21 static const frj_abi *PMCP_FRJ = NULL;
22 static const jsf_abi *PMCP_JSF = NULL;
23  
24 /* One provider's table address, or 0 with *why set to a mortal reason. */
25 95 static UV pmcp_abi_ptr(pTHX_ const char *module, SV **why)
26 {
27 95 SV *err = NULL;
28 SV *fn, *r;
29 95 UV p = 0;
30  
31 95 *why = NULL;
32 95 ▶ if (!pmcp_require(aTHX_ module, &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_require(aTHX_ module, &err)) { 1 94 95
33 1 *why = sv_2mortal(err);
34 1 return 0;
35 }
36 94 fn = sv_2mortal(newSVpvf("%s::_abi_ptr", module));
37 94 r = pmcp_call_sv(aTHX_ fn, NULL, 0, G_SCALAR, &err);
38 94 ▶ if (!r) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { 1 93 94
39 1 *why = sv_2mortal(err);
40 1 return 0;
41 }
42 93 ▶ if (SvOK(r)) p = SvUV(r);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) p = SvUV(r); 93 0 93
43 93 SvREFCNT_dec(r);
44 93 ▶ if (!p) *why = sv_2mortal(newSVpvs("its _abi_ptr returned nothing"));
Branch: 1 branch
BranchTrueFalseTotal
if (!p) *why = sv_2mortal(newSVpvs("its _abi_ptr returned nothing")); 0 93 93
45 93 return p;
46 }
47  
48 /* The provider's own $VERSION, mortal, or "none" when it did not load. */
49 5 static SV *pmcp_dist_version(pTHX_ const char *module)
50 {
51 5 SV *name = sv_2mortal(newSVpvf("%s::VERSION", module));
52 5 SV *v = get_sv(SvPV_nolen(name), 0);
53 5 ▶ return (v && SvOK(v)) ? v : sv_2mortal(newSVpvs("none"));
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvOK(v)) ? v : sv_2mortal(newSVpvs("none")); 4 1 5
return (v && SvOK(v)) ? v : sv_2mortal(newSVpvs("none")); 0 4 4
54 }
55  
56 /* Croak unless `found` is a table at ABI `need` or later. `version` is the
57 * table's abi_version, read by the caller because the two structs are
58 * different types. `why` explains a 0 address. Kept apart from the resolve so
59 * t/02 can hand it an old table without an old provider installed. */
60 100 static void pmcp_abi_check(pTHX_ const char *module, UV found, int version,
61 int need, const char *since, SV *why)
62 {
63 100 ▶ if (found && version >= need) return;
Branch: 2 branches
BranchTrueFalseTotal
if (found && version >= need) return; 97 3 100
if (found && version >= need) return; 95 2 97
64 5 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 3 2 5
65 3 ▶ croak("%s needs %s %s or later for its C ABI %d, and %s is unusable "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s needs %s %s or later for its C ABI %d, and %s is unusable " 0 3 3
66 "(version %" SVf "): %" SVf,
67 PMCP_DIST, module, since, need, module,
68 SVfARG(pmcp_dist_version(aTHX_ module)),
69 SVfARG(why ? why : sv_2mortal(newSVpvs("no reason given"))));
70 2 croak("%s needs %s %s or later for its C ABI %d, and found %s %" SVf
71 " with C ABI %d",
72 PMCP_DIST, module, since, need, module,
73 SVfARG(pmcp_dist_version(aTHX_ module)), version);
74 }
75  
76 #define PMCP_FRJ_SINCE "0.08"
77 #define PMCP_JSF_SINCE "0.10"
78  
79 /* BOOT: resolve both tables or croak. */
80 48 static void pmcp_abi_boot(pTHX)
81 {
82 SV *why;
83 UV p;
84  
85 48 p = pmcp_abi_ptr(aTHX_ "File::Raw::JSON", &why);
86 95 ▶ pmcp_abi_check(aTHX_ "File::Raw::JSON", p,
Branch: 1 branch
BranchTrueFalseTotal
pmcp_abi_check(aTHX_ "File::Raw::JSON", p, 47 1 48
87 47 p ? (INT2PTR(const frj_abi *, p))->abi_version : 0,
88 FRJ_ABI_VERSION, PMCP_FRJ_SINCE, why);
89 47 PMCP_FRJ = INT2PTR(const frj_abi *, p);
90  
91 47 p = pmcp_abi_ptr(aTHX_ "JSON::Schema::Fast", &why);
92 93 ▶ pmcp_abi_check(aTHX_ "JSON::Schema::Fast", p,
Branch: 1 branch
BranchTrueFalseTotal
pmcp_abi_check(aTHX_ "JSON::Schema::Fast", p, 46 1 47
93 46 p ? (INT2PTR(const jsf_abi *, p))->abi_version : 0,
94 JSF_ABI_VERSION, PMCP_JSF_SINCE, why);
95 46 PMCP_JSF = INT2PTR(const jsf_abi *, p);
96 46 }
97  
98 #endif /* PMCP_ABI_H */
« include/pmcp/pmcp_rand.h ↑ Summary include/pmcp/pmcp_proto.h »