yeptris 0.6.59.1-arm-linux-eabihf → 0.6.60.1-arm-linux-eabihf

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/ext/yeptris_native/version.h +2 -2
  3. data/lib/yeptris/3.4/native.so +0 -0
  4. data/lib/yeptris/ffi.rb +19 -0
  5. data/lib/yeptris/yaml.rb +61 -0
  6. data/lib/yeptris.rb +1 -1
  7. data/libyeptris.so +0 -0
  8. data/vendor/libyeptris/CMakeLists.txt +1 -1
  9. data/vendor/libyeptris/src/CMakeLists.txt +7 -0
  10. data/vendor/libyeptris/src/include/yeptris/emit.h +30 -2
  11. data/vendor/libyeptris/src/include/yeptris/parse.h +52 -0
  12. data/vendor/libyeptris/src/include/yeptris/resolve.h +8 -0
  13. data/vendor/libyeptris/src/include/yeptris/validate.h +222 -21
  14. data/vendor/libyeptris/src/include/yeptris/values.h +8 -2
  15. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +10 -0
  16. data/vendor/libyeptris/src/yeptris/cbor/decode.c +5 -7
  17. data/vendor/libyeptris/src/yeptris/cbor/encode.c +9 -9
  18. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +1 -1
  19. data/vendor/libyeptris/src/yeptris/common/error.h +2 -0
  20. data/vendor/libyeptris/src/yeptris/doc.h +20 -0
  21. data/vendor/libyeptris/src/yeptris/emit/emit.c +85 -13
  22. data/vendor/libyeptris/src/yeptris/emit/writer.c +158 -642
  23. data/vendor/libyeptris/src/yeptris/emit/writer.h +15 -1
  24. data/vendor/libyeptris/src/yeptris/events/push.c +28 -3
  25. data/vendor/libyeptris/src/yeptris/events/values.c +24 -0
  26. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  27. data/vendor/libyeptris/src/yeptris/parse/engine.c +121 -40
  28. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +326 -0
  29. data/vendor/libyeptris/src/yeptris/parse.c +173 -43
  30. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +1 -1
  31. data/vendor/libyeptris/src/yeptris/resolve/core12.c +11 -3
  32. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  33. data/vendor/libyeptris/src/yeptris/resolve/json12.c +12 -6
  34. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +20 -0
  35. data/vendor/libyeptris/src/yeptris/validate/compile.c +39 -3
  36. data/vendor/libyeptris/src/yeptris/validate/internal.h +223 -7
  37. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +645 -115
  38. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  39. data/vendor/libyeptris/src/yeptris/validate/regexp.c +623 -157
  40. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  41. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  42. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  43. data/vendor/libyeptris/src/yeptris/validate/validate.c +288 -145
  44. metadata +8 -1
@@ -1,5 +1,5 @@
1
1
  /* jsonschema.c — the JSON Schema draft 2020-12 keyword subset
2
- * (#882 slice 2; #885 item 2 sweep).
2
+ * (#882 slice 2; #885 item 2 sweep; the 2020-12 sota sweep).
3
3
  *
4
4
  * The schema and the target are both parsed with the same engine; the
5
5
  * walk applies the schema tree to the instance tree and emits
@@ -14,23 +14,62 @@
14
14
  * patternProperties matches), dependentRequired, dependentSchemas,
15
15
  * if/then/else (the if-schema rides a suppressed probe), items
16
16
  * (+ prefixItems; the draft-07 array form rides as prefixItems),
17
- * allOf/anyOf/oneOf/not, $ref (LOCAL "#/..." pointers — $defs and
18
- * definitions both resolve), boolean schemas. Unknown keywords, and
19
- * known keywords whose values cannot be read, are IGNORED (2020-12's
20
- * posture) — an unreadable constraint never guesses. Carried out of
21
- * the subset (documented in test/conformance/jsonschema/MATRIX.md):
22
- * unevaluatedItems/unevaluatedProperties (annotation plumbing),
23
- * $anchor/$id/$dynamicRef (pointer/base-URI machinery), remote $ref,
24
- * contentMediaType/contentSchema (annotation-only in 2020-12).
17
+ * allOf/anyOf/oneOf/not, unevaluatedProperties/unevaluatedItems (the
18
+ * annotation model, below), $ref, $dynamicRef (below), boolean
19
+ * schemas. Unknown keywords, and known keywords whose values cannot
20
+ * be read, are IGNORED (2020-12's posture) — an unreadable constraint
21
+ * never guesses.
22
+ *
23
+ * THE ANNOTATION MODEL (2020-12's unevaluated*): every application of
24
+ * a schema object to an instance location returns (a) its validity —
25
+ * one fail_mark comparison around the application, unsuppressed emits
26
+ * increment the mark — and (b) the properties/items it EVALUATED.
27
+ * Annotations collect upward only: a schema object's set is its own
28
+ * adjacent keywords' appends (properties, patternProperties,
29
+ * additionalProperties, contains, items, prefixItems,
30
+ * unevaluatedProperties/unevaluatedItems themselves) plus whatever its
31
+ * in-place applicators brought back (allOf branches always;
32
+ * anyOf/oneOf SUCCESSFUL branches only — a failed branch's annotations
33
+ * are released; the if-probe when it holds, plus the branch taken;
34
+ * $ref/$dynamicRef targets; dependentSchemas when triggered). `not`
35
+ * and the probes never annotate outward. Applications to CHILD
36
+ * locations (properties' values, items, propertyNames' keys, the
37
+ * unevaluated* subschemas) release their sets — an annotation never
38
+ * crosses an instance-location boundary. unevaluatedProperties /
39
+ * unevaluatedItems run LAST in their schema object, whatever the
40
+ * document order, and flag the properties/items absent from the
41
+ * collected set.
42
+ *
43
+ * REFERENCES (the sota sweep): resolution rides the compiled schema's
44
+ * identifier map (resources.c): $id base-URI composition (RFC 3986
45
+ * path merge, fragment-free canonical forms), $anchor and
46
+ * $dynamicAnchor plain-name fragments scoped to their nearest $id
47
+ * resource, JSON Pointer fragments with the percent-encoding layer
48
+ * (%25 -> '%', then ~0/~1), and $ref targets anywhere in the schema's
49
+ * own identifier space. No network: a target outside that space is
50
+ * unresolvable (DIAG_REF), never fetched. $dynamicRef rides the
51
+ * draft's runtime dynamic scope (core spec 8.2.3.2 + appendix C): a
52
+ * plain-name fragment that statically resolves to a $dynamicAnchor is
53
+ * replaced by the identically-named $dynamicAnchor of the OUTERMOST
54
+ * schema resource in the dynamic scope — the stack of resources
55
+ * entered during evaluation; probes (anyOf/oneOf/not/if trials) enter
56
+ * no scope, exactly as their annotations are discarded.
57
+ *
58
+ * Carried out of the subset (documented in
59
+ * test/conformance/jsonschema/MATRIX.md): remote targets (the
60
+ * metaschema and the suite's HTTP fixtures — no network corpus is
61
+ * vendored), contentMediaType/contentSchema (annotation-only in
62
+ * 2020-12).
25
63
  *
26
64
  * Instance types ride the core-schema resolution (the YAML->JSON data
27
65
  * model): "y" is boolean, 1.0 is integer (2020-12: zero fractional
28
66
  * part), floats and ints are both numbers.
29
67
  *
30
68
  * Guards: the walk depth is the DOM's own parse-bounded depth; a
31
- * consecutive-$ref chain (schema-only cycles that consume no instance
32
- * structure) caps at 64 expansions and reports DIAG_REF. Every array
33
- * is bounded; allocation failure flags the context and unwinds.
69
+ * consecutive-$ref/$dynamicRef chain (schema-only cycles that consume
70
+ * no instance structure) caps at 64 expansions and reports DIAG_REF.
71
+ * Every array is bounded; allocation failure flags the context and
72
+ * unwinds.
34
73
  */
35
74
 
36
75
  #include <stdio.h>
@@ -46,6 +85,18 @@
46
85
  #define JS_MAX_REF_CHAIN 64
47
86
  #define JS_MAX_TOKENS 256
48
87
  #define JS_MAX_UNIQUE 4096 /* uniqueItems' hash table cap; over: O(n^2) probe */
88
+ #define JS_MAX_EVALS 16384 /* the annotation arena's entry cap; over: OOM */
89
+
90
+ /* One application's result: validity plus the annotation window it
91
+ * owns — [pmark, plen) of the evaluated-property key-node arena and
92
+ * [imark, ilen) of the evaluated-item index arena. Absorbing a result
93
+ * into the caller's set is a NO-OP (the window simply stays); dropping
94
+ * it is two assignments (the release). Zero-copy in both directions. */
95
+ typedef struct {
96
+ int valid;
97
+ size_t pmark, plen;
98
+ size_t imark, ilen;
99
+ } js_eval;
49
100
 
50
101
  typedef struct {
51
102
  yep_vctx v; /* instance-side context (path, report, guards) */
@@ -54,6 +105,20 @@ typedef struct {
54
105
  /* the compiled schema's retained pattern programs (NULL: the
55
106
  * one-shot face — every search compiles fresh, identical verdicts) */
56
107
  const yep_re_cache* cache;
108
+ /* the compiled schema's identifier map (NULL: only bare "#/..."
109
+ * pointers against the document root resolve) */
110
+ const js_resources* res;
111
+ /* the dynamic scope: resource indices, outermost first — pushed
112
+ * as evaluation enters each schema object's resource (dedup'd
113
+ * against the top), popped on exit; probes push nothing */
114
+ uint32_t scope[YEP_JS_MAX_SCOPE];
115
+ int scope_n;
116
+ /* the annotation arenas: evaluated property KEY node ids and item
117
+ * indices, appended during the walk, released per application */
118
+ uint32_t* ep;
119
+ size_t ep_len, ep_cap;
120
+ uint32_t* ei;
121
+ size_t ei_len, ei_cap;
57
122
  } js_ctx;
58
123
 
59
124
  /* ---- schema-side facts -------------------------------------------------- */
@@ -260,51 +325,165 @@ static uint32_t inst_find_key(const yep_dom* d, uint32_t map_id, const char* nam
260
325
  return UINT32_MAX;
261
326
  }
262
327
 
328
+ /* ---- the annotation arenas ---------------------------------------------- */
329
+
330
+ /* Appends an evaluated property key node; -1 over cap (the walk's OOM
331
+ * flag fires — bounded memory is the law, never a silent guess). */
332
+ static int ep_append(js_ctx* j, uint32_t key_node) {
333
+ if (j->ep_len == j->ep_cap) {
334
+ const yep_allocator* sys = yep_system_allocator();
335
+ size_t ncap = j->ep_cap ? j->ep_cap * 2 : 64;
336
+ uint32_t* np = yep_alloc(sys, ncap * sizeof(uint32_t));
337
+ if (np == NULL) {
338
+ j->v.oom = 1;
339
+ return -1;
340
+ }
341
+ if (j->ep != NULL) {
342
+ memcpy(np, j->ep, j->ep_len * sizeof(uint32_t));
343
+ yep_free(sys, j->ep);
344
+ }
345
+ j->ep = np;
346
+ j->ep_cap = ncap;
347
+ }
348
+ if (j->ep_len >= JS_MAX_EVALS) {
349
+ j->v.oom = 1;
350
+ return -1;
351
+ }
352
+ j->ep[j->ep_len++] = key_node;
353
+ return 0;
354
+ }
355
+
356
+ static int ei_append(js_ctx* j, uint32_t index) {
357
+ if (j->ei_len == j->ei_cap) {
358
+ const yep_allocator* sys = yep_system_allocator();
359
+ size_t ncap = j->ei_cap ? j->ei_cap * 2 : 64;
360
+ uint32_t* np = yep_alloc(sys, ncap * sizeof(uint32_t));
361
+ if (np == NULL) {
362
+ j->v.oom = 1;
363
+ return -1;
364
+ }
365
+ if (j->ei != NULL) {
366
+ memcpy(np, j->ei, j->ei_len * sizeof(uint32_t));
367
+ yep_free(sys, j->ei);
368
+ }
369
+ j->ei = np;
370
+ j->ei_cap = ncap;
371
+ }
372
+ if (j->ei_len >= JS_MAX_EVALS) {
373
+ j->v.oom = 1;
374
+ return -1;
375
+ }
376
+ j->ei[j->ei_len++] = index;
377
+ return 0;
378
+ }
379
+
380
+ /* 1 when key_node's name is in the window [pmark, ep_len). */
381
+ static int eval_has_prop(const js_ctx* j, size_t pmark, uint32_t key_node) {
382
+ const yep_dnode* kn = yep_dom_node(j->v.dom, key_node);
383
+ if (kn == NULL) {
384
+ return 0;
385
+ }
386
+ yep_view kv = yep_dom_view(j->v.dom, kn->value);
387
+ for (size_t i = pmark; i < j->ep_len; i++) {
388
+ const yep_dnode* en = yep_dom_node(j->v.dom, j->ep[i]);
389
+ if (en == NULL) {
390
+ continue;
391
+ }
392
+ yep_view ev = yep_dom_view(j->v.dom, en->value);
393
+ if (ev.len == kv.len && (kv.len == 0 || memcmp(ev.p, kv.p, kv.len) == 0)) {
394
+ return 1;
395
+ }
396
+ }
397
+ return 0;
398
+ }
399
+
400
+ static int eval_has_item(const js_ctx* j, size_t imark, uint32_t index) {
401
+ for (size_t i = imark; i < j->ei_len; i++) {
402
+ if (j->ei[i] == index) {
403
+ return 1;
404
+ }
405
+ }
406
+ return 0;
407
+ }
408
+
263
409
  /* ---- the walk ----------------------------------------------------------- */
264
410
 
265
- static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth);
411
+ static js_eval js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth);
412
+ static void js_apply_body(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth, size_t pmark,
413
+ size_t imark);
266
414
 
267
415
  /* the object-keyword walkers (defined after js_apply) */
268
416
  static void check_pattern_properties(js_ctx* j, uint32_t pp_id, uint32_t inst_id, int depth);
269
417
  static void check_property_names(js_ctx* j, uint32_t pn_id, uint32_t inst_id, int depth);
270
418
  static void check_dependent_required(js_ctx* j, uint32_t dr_id, uint32_t inst_id, int depth);
271
419
  static void check_dependent_schemas(js_ctx* j, uint32_t ds_id, uint32_t inst_id, int depth);
420
+ static void check_unevaluated_properties(js_ctx* j, uint32_t upv, int upf, uint32_t inst_id,
421
+ int depth, size_t pmark);
422
+ static void check_unevaluated_items(js_ctx* j, uint32_t uv, int uf, uint32_t inst_id, int depth,
423
+ size_t imark);
424
+
425
+ /* Applies a schema to a CHILD instance location: the child's own
426
+ * annotations stay at the child (released here) — an annotation never
427
+ * crosses an instance-location boundary. */
428
+ static void js_apply_child(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
429
+ size_t pm = j->ep_len, im = j->ei_len;
430
+ js_apply(j, schema_id, inst_id, depth);
431
+ j->ep_len = pm;
432
+ j->ei_len = im;
433
+ }
272
434
 
273
435
  /* A suppressed probe: does `schema_id` hold at `inst_id`? Diagnostics
274
- * during the trial are swallowed. */
275
- static int js_holds(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
276
- int save = j->v.trial_hit;
277
- j->v.trial_hit = 0;
436
+ * during the trial are swallowed (and the trial enters no dynamic
437
+ * scope — its anchors are as discarded as its annotations). The
438
+ * probe's validity is its OWN trial_fail delta — a suppressed emit
439
+ * never touches the enclosing application's fail_mark. The CALLER
440
+ * decides the window's fate: absorbed when the probe's annotations
441
+ * feed the parent (a held if / a matching anyOf branch), released
442
+ * otherwise. */
443
+ static js_eval js_probe(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
444
+ int tf = j->v.trial_fail;
278
445
  j->v.suppress++;
279
- js_apply(j, schema_id, inst_id, depth);
446
+ js_eval r = js_apply(j, schema_id, inst_id, depth);
280
447
  j->v.suppress--;
281
- int hit = j->v.trial_hit;
282
- j->v.trial_hit = save;
283
- return !hit;
448
+ /* the extent's own delta only: nested probes restored their share,
449
+ * so what remains is what THIS trial swallowed directly */
450
+ int my = j->v.trial_fail - tf;
451
+ j->v.trial_fail = tf;
452
+ if (my != 0) {
453
+ r.valid = 0;
454
+ }
455
+ return r;
284
456
  }
285
457
 
286
- /* Local "#/..." pointer resolution against the schema document. */
287
- static int resolve_ref(js_ctx* j, const char* p, uint32_t plen, uint32_t* out) {
288
- if (plen == 0 || p[0] != '#') {
289
- return 0; /* remote/anchor forms are out of the subset */
290
- }
291
- uint32_t cur = j->sroot;
292
- size_t i = 1;
293
- if (i >= plen || p[i] != '/') {
294
- *out = cur; /* "#" alone is the root */
295
- return i == plen;
296
- }
458
+ /* ---- reference resolution ------------------------------------------------ */
459
+
460
+ /* JSON Pointer walk from `start` with the URI percent layer decoded
461
+ * before the ~0/~1 escapes (%25 -> '%', then the RFC 6901 unescape).
462
+ * `p` spans the raw fragment text ("/$defs/foo"); the loop consumes a
463
+ * '/' before each token. */
464
+ static int pointer_walk(js_ctx* j, uint32_t start, const char* p, size_t plen, uint32_t* out) {
465
+ uint32_t cur = start;
466
+ size_t i = 0;
297
467
  int tokens = 0;
298
468
  while (i < plen) {
299
469
  if (p[i] != '/') {
300
470
  return 0;
301
471
  }
302
472
  i++;
303
- char token[128];
473
+ char token[256];
304
474
  size_t tl = 0;
305
475
  while (i < plen && p[i] != '/') {
306
476
  char b = p[i];
307
- if (b == '~' && i + 1 < plen && (p[i + 1] == '0' || p[i + 1] == '1')) {
477
+ if (b == '%' && i + 2 < plen) {
478
+ int hi = (p[i + 1] >= '0' && p[i + 1] <= '9') ? p[i + 1] - '0'
479
+ : ((p[i + 1] | 0x20) - 'a' + 10);
480
+ int lo = (p[i + 2] >= '0' && p[i + 2] <= '9') ? p[i + 2] - '0'
481
+ : ((p[i + 2] | 0x20) - 'a' + 10);
482
+ if ((unsigned)hi < 16 && (unsigned)lo < 16) {
483
+ b = (char)((hi << 4) | lo);
484
+ i += 2;
485
+ }
486
+ } else if (b == '~' && i + 1 < plen && (p[i + 1] == '0' || p[i + 1] == '1')) {
308
487
  b = p[i + 1] == '0' ? '~' : '/';
309
488
  i++;
310
489
  } else if (b == '~') {
@@ -372,6 +551,125 @@ static int resolve_ref(js_ctx* j, const char* p, uint32_t plen, uint32_t* out) {
372
551
  return cur != UINT32_MAX;
373
552
  }
374
553
 
554
+ /* The plain-name anchor lookup: canonical base URI + '#' + name. When
555
+ * `dynamic` and the static anchor is a $dynamicAnchor, the draft's
556
+ * runtime rule applies (core spec 8.2.3.2): the OUTERMOST schema
557
+ * resource in the dynamic scope defining an identically-named
558
+ * $dynamicAnchor wins; with nothing in scope, the static target
559
+ * stands (the behavior is "$ref-like" — the bookending requirement is
560
+ * the schema author's). */
561
+ static int anchor_lookup(js_ctx* j, const char* base, const char* name, size_t nl, int dynamic,
562
+ uint32_t* out) {
563
+ if (j->res == NULL) {
564
+ return 0;
565
+ }
566
+ char key[YEP_JS_URI_MAX];
567
+ size_t bn = strlen(base);
568
+ if (bn + 1 + nl + 1 > sizeof(key)) {
569
+ return 0;
570
+ }
571
+ memcpy(key, base, bn);
572
+ key[bn] = '#';
573
+ memcpy(key + bn + 1, name, nl);
574
+ key[bn + 1 + nl] = '\0';
575
+ uint32_t node = 0;
576
+ uint8_t dyn = 0;
577
+ if (!js_anchor_find(j->res, key, bn + 1 + nl, &node, &dyn)) {
578
+ return 0;
579
+ }
580
+ if (dynamic && dyn == 1) {
581
+ uint32_t found = UINT32_MAX;
582
+ for (int i = 0; i < j->scope_n && found == UINT32_MAX; i++) {
583
+ uint32_t anode = 0;
584
+ if (js_anchor_dynamic_in_res(j->res, j->scope[i], name, nl, &anode)) {
585
+ found = anode;
586
+ }
587
+ }
588
+ if (found != UINT32_MAX) {
589
+ *out = found;
590
+ return 1;
591
+ }
592
+ }
593
+ *out = node;
594
+ return 1;
595
+ }
596
+
597
+ /* Resolves the URI-reference at `site` (the schema object carrying
598
+ * $ref/$dynamicRef) against the site's compiled base. `dynamic`
599
+ * enables the $dynamicRef runtime scope. 1 + *out, 0 unresolvable. */
600
+ static int resolve_ref_ext(js_ctx* j, uint32_t site, const char* rp, uint32_t rl, int dynamic,
601
+ uint32_t* out) {
602
+ const char* base = "";
603
+ uint32_t base_root = j->sroot;
604
+ if (j->res != NULL) {
605
+ js_refsite_find(j->res, site, &base, &base_root);
606
+ }
607
+ if (rl == 0) {
608
+ *out = base_root; /* "" : the base resource root */
609
+ return 1;
610
+ }
611
+ if (rp[0] == '#') {
612
+ const char* frag = rp + 1;
613
+ size_t fl = rl - 1;
614
+ if (fl == 0) {
615
+ *out = base_root; /* "#" : the base resource root */
616
+ return 1;
617
+ }
618
+ if (frag[0] == '/') {
619
+ return pointer_walk(j, base_root, frag, fl, out);
620
+ }
621
+ return anchor_lookup(j, base, frag, fl, dynamic, out);
622
+ }
623
+ /* a URI-reference with its own (possibly empty) fragment */
624
+ const char* hash = memchr(rp, '#', rl);
625
+ size_t ul = hash != NULL ? (size_t)(hash - rp) : rl;
626
+ char canon[YEP_JS_URI_MAX];
627
+ if (js_uri_resolve(base, rp, ul, canon, sizeof(canon)) != 0) {
628
+ return 0;
629
+ }
630
+ if (hash == NULL) {
631
+ /* no fragment: the resource root itself */
632
+ uint32_t ri = 0;
633
+ if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
634
+ *out = j->res->res[ri].root;
635
+ return 1;
636
+ }
637
+ if (strcmp(canon, base) == 0) {
638
+ *out = base_root;
639
+ return 1;
640
+ }
641
+ return 0; /* outside the schema's identifier space: never fetched */
642
+ }
643
+ const char* frag = hash + 1;
644
+ size_t fl = (size_t)(rp + rl - frag);
645
+ if (frag[0] == '/') {
646
+ uint32_t start = UINT32_MAX;
647
+ uint32_t ri = 0;
648
+ if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
649
+ start = j->res->res[ri].root;
650
+ } else if (strcmp(canon, base) == 0) {
651
+ start = base_root;
652
+ }
653
+ if (start == UINT32_MAX) {
654
+ return 0;
655
+ }
656
+ return pointer_walk(j, start, frag, fl, out);
657
+ }
658
+ if (fl == 0) {
659
+ uint32_t ri = 0;
660
+ if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
661
+ *out = j->res->res[ri].root;
662
+ return 1;
663
+ }
664
+ if (strcmp(canon, base) == 0) {
665
+ *out = base_root;
666
+ return 1;
667
+ }
668
+ return 0;
669
+ }
670
+ return anchor_lookup(j, canon, frag, fl, dynamic, out);
671
+ }
672
+
375
673
  /* uniqueItems: canonical hash buckets, canonical equality inside a
376
674
  * bucket; allocation failure skips the check (oom flags the walk). */
377
675
  static void check_unique(js_ctx* j, uint32_t seq_id, uint32_t inst_off) {
@@ -436,17 +734,18 @@ static void check_unique(js_ctx* j, uint32_t seq_id, uint32_t inst_off) {
436
734
  /* The instance's string length in code points (2020-12 minLength/
437
735
  * maxLength). 0 when the instance is not a string. */
438
736
  static int inst_str_len(const yep_dom* d, uint32_t id, uint32_t* out) {
439
- const yep_dnode* n = yep_dom_node(d, id);
737
+ const yep_dom* dd = d;
738
+ const yep_dnode* n = yep_dom_node(dd, id);
440
739
  if (n == NULL) {
441
740
  return 0;
442
741
  }
443
742
  if (n->kind == YEP_DOM_ALIAS) {
444
- return n->target != UINT32_MAX && inst_str_len(d, n->target, out);
743
+ return n->target != UINT32_MAX && inst_str_len(dd, n->target, out);
445
744
  }
446
745
  if (n->kind != YEP_DOM_SCALAR || yep_validate_scalar_tag(n) != YEPTRIS_TAG_STR) {
447
746
  return 0;
448
747
  }
449
- yep_view v = yep_dom_view(d, n->value);
748
+ yep_view v = yep_dom_view(dd, n->value);
450
749
  uint32_t chars = 0;
451
750
  for (uint32_t i = 0; i < v.len; i++) {
452
751
  chars += ((((unsigned char)v.p[i]) & 0xC0) != 0x80);
@@ -490,7 +789,57 @@ static int schema_word(const js_ctx* j, uint32_t id, const char** p, uint32_t* l
490
789
  return 1;
491
790
  }
492
791
 
493
- static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
792
+ /* Copies a bounded name for diagnostics/detail text: the copied
793
+ * length (the caller pushes paths with it — keys may hold NULs). */
794
+ static size_t name_copy(char* buf, size_t cap, const char* p, size_t len) {
795
+ size_t cl = len < cap - 1 ? len : cap - 1;
796
+ if (cl > 0 && p != NULL) {
797
+ memcpy(buf, p, cl);
798
+ }
799
+ buf[cl] = '\0';
800
+ return cl;
801
+ }
802
+
803
+ /* The dynamic scope: entering a schema object whose resource differs
804
+ * from the current top pushes that resource (outermost first); probes
805
+ * (suppress > 0) enter no scope — their anchors are as discarded as
806
+ * their annotations. 1 pushed, 0 not. */
807
+ static int js_scope_push(js_ctx* j, uint32_t schema_id) {
808
+ if (j->res == NULL || j->v.suppress > 0 || schema_id >= j->res->rindex_n) {
809
+ return 0;
810
+ }
811
+ uint32_t ri = j->res->rindex[schema_id];
812
+ if (ri == UINT32_MAX || j->scope_n >= YEP_JS_MAX_SCOPE) {
813
+ return 0;
814
+ }
815
+ if (j->scope_n > 0 && j->scope[j->scope_n - 1] == ri) {
816
+ return 0;
817
+ }
818
+ j->scope[j->scope_n++] = ri;
819
+ return 1;
820
+ }
821
+
822
+ /* One application: brackets the body with the validity mark and the
823
+ * annotation-window marks, pushes the dynamic scope, and hands the
824
+ * whole window to the caller (absorb = keep, drop = release). */
825
+ static js_eval js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
826
+ int pushed = js_scope_push(j, schema_id);
827
+ int fm = j->v.fail_mark;
828
+ js_eval r;
829
+ r.pmark = j->ep_len;
830
+ r.imark = j->ei_len;
831
+ js_apply_body(j, schema_id, inst_id, depth, r.pmark, r.imark);
832
+ r.valid = (fm == j->v.fail_mark && !j->v.oom);
833
+ r.plen = j->ep_len;
834
+ r.ilen = j->ei_len;
835
+ if (pushed) {
836
+ j->scope_n--;
837
+ }
838
+ return r;
839
+ }
840
+
841
+ static void js_apply_body(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth, size_t pmark,
842
+ size_t imark) {
494
843
  if (j->v.oom) {
495
844
  return;
496
845
  }
@@ -520,32 +869,46 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
520
869
  return; /* true (or anything unreadable) constrains nothing */
521
870
  }
522
871
 
523
- /* $ref composes with its siblings (2020-12); the chain guard kills
524
- * schema-only cycles that consume no instance structure */
525
- uint32_t ref_val;
526
- if (kw_get(j, schema_id, "$ref", &ref_val) && ref_val != UINT32_MAX) {
527
- const char* rp = NULL;
528
- uint32_t rl = 0;
529
- if (schema_word(j, ref_val, &rp, &rl)) {
530
- if (++j->v.ref_depth > JS_MAX_REF_CHAIN) {
531
- yep_validate_emit(&j->v, YEPTRIS_DIAG_REF, yep_validate_node_off(j->v.dom, inst_id),
532
- "$ref chain exceeds %d (a cycle?)", JS_MAX_REF_CHAIN);
533
- j->v.ref_depth = 0;
534
- return;
872
+ /* $ref / $dynamicRef compose with their siblings (2020-12); the
873
+ * chain guard kills schema-only cycles that consume no instance
874
+ * structure. Same-location application: the target's annotations
875
+ * are absorbed into this object's set. */
876
+ {
877
+ int is_dyn = 0;
878
+ uint32_t ref_val = UINT32_MAX;
879
+ if (!kw_get(j, schema_id, "$ref", &ref_val) || ref_val == UINT32_MAX) {
880
+ is_dyn = 1;
881
+ if (!kw_get(j, schema_id, "$dynamicRef", &ref_val) || ref_val == UINT32_MAX) {
882
+ is_dyn = 0;
883
+ ref_val = UINT32_MAX;
535
884
  }
536
- uint32_t target = UINT32_MAX;
537
- if (!resolve_ref(j, rp, rl, &target)) {
538
- yep_validate_emit(&j->v, YEPTRIS_DIAG_REF, yep_validate_node_off(j->v.dom, inst_id),
539
- "$ref \"%.*s\" does not resolve", (int)(rl > 64 ? 64 : rl), rp);
885
+ }
886
+ if (ref_val != UINT32_MAX) {
887
+ const char* rp = NULL;
888
+ uint32_t rl = 0;
889
+ if (schema_word(j, ref_val, &rp, &rl)) {
890
+ if (++j->v.ref_depth > JS_MAX_REF_CHAIN) {
891
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_REF,
892
+ yep_validate_node_off(j->v.dom, inst_id),
893
+ "$ref chain exceeds %d (a cycle?)", JS_MAX_REF_CHAIN);
894
+ j->v.ref_depth = 0;
895
+ return;
896
+ }
897
+ uint32_t target = UINT32_MAX;
898
+ if (!resolve_ref_ext(j, schema_id, rp, rl, is_dyn, &target)) {
899
+ yep_validate_emit(
900
+ &j->v, YEPTRIS_DIAG_REF, yep_validate_node_off(j->v.dom, inst_id),
901
+ "$ref \"%.*s\" does not resolve", (int)(rl > 64 ? 64 : rl), rp);
902
+ j->v.ref_depth = 0;
903
+ return;
904
+ }
905
+ js_apply(j, target, inst_id, depth); /* absorbed */
540
906
  j->v.ref_depth = 0;
541
- return;
542
907
  }
543
- js_apply(j, target, inst_id, depth);
908
+ /* an unreadable $ref value: ignored like any unknown keyword */
909
+ } else {
544
910
  j->v.ref_depth = 0;
545
911
  }
546
- /* an unreadable $ref value: ignored like any unknown keyword */
547
- } else {
548
- j->v.ref_depth = 0;
549
912
  }
550
913
 
551
914
  /* type: a single word or a list of words */
@@ -762,7 +1125,9 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
762
1125
  /* contains: count the items the schema holds on (suppressed
763
1126
  * probes), then the minContains/maxContains qualifiers
764
1127
  * (defaults: at least one; no maximum). min/maxContains
765
- * without contains are annotations — ignored. */
1128
+ * without contains are annotations — ignored. The matching
1129
+ * indices are this object's ANNOTATION — kept when contains
1130
+ * holds, released when it fails. */
766
1131
  uint32_t ctv;
767
1132
  if (kw_get(j, schema_id, "contains", &ctv) && ctv != UINT32_MAX) {
768
1133
  int ctf = 0;
@@ -775,8 +1140,12 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
775
1140
  uint32_t ni = yep_dom_node(j->v.dom, ich)
776
1141
  ? yep_dom_node(j->v.dom, ich)->next_sibling
777
1142
  : UINT32_MAX;
778
- if (js_holds(j, ctv, ich, depth)) {
1143
+ js_eval pr = js_probe(j, ctv, ich, depth);
1144
+ j->ep_len = pr.pmark; /* the item probe annotates nothing outward */
1145
+ j->ei_len = pr.imark;
1146
+ if (pr.valid) {
779
1147
  matches++;
1148
+ ei_append(j, i);
780
1149
  }
781
1150
  ich = ni;
782
1151
  }
@@ -797,12 +1166,14 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
797
1166
  }
798
1167
  }
799
1168
  if ((double)matches < minc) {
1169
+ j->ei_len = imark; /* a failed contains annotates nothing */
800
1170
  yep_validate_emit(
801
1171
  &j->v, have_minc ? YEPTRIS_DIAG_MIN_CONTAINS : YEPTRIS_DIAG_CONTAINS,
802
1172
  yep_validate_node_off(j->v.dom, inst_id),
803
1173
  "%u of %u items match the contains schema (need %g)", matches, cnt,
804
1174
  minc);
805
1175
  } else if (maxc >= 0.0 && (double)matches > maxc) {
1176
+ j->ei_len = imark;
806
1177
  yep_validate_emit(&j->v, YEPTRIS_DIAG_MAX_CONTAINS,
807
1178
  yep_validate_node_off(j->v.dom, inst_id),
808
1179
  "%u of %u items match the contains schema (max %g)",
@@ -827,9 +1198,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
827
1198
  : UINT32_MAX;
828
1199
  size_t mark = j->v.path_len;
829
1200
  if (yep_validate_path_push_index(&j->v, i) == 0) {
830
- js_apply(j, ch, ich, depth);
1201
+ js_apply_child(j, ch, ich, depth);
831
1202
  yep_validate_path_pop(&j->v, mark);
832
1203
  }
1204
+ ei_append(j, i);
833
1205
  prefix++;
834
1206
  i++;
835
1207
  ch = ns;
@@ -854,9 +1226,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
854
1226
  : UINT32_MAX;
855
1227
  size_t mark = j->v.path_len;
856
1228
  if (yep_validate_path_push_index(&j->v, i) == 0) {
857
- js_apply(j, ch, ich, depth);
1229
+ js_apply_child(j, ch, ich, depth);
858
1230
  yep_validate_path_pop(&j->v, mark);
859
1231
  }
1232
+ ei_append(j, i);
860
1233
  i++;
861
1234
  ch = ns;
862
1235
  ich = ni;
@@ -875,9 +1248,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
875
1248
  : UINT32_MAX;
876
1249
  size_t mark = j->v.path_len;
877
1250
  if (yep_validate_path_push_index(&j->v, i) == 0) {
878
- js_apply(j, pv, ich, depth);
1251
+ js_apply_child(j, pv, ich, depth);
879
1252
  yep_validate_path_pop(&j->v, mark);
880
1253
  }
1254
+ ei_append(j, i);
881
1255
  ich = ni;
882
1256
  }
883
1257
  }
@@ -885,7 +1259,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
885
1259
  }
886
1260
  }
887
1261
 
888
- /* allOf / anyOf / oneOf / not */
1262
+ /* allOf / anyOf / oneOf / not: same-location applicators — a
1263
+ * branch's annotations join this object's set when the branch
1264
+ * holds (allOf unconditionally: its verdict is this object's), a
1265
+ * failed probe's window is released, `not` never annotates. */
889
1266
  {
890
1267
  uint32_t cv;
891
1268
  if (kw_get(j, schema_id, "allOf", &cv) && cv != UINT32_MAX) {
@@ -894,7 +1271,7 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
894
1271
  uint32_t ch = cn->first_child;
895
1272
  while (ch != UINT32_MAX) {
896
1273
  uint32_t nc = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
897
- js_apply(j, ch, inst_id, depth);
1274
+ js_apply(j, ch, inst_id, depth); /* absorbed */
898
1275
  ch = nc;
899
1276
  }
900
1277
  }
@@ -907,8 +1284,12 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
907
1284
  uint32_t ch = cn->first_child;
908
1285
  while (ch != UINT32_MAX) {
909
1286
  branches++;
910
- if (!any && js_holds(j, ch, inst_id, depth)) {
911
- any = 1;
1287
+ js_eval br = js_probe(j, ch, inst_id, depth);
1288
+ if (br.valid) {
1289
+ any = 1; /* absorbed: the held branch annotates */
1290
+ } else {
1291
+ j->ep_len = br.pmark;
1292
+ j->ei_len = br.imark;
912
1293
  }
913
1294
  ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
914
1295
  }
@@ -927,12 +1308,18 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
927
1308
  uint32_t ch = cn->first_child;
928
1309
  while (ch != UINT32_MAX) {
929
1310
  branches++;
930
- if (js_holds(j, ch, inst_id, depth)) {
931
- matches++;
1311
+ js_eval br = js_probe(j, ch, inst_id, depth);
1312
+ if (br.valid) {
1313
+ matches++; /* absorbed */
1314
+ } else {
1315
+ j->ep_len = br.pmark;
1316
+ j->ei_len = br.imark;
932
1317
  }
933
1318
  ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
934
1319
  }
935
1320
  if (matches != 1) {
1321
+ j->ep_len = pmark; /* no single branch: no annotations */
1322
+ j->ei_len = imark;
936
1323
  yep_validate_emit(&j->v, YEPTRIS_DIAG_ONE_OF,
937
1324
  yep_validate_node_off(j->v.dom, inst_id),
938
1325
  "%u of the %u oneOf branches matched (exactly one "
@@ -942,7 +1329,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
942
1329
  }
943
1330
  }
944
1331
  if (kw_get(j, schema_id, "not", &cv) && cv != UINT32_MAX) {
945
- if (js_holds(j, cv, inst_id, depth)) {
1332
+ js_eval nr = js_probe(j, cv, inst_id, depth);
1333
+ j->ep_len = nr.pmark; /* the not-schema never annotates outward */
1334
+ j->ei_len = nr.imark;
1335
+ if (nr.valid) {
946
1336
  yep_validate_emit(&j->v, YEPTRIS_DIAG_NOT, yep_validate_node_off(j->v.dom, inst_id),
947
1337
  "the not-schema matched");
948
1338
  }
@@ -950,7 +1340,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
950
1340
  }
951
1341
 
952
1342
  /* if/then/else: the if-schema rides a suppressed probe; exactly one
953
- * of then/else applies. An unreadable if (or then/else) is ignored. */
1343
+ * of then/else applies. A HOLDING if annotates (the probe's window
1344
+ * is absorbed); a failed one is released. then/else are
1345
+ * same-location applications — absorbed. An unreadable if (or
1346
+ * then/else) is ignored. */
954
1347
  {
955
1348
  uint32_t ifv;
956
1349
  if (kw_get(j, schema_id, "if", &ifv) && ifv != UINT32_MAX) {
@@ -960,12 +1353,17 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
960
1353
  int tf = 0, ef = 0;
961
1354
  int have_t = kw_schema(j, schema_id, "then", &tv, &tf);
962
1355
  int have_e = kw_schema(j, schema_id, "else", &ev, &ef);
963
- if (js_holds(j, ifv, inst_id, depth)) {
1356
+ js_eval ir = js_probe(j, ifv, inst_id, depth);
1357
+ if (ir.valid) { /* absorbed: the if's annotations count */
964
1358
  if (have_t) {
965
1359
  js_apply(j, tv, inst_id, depth);
966
1360
  }
967
- } else if (have_e) {
968
- js_apply(j, ev, inst_id, depth);
1361
+ } else {
1362
+ j->ep_len = ir.pmark;
1363
+ j->ei_len = ir.imark;
1364
+ if (have_e) {
1365
+ js_apply(j, ev, inst_id, depth);
1366
+ }
969
1367
  }
970
1368
  }
971
1369
  }
@@ -1024,7 +1422,8 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1024
1422
  }
1025
1423
  }
1026
1424
  }
1027
- /* properties walk (values through their schemas) */
1425
+ /* properties walk (values through their schemas): the
1426
+ * PRESENT keys are this object's annotation */
1028
1427
  if (have_props) {
1029
1428
  const yep_dnode* pn = snode(j, props);
1030
1429
  uint32_t k = pn->first_child;
@@ -1047,9 +1446,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1047
1446
  size_t mark = j->v.path_len;
1048
1447
  if (yep_validate_path_push(&j->v, kv.p != NULL ? kv.p : "",
1049
1448
  kv.len) == 0) {
1050
- js_apply(j, v, cv, depth);
1449
+ js_apply_child(j, v, cv, depth);
1051
1450
  yep_validate_path_pop(&j->v, mark);
1052
1451
  }
1452
+ ep_append(j, ck);
1053
1453
  break;
1054
1454
  }
1055
1455
  }
@@ -1066,7 +1466,8 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1066
1466
  if (pp_id != UINT32_MAX) {
1067
1467
  check_pattern_properties(j, pp_id, inst_id, depth);
1068
1468
  }
1069
- /* propertyNames: the schema applies to each KEY string */
1469
+ /* propertyNames: the schema applies to each KEY string
1470
+ * (a different instance location: annotates nothing here) */
1070
1471
  {
1071
1472
  uint32_t pnv;
1072
1473
  if (kw_get(j, schema_id, "propertyNames", &pnv) && pnv != UINT32_MAX) {
@@ -1104,15 +1505,16 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1104
1505
  }
1105
1506
  }
1106
1507
  /* dependentSchemas: when the named key is present, its
1107
- * schema applies to the whole object (walked directly, like
1108
- * allOf: the inner diagnostics carry the failure) */
1508
+ * schema applies to the whole object (same location: the
1509
+ * inner annotations join this object's set) */
1109
1510
  {
1110
1511
  uint32_t dsv;
1111
1512
  if (kw_get(j, schema_id, "dependentSchemas", &dsv) && dsv != UINT32_MAX) {
1112
1513
  check_dependent_schemas(j, dsv, inst_id, depth);
1113
1514
  }
1114
1515
  }
1115
- /* additionalProperties: the keys properties does not name */
1516
+ /* additionalProperties: the keys properties does not name —
1517
+ * the keys it APPLIED to are this object's annotation */
1116
1518
  uint32_t ap;
1117
1519
  int af = 0;
1118
1520
  if (kw_schema(j, schema_id, "additionalProperties", &ap, &af)) {
@@ -1129,15 +1531,16 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1129
1531
  uint32_t pk = snode(j, props)->first_child;
1130
1532
  while (pk != UINT32_MAX && !known) {
1131
1533
  const yep_dnode* pkn = snode(j, pk);
1132
- uint32_t pv =
1534
+ uint32_t pv2 =
1133
1535
  snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
1134
1536
  if (pkn != NULL && pkn->kind == YEP_DOM_SCALAR) {
1135
1537
  yep_view pw = yep_dom_view(j->sd, pkn->value);
1136
1538
  known = pw.len == kv.len &&
1137
1539
  (kv.len == 0 || memcmp(pw.p, kv.p, kv.len) == 0);
1138
1540
  }
1139
- pk = pv != UINT32_MAX && snode(j, pv) ? snode(j, pv)->next_sibling
1140
- : UINT32_MAX;
1541
+ pk = pv2 != UINT32_MAX && snode(j, pv2)
1542
+ ? snode(j, pv2)->next_sibling
1543
+ : UINT32_MAX;
1141
1544
  }
1142
1545
  }
1143
1546
  /* patternProperties matches take the key out of
@@ -1147,7 +1550,7 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1147
1550
  uint32_t pk = pnm->first_child;
1148
1551
  while (pk != UINT32_MAX && !known) {
1149
1552
  const yep_dnode* pkn = snode(j, pk);
1150
- uint32_t pv =
1553
+ uint32_t pv2 =
1151
1554
  snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
1152
1555
  const char* pat = NULL;
1153
1556
  uint32_t pl = 0;
@@ -1157,8 +1560,9 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1157
1560
  kv.len) == 1) {
1158
1561
  known = 1;
1159
1562
  }
1160
- pk = pv != UINT32_MAX && snode(j, pv) ? snode(j, pv)->next_sibling
1161
- : UINT32_MAX;
1563
+ pk = pv2 != UINT32_MAX && snode(j, pv2)
1564
+ ? snode(j, pv2)->next_sibling
1565
+ : UINT32_MAX;
1162
1566
  }
1163
1567
  }
1164
1568
  if (!known) {
@@ -1175,9 +1579,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1175
1579
  } else {
1176
1580
  size_t mark = j->v.path_len;
1177
1581
  if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
1178
- js_apply(j, ap, v, depth);
1582
+ js_apply_child(j, ap, v, depth);
1179
1583
  yep_validate_path_pop(&j->v, mark);
1180
1584
  }
1585
+ ep_append(j, k);
1181
1586
  }
1182
1587
  }
1183
1588
  }
@@ -1188,24 +1593,29 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
1188
1593
  }
1189
1594
  }
1190
1595
  }
1191
- }
1192
-
1193
- /* ---- the object-keyword walkers ------------------------------------------ */
1194
1596
 
1195
- /* Copies a bounded name for diagnostics/detail text: the copied
1196
- * length (the caller pushes paths with it — keys may hold NULs). */
1197
- static size_t name_copy(char* buf, size_t cap, const char* p, size_t len) {
1198
- size_t cl = len < cap - 1 ? len : cap - 1;
1199
- if (cl > 0 && p != NULL) {
1200
- memcpy(buf, p, cl);
1597
+ /* the unevaluated* keywords ride LAST in their schema object,
1598
+ * whatever the document order: their verdict reads every sibling
1599
+ * keyword's and every in-place applicator's annotations for THIS
1600
+ * location — [pmark, ep_len) / [imark, ei_len) */
1601
+ {
1602
+ uint32_t uv;
1603
+ int uf = 0;
1604
+ if (kw_schema(j, schema_id, "unevaluatedProperties", &uv, &uf)) {
1605
+ check_unevaluated_properties(j, uv, uf, inst_id, depth, pmark);
1606
+ }
1607
+ if (kw_schema(j, schema_id, "unevaluatedItems", &uv, &uf)) {
1608
+ check_unevaluated_items(j, uv, uf, inst_id, depth, imark);
1609
+ }
1201
1610
  }
1202
- buf[cl] = '\0';
1203
- return cl;
1204
1611
  }
1205
1612
 
1613
+ /* ---- the object-keyword walkers ------------------------------------------ */
1614
+
1206
1615
  /* patternProperties: pattern -> schema over the KEY strings; every
1207
1616
  * matching pattern applies its schema to the key's value (a false
1208
- * schema rides js_apply's FALSE_SCHEMA diagnostic). */
1617
+ * schema rides js_apply's FALSE_SCHEMA diagnostic). The matched keys
1618
+ * are this object's annotation. */
1209
1619
  static void check_pattern_properties(js_ctx* j, uint32_t pp_id, uint32_t inst_id, int depth) {
1210
1620
  const yep_dom* d = j->v.dom;
1211
1621
  const yep_dnode* pnm = snode(j, pp_id);
@@ -1236,9 +1646,10 @@ static void check_pattern_properties(js_ctx* j, uint32_t pp_id, uint32_t inst_id
1236
1646
  size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
1237
1647
  size_t mark = j->v.path_len;
1238
1648
  if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
1239
- js_apply(j, pv, v, depth);
1649
+ js_apply_child(j, pv, v, depth);
1240
1650
  yep_validate_path_pop(&j->v, mark);
1241
1651
  }
1652
+ ep_append(j, k);
1242
1653
  }
1243
1654
  pk = np;
1244
1655
  }
@@ -1267,7 +1678,7 @@ static void check_property_names(js_ctx* j, uint32_t pn_id, uint32_t inst_id, in
1267
1678
  size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
1268
1679
  size_t mark = j->v.path_len;
1269
1680
  if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
1270
- js_apply(j, pn_id, k, depth);
1681
+ js_apply_child(j, pn_id, k, depth);
1271
1682
  yep_validate_path_pop(&j->v, mark);
1272
1683
  }
1273
1684
  }
@@ -1327,7 +1738,8 @@ static void check_dependent_required(js_ctx* j, uint32_t dr_id, uint32_t inst_id
1327
1738
  }
1328
1739
 
1329
1740
  /* dependentSchemas: when the named key is present, its schema applies
1330
- * to the whole object. */
1741
+ * to the whole object (same location: its annotations join this
1742
+ * object's set). */
1331
1743
  static void check_dependent_schemas(js_ctx* j, uint32_t ds_id, uint32_t inst_id, int depth) {
1332
1744
  const yep_dom* d = j->v.dom;
1333
1745
  const yep_dnode* dm = snode(j, ds_id);
@@ -1342,13 +1754,90 @@ static void check_dependent_schemas(js_ctx* j, uint32_t ds_id, uint32_t inst_id,
1342
1754
  if (skn != NULL && skn->kind == YEP_DOM_SCALAR && sv != UINT32_MAX) {
1343
1755
  yep_view trig = yep_dom_view(j->sd, skn->value);
1344
1756
  if (trig.p != NULL && inst_find_key(d, inst_id, trig.p, trig.len, NULL) != UINT32_MAX) {
1345
- js_apply(j, sv, inst_id, depth);
1757
+ js_apply(j, sv, inst_id, depth); /* absorbed */
1346
1758
  }
1347
1759
  }
1348
1760
  sk = ns;
1349
1761
  }
1350
1762
  }
1351
1763
 
1764
+ /* unevaluatedProperties: the keys no sibling keyword and no in-place
1765
+ * applicator evaluated — [pmark, ep_len). A false schema flags each
1766
+ * one; a readable schema is APPLIED to the value (the applied keys
1767
+ * then annotate, so a nested unevaluatedProperties:true marks every
1768
+ * property evaluated for the parent). */
1769
+ static void check_unevaluated_properties(js_ctx* j, uint32_t upv, int upf, uint32_t inst_id,
1770
+ int depth, size_t pmark) {
1771
+ const yep_dom* d = j->v.dom;
1772
+ const yep_dnode* n = yep_dom_node(d, inst_id);
1773
+ if (n == NULL || n->kind != YEP_DOM_MAPPING) {
1774
+ return;
1775
+ }
1776
+ uint32_t k = n->first_child;
1777
+ while (k != UINT32_MAX && !j->v.oom) {
1778
+ uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
1779
+ uint32_t nk =
1780
+ v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
1781
+ const yep_dnode* kn = yep_dom_node(d, k);
1782
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR && !eval_has_prop(j, pmark, k)) {
1783
+ yep_view kv = yep_dom_view(d, kn->value);
1784
+ char namebuf[64];
1785
+ size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
1786
+ if (upf) {
1787
+ size_t mark = j->v.path_len;
1788
+ int pushed = yep_validate_path_push(&j->v, namebuf, cl);
1789
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_UNEVALUATED_PROPERTY,
1790
+ yep_validate_node_off(d, k), "property \"%s\" is not evaluated",
1791
+ namebuf);
1792
+ if (pushed == 0) {
1793
+ yep_validate_path_pop(&j->v, mark);
1794
+ }
1795
+ } else {
1796
+ size_t mark = j->v.path_len;
1797
+ if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
1798
+ js_apply_child(j, upv, v, depth);
1799
+ yep_validate_path_pop(&j->v, mark);
1800
+ }
1801
+ ep_append(j, k); /* the applied property now counts as evaluated */
1802
+ }
1803
+ }
1804
+ k = nk;
1805
+ }
1806
+ }
1807
+
1808
+ /* unevaluatedItems: the mirror over array indices ([imark, ei_len)). */
1809
+ static void check_unevaluated_items(js_ctx* j, uint32_t uv, int uf, uint32_t inst_id, int depth,
1810
+ size_t imark) {
1811
+ const yep_dom* d = j->v.dom;
1812
+ const yep_dnode* n = yep_dom_node(d, inst_id);
1813
+ if (n == NULL || n->kind != YEP_DOM_SEQUENCE) {
1814
+ return;
1815
+ }
1816
+ uint32_t ich = n->first_child;
1817
+ for (uint32_t i = 0; ich != UINT32_MAX && !j->v.oom; i++) {
1818
+ uint32_t ni = yep_dom_node(d, ich) ? yep_dom_node(d, ich)->next_sibling : UINT32_MAX;
1819
+ if (!eval_has_item(j, imark, i)) {
1820
+ if (uf) {
1821
+ size_t mark = j->v.path_len;
1822
+ int pushed = yep_validate_path_push_index(&j->v, i);
1823
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_UNEVALUATED_ITEM,
1824
+ yep_validate_node_off(d, ich), "item %u is not evaluated", i);
1825
+ if (pushed == 0) {
1826
+ yep_validate_path_pop(&j->v, mark);
1827
+ }
1828
+ } else {
1829
+ size_t mark = j->v.path_len;
1830
+ if (yep_validate_path_push_index(&j->v, i) == 0) {
1831
+ js_apply_child(j, uv, ich, depth);
1832
+ yep_validate_path_pop(&j->v, mark);
1833
+ }
1834
+ ei_append(j, i);
1835
+ }
1836
+ }
1837
+ ich = ni;
1838
+ }
1839
+ }
1840
+
1352
1841
  /* ---- the entry ----------------------------------------------------------- */
1353
1842
 
1354
1843
  /* Validates one instance document root against the schema root.
@@ -1361,32 +1850,73 @@ static int js_validate_doc(js_ctx* j, uint32_t inst_root) {
1361
1850
  /* The public entries live in validate.c/compile.c (yeptris_json_schema_
1362
1851
  * validate compiles+validates+frees through the compiled handle; the
1363
1852
  * compiled face drives this seam per instance stream): they parse the
1364
- * sides, build the report and hand over the schema DOM + the retained
1365
- * pattern cache (the declarations are internal.h's — the shared SSOT). */
1853
+ * sides, build the report and hand over the schema DOM, the retained
1854
+ * pattern cache and the identifier map (the declarations are
1855
+ * internal.h's — the shared SSOT). */
1366
1856
  int yep_jsonschema_run(const yep_dom* schema_dom, uint32_t schema_root, const yep_dom* instance_dom,
1367
1857
  const uint32_t* doc_roots, uint32_t doc_count, yeptris_validate_report* rep,
1368
- const yep_re_cache* cache) {
1858
+ const yep_re_cache* cache, const js_resources* res) {
1369
1859
  js_ctx j;
1370
1860
  memset(&j, 0, sizeof(j));
1371
1861
  j.sd = schema_dom;
1372
1862
  j.sroot = schema_root;
1373
1863
  j.cache = cache;
1864
+ j.res = res;
1374
1865
  j.v.dom = instance_dom;
1375
1866
  j.v.rep = rep;
1376
1867
  j.v.input = instance_dom->input_base;
1377
1868
  j.v.input_len = instance_dom->input_len;
1869
+ int rc = 0;
1378
1870
  for (uint32_t i = 0; i < doc_count; i++) {
1379
1871
  j.v.path_len = 0;
1872
+ /* the stream face's document attribution: a multi-document
1873
+ * stream addresses its documents as array elements */
1874
+ size_t mark = yep_validate_doc_prefix(&j.v, doc_count > 1, i);
1380
1875
  js_validate_doc(&j, doc_roots[i]);
1876
+ yep_validate_path_pop(&j.v, mark);
1381
1877
  if (j.v.oom) {
1382
- yep_free(yep_system_allocator(), j.v.line_off);
1383
- yep_free(yep_system_allocator(), j.v.path);
1384
- return -1;
1878
+ rc = -1;
1879
+ break;
1385
1880
  }
1386
1881
  }
1387
- /* the walk's scratch (the lazy line index, the path buffer) dies
1388
- * here: the report carries only finished diagnostics */
1389
- yep_free(yep_system_allocator(), j.v.line_off);
1390
- yep_free(yep_system_allocator(), j.v.path);
1391
- return 0;
1882
+ /* the walk's scratch (the lazy line index, the path buffer, the
1883
+ * annotation arenas) dies here: the report carries only finished
1884
+ * diagnostics */
1885
+ const yep_allocator* sys = yep_system_allocator();
1886
+ yep_free(sys, j.v.line_off);
1887
+ yep_free(sys, j.v.path);
1888
+ yep_free(sys, j.ep);
1889
+ yep_free(sys, j.ei);
1890
+ return rc;
1891
+ }
1892
+
1893
+ /* The tag-driven hook's seam (internal.h, #906 slice 4): one bound
1894
+ * schema applied to ONE instance subtree through the CALLER'S walk
1895
+ * context. The context is copied in (its path prefix is the tag
1896
+ * site's — the subtree's instance pointers extend it), the schema
1897
+ * root is applied at depth 0 (the same walk, the same annotation
1898
+ * model), and the context is copied back: the report, the guards and
1899
+ * the scratch pointers stay the caller's, so the cap law and the
1900
+ * line/col resolution hold across the whole report. */
1901
+ int yep_jsonschema_apply_subtree(const yep_dom* schema_dom, uint32_t schema_root, yep_vctx* inst,
1902
+ uint32_t inst_root, const yep_re_cache* cache,
1903
+ const js_resources* res) {
1904
+ if (schema_dom == NULL || inst == NULL) {
1905
+ return 0;
1906
+ }
1907
+ js_ctx j;
1908
+ memset(&j, 0, sizeof(j));
1909
+ j.sd = schema_dom;
1910
+ j.sroot = schema_root;
1911
+ j.cache = cache;
1912
+ j.res = res;
1913
+ j.v = *inst; /* the caller's prefix, report, line index, guards */
1914
+ js_apply(&j, j.sroot, inst_root, 0);
1915
+ /* the annotation arenas are THIS application's scratch (the full
1916
+ * driver's tail law) — they never join the caller's context */
1917
+ const yep_allocator* sys = yep_system_allocator();
1918
+ yep_free(sys, j.ep);
1919
+ yep_free(sys, j.ei);
1920
+ *inst = j.v; /* scratch may have grown: the caller still frees it */
1921
+ return j.v.oom ? -1 : 0;
1392
1922
  }