include/jsf_types.h

filter: Statementstmt 86.9% 20 / 23 Branchbran 77.7% 28 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.3% 48 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/jsf_uri.h ↑ Summary Fast.xs »
1 #ifndef JSF_TYPES_H
2 #define JSF_TYPES_H
3  
4 /* The seven JSON types as a bitmask. `integer` is a subset of `number`
5 * (draft 2020-12), so an integral value sets both bits and a `type` check is a
6 * single `&` against the classified bit (principle 5). Needs perl.h (SV etc.)
7 * which the .xs includes before jsf.h. */
8  
9 enum {
10 JSF_T_NULL = 1u << 0,
11 JSF_T_OBJECT = 1u << 1,
12 JSF_T_ARRAY = 1u << 2,
13 JSF_T_STRING = 1u << 3,
14 JSF_T_NUMBER = 1u << 4,
15 JSF_T_INTEGER = 1u << 5,
16 JSF_T_BOOLEAN = 1u << 6
17 };
18  
19 /* Is this blessed ref one of the known JSON boolean wrappers? */
20 460 static int jsf__is_bool_class(pTHX_ SV *sv) {
21 static const char *const cls[] = {
22 "JSON::PP::Boolean",
23 "Cpanel::JSON::XS::Boolean",
24 "JSON::XS::Boolean",
25 "Types::Serialiser::Boolean",
26 "File::Raw::JSON::Boolean",
27 NULL
28 };
29 int i;
30 2300 ▶ for (i = 0; cls[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; cls[i]; i++) 2300 0 2300
31 2300 ▶ if (sv_isa(sv, cls[i])) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isa(sv, cls[i])) return 1; 460 1840 2300
32 0 return 0;
33 }
34  
35 /* The numeric value of an already-classified JSON number, without disturbing
36 * the SV.
37 *
38 * SvNV on an integer caches the NV and turns NOK on. That is invisible to the
39 * validator and changes nothing about the verdict - but it changes the value
40 * afterwards: an encoder that tests NOK before IOK then writes 1 as 1.0, so a
41 * caller who validates a document and then serialises it puts different bytes
42 * on the wire than the ones it was handed. Validation is a question asked of a
43 * value, not something done to it, so the flags a decoder set are read rather
44 * than forced.
45 *
46 * The order matches sv_2nv's own preference (NV before IV) so a value carrying
47 * both still answers exactly what SvNV would have answered. Anything else -
48 * a string coerced into a number, an SV with magic - falls through to SvNV,
49 * which by then has nothing left to spoil. */
50 467 static NV jsf__nv_of(pTHX_ SV *sv) {
51 467 ▶ if (SvNOK(sv)) return SvNVX(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) return SvNVX(sv); 55 412 467
52 412 ▶ if (SvIOK(sv)) return SvIOK_UV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv)) return SvIOK_UV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); 412 0 412
if (SvIOK(sv)) return SvIOK_UV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); 0 412 412
53 0 return SvNV(sv);
54 }
55  
56 /* Classify a live SV into one JSON type bit (integer also sets number). Called
57 * once per value; the result is reused by the interpreter. JSON Schema types
58 * follow the JSON value's type, not Perl's DWIM - a numeric-looking string is
59 * a string here; `coerce` (phase 04) is the explicit opt-in. */
60 4560 static unsigned jsf_classify_type(pTHX_ SV *sv) {
61 4560 ▶ if (!sv || !SvOK(sv)) return JSF_T_NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return JSF_T_NULL; 4560 0 4560
if (!sv || !SvOK(sv)) return JSF_T_NULL; 90 4470 4560
62  
63 4470 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 2233 2237 4470
64 2233 SV *rv = SvRV(sv);
65 2233 ▶ if (SvOBJECT(rv) && jsf__is_bool_class(aTHX_ sv)) return JSF_T_BOOLEAN;
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(rv) && jsf__is_bool_class(aTHX_ sv)) return JSF_T_BOOLEAN; 238 1995 2233
if (SvOBJECT(rv) && jsf__is_bool_class(aTHX_ sv)) return JSF_T_BOOLEAN; 238 0 238
66 1995 ▶ if (SvTYPE(rv) == SVt_PVHV) return JSF_T_OBJECT;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) return JSF_T_OBJECT; 1428 567 1995
67 567 ▶ if (SvTYPE(rv) == SVt_PVAV) return JSF_T_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) return JSF_T_ARRAY; 567 0 567
68 0 return 0; /* other refs are not JSON values */
69 }
70  
71 /* Non-ref scalar: trust the numeric flags a JSON decoder set. */
72 2237 ▶ if (SvIOK(sv) && !SvPOK(sv)) return JSF_T_NUMBER | JSF_T_INTEGER;
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvPOK(sv)) return JSF_T_NUMBER | JSF_T_INTEGER; 1050 1187 2237
if (SvIOK(sv) && !SvPOK(sv)) return JSF_T_NUMBER | JSF_T_INTEGER; 1050 0 1050
73 1187 ▶ if (SvNOK(sv) && !SvPOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvNOK(sv) && !SvPOK(sv)) { 134 1053 1187
if (SvNOK(sv) && !SvPOK(sv)) { 127 7 134
74 /* An integer is a number with zero fractional part, at any magnitude.
75 * Testing that with (NV)(IV)nv is undefined behaviour once nv is
76 * outside the IV range - which on a 32-bit-IV perl starts at 2**31,
77 * where every JSON integer a decoder had to widen to an NV lives. */
78 127 NV nv = SvNVX(sv);
79 127 ▶ if (nv - nv != 0) return (unsigned)JSF_T_NUMBER; /* NaN or infinite */
Branch: 1 branch
BranchTrueFalseTotal
if (nv - nv != 0) return (unsigned)JSF_T_NUMBER; /* NaN or infinite */ 0 127 127
80 /* Perl_floor, not floor: on a long-double or quadmath perl the plain
81 * one would round the NV through a double first. */
82 127 return (nv == Perl_floor(nv)) ? (unsigned)(JSF_T_NUMBER | JSF_T_INTEGER)
83 127 ▶ : (unsigned)JSF_T_NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
: (unsigned)JSF_T_NUMBER; 52 75 127
84 }
85 1060 return JSF_T_STRING;
86 }
87  
88 #endif /* JSF_TYPES_H */
« include/jsf_uri.h ↑ Summary Fast.xs »