yeptris 0.6.64.0-riscv64-linux-gnu

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 (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,319 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Yeptris::Document
4
+ attr_reader :c_ptr
5
+
6
+ # Shared between the instance and its GC finalizer (Procs close over
7
+ # variables by reference) — the leptris-ruby double-free fix: the
8
+ # explicit free path and the finalizer both flip the same flag.
9
+ Freed = Struct.new(:state) # :alive | :freed
10
+
11
+ # The raw C handle — the Yeptris::Node wrappers cache by its
12
+ # address; stream-children wrappers (Document.without_finalizer)
13
+ # carry it without registering a finalizer (#182).
14
+ attr_reader :c_ptr
15
+
16
+ # The schema this document was parsed with (a parse property).
17
+ attr_reader :parse_schema
18
+
19
+ def initialize(c_ptr = nil, freed = Freed.new(:alive), parse_schema = :core_12, source = nil)
20
+ # The pointer is produced by the FFI parse/create calls; a nil (or
21
+ # non-pointer) argument is an API misuse — Document.parse and
22
+ # Document.create are the entry points. Fail with that guidance
23
+ # instead of a NoMethodError from the finalizer guard below (the
24
+ # FFI::Pointer#null? probe does not exist on nil).
25
+ unless c_ptr.is_a?(::FFI::Pointer)
26
+ raise ArgumentError,
27
+ "a document FFI pointer is required — build documents with " \
28
+ "Yeptris::Document.parse or Yeptris::Document.create"
29
+ end
30
+ @c_ptr = c_ptr
31
+ @freed = freed
32
+ @parse_schema = parse_schema
33
+ # the C document BORROWS the input: string values are zero-copy
34
+ # views over the caller's bytes (parse.h/doc.h) — the FFI call only
35
+ # pins the String for the call's duration, so the document must
36
+ # retain it or the next GC frees memory every later node.value/
37
+ # tag read still points into (audit 2026-10-31: parse(io.read) was
38
+ # collectable immediately)
39
+ @source = source
40
+ @readonly = false
41
+ # Strong wrapper cache keyed on the C node address: the same C node
42
+ # always yields the same Ruby object (identity for aliases/eql?),
43
+ # cleared at free — no stale entries, no GC-race weak maps.
44
+ @wrapper_cache = {}
45
+ ObjectSpace.define_finalizer(self, self.class.finalize(c_ptr)) unless c_ptr.null?
46
+ end
47
+
48
+ def self.parse(yaml, schema: :core_12, max_depth: 0)
49
+ yaml = Yeptris.read_input(yaml)
50
+ yaml = yaml.to_s
51
+ status = ::FFI::MemoryPointer.new(:int)
52
+ doc =
53
+ if schema == :core_12 && max_depth.zero?
54
+ Yeptris::FFI.yeptris_parse(yaml, yaml.bytesize, status)
55
+ else
56
+ opts = Yeptris::FFI::ParseOptions.new
57
+ opts[:schema] = schema == :compat_11 ? Yeptris::FFI::SCHEMA_11_COMPAT : Yeptris::FFI::SCHEMA_12_CORE
58
+ opts[:max_depth] = max_depth
59
+ Yeptris::FFI.yeptris_parse_ex(yaml, yaml.bytesize, opts, status)
60
+ end
61
+ if doc.null?
62
+ # NULL + OK is the legal empty stream (no documents): nil, like
63
+ # stdlib. The status out-param is the discriminator — the TLS
64
+ # error message cannot serve (a stale error from an earlier
65
+ # parse persists on the thread).
66
+ return nil if status.read_int.zero?
67
+
68
+ raise Yeptris::ParseError,
69
+ "parse failed: #{Yeptris::FFI.last_error_message}"
70
+ end
71
+ wrap_schema(doc, schema, yaml)
72
+ end
73
+
74
+ def self.parse_json(json)
75
+ json = Yeptris.read_input(json)
76
+ json = json.to_s
77
+ doc = Yeptris::FFI.yeptris_parse_json(json, json.bytesize, nil)
78
+ raise Yeptris::ParseError,
79
+ "json parse failed: #{Yeptris::FFI.last_error_message}" if doc.null?
80
+
81
+ wrap_schema(doc, :core_12, json) # strict JSON is core by construction
82
+ end
83
+
84
+ # An empty document for from-scratch construction (TODO.impl/11 p3).
85
+ # schema: :compat_11 types its plain scalars through the Psych-
86
+ # compatible resolver — the dump faces build with it so a host String
87
+ # the tree builder kept plain ("1e0") stays a string (#293).
88
+ def self.create(schema: nil)
89
+ doc =
90
+ if schema == :compat_11
91
+ Yeptris::FFI.yeptris_document_new_schema(Yeptris::FFI::SCHEMA_11_COMPAT)
92
+ else
93
+ Yeptris::FFI.yeptris_document_new
94
+ end
95
+ raise Yeptris::Error, "yeptris_document_new failed" if doc.null?
96
+
97
+ wrap(doc)
98
+ end
99
+
100
+ # @api private
101
+ def self.wrap(c_ptr)
102
+ new(c_ptr)
103
+ end
104
+
105
+ # The schema the document was parsed with (:core_12 / :compat_11) —
106
+ # host scalar policies (Psych's dot-required float) are conditioned
107
+ # on it. The parse constructors set the real one; plain wrap keeps
108
+ # the core_12 default (yeptris_parse's default).
109
+ def self.wrap_schema(c_ptr, schema, source = nil)
110
+ new(c_ptr, Freed.new(:alive), schema, source)
111
+ end
112
+
113
+ # #182: the stream-children wrapper owns NOTHING (the parent
114
+ # Document wrapper is the sole freer of the C memory). A plain
115
+ # Document.new would register a finalizer on a c_ptr we don't own;
116
+ # a parse_stream on a multi-doc stream would free the same pointer
117
+ # three times at GC → SIGABRT. This factory builds the wrapper
118
+ # WITHOUT a finalizer; the wrapper's lifetime follows the owner.
119
+ def self.without_finalizer(c_ptr, source = nil)
120
+ doc = new(c_ptr, Freed.new(:alive), :core_12, source)
121
+ ObjectSpace.undefine_finalizer(doc)
122
+ doc
123
+ end
124
+
125
+ def ensure_alive!
126
+ raise Yeptris::FreedError, "document is freed" if @freed.state == :freed
127
+ end
128
+
129
+ def free
130
+ return if @freed.state == :freed
131
+
132
+ @freed.state = :freed
133
+ # stop the GC finalizer BEFORE the C free — it captures the same
134
+ # pointer, and its firing after this free is the double free
135
+ ObjectSpace.undefine_finalizer(self)
136
+ @wrapper_cache.clear
137
+ Yeptris::FFI.yeptris_document_free(@c_ptr)
138
+ end
139
+
140
+ def freed?
141
+ @freed.state == :freed
142
+ end
143
+
144
+ def readonly!
145
+ @readonly = true
146
+ self
147
+ end
148
+
149
+ def readonly?
150
+ @readonly
151
+ end
152
+
153
+ # @api private — memo table for readonly materialization: node ids
154
+ # that already produced their Ruby object keep it (leptris pattern:
155
+ # readonly documents never change, so the memo is forever valid).
156
+ def readonly_memo
157
+ @readonly_memo ||= {}
158
+ end
159
+
160
+ # @api private — whether the C marshal materialization lane may
161
+ # serve this document: refused when the parse source carries a
162
+ # quoted-symbol literal's byte fingerprint (the C builder names
163
+ # symbols from the raw scalar text and keeps the quotes, #1304 —
164
+ # the Ruby walks own the strip until the engine strip lands).
165
+ # Memoized: the verdict is a property of the immutable source.
166
+ def marshal_lane_ok?
167
+ @marshal_lane_ok ||= @source.nil? || ::Yeptris::ValueML.marshal_lane_ok?(@source)
168
+ end
169
+
170
+ # @api private — the single Node construction path. Query handles
171
+ # are transient C allocations; the wrapper cache is keyed on the
172
+ # STABLE node id (yeptris_node_id), so the same node always yields
173
+ # the same Ruby object no matter which query produced the handle.
174
+ def wrap_node(c_ptr)
175
+ ensure_alive!
176
+ return nil if c_ptr.null?
177
+
178
+ id = Yeptris::FFI.yeptris_node_id(c_ptr)
179
+ @wrapper_cache[id] ||= Yeptris::Node.new(c_ptr, self)
180
+ end
181
+
182
+ def document_count
183
+ ensure_alive!
184
+ Yeptris::FFI.yeptris_document_count(@c_ptr)
185
+ end
186
+
187
+ # Root node of stream document i (0-based).
188
+ def root(index = 0)
189
+ ensure_alive!
190
+ wrap_node(Yeptris::FFI.yeptris_document_root(@c_ptr, index))
191
+ end
192
+
193
+ # Bulk build (TODO.impl/15 phase D): one call raises the whole
194
+ # tree from a flat entry array + blob (see YAML::BulkBuilder).
195
+ def build_entries(entries, count, blob, blob_len)
196
+ ensure_alive!
197
+ Yeptris::FFI.yeptris_document_build(@c_ptr, entries, count, blob, blob_len)
198
+ end
199
+
200
+ def serialize(canonical: false, best_width: 0, explicit_doc_start: false)
201
+ ensure_alive!
202
+ len = ::FFI::MemoryPointer.new(:size_t)
203
+ # any non-zero width builds the opts — negative rides the engine's
204
+ # #1257 clamp (psych's line_width: -1 unwrap), 0/absent rides the
205
+ # engine default (80, folding)
206
+ ptr =
207
+ if canonical || !best_width.zero? || explicit_doc_start
208
+ opts = Yeptris::FFI::EmitOptions.new
209
+ opts[:size] = Yeptris::FFI::EmitOptions.size
210
+ opts[:canonical] = canonical ? 1 : 0
211
+ opts[:best_width] = best_width
212
+ opts[:explicit_doc_start] = explicit_doc_start ? 1 : 0
213
+ Yeptris::FFI.yeptris_serialize_ex(@c_ptr, opts, len)
214
+ else
215
+ Yeptris::FFI.yeptris_serialize(@c_ptr, len)
216
+ end
217
+ Yeptris::FFI::Owned.string(ptr, len)
218
+ end
219
+
220
+ def serialize_json_compact
221
+ len = ::FFI::MemoryPointer.new(:size_t)
222
+ Yeptris::FFI::Owned.string(Yeptris::FFI.yeptris_serialize_json_ex(@c_ptr, len, 1), len)
223
+ end
224
+
225
+ # JSON.pretty_generate's shape; nil indent rides the engine default
226
+ # (two spaces), any other String's bytes ride verbatim per depth
227
+ def serialize_json_pretty(indent = nil)
228
+ ensure_alive!
229
+ len = ::FFI::MemoryPointer.new(:size_t)
230
+ data = indent&.b
231
+ Yeptris::FFI::Owned.string(
232
+ Yeptris::FFI.yeptris_serialize_json_pretty(@c_ptr, len, data, data ? data.bytesize : 0), len
233
+ )
234
+ end
235
+
236
+ def serialize_json
237
+ ensure_alive!
238
+ len = ::FFI::MemoryPointer.new(:size_t)
239
+ Yeptris::FFI::Owned.string(Yeptris::FFI.yeptris_serialize_json(@c_ptr, len), len)
240
+ end
241
+
242
+ def to_s
243
+ serialize
244
+ end
245
+
246
+ # The Ruby object graph of stream document i (Psych-compatible
247
+ # materialization; alias identity preserved via the memo).
248
+ def to_ruby(index = 0)
249
+ ensure_alive!
250
+ r = root(index)
251
+ r.nil? ? nil : r.to_ruby
252
+ end
253
+
254
+ # The schema-family validation walk (validate.h, #882) over this
255
+ # document — or over every document of the stream it holds (the
256
+ # engine's report is flat across the stream; paths are root-relative
257
+ # per document). schema: :core_12 or :json_12 (the validation
258
+ # families; :compat_11 is a resolve schema, not a validation
259
+ # family). Returns plain Yeptris::Validation::Diagnostic values
260
+ # (empty = valid).
261
+ def validate(schema: :core_12)
262
+ ensure_alive!
263
+ report = ::FFI::MemoryPointer.new(:pointer)
264
+ v = ::Yeptris::Validation
265
+ st = Yeptris::FFI.yeptris_validate_document(@c_ptr, v.schema_id(schema), report)
266
+ v.consume_report(st, report, "validate document")
267
+ end
268
+
269
+ # ---- construction conveniences (TODO.impl/11 phase 3) ----
270
+
271
+ def new_mapping
272
+ ensure_alive!
273
+ wrap_node(Yeptris::FFI.yeptris_node_new_mapping(@c_ptr)) or
274
+ raise Yeptris::Error, "yeptris_node_new_mapping failed"
275
+ end
276
+
277
+ def new_sequence
278
+ ensure_alive!
279
+ wrap_node(Yeptris::FFI.yeptris_node_new_sequence(@c_ptr)) or
280
+ raise Yeptris::Error, "yeptris_node_new_sequence failed"
281
+ end
282
+
283
+ # style: :plain / :single_quoted / :double_quoted / :literal / :folded.
284
+ # The value is copied into the document (nothing is borrowed).
285
+ def new_scalar(text, style = :plain)
286
+ ensure_alive!
287
+ code = Yeptris::Node::STYLES.key(style) or
288
+ raise ArgumentError, "unknown scalar style #{style.inspect}"
289
+ text = text.to_s
290
+ n = Yeptris::FFI.yeptris_node_new_scalar(@c_ptr, text, text.bytesize, code)
291
+ wrap_node(n) or raise Yeptris::Error, "yeptris_node_new_scalar failed"
292
+ end
293
+
294
+ # An alias node: display name + the target it resolves to.
295
+ def new_alias(target, name)
296
+ ensure_alive!
297
+ n = Yeptris::FFI.yeptris_node_new_alias(@c_ptr, target.c_ptr, name, name.bytesize)
298
+ wrap_node(n) or raise Yeptris::Error, "yeptris_node_new_alias failed"
299
+ end
300
+
301
+ def set_root(node)
302
+ ensure_alive!
303
+ rc = Yeptris::FFI.yeptris_document_set_root(@c_ptr, node.c_ptr)
304
+ Yeptris::FFI.check_status(rc, "yeptris_document_set_root")
305
+ self
306
+ end
307
+
308
+ # GC safety net (#182): the proc captures ONLY the raw pointer — no
309
+ # Ruby objects, no wrapper, no freed-flag struct. A finalizer that
310
+ # closes over the owning wrapper races that wrapper's own sweep
311
+ # (the classic use-after-free: freed?/free against half-collected
312
+ # ivars, then a garbage C pointer into yeptris_document_free →
313
+ # SIGABRT). Explicit #free undefines this finalizer BEFORE the C
314
+ # free, so the double free cannot happen; finalizers run on the
315
+ # main thread, so there is no cross-thread window.
316
+ def self.finalize(c_ptr)
317
+ proc { Yeptris::FFI.yeptris_document_free(c_ptr) }
318
+ end
319
+ end