include/jsf_interp.h

filter: Statementstmt 99.4% 384 / 386 Branchbran 80.7% 572 / 708 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.3% 956 / 1094 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/jsf_compiled.h ↑ Summary include/jsf_arena.h »
1 #ifndef JSF_INTERP_H
2 #define JSF_INTERP_H
3  
4 /* The validation engine. is_valid() runs with ctx->errors == NULL: fail-fast,
5 * no instance-location bookkeeping, zero allocation on the valid path. validate()
6 * / errors() run with a sink AV and a reused JSON-Pointer buffer (ctx->iloc),
7 * collecting every error with instanceLocation / keyword / schemaLocation.
8 * Combinators (anyOf/oneOf/not/if) probe their branches with a throwaway ctx so
9 * a branch that is *allowed* to fail does not leak errors into the sink.
10 *
11 * Needs jsf_arena.h, jsf_ir.h, jsf_types.h, jsf_compiled.h, jsf_err.h + Perl. */
12  
13 #include <math.h>
14  
15 #define JSF_MAX_DEPTH 1024
16  
17 static int jsf_validate(pTHX_ jsf_compiled_t *C, uint32_t off, SV *data, jsf_ctx_t *ctx);
18  
19 /* ---- annotation collector for unevaluatedProperties/Items --------------- *
20 * Per data-instance: the set of property names / item indices that some
21 * keyword or successful in-place applicator has already evaluated. Only used
22 * when the schema declares unevaluated* (C->has_unevaluated); NULL otherwise. */
23 typedef struct jsf_annot { HV *props; HV *items; int all_items; } jsf_annot;
24  
25 1379 static jsf_annot *jsf_annot_new(pTHX) {
26 1379 jsf_annot *a = (jsf_annot *)malloc(sizeof *a);
27 1379 ▶ if (!a) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return NULL; 0 1379 1379
28 1379 a->props = newHV(); a->items = newHV(); a->all_items = 0;
29 1379 return a;
30 }
31 1379 static void jsf_annot_free(pTHX_ jsf_annot *a) {
32 1379 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 1379 1379
33 1379 SvREFCNT_dec((SV *)a->props); SvREFCNT_dec((SV *)a->items); free(a);
34 }
35 425 static void jsf_annot_merge(pTHX_ jsf_annot *dst, jsf_annot *src) {
36 HE *he;
37 425 ▶ if (!dst || !src) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!dst || !src) return; 425 0 425
if (!dst || !src) return; 0 425 425
38 622 ▶ hv_iterinit(src->props); while ((he = hv_iternext(src->props))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->props, k, kl, &PL_sv_yes, 0); }
Branch: 2 branches
BranchTrueFalseTotal
hv_iterinit(src->props); while ((he = hv_iternext(src->props))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->props, k, kl, &PL_sv_yes, 0); } 0 197 197
hv_iterinit(src->props); while ((he = hv_iternext(src->props))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->props, k, kl, &PL_sv_yes, 0); } 197 425 622
39 528 ▶ hv_iterinit(src->items); while ((he = hv_iternext(src->items))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->items, k, kl, &PL_sv_yes, 0); }
Branch: 2 branches
BranchTrueFalseTotal
hv_iterinit(src->items); while ((he = hv_iternext(src->items))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->items, k, kl, &PL_sv_yes, 0); } 0 103 103
hv_iterinit(src->items); while ((he = hv_iternext(src->items))) { STRLEN kl; char *k = HePV(he, kl); (void)hv_store(dst->items, k, kl, &PL_sv_yes, 0); } 103 425 528
40 425 dst->all_items |= src->all_items;
41 }
42 758 static void jsf_annot_add_key(pTHX_ jsf_annot *a, const char *k, STRLEN kl) {
43 758 ▶ if (a) (void)hv_store(a->props, k, kl, &PL_sv_yes, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (a) (void)hv_store(a->props, k, kl, &PL_sv_yes, 0); 253 505 758
44 758 }
45 352 static void jsf_annot_add_idx(pTHX_ jsf_annot *a, IV i) {
46 352 ▶ if (a) { char b[24]; int n = (int)my_snprintf(b, sizeof b, "%" IVdf, i); (void)hv_store(a->items, b, n, &PL_sv_yes, 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (a) { char b[24]; int n = (int)my_snprintf(b, sizeof b, "%" IVdf, i); (void)hv_store(a->items, b, n, &PL_sv_yes, 0); } 176 176 352
47 352 }
48 137 static int jsf_annot_has_idx(pTHX_ jsf_annot *a, IV i) {
49 char b[24]; int n;
50 137 ▶ if (!a) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return 0; 0 137 137
51 137 ▶ if (a->all_items) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a->all_items) return 1; 13 124 137
52 124 n = (int)my_snprintf(b, sizeof b, "%" IVdf, i);
53 124 return hv_exists(a->items, b, n);
54 }
55  
56 /* ---- JSON value equality (const/enum, uniqueItems) ---------------------- */
57 518 static int jsf_json_equal(pTHX_ SV *a, SV *b) {
58 518 unsigned ta = jsf_classify_type(aTHX_ a);
59 518 unsigned tb = jsf_classify_type(aTHX_ b);
60 518 ▶ if ((ta & JSF_T_NUMBER) && (tb & JSF_T_NUMBER))
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_NUMBER) && (tb & JSF_T_NUMBER)) 176 342 518
if ((ta & JSF_T_NUMBER) && (tb & JSF_T_NUMBER)) 134 42 176
61 134 return jsf__nv_of(aTHX_ a) == jsf__nv_of(aTHX_ b);
62 384 ▶ if ((ta & JSF_T_STRING) && (tb & JSF_T_STRING)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_STRING) && (tb & JSF_T_STRING)) { 187 197 384
if ((ta & JSF_T_STRING) && (tb & JSF_T_STRING)) { 179 8 187
63 179 ▶ STRLEN la, lb; const char *sa = SvPV_const(a, la); const char *sb = SvPV_const(b, lb);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN la, lb; const char *sa = SvPV_const(a, la); const char *sb = SvPV_const(b, lb); 179 0 179
64 179 ▶ return la == lb && memcmp(sa, sb, la) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return la == lb && memcmp(sa, sb, la) == 0; 167 12 179
return la == lb && memcmp(sa, sb, la) == 0; 99 68 167
65 }
66 205 ▶ if ((ta & JSF_T_BOOLEAN) && (tb & JSF_T_BOOLEAN)) return (SvTRUE(a) ? 1 : 0) == (SvTRUE(b) ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_BOOLEAN) && (tb & JSF_T_BOOLEAN)) return (SvTRUE(a) ? 1 : 0) == (SvTRUE(b) ? 1 : 0); 52 153 205
if ((ta & JSF_T_BOOLEAN) && (tb & JSF_T_BOOLEAN)) return (SvTRUE(a) ? 1 : 0) == (SvTRUE(b) ? 1 : 0); 24 28 52
67 181 ▶ if ((ta & JSF_T_NULL) && (tb & JSF_T_NULL)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_NULL) && (tb & JSF_T_NULL)) return 1; 10 171 181
if ((ta & JSF_T_NULL) && (tb & JSF_T_NULL)) return 1; 2 8 10
68 179 ▶ if ((ta & JSF_T_ARRAY) && (tb & JSF_T_ARRAY)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_ARRAY) && (tb & JSF_T_ARRAY)) { 40 139 179
if ((ta & JSF_T_ARRAY) && (tb & JSF_T_ARRAY)) { 32 8 40
69 32 AV *aa = (AV *)SvRV(a), *ab = (AV *)SvRV(b);
70 32 SSize_t i, na = av_len(aa) + 1;
71 32 ▶ if (na != av_len(ab) + 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (na != av_len(ab) + 1) return 0; 1 31 32
72 42 ▶ for (i = 0; i < na; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < na; i++) { 30 12 42
73 30 SV **x = av_fetch(aa, i, 0), **y = av_fetch(ab, i, 0);
74 30 ▶ if (!jsf_json_equal(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!jsf_json_equal(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef)) return 0; 30 0 30
if (!jsf_json_equal(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef)) return 0; 30 0 30
if (!jsf_json_equal(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef)) return 0; 19 11 30
75 }
76 12 return 1;
77 }
78 147 ▶ if ((ta & JSF_T_OBJECT) && (tb & JSF_T_OBJECT)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_OBJECT) && (tb & JSF_T_OBJECT)) { 53 94 147
if ((ta & JSF_T_OBJECT) && (tb & JSF_T_OBJECT)) { 32 21 53
79 32 HV *ha = (HV *)SvRV(a), *hb = (HV *)SvRV(b); HE *he;
80 32 ▶ if (HvKEYS(ha) != HvKEYS(hb)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (HvKEYS(ha) != HvKEYS(hb)) return 0; 0 32 32
if (HvKEYS(ha) != HvKEYS(hb)) return 0; 0 32 32
if (HvKEYS(ha) != HvKEYS(hb)) return 0; 2 30 32
81 30 hv_iterinit(ha);
82 49 ▶ while ((he = hv_iternext(ha))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ha))) { 33 16 49
83 33 ▶ STRLEN kl; char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; char *k = HePV(he, kl); 0 33 33
84 33 SV **y = hv_fetch(hb, k, (I32)kl, 0);
85 33 ▶ if (!y || !jsf_json_equal(aTHX_ HeVAL(he), *y)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!y || !jsf_json_equal(aTHX_ HeVAL(he), *y)) return 0; 32 1 33
if (!y || !jsf_json_equal(aTHX_ HeVAL(he), *y)) return 0; 13 19 32
86 }
87 16 return 1;
88 }
89 115 return 0;
90 }
91  
92 /* enum/const under coercion: a string standing in for a number or a
93 * boolean must also compare as one, or "?page=1" passes `type: integer`
94 * and then fails `enum: [1, 2]` on the string it still is. Only the
95 * instance (a) is read across domains - the schema side keeps the type
96 * it declared. */
97 12 static int jsf_json_equal_coerced(pTHX_ SV *a, SV *b) {
98 12 unsigned ta = jsf_classify_type(aTHX_ a);
99 12 unsigned tb = jsf_classify_type(aTHX_ b);
100 12 ▶ if ((ta & JSF_T_STRING) && !(tb & JSF_T_STRING)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((ta & JSF_T_STRING) && !(tb & JSF_T_STRING)) { 10 2 12
if ((ta & JSF_T_STRING) && !(tb & JSF_T_STRING)) { 9 1 10
101 9 ▶ if ((tb & JSF_T_NUMBER) && looks_like_number(a))
Branch: 2 branches
BranchTrueFalseTotal
if ((tb & JSF_T_NUMBER) && looks_like_number(a)) 7 2 9
if ((tb & JSF_T_NUMBER) && looks_like_number(a)) 7 0 7
102 7 return SvNV(a) == jsf__nv_of(aTHX_ b);
103 2 ▶ if (tb & JSF_T_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb & JSF_T_BOOLEAN) { 2 0 2
104 2 STRLEN l; const char *s = SvPV_const(a, l);
105 3 ▶ if (l == 4 && memEQ(s, "true", 4)) return SvTRUE(b) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(s, "true", 4)) return SvTRUE(b) ? 1 : 0; 1 1 2
if (l == 4 && memEQ(s, "true", 4)) return SvTRUE(b) ? 1 : 0; 1 0 1
106 1 ▶ if (l == 5 && memEQ(s, "false", 5)) return SvTRUE(b) ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(s, "false", 5)) return SvTRUE(b) ? 0 : 1; 1 0 1
if (l == 5 && memEQ(s, "false", 5)) return SvTRUE(b) ? 0 : 1; 1 0 1
107 }
108 }
109 3 return jsf_json_equal(aTHX_ a, b);
110 }
111  
112 /* ---- pattern regex (compiled once, cached) ------------------------------ */
113 111 static REGEXP *jsf_get_regex(pTHX_ jsf_compiled_t *C, uint32_t pat_off) {
114 int i; uint32_t len; const char *p; SV *pat; REGEXP *rx;
115 124 ▶ for (i = 0; i < C->rxn; i++) if (C->rx[i].pat_off == pat_off) return C->rx[i].rx;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < C->rxn; i++) if (C->rx[i].pat_off == pat_off) return C->rx[i].rx; 87 13 100
for (i = 0; i < C->rxn; i++) if (C->rx[i].pat_off == pat_off) return C->rx[i].rx; 100 24 124
116 24 p = jsf_str_bytes(C->arena, pat_off, &len);
117 24 pat = sv_2mortal(newSVpvn(p, len));
118 24 rx = pregcomp(pat, 0);
119 24 ▶ if (C->rxn == C->rxcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (C->rxn == C->rxcap) { 21 3 24
120 21 ▶ int nc = C->rxcap ? C->rxcap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int nc = C->rxcap ? C->rxcap * 2 : 8; 0 21 21
121 21 jsf_rxent *nr = (jsf_rxent *)realloc(C->rx, nc * sizeof(jsf_rxent));
122 21 ▶ if (!nr) return rx;
Branch: 1 branch
BranchTrueFalseTotal
if (!nr) return rx; 0 21 21
123 21 C->rx = nr; C->rxcap = nc;
124 }
125 24 C->rx[C->rxn].pat_off = pat_off; C->rx[C->rxn].rx = rx; C->rxn++;
126 24 return rx;
127 }
128  
129 111 static int jsf_rx_match(pTHX_ REGEXP *rx, SV *sv) {
130 STRLEN l; char *s;
131 111 ▶ if (!rx) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!rx) return 1; 0 111 111
132 111 s = SvPV(sv, l);
133 111 return pregexec(rx, s, s + l, s, 0, sv, 1) ? 1 : 0;
134 }
135  
136 52 static int jsf_find_prop(jsf_proptab *pt, jsf_propent *en, char *base,
137 const char *k, STRLEN kl) {
138 52 int lo = 0, hi = (int)pt->n - 1;
139 93 ▶ while (lo <= hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo <= hi) { 67 26 93
140 67 int mid = (lo + hi) >> 1;
141 67 const char *nm = base + en[mid].name_off + sizeof(jsf_str_t);
142 67 uint32_t nl = en[mid].name_len, m = nl < (uint32_t)kl ? nl : (uint32_t)kl;
143 67 int c = memcmp(k, nm, m);
144 67 ▶ if (c == 0) c = ((uint32_t)kl < nl) ? -1 : ((uint32_t)kl > nl ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0) c = ((uint32_t)kl < nl) ? -1 : ((uint32_t)kl > nl ? 1 : 0); 27 40 67
if (c == 0) c = ((uint32_t)kl < nl) ? -1 : ((uint32_t)kl > nl ? 1 : 0); 26 1 27
145 67 ▶ if (c == 0) return mid;
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) return mid; 26 41 67
146 41 ▶ if (c < 0) hi = mid - 1; else lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) hi = mid - 1; else lo = mid + 1; 10 31 41
147 }
148 26 return -1;
149 }
150  
151 /* ---- instanceLocation buffer (JSON Pointer, RFC 6901 escaping) ---------- */
152 10 static STRLEN jsf_iloc_push_key(pTHX_ SV *iloc, const char *k, STRLEN kl) {
153 10 STRLEN save = SvCUR(iloc), i; int special = 0;
154 36 ▶ for (i = 0; i < kl; i++) if (k[i] == '~' || k[i] == '/') { special = 1; break; }
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < kl; i++) if (k[i] == '~' || k[i] == '/') { special = 1; break; } 27 1 28
for (i = 0; i < kl; i++) if (k[i] == '~' || k[i] == '/') { special = 1; break; } 1 26 27
for (i = 0; i < kl; i++) if (k[i] == '~' || k[i] == '/') { special = 1; break; } 28 8 36
155 10 sv_catpvn(iloc, "/", 1);
156 10 ▶ if (!special) { sv_catpvn(iloc, k, kl); }
Branch: 1 branch
BranchTrueFalseTotal
if (!special) { sv_catpvn(iloc, k, kl); } 8 2 10
157 8 ▶ else for (i = 0; i < kl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 0; i < kl; i++) { 6 2 8
158 6 ▶ if (k[i] == '~') sv_catpvn(iloc, "~0", 2);
Branch: 1 branch
BranchTrueFalseTotal
if (k[i] == '~') sv_catpvn(iloc, "~0", 2); 1 5 6
159 5 ▶ else if (k[i] == '/') sv_catpvn(iloc, "~1", 2);
Branch: 1 branch
BranchTrueFalseTotal
else if (k[i] == '/') sv_catpvn(iloc, "~1", 2); 1 4 5
160 4 else sv_catpvn(iloc, k + i, 1);
161 }
162 10 return save;
163 }
164 5 static STRLEN jsf_iloc_push_idx(pTHX_ SV *iloc, IV idx) {
165 5 STRLEN save = SvCUR(iloc);
166 5 sv_catpvf(iloc, "/%" IVdf, idx);
167 5 return save;
168 }
169 15 static void jsf_iloc_pop(SV *iloc, STRLEN save) {
170 15 SvCUR_set(iloc, save);
171 15 SvPVX(iloc)[save] = '\0';
172 15 }
173  
174 /* ---- error push (cold path) --------------------------------------------- */
175 15 static void jsf_err_push(pTHX_ jsf_ctx_t *ctx, jsf_compiled_t *C, uint32_t off, const char *kw) {
176 15 jsf_node_t *n = (jsf_node_t *)jsf_arena_ptr(C->arena, off);
177 15 uint32_t sl; const char *sp = jsf_str_bytes(C->arena, n->schema_path_off, &sl);
178 15 HV *e = newHV();
179 15 SV *schemaLoc = newSVpvn(sp, sl);
180 15 sv_catpvs(schemaLoc, "/");
181 15 sv_catpv(schemaLoc, kw);
182 15 (void)hv_stores(e, "instanceLocation", newSVsv(ctx->iloc));
183 15 (void)hv_stores(e, "keyword", newSVpv(kw, 0));
184 15 (void)hv_stores(e, "schemaLocation", schemaLoc);
185 15 (void)hv_stores(e, "message", newSVpvf("failed '%s' constraint", kw));
186 15 av_push(ctx->errors, newRV_noinc((SV *)e));
187 15 }
188  
189 /* coercion (opt-in): which type bits a string can stand in for */
190 16 static unsigned jsf_coerce_bits(pTHX_ uint32_t mask, SV *data, unsigned t) {
191 STRLEN l; const char *s;
192 16 ▶ if (!(t & JSF_T_STRING)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(t & JSF_T_STRING)) return 0; 0 16 16
193 16 s = SvPV_const(data, l);
194 16 ▶ if ((mask & (JSF_T_NUMBER | JSF_T_INTEGER)) && looks_like_number(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((mask & (JSF_T_NUMBER | JSF_T_INTEGER)) && looks_like_number(data)) { 11 5 16
if ((mask & (JSF_T_NUMBER | JSF_T_INTEGER)) && looks_like_number(data)) { 10 1 11
195 10 NV nv = SvNV(data);
196 10 int integral = (nv == (NV)(IV)nv);
197 10 ▶ if (mask & JSF_T_NUMBER) return (unsigned)(JSF_T_NUMBER | (integral ? JSF_T_INTEGER : 0));
Branch: 2 branches
BranchTrueFalseTotal
if (mask & JSF_T_NUMBER) return (unsigned)(JSF_T_NUMBER | (integral ? JSF_T_INTEGER : 0)); 1 9 10
if (mask & JSF_T_NUMBER) return (unsigned)(JSF_T_NUMBER | (integral ? JSF_T_INTEGER : 0)); 0 1 1
198 9 ▶ if ((mask & JSF_T_INTEGER) && integral) return JSF_T_NUMBER | JSF_T_INTEGER;
Branch: 2 branches
BranchTrueFalseTotal
if ((mask & JSF_T_INTEGER) && integral) return JSF_T_NUMBER | JSF_T_INTEGER; 9 0 9
if ((mask & JSF_T_INTEGER) && integral) return JSF_T_NUMBER | JSF_T_INTEGER; 8 1 9
199 }
200 7 ▶ if (mask & JSF_T_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (mask & JSF_T_BOOLEAN) { 5 2 7
201 5 ▶ if ((l == 4 && memEQ(s, "true", 4)) || (l == 5 && memEQ(s, "false", 5))) return JSF_T_BOOLEAN;
Branch: 4 branches
BranchTrueFalseTotal
if ((l == 4 && memEQ(s, "true", 4)) || (l == 5 && memEQ(s, "false", 5))) return JSF_T_BOOLEAN; 2 3 5
if ((l == 4 && memEQ(s, "true", 4)) || (l == 5 && memEQ(s, "false", 5))) return JSF_T_BOOLEAN; 0 2 2
if ((l == 4 && memEQ(s, "true", 4)) || (l == 5 && memEQ(s, "false", 5))) return JSF_T_BOOLEAN; 2 1 3
if ((l == 4 && memEQ(s, "true", 4)) || (l == 5 && memEQ(s, "false", 5))) return JSF_T_BOOLEAN; 2 0 2
202 }
203 3 return 0;
204 }
205  
206 /* run a subschema as a boolean probe (no error leakage, no annotations) */
207 247 static int jsf_probe(pTHX_ jsf_compiled_t *C, uint32_t off, SV *data, jsf_ctx_t *parent) {
208 jsf_ctx_t p;
209 247 p.errors = NULL; p.iloc = NULL; p.collect = 0; p.depth = parent->depth; p.ann = NULL;
210 247 p.dynscope = parent->dynscope;
211 247 return jsf_validate(aTHX_ C, off, data, &p);
212 }
213  
214 /* probe that collects annotations into `ann` (for in-place applicators whose
215 * successful branches contribute to the parent's evaluated set) */
216 421 static int jsf_probe_ann(pTHX_ jsf_compiled_t *C, uint32_t off, SV *data, jsf_ctx_t *parent, jsf_annot *ann) {
217 jsf_ctx_t p;
218 421 p.errors = NULL; p.iloc = NULL; p.collect = 0; p.depth = parent->depth; p.ann = ann;
219 421 p.dynscope = parent->dynscope;
220 421 return jsf_validate(aTHX_ C, off, data, &p);
221 }
222  
223 #define JSF_FAIL(kw) do { \
224 if (ctx->errors) jsf_err_push(aTHX_ ctx, C, off, (kw)); \
225 ok = 0; if (!ctx->collect) goto done; \
226 } while (0)
227  
228 /* recurse into a data child (a new instance): keep the instanceLocation in sync
229 * and give the child its own annotation collector. */
230 #define JSF_CHILD(coff, cdata, pushexpr) do { \
231 STRLEN _sav = 0; int _r; jsf_annot *_oa = ctx->ann; \
232 if (ctx->errors) _sav = (pushexpr); \
233 ctx->ann = C->has_unevaluated ? jsf_annot_new(aTHX) : NULL; \
234 _r = jsf_validate(aTHX_ C, (coff), (cdata), ctx); \
235 if (ctx->ann) jsf_annot_free(aTHX_ ctx->ann); \
236 ctx->ann = _oa; \
237 if (ctx->errors) jsf_iloc_pop(ctx->iloc, _sav); \
238 if (!_r) { ok = 0; if (!ctx->collect) goto done; } \
239 } while (0)
240  
241 4101 static int jsf_validate(pTHX_ jsf_compiled_t *C, uint32_t off, SV *data, jsf_ctx_t *ctx) {
242 4101 jsf_arena_t *a = C->arena;
243 4101 jsf_node_t *n = (jsf_node_t *)jsf_arena_ptr(a, off);
244 unsigned t;
245 4101 int ok = 1;
246 4101 HV *filled = NULL; /* apply_defaults working copy (freed at done) */
247 4101 jsf_annot *sink = NULL, *local = NULL; /* evaluated-set (unevaluated*) */
248 4101 int pushed_dyn = 0; /* pushed this resource's base onto the dynamic scope */
249  
250 4101 ▶ if (n->tag == JSF_TAG_TRUE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n->tag == JSF_TAG_TRUE) return 1; 309 3792 4101
251 3792 ▶ if (n->tag == JSF_TAG_FALSE) { if (ctx->errors) jsf_err_push(aTHX_ ctx, C, off, "false"); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (n->tag == JSF_TAG_FALSE) { if (ctx->errors) jsf_err_push(aTHX_ ctx, C, off, "false"); return 0; } 127 3665 3792
if (n->tag == JSF_TAG_FALSE) { if (ctx->errors) jsf_err_push(aTHX_ ctx, C, off, "false"); return 0; } 0 127 127
252 3665 ▶ if (++ctx->depth > JSF_MAX_DEPTH) { ctx->depth--; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (++ctx->depth > JSF_MAX_DEPTH) { ctx->depth--; return 0; } 0 3665 3665
253  
254 /* This frame collects its own evaluated set (from its keywords and its
255 * in-place applicators); on success it merges into the caller's set (sink).
256 * A sibling ("cousin") subschema therefore does not see it. */
257 3665 sink = ctx->ann;
258 3665 ▶ if (C->has_unevaluated) { local = jsf_annot_new(aTHX); ctx->ann = local; }
Branch: 1 branch
BranchTrueFalseTotal
if (C->has_unevaluated) { local = jsf_annot_new(aTHX); ctx->ann = local; } 962 2703 3665
259  
260 /* track the dynamic scope: push this node's resource base when it differs
261 * from the current innermost, so $dynamicRef can search it. */
262 3665 ▶ if (C->has_dynamic && ctx->dynscope) {
Branch: 2 branches
BranchTrueFalseTotal
if (C->has_dynamic && ctx->dynscope) { 346 3319 3665
if (C->has_dynamic && ctx->dynscope) { 346 0 346
263 346 uint32_t bl2; const char *bp2 = jsf_str_bytes(a, n->base_off, &bl2);
264 346 SSize_t top = av_len(ctx->dynscope); int same = 0;
265 346 ▶ if (top >= 0) { SV **tp = av_fetch(ctx->dynscope, top, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (top >= 0) { SV **tp = av_fetch(ctx->dynscope, top, 0); 293 53 346
266 586 ▶ if (tp && *tp) { STRLEN tl; const char *ts = SvPV_const(*tp, tl); if (tl == bl2 && memEQ(ts, bp2, bl2)) same = 1; } }
Branch: 4 branches
BranchTrueFalseTotal
if (tp && *tp) { STRLEN tl; const char *ts = SvPV_const(*tp, tl); if (tl == bl2 && memEQ(ts, bp2, bl2)) same = 1; } } 293 0 293
if (tp && *tp) { STRLEN tl; const char *ts = SvPV_const(*tp, tl); if (tl == bl2 && memEQ(ts, bp2, bl2)) same = 1; } } 293 0 293
if (tp && *tp) { STRLEN tl; const char *ts = SvPV_const(*tp, tl); if (tl == bl2 && memEQ(ts, bp2, bl2)) same = 1; } } 182 111 293
if (tp && *tp) { STRLEN tl; const char *ts = SvPV_const(*tp, tl); if (tl == bl2 && memEQ(ts, bp2, bl2)) same = 1; } } 165 17 182
267 346 ▶ if (!same) { av_push(ctx->dynscope, newSVpvn(bp2, bl2)); pushed_dyn = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!same) { av_push(ctx->dynscope, newSVpvn(bp2, bl2)); pushed_dyn = 1; } 181 165 346
268 }
269  
270 3665 ▶ if ((n->present & JSF_HAS_REF) && n->ref_off) {
Branch: 2 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_REF) && n->ref_off) { 488 3177 3665
if ((n->present & JSF_HAS_REF) && n->ref_off) { 488 0 488
271 488 ▶ if (!jsf_validate(aTHX_ C, n->ref_off, data, ctx)) { ok = 0; if (!ctx->collect) goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!jsf_validate(aTHX_ C, n->ref_off, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 151 337 488
if (!jsf_validate(aTHX_ C, n->ref_off, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 151 0 151
272 }
273 /* $dynamicRef: outermost matching $dynamicAnchor in the dynamic scope wins;
274 * otherwise fall back to the lexical target (ref_off). */
275 3514 ▶ if (n->present & JSF_HAS_DYNREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_DYNREF) { 60 3454 3514
276 60 uint32_t target = n->ref_off;
277 60 ▶ if (ctx->dynscope && n->dynref_name_off) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->dynscope && n->dynref_name_off) { 60 0 60
if (ctx->dynscope && n->dynref_name_off) { 46 14 60
278 46 uint32_t nml; const char *nm = jsf_str_bytes(a, n->dynref_name_off, &nml);
279 46 SSize_t i, top = av_len(ctx->dynscope);
280 65 ▶ for (i = 0; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= top; i++) { 65 0 65
281 65 SV **b = av_fetch(ctx->dynscope, i, 0);
282 SV *key; STRLEN kl; const char *kp; SV **te;
283 65 ▶ if (!b || !*b) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !*b) continue; 65 0 65
if (!b || !*b) continue; 0 65 65
284 65 key = sv_2mortal(newSVsv(*b)); sv_catpvs(key, "#"); sv_catpvn(key, nm, nml);
285 65 kp = SvPV_const(key, kl);
286 65 te = hv_fetch(C->dynmap, kp, (I32)kl, 0);
287 65 ▶ if (te && *te && SvIOK(*te)) { target = (uint32_t)SvIV(*te); break; }
Branch: 3 branches
BranchTrueFalseTotal
if (te && *te && SvIOK(*te)) { target = (uint32_t)SvIV(*te); break; } 46 19 65
if (te && *te && SvIOK(*te)) { target = (uint32_t)SvIV(*te); break; } 46 0 46
if (te && *te && SvIOK(*te)) { target = (uint32_t)SvIV(*te); break; } 46 0 46
288 }
289 }
290 60 ▶ if (target && !jsf_validate(aTHX_ C, target, data, ctx)) { ok = 0; if (!ctx->collect) goto done; }
Branch: 3 branches
BranchTrueFalseTotal
if (target && !jsf_validate(aTHX_ C, target, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 60 0 60
if (target && !jsf_validate(aTHX_ C, target, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 19 41 60
if (target && !jsf_validate(aTHX_ C, target, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 19 0 19
291 }
292  
293 3495 t = jsf_classify_type(aTHX_ data);
294 3495 ▶ if (C->coerce && (n->present & JSF_HAS_TYPE) && !(t & n->type_mask))
Branch: 3 branches
BranchTrueFalseTotal
if (C->coerce && (n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) 18 3477 3495
if (C->coerce && (n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) 18 0 18
if (C->coerce && (n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) 16 2 18
295 16 t |= jsf_coerce_bits(aTHX_ n->type_mask, data, t);
296  
297 /* the validation vocabulary (type/const/enum, the numeric/string/count
298 * keywords) is skipped when a custom $schema metaschema disables it. */
299 3495 ▶ if (!C->no_validation_vocab) {
Branch: 1 branch
BranchTrueFalseTotal
if (!C->no_validation_vocab) { 3488 7 3495
300  
301 3488 ▶ if ((n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) JSF_FAIL("type");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) JSF_FAIL("type"); 1157 2331 3488
if ((n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) JSF_FAIL("type"); 231 926 1157
if ((n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) JSF_FAIL("type"); 7 224 231
if ((n->present & JSF_HAS_TYPE) && !(t & n->type_mask)) JSF_FAIL("type"); 224 7 231
302  
303 3264 ▶ if (n->present & JSF_HAS_CONST) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_CONST) { 261 3003 3264
304 261 SV **cv = av_fetch(C->keep, n->const_keep, 0);
305 522 ▶ int eq = cv && (C->coerce
Branch: 1 branch
BranchTrueFalseTotal
int eq = cv && (C->coerce 261 0 261
306 2 ? jsf_json_equal_coerced(aTHX_ data, *cv)
307 259 : jsf_json_equal(aTHX_ data, *cv));
308 261 ▶ if (!eq) JSF_FAIL("const");
Branch: 3 branches
BranchTrueFalseTotal
if (!eq) JSF_FAIL("const"); 112 149 261
if (!eq) JSF_FAIL("const"); 0 112 112
if (!eq) JSF_FAIL("const"); 112 0 112
309 }
310 3152 ▶ if (n->present & JSF_HAS_ENUM) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_ENUM) { 74 3078 3152
311 74 int found = 0; uint32_t i;
312 146 ▶ for (i = 0; i < n->enum_n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->enum_n; i++) { 112 34 146
313 112 SV **ev = av_fetch(C->keep, n->enum_keep + i, 0);
314 224 ▶ if (ev && (C->coerce
Branch: 3 branches
BranchTrueFalseTotal
if (ev && (C->coerce 112 0 112
if (ev && (C->coerce 10 102 112
if (ev && (C->coerce 40 72 112
315 10 ? jsf_json_equal_coerced(aTHX_ data, *ev)
316 142 : jsf_json_equal(aTHX_ data, *ev))) { found = 1; break; }
317 }
318 74 ▶ if (!found) JSF_FAIL("enum");
Branch: 3 branches
BranchTrueFalseTotal
if (!found) JSF_FAIL("enum"); 34 40 74
if (!found) JSF_FAIL("enum"); 0 34 34
if (!found) JSF_FAIL("enum"); 34 0 34
319 }
320  
321 /* Gated on the keywords actually present: a number the schema only gives a
322 * `type` is never read numerically at all, which is both one branch cheaper
323 * and one fewer chance to touch the value. */
324 3118 ▶ if ((t & JSF_T_NUMBER)
Branch: 1 branch
BranchTrueFalseTotal
if ((t & JSF_T_NUMBER) 708 2410 3118
325 708 ▶ && (n->present & (JSF_HAS_MIN | JSF_HAS_MAX | JSF_HAS_EXMIN
Branch: 1 branch
BranchTrueFalseTotal
&& (n->present & (JSF_HAS_MIN | JSF_HAS_MAX | JSF_HAS_EXMIN 190 518 708
326 | JSF_HAS_EXMAX | JSF_HAS_MULOF))) {
327 190 NV x = jsf__nv_of(aTHX_ data);
328 190 ▶ if ((n->present & JSF_HAS_MIN) && x < n->minimum) JSF_FAIL("minimum");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MIN) && x < n->minimum) JSF_FAIL("minimum"); 71 119 190
if ((n->present & JSF_HAS_MIN) && x < n->minimum) JSF_FAIL("minimum"); 33 38 71
if ((n->present & JSF_HAS_MIN) && x < n->minimum) JSF_FAIL("minimum"); 3 30 33
if ((n->present & JSF_HAS_MIN) && x < n->minimum) JSF_FAIL("minimum"); 30 3 33
329 160 ▶ if ((n->present & JSF_HAS_MAX) && x > n->maximum) JSF_FAIL("maximum");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MAX) && x > n->maximum) JSF_FAIL("maximum"); 26 134 160
if ((n->present & JSF_HAS_MAX) && x > n->maximum) JSF_FAIL("maximum"); 9 17 26
if ((n->present & JSF_HAS_MAX) && x > n->maximum) JSF_FAIL("maximum"); 0 9 9
if ((n->present & JSF_HAS_MAX) && x > n->maximum) JSF_FAIL("maximum"); 9 0 9
330 151 ▶ if ((n->present & JSF_HAS_EXMIN) && x <= n->exmin) JSF_FAIL("exclusiveMinimum");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_EXMIN) && x <= n->exmin) JSF_FAIL("exclusiveMinimum"); 10 141 151
if ((n->present & JSF_HAS_EXMIN) && x <= n->exmin) JSF_FAIL("exclusiveMinimum"); 4 6 10
if ((n->present & JSF_HAS_EXMIN) && x <= n->exmin) JSF_FAIL("exclusiveMinimum"); 0 4 4
if ((n->present & JSF_HAS_EXMIN) && x <= n->exmin) JSF_FAIL("exclusiveMinimum"); 4 0 4
331 147 ▶ if ((n->present & JSF_HAS_EXMAX) && x >= n->exmax) JSF_FAIL("exclusiveMaximum");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_EXMAX) && x >= n->exmax) JSF_FAIL("exclusiveMaximum"); 18 129 147
if ((n->present & JSF_HAS_EXMAX) && x >= n->exmax) JSF_FAIL("exclusiveMaximum"); 9 9 18
if ((n->present & JSF_HAS_EXMAX) && x >= n->exmax) JSF_FAIL("exclusiveMaximum"); 0 9 9
if ((n->present & JSF_HAS_EXMAX) && x >= n->exmax) JSF_FAIL("exclusiveMaximum"); 9 0 9
332 138 ▶ if (n->present & JSF_HAS_MULOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_MULOF) { 75 63 138
333 75 NV m = n->mulof;
334 75 ▶ if (m != 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (m != 0.0) { 75 0 75
335 75 ▶ NV eps = 1e-9 * (fabs(m) > 1.0 ? fabs(m) : 1.0), r = fmod(x, m);
Branch: 1 branch
BranchTrueFalseTotal
NV eps = 1e-9 * (fabs(m) > 1.0 ? fabs(m) : 1.0), r = fmod(x, m); 70 5 75
336 75 ▶ if (fabs(r) > eps && fabs(fabs(r) - fabs(m)) > eps) JSF_FAIL("multipleOf");
Branch: 4 branches
BranchTrueFalseTotal
if (fabs(r) > eps && fabs(fabs(r) - fabs(m)) > eps) JSF_FAIL("multipleOf"); 36 39 75
if (fabs(r) > eps && fabs(fabs(r) - fabs(m)) > eps) JSF_FAIL("multipleOf"); 35 1 36
if (fabs(r) > eps && fabs(fabs(r) - fabs(m)) > eps) JSF_FAIL("multipleOf"); 0 35 35
if (fabs(r) > eps && fabs(fabs(r) - fabs(m)) > eps) JSF_FAIL("multipleOf"); 35 0 35
337 }
338 }
339 }
340  
341 3031 ▶ if (t & JSF_T_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (t & JSF_T_STRING) { 478 2553 3031
342 478 ▶ if (n->present & (JSF_HAS_MINLEN | JSF_HAS_MAXLEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & (JSF_HAS_MINLEN | JSF_HAS_MAXLEN)) { 46 432 478
343 46 UV len = (UV)sv_len_utf8(data);
344 46 ▶ if ((n->present & JSF_HAS_MINLEN) && len < n->min_length) JSF_FAIL("minLength");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MINLEN) && len < n->min_length) JSF_FAIL("minLength"); 23 23 46
if ((n->present & JSF_HAS_MINLEN) && len < n->min_length) JSF_FAIL("minLength"); 9 14 23
if ((n->present & JSF_HAS_MINLEN) && len < n->min_length) JSF_FAIL("minLength"); 0 9 9
if ((n->present & JSF_HAS_MINLEN) && len < n->min_length) JSF_FAIL("minLength"); 9 0 9
345 37 ▶ if ((n->present & JSF_HAS_MAXLEN) && len > n->max_length) JSF_FAIL("maxLength");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MAXLEN) && len > n->max_length) JSF_FAIL("maxLength"); 27 10 37
if ((n->present & JSF_HAS_MAXLEN) && len > n->max_length) JSF_FAIL("maxLength"); 10 17 27
if ((n->present & JSF_HAS_MAXLEN) && len > n->max_length) JSF_FAIL("maxLength"); 0 10 10
if ((n->present & JSF_HAS_MAXLEN) && len > n->max_length) JSF_FAIL("maxLength"); 10 0 10
346 }
347 472 if ((n->present & JSF_HAS_PATTERN) &&
348 17 ▶ !jsf_rx_match(aTHX_ jsf_get_regex(aTHX_ C, n->pattern_off), data)) JSF_FAIL("pattern");
Branch: 2 branches
BranchTrueFalseTotal
!jsf_rx_match(aTHX_ jsf_get_regex(aTHX_ C, n->pattern_off), data)) JSF_FAIL("pattern"); 0 4 4
!jsf_rx_match(aTHX_ jsf_get_regex(aTHX_ C, n->pattern_off), data)) JSF_FAIL("pattern"); 4 0 4
349 }
350  
351 } /* end validation-vocabulary scalar keywords */
352  
353 3015 ▶ if (t & JSF_T_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (t & JSF_T_ARRAY) { 466 2549 3015
354 466 AV *av = (AV *)SvRV(data);
355 466 SSize_t i, cnt = av_len(av) + 1;
356 466 uint32_t prefn = 0;
357 466 ▶ if ((n->present & JSF_HAS_MINITEMS) && (UV)cnt < n->min_items) JSF_FAIL("minItems");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MINITEMS) && (UV)cnt < n->min_items) JSF_FAIL("minItems"); 12 454 466
if ((n->present & JSF_HAS_MINITEMS) && (UV)cnt < n->min_items) JSF_FAIL("minItems"); 5 7 12
if ((n->present & JSF_HAS_MINITEMS) && (UV)cnt < n->min_items) JSF_FAIL("minItems"); 0 5 5
if ((n->present & JSF_HAS_MINITEMS) && (UV)cnt < n->min_items) JSF_FAIL("minItems"); 5 0 5
358 461 ▶ if ((n->present & JSF_HAS_MAXITEMS) && (UV)cnt > n->max_items) JSF_FAIL("maxItems");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MAXITEMS) && (UV)cnt > n->max_items) JSF_FAIL("maxItems"); 12 449 461
if ((n->present & JSF_HAS_MAXITEMS) && (UV)cnt > n->max_items) JSF_FAIL("maxItems"); 5 7 12
if ((n->present & JSF_HAS_MAXITEMS) && (UV)cnt > n->max_items) JSF_FAIL("maxItems"); 0 5 5
if ((n->present & JSF_HAS_MAXITEMS) && (UV)cnt > n->max_items) JSF_FAIL("maxItems"); 5 0 5
359 456 ▶ if (n->present & JSF_HAS_PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_PREFIX) { 135 321 456
360 135 jsf_offlist *ol = (jsf_offlist *)jsf_arena_ptr(a, n->prefix_off);
361 135 uint32_t *offs = (uint32_t *)((char *)ol + sizeof(jsf_offlist));
362 135 prefn = ol->n;
363 342 ▶ for (i = 0; i < cnt && (uint32_t)i < prefn; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < cnt && (uint32_t)i < prefn; i++) { 283 59 342
for (i = 0; i < cnt && (uint32_t)i < prefn; i++) { 221 62 283
364 221 SV **el = av_fetch(av, i, 0);
365 221 jsf_annot_add_idx(aTHX_ ctx->ann, i);
366 221 ▶ if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i));
Branch: 7 branches
BranchTrueFalseTotal
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 221 0 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 0 221 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 107 114 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 107 114 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 0 221 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 14 207 221
if (el) JSF_CHILD(offs[i], *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 14 0 14
367 }
368 }
369 442 ▶ if (n->present & JSF_HAS_ITEMS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_ITEMS) { 119 323 442
370 221 ▶ for (i = (SSize_t)prefn; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (SSize_t)prefn; i < cnt; i++) { 143 78 221
371 143 SV **el = av_fetch(av, i, 0);
372 143 ▶ if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i));
Branch: 7 branches
BranchTrueFalseTotal
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 143 0 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 5 138 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 17 126 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 17 126 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 5 138 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 43 100 143
if (el) JSF_CHILD(n->items_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 41 2 43
373 }
374 78 ▶ if (ctx->ann && cnt > (SSize_t)prefn) ctx->ann->all_items = 1; /* items covers the tail */
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->ann && cnt > (SSize_t)prefn) ctx->ann->all_items = 1; /* items covers the tail */ 7 71 78
if (ctx->ann && cnt > (SSize_t)prefn) ctx->ann->all_items = 1; /* items covers the tail */ 6 1 7
375 }
376 401 ▶ if (n->unique) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->unique) { 44 357 401
377 110 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 85 25 110
378 85 SSize_t j; SV **ei = av_fetch(av, i, 0);
379 158 ▶ for (j = i + 1; j < cnt; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i + 1; j < cnt; j++) { 92 66 158
380 92 SV **ej = av_fetch(av, j, 0);
381 92 ▶ if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); }
Branch: 5 branches
BranchTrueFalseTotal
if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); } 92 0 92
if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); } 92 0 92
if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); } 19 73 92
if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); } 0 19 19
if (ei && ej && jsf_json_equal(aTHX_ *ei, *ej)) { JSF_FAIL("uniqueItems"); } 19 0 19
382 }
383 }
384 }
385 382 ▶ if (n->present & JSF_HAS_CONTAINS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_CONTAINS) { 82 300 382
386 82 ▶ UV matched = 0, minc = (n->present & JSF_HAS_MINCONT) ? n->min_contains : 1;
Branch: 1 branch
BranchTrueFalseTotal
UV matched = 0, minc = (n->present & JSF_HAS_MINCONT) ? n->min_contains : 1; 37 45 82
387 262 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 180 82 262
388 180 SV **el = av_fetch(av, i, 0);
389 180 ▶ if (el && jsf_probe(aTHX_ C, n->contains_off, *el, ctx)) { matched++; jsf_annot_add_idx(aTHX_ ctx->ann, i); }
Branch: 2 branches
BranchTrueFalseTotal
if (el && jsf_probe(aTHX_ C, n->contains_off, *el, ctx)) { matched++; jsf_annot_add_idx(aTHX_ ctx->ann, i); } 180 0 180
if (el && jsf_probe(aTHX_ C, n->contains_off, *el, ctx)) { matched++; jsf_annot_add_idx(aTHX_ ctx->ann, i); } 91 89 180
390 }
391 82 ▶ if (matched < minc) JSF_FAIL("contains");
Branch: 3 branches
BranchTrueFalseTotal
if (matched < minc) JSF_FAIL("contains"); 30 52 82
if (matched < minc) JSF_FAIL("contains"); 0 30 30
if (matched < minc) JSF_FAIL("contains"); 30 0 30
392 52 ▶ if ((n->present & JSF_HAS_MAXCONT) && matched > n->max_contains) JSF_FAIL("maxContains");
Branch: 4 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MAXCONT) && matched > n->max_contains) JSF_FAIL("maxContains"); 16 36 52
if ((n->present & JSF_HAS_MAXCONT) && matched > n->max_contains) JSF_FAIL("maxContains"); 8 8 16
if ((n->present & JSF_HAS_MAXCONT) && matched > n->max_contains) JSF_FAIL("maxContains"); 0 8 8
if ((n->present & JSF_HAS_MAXCONT) && matched > n->max_contains) JSF_FAIL("maxContains"); 8 0 8
393 }
394 }
395  
396 2893 ▶ if (t & JSF_T_OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if (t & JSF_T_OBJECT) { 1314 1579 2893
397 1314 HV *hv = (HV *)SvRV(data);
398 1314 jsf_proptab *pt = NULL; jsf_propent *en = NULL; char *base = a->base;
399  
400 /* apply_defaults: fill missing properties that have a `default` into a
401 * shallow working copy (never the caller's data). Only allocates when
402 * a defaulted property is actually absent. */
403 1314 ▶ if (C->apply_defaults && (n->present & JSF_HAS_PROPS)) {
Branch: 2 branches
BranchTrueFalseTotal
if (C->apply_defaults && (n->present & JSF_HAS_PROPS)) { 6 1308 1314
if (C->apply_defaults && (n->present & JSF_HAS_PROPS)) { 6 0 6
404 6 jsf_proptab *dpt = (jsf_proptab *)jsf_arena_ptr(a, n->props_off);
405 6 jsf_propent *den = (jsf_propent *)((char *)dpt + sizeof(jsf_proptab));
406 6 uint32_t p; int need = 0;
407 13 ▶ for (p = 0; p < dpt->n && !need; p++) {
Branch: 2 branches
BranchTrueFalseTotal
for (p = 0; p < dpt->n && !need; p++) { 10 3 13
for (p = 0; p < dpt->n && !need; p++) { 7 3 10
408 7 jsf_node_t *ch = (jsf_node_t *)jsf_arena_ptr(a, den[p].child_off);
409 7 ▶ if (ch->present & JSF_HAS_DEFAULT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch->present & JSF_HAS_DEFAULT) { 5 2 7
410 5 uint32_t nl; const char *nm = jsf_str_bytes(a, den[p].name_off, &nl);
411 5 ▶ if (!hv_exists(hv, nm, (I32)nl)) need = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(hv, nm, (I32)nl)) need = 1; 4 1 5
412 }
413 }
414 6 ▶ if (need) {
Branch: 1 branch
BranchTrueFalseTotal
if (need) { 4 2 6
415 11 ▶ for (p = 0; p < dpt->n; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < dpt->n; p++) { 7 4 11
416 7 jsf_node_t *ch = (jsf_node_t *)jsf_arena_ptr(a, den[p].child_off);
417 7 ▶ if (ch->present & JSF_HAS_DEFAULT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch->present & JSF_HAS_DEFAULT) { 4 3 7
418 4 uint32_t nl; const char *nm = jsf_str_bytes(a, den[p].name_off, &nl);
419 4 ▶ if (!hv_exists(hv, nm, (I32)nl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(hv, nm, (I32)nl)) { 4 0 4
420 4 SV **dv = av_fetch(C->keep, ch->default_keep, 0);
421 4 ▶ (void)hv_store(hv, nm, (I32)nl, newSVsv(dv ? *dv : &PL_sv_undef), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(hv, nm, (I32)nl, newSVsv(dv ? *dv : &PL_sv_undef), 0); 4 0 4
422 }
423 }
424 }
425 }
426 }
427  
428 1314 ▶ if (n->present & JSF_HAS_PROPS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_PROPS) { 672 642 1314
429 uint32_t p;
430 672 pt = (jsf_proptab *)jsf_arena_ptr(a, n->props_off);
431 672 en = (jsf_propent *)((char *)pt + sizeof(jsf_proptab));
432 1686 ▶ for (p = 0; p < pt->n; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < pt->n; p++) { 1104 582 1686
433 1104 uint32_t nl; const char *nm = jsf_str_bytes(a, en[p].name_off, &nl);
434 1104 SV **vp = (SV **)hv_common(hv, NULL, nm, nl, 0, HV_FETCH_JUST_SV, NULL, en[p].hash);
435 1104 ▶ if (vp && *vp) {
Branch: 2 branches
BranchTrueFalseTotal
if (vp && *vp) { 617 487 1104
if (vp && *vp) { 617 0 617
436 617 jsf_annot_add_key(aTHX_ ctx->ann, nm, nl);
437 617 ▶ JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl));
Branch: 6 branches
BranchTrueFalseTotal
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 10 607 617
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 160 457 617
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 160 457 617
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 10 607 617
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 97 520 617
JSF_CHILD(en[p].child_off, *vp, jsf_iloc_push_key(aTHX_ ctx->iloc, nm, nl)); 90 7 97
438 }
439 }
440 }
441 1224 ▶ if (n->present & (JSF_HAS_PATPROPS | JSF_HAS_ADDPROPS | JSF_HAS_PROPNAMES)) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & (JSF_HAS_PATPROPS | JSF_HAS_ADDPROPS | JSF_HAS_PROPNAMES)) { 132 1092 1224
442 132 HE *he; hv_iterinit(hv);
443 250 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 160 90 250
444 160 ▶ STRLEN kl; char *k = HePV(he, kl); SV *val = HeVAL(he);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; char *k = HePV(he, kl); SV *val = HeVAL(he); 0 160 160
445 160 int matched = 0;
446 160 ▶ if ((n->present & JSF_HAS_PROPS) && jsf_find_prop(pt, en, base, k, kl) >= 0) matched = 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_PROPS) && jsf_find_prop(pt, en, base, k, kl) >= 0) matched = 1; 52 108 160
if ((n->present & JSF_HAS_PROPS) && jsf_find_prop(pt, en, base, k, kl) >= 0) matched = 1; 26 26 52
447 160 ▶ if (n->present & JSF_HAS_PATPROPS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_PATPROPS) { 85 75 160
448 85 jsf_pattab *qt = (jsf_pattab *)jsf_arena_ptr(a, n->patprops_off);
449 85 jsf_patent *pe = (jsf_patent *)((char *)qt + sizeof(jsf_pattab));
450 85 SV *ksv = sv_2mortal(newSVpvn(k, kl)); uint32_t q;
451 170 ▶ for (q = 0; q < qt->n; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = 0; q < qt->n; q++) { 98 72 170
452 98 ▶ if (jsf_rx_match(aTHX_ jsf_get_regex(aTHX_ C, pe[q].pat_off), ksv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (jsf_rx_match(aTHX_ jsf_get_regex(aTHX_ C, pe[q].pat_off), ksv)) { 46 52 98
453 46 matched = 1;
454 46 jsf_annot_add_key(aTHX_ ctx->ann, k, kl);
455 75 ▶ JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl));
Branch: 6 branches
BranchTrueFalseTotal
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 46 46
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 15 31 46
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 15 31 46
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 46 46
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 13 33 46
JSF_CHILD(pe[q].child_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 13 0 13
456 }
457 }
458 }
459 147 ▶ if ((n->present & JSF_HAS_ADDPROPS) && !matched) {
Branch: 2 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_ADDPROPS) && !matched) { 68 79 147
if ((n->present & JSF_HAS_ADDPROPS) && !matched) { 40 28 68
460 40 ▶ if (n->addprops_off == JSF_NULL_OFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->addprops_off == JSF_NULL_OFF) { 16 24 40
461 16 STRLEN sav = 0;
462 16 ▶ if (ctx->errors) sav = jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->errors) sav = jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl); 0 16 16
463 16 ▶ JSF_FAIL("additionalProperties");
Branch: 2 branches
BranchTrueFalseTotal
JSF_FAIL("additionalProperties"); 0 16 16
JSF_FAIL("additionalProperties"); 16 0 16
464 0 ▶ if (ctx->errors) jsf_iloc_pop(ctx->iloc, sav);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->errors) jsf_iloc_pop(ctx->iloc, sav); 0 0 0
465 } else {
466 24 jsf_annot_add_key(aTHX_ ctx->ann, k, kl);
467 24 ▶ JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl));
Branch: 6 branches
BranchTrueFalseTotal
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 24 24
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 7 17 24
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 7 17 24
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 24 24
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 7 17 24
JSF_CHILD(n->addprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 7 0 7
468 }
469 }
470 124 ▶ if (n->present & JSF_HAS_PROPNAMES) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_PROPNAMES) { 21 103 124
471 21 SV *ksv = sv_2mortal(newSVpvn(k, kl));
472 21 ▶ if (!jsf_probe(aTHX_ C, n->propnames_off, ksv, ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!jsf_probe(aTHX_ C, n->propnames_off, ksv, ctx)) { 6 15 21
473 6 STRLEN sav = 0;
474 6 ▶ if (ctx->errors) sav = jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->errors) sav = jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl); 0 6 6
475 6 ▶ JSF_FAIL("propertyNames");
Branch: 2 branches
BranchTrueFalseTotal
JSF_FAIL("propertyNames"); 0 6 6
JSF_FAIL("propertyNames"); 6 0 6
476 0 ▶ if (ctx->errors) jsf_iloc_pop(ctx->iloc, sav);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->errors) jsf_iloc_pop(ctx->iloc, sav); 0 0 0
477 }
478 }
479 }
480 }
481 1182 ▶ if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties");
Branch: 5 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties"); 10 1172 1182
if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties"); 0 10 10
if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties"); 4 6 10
if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties"); 0 4 4
if ((n->present & JSF_HAS_MINPROPS) && (UV)HvKEYS(hv) < n->min_props) JSF_FAIL("minProperties"); 4 0 4
482 1178 ▶ if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties");
Branch: 5 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties"); 9 1169 1178
if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties"); 0 9 9
if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties"); 4 5 9
if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties"); 0 4 4
if ((n->present & JSF_HAS_MAXPROPS) && (UV)HvKEYS(hv) > n->max_props) JSF_FAIL("maxProperties"); 4 0 4
483 1174 ▶ if (n->present & JSF_HAS_REQUIRED) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_REQUIRED) { 394 780 1174
484 394 jsf_reqset *rs = (jsf_reqset *)jsf_arena_ptr(a, n->required_off);
485 394 jsf_reqent *re = (jsf_reqent *)((char *)rs + sizeof(jsf_reqset));
486 uint32_t r;
487 660 ▶ for (r = 0; r < rs->n; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < rs->n; r++) { 412 248 660
488 412 uint32_t nl; const char *nm = jsf_str_bytes(a, re[r].name_off, &nl);
489 412 ▶ if (!hv_exists(hv, nm, (I32)nl)) JSF_FAIL("required");
Branch: 3 branches
BranchTrueFalseTotal
if (!hv_exists(hv, nm, (I32)nl)) JSF_FAIL("required"); 151 261 412
if (!hv_exists(hv, nm, (I32)nl)) JSF_FAIL("required"); 5 146 151
if (!hv_exists(hv, nm, (I32)nl)) JSF_FAIL("required"); 146 5 151
490 }
491 }
492 1028 ▶ if (n->present & JSF_HAS_DEPREQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_DEPREQ) { 19 1009 1028
493 19 jsf_deptab *dt = (jsf_deptab *)jsf_arena_ptr(a, n->depreq_off);
494 19 jsf_depent *de = (jsf_depent *)((char *)dt + sizeof(jsf_deptab));
495 uint32_t di;
496 34 ▶ for (di = 0; di < dt->n; di++) {
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < dt->n; di++) { 22 12 34
497 22 uint32_t nl; const char *nm = jsf_str_bytes(a, de[di].name_off, &nl);
498 22 ▶ if (hv_exists(hv, nm, (I32)nl) && de[di].names_off) {
Branch: 2 branches
BranchTrueFalseTotal
if (hv_exists(hv, nm, (I32)nl) && de[di].names_off) { 13 9 22
if (hv_exists(hv, nm, (I32)nl) && de[di].names_off) { 13 0 13
499 13 jsf_offlist *ol = (jsf_offlist *)jsf_arena_ptr(a, de[di].names_off);
500 13 uint32_t *offs = (uint32_t *)((char *)ol + sizeof(jsf_offlist)); uint32_t z;
501 20 ▶ for (z = 0; z < ol->n; z++) {
Branch: 1 branch
BranchTrueFalseTotal
for (z = 0; z < ol->n; z++) { 14 6 20
502 14 uint32_t rl; const char *rn = jsf_str_bytes(a, offs[z], &rl);
503 14 ▶ if (!hv_exists(hv, rn, (I32)rl)) JSF_FAIL("dependentRequired");
Branch: 3 branches
BranchTrueFalseTotal
if (!hv_exists(hv, rn, (I32)rl)) JSF_FAIL("dependentRequired"); 7 7 14
if (!hv_exists(hv, rn, (I32)rl)) JSF_FAIL("dependentRequired"); 0 7 7
if (!hv_exists(hv, rn, (I32)rl)) JSF_FAIL("dependentRequired"); 7 0 7
504 }
505 }
506 }
507 }
508 1021 ▶ if (n->present & JSF_HAS_DEPSCHEMAS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_DEPSCHEMAS) { 25 996 1021
509 25 jsf_dstab *dt = (jsf_dstab *)jsf_arena_ptr(a, n->depsch_off);
510 25 jsf_dsent *de = (jsf_dsent *)((char *)dt + sizeof(jsf_dstab));
511 uint32_t di;
512 47 ▶ for (di = 0; di < dt->n; di++) {
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < dt->n; di++) { 33 14 47
513 33 uint32_t nl; const char *nm = jsf_str_bytes(a, de[di].name_off, &nl);
514 51 if (hv_exists(hv, nm, (I32)nl) &&
515 29 ▶ !jsf_validate(aTHX_ C, de[di].sch_off, data, ctx)) { ok = 0; if (!ctx->collect) goto done; }
Branch: 1 branch
BranchTrueFalseTotal
!jsf_validate(aTHX_ C, de[di].sch_off, data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 11 0 11
516 }
517 }
518 }
519  
520 2589 ▶ if (n->present & JSF_HAS_ALLOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_ALLOF) { 107 2482 2589
521 107 jsf_offlist *ol = (jsf_offlist *)jsf_arena_ptr(a, n->allof_off);
522 107 uint32_t *offs = (uint32_t *)((char *)ol + sizeof(jsf_offlist)); uint32_t i;
523 242 ▶ for (i = 0; i < ol->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ol->n; i++) 180 62 242
524 180 ▶ if (!jsf_validate(aTHX_ C, offs[i], data, ctx)) { ok = 0; if (!ctx->collect) goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!jsf_validate(aTHX_ C, offs[i], data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 45 135 180
if (!jsf_validate(aTHX_ C, offs[i], data, ctx)) { ok = 0; if (!ctx->collect) goto done; } 45 0 45
525 }
526 /* anyOf: probe EVERY branch (no break); each successful branch merges its
527 * evaluated set into this frame's set (jsf_probe_ann's frame merges into
528 * ctx->ann on success). Collecting from all matches is required by spec. */
529 2544 ▶ if (n->present & JSF_HAS_ANYOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_ANYOF) { 50 2494 2544
530 50 jsf_offlist *ol = (jsf_offlist *)jsf_arena_ptr(a, n->anyof_off);
531 50 uint32_t *offs = (uint32_t *)((char *)ol + sizeof(jsf_offlist)); uint32_t i; int any = 0;
532 140 ▶ for (i = 0; i < ol->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ol->n; i++) 90 50 140
533 90 ▶ if (jsf_probe_ann(aTHX_ C, offs[i], data, ctx, ctx->ann)) any = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (jsf_probe_ann(aTHX_ C, offs[i], data, ctx, ctx->ann)) any = 1; 49 41 90
534 50 ▶ if (!any) JSF_FAIL("anyOf");
Branch: 3 branches
BranchTrueFalseTotal
if (!any) JSF_FAIL("anyOf"); 13 37 50
if (!any) JSF_FAIL("anyOf"); 0 13 13
if (!any) JSF_FAIL("anyOf"); 13 0 13
535 }
536 2531 ▶ if (n->present & JSF_HAS_ONEOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_ONEOF) { 109 2422 2531
537 109 jsf_offlist *ol = (jsf_offlist *)jsf_arena_ptr(a, n->oneof_off);
538 109 uint32_t *offs = (uint32_t *)((char *)ol + sizeof(jsf_offlist)); uint32_t i, c2 = 0;
539 367 ▶ for (i = 0; i < ol->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ol->n; i++) 258 109 367
540 258 ▶ if (jsf_probe_ann(aTHX_ C, offs[i], data, ctx, ctx->ann)) c2++;
Branch: 1 branch
BranchTrueFalseTotal
if (jsf_probe_ann(aTHX_ C, offs[i], data, ctx, ctx->ann)) c2++; 99 159 258
541 109 ▶ if (c2 != 1) JSF_FAIL("oneOf");
Branch: 3 branches
BranchTrueFalseTotal
if (c2 != 1) JSF_FAIL("oneOf"); 55 54 109
if (c2 != 1) JSF_FAIL("oneOf"); 0 55 55
if (c2 != 1) JSF_FAIL("oneOf"); 55 0 55
542 }
543 2476 ▶ if (n->present & JSF_HAS_NOT) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_NOT) { 46 2430 2476
544 /* `not` discards its own annotations, but its internal unevaluated* must
545 * still apply - jsf_probe gives it its own frame with no sink. */
546 46 ▶ if (jsf_probe(aTHX_ C, n->not_off, data, ctx)) JSF_FAIL("not");
Branch: 3 branches
BranchTrueFalseTotal
if (jsf_probe(aTHX_ C, n->not_off, data, ctx)) JSF_FAIL("not"); 28 18 46
if (jsf_probe(aTHX_ C, n->not_off, data, ctx)) JSF_FAIL("not"); 0 28 28
if (jsf_probe(aTHX_ C, n->not_off, data, ctx)) JSF_FAIL("not"); 28 0 28
547 }
548 2448 ▶ if (n->present & JSF_HAS_IF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->present & JSF_HAS_IF) { 73 2375 2448
549 73 int branchok = 1;
550 73 ▶ if (jsf_probe_ann(aTHX_ C, n->if_off, data, ctx, ctx->ann)) {
Branch: 1 branch
BranchTrueFalseTotal
if (jsf_probe_ann(aTHX_ C, n->if_off, data, ctx, ctx->ann)) { 39 34 73
551 39 ▶ if (n->then_off) branchok = jsf_validate(aTHX_ C, n->then_off, data, ctx);
Branch: 1 branch
BranchTrueFalseTotal
if (n->then_off) branchok = jsf_validate(aTHX_ C, n->then_off, data, ctx); 28 11 39
552 } else {
553 34 ▶ if (n->else_off) branchok = jsf_validate(aTHX_ C, n->else_off, data, ctx);
Branch: 1 branch
BranchTrueFalseTotal
if (n->else_off) branchok = jsf_validate(aTHX_ C, n->else_off, data, ctx); 20 14 34
554 }
555 73 ▶ if (!branchok) { ok = 0; if (!ctx->collect) goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!branchok) { ok = 0; if (!ctx->collect) goto done; } 16 57 73
if (!branchok) { ok = 0; if (!ctx->collect) goto done; } 16 0 16
556 }
557  
558 /* unevaluated*: after every other keyword and in-place applicator, apply to
559 * any property / item not in this instance's evaluated set. */
560 2432 ▶ if ((n->present & JSF_HAS_UNEVALPROPS) && (t & JSF_T_OBJECT)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_UNEVALPROPS) && (t & JSF_T_OBJECT)) { 145 2287 2432
if ((n->present & JSF_HAS_UNEVALPROPS) && (t & JSF_T_OBJECT)) { 139 6 145
561 139 HV *hv = (HV *)SvRV(data); HE *he;
562 139 hv_iterinit(hv);
563 271 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 183 88 271
564 183 ▶ STRLEN kl; char *k = HePV(he, kl); SV *val = HeVAL(he);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; char *k = HePV(he, kl); SV *val = HeVAL(he); 0 183 183
565 183 ▶ if (ctx->ann && hv_exists(ctx->ann->props, k, kl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->ann && hv_exists(ctx->ann->props, k, kl)) continue; 183 0 183
if (ctx->ann && hv_exists(ctx->ann->props, k, kl)) continue; 112 71 183
566 71 jsf_annot_add_key(aTHX_ ctx->ann, k, kl);
567 71 ▶ JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl));
Branch: 6 branches
BranchTrueFalseTotal
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 71 71
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 71 0 71
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 71 0 71
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 0 71 71
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 51 20 71
JSF_CHILD(n->unevalprops_off, val, jsf_iloc_push_key(aTHX_ ctx->iloc, k, kl)); 51 0 51
568 }
569 }
570 2381 ▶ if ((n->present & JSF_HAS_UNEVALITEMS) && (t & JSF_T_ARRAY)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((n->present & JSF_HAS_UNEVALITEMS) && (t & JSF_T_ARRAY)) { 2308 73 2381
if ((n->present & JSF_HAS_UNEVALITEMS) && (t & JSF_T_ARRAY)) { 6 67 73
571 67 AV *av = (AV *)SvRV(data); SSize_t i, cnt = av_len(av) + 1;
572 176 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 137 39 176
573 SV **el;
574 137 ▶ if (jsf_annot_has_idx(aTHX_ ctx->ann, i)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (jsf_annot_has_idx(aTHX_ ctx->ann, i)) continue; 97 40 137
575 40 el = av_fetch(av, i, 0);
576 40 jsf_annot_add_idx(aTHX_ ctx->ann, i);
577 40 ▶ if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i));
Branch: 7 branches
BranchTrueFalseTotal
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 40 0 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 0 40 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 40 0 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 40 0 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 0 40 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 28 12 40
if (el) JSF_CHILD(n->unevalitems_off, *el, jsf_iloc_push_idx(aTHX_ ctx->iloc, i)); 28 0 28
578 }
579 }
580  
581 2353 done:
582 3665 ▶ if (filled) SvREFCNT_dec((SV *)filled);
Branch: 1 branch
BranchTrueFalseTotal
if (filled) SvREFCNT_dec((SV *)filled); 0 3665 3665
583 3665 ▶ if (pushed_dyn) { SV *x = av_pop(ctx->dynscope); if (x) SvREFCNT_dec(x); }
Branch: 2 branches
BranchTrueFalseTotal
if (pushed_dyn) { SV *x = av_pop(ctx->dynscope); if (x) SvREFCNT_dec(x); } 181 3484 3665
if (pushed_dyn) { SV *x = av_pop(ctx->dynscope); if (x) SvREFCNT_dec(x); } 181 0 181
584 3665 ctx->ann = sink; /* restore caller's set */
585 3665 ▶ if (ok && sink && local) jsf_annot_merge(aTHX_ sink, local);
Branch: 3 branches
BranchTrueFalseTotal
if (ok && sink && local) jsf_annot_merge(aTHX_ sink, local); 2333 1332 3665
if (ok && sink && local) jsf_annot_merge(aTHX_ sink, local); 425 1908 2333
if (ok && sink && local) jsf_annot_merge(aTHX_ sink, local); 425 0 425
586 3665 ▶ if (local) jsf_annot_free(aTHX_ local);
Branch: 1 branch
BranchTrueFalseTotal
if (local) jsf_annot_free(aTHX_ local); 962 2703 3665
587 3665 ctx->depth--;
588 3665 return ok;
589 }
590  
591 #undef JSF_FAIL
592 #undef JSF_CHILD
593  
594 1477 static int jsf_run(pTHX_ jsf_compiled_t *C, SV *data, AV *errors) {
595 jsf_ctx_t ctx;
596 1477 ctx.errors = errors;
597 1477 ctx.collect = errors ? 1 : 0;
598 1477 ctx.depth = 0;
599 1477 ▶ ctx.iloc = errors ? sv_2mortal(newSVpvs("")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ctx.iloc = errors ? sv_2mortal(newSVpvs("")) : NULL; 15 1462 1477
600 1477 ctx.ann = NULL; /* the root frame creates its own collector */
601 1477 ▶ ctx.dynscope = C->has_dynamic ? (AV *)sv_2mortal((SV *)newAV()) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ctx.dynscope = C->has_dynamic ? (AV *)sv_2mortal((SV *)newAV()) : NULL; 53 1424 1477
602 1477 return jsf_validate(aTHX_ C, C->root, data, &ctx);
603 }
604  
605 1462 static int jsf_is_valid(pTHX_ jsf_compiled_t *C, SV *data) {
606 1462 return jsf_run(aTHX_ C, data, NULL);
607 }
608  
609 #endif /* JSF_INTERP_H */
« include/jsf_compiled.h ↑ Summary include/jsf_arena.h »