yeptris 0.6.65.0-powerpc64le-linux → 0.6.66.1-powerpc64le-linux

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f67ab2eb08a370f656b737e652678325924f8251879387ab2c1fdb7ae45ddbf1
4
- data.tar.gz: f8408f964d5262495987f55cdfe8a37537e21c72c16c75ef1dd1fbea4367a7eb
3
+ metadata.gz: 5418ccee21cd870718c3c849224e98626245572a760a44111752f4b7abebd249
4
+ data.tar.gz: 97cb842294f1cc00c1b549d3fcfd6932403ab7aff797a22f35d2af103b52e79f
5
5
  SHA512:
6
- metadata.gz: 4c401a4ec34e345e79b77db608076f3b0198d1158abf0879d76a95650f9d8106d97b150955f40b0af4cbf4917210bed75802e6f449fc4ad6628f112d0d406518
7
- data.tar.gz: 478cbaaaa8cbc0535797d6223d1059d7a5e8e1d29516a77550fe520f8750f1e4a07a33efa55f0d543432f0502e73e96d166babe79eb56adf16742f31a211f636
6
+ metadata.gz: 9f02761d4df43391934c5903d37827507ae2eeb6c123e6165aba8723375c18993c09ee5fc8e254466f5dd66bf688884966dedba73513ff3c9525c666e95d07f5
7
+ data.tar.gz: d3e2c9fd398c510ab8caee418b3cfcd627936ee9ae9f1e45941cc951e51dc506244a0ae5954b68b46224762f4725cb1e920e20bff604318a0d9370027329f39f
@@ -11,8 +11,8 @@
11
11
 
12
12
  #define YEPTRIS_VERSION_MAJOR 0
13
13
  #define YEPTRIS_VERSION_MINOR 6
14
- #define YEPTRIS_VERSION_PATCH 65
15
- #define YEPTRIS_VERSION_STRING "0.6.65"
14
+ #define YEPTRIS_VERSION_PATCH 66
15
+ #define YEPTRIS_VERSION_STRING "0.6.66"
16
16
 
17
17
  #ifdef __cplusplus
18
18
  extern "C" {
data/lib/yeptris/ffi.rb CHANGED
@@ -288,6 +288,10 @@ module Yeptris
288
288
  end
289
289
  attach_function :yeptris_document_build,
290
290
  %i[yeptris_document pointer size_t pointer size_t], :int
291
+ # the bulk build's probe (the MARSHAL/MARSHAL_JSON pattern, engine
292
+ # 0.6.45+): YAMLTree's node-free dump lane (#857) gates on it — an
293
+ # engine older than the bulk build keeps the per-node walk
294
+ BULK_BUILD = !(@missing ||= []).include?(:yeptris_document_build)
291
295
 
292
296
  attach_function :yeptris_serialize, %i[yeptris_document pointer], :pointer
293
297
  attach_function :yeptris_serialize_ex,
@@ -249,6 +249,14 @@ module Yeptris
249
249
  walk(flat, arena)
250
250
  end
251
251
 
252
+ # The compositional seam for hosts that need the records BEFORE the
253
+ # build (the #862 facade gate: one drain feeds the permission check
254
+ # AND the materialization — no per-node FFI, no second parse).
255
+ # Arguments are exactly what Materializer.drain returns.
256
+ def materialize_records(flat, arena)
257
+ walk(flat, arena)
258
+ end
259
+
252
260
  private
253
261
 
254
262
  # The stack machine: containers on a stack, a pending-key slot per
@@ -257,9 +265,10 @@ module Yeptris
257
265
  # (<<) resolve inline: existing keys win, sequences merge in
258
266
  # order — Psych load-time semantics.
259
267
  #
260
- # flat: one Integer per unpack field, 12 per record:
261
- # 0 type, 1 style, 2 flags, 3 tag_id, 4 line, 5 col, 6 value_off,
262
- # 7 value_len, 8 anchor_off, 9 anchor_len, 10 tag_off, 11 tag_len.
268
+ # flat: one Integer per unpack field, 14 per record (RECORD_UNPACK
269
+ # = C4V10): 0 type, 1 style, 2 flags, 3 tag_id, 4 line, 5 col,
270
+ # 6 end_line, 7 end_col (#179), 8 value_off, 9 value_len,
271
+ # 10 anchor_off, 11 anchor_len, 12 tag_off, 13 tag_len.
263
272
  def walk(flat, arena)
264
273
  docs = []
265
274
  stack = []
@@ -272,7 +281,10 @@ module Yeptris
272
281
 
273
282
  # the arena is UTF-8 by construction (validated at parse), so
274
283
  # forcing its encoding ONCE makes every slice UTF-8 for free —
275
- # no per-scalar force_encoding
284
+ # no per-scalar force_encoding. The record offsets are BYTES:
285
+ # slice with byteslice, never String#[] — on a UTF-8 String that
286
+ # counts characters and drifts after the first multibyte char
287
+ # (#1359)
276
288
  arena.force_encoding(Encoding::UTF_8)
277
289
  # (each_slice DESTRUCTURING measured SLOWER than plain indexing
278
290
  # here: it allocates a 12-slot Array per record. Index away.)
@@ -287,10 +299,18 @@ module Yeptris
287
299
  # not the text
288
300
  tag_id = flat[i + 3]
289
301
  v = Materializer.scan_by_tag(
290
- arena[flat[i + 8], flat[i + 9]], tag_id, (flat[i + 2] & EF_IMPLICIT) != 0
302
+ arena.byteslice(flat[i + 8], flat[i + 9]), tag_id, (flat[i + 2] & EF_IMPLICIT) != 0
291
303
  )
304
+ # !ruby/symbol (#258): the tag TEXT (not the resolver tag_id)
305
+ # decides — psych deserializes the scalar to a Symbol, the
306
+ # same arm the DOM walk carries (Node#scalar_to_ruby)
307
+ tl = flat[i + 13]
308
+ if tl != 0
309
+ t = arena.byteslice(flat[i + 12], tl)
310
+ v = v.to_sym if t == "!ruby/symbol" || t == "tag:ruby.yaml.org,2002:symbol"
311
+ end
292
312
  l = flat[i + 11]
293
- anchors[arena[flat[i + 10], l]] = v if l != 0
313
+ anchors[arena.byteslice(flat[i + 10], l)] = v if l != 0
294
314
  # place(): the scalar fast path inlined — the overwhelming
295
315
  # majority of events land here
296
316
  if stack.empty?
@@ -304,7 +324,7 @@ module Yeptris
304
324
  else
305
325
  pending_key[-1] = nil
306
326
  if pending_key_merge[-1]
307
- merge_into(parent, v)
327
+ Materializer.merge_into(parent, v)
308
328
  else
309
329
  parent[key] = v
310
330
  end
@@ -316,7 +336,7 @@ module Yeptris
316
336
  when MAPPING_START
317
337
  h = {}
318
338
  l = flat[i + 11]
319
- anchors[arena[flat[i + 10], l]] = h if l != 0
339
+ anchors[arena.byteslice(flat[i + 10], l)] = h if l != 0
320
340
  merge_target.push(place(docs, stack, pending_key, pending_key_merge, h))
321
341
  stack.push(h)
322
342
  pending_key.push(nil)
@@ -324,14 +344,19 @@ module Yeptris
324
344
  when SEQUENCE_START
325
345
  a = []
326
346
  l = flat[i + 11]
327
- anchors[arena[flat[i + 10], l]] = a if l != 0
347
+ anchors[arena.byteslice(flat[i + 10], l)] = a if l != 0
328
348
  merge_target.push(place(docs, stack, pending_key, pending_key_merge, a))
329
349
  stack.push(a)
330
350
  pending_key.push(nil)
331
351
  pending_key_merge.push(nil)
332
352
  when ALIAS
333
- name = arena[flat[i + 8], flat[i + 9]]
334
- raise Yeptris::ParseError, "unknown anchor: #{name.inspect}" unless anchors.key?(name)
353
+ name = arena.byteslice(flat[i + 8], flat[i + 9])
354
+ # the engine DOM parse's spelling, with the record's own
355
+ # location — the ONE undefined-anchor phrase every face
356
+ # raises (the facade's AnchorNotDefined lift matches on it)
357
+ raise Yeptris::ParseError,
358
+ "alias references an undefined anchor at line #{flat[i + 4]}, " \
359
+ "column #{flat[i + 5]}: #{name.inspect}" unless anchors.key?(name)
335
360
 
336
361
  place(docs, stack, pending_key, pending_key_merge, anchors[name])
337
362
  when MAPPING_END, SEQUENCE_END
@@ -339,7 +364,7 @@ module Yeptris
339
364
  pending_key.pop
340
365
  pending_key_merge.pop
341
366
  target = merge_target.pop
342
- merge_into(target, done) if target
367
+ Materializer.merge_into(target, done) if target
343
368
  when DOCUMENT_START
344
369
  # anchors are per-document (the engine memo law; PyYAML's
345
370
  # composer resets per document): a later document's alias
@@ -373,7 +398,7 @@ module Yeptris
373
398
  key = pending_key.last
374
399
  pending_key[-1] = nil
375
400
  if pending_key_merge[-1]
376
- merge_into(parent, obj)
401
+ Materializer.merge_into(parent, obj)
377
402
  parent
378
403
  else
379
404
  parent[key] = obj
data/lib/yeptris/node.rb CHANGED
@@ -407,7 +407,7 @@ class Yeptris::Node
407
407
  return symbolize if symbol? && tag_id == :str
408
408
 
409
409
  # !ruby/symbol (#258): psych deserializes the scalar to a Symbol
410
- # (both spellings; safety is walk_safe's permitted_classes gate)
410
+ # (both spellings; safety is check_records's permitted_classes gate)
411
411
  if tag == "!ruby/symbol" || tag == "tag:ruby.yaml.org,2002:symbol"
412
412
  return value.to_s.to_sym
413
413
  end
@@ -12,6 +12,12 @@ module Yeptris
12
12
  # objects, aliases for repeats, the Encodable protocol, and the
13
13
  # !ruby/... tags Psych's own emitter produces.
14
14
  class YAMLTree
15
+ # The chevron KEY tag the bulk lane stores: the full core URI
16
+ # the engine's tag writer renders as !!str (the verbatim
17
+ # shorthand would emit as a LOCAL tag — writer.c's regression).
18
+ # One definition, in the writer of the tag bytes (BulkBuilder).
19
+ MERGE_KEY_TAG = ::Yeptris::YAML::BulkBuilder::MERGE_KEY_TAG
20
+
15
21
  # stdlib's factory (relaton's db cache calls it): the options
16
22
  # reach the engine's emitter — :line_width and :canonical ride
17
23
  # yeptris_emit_options (13B), :indentation is accepted for
@@ -22,23 +28,42 @@ module Yeptris
22
28
  new(options: options)
23
29
  end
24
30
 
25
- def initialize(schema: nil, options: {})
31
+ # force_node: the node walk's escape hatch — the A/B switch the
32
+ # byte-parity specs drive (#857). Hosts never pass it; stdlib's
33
+ # .create(options, emitter) signature is untouched.
34
+ def initialize(schema: nil, options: {}, force_node: false)
26
35
  @options = options || {}
27
36
  @tree = ::Yeptris::Document.create(schema: schema)
28
37
  @names = {} # object_id -> anchor name
29
38
  @counter = 0
39
+ @bulk_ok = ::Yeptris::FFI::BULK_BUILD && !force_node
40
+ @force_node = force_node
30
41
  end
31
42
 
32
43
  def push(obj)
33
44
  @refs = Hash.new(0)
34
45
  @refs[obj.object_id] += 1 # the root counts as a reference
46
+ # re-armed per push: the gate walk below clears it for any
47
+ # graph the bulk lane cannot express
48
+ @fast = @bulk_ok && !@force_node
35
49
  count_refs(obj, {})
50
+ if @fast
51
+ build_bulk(obj) # the node-free lane (#857): one FFI call, no handles
52
+ return self
53
+ end
36
54
  root = visit(obj)
37
55
  @tree.set_root(root) if root
38
56
  self
39
57
  end
40
58
  alias_method :<<, :push # stdlib's dump body pushes via <<
41
59
 
60
+ # Whether push took the node-free lane. False when the graph
61
+ # carries anchor candidates, tagged/object shapes, or the
62
+ # engine predates the bulk build (the capability probe).
63
+ def fast_lane?
64
+ @took_fast == true
65
+ end
66
+
42
67
  # stdlib Psych.dump's tail: `visitor.tree.yaml io, options` —
43
68
  # the tree wraps the built document; yaml serializes it
44
69
  # (string return, or writes + returns the io)
@@ -139,21 +164,34 @@ module Yeptris
139
164
  [:new, name]
140
165
  end
141
166
 
167
+ # The reference-count walk doubles as the node-free lane's
168
+ # gate (#857): the graph rides the bulk build only when every
169
+ # value is a plain-data type AND no anchorable object repeats
170
+ # (anchors/aliases are inexpressible in the flat entry stream).
142
171
  def count_refs(obj, seen)
143
172
  return if seen.key?(obj.object_id)
144
173
 
145
174
  seen[obj.object_id] = true
146
175
  case obj
147
176
  when Hash
148
- obj.each_value { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
177
+ obj.each_value { |v| bump_ref(v); count_refs(v, seen) }
149
178
  when Array
150
- obj.each { |v| @refs[v.object_id] += 1; count_refs(v, seen) }
179
+ obj.each { |v| bump_ref(v); count_refs(v, seen) }
151
180
  when Struct
181
+ @fast = false # !ruby/struct tag
152
182
  obj.each_pair { |_k, v| @refs[v.object_id] += 1; count_refs(v, seen) }
153
183
  when Set
184
+ @fast = false # !ruby/set tag
154
185
  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
186
+ when ::String, ::Integer, ::Float, ::Symbol, ::Date, ::Time, true, false, nil
187
+ # bulk-lane immutables — except DateTime, which carries the
188
+ # !ruby/object:DateTime tag even when unshared (the visit
189
+ # dispatch's own DateTime arm)
190
+ @fast = false if obj.is_a?(::DateTime)
191
+ when ::Numeric
192
+ # BigDecimal & co: visit's refuse_dump owns the contract
193
+ # (and its message) — the node walk keeps it
194
+ @fast = false
157
195
  else
158
196
  unless obj.instance_of?(::Object)
159
197
  refuse_dump(obj) unless obj.is_a?(Encodable)
@@ -170,9 +208,37 @@ module Yeptris
170
208
  coder.each { |_k, v| @refs[v.object_id] += 1; count_refs(v, seen) }
171
209
  end
172
210
  end
211
+ @fast = false # bare Object and Encodable: the node walk owns them
173
212
  end
174
213
  end
175
214
 
215
+ # A SECOND occurrence forces the node walk when the object's
216
+ # visit arm anchors (Hash/Array and the timestamp family —
217
+ # anchored_scalar). Plain scalars never anchor (the stdlib
218
+ # law), so shared strings, numbers, bools and nils stay on the
219
+ # bulk lane.
220
+ def bump_ref(v)
221
+ @refs[v.object_id] += 1
222
+ return unless @refs[v.object_id] == 2
223
+
224
+ @fast = false unless v.is_a?(::String) || v.is_a?(::Symbol) ||
225
+ v.is_a?(::Integer) || v.is_a?(::Float) ||
226
+ v.equal?(true) || v.equal?(false) || v.nil?
227
+ end
228
+
229
+ def build_bulk(obj)
230
+ # The node-free lane (#857): the SAME flat entry stream the
231
+ # neutral face's BulkBuilder feeds yeptris_document_build —
232
+ # one FFI call raises the document, zero node handles, zero
233
+ # anchor bookkeeping (a graph that needed any would have
234
+ # taken the node walk above). Merge keys carry the FULL core
235
+ # URI: the engine's tag writer spells !!str from it (the
236
+ # verbatim shorthand would emit as a LOCAL tag — the
237
+ # writer.c chevron-key regression).
238
+ ::Yeptris::YAML::BulkBuilder.build_into(@tree, obj, MERGE_KEY_TAG)
239
+ @took_fast = true
240
+ end
241
+
176
242
  def refuse_dump(obj)
177
243
  raise ::Yeptris::DumpError,
178
244
  "cannot dump #{obj.class}: include Yeptris::Psych::Encodable and implement encode_with(coder)"
data/lib/yeptris/psych.rb CHANGED
@@ -209,23 +209,61 @@ module Yeptris
209
209
  def safe_load(yaml, permitted_classes: [::Date, ::Time], aliases: false,
210
210
  symbolize_names: false, **)
211
211
  # ONE read at the boundary (read_input): IO-like input must be
212
- # consumed once — the string rides parse AND the slice below
212
+ # consumed once — the string rides every lane below
213
213
  source = ::Yeptris.read_input(yaml)
214
214
  begin
215
- doc = Yeptris::Document.parse(source, schema: :compat_11)
216
- return nil if doc.nil? # the legal empty stream
217
-
218
- res = force_utf8_scalars(walk_safe(doc.root(0), permitted_classes, aliases, source))
215
+ res = safe_load_source(source, permitted_classes, aliases)
219
216
  res = deep_symbolize_keys(res) if symbolize_names
220
217
  res
221
218
  rescue ::Yeptris::ParseError => e
222
- doc&.free unless doc&.freed?
223
219
  translate_parse_error(e)
224
- ensure
225
- doc&.free
226
220
  end
227
221
  end
228
222
 
223
+ # The drain-lane dispatcher (#862). The gate walk IS the facade's
224
+ # parse cost — per-node FFI reads paid twice (check + to_ruby).
225
+ # Two byte facts retire it for clean sources: the parser cannot
226
+ # build an alias without a `*` byte, cannot build an explicit tag
227
+ # without a `!` byte — and the marshal builder refuses timestamps
228
+ # and merge keys, so its SUCCESS proves the gate could not have
229
+ # raised: ONE C call materializes the whole graph (the same lane
230
+ # Yeptris::YAML rides). A refusal (timestamps, merge keys, quoted
231
+ # symbols) or a gate byte drops onto the records lane — one
232
+ # recorder drain feeds BOTH the permission check (over the flat
233
+ # record array, pure Ruby) and the build.
234
+ def safe_load_source(source, permitted, aliases_enabled)
235
+ res =
236
+ if ::Yeptris::FFI::MARSHAL && !source.include?("*") &&
237
+ !source.include?("!") && ValueML.marshal_lane_ok?(source)
238
+ docs = ValueML.load_all_marshal(source, schema: :compat_11, mode: :all)
239
+ # nil = the marshal lane refused (its refusal IS the gate
240
+ # probe): the records lane judges. An empty stream marshals
241
+ # as [nil] — the first document, exactly the legal-empty nil.
242
+ docs.nil? ? records_lane(source, permitted, aliases_enabled)
243
+ : (docs.empty? ? nil : docs.first)
244
+ else
245
+ records_lane(source, permitted, aliases_enabled)
246
+ end
247
+ # The Unicode re-tag (#135) provably does nothing for an
248
+ # ASCII-only source: no scalar can carry a non-ASCII byte, so
249
+ # no BINARY string can pass the valid-UTF-8 probe — the whole
250
+ # result-tree walk is skipped.
251
+ return res if res.nil? || source.ascii_only?
252
+
253
+ force_utf8_scalars(res)
254
+ end
255
+
256
+ # The gated lane: ONE recorder drain, the permission check over
257
+ # the flat record array (no per-node FFI), then the record stack
258
+ # machine builds the graph — the same Materializer the direct
259
+ # surface rides.
260
+ def records_lane(source, permitted, aliases_enabled)
261
+ flat, arena = ::Yeptris::Materializer.drain(source, schema: :compat_11)
262
+ check_records(flat, arena, permitted, aliases_enabled)
263
+ docs = ::Yeptris::Materializer.new.materialize_records(flat, arena)
264
+ docs.empty? ? nil : docs.first
265
+ end
266
+
229
267
  # A YAML stream is a Unicode character stream: stdlib psych tags
230
268
  # non-ASCII scalars UTF-8 even when the input String is BINARY
231
269
  # (#135). ASCII-only values keep their (binary) encoding — a
@@ -374,9 +412,10 @@ module Yeptris
374
412
 
375
413
  # The first document's node tree (no Ruby materialization).
376
414
  def translate_parse_error(e)
377
- # yeptris rejects *foo without &foo at PARSE time (stdlib
378
- # raises at visit) — surface it as stdlib's class so
379
- # consumers' rescues keep working
415
+ # yeptris rejects *foo without &foo (the DOM parse at parse
416
+ # time; the record walk at resolution — the SAME "undefined
417
+ # anchor" phrase) — surface it as stdlib's class so consumers'
418
+ # rescues keep working
380
419
  raise AnchorNotDefined, e.message if e.message.include?("undefined anchor")
381
420
 
382
421
  raise SyntaxError.from_parse_error(e)
@@ -441,10 +480,12 @@ module Yeptris
441
480
  options = io
442
481
  io = nil
443
482
  end
444
- # scalars take the fast builder; EVERYTHING else (including
445
- # plain containers — they may nest custom objects) goes
446
- # through the visitor, which builds the same DOM for plain
447
- # data anyway
483
+ # scalars take the fast builder; plain-data containers take it
484
+ # too (the #862 dump cut: ONE bulk build + ONE serialize — the
485
+ # YAMLTree DOM build is per-node FFI and buried the engine's
486
+ # speed on document-tree workloads); EVERYTHING else (custom
487
+ # objects, Struct/Set, DateTime, shared containers) goes
488
+ # through the visitor
448
489
  out =
449
490
  case obj
450
491
  # DateTime < Date in ruby's hierarchy — the fast scalar
@@ -455,6 +496,16 @@ module Yeptris
455
496
  when nil, true, false, ::String, ::Integer, ::Float, ::Symbol, ::Date, ::Time
456
497
  Yeptris::YAML.dump(obj, header: true, width: options[:line_width],
457
498
  canonical: options[:canonical] ? true : false)
499
+ when ::Hash, ::Array
500
+ if plain_graph?(obj, {})
501
+ # merge_tag: the URI the engine's tag writer renders as
502
+ # !!str (the shorthand rides the bulk build's URI escaper)
503
+ Yeptris::YAML.dump(obj, header: true, width: options[:line_width],
504
+ canonical: options[:canonical] ? true : false,
505
+ merge_tag: Yeptris::YAML::BulkBuilder::MERGE_KEY_TAG)
506
+ else
507
+ Visitors::YAMLTree.new(options: options).push(obj).finish
508
+ end
458
509
  else
459
510
  Visitors::YAMLTree.new(options: options).push(obj).finish
460
511
  end
@@ -466,36 +517,59 @@ module Yeptris
466
517
 
467
518
  private
468
519
 
469
- # yeptris materializes plain data only — there is nothing
470
- # unsafe it COULD load. The safety walk enforces Psych's
471
- # contract: aliases need opt-in, explicit non-core tags raise
472
- # DisallowedClass, and a scalar whose IMPLICIT typing yields a
473
- # class outside the permitted set raises too (issue #69: a
474
- # compat_11 date must not become a Date unless Date is
475
- # permitted — Psych::DisallowedClass semantics).
476
- #
477
- # The single-pass slice (#862): the gate walk IS the cost —
478
- # per-node FFI reads. Three provable facts retire it for clean
479
- # documents: the parser cannot build an alias without a `*`
480
- # byte in the source, cannot build an explicit tag without a
481
- # `!` byte, and the marshal builder REFUSES timestamps and
482
- # merge keys (its success proves none exist — every
483
- # timestamp-typed shape is refused, verified per shape). So
484
- # `marshal_fast` doubles as the probe AND the build in one C
485
- # call: on a gate-free source its success proves check() could
486
- # not have raised, and the walk disappears; a refusal or a gate
487
- # byte falls back to the checked walk, which is byte-for-byte
488
- # the old path.
489
- def walk_safe(root, permitted, aliases_enabled, source = nil)
490
- if root && !source.nil? &&
491
- !source.include?("*") && !source.include?("!")
492
- fast = root.marshal_fast
493
- return fast unless fast.nil?
520
+ # The bulk lane's admission probe (#862): true when every node is
521
+ # plain data AND no anchorable object repeats — the visitor
522
+ # anchors a repeated Hash/Array (&1/*1, the stdlib law) and a
523
+ # repeated Time/Date alike (visit_Time/visit_Date route through
524
+ # anchored_scalar; test_alias_with_time), the bulk walk would
525
+ # duplicate them instead, so those graphs keep the visitor.
526
+ # Strings/symbols/numbers never anchor (immutable identity), so
527
+ # they are leaves. Keys ride both builders' identical key law
528
+ # (nil → `! ''`, "<<" → !!str, Strings → the visit_String rules,
529
+ # Symbols → :sym, everything else → to_s plain), so only VALUES
530
+ # are probed. Numeric beyond Integer/Float (BigDecimal) and
531
+ # DateTime keep the visitor, matching what the visitor does with
532
+ # them.
533
+ BULK_LEAF = [::String, ::Symbol, ::Integer, ::Float, ::TrueClass,
534
+ ::FalseClass, ::NilClass].freeze
535
+ private_constant :BULK_LEAF
536
+
537
+ def plain_graph?(obj, seen)
538
+ case obj
539
+ when ::Hash
540
+ return false if seen.key?(obj.object_id)
541
+
542
+ seen[obj.object_id] = true
543
+ obj.each_value { |v| return false unless plain_graph?(v, seen) }
544
+ true
545
+ when ::Array
546
+ return false if seen.key?(obj.object_id)
547
+
548
+ seen[obj.object_id] = true
549
+ obj.each { |v| return false unless plain_graph?(v, seen) }
550
+ true
551
+ when ::Time, ::Date
552
+ # DateTime < Date: the visitor carries its !ruby/object:DateTime tag
553
+ return false if obj.is_a?(::DateTime)
554
+ # identity-anchored per object (unlike the immutables)
555
+ return false if seen.key?(obj.object_id)
556
+
557
+ seen[obj.object_id] = true
558
+ true
559
+ else
560
+ BULK_LEAF.any? { |k| obj.is_a?(k) }
494
561
  end
495
- check(root, permitted, aliases_enabled) if root
496
- root.to_ruby
497
562
  end
498
563
 
564
+ # yeptris materializes plain data only — there is nothing
565
+ # unsafe it COULD load. The safety enforcement lives on the
566
+ # drained records (check_records): aliases need opt-in, explicit
567
+ # non-core tags raise DisallowedClass, and a scalar whose
568
+ # RESOLVED typing yields a class outside the permitted set
569
+ # raises too (issue #69: a compat_11 date must not become a
570
+ # Date unless Date is permitted — Psych::DisallowedClass
571
+ # semantics).
572
+
499
573
  PERMITTED_BY_DEFAULT = [TrueClass, FalseClass, NilClass, Integer, Float,
500
574
  String, Array, Hash].freeze
501
575
 
@@ -535,56 +609,72 @@ module Yeptris
535
609
  end
536
610
  end
537
611
 
538
- def check(node, permitted, aliases_enabled)
539
- case node.kind
540
- when :alias
541
- raise AliasesError, "Unknown alias" unless aliases_enabled
542
- when :scalar, :mapping, :sequence
543
- tag = node.tag
544
- unless tag.nil?
545
- # both tag spellings pass: the URI form's last segment and
546
- # the short "!binary" shorthand (#168's cassettes carry it)
547
- name = tag.split(":").last.sub(/\A!/, "")
548
- unless CORE_TAGS.include?(name)
549
- # #258: !ruby/… tags follow psych's ClassLoader rules —
550
- # the scalar primitives (string/integer/float) coerce
551
- # through the scanner and are allowed as-is; !ruby/symbol
552
- # passes exactly when Symbol is in permitted_classes;
553
- # class-bearing forms (ruby/object:K, ruby/hash:K, …)
554
- # pass when K is in permitted_classes
555
- short = ruby_tag_short(tag)
556
- unless RUBY_TAG_PRIMITIVES.include?(short)
557
- klass = ruby_tag_class(short)
558
- raise DisallowedClass, name if klass.nil?
559
- raise DisallowedClass, name unless permitted.include?(klass)
612
+ # The gate over drained records — the same law as the old
613
+ # per-node DOM walk, read off the flat record array instead of
614
+ # FFI handles: alias records need opt-in; an explicit tag
615
+ # (tag_len != 0, any node kind) follows the core/ruby rules; and
616
+ # a timestamp tag_id resolves through parse_timestamp and rides
617
+ # the permitted set. Records are 14 fields, type first
618
+ # (Materializer::FIELDS; the #179 layout puts end_line/end_col
619
+ # at 6/7, the value at 8/9, the anchor at 10/11 and the tag
620
+ # text at 12/13 — the walk() reads are the SSOT). Offsets are
621
+ # bytes: byteslice, whatever the arena's encoding (#1359).
622
+ def check_records(flat, arena, permitted, aliases_enabled)
623
+ # The timestamp gate needs the RESOLVED class only when the
624
+ # permitted set cannot absorb both answers (the facade default
625
+ # permits Date AND Time — the resolution is skipped entirely).
626
+ gate_ts = !(permitted.include?(::Date) && permitted.include?(::Time))
627
+ m = ::Yeptris::Materializer
628
+ i = 0
629
+ n = flat.length
630
+ while i < n
631
+ type = flat[i]
632
+ if type == m::ALIAS
633
+ raise AliasesError, "Unknown alias" unless aliases_enabled
634
+ else
635
+ l = flat[i + 13]
636
+ if l != 0
637
+ tag = arena.byteslice(flat[i + 12], l)
638
+ # the bare non-specific tag (`! ''` — the nil-key form,
639
+ # #300): the DOM face normalized it away (node.tag nil),
640
+ # the record keeps it verbatim — it names no class and is
641
+ # always plain data
642
+ unless tag == "!"
643
+ # both tag spellings pass: the URI form's last segment and
644
+ # the short "!binary" shorthand (#168's cassettes carry it)
645
+ name = tag.split(":").last.sub(/\A!/, "")
646
+ unless CORE_TAGS.include?(name)
647
+ # #258: !ruby/… tags follow psych's ClassLoader rules —
648
+ # the scalar primitives (string/integer/float) coerce
649
+ # through the scanner and are allowed as-is; !ruby/symbol
650
+ # passes exactly when Symbol is in permitted_classes;
651
+ # class-bearing forms (ruby/object:K, ruby/hash:K, …)
652
+ # pass when K is in permitted_classes
653
+ short = ruby_tag_short(tag)
654
+ unless RUBY_TAG_PRIMITIVES.include?(short)
655
+ klass = ruby_tag_class(short)
656
+ raise DisallowedClass, name if klass.nil?
657
+ raise DisallowedClass, name unless permitted.include?(klass)
658
+ end
659
+ end
560
660
  end
561
661
  end
562
- end
563
- # psych's safe law for the timestamp family (#1305): the gate
564
- # applies to the RESOLVED class, not the tag. A plain
565
- # timestamp resolves like psych's scanner — date-only to
566
- # Date, date+time to Time (ClassLoader::Restricted#time /
567
- # #date raise DisallowedClass naming the class) — so the
568
- # decision rides the ONE resolver the materializer itself
569
- # uses (parse_timestamp); a malformed timestamp text
570
- # resolves to String there and psych likewise yields a
571
- # String (no gate — plain data).
572
- if node.kind == :scalar && node.tag_id == :timestamp
573
- resolved = ::Yeptris::Materializer.parse_timestamp(node.value.to_s)
574
- klass = resolved.class
575
- if (klass == ::Date || klass == ::Time) && !permitted.include?(klass)
576
- raise DisallowedClass, klass.name
577
- end
578
- end
579
- case node.kind
580
- when :mapping
581
- node.each_pair do |k, v|
582
- check(k, permitted, aliases_enabled)
583
- check(v, permitted, aliases_enabled)
662
+ # psych's safe law for the timestamp family (#1305): the
663
+ # gate applies to the RESOLVED class, not the tag. A
664
+ # timestamp resolves like psych's scanner — date-only to
665
+ # Date, date+time to Time — via the ONE resolver the
666
+ # materializer itself uses; a malformed timestamp text
667
+ # resolves to String there and psych likewise yields a
668
+ # String (no gate — plain data).
669
+ if gate_ts && type == m::SCALAR && flat[i + 3] == m::TAG_TIMESTAMP
670
+ resolved = m.parse_timestamp(arena.byteslice(flat[i + 8], flat[i + 9]))
671
+ klass = resolved.class
672
+ if (klass == ::Date || klass == ::Time) && !permitted.include?(klass)
673
+ raise DisallowedClass, klass.name
674
+ end
584
675
  end
585
- when :sequence
586
- node.each { |e| check(e, permitted, aliases_enabled) }
587
676
  end
677
+ i += m::FIELDS
588
678
  end
589
679
  end
590
680
  end