include/jsf_types.h

filter: Statementstmt 86.3% 19 / 22 Branchbran 79.4% 27 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.1% 46 / 56 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/jsf_parse.h ↑ Summary include/jsf_compiled.h »
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 461 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 2305 ▶ for (i = 0; cls[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; cls[i]; i++) 2305 0 2305
31 2305 ▶ if (sv_isa(sv, cls[i])) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isa(sv, cls[i])) return 1; 461 1844 2305
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 465 static NV jsf__nv_of(pTHX_ SV *sv) {
51 465 ▶ if (SvNOK(sv)) return SvNVX(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) return SvNVX(sv); 54 411 465
52 411 ▶ 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); 411 0 411
if (SvIOK(sv)) return SvIOK_UV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); 0 411 411
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 4564 static unsigned jsf_classify_type(pTHX_ SV *sv) {
61 4564 ▶ if (!sv || !SvOK(sv)) return JSF_T_NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return JSF_T_NULL; 4564 0 4564
if (!sv || !SvOK(sv)) return JSF_T_NULL; 90 4474 4564
62  
63 4474 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 2234 2240 4474
64 2234 SV *rv = SvRV(sv);
65 2234 ▶ 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; 239 1995 2234
if (SvOBJECT(rv) && jsf__is_bool_class(aTHX_ sv)) return JSF_T_BOOLEAN; 239 0 239
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 2240 ▶ 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 1190 2240
if (SvIOK(sv) && !SvPOK(sv)) return JSF_T_NUMBER | JSF_T_INTEGER; 1050 0 1050
73 1190 ▶ if (SvNOK(sv) && !SvPOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvNOK(sv) && !SvPOK(sv)) { 134 1056 1190
if (SvNOK(sv) && !SvPOK(sv)) { 127 7 134
74 127 NV nv = SvNVX(sv);
75 127 NV iv = (NV)(IV)nv;
76 return (nv == iv) ? (unsigned)(JSF_T_NUMBER | JSF_T_INTEGER)
77 127 ▶ : (unsigned)JSF_T_NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
: (unsigned)JSF_T_NUMBER; 51 76 127
78 }
79 1063 return JSF_T_STRING;
80 }
81  
82 #endif /* JSF_TYPES_H */
« include/jsf_parse.h ↑ Summary include/jsf_compiled.h »