include/jsf_compiled.h

filter: Statementstmt 100% 30 / 30 Branchbran 56.2% 9 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 39 / 46 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/jsf_types.h ↑ Summary include/jsf_interp.h »
1 #ifndef JSF_COMPILED_H
2 #define JSF_COMPILED_H
3  
4 /* The top-level compiled object: the arena IR, the root node offset, and a
5 * keepalive AV holding deep copies of const/enum value SVs. Complex JSON values
6 * (enum arrays, const objects) are copied into `keep` rather than re-encoded
7 * into the arena - the schema hashref can still be GC'd afterwards (principle 2
8 * for the schema document; the specific literal values live on in `keep`).
9 *
10 * A JSON::Schema::Fast::Compiled is a blessed IV pointing at this struct.
11 * Needs jsf_arena.h and the Perl API (perl.h, included by the .xs). */
12  
13 /* lazily-compiled `pattern` regex, cached by the interned pattern offset so a
14 * schema's pattern is compiled at most once (not per validate). */
15 typedef struct jsf_rxent { uint32_t pat_off; REGEXP *rx; } jsf_rxent;
16  
17 typedef struct jsf_compiled {
18 jsf_arena_t *arena;
19 uint32_t root; /* root node offset */
20 AV *keep; /* Perl-owned const/enum values */
21 int unsupported; /* count of out-of-matrix keywords seen */
22 jsf_rxent *rx; /* compiled-pattern cache */
23 int rxn, rxcap;
24 int coerce; /* string<->number/boolean coercion (opt-in) */
25 int apply_defaults; /* fill missing props from `default` (opt-in) */
26 int has_unevaluated;/* schema uses unevaluatedProperties/Items */
27 int has_dynamic; /* schema uses $dynamicRef/$dynamicAnchor */
28 int no_validation_vocab; /* $schema metaschema disables validation vocab */
29 HV *dynmap; /* "base#anchor" -> IV node offset (dynamic anchors) */
30 SV *schema_sv; /* keepalive of the (decoded) schema, for schema() */
31 } jsf_compiled_t;
32  
33 480 static jsf_compiled_t *jsf_compiled_new(pTHX) {
34 480 jsf_compiled_t *c = (jsf_compiled_t *)malloc(sizeof *c);
35 480 ▶ if (!c) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return NULL; 0 480 480
36 480 c->arena = jsf_arena_new(1024);
37 480 ▶ if (!c->arena) { free(c); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c->arena) { free(c); return NULL; } 0 480 480
38 480 c->root = JSF_NULL_OFF;
39 480 c->keep = newAV();
40 480 c->unsupported = 0;
41 480 c->rx = NULL;
42 480 c->rxn = c->rxcap = 0;
43 480 c->coerce = 0;
44 480 c->apply_defaults = 0;
45 480 c->has_unevaluated = 0;
46 480 c->has_dynamic = 0;
47 480 c->no_validation_vocab = 0;
48 480 c->dynmap = newHV();
49 480 c->schema_sv = NULL;
50 480 return c;
51 }
52  
53 478 static void jsf_compiled_free(pTHX_ jsf_compiled_t *c) {
54 int i;
55 478 ▶ if (!c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return; 0 478 478
56 502 ▶ for (i = 0; i < c->rxn; i++) if (c->rx[i].rx) ReREFCNT_dec(c->rx[i].rx);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < c->rxn; i++) if (c->rx[i].rx) ReREFCNT_dec(c->rx[i].rx); 24 0 24
for (i = 0; i < c->rxn; i++) if (c->rx[i].rx) ReREFCNT_dec(c->rx[i].rx); 24 478 502
57 478 free(c->rx);
58 478 jsf_arena_free(c->arena);
59 478 ▶ if (c->keep) SvREFCNT_dec((SV *)c->keep);
Branch: 1 branch
BranchTrueFalseTotal
if (c->keep) SvREFCNT_dec((SV *)c->keep); 478 0 478
60 478 ▶ if (c->dynmap) SvREFCNT_dec((SV *)c->dynmap);
Branch: 1 branch
BranchTrueFalseTotal
if (c->dynmap) SvREFCNT_dec((SV *)c->dynmap); 478 0 478
61 478 ▶ if (c->schema_sv) SvREFCNT_dec(c->schema_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->schema_sv) SvREFCNT_dec(c->schema_sv); 478 0 478
62 478 free(c);
63 }
64  
65 /* Deep-copy a value SV into the keepalive; returns its index. */
66 162 static uint32_t jsf_keep_push(pTHX_ jsf_compiled_t *c, SV *v) {
67 162 av_push(c->keep, newSVsv(v));
68 162 return (uint32_t)av_len(c->keep); /* index of the element just pushed */
69 }
70  
71 #endif /* JSF_COMPILED_H */
« include/jsf_types.h ↑ Summary include/jsf_interp.h »