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,502 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yeptris"
4
+
5
+ module Yeptris
6
+ # The STRICT JSON surface (TODO.restructure/31).
7
+ #
8
+ # `Yeptris::JSON.load` targets EXACT `JSON.parse` semantics — that
9
+ # is its parity target, spec-pinned (spec/json_parity_spec.rb).
10
+ # It is deliberately separate from Yeptris::YAML: the YAML surface
11
+ # keeps the Psych contract for every input (JSON-shaped included),
12
+ # so the two never drift into each other's semantics.
13
+ #
14
+ # Engines, fastest first (all exact — the parity spec runs against
15
+ # whichever is loaded):
16
+ # 1. the native materializer (fused C scan → VALUE; opt-in build)
17
+ # 2. the marshal engine (#278): strict-JSON parse + Marshal 4.8
18
+ # emission + ONE core Marshal.load — all C, no per-value Ruby
19
+ # (engine 0.6.34+; the strict flag follows the resolved json gem)
20
+ # 3. the record drain + strict conversion walk (always available)
21
+ module JSON
22
+ class Error < ::Yeptris::Error; end
23
+ class ParseError < Error; end
24
+
25
+ autoload :Descriptor, "yeptris/json/descriptor"
26
+
27
+ # json gem 3.0 made duplicate keys an error by DEFAULT; 2.x is
28
+ # last-wins. The parity target is the RESOLVED json gem's own
29
+ # behavior — strictness follows it (issue #37, found by canon's
30
+ # CI where json 3.0.0 resolved while the dev box had 2.x).
31
+ require "json"
32
+ require "date" # place_obj's `when Date` — psych loads it elsewhere, JSON.dump must not depend on that
33
+ STRICT_DUPLICATE_KEYS = Gem::Version.new(::JSON::VERSION) >= Gem::Version.new("3")
34
+
35
+ module_function
36
+
37
+ # allow_duplicate_key: opts out of the resolved json gem's strict
38
+ # duplicate-key contract (json >= 3 rejects by default) — last value
39
+ # wins, exactly the historical JSON.parse contract. System-boundary
40
+ # parsers need it: third-party feeds (Crossref book records keyed by
41
+ # ISBN, NIST's pubs export keyed by "&nbsp", the UN search API's blank
42
+ # keys) genuinely carry duplicates, and rejecting them breaks the fetch
43
+ # rather than flagging the data (relaton/relaton#413, the ask in
44
+ # yeptris-ruby#335). Default stays the json-gem-parity strictness.
45
+ def load(source, allow_duplicate_key: false)
46
+ strict = STRICT_DUPLICATE_KEYS && !allow_duplicate_key
47
+ source = ::Yeptris.read_input(source)
48
+ source = source.to_s
49
+ if defined?(::Yeptris::Native)
50
+ begin
51
+ return ::Yeptris::Native.load_json(source, strict)
52
+ rescue ::Yeptris::ParseError => e
53
+ raise ParseError, e.message
54
+ end
55
+ end
56
+ # yeptris-ruby#278: with no Ruby-version-matched native build the
57
+ # pure-Ruby walk measured 0.11x vs JSON.parse on real documents —
58
+ # the marshal engine keeps the fallback C-side end to end.
59
+ # STRICT_DUPLICATE_KEYS rides the flags: json >= 3's default
60
+ # contract raises on duplicates. The flags were hardcoded to 0
61
+ # here, so every lane without the native ext (windows-latest has
62
+ # none) answered last-wins under json >= 3 — found by canon's
63
+ # dependent CI, whose parity pin now names the lane.
64
+ return marshal_engine(
65
+ source, strict ? ::Yeptris::FFI::MARSHAL_STRICT_DUP_KEYS : 0
66
+ ) if ::Yeptris::FFI::MARSHAL_STRICT_DUP
67
+ tape_engine(source, strict)
68
+ end
69
+
70
+ # The marshal engine (#278): yeptris_parse_json gates the strict
71
+ # grammar, yeptris_marshal_node_ex writes the Ruby Marshal 4.8
72
+ # stream (STRICT_DUP_KEYS raising on json 3's default contract),
73
+ # and ONE core Marshal.load materializes the graph. Returns nil
74
+ # when the engine cannot serve the route (caller keeps walking);
75
+ # every parse/dup failure raises ParseError.
76
+ def marshal_engine(source, flags)
77
+ begin
78
+ gate = ::Yeptris::Document.parse_json(source)
79
+ rescue ::Yeptris::ParseError => e
80
+ raise ParseError, e.message
81
+ end
82
+ begin
83
+ out_p = ::FFI::MemoryPointer.new(:pointer)
84
+ olen_p = ::FFI::MemoryPointer.new(:size_t)
85
+ st = ::Yeptris::FFI.yeptris_marshal_node_ex(
86
+ ::Yeptris::FFI.yeptris_document_root(gate.c_ptr, 0), flags, out_p, olen_p
87
+ )
88
+ if st != ::Yeptris::FFI::OK
89
+ raise ParseError, ::Yeptris::FFI.last_error_message if st == ::Yeptris::FFI::ERROR_PARSE
90
+
91
+ return nil # UNSUPPORTED: merge keys et al. cannot occur in JSON
92
+ end
93
+ buf = out_p.read_pointer
94
+ data = buf.read_bytes(::Yeptris::FFI.read_c_size_t(olen_p))
95
+ data.force_encoding(Encoding::ASCII_8BIT)
96
+ ::Yeptris::FFI.yeptris_marshal_free(buf)
97
+ Marshal.load(data)
98
+ ensure
99
+ gate.free
100
+ end
101
+ end
102
+
103
+ # The tape engine (issue #81): ONE call validates and materializes
104
+ # the record stream; the walk reads four bulk columns and slices
105
+ # strings straight out of the caller's source — no gate document,
106
+ # no recorder transform, no arena copy (the drain path paid all
107
+ # three, which is where medium/large throughput went).
108
+ T_DOC = 0
109
+ T_NULL = 1
110
+ T_TRUE = 2
111
+ T_FALSE = 3
112
+ T_INT = 4
113
+ T_FLOAT = 5
114
+ T_STR = 6
115
+ T_SEQ_OPEN = 7
116
+ T_MAP_OPEN = 8
117
+ T_CLOSE = 9
118
+
119
+ # The tape keeps RAW string spans (escapes untouched — the C
120
+ # contract); the walk decodes them here. The hot path (no
121
+ # backslash) returns the slice untouched.
122
+ SIMPLE_ESCAPES = {
123
+ '"' => '"'.b, "\\" => "\\".b, "/" => "/".b, "b" => "\b".b, "f" => "\f".b,
124
+ "n" => "\n".b, "r" => "\r".b, "t" => "\t".b
125
+ }.freeze
126
+
127
+ def decode_span(src, off, len)
128
+ s = src.byteslice(off, len)
129
+ s.force_encoding(Encoding::UTF_8)
130
+ return s unless s.include?("\\")
131
+
132
+ b = s.b
133
+ out = String.new(encoding: Encoding::BINARY)
134
+ i = 0
135
+ n = b.bytesize
136
+ while i < n
137
+ c = b.getbyte(i)
138
+ if c != 0x5C || i + 1 >= n
139
+ out << c
140
+ i += 1
141
+ next
142
+ end
143
+ e = b.getbyte(i + 1)
144
+ if e != 0x75 # simple escape
145
+ ch = e.chr
146
+ out << (SIMPLE_ESCAPES[ch] || ch)
147
+ i += 2
148
+ next
149
+ end
150
+ cp = b.byteslice(i + 2, 4).to_i(16)
151
+ if cp >= 0xD800 && cp <= 0xDBFF && i + 12 <= n &&
152
+ b.getbyte(i + 6) == 0x5C && b.getbyte(i + 7) == 0x75
153
+ lo = b.byteslice(i + 8, 4).to_i(16)
154
+ if lo >= 0xDC00 && lo <= 0xDFFF
155
+ cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00)
156
+ i += 12
157
+ else
158
+ i += 6
159
+ end
160
+ else
161
+ i += 6
162
+ end
163
+ out << [cp].pack("U").b
164
+ end
165
+ out.force_encoding(Encoding::UTF_8)
166
+ end
167
+
168
+ def tape_engine(source, strict_dup = STRICT_DUPLICATE_KEYS)
169
+ tape = ::Yeptris::FFI::JsonTape.new
170
+ rc = ::Yeptris::FFI.yeptris_parse_json_tape(source, source.bytesize, tape)
171
+ raise ParseError, ::Yeptris::FFI.last_error_message if rc != ::Yeptris::FFI::OK
172
+
173
+ begin
174
+ walk_tape(source, tape, strict_dup)
175
+ ensure
176
+ ::Yeptris::FFI.yeptris_tape_free(tape)
177
+ end
178
+ end
179
+
180
+ def walk_tape(src, tape, strict_dup)
181
+ n = tape[:count]
182
+ kinds = tape[:kinds].read_bytes(n).unpack("C*")
183
+ offs = tape[:offs].read_bytes(n * 4).unpack("V*")
184
+ lens = tape[:lens].read_bytes(n * 4).unpack("V*")
185
+ # v2: parse records number spans only — ONE bulk convert call
186
+ # materializes them (per-number FFI calls would sink the ladder)
187
+ ivp = ::FFI::MemoryPointer.new(:int64, n, true)
188
+ dvp = ::FFI::MemoryPointer.new(:double, n, true)
189
+ ::Yeptris::FFI.yeptris_tape_convert(tape, 0, n, ivp, dvp)
190
+ vals_i = ivp.read_bytes(n * 8).unpack("q<*")
191
+ vals_f = dvp.read_bytes(n * 8).unpack("E*")
192
+ # any integer text beyond int64: every INT rebuilds from its span
193
+ # (exact Bignum; JSON.parse parity) — int_min is set by the
194
+ # convert call above
195
+ exact_ints = !tape[:int_min].zero?
196
+ utf8 = src.encoding == Encoding::UTF_8
197
+
198
+ docs = [nil] # record 0 (DOC) pre-consumed — the root's slot
199
+ stack = []
200
+ key_sets = strict_dup ? [{}] : nil
201
+ pending_key = nil
202
+ i = 1
203
+ while i < n
204
+ case kinds[i]
205
+ when T_SEQ_OPEN
206
+ place(docs, stack, pending_key) { [] }
207
+ pending_key = nil
208
+ when T_MAP_OPEN
209
+ key_sets&.push({})
210
+ place(docs, stack, pending_key) { {} }
211
+ pending_key = nil
212
+ when T_CLOSE
213
+ # Key frames are pushed per MAP_OPEN — an ARRAY close must
214
+ # not pop the enclosing map's frame (issue found by ea's CI:
215
+ # {"a":[1],"b":[2],"c":[3]} exhausted the frames and the next
216
+ # map key hit key_sets.last.key? on nil under strict mode).
217
+ key_sets&.pop if stack.last.is_a?(Hash)
218
+ stack.pop
219
+ when T_STR
220
+ # empty-string literals are frozen+shared since Ruby 3.4
221
+ # (and `+""` binds after the method chain anyway), so the
222
+ # fresh copy must come from the encoding call itself
223
+ text = lens[i] == 0 ? String.new(encoding: Encoding::UTF_8) : decode_span(src, offs[i], lens[i])
224
+ if pending_key.nil? && !stack.empty? && stack.last.is_a?(Hash)
225
+ if key_sets && key_sets.last.key?(text)
226
+ raise ParseError, %(duplicate key "#{text}" in JSON object)
227
+ end
228
+ key_sets&.last&.store(text, true)
229
+ pending_key = text
230
+ else
231
+ place(docs, stack, pending_key) { text }
232
+ pending_key = nil
233
+ end
234
+ when T_INT
235
+ if exact_ints
236
+ place(docs, stack, pending_key) { Integer(src.byteslice(offs[i], lens[i]), 10) }
237
+ else
238
+ v = vals_i[i]
239
+ place(docs, stack, pending_key) { v }
240
+ end
241
+ pending_key = nil
242
+ when T_FLOAT
243
+ v = vals_f[i]
244
+ place(docs, stack, pending_key) { v }
245
+ pending_key = nil
246
+ when T_TRUE, T_FALSE # v2: the kind IS the value
247
+ place(docs, stack, pending_key) { kinds[i] == T_TRUE }
248
+ pending_key = nil
249
+ when T_NULL
250
+ place(docs, stack, pending_key) { nil }
251
+ pending_key = nil
252
+ else
253
+ raise Error, "internal: impossible tape record #{kinds[i]}"
254
+ end
255
+ i += 1
256
+ end
257
+ docs.empty? ? nil : docs.first
258
+ end
259
+
260
+ # The always-available engine: the strict-JSON validator gates
261
+ # (parse_json raises on anything RFC 8259 rejects), then the
262
+ # value records convert WITHOUT the Psych quirk table — floats
263
+ # are always Floats, bools always bools ("1e3" is 1000.0 here
264
+ # and a String on the YAML surface; each surface is its own
265
+ # contract).
266
+ def strict_fallback(source)
267
+ begin
268
+ gate = ::Yeptris::Document.parse_json(source)
269
+ rescue ::Yeptris::ParseError => e
270
+ raise ParseError, e.message
271
+ end
272
+ begin
273
+ if ::Yeptris::FFI::MARSHAL_STRICT_DUP
274
+ routed = marshal_engine(
275
+ source, STRICT_DUPLICATE_KEYS ? ::Yeptris::FFI::MARSHAL_STRICT_DUP_KEYS : 0
276
+ )
277
+ return routed unless routed.nil?
278
+ end
279
+ cols = ::Yeptris::FFI::ValueColumns.new
280
+ st = ::Yeptris::FFI.yeptris_value_drain_columns(
281
+ source, source.bytesize, ::Yeptris::FFI::SCHEMA_12_CORE, cols
282
+ )
283
+ raise ParseError, ::Yeptris::FFI.last_error_message if st != ::Yeptris::FFI::OK
284
+
285
+ begin
286
+ walk_strict(cols, STRICT_DUPLICATE_KEYS)
287
+ ensure
288
+ ::Yeptris::FFI.yeptris_value_free_columns(cols)
289
+ end
290
+ ensure
291
+ gate.free
292
+ end
293
+ end
294
+
295
+ # Placement mechanics mirror ValueML.walk_columns; the CONVERSION
296
+ # is the strict-JSON one (no ':sym' scan, no y/n quirk, no
297
+ # dot-required floats). Anchors/aliases/timestamps cannot occur
298
+ # in strict JSON — reaching them is an internal error.
299
+ def walk_strict(cols, strict_dup = STRICT_DUPLICATE_KEYS)
300
+ n = cols[:count]
301
+ kinds = cols[:kinds].read_bytes(n).unpack("C*")
302
+ ikeys = cols[:is_keys].read_bytes(n).unpack("C*")
303
+ bools = cols[:bools].read_bytes(n).unpack("C*")
304
+ offs = cols[:offs].read_bytes(n * 4).unpack("V*")
305
+ lens = cols[:lens].read_bytes(n * 4).unpack("V*")
306
+ pays = cols[:payloads].read_bytes(n * 8).unpack("q<*")
307
+ arena = cols[:arena_len].zero? ? +"" : cols[:arena].read_bytes(cols[:arena_len])
308
+ arena.force_encoding(Encoding::UTF_8)
309
+
310
+ docs = []
311
+ stack = []
312
+ key_sets = strict_dup ? [{}] : nil
313
+ pending_key = nil
314
+ i = 0
315
+ while i < n
316
+ case kinds[i]
317
+ when ValueML::DOC
318
+ docs.push(nil)
319
+ when ValueML::SEQ_OPEN
320
+ place(docs, stack, pending_key) { [] }
321
+ pending_key = nil
322
+ when ValueML::MAP_OPEN
323
+ key_sets&.push({})
324
+ place(docs, stack, pending_key) { {} }
325
+ pending_key = nil
326
+ when ValueML::CLOSE
327
+ key_sets&.pop if stack.last.is_a?(Hash)
328
+ stack.pop
329
+ when ValueML::V_STR
330
+ text = arena.byteslice(offs[i], lens[i])
331
+ if ikeys[i] == 1 && !stack.empty? && stack.last.is_a?(Hash)
332
+ if strict_dup && key_sets.last.key?(text)
333
+ raise ParseError, %(duplicate key "#{text}" in JSON object)
334
+ end
335
+ key_sets&.last&.store(text, true)
336
+ pending_key = text
337
+ else
338
+ # Records carry int64 payloads: an integer-beyond-int64
339
+ # degrades to a PLAIN string (b==1). In strict JSON a
340
+ # plain (unquoted) scalar can ONLY be a number — every
341
+ # real string is quoted and arrives b==0 — so rebuild the
342
+ # exact Integer (JSON.parse parity, Bignum included).
343
+ if bools[i] == 1
344
+ place(docs, stack, pending_key) { Integer(text, 10) }
345
+ else
346
+ place(docs, stack, pending_key) { text }
347
+ end
348
+ pending_key = nil
349
+ end
350
+ when ValueML::V_INT
351
+ place(docs, stack, pending_key) { pays[i] }
352
+ pending_key = nil
353
+ when ValueML::V_FLOAT
354
+ place(docs, stack, pending_key) { [pays[i]].pack("q<").unpack1("E") }
355
+ pending_key = nil
356
+ when ValueML::V_BOOL
357
+ place(docs, stack, pending_key) { bools[i] == 1 }
358
+ pending_key = nil
359
+ when ValueML::V_NULL
360
+ place(docs, stack, pending_key) { nil }
361
+ pending_key = nil
362
+ else
363
+ raise Error, "internal: impossible record #{kinds[i]} in strict JSON"
364
+ end
365
+ i += 1
366
+ end
367
+ docs.empty? ? nil : docs.first
368
+ end
369
+
370
+ # ---- generation (issue #81) ------------------------------------------
371
+ #
372
+ # `dump` targets JSON.generate semantics for the core types: the
373
+ # object walk reuses YAML.dump's packed-entry bulk builder (one
374
+ # FFI build call), then the strict-JSON writer serializes —
375
+ # Strings ride DOUBLE_QUOTED (a "42" stays quoted; numbers, bools
376
+ # and null stay plain), Symbols become their name.
377
+ class DumpError < Error; end
378
+
379
+ def dump(obj, pretty: false, indent: " ")
380
+ # yeptris-ruby#322: the marshal route — Marshal.dump walks the
381
+ # host tree IN C (3.3 ms vs place_obj's 12.3 ms on the 200 KB
382
+ # corpus) and the engine's marshal reader emits JSON from those
383
+ # bytes in one call. The per-node place_obj walk remains for
384
+ # pretty (the reader emits compact/spaced only), for engines
385
+ # without the face, and for marshal-undumpable hosts.
386
+ unless pretty
387
+ r = marshal_json_dump(obj)
388
+ return r unless r.nil?
389
+ end
390
+ parts = String.new(encoding: Encoding::BINARY,
391
+ capacity: 12 * 16) # 12-byte entries, appended in place
392
+ blob = String.new(encoding: Encoding::BINARY)
393
+ off = [0]
394
+ place_obj(obj, parts, blob, off)
395
+ doc = ::Yeptris::Document.create
396
+ # FFI passes String args BY REFERENCE — no MemoryPointer copies
397
+ rc = doc.build_entries(parts, parts.bytesize / 12, blob, blob.bytesize)
398
+ raise DumpError, "document_build failed: #{rc}" unless rc == ::Yeptris::FFI::OK
399
+ out =
400
+ if pretty
401
+ unless ::Yeptris::FFI::JSON_PRETTY
402
+ raise DumpError, "pretty JSON needs a libyeptris with yeptris_serialize_json_pretty"
403
+ end
404
+ doc.serialize_json_pretty(indent)
405
+ elsif ::Yeptris::FFI::JSON_EX
406
+ doc.serialize_json_compact
407
+ else
408
+ doc.serialize_json
409
+ end
410
+ # JSON.generate emits no trailing newline (the document writer does)
411
+ out.end_with?("\n") ? out[0, out.bytesize - 1] : out
412
+ ensure
413
+ doc&.free
414
+ end
415
+
416
+ # yeptris-ruby#322: Marshal.dump bytes -> JSON text in ONE C call
417
+ # (yeptris_json_from_marshal, engine 0.6.45+). Returns nil when
418
+ # the route is unavailable (no face, or the engine answered
419
+ # UNSUPPORTED — objects, structs, wide bignums) and the caller
420
+ # falls back to the per-node walk. Marshal-undumpable hosts raise
421
+ # TypeError, which the caller's own contract surfaces anyway.
422
+ def marshal_json_dump(obj)
423
+ return nil unless defined?(::Yeptris::FFI::MARSHAL_JSON) &&
424
+ ::Yeptris::FFI::MARSHAL_JSON
425
+ m = Marshal.dump(obj)
426
+ out_p = ::FFI::MemoryPointer.new(:pointer)
427
+ olen_p = ::FFI::MemoryPointer.new(:size_t)
428
+ st = ::Yeptris::FFI.yeptris_json_from_marshal(
429
+ m, m.bytesize, 1, out_p, olen_p
430
+ )
431
+ return nil if st == ::Yeptris::FFI::ERROR_UNSUPPORTED
432
+ raise ParseError, ::Yeptris::FFI.last_error_message unless st == ::Yeptris::FFI::OK
433
+ buf = out_p.read_pointer
434
+ data = buf.read_bytes(::Yeptris::FFI.read_c_size_t(olen_p))
435
+ ::Yeptris::FFI.yeptris_marshal_free(buf)
436
+ data.force_encoding(Encoding::UTF_8)
437
+ data
438
+ end
439
+
440
+ F = ::Yeptris::FFI
441
+ MAP_E = [F::BUILD_MAP, 0, 0, 0].pack("CCx2VV").freeze
442
+ SEQ_E = [F::BUILD_SEQ, 0, 0, 0].pack("CCx2VV").freeze
443
+ END_E = [F::BUILD_END, 0, 0, 0].pack("CCx2VV").freeze
444
+ DQ = F::STYLE_DOUBLE_QUOTED
445
+ PL = F::STYLE_PLAIN
446
+ SC_DQ = [F::BUILD_SCALAR, DQ].pack("CCx2").freeze
447
+ SC_PL = [F::BUILD_SCALAR, PL].pack("CCx2").freeze
448
+
449
+ def place_obj(obj, parts, blob, off)
450
+ case obj
451
+ when Hash
452
+ parts << MAP_E
453
+ obj.each do |k, v|
454
+ key = k.is_a?(String) ? k : k.is_a?(Symbol) ? k.name : k.to_s
455
+ scalar_json(key, SC_DQ, parts, blob, off)
456
+ place_obj(v, parts, blob, off)
457
+ end
458
+ parts << END_E
459
+ when Array
460
+ parts << SEQ_E
461
+ obj.each { |e| place_obj(e, parts, blob, off) }
462
+ parts << END_E
463
+ when String
464
+ scalar_json(obj, SC_DQ, parts, blob, off)
465
+ when Symbol
466
+ scalar_json(obj.name, SC_DQ, parts, blob, off)
467
+ when Integer, Float
468
+ scalar_json(obj.to_s, SC_PL, parts, blob, off)
469
+ when true
470
+ scalar_json("true", SC_PL, parts, blob, off)
471
+ when false
472
+ scalar_json("false", SC_PL, parts, blob, off)
473
+ when nil
474
+ scalar_json("null", SC_PL, parts, blob, off)
475
+ when Date, Time
476
+ scalar_json(obj.iso8601, SC_DQ, parts, blob, off)
477
+ else
478
+ raise DumpError, "cannot dump #{obj.class}: unsupported object " \
479
+ "(JSON.generate calls to_json on custom types)"
480
+ end
481
+ end
482
+
483
+ def scalar_json(text, prefix, parts, blob, off)
484
+ parts << (prefix + [off[0], text.bytesize].pack("VV"))
485
+ blob << text
486
+ off[0] += text.bytesize
487
+ end
488
+
489
+ def place(docs, stack, key)
490
+ v = yield
491
+ if stack.empty?
492
+ docs[-1] = v
493
+ elsif key
494
+ stack.last[key] = v
495
+ else
496
+ stack.last.push(v)
497
+ end
498
+ stack.push(v) if v.is_a?(Array) || v.is_a?(Hash)
499
+ v
500
+ end
501
+ end
502
+ end