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,482 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
5
+
6
+ # A node inside a document. Borrowed C memory: every call routes
7
+ # through the owning document's liveness check — use after free
8
+ # raises Yeptris::FreedError, never a segfault.
9
+ class Yeptris::Node
10
+ include Enumerable
11
+
12
+ attr_reader :c_ptr
13
+
14
+ # @api private — always constructed via Document#wrap_node (identity).
15
+ def initialize(c_ptr, document)
16
+ @c_ptr = c_ptr
17
+ @document = document
18
+ end
19
+
20
+ def document
21
+ @document
22
+ end
23
+
24
+ KINDS = {
25
+ Yeptris::FFI::NODE_SCALAR => :scalar,
26
+ Yeptris::FFI::NODE_SEQUENCE => :sequence,
27
+ Yeptris::FFI::NODE_MAPPING => :mapping,
28
+ Yeptris::FFI::NODE_ALIAS => :alias,
29
+ }.freeze
30
+
31
+ def kind
32
+ KINDS[alive { Yeptris::FFI.yeptris_node_kind(@c_ptr) }]
33
+ end
34
+
35
+ def scalar?
36
+ kind == :scalar
37
+ end
38
+
39
+ def sequence?
40
+ kind == :sequence
41
+ end
42
+
43
+ def mapping?
44
+ kind == :mapping
45
+ end
46
+
47
+ def alias?
48
+ kind == :alias
49
+ end
50
+
51
+ # Scalar content / alias name (UTF-8 String), nil for collections.
52
+ def value
53
+ len = len_buffer
54
+ ptr = alive { Yeptris::FFI.yeptris_node_value(@c_ptr, len) }
55
+ return nil if ptr.null?
56
+
57
+ ptr.read_bytes(Yeptris::FFI.read_c_size_t(len)).force_encoding(Encoding::UTF_8)
58
+ end
59
+
60
+ STYLES = {
61
+ Yeptris::FFI::STYLE_PLAIN => :plain,
62
+ Yeptris::FFI::STYLE_SINGLE_QUOTED => :single_quoted,
63
+ Yeptris::FFI::STYLE_DOUBLE_QUOTED => :double_quoted,
64
+ Yeptris::FFI::STYLE_LITERAL => :literal,
65
+ Yeptris::FFI::STYLE_FOLDED => :folded,
66
+ }.freeze
67
+
68
+ def style
69
+ STYLES[alive { Yeptris::FFI.yeptris_node_style(@c_ptr) }]
70
+ end
71
+
72
+ # The out-length receiver: ONE buffer per thread, reused. A fresh
73
+ # FFI::MemoryPointer per accessor call was an allocation storm that
74
+ # stalled the Windows heap under the #182 churn loop (the #504
75
+ # family — the hang's stack sits in FFI::MemoryPointer#initialize,
76
+ # node.rb anchor/tag reads inside Integer#times with GC.start).
77
+ def len_buffer
78
+ Thread.current[:yeptris_len_buf] ||= ::FFI::MemoryPointer.new(:size_t)
79
+ end
80
+
81
+ # Explicit tag URI when present, else nil.
82
+ def tag
83
+ len = len_buffer
84
+ ptr = alive { Yeptris::FFI.yeptris_node_tag(@c_ptr, len) }
85
+ return nil if ptr.null?
86
+
87
+ ptr.read_bytes(Yeptris::FFI.read_c_size_t(len)).force_encoding(Encoding::UTF_8)
88
+ end
89
+
90
+ def anchor
91
+ len = len_buffer
92
+ ptr = alive { Yeptris::FFI.yeptris_node_anchor(@c_ptr, len) }
93
+ return nil if ptr.null?
94
+
95
+ ptr.read_bytes(Yeptris::FFI.read_c_size_t(len)).force_encoding(Encoding::UTF_8)
96
+ end
97
+
98
+ def alias_target
99
+ t = alive { Yeptris::FFI.yeptris_node_alias_target(@c_ptr) }
100
+ @document.wrap_node(t)
101
+ end
102
+
103
+ # ---- typed scalar reads (tag id decides eligibility) ----
104
+
105
+ def to_i
106
+ out = ::FFI::MemoryPointer.new(:int64)
107
+ status = alive { Yeptris::FFI.yeptris_node_int(@c_ptr, out) }
108
+ Yeptris::FFI.check_status(status, "yeptris_node_int")
109
+ out.read_int64
110
+ end
111
+
112
+ def to_f
113
+ out = ::FFI::MemoryPointer.new(:double)
114
+ status = alive { Yeptris::FFI.yeptris_node_float(@c_ptr, out) }
115
+ Yeptris::FFI.check_status(status, "yeptris_node_float")
116
+ out.read_double
117
+ end
118
+
119
+ def to_bool
120
+ out = ::FFI::MemoryPointer.new(:int)
121
+ status = alive { Yeptris::FFI.yeptris_node_bool(@c_ptr, out) }
122
+ Yeptris::FFI.check_status(status, "yeptris_node_bool")
123
+ out.read_int != 0
124
+ end
125
+
126
+ TAGS = {
127
+ Yeptris::FFI::TAG_STR => :str,
128
+ Yeptris::FFI::TAG_INT => :int,
129
+ Yeptris::FFI::TAG_FLOAT => :float,
130
+ Yeptris::FFI::TAG_BOOL => :bool,
131
+ Yeptris::FFI::TAG_NULL => :null,
132
+ Yeptris::FFI::TAG_TIMESTAMP => :timestamp,
133
+ Yeptris::FFI::TAG_BINARY => :binary,
134
+ }.freeze
135
+
136
+ def tag_id
137
+ TAGS[alive { Yeptris::FFI.yeptris_node_tag_id(@c_ptr) }]
138
+ end
139
+
140
+ # ---- sequence access ----
141
+
142
+ def size
143
+ case kind
144
+ when :sequence then seq_count
145
+ when :mapping then map_count
146
+ else 1
147
+ end
148
+ end
149
+
150
+ def seq_count
151
+ alive { Yeptris::FFI.yeptris_node_seq_count(@c_ptr) }
152
+ end
153
+
154
+ def seq_at(index)
155
+ @document.wrap_node(alive { Yeptris::FFI.yeptris_node_seq_at(@c_ptr, index) })
156
+ end
157
+
158
+ def each
159
+ return enum_for(:each) unless block_given?
160
+ raise Yeptris::Error, "#each is for sequences" unless sequence?
161
+
162
+ if Yeptris::FFI::CHILDREN_DRAIN
163
+ # one bulk walk (#168): the per-index seq_at loop paid i sibling
164
+ # hops per element — n^2/2 over an 80k-row sequence
165
+ n = alive { Yeptris::FFI.yeptris_node_seq_count(@c_ptr) }
166
+ buf = ::FFI::MemoryPointer.new(:pointer, n)
167
+ alive { Yeptris::FFI.yeptris_node_children(@c_ptr, buf, n) }
168
+ step = buf.type_size
169
+ n.times { |i| yield @document.wrap_node(buf.get_pointer(i * step)) }
170
+ else
171
+ (0...seq_count).each { |i| yield seq_at(i) }
172
+ end
173
+ end
174
+
175
+ # ---- mapping access ----
176
+
177
+ def map_count
178
+ alive { Yeptris::FFI.yeptris_node_map_count(@c_ptr) }
179
+ end
180
+
181
+ def [](key)
182
+ raise Yeptris::Error, "#[] is for mappings" unless mapping?
183
+
184
+ key = key.to_s
185
+ @document.wrap_node(
186
+ alive { Yeptris::FFI.yeptris_node_map_get(@c_ptr, key, key.bytesize) }
187
+ )
188
+ end
189
+
190
+ def key?(key)
191
+ !self[key].nil?
192
+ end
193
+
194
+ # Ordered [key, value] pairs.
195
+ def each_pair
196
+ return enum_for(:each_pair) unless block_given?
197
+ raise Yeptris::Error, "#each_pair is for mappings" unless mapping?
198
+
199
+ if Yeptris::FFI::CHILDREN_DRAIN
200
+ # key,value interleaved in one walk — the same #168 cure
201
+ pairs = alive { Yeptris::FFI.yeptris_node_map_count(@c_ptr) }
202
+ buf = ::FFI::MemoryPointer.new(:pointer, pairs * 2)
203
+ alive { Yeptris::FFI.yeptris_node_children(@c_ptr, buf, pairs * 2) }
204
+ step = buf.type_size
205
+ pairs.times do |p|
206
+ yield @document.wrap_node(buf.get_pointer((2 * p) * step)),
207
+ @document.wrap_node(buf.get_pointer((2 * p + 1) * step))
208
+ end
209
+ else
210
+ k = ::FFI::MemoryPointer.new(:pointer)
211
+ v = ::FFI::MemoryPointer.new(:pointer)
212
+ (0...map_count).each do |i|
213
+ rc = alive { Yeptris::FFI.yeptris_node_map_at(@c_ptr, i, k, v) }
214
+ next unless rc.zero?
215
+
216
+ yield @document.wrap_node(k.read_pointer), @document.wrap_node(v.read_pointer)
217
+ end
218
+ end
219
+ end
220
+
221
+ def keys
222
+ # explicit block: two-arg yield + Symbol#to_proc would call
223
+ # key.first(value) instead of taking the pair
224
+ each_pair.map { |k, _v| k }
225
+ end
226
+
227
+ # ---- construction (TODO.impl/11 phase 3; errors raise) ----
228
+
229
+ def map_add(key, node)
230
+ key = key.to_s
231
+ rc = alive { Yeptris::FFI.yeptris_node_map_add(@c_ptr, key, key.bytesize, node.c_ptr) }
232
+ Yeptris::FFI.check_status(rc, "yeptris_node_map_add")
233
+ self
234
+ end
235
+
236
+ # A pre-built key NODE ( Psych parity: nil keys ride the explicit
237
+ # `!` tag on an empty single-quoted scalar, which the byte-key form
238
+ # cannot carry)
239
+ def map_add_node(key_node, node)
240
+ rc = alive do
241
+ Yeptris::FFI.yeptris_node_map_add_node(@c_ptr, key_node.c_ptr, node.c_ptr)
242
+ end
243
+ Yeptris::FFI.check_status(rc, "yeptris_node_map_add_node")
244
+ self
245
+ end
246
+
247
+ def map_set(key, node)
248
+ key = key.to_s
249
+ rc = alive { Yeptris::FFI.yeptris_node_map_set(@c_ptr, key, key.bytesize, node.c_ptr) }
250
+ Yeptris::FFI.check_status(rc, "yeptris_node_map_set")
251
+ self
252
+ end
253
+
254
+ def map_del(key)
255
+ key = key.to_s
256
+ alive { Yeptris::FFI.yeptris_node_map_del(@c_ptr, key, key.bytesize) }.zero?
257
+ end
258
+
259
+ def seq_add(node)
260
+ rc = alive { Yeptris::FFI.yeptris_node_seq_add(@c_ptr, node.c_ptr) }
261
+ Yeptris::FFI.check_status(rc, "yeptris_node_seq_add")
262
+ self
263
+ end
264
+
265
+ # Props on synthesized nodes (15's YAMLTree); values copied in.
266
+ def set_anchor(name)
267
+ alive { Yeptris::FFI.yeptris_node_set_anchor(@c_ptr, name, name.bytesize) }.zero?
268
+ end
269
+
270
+ def set_tag(tag)
271
+ alive { Yeptris::FFI.yeptris_node_set_tag(@c_ptr, tag, tag.bytesize) }.zero?
272
+ end
273
+
274
+ def seq_del(index)
275
+ alive { Yeptris::FFI.yeptris_node_seq_del(@c_ptr, index) }.zero?
276
+ end
277
+
278
+ # ---- Ruby materialization (Psych-compatible) ----
279
+
280
+ # Materializes the subtree as native Ruby objects. Aliases preserve
281
+ # object identity (the same Ruby object for every reference), keys
282
+ # materialize with Psych's implicit typing (":sym" -> Symbol under
283
+ # compat), and the anchor memo makes cycles defined by anchors safe.
284
+ def node_id
285
+ alive { Yeptris::FFI.yeptris_node_id(@c_ptr) }
286
+ end
287
+
288
+ # The marshal fast path (TODO.restructure/21; #178): one C call
289
+ # turns this subtree into Marshal 4.8 bytes — Marshal.load builds
290
+ # the Ruby objects in C, no per-node FFI. nil when the document
291
+ # carries constructs the format cannot express (merge keys,
292
+ # timestamps, tagged revivals, quoted-symbol literals — the C
293
+ # builder names symbols from the raw text, #1304): the caller
294
+ # walks instead. Raises ParseError on engine errors (not on the
295
+ # bail).
296
+ def marshal_fast
297
+ return nil unless Yeptris::FFI::MARSHAL
298
+ return nil unless @document.marshal_lane_ok?
299
+
300
+ out_p = ::FFI::MemoryPointer.new(:pointer)
301
+ olen_p = ::FFI::MemoryPointer.new(:size_t)
302
+ st = alive { Yeptris::FFI.yeptris_marshal_node(@c_ptr, out_p, olen_p) }
303
+ if st == Yeptris::FFI::ERROR_UNSUPPORTED
304
+ nil
305
+ elsif st != Yeptris::FFI::OK
306
+ raise Yeptris::ParseError, Yeptris::FFI.last_error_message
307
+ else
308
+ bytes = out_p.read_pointer.read_bytes(Yeptris::FFI.read_c_size_t(olen_p))
309
+ bytes.force_encoding(Encoding::ASCII_8BIT)
310
+ ::Marshal.load(bytes)
311
+ end
312
+ ensure
313
+ Yeptris::FFI.yeptris_marshal_free(out_p.read_pointer) if out_p
314
+ end
315
+
316
+ def to_ruby(memo = nil)
317
+ # readonly documents memoize per node: a second materialization
318
+ # returns the SAME object (the readonly cache is the ground truth
319
+ # for alias identity across calls).
320
+ if @document.readonly?
321
+ rm = @document.readonly_memo
322
+ cached = rm[node_id]
323
+ return cached if cached
324
+ end
325
+ # Marshal fast path (TODO.restructure/21): one C call turns the
326
+ # whole subtree into Ruby objects, preserving alias identity via
327
+ # the object's own object-link table. Only at the TOP-LEVEL entry
328
+ # (memo nil): inside a recursive walk a partial marshal would
329
+ # materialize outside the shared memo and break alias identity.
330
+ # Falls back to the per-node FFI walk on constructs the format
331
+ # cannot express (merge keys, timestamps) and on older builds.
332
+ if Yeptris::FFI::MARSHAL && memo.nil?
333
+ result = marshal_fast
334
+ unless result.nil?
335
+ @document.readonly_memo[node_id] = result if @document.readonly?
336
+ return result
337
+ end
338
+ end
339
+ if @document.readonly?
340
+ return @document.readonly_memo[node_id] = to_ruby_walk({})
341
+ end
342
+ to_ruby_walk(memo || {})
343
+ end
344
+
345
+ def to_ruby_walk(memo)
346
+ cached = memo[node_id]
347
+ return cached if cached
348
+
349
+ case kind
350
+ when :mapping
351
+ h = {}
352
+ memo[node_id] = h
353
+ each_pair do |k, v|
354
+ key = k.to_ruby_key
355
+ val = v.to_ruby(memo)
356
+ if key == "<<" && k.tag != "tag:yaml.org,2002:str"
357
+ # stdlib revive_hash's merge branch: the VALUE's node kind
358
+ # picks the arm; every merge is TypeError-guarded (a bad
359
+ # element keeps the whole '<<' pair literal)
360
+ if v.kind == :alias || v.mapping?
361
+ begin
362
+ h.merge!(val)
363
+ rescue ::TypeError
364
+ h[key] = val
365
+ end
366
+ elsif v.sequence?
367
+ begin
368
+ merged = {}
369
+ val.reverse_each { |e| merged.merge!(e) }
370
+ h.merge!(merged)
371
+ rescue ::TypeError
372
+ h[key] = val
373
+ end
374
+ else
375
+ h[key] = val
376
+ end
377
+ else
378
+ h[key] = val
379
+ end
380
+ end
381
+ h
382
+ when :sequence
383
+ a = []
384
+ memo[node_id] = a
385
+ each { |e| a << e.to_ruby(memo) }
386
+ a
387
+ when :alias
388
+ t = alias_target
389
+ if t.nil?
390
+ raise ::Yeptris::Psych::AnchorNotDefined,
391
+ "Unknown anchor: #{anchor}"
392
+ end
393
+ t.to_ruby(memo)
394
+ else
395
+ scalar_to_ruby
396
+ end
397
+ end
398
+
399
+ # Mapping keys: Psych semantics — a plain scalar ":name" under the
400
+ # compat schema scans to a Symbol; everything else materializes as
401
+ # the scalar itself.
402
+ def to_ruby_key
403
+ symbol? ? symbolize : to_ruby
404
+ end
405
+
406
+ def scalar_to_ruby
407
+ return symbolize if symbol? && tag_id == :str
408
+
409
+ # !ruby/symbol (#258): psych deserializes the scalar to a Symbol
410
+ # (both spellings; safety is walk_safe's permitted_classes gate)
411
+ if tag == "!ruby/symbol" || tag == "tag:ruby.yaml.org,2002:symbol"
412
+ return value.to_s.to_sym
413
+ end
414
+
415
+ # scanner-coercion primitives (psych's ClassLoader-free path)
416
+ case tag
417
+ when "!ruby/string", "tag:ruby.yaml.org,2002:string" then return value.to_s
418
+ when "!ruby/integer", "tag:ruby.yaml.org,2002:integer" then return value.to_i
419
+ when "!ruby/float", "tag:ruby.yaml.org,2002:float" then return value.to_f
420
+ end
421
+
422
+ # stdlib deserialize's tagged-scalar arms: the class tag carries
423
+ # DateTime (dumped tagged — a plain timestamp re-loads as Time)
424
+ if tag == "!ruby/object:DateTime"
425
+ ts = value.to_s.sub(/ (\d)/, 'T\\1').sub(/ ([+-]\d)/, '\\1')
426
+ t = ::Time.xmlschema(ts)
427
+ return ::DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec,
428
+ Rational(t.utc_offset, 86_400)) + Rational(t.subsec, 86_400)
429
+ end
430
+
431
+ case tag_id
432
+ when :null then nil
433
+ when :bool then to_bool
434
+ when :int then to_i
435
+ when :float
436
+ # Psych's dot-required float applies ONLY under compat_11; the
437
+ # core schema's regexp has an optional dot (spec 10.3.2), so
438
+ # core_12 exponent-only floats materialize as Floats
439
+ text = value.to_s
440
+ if @document.parse_schema == :compat_11 &&
441
+ !text.include?(".") && !text.include?(":") && !text.start_with?(".")
442
+ text
443
+ else
444
+ to_f
445
+ end
446
+ when :timestamp then ::Yeptris::Materializer.parse_timestamp(value)
447
+ when :binary then value.unpack1("m") # ASCII-8BIT bytes (#168)
448
+ else
449
+ # beyond int64 the C resolver leaves plain scalars :str; Psych
450
+ # materializes Integer (issue #31) — rebuild from the text
451
+ text = value.to_s
452
+ if style == :plain && text.length > 18 &&
453
+ ::Yeptris::ValueML::PSYCH_INT_SHAPE.match?(text)
454
+ text.delete(",_").to_i
455
+ else
456
+ value
457
+ end
458
+ end
459
+ end
460
+
461
+ # Psych's ScalarScanner: plain ":name" (and ":", the null Symbol)
462
+ # scans to a Symbol; quoting defeats it. The name rides the ONE
463
+ # resolve law (#1304): a quoted literal form (:"x y") strips the
464
+ # quoting before resolution.
465
+ def symbol?
466
+ style == :plain && value&.start_with?(":") && !value.start_with?("::")
467
+ end
468
+
469
+ def symbolize
470
+ v = value
471
+ return :"" if v.length <= 1
472
+
473
+ ::Yeptris::Materializer.resolve_symbol(v)
474
+ end
475
+
476
+
477
+
478
+ def alive
479
+ @document.ensure_alive!
480
+ yield
481
+ end
482
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Yeptris
6
+ module Psych
7
+ # The minimal ClassLoader face the ScalarScanner port drives
8
+ # (stdlib's loader resolves through the class hierarchy; the
9
+ # scanner only needs the date class and symbolize).
10
+ class ClassLoader
11
+ def date
12
+ Date
13
+ end
14
+
15
+ def symbolize str
16
+ str.to_sym
17
+ end
18
+
19
+ def load name
20
+ Object.const_get(name)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yeptris
4
+ module Psych
5
+ # Minimal Psych::Coder stand-in (encode_with / init_with carry
6
+ # these): tag + one of scalar / seq / map. map is ordered.
7
+ class CoderShim
8
+ attr_reader :type, :tag, :scalar, :seq
9
+
10
+ def initialize(class_name = nil)
11
+ @type = :map
12
+ # Psych's default for encode_with objects: their own class tag
13
+ @tag = class_name ? "!ruby/object:#{class_name}" : nil
14
+ @scalar = nil
15
+ @seq = nil
16
+ @map = nil
17
+ @class_name = class_name
18
+ end
19
+
20
+ def tag=(t)
21
+ @tag = t
22
+ end
23
+
24
+ def scalar=(value)
25
+ @type = :scalar
26
+ @scalar = value
27
+ end
28
+
29
+ def seq=(list)
30
+ @type = :seq
31
+ @seq = list
32
+ end
33
+
34
+ def map=(hash)
35
+ @type = :map
36
+ @map = nil
37
+ hash&.each { |k, v| self[k] = v }
38
+ end
39
+
40
+ def []=(key, value)
41
+ @type = :map
42
+ @map ||= {}
43
+ @map[key.to_s] = value
44
+ end
45
+
46
+ def [](key)
47
+ @map&.[](key.to_s)
48
+ end
49
+
50
+ def each(&block)
51
+ @map&.each(&block)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The process-exclusive drop-in (issue #69): rebinds the top-level
4
+ # Psych constant to Yeptris::Psych. Once this runs, stdlib psych must
5
+ # NOT be loaded afterwards — its require would re-open THIS module and
6
+ # clobber constants ("already initialized" warnings, then
7
+ # NameError/NoMethodError at call time). Bundles that cannot
8
+ # guarantee the load order (activesupport et al.) should require
9
+ # "yeptris/psych" only and reference Yeptris::Psych directly.
10
+ require "yeptris" # the autoload declarations (Document, Node, ...) live here
11
+ require "yeptris/psych"
12
+
13
+ # ( Psych-stdlib, if already loaded, stays reachable as
14
+ # ::Psych::ORIGINAL.)
15
+ if defined?(::Psych) && !::Psych.equal?(Yeptris::Psych) &&
16
+ !Yeptris::Psych.const_defined?(:ORIGINAL, false)
17
+ Yeptris::Psych.const_set(:ORIGINAL, ::Psych)
18
+ end
19
+ # class_eval reaches Module-private methods WITHOUT send (the law: no
20
+ # send to private methods); remove_const has no public form, and the
21
+ # rebind is this file's whole purpose
22
+ Object.class_eval { remove_const(:Psych) } if defined?(::Psych) && !::Psych.equal?(Yeptris::Psych)
23
+ ::Psych = Yeptris::Psych
24
+
25
+ # #167: third-party gems feature-gate on Psych::VERSION at require
26
+ # time (mechanize 2.14.1's cookie-jar reads it in a class body). The
27
+ # face reports the STDLIB version it replaces: ORIGINAL's when psych
28
+ # was preloaded, else the default-gem spec's version (local lookup —
29
+ # requiring stdlib psych post-rebind is censored by the header).
30
+ unless Yeptris::Psych.const_defined?(:VERSION, false)
31
+ version =
32
+ if Yeptris::Psych.const_defined?(:ORIGINAL, false)
33
+ Yeptris::Psych::ORIGINAL::VERSION
34
+ else
35
+ spec = begin
36
+ ::Gem.loaded_specs["psych"] ||
37
+ ::Gem::Specification.find_by_name("psych", ::Gem::Requirement.default)
38
+ rescue ::StandardError, ::Gem::Exception, ::LoadError
39
+ nil
40
+ end
41
+ spec&.version&.to_s
42
+ end
43
+ Yeptris::Psych.const_set(:VERSION, version) if version
44
+ end
45
+
46
+ # yaml-first boot order: ::YAML still references the ORIGINAL stdlib
47
+ # module object, whose singleton methods route through stdlib's parse
48
+ # stream — the UTF-8 tagging (and every other normalization) never
49
+ # runs (#135's spec/sdo discriminant). Delegate the original's public
50
+ # singleton surface to the Yeptris face so BOTH constants behave
51
+ # identically regardless of which module object a caller holds.
52
+ # ORIGINAL exists only when stdlib psych was already loaded (the
53
+ # drop-in-before-psych order needs no delegation — nothing references
54
+ # the original yet).
55
+ if ::Yeptris::Psych.const_defined?(:ORIGINAL, false)
56
+ ::Psych::ORIGINAL.singleton_class.class_eval do
57
+ ::Psych::ORIGINAL.singleton_methods(false).each do |m|
58
+ define_method(m) do |*args, **kwargs, &block|
59
+ ::Yeptris::Psych.public_send(m, *args, **kwargs, &block)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO.restructure/23 — the typed opt-in marker for arbitrary-object
4
+ # dump/load. Classes include this module and implement
5
+ # encode_with(coder) / init_with(coder) (Psych's coder protocol).
6
+ # The visitors call those methods directly; the library never uses
7
+ # respond_to?, never reads or writes instance variables from outside
8
+ # the object's public surface (the encapsulation law).
9
+ module Yeptris
10
+ module Psych
11
+ module Encodable
12
+ end
13
+ end
14
+ end