yeptris 0.6.59 → 0.6.61

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.
@@ -16,6 +16,7 @@
16
16
  #include "yeptris/events.h"
17
17
  #include "yeptris/json.h"
18
18
  #include "yeptris/parse.h"
19
+ #include "yeptris/validate.h"
19
20
  #include "yeptris/version.h"
20
21
 
21
22
  static char *g_out;
@@ -334,19 +335,29 @@ int yeptris_wasm_load_all(const char *data, size_t len) {
334
335
  return rc;
335
336
  }
336
337
 
337
- /* JSON in -> canonical YAML out (JSON is a subset of YAML 1.2 flow, so
338
- * the one parse engine takes it; the emitter's canonical form is
339
- * byte-stable across round trips). */
338
+ /* The dump-options face (emit.h's yeptris_emit_options): canonical
339
+ * selects the engine's fixed form (flow collections, quoted strings,
340
+ * typed words, shortest floats); best_width folds flow collections
341
+ * after a value once the line exceeds it (<= 0 rides the library
342
+ * default, 80 — the libyaml parity). The defaults (0, 0) are
343
+ * byte-identical to the no-options yeptris_serialize: opts_width
344
+ * answers 80 for best_width <= 0 and canonical 0 is the fidelity
345
+ * mode. */
340
346
  EMSCRIPTEN_KEEPALIVE
341
- int yeptris_wasm_dump(const char *data, size_t len) {
347
+ int yeptris_wasm_dump_ex(const char *data, size_t len, int canonical, int best_width) {
342
348
  YeptrisStatus st = YEPTRIS_OK;
343
349
  YeptrisDocument doc = yeptris_parse(data, len, &st);
344
350
  if (doc == NULL || st != YEPTRIS_OK) {
345
351
  capture_error();
346
352
  return (int)st;
347
353
  }
354
+ yeptris_emit_options opts;
355
+ memset(&opts, 0, sizeof opts);
356
+ opts.size = (uint32_t)sizeof opts; /* the size-versioned contract */
357
+ opts.canonical = canonical;
358
+ opts.best_width = best_width;
348
359
  size_t out_len = 0;
349
- char *out = yeptris_serialize(doc, &out_len);
360
+ char *out = yeptris_serialize_ex(doc, &opts, &out_len);
350
361
  yeptris_document_free(doc);
351
362
  if (out == NULL) {
352
363
  snprintf(last_error, sizeof last_error, "serialization failed");
@@ -359,6 +370,14 @@ int yeptris_wasm_dump(const char *data, size_t len) {
359
370
  return YEPTRIS_OK;
360
371
  }
361
372
 
373
+ /* JSON in -> canonical YAML out (JSON is a subset of YAML 1.2 flow, so
374
+ * the one parse engine takes it; the emitter's canonical form is
375
+ * byte-stable across round trips). */
376
+ EMSCRIPTEN_KEEPALIVE
377
+ int yeptris_wasm_dump(const char *data, size_t len) {
378
+ return yeptris_wasm_dump_ex(data, len, 0, 0);
379
+ }
380
+
362
381
  EMSCRIPTEN_KEEPALIVE
363
382
  const char *yeptris_wasm_last_error(void) { return last_error; }
364
383
 
@@ -465,3 +484,394 @@ void yeptris_wasm_events_free(void) {
465
484
  g_ev_count = 0;
466
485
  g_ev_arena_len = 0;
467
486
  }
487
+
488
+ /* ---- the tolerant per-document stream iterator (parse.h) ----------------
489
+ *
490
+ * loadAll is all-or-nothing: one bad document fails the whole stream.
491
+ * The stream iterator walks document by document: a parse-class failure
492
+ * surfaces as ONE failed item (the failed document's own line/col via
493
+ * the iterator's status accessor) and the walk RESUMES at the next
494
+ * line-start marker — the valid documents after a bad one are kept.
495
+ * The MEMORY/ENCODING classes are stream-fatal; the iterator is done.
496
+ *
497
+ * The wasm boundary needs a RETAINED walk (one next() per JS call),
498
+ * which means the shim retains two things across calls: the iterator
499
+ * handle and a COPY of the input — the engine iterator only borrows
500
+ * its buffer until free, and the host's feed pointer dies when the
501
+ * JS call that opened the walk returns. Lifecycle mirrors the events
502
+ * face: open (feed, copy, construct), next() repeatedly, close always
503
+ * (the JS generator's finally is the owner).
504
+ *
505
+ * The base four functions (new/next/status/free) — this face ships
506
+ * against the v0.6.60 engine, whose iterator predates the slice
507
+ * accessors; the frame document crosses as JSON through the same
508
+ * g_out discipline every answer uses (a frame is a dcount==1 forest,
509
+ * so serialize_json answers it directly — the load path).
510
+ *
511
+ * The options door (ruby/py parity): open_ex carries (schema_kind,
512
+ * max_depth) and fills the engine's YeptrisParseOptions for the walk —
513
+ * the resolver family every frame types through and the per-frame
514
+ * nesting cap. The options struct is RETAINED (g_it_opts): the engine
515
+ * iterator only borrows its options (parse.h) and dereferences them
516
+ * on every next(), so a stack copy would die when open returns.
517
+ * schema_kind < 0 keeps the NULL ride — the engine defaults, the v1
518
+ * contract — and zeroed fields are the parse defaults, so only the
519
+ * two live fields ride (strict/tab_policy/recover stay reserved pins).
520
+ * The door is a NEW export, never a signature change of the v1 open
521
+ * above: this wasm surface's ABI is append-only, and the host probes
522
+ * the door's presence instead of trusting an export's arity. */
523
+ static yeptris_stream_iter *g_it;
524
+ static char *g_it_buf; /* the borrowed input's retained copy */
525
+ static size_t g_it_buf_len;
526
+ static int g_it_frame; /* 1: g_out carries a document JSON; 0: none */
527
+ static int g_it_index; /* the last next() step's consumed stream index */
528
+ static YeptrisParseOptions g_it_opts; /* borrowed by the iterator */
529
+
530
+ static int each_open_common(const char *data, size_t len, int schema_kind, int max_depth) {
531
+ if (g_it != NULL) { /* defensive: the previous walk was not closed */
532
+ yeptris_stream_iter_free(g_it);
533
+ g_it = NULL;
534
+ }
535
+ free(g_it_buf);
536
+ g_it_buf = NULL;
537
+ g_it_buf_len = 0;
538
+ g_it_frame = 0;
539
+ g_it_index = 0;
540
+ g_it_buf = (char *)malloc(len > 0 ? len : 1);
541
+ if (g_it_buf == NULL) {
542
+ capture_plain("stream iterator allocation failed");
543
+ return YEPTRIS_ERROR_MEMORY;
544
+ }
545
+ if (len > 0) {
546
+ memcpy(g_it_buf, data, len);
547
+ }
548
+ g_it_buf_len = len;
549
+ if (schema_kind < 0) {
550
+ /* the v1 contract: NULL opts rides the engine defaults */
551
+ g_it = yeptris_stream_iter_new(g_it_buf, len, NULL);
552
+ } else {
553
+ memset(&g_it_opts, 0, sizeof g_it_opts); /* zero = the parse defaults */
554
+ g_it_opts.schema = (YeptrisSchema)schema_kind;
555
+ g_it_opts.max_depth = max_depth;
556
+ g_it = yeptris_stream_iter_new(g_it_buf, len, &g_it_opts);
557
+ }
558
+ if (g_it == NULL) {
559
+ free(g_it_buf);
560
+ g_it_buf = NULL;
561
+ g_it_buf_len = 0;
562
+ capture_plain("stream iterator allocation failed");
563
+ return YEPTRIS_ERROR_MEMORY;
564
+ }
565
+ return YEPTRIS_OK;
566
+ }
567
+
568
+ /* The v1 open: NULL opts — the engine defaults, the face's shipped
569
+ * contract, byte-identical on every dist era. */
570
+ EMSCRIPTEN_KEEPALIVE
571
+ int yeptris_wasm_each_open(const char *data, size_t len) {
572
+ return each_open_common(data, len, -1, 0);
573
+ }
574
+
575
+ /* The options door (the v1 contract grown, not changed — see above). */
576
+ EMSCRIPTEN_KEEPALIVE
577
+ int yeptris_wasm_each_open_ex(const char *data, size_t len, int schema_kind, int max_depth) {
578
+ return each_open_common(data, len, schema_kind, max_depth);
579
+ }
580
+
581
+ EMSCRIPTEN_KEEPALIVE
582
+ int yeptris_wasm_each_next(void) {
583
+ if (g_it == NULL) {
584
+ capture_plain("the walk is closed");
585
+ return YEPTRIS_ERROR_ARG;
586
+ }
587
+ YeptrisDocument doc = NULL;
588
+ size_t index = 0;
589
+ YeptrisStatus st = yeptris_stream_iter_next(g_it, &doc, &index);
590
+ g_it_index = (int)index;
591
+ if (st == YEPTRIS_OK && doc == NULL) { /* the stream is over */
592
+ g_it_frame = 0;
593
+ return YEPTRIS_OK;
594
+ }
595
+ if (st == YEPTRIS_OK) {
596
+ size_t out_len = 0;
597
+ char *out = yeptris_serialize_json(doc, &out_len);
598
+ yeptris_document_free(doc);
599
+ if (out == NULL) {
600
+ capture_plain("serialization failed");
601
+ return YEPTRIS_ERROR_MEMORY;
602
+ }
603
+ publish_out(out, out_len);
604
+ g_it_frame = 1;
605
+ return YEPTRIS_OK;
606
+ }
607
+ /* A failed document (parse/depth class; the walk may continue) or
608
+ * a stream-fatal verdict (MEMORY/ENCODING; it may not). The why
609
+ * rides the engine's error channel; the position rides the
610
+ * iterator's per-document status accessor — the authoritative
611
+ * source for the failed document's own mark. */
612
+ g_it_frame = 0;
613
+ capture_error();
614
+ uint32_t line = 0;
615
+ uint32_t col = 0;
616
+ yeptris_stream_iter_status(g_it, &line, &col);
617
+ last_error_line = (int)line;
618
+ last_error_column = (int)col;
619
+ return (int)st;
620
+ }
621
+
622
+ /* 1 when the last successful next() published a document JSON into
623
+ * g_out; 0 when the stream is over (the JS door's end sentinel). */
624
+ EMSCRIPTEN_KEEPALIVE
625
+ int yeptris_wasm_each_frame(void) { return g_it_frame; }
626
+
627
+ /* The 0-based stream index the last next() consumed — failures
628
+ * consume an index too, so the yield order is the stream's. */
629
+ EMSCRIPTEN_KEEPALIVE
630
+ int yeptris_wasm_each_index(void) { return g_it_index; }
631
+
632
+ EMSCRIPTEN_KEEPALIVE
633
+ void yeptris_wasm_each_close(void) {
634
+ yeptris_stream_iter_free(g_it);
635
+ g_it = NULL;
636
+ free(g_it_buf);
637
+ g_it_buf = NULL;
638
+ g_it_buf_len = 0;
639
+ g_it_frame = 0;
640
+ g_it_index = 0;
641
+ }
642
+
643
+ /* ---- validation (validate.h) -------------------------------------------
644
+ *
645
+ * The engine ships three validation doors — the SCHEMA-FAMILY walk
646
+ * (yeptris_validate, the YeptrisSchema kind rides as a plain int
647
+ * parameter), the one-shot JSON SCHEMA walk (yeptris_json_schema_
648
+ * validate) and the COMPILED economy (yeptris_schema_compile /
649
+ * yeptris_schema_validate / yeptris_schema_free: parse the schema
650
+ * once, validate many streams).
651
+ *
652
+ * Diagnostics cross as DATA, never as pointers: the whole report
653
+ * drains once into the same g_out arena the JSON answers use, spelled
654
+ * as one JSON array of raw records
655
+ *
656
+ * {"code":N,"path":"...","message":"...","line":N,"col":N,"offset":N}
657
+ *
658
+ * `path` is the engine's JSON-pointer instance path ("/a/b/0", ~0/~1
659
+ * escaped; "" at the root), `message` its bounded detail. The
660
+ * code -> keyword mapping is binding-owned (the same law the ruby/py
661
+ * faces follow) and lives in src/index.js — the shim carries the raw
662
+ * record only. The report frees inside the shim on every path; the
663
+ * JS answer arrives via the g_out discipline (read + out_free), so
664
+ * nothing engine-owned survives the call.
665
+ *
666
+ * Statuses through the wasm boundary: YEPTRIS_OK (valid) and
667
+ * YEPTRIS_ERROR_SCHEMA (invalid: the report carries diagnostics) both
668
+ * publish the array; every other status is a call failure —
669
+ * last_error carries the why, g_out is untouched. The host reads
670
+ * g_out for statuses 0 and 9 only.
671
+ *
672
+ * The compiled handle crosses as an opaque i32 (a wasm32 pointer):
673
+ * the host stores it and hands it back; free rides
674
+ * yeptris_wasm_schema_free. A compile failure answers 0 with
675
+ * last_error set. The engine carries NO schema KIND on the compile
676
+ * door (a compiled handle is a JSON-Schema document by
677
+ * validate.h's contract) — the family kind selection lives on the
678
+ * validate door, exactly as the C surface spells it. */
679
+
680
+ /* A growable append buffer for the report JSON. */
681
+ typedef struct {
682
+ char *p;
683
+ size_t len, cap;
684
+ int failed;
685
+ } yw_buf;
686
+
687
+ static void yw_buf_grow(yw_buf *b, size_t need) {
688
+ if (b->failed || b->len + need <= b->cap) {
689
+ return;
690
+ }
691
+ size_t ncap = b->cap ? b->cap : 256;
692
+ while (ncap < b->len + need) {
693
+ ncap *= 2;
694
+ }
695
+ char *np = (char *)realloc(b->p, ncap);
696
+ if (np == NULL) {
697
+ free(b->p);
698
+ b->p = NULL;
699
+ b->failed = 1;
700
+ return;
701
+ }
702
+ b->p = np;
703
+ b->cap = ncap;
704
+ }
705
+
706
+ static void yw_buf_puts(yw_buf *b, const char *s) {
707
+ size_t n = strlen(s);
708
+ yw_buf_grow(b, n + 1);
709
+ if (b->failed) {
710
+ return;
711
+ }
712
+ memcpy(b->p + b->len, s, n);
713
+ b->len += n;
714
+ }
715
+
716
+ /* Appends s as one JSON string literal: quote, escape (the bounded
717
+ * detail text can carry quotes, backslashes and control characters —
718
+ * the instance's own scalars appear in it), close. */
719
+ static void yw_buf_putjson(yw_buf *b, const char *s) {
720
+ yw_buf_grow(b, 1);
721
+ if (b->failed) {
722
+ return;
723
+ }
724
+ b->p[b->len++] = '"';
725
+ for (const unsigned char *c = (const unsigned char *)s; *c != '\0'; c++) {
726
+ char esc[8];
727
+ switch (*c) {
728
+ case '"':
729
+ yw_buf_puts(b, "\\\"");
730
+ break;
731
+ case '\\':
732
+ yw_buf_puts(b, "\\\\");
733
+ break;
734
+ case '\b':
735
+ yw_buf_puts(b, "\\b");
736
+ break;
737
+ case '\f':
738
+ yw_buf_puts(b, "\\f");
739
+ break;
740
+ case '\n':
741
+ yw_buf_puts(b, "\\n");
742
+ break;
743
+ case '\r':
744
+ yw_buf_puts(b, "\\r");
745
+ break;
746
+ case '\t':
747
+ yw_buf_puts(b, "\\t");
748
+ break;
749
+ default:
750
+ if (*c < 0x20) {
751
+ snprintf(esc, sizeof esc, "\\u%04x", (unsigned)*c);
752
+ yw_buf_puts(b, esc);
753
+ } else {
754
+ yw_buf_grow(b, 1);
755
+ if (b->failed) {
756
+ return;
757
+ }
758
+ b->p[b->len++] = (char)*c;
759
+ }
760
+ break;
761
+ }
762
+ if (b->failed) {
763
+ return;
764
+ }
765
+ }
766
+ yw_buf_grow(b, 2);
767
+ if (b->failed) {
768
+ return;
769
+ }
770
+ b->p[b->len++] = '"';
771
+ b->p[b->len] = '\0';
772
+ }
773
+
774
+ static void yw_buf_putint(yw_buf *b, long v) {
775
+ char tmp[32];
776
+ snprintf(tmp, sizeof tmp, "%ld", v);
777
+ yw_buf_puts(b, tmp);
778
+ }
779
+
780
+ /* Drains the whole report into g_out as the JSON array and frees the
781
+ * report — exactly once, on every path. rep may be NULL (an empty
782
+ * answer: the parse-failed statuses never reach here, but an engine
783
+ * that hands back no report for a valid empty stream does). */
784
+ static int yeptris_wasm_publish_report(yeptris_validate_report *rep) {
785
+ size_t n = rep != NULL ? yeptris_validate_report_count(rep) : 0;
786
+ yw_buf b;
787
+ memset(&b, 0, sizeof b);
788
+ yw_buf_puts(&b, "[");
789
+ for (size_t i = 0; i < n; i++) {
790
+ const yeptris_diagnostic *d = yeptris_validate_report_at(rep, i);
791
+ if (d == NULL) {
792
+ break;
793
+ }
794
+ if (i != 0) {
795
+ yw_buf_puts(&b, ",");
796
+ }
797
+ yw_buf_puts(&b, "{\"code\":");
798
+ yw_buf_putint(&b, (long)d->code);
799
+ yw_buf_puts(&b, ",\"path\":");
800
+ yw_buf_putjson(&b, d->path);
801
+ yw_buf_puts(&b, ",\"message\":");
802
+ yw_buf_putjson(&b, d->detail);
803
+ yw_buf_puts(&b, ",\"line\":");
804
+ yw_buf_putint(&b, (long)d->line);
805
+ yw_buf_puts(&b, ",\"col\":");
806
+ yw_buf_putint(&b, (long)d->col);
807
+ yw_buf_puts(&b, ",\"offset\":");
808
+ yw_buf_putint(&b, (long)d->offset);
809
+ yw_buf_puts(&b, "}");
810
+ }
811
+ yw_buf_puts(&b, "]");
812
+ yeptris_validate_report_free(rep);
813
+ if (b.failed) {
814
+ return YEPTRIS_ERROR_MEMORY;
815
+ }
816
+ /* Ownership moves to g_out: yeptris_wasm_out_free releases it via
817
+ * yeptris_free (plain free) — the same allocator this realloc/malloc
818
+ * pair rides, so the hand-off is symmetric. */
819
+ publish_out(b.p, b.len);
820
+ return YEPTRIS_OK;
821
+ }
822
+
823
+ /* The status a validate call answers with: OK and ERROR_SCHEMA both
824
+ * publish the report; the caller (JS) reads g_out for those two and
825
+ * treats the rest as a call failure. */
826
+ static int yeptris_wasm_validate_status(YeptrisStatus st, yeptris_validate_report *rep) {
827
+ if (st != YEPTRIS_OK && st != YEPTRIS_ERROR_SCHEMA) {
828
+ capture_error();
829
+ return (int)st;
830
+ }
831
+ if (yeptris_wasm_publish_report(rep) != 0) {
832
+ capture_plain("diagnostic serialization failed");
833
+ return YEPTRIS_ERROR_MEMORY;
834
+ }
835
+ return (int)st;
836
+ }
837
+
838
+ /* The SCHEMA-FAMILY walk: schema_kind is YeptrisSchema (resolve.h) —
839
+ * the engine validates that the kind IS a validation family and
840
+ * answers YEPTRIS_ERROR_ARG otherwise. */
841
+ EMSCRIPTEN_KEEPALIVE
842
+ int yeptris_wasm_validate(const char *data, size_t len, int schema_kind) {
843
+ yeptris_validate_report *rep = NULL;
844
+ YeptrisStatus st = yeptris_validate(data, len, (YeptrisSchema)schema_kind, &rep);
845
+ return yeptris_wasm_validate_status(st, rep);
846
+ }
847
+
848
+ /* The one-shot JSON SCHEMA walk (draft 2020-12 subset; validate.h). */
849
+ EMSCRIPTEN_KEEPALIVE
850
+ int yeptris_wasm_json_schema_validate(const char *schema, size_t schema_len, const char *data,
851
+ size_t len) {
852
+ yeptris_validate_report *rep = NULL;
853
+ YeptrisStatus st = yeptris_json_schema_validate(schema, schema_len, data, len, &rep);
854
+ return yeptris_wasm_validate_status(st, rep);
855
+ }
856
+
857
+ /* The COMPILED economy. The handle is an opaque wasm32 pointer the
858
+ * host stores verbatim; 0 = compile failed (last_error carries the
859
+ * why). */
860
+ EMSCRIPTEN_KEEPALIVE
861
+ yeptris_schema *yeptris_wasm_schema_compile(const char *bytes, size_t n) {
862
+ yeptris_schema *s = yeptris_schema_compile(bytes, n);
863
+ if (s == NULL) {
864
+ capture_error();
865
+ }
866
+ return s;
867
+ }
868
+
869
+ EMSCRIPTEN_KEEPALIVE
870
+ int yeptris_wasm_schema_validate(yeptris_schema *s, const char *data, size_t len) {
871
+ yeptris_validate_report *rep = NULL;
872
+ YeptrisStatus st = yeptris_schema_validate(s, data, len, &rep);
873
+ return yeptris_wasm_validate_status(st, rep);
874
+ }
875
+
876
+ EMSCRIPTEN_KEEPALIVE
877
+ void yeptris_wasm_schema_free(yeptris_schema *s) { yeptris_schema_free(s); }