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,670 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
5
+ require "set"
6
+
7
+ module Yeptris
8
+ module Psych
9
+ module Visitors
10
+ # The dump-side visitor (Psych's YAMLTree core): an arbitrary
11
+ # Ruby object graph becomes DOM nodes — anchors for shared
12
+ # objects, aliases for repeats, the Encodable protocol, and the
13
+ # !ruby/... tags Psych's own emitter produces.
14
+ class YAMLTree
15
+ # stdlib's factory (relaton's db cache calls it): the options
16
+ # reach the engine's emitter — :line_width and :canonical ride
17
+ # yeptris_emit_options (13B), :indentation is accepted for
18
+ # Psych parity (the engine's YAML emitter carries no
19
+ # indentation knob; emit.h's struct is size/canonical/
20
+ # best_width/explicit_doc_start).
21
+ def self.create(options = {}, emitter = nil)
22
+ new(options: options)
23
+ end
24
+
25
+ def initialize(schema: nil, options: {})
26
+ @options = options || {}
27
+ @tree = ::Yeptris::Document.create(schema: schema)
28
+ @names = {} # object_id -> anchor name
29
+ @counter = 0
30
+ end
31
+
32
+ def push(obj)
33
+ @refs = Hash.new(0)
34
+ @refs[obj.object_id] += 1 # the root counts as a reference
35
+ count_refs(obj, {})
36
+ root = visit(obj)
37
+ @tree.set_root(root) if root
38
+ self
39
+ end
40
+ alias_method :<<, :push # stdlib's dump body pushes via <<
41
+
42
+ # stdlib Psych.dump's tail: `visitor.tree.yaml io, options` —
43
+ # the tree wraps the built document; yaml serializes it
44
+ # (string return, or writes + returns the io)
45
+ def tree
46
+ BuiltDocument.new(@tree)
47
+ end
48
+
49
+ # The raw document (the CBOR encoder consumes the tree
50
+ # directly; #tree wraps it for stdlib's yaml face)
51
+ def document
52
+ @tree
53
+ end
54
+
55
+ def finish
56
+ @tree.serialize(canonical: @options[:canonical] ? true : false,
57
+ best_width: @options[:line_width] || 0,
58
+ explicit_doc_start: true)
59
+ end
60
+
61
+ # The built-document handle stdlib's dump tail serializes
62
+ class BuiltDocument
63
+ def initialize(doc)
64
+ @doc = doc
65
+ end
66
+
67
+ def yaml(io = nil, options = {})
68
+ options ||= {}
69
+ out = @doc.serialize(canonical: options[:canonical] ? true : false,
70
+ best_width: options[:line_width] || 0,
71
+ explicit_doc_start: true)
72
+ return out unless io
73
+
74
+ io.write(out)
75
+ io
76
+ end
77
+ end
78
+
79
+ # stdlib register()s every object — scalars included — so a
80
+ # repeated scalar dumps &name/*name (test_float_references,
81
+ # test_alias_with_time). Containers already route through
82
+ # anchor_for; this is the scalar arm of the same machinery.
83
+ def anchored_scalar(obj, text: nil, tag: nil)
84
+ state, name = anchor_for(obj)
85
+ return alias_of(obj, name) if state == :alias
86
+
87
+ n = scalar(text || obj, tag: tag)
88
+ n.set_anchor(name) if name
89
+ remember(obj, n)
90
+ n
91
+ end
92
+
93
+ def visit(obj)
94
+ case obj
95
+ # strings/floats/ints: stdlib yaml_tree does NOT anchor them
96
+ # (its float test is load-side only) — and CBOR encode
97
+ # rejects the anchors the cbor profile feeds it
98
+ when nil, true, false, ::Integer, ::Float, ::String then scalar(obj)
99
+ when ::Symbol then @tree.new_scalar(":#{obj}", :plain) # psych emits symbols bare, never through visit_String's quoting rules
100
+ # stdlib visit_DateTime: the class tag carries the type (a
101
+ # plain timestamp re-loads as Time) — "!ruby/object:DateTime
102
+ # 2017-04-13 12:00:00.500000000 +09:00", to_s text, one line
103
+ when ::DateTime
104
+ anchored_scalar(obj, text: obj.strftime("%F %H:%M:%S.%9N %:z"),
105
+ tag: "!ruby/object:DateTime")
106
+ when ::Date, ::Time then anchored_scalar(obj) # timestamp text, never ivars
107
+ when ::Hash then visit_hash(obj)
108
+ when ::Array then visit_array(obj)
109
+ when ::Struct then visit_struct(obj)
110
+ when ::Set then visit_set(obj)
111
+ when Encodable then visit_encode_with(obj)
112
+ else
113
+ if obj.instance_of?(::Object)
114
+ # A BARE Object has no declared state to lose — the
115
+ # empty !ruby/object form is correct by construction
116
+ # (an Object with ivars set from outside its class is
117
+ # outside the encapsulation contract; use a real class
118
+ # with Encodable for stateful objects).
119
+ return visit_bare_object(obj)
120
+ end
121
+ refuse_dump(obj)
122
+ end
123
+ end
124
+
125
+ private
126
+
127
+ # Psych anchors only objects that appear more than once;
128
+ # single-occurrence containers dump clean (no &o1 noise)
129
+ def anchor_for(obj)
130
+ oid = obj.object_id
131
+ name = @names[oid]
132
+ return [:alias, name] if name
133
+
134
+ if @refs[oid] < 2
135
+ return [:none, nil]
136
+ end
137
+ name = (@counter += 1).to_s # stdlib: &1, *1
138
+ @names[oid] = name
139
+ [:new, name]
140
+ end
141
+
142
+ def count_refs(obj, seen)
143
+ return if seen.key?(obj.object_id)
144
+
145
+ seen[obj.object_id] = true
146
+ case obj
147
+ when Hash
148
+ obj.each_value { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
149
+ when Array
150
+ obj.each { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
151
+ when Struct
152
+ obj.each_pair { |_k, v| @refs[v.object_id] += 1; count_refs(v, seen) }
153
+ when Set
154
+ obj.each { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
155
+ when ::String, ::Integer, ::Float, ::Symbol, ::Date, ::Time, true, false, nil, ::Numeric
156
+ # immutables: identity anchors are meaningless
157
+ else
158
+ unless obj.instance_of?(::Object)
159
+ refuse_dump(obj) unless obj.is_a?(Encodable)
160
+ # Encodable: descend through the PUBLIC protocol so
161
+ # shared objects inside coder contents still get
162
+ # anchors — encode_with must be pure (it runs once more
163
+ # at dump).
164
+ coder = ::Yeptris::Psych::CoderShim.new(nil)
165
+ obj.encode_with(coder)
166
+ case coder.type
167
+ when :seq
168
+ coder.seq.each { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
169
+ when :map
170
+ coder.each { |_k, v| @refs[v.object_id] += 1; count_refs(v, seen) }
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ def refuse_dump(obj)
177
+ raise ::Yeptris::DumpError,
178
+ "cannot dump #{obj.class}: include Yeptris::Psych::Encodable and implement encode_with(coder)"
179
+ end
180
+
181
+ def scalar(obj, tag: nil)
182
+ # strings route through the builder's plain-safety helper
183
+ # (one quoting rule, DRY); other scalars are their own text
184
+ text =
185
+ if obj.is_a?(::Time)
186
+ # psych-5's format_time (#300 family 1) — the FIRST cut
187
+ # put this branch after the Date||Time iso8601 one, dead
188
+ # code: Psych.dump of a Hash still emitted iso8601 (the
189
+ # neutral surface alone was pinned)
190
+ ::Yeptris::YAML::BulkBuilder.time_text(obj)
191
+ elsif obj.is_a?(::Date)
192
+ obj.iso8601 # canonical calendar form
193
+ elsif obj.nil?
194
+ "" # libyaml's null rendering rides bare (issue #290)
195
+ elsif obj.is_a?(::Float)
196
+ ::Yeptris::YAML::BulkBuilder.float_text(obj)
197
+ else
198
+ obj.to_s
199
+ end
200
+ n =
201
+ if obj.is_a?(::String) && !tag
202
+ ::Yeptris::YAML::Builder.build_string(@tree, obj)
203
+ else
204
+ @tree.new_scalar(text, :plain)
205
+ end
206
+ n.set_tag(tag) if tag
207
+ n
208
+ end
209
+
210
+ def visit_hash(hash)
211
+ state, name = anchor_for(hash)
212
+ return alias_of(hash, name) if state == :alias
213
+
214
+ m = @tree.new_mapping
215
+ remember(hash, m)
216
+ m.set_anchor(name) if name
217
+ hash.each do |k, v|
218
+ if k.nil?
219
+ # Psych's nil-key form: `! ''` — the explicit tag on an
220
+ # empty single-quoted key (#300 family 2)
221
+ key_node = @tree.new_scalar("", :single_quoted)
222
+ key_node.set_tag("!")
223
+ m.map_add_node(key_node, visit(v))
224
+ elsif k.is_a?(::String) && k == "<<"
225
+ # stdlib yaml_tree: a literal chevron key carries the
226
+ # explicit !!str tag (single-quoted) so it does not
227
+ # re-load as a merge
228
+ key_node = @tree.new_scalar("<<", :single_quoted)
229
+ key_node.set_tag("tag:yaml.org,2002:str")
230
+ m.map_add_node(key_node, visit(v))
231
+ else
232
+ # String keys ride psych's ONE visit_String law (the
233
+ # reshape words quote in key position too: "n": 1,
234
+ # 'yes': ... — #1264's premise had the psych side
235
+ # inverted; stdlib quotes these keys on every version
236
+ # since 4.0.2). Symbols stay :sym text (never through
237
+ # visit_String), matching stdlib.
238
+ key_node =
239
+ if k.is_a?(::String)
240
+ ::Yeptris::YAML::Builder.build_string(@tree, k)
241
+ else
242
+ @tree.new_scalar(key_text(k), :plain)
243
+ end
244
+ m.map_add_node(key_node, visit(v))
245
+ end
246
+ end
247
+ m
248
+ end
249
+
250
+ def visit_array(ary)
251
+ state, name = anchor_for(ary)
252
+ return alias_of(ary, name) if state == :alias
253
+
254
+ s = @tree.new_sequence
255
+ remember(ary, s)
256
+ s.set_anchor(name) if name
257
+ ary.each { |e| s.seq_add(visit(e)) }
258
+ s
259
+ end
260
+
261
+ def visit_struct(strct)
262
+ state, name = anchor_for(strct)
263
+ return alias_of(strct, name) if state == :alias
264
+
265
+ m = @tree.new_mapping
266
+ remember(strct, m)
267
+ m.set_anchor(name) if name
268
+ m.set_tag(strct.class.name.to_s.empty? ? "!ruby/struct" : "!ruby/struct:#{strct.class.name}")
269
+ strct.each_pair { |k, v| m.map_add(key_text(k), visit(v)) }
270
+ m
271
+ end
272
+
273
+ def visit_set(set)
274
+ state, name = anchor_for(set)
275
+ return alias_of(set, name) if state == :alias
276
+
277
+ s = @tree.new_sequence
278
+ remember(set, s)
279
+ s.set_anchor(name) if name
280
+ s.set_tag("!ruby/set")
281
+ set.each { |e| s.seq_add(visit(e)) }
282
+ s
283
+ end
284
+
285
+ def visit_encode_with(obj)
286
+ state, name = anchor_for(obj)
287
+ return alias_of(obj, name) if state == :alias
288
+
289
+ coder = ::Yeptris::Psych::CoderShim.new(obj.class.name)
290
+ obj.encode_with(coder) # the typed public surface
291
+ # Wrap coder contents in a __init__ sub-mapping so the load
292
+ # side can dispatch back into obj.init_with(coder) without
293
+ # library-side ivar reflection (the encapsulation law).
294
+ init = @tree.new_mapping
295
+ case coder.type
296
+ when :scalar
297
+ init.map_add("__scalar__", scalar(coder.scalar))
298
+ init.map_add("__tag__", scalar(coder.tag)) if coder.tag
299
+ when :seq
300
+ inner = @tree.new_sequence
301
+ inner.set_tag(coder.tag) if coder.tag
302
+ coder.seq.each { |e| inner.seq_add(visit(e)) }
303
+ init.seq_add(inner)
304
+ else
305
+ coder.each { |k, v| init.map_add(key_text(k), visit(v)) }
306
+ init.set_tag(coder.tag) if coder.tag
307
+ end
308
+ m = @tree.new_mapping
309
+ m.set_anchor(name) if name
310
+ custom = ::Yeptris::Psych.dump_tags[obj.class]
311
+ m.set_tag(custom || "!ruby/object:#{obj.class.name}")
312
+ m.map_add("__init__", init)
313
+ remember(obj, m)
314
+ m
315
+ end
316
+
317
+ def visit_bare_object(obj)
318
+ state, name = anchor_for(obj)
319
+ return alias_of(obj, name) if state == :alias
320
+
321
+ m = @tree.new_mapping
322
+ remember(obj, m)
323
+ m.set_anchor(name) if name
324
+ m.set_tag("!ruby/object")
325
+ m
326
+ end
327
+
328
+ def alias_of(obj, name)
329
+ @tree.new_alias(@nodes.fetch(obj.object_id), name)
330
+ end
331
+
332
+ def remember(obj, node)
333
+ (@nodes ||= {})[obj.object_id] = node
334
+ end
335
+
336
+ def key_text(k)
337
+ k.is_a?(Symbol) ? ":#{k}" : k.to_s
338
+ end
339
+
340
+ end
341
+
342
+ # Load side: revival of !ruby/... tags over the parsed Nodes
343
+ # tree. The Materializer stays the plain-data fast path.
344
+ class ToRuby
345
+ def self.visit(node)
346
+ new.visit(node)
347
+ end
348
+
349
+ # stdlib ToRuby#accept's domain post-pass: normalize the tag
350
+ # (strip leading !//, ensure tag: unless x-private) and run the
351
+ # registered block over the revived result (#95 thread)
352
+ def apply_domain_type(result, node)
353
+ return result if node.tag.nil? || node.tag.empty?
354
+
355
+ key = node.tag.sub(/\A[!\/]*/, "").sub(/(,\d+)\//, '\1:')
356
+ key = "tag:#{key}" unless key.match?(/\A(?:tag:|x-private)/)
357
+ entry = ::Yeptris::Psych.domain_types[key]
358
+ return result unless entry
359
+
360
+ _value, block = entry
361
+ block.call(key, result)
362
+ end
363
+
364
+ def visit(node)
365
+ @root ||= node
366
+ result =
367
+ case node
368
+ when ::Yeptris::Psych::Nodes::Alias then visit_alias(node)
369
+ when ::Yeptris::Psych::Nodes::Scalar then visit_scalar(node)
370
+ when ::Yeptris::Psych::Nodes::Sequence then visit_sequence(node)
371
+ when ::Yeptris::Psych::Nodes::Mapping then visit_mapping(node)
372
+ else node&.to_ruby
373
+ end
374
+ # stdlib register(): an anchored node's result is THE object
375
+ # for every alias to that anchor (scalars included)
376
+ anchors[node.anchor] = result if node.respond_to?(:anchor) && node.anchor
377
+ # the domain-types post-pass (stdlib ToRuby#accept's tail):
378
+ # registered blocks transform the revived result per node
379
+ if node && !::Yeptris::Psych.domain_types.empty?
380
+ result = apply_domain_type(result, node)
381
+ end
382
+ result
383
+ end
384
+
385
+ private
386
+
387
+ def anchors
388
+ @anchors ||= {}
389
+ end
390
+
391
+ def visit_alias(node)
392
+ name = node.anchor
393
+ unless anchors.key?(name)
394
+ target = find_anchored(root_container(node), name)
395
+ anchors[name] = target ? visit(target) : nil
396
+ end
397
+ anchors[name]
398
+ end
399
+
400
+ # the walk for alias targets starts at the first node this
401
+ # visitor was handed (the document's root)
402
+ def root_container(_node)
403
+ @root
404
+ end
405
+
406
+ def find_anchored(node, name)
407
+ return node if node.anchor == name
408
+
409
+ node.children.each do |c|
410
+ found = find_anchored(c, name)
411
+ return found if found
412
+ end
413
+ nil
414
+ end
415
+
416
+ def visit_scalar(node)
417
+ node.to_ruby
418
+ end
419
+
420
+ def visit_sequence(node)
421
+ if node.tag == "!ruby/set"
422
+ set = ::Set.new
423
+ anchors[node.anchor] = set if node.anchor
424
+ node.children.each { |c| set << visit(c) }
425
+ return set
426
+ end
427
+ ary = []
428
+ anchors[node.anchor] = ary if node.anchor
429
+ node.children.each { |c| ary << visit(c) }
430
+ ary
431
+ end
432
+
433
+ def visit_mapping(node)
434
+ tag = node.tag
435
+ return hash_into({}, node) if tag.nil? || tag.empty?
436
+
437
+ case tag
438
+ when "!ruby/set"
439
+ set = Set.new
440
+ anchors[node.anchor] = set if node.anchor
441
+ node.children.each_slice(2) { |_k, v| set << visit(v) }
442
+ set
443
+ when /\A!ruby\/struct(?::(.+))?\z/
444
+ klass = resolve_class(Regexp.last_match(1).to_s)
445
+ pairs = {}
446
+ anchors[node.anchor] = nil # placeholder: filled below
447
+ node.children.each_slice(2) do |k, v|
448
+ pairs[visit(k).to_s.sub(/\A:/, "")] = visit(v)
449
+ end
450
+ keys = pairs.keys.map(&:to_sym)
451
+ built =
452
+ if klass && klass <= Struct
453
+ klass.new(*pairs.values)
454
+ else
455
+ Struct.new(*keys).new(*pairs.values)
456
+ end
457
+ anchors[node.anchor] = built if node.anchor
458
+ built
459
+ when /\A!ruby\/object(?::(.+))?\z/
460
+ klass = resolve_class(Regexp.last_match(1).to_s)
461
+ revive_object(klass, node)
462
+ when /\A!ruby\/hash:(.+)\z/
463
+ revive_hash_class(resolve_class(Regexp.last_match(1)), node)
464
+ when /\A!ruby\/exception(?::(.*))?\z/
465
+ revive_exception(resolve_class(Regexp.last_match(1).to_s), node)
466
+ else
467
+ # Psych.load_tags first (the class registry, #95 bug 4);
468
+ # anything else stays the plain mapping
469
+ if (klass = ::Yeptris::Psych.load_tags[tag])
470
+ revive_tagged(klass, node)
471
+ else
472
+ hash_into({}, node)
473
+ end
474
+ end
475
+ end
476
+
477
+ # stdlib visit_hash's merge branch, verbatim in structure: the
478
+ # key's SHAPE decides (an explicit !!str '<<' is a literal key,
479
+ # not a merge), the VALUE's node kind picks the arm, and every
480
+ # merge is TypeError-guarded (a bad element keeps the whole
481
+ # '<<' pair literal — merges are all-or-nothing). The anchor
482
+ # registrant is the receiver by default; the object revivals
483
+ # pass the revived OBJ (psych's revive(): the object itself is
484
+ # registered before the children walk, so an alias inside an
485
+ # ivar pointing back at the object resolves to it).
486
+ def hash_into(h, node, anchor: nil)
487
+ anchors[node.anchor] = (anchor || h) if node.anchor
488
+ node.children.each_slice(2) do |k, v|
489
+ key = visit(k)
490
+ val = visit(v)
491
+ if key == "<<" && k.tag != "tag:yaml.org,2002:str"
492
+ case v
493
+ when ::Yeptris::Psych::Nodes::Alias, ::Yeptris::Psych::Nodes::Mapping
494
+ begin
495
+ h.merge!(val)
496
+ rescue ::TypeError
497
+ h[key] = val
498
+ end
499
+ when ::Yeptris::Psych::Nodes::Sequence
500
+ begin
501
+ merged = {}
502
+ val.reverse_each { |value| merged.merge!(value) }
503
+ h.merge!(merged)
504
+ rescue ::TypeError
505
+ h[key] = val
506
+ end
507
+ else
508
+ h[key] = val
509
+ end
510
+ else
511
+ h[key] = val
512
+ end
513
+ end
514
+ h
515
+ end
516
+
517
+ # A load_tags-registered class revives from the mapping's own
518
+ # top-level pairs (Psych's coder semantics — no __init__
519
+ # wrapper, which is OUR encoder's internal convention)
520
+ def revive_tagged(klass, node)
521
+ raise ::Yeptris::DumpError,
522
+ "#{klass} is not Yeptris::Psych::Encodable: implement init_with(coder)" unless klass <= ::Yeptris::Psych::Encodable
523
+
524
+ obj = klass.allocate
525
+ anchors[node.anchor] = obj if node.anchor
526
+ coder = ::Yeptris::Psych::CoderShim.new
527
+ node.children.each_slice(2) do |k, v|
528
+ coder[k.to_ruby.to_s] = visit(v)
529
+ end
530
+ obj.init_with(coder)
531
+ obj
532
+ end
533
+
534
+ def revive_object(klass, node)
535
+ obj = klass ? klass.allocate : ::Object.allocate
536
+ # psych's revive(): the object registers BEFORE the children
537
+ # walk — an alias inside the state pointing back at the
538
+ # object resolves to it (identity through ivars)
539
+ anchors[node.anchor] = obj if node.anchor
540
+
541
+ if klass
542
+ # The yeptris encoder's format (visit_encode_with): the
543
+ # Encodable protocol rides the __init__ marker, so the
544
+ # state passes back through init_with(coder) without
545
+ # library-side ivar reflection. The format decides on the
546
+ # RAW key before any value is visited (the old walk's
547
+ # shape; sibling values stay untouched when it fires).
548
+ init = nil
549
+ node.children.each_slice(2) do |k, v|
550
+ init = v if k.to_ruby.to_s == "__init__"
551
+ end
552
+ if init
553
+ unless klass <= ::Yeptris::Psych::Encodable
554
+ raise ::Yeptris::DumpError,
555
+ "#{klass} is not Yeptris::Psych::Encodable: implement init_with(coder)"
556
+ end
557
+
558
+ coder = ::Yeptris::Psych::CoderShim.new
559
+ if init.is_a?(::Yeptris::Psych::Nodes::Mapping)
560
+ init.children.each_slice(2) do |ck, cv|
561
+ coder[ck.to_ruby.to_s] = visit(cv)
562
+ end
563
+ end
564
+ obj.init_with(coder)
565
+ return obj
566
+ end
567
+ end
568
+
569
+ # stdlib psych's format (visit_Object + dump_ivars): the
570
+ # mapping's own top-level pairs ARE the object state —
571
+ # revives through init_with when the class defines it, ivar
572
+ # assignment otherwise. THE AUDIT FIX: the __init__-only
573
+ # walk silently dropped the ivars of every stdlib
574
+ # Psych.dump-produced document.
575
+ init_with_state(obj, hash_into({}, node, anchor: obj), node)
576
+ obj
577
+ end
578
+
579
+ # !ruby/hash:K (stdlib visit_Hash of a Hash subclass): the
580
+ # SUBCLASS allocates and the mapping's own pairs fill it
581
+ # (merge keys included, hash_into semantics). A name that
582
+ # resolves to a non-Hash raises stdlib resolve_subclass's
583
+ # ArgumentError; an unresolvable name stays a plain Hash (the
584
+ # resolve_class-returns-nil convention this binding already
585
+ # applies to !ruby/object and !ruby/struct — safe_load raises
586
+ # DisallowedClass for it first).
587
+ def revive_hash_class(klass, node)
588
+ if klass
589
+ unless klass <= ::Hash
590
+ raise ::ArgumentError, "Invalid tag: expected a subclass of Hash, got #{klass}"
591
+ end
592
+
593
+ h = klass.allocate
594
+ else
595
+ h = {}
596
+ end
597
+ anchors[node.anchor] = h if node.anchor
598
+ hash_into(h, node)
599
+ h
600
+ end
601
+
602
+ # !ruby/exception[:K] (stdlib visit_Exception + build_exception):
603
+ # the message rides the exception's own construction surface
604
+ # (Class#exception — psych's C build_exception writes the
605
+ # engine-internal mesg slot on an allocated instance, which
606
+ # Ruby-level ivar assignment cannot reach: message would stay
607
+ # the class name), backtrace is consumed if present, and every
608
+ # other pair lands as ivars (message/backtrace excepted — they
609
+ # name the exception's own surface, not object state). Bare
610
+ # !ruby/exception rides ::Exception (psych's class_loader
611
+ # exception fallback). A subclass whose initialize refuses a
612
+ # message argument constructs bare — same fallback psych's
613
+ # Ruby-side build_exception carried before the C move.
614
+ def revive_exception(klass, node)
615
+ h = {}
616
+ node.children.each_slice(2) do |k, v|
617
+ h[visit(k)] = visit(v)
618
+ end
619
+ mesg = h.delete("message")
620
+ e = build_exception(klass || ::Exception, mesg)
621
+ anchors[node.anchor] = e if node.anchor
622
+ e.set_backtrace(h.delete("backtrace")) if h.key?("backtrace")
623
+ init_with_state(e, h, node)
624
+ e
625
+ end
626
+
627
+ def build_exception(klass, mesg)
628
+ return klass.exception if mesg.nil?
629
+
630
+ begin
631
+ klass.exception(mesg)
632
+ rescue ::ArgumentError, ::TypeError
633
+ klass.exception
634
+ end
635
+ end
636
+
637
+ # psych's init_with: a class defining init_with takes the coder
638
+ # (the raw pair map, coder.tag = the mapping's tag); everything
639
+ # else — bare Object included — takes the pairs as ivars. Keys
640
+ # are @-normalized both ways: stdlib DUMPS the ivars @-stripped
641
+ # (yaml_tree's dump_ivars), older psych and hand-written docs
642
+ # spell them WITH the @ — both map to the same ivar.
643
+ def init_with_state(obj, h, node)
644
+ if obj.respond_to?(:init_with)
645
+ coder = ::Yeptris::Psych::CoderShim.new
646
+ coder.tag = node.tag
647
+ h.each { |k, v| coder[k] = v }
648
+ obj.init_with(coder)
649
+ else
650
+ h.each do |k, v|
651
+ obj.instance_variable_set(:"@#{k.to_s.sub(/\A@/, "")}", v)
652
+ end
653
+ end
654
+ obj
655
+ end
656
+
657
+ def resolve_class(name)
658
+ return nil if name.nil? || name.empty?
659
+
660
+ klass = ::Object.const_get(name)
661
+ raise DisallowedClass, name unless klass.is_a?(Class) || klass.is_a?(Module)
662
+
663
+ klass
664
+ rescue ::NameError
665
+ nil
666
+ end
667
+ end
668
+ end
669
+ end
670
+ end