lutaml-model 0.8.162 → 0.8.164

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.
@@ -30,6 +30,15 @@ module Lutaml
30
30
  module NativeEmission
31
31
  ROOT_ROW = 0xFFFF_FFFF
32
32
  EMITTABLE_KINDS = %i[scalar collection nested].freeze
33
+ # Row kinds the element_order replay can address with ops.
34
+ # raw/child_deferred/wildcard rows keep the DOM lane.
35
+ ORDERED_ROW_KINDS = %i[scalar collection_native collection_cb
36
+ nested ordered_deferred].freeze
37
+ DEFERRED_ROW_KINDS = %i[raw ordered_deferred].freeze
38
+ NESTED_ROW_KINDS = %i[nested ordered_deferred].freeze
39
+ NS_EMITTABLE_ROW_KINDS = %i[scalar collection nested
40
+ collection_native
41
+ ordered_deferred].freeze
33
42
 
34
43
  # Fiber-local buffer of reusable op Hash slots. The engine's
35
44
  # build_result copies every op into FFI memory before it
@@ -43,23 +52,158 @@ module Lutaml
43
52
  :lutaml_model_leptris_native_emission_op_buffer_base
44
53
 
45
54
  class << self
46
- def compatible?(plan)
47
- !plan[:ordered] && tree_emittable?(plan[:tree])
55
+ # The RAW-splice capability rides engine 1.9.406.0: the gem
56
+ # maps PLAN_OP_RAW and the :raw row-op kind (both ship in
57
+ # the same release). Probing the public constant tracks the
58
+ # LOADED gem — a bundle pinned to an older engine (the
59
+ # metanorma consumer matrix resolves 1.9.405.0) takes the
60
+ # DOM lane instead of splicing :raw ops the engine cannot
61
+ # parse (which emitted empty fragments and died on the
62
+ # round-trip parse).
63
+ def raw_op_supported?(ffi = ::Leptris::XML::FFI)
64
+ ffi.const_defined?(:PLAN_OP_RAW)
48
65
  end
49
66
 
50
- # Pre-order depth-first, children in row order — the same
51
- # walk Descriptor.flatten_plan performs, so the plan_index
52
- # counters here match the engine's flattened plans array.
53
- def tree_emittable?(tree)
54
- return false if tree[:ns]
55
- return false if (tree[:attributes] || []).any? { |a| a[:ns].is_a?(Hash) }
67
+ # plan: the compiled plan; instance: the emitting instance.
68
+ # A nil instance runs the static checks only.
69
+ def compatible?(plan, instance = nil, register = nil)
70
+ return false if plan.nil?
71
+ return false unless raw_op_supported?
72
+ return false unless ordered_ns_emittable?(plan)
73
+ return false if plan[:ordered] && instance.is_a?(::Lutaml::Model::Serialize) &&
74
+ instance.element_order.nil?
75
+
76
+ subtree_gate_ok?(plan, instance, register)
77
+ end
78
+
79
+ # The emission graph is a DAG: Descriptor.flatten_plan
80
+ # memoizes child plans by object identity, so a shared
81
+ # subtree keeps ONE plan slot and every parent references
82
+ # it. The identity map mirrors that numbering exactly —
83
+ # op plan_index values are these absolute slots.
84
+ def emission_graph_for(plan, register)
85
+ map = {}.compare_by_identity
86
+ counter = [0]
87
+ walk = lambda do |tree|
88
+ return map[tree] if map.key?(tree)
89
+
90
+ idx = counter[0]
91
+ counter[0] += 1
92
+ map[tree] = idx
93
+ (tree[:children] || []).each do |r|
94
+ walk.call(r[:plan]) if r[:kind] == :nested && r[:plan]
95
+ end
96
+ idx
97
+ end
98
+ walk.call(emission_tree_for(plan, register))
99
+ map
100
+ end
101
+
102
+ # A plan whose tree defers subtrees verbatim (:raw rows —
103
+ # ordered children, e.g. uniword's document → body).
104
+ def deferred_rows?(tree)
105
+ (tree[:children] || []).any? do |r|
106
+ DEFERRED_ROW_KINDS.include?(r[:kind])
107
+ end
108
+ end
109
+
110
+ # The recursive ops-addressability gate: EVERY plan in the
111
+ # emission graph must materialize row-for-row (an unhandled
112
+ # kind would shift every later row_index), content runs must
113
+ # be text-expressible (no CDATA), and every ORDERED subtree
114
+ # instance must carry a pristine parse-built element_order
115
+ # with Element/Text entries only (comments and PIs keep the
116
+ # DOM lane; mutation reroutes through the reconciler).
117
+ def subtree_gate_ok?(plan, instance, register)
118
+ return false unless plan[:rows].all? do |row|
119
+ gate_row_kind?(row, register)
120
+ end
121
+ return false if content_cdata?(plan)
122
+
123
+ if plan[:ordered] && instance.is_a?(::Lutaml::Model::Serialize)
124
+ return false if instance.element_order.nil?
125
+ return false if instance.mutated_after_parse?
126
+ return false unless instance.element_order.all? do |o|
127
+ (o.type == "Element") ||
128
+ (o.type == "Text" && o.node_type != :cdata)
129
+ end
130
+ end
131
+
132
+ return true unless instance.is_a?(::Lutaml::Model::Serialize)
133
+
134
+ plan[:rows].each do |(rule, attr, kind, _sp, delegate, type)|
135
+ next unless rule.name
136
+ next if kind == :child_deferred
137
+ next unless NESTED_ROW_KINDS.include?(kind)
138
+
139
+ value = value_of(instance, attr, delegate)
140
+ Array(value).each do |item|
141
+ next unless item
142
+
143
+ child_type = begin
144
+ type || attr.type(register)
145
+ rescue StandardError
146
+ nil
147
+ end
148
+ child_plan = child_type.is_a?(Class) &&
149
+ PlanCompiler.compile(child_type, register)
150
+ return false unless child_plan
151
+ return false unless subtree_gate_ok?(child_plan, item,
152
+ register)
153
+ end
154
+ end
155
+ true
156
+ end
157
+
158
+ # Row kinds the emission graph can address: scalar and
159
+ # collection rows (text ops), nested/ordered_deferred rows
160
+ # (element ops with a compilable child), and content rows
161
+ # (the mixed-content text channel). raw, child_deferred,
162
+ # wildcard, and callback rows keep the DOM lane.
163
+ def gate_row_kind?(row, register)
164
+ kind = row[2]
165
+ return true if PlanSerializer::CONTENT_KINDS.include?(kind)
166
+ return true if %i[scalar collection_native
167
+ collection_cb child_deferred].include?(kind)
168
+ return false unless %i[nested ordered_deferred].include?(kind)
169
+ return false unless row[0].name
170
+
171
+ child_type = begin
172
+ row[5] || row[1].type(register)
173
+ rescue StandardError
174
+ nil
175
+ end
176
+ child_type.is_a?(Class) &&
177
+ !PlanCompiler.compile(child_type, register).nil?
178
+ end
179
+
180
+ # Namespace-emittability for ORDERED trees: content and
181
+ # wildcard rows carry no spelling of their own (content
182
+ # emits text; wildcards bail in subtree_gate_ok?), so the
183
+ # check covers the tree ns, attribute rows, and the
184
+ # named-element rows only.
185
+ def ordered_ns_emittable?(plan)
186
+ tree = plan[:tree]
187
+ return false unless ns_emittable?(tree[:ns])
188
+ return false if (tree[:attributes] || []).any? do |a|
189
+ !ns_emittable?(a[:ns])
190
+ end
56
191
 
57
192
  (tree[:children] || []).all? do |row|
58
- EMITTABLE_KINDS.include?(row[:kind]) &&
59
- (row[:kind] != :nested || !row[:plan] || tree_emittable?(row[:plan]))
193
+ next true unless NS_EMITTABLE_ROW_KINDS.include?(row[:kind])
194
+
195
+ ns_emittable?(row[:ns])
60
196
  end
61
197
  end
62
198
 
199
+ # nil (:none/:unqualified — unwritten) and {exact:, prefix:}
200
+ # spell; :any and :none-symbol bail to the DOM lane.
201
+ def ns_emittable?(ns_form)
202
+ ns_form.nil? || ns_form == :unqualified ||
203
+ (ns_form.is_a?(Hash) && ns_form[:prefix] &&
204
+ !ns_form[:prefix].empty?)
205
+ end
206
+
63
207
  # len is the logical length of the buffer: slots [base, len) hold
64
208
  # the ops of the emission in progress. base is nonzero when a
65
209
  # user hook re-enters to_xml mid-emission — the inner
@@ -75,13 +219,26 @@ module Lutaml
75
219
  op_slot(buf, len)
76
220
  buf[len][:kind] = :element
77
221
  buf[len][:row_index] = ROOT_ROW
78
- len = emit_element(buf, len + 1, instance, plan, register, 0)
222
+ graph = if emission_lane?(plan, instance)
223
+ emission_graph_for(plan, register)
224
+ end
225
+ root_index = graph ? graph[emission_tree_for(plan, register)] : 0
226
+ len = if plan[:ordered] && !instance.element_order.nil?
227
+ emit_ordered(buf, len + 1, instance, plan, register,
228
+ root_index, graph)
229
+ else
230
+ emit_element(buf, len + 1, instance, plan, register,
231
+ root_index, graph)
232
+ end
79
233
  op_slot(buf, len)
80
234
  buf[len][:kind] = :end
81
235
  len += 1
82
- compact = plan[:descriptor].serialize(
83
- plan[:descriptor].build_result(buf[base, len - base]),
84
- )
236
+ d = if emission_lane?(plan, instance)
237
+ emission_descriptor_for(plan, register)
238
+ else
239
+ plan[:descriptor]
240
+ end
241
+ compact = d.serialize(d.build_result(buf[base, len - base]))
85
242
  ::Leptris::XML.parse(compact).to_xml(indent: 2, no_decl: true)
86
243
  ensure
87
244
  buf.slice!(len..) if buf.size > len
@@ -89,8 +246,239 @@ module Lutaml
89
246
  end
90
247
  end
91
248
 
249
+ # The emission graph is needed when the plan replays an
250
+ # element_order (ordered roots) or materializes deferred
251
+ # subtrees (unordered roots with :raw rows).
252
+ def emission_lane?(plan, instance)
253
+ return true if plan[:ordered] && !instance.element_order.nil?
254
+
255
+ !plan[:ordered] && deferred_rows?(plan[:tree])
256
+ end
257
+
258
+ # Ordered/deferred plans carry :raw tree rows — the parse
259
+ # descriptor has no child plans for them. The emission lane
260
+ # builds its own descriptor from an emission-shaped tree:
261
+ # dispatchable rows materialize as :nested/:scalar/
262
+ # :collection rows with real child plans, one emission row
263
+ # per plan row (op row_index values are plan row indices).
264
+ def emission_tree_for(plan, register)
265
+ build = lambda do
266
+ tree = plan[:tree]
267
+ ns_index = ::Lutaml::Xml::Leptris::PlanSerializer
268
+ .ns_index_for(plan)
269
+ {
270
+ name: tree[:name], ns: tree[:ns], flags: tree[:flags],
271
+ attributes: tree[:attributes],
272
+ children: emission_rows_for(plan, ns_index, register)
273
+ }
274
+ end
275
+ # Never memoize a nil-register build (child types cannot
276
+ # materialize without a register).
277
+ return build.call if register.nil?
278
+
279
+ plan[:emission_tree] ||= build.call
280
+ end
281
+
282
+ def emission_descriptor_for(plan, register)
283
+ plan[:emission_descriptor] ||= begin
284
+ require "leptris/xml/descriptor"
285
+ ::Leptris::XML::Descriptor.build(**emission_tree_for(plan,
286
+ register))
287
+ end
288
+ end
289
+
290
+ def emission_rows_for(plan, ns_index, register)
291
+ rows = plan[:rows]
292
+ content_idx = content_row_for(plan)[1]
293
+ out = []
294
+ rows.each_with_index do |(rule, attr, kind, _sp, _del, type), idx|
295
+ if idx == content_idx
296
+ # The mixed-content text channel: SCALAR text ops
297
+ # reference this row index; the engine renders
298
+ # CONTENT-kind rows as bare escaped text.
299
+ out << { name: "", kind: :content }
300
+ next
301
+ end
302
+ name = rule.name ? rule.name.to_s : ""
303
+ ns = ns_index.key?(name) ? { ns: ns_index[name] } : {}
304
+ case kind
305
+ when :scalar
306
+ out << { name: name, kind: :scalar, **ns }
307
+ when :collection_native, :collection_cb
308
+ out << { name: name, kind: :collection, **ns }
309
+ when :nested, :ordered_deferred
310
+ child_type = type || attr.type(register)
311
+ child_plan = child_type &&
312
+ PlanCompiler.compile(child_type, register)
313
+ out << if child_plan
314
+ { name: name, kind: :nested, **ns,
315
+ plan: emission_tree_for(child_plan, register) }
316
+ else
317
+ { name: name, kind: :scalar, **ns }
318
+ end
319
+ else
320
+ # raw/child_deferred/wildcard slots: never addressed
321
+ # by ops (the gate rejects the plans where they
322
+ # matter; empty child_deferred values skip emission)
323
+ # — the placeholder keeps index alignment.
324
+ out << { name: name, kind: :scalar, **ns }
325
+ end
326
+ end
327
+ out
328
+ end
329
+
92
330
  private
93
331
 
332
+ # The (URI, local)-dispatched element_order replay — the
333
+ # DOM lane's build_ordered contract over ops instead of
334
+ # mints. Text entries ride SCALAR ops on the CONTENT row
335
+ # (the engine renders CONTENT-kind rows as bare escaped
336
+ # text); element entries dispatch like build_ordered
337
+ # (unique-name lenient, contested (URI, local) strict with
338
+ # URI-less fallback) and push the same element/scalar ops
339
+ # the declaration-order walk pushes.
340
+ def emit_ordered(buf, len, instance, plan, register, plan_index,
341
+ graph = nil)
342
+ my_index = plan_index
343
+ len = emit_attributes(buf, len, instance, plan, my_index,
344
+ register)
345
+ inherited_ns = nil
346
+ ns_index = ::Lutaml::Xml::Leptris::PlanSerializer.ns_index_for(plan)
347
+ rows_by_name, name_keys =
348
+ ::Lutaml::Xml::Leptris::PlanSerializer.ordered_layout_for(
349
+ plan, inherited_ns, ns_index, register
350
+ )
351
+ content_entry, content_row_index = content_row_for(plan)
352
+ content_attr = content_entry && content_entry[1]
353
+ content_value = content_attr &&
354
+ value_of(instance, content_attr, content_entry[4])
355
+ order = instance.element_order
356
+ text_node_count = order.count { |o| o.type == "Text" }
357
+ use_content_index = content_value.is_a?(::Array) &&
358
+ content_value.length == text_node_count
359
+ text_node_index = 0
360
+ element_indices = ::Hash.new(0)
361
+ nested_plans =
362
+ ::Lutaml::Xml::Leptris::PlanSerializer.nested_plans_for(plan)
363
+
364
+ order.each do |object|
365
+ case object.type
366
+ when "Text"
367
+ text = if content_attr && !content_value.nil?
368
+ if use_content_index
369
+ content_value[text_node_index].to_s
370
+ elsif !content_value.is_a?(::Array) &&
371
+ text_node_count <= 1
372
+ content_value.to_s
373
+ else
374
+ object.text_content || object.name
375
+ end
376
+ else
377
+ object.text_content || object.name
378
+ end
379
+ text_node_index += 1
380
+ next if text.nil?
381
+ next if content_attr.nil? && text.strip.empty?
382
+
383
+ op_slot(buf, len)
384
+ buf[len][:kind] = :scalar
385
+ buf[len][:plan_index] = my_index
386
+ buf[len][:row_index] = content_row_index
387
+ buf[len][:value] = text
388
+ len += 1
389
+ when "Element"
390
+ name = object.name
391
+ keys = name_keys[name]
392
+ if keys&.length == 1
393
+ dispatch_key = keys.first
394
+ entry = rows_by_name[dispatch_key]
395
+ else
396
+ dispatch_key = [object.namespace_uri, name]
397
+ entry = rows_by_name[dispatch_key]
398
+ unless entry
399
+ dispatch_key = [nil, name]
400
+ entry = rows_by_name[dispatch_key]
401
+ end
402
+ end
403
+ next unless entry
404
+
405
+ _rule, attr, kind, _spelling, delegate, _row_ns,
406
+ row_index = entry
407
+ value = value_of(instance, attr, delegate)
408
+ case kind
409
+ when :collection_native, :collection_cb
410
+ index = element_indices[dispatch_key]
411
+ items = Array(value)
412
+ next unless index < items.length
413
+
414
+ element_indices[dispatch_key] = index + 1
415
+ len = scalar_op(buf, len, my_index, row_index, attr,
416
+ items[index], register)
417
+ when :nested, :ordered_deferred
418
+ item = if attr.collection?
419
+ index = element_indices[dispatch_key]
420
+ element_indices[dispatch_key] = index + 1
421
+ Array(value)[index]
422
+ else
423
+ value
424
+ end
425
+ next unless item
426
+
427
+ child_plan =
428
+ nested_plans[[item.class, name]] ||
429
+ (nested_plans[[item.class, name]] =
430
+ PlanCompiler.compile(item.class, register))
431
+ child_index =
432
+ if graph
433
+ tree = emission_tree_for(child_plan, register)
434
+ graph[tree]
435
+ else
436
+ flat_layout_for(plan, register)[row_index]&.last
437
+ end
438
+ next unless child_index
439
+
440
+ op_slot(buf, len)
441
+ buf[len][:kind] = :element
442
+ buf[len][:plan_index] = my_index
443
+ buf[len][:row_index] = row_index
444
+ len += 1
445
+ len = if child_plan[:ordered] || !item.element_order.nil?
446
+ emit_ordered(buf, len, item, child_plan, register,
447
+ child_index, graph)
448
+ else
449
+ emit_element(buf, len, item, child_plan, register,
450
+ child_index, graph)
451
+ end
452
+ op_slot(buf, len)
453
+ buf[len][:kind] = :end
454
+ len += 1
455
+ when :scalar
456
+ len = scalar_op(buf, len, my_index, row_index, attr,
457
+ value, register)
458
+ end
459
+ end
460
+ end
461
+ len
462
+ end
463
+
464
+ # [content row tuple, plan-children row index] — the mixed-
465
+ # content text channel (rule.name nil, kind content), nil
466
+ # when the model maps no bare content.
467
+ def content_row_for(plan)
468
+ return plan[:content_row_cache] if plan[:content_row_cache]
469
+
470
+ idx = plan[:rows].index do |(rule, _attr, kind, _sp, _del, _ty)|
471
+ PlanSerializer::CONTENT_KINDS.include?(kind) &&
472
+ rule.name.nil?
473
+ end
474
+ plan[:content_row_cache] = [idx && plan[:rows][idx], idx]
475
+ end
476
+
477
+ def content_cdata?(plan)
478
+ entry, = content_row_for(plan)
479
+ entry && entry[0]&.cdata
480
+ end
481
+
94
482
  # Cleared slot len of the buffer, reusing the Hash object a
95
483
  # previous emission left there when present. Also publishes
96
484
  # this emission's exclusive end as the fiber-local base: the
@@ -108,9 +496,10 @@ module Lutaml
108
496
  # once — collection members share that slot — so child flat
109
497
  # indices come from a static per-plan layout (relative to
110
498
  # this plan's index), never from a runtime counter.
111
- def emit_element(buf, len, instance, plan, register, plan_index)
499
+ def emit_element(buf, len, instance, plan, register, plan_index,
500
+ graph = nil)
112
501
  my_index = plan_index
113
- layout = flat_layout_for(plan)
502
+ layout = flat_layout_for(plan, register)
114
503
 
115
504
  len = emit_attributes(buf, len, instance, plan, my_index, register)
116
505
 
@@ -137,22 +526,65 @@ module Lutaml
137
526
  len = scalar_op(buf, len, my_index, row_index, attr, item,
138
527
  register)
139
528
  end
140
- when :nested
141
- _tree, child_rel = layout[row_index]
142
- next unless child_rel
529
+ when :child_deferred
530
+ # leptris#1941: a VALUED cycle-point row serializes
531
+ # the child model through its own best lane (the plan
532
+ # serializer — native or DOM) and splices the fragment
533
+ # verbatim through the RAW op; the empty value emits
534
+ # nothing (the DOM lane's rule).
535
+ Array(value).each do |item|
536
+ next unless item
143
537
 
538
+ fragment = serialize_fragment(item, register)
539
+ next if fragment.nil? || fragment.empty?
540
+
541
+ op_slot(buf, len)
542
+ buf[len][:kind] = :raw
543
+ buf[len][:plan_index] = my_index
544
+ buf[len][:value] = fragment
545
+ len += 1
546
+ end
547
+ when :content, :content_deferred
548
+ # Mixed-content runs ride SCALAR ops on the content
549
+ # row itself — the engine renders CONTENT-kind rows as
550
+ # bare escaped text. Unreachable from the non-ordered
551
+ # lane (tree_emittable? refuses content rows); the
552
+ # ordered lane's unordered leaves land here.
553
+ Array(value).each do |run|
554
+ op_slot(buf, len)
555
+ buf[len][:kind] = :scalar
556
+ buf[len][:plan_index] = my_index
557
+ buf[len][:row_index] = row_index
558
+ buf[len][:value] = run.to_s
559
+ len += 1
560
+ end
561
+ when :nested, :ordered_deferred
144
562
  child_plans = child_plans_for(plan, row_index)
145
563
  Array(value).each do |item|
146
564
  child_plan = child_plans[item.class] ||
147
565
  (child_plans[item.class] =
148
566
  PlanCompiler.compile(item.class, register))
567
+ child_index =
568
+ if graph
569
+ tree = emission_tree_for(child_plan, register)
570
+ graph[tree]
571
+ else
572
+ layout[row_index]&.last
573
+ end
574
+ next unless child_index
575
+
149
576
  op_slot(buf, len)
150
577
  buf[len][:kind] = :element
151
578
  buf[len][:plan_index] = my_index
152
579
  buf[len][:row_index] = row_index
153
580
  len += 1
154
- len = emit_element(buf, len, item, child_plan,
155
- register, my_index + child_rel)
581
+ len = if child_plan[:ordered] || !item.element_order.nil?
582
+ emit_ordered(buf, len, item, child_plan, register,
583
+ child_index, graph)
584
+ else
585
+ emit_element(buf, len, item, child_plan,
586
+ register, child_index, graph)
587
+ end
156
588
  op_slot(buf, len)
157
589
  buf[len][:kind] = :end
158
590
  len += 1
@@ -162,21 +594,53 @@ module Lutaml
162
594
  len
163
595
  end
164
596
 
597
+ # A child model's serialized fragment for RAW splicing: the
598
+ # plan serializer when the child compiles (native or DOM —
599
+ # its own best lane), else the instance's own to_xml (the
600
+ # interpretive writer; plan-less models own that lane).
601
+ def serialize_fragment(item, register)
602
+ child_plan = PlanCompiler.compile(item.class, register)
603
+ return item.to_xml if child_plan.nil?
604
+
605
+ out = ::Lutaml::Xml::Leptris::PlanSerializer.call(item,
606
+ child_plan,
607
+ register)
608
+ out || item.to_xml
609
+ end
610
+
165
611
  # {nested row_index => [child tree, relative flat index]} —
166
612
  # children of this plan occupy slots my_index+1 .. in row
167
613
  # order, each nested row's subtree sized once.
168
- def flat_layout_for(plan)
169
- plan[:flat_layout] ||= begin
170
- layout = {}
171
- rel = 1
172
- (plan[:tree][:children] || []).each_with_index do |row, i|
173
- next unless row[:kind] == :nested && row[:plan]
174
-
175
- layout[i] = [row[:plan], rel]
176
- rel += subtree_size(row[:plan])
177
- end
178
- layout
614
+ def flat_layout_for(plan, register = nil)
615
+ if plan[:ordered] || deferred_rows?(plan[:tree])
616
+ # Never memoize a nil-register build: child types can
617
+ # only materialize with a register (an early nil-register
618
+ # call would cache an EMPTY layout and every later
619
+ # emission would drop the subtrees).
620
+ return build_flat_layout(emission_tree_for(plan, register)) if
621
+ register.nil?
622
+
623
+ plan[:flat_layout_emission] ||= build_flat_layout(
624
+ emission_tree_for(plan, register),
625
+ )
626
+ return plan[:flat_layout_emission]
179
627
  end
628
+
629
+ plan[:flat_layout] ||= build_flat_layout(plan[:tree])
630
+ end
631
+
632
+ def build_flat_layout(tree)
633
+
634
+ layout = {}
635
+ rel = 1
636
+ (tree[:children] || []).each_with_index do |row, i|
637
+ next unless row[:kind] == :nested && row[:plan]
638
+
639
+ layout[i] = [row[:plan], rel]
640
+ rel += subtree_size(row[:plan])
641
+ end
642
+ layout
643
+
180
644
  end
181
645
 
182
646
  def subtree_size(tree)
@@ -194,9 +658,9 @@ module Lutaml
194
658
  def emit_attributes(buf, len, instance, plan, plan_index, register)
195
659
  recorded = instance.attribute_order
196
660
  if recorded && !recorded.empty?
197
- emit_attributes_recorded(buf, len, instance, plan, plan_index,
198
- register, recorded)
199
- return len
661
+ return emit_attributes_recorded(buf, len, instance, plan,
662
+ plan_index, register,
663
+ recorded)
200
664
  end
201
665
 
202
666
  tree_attrs = plan[:tree][:attributes] || []
@@ -67,7 +67,7 @@ module Lutaml
67
67
  # serialize through the ordered walk, not the flat
68
68
  # schema-order native ops layout.
69
69
  return NativeEmission.emit(instance, plan, register) if
70
- NativeEmission.compatible?(plan) && instance.element_order.nil?
70
+ NativeEmission.compatible?(plan, instance, register)
71
71
 
72
72
  doc = ::Leptris::XML::Document.create
73
73
  ns_ctx = Namespaces.new(instance, plan, register)
@@ -379,7 +379,7 @@ module Lutaml
379
379
 
380
380
  rows_by_name = {}
381
381
  name_keys = ::Hash.new { |h, k| h[k] = [] }
382
- plan[:rows].each do |rule, attr, kind, spelling, delegate, type|
382
+ plan[:rows].each_with_index do |(rule, attr, kind, spelling, delegate, type), row_index|
383
383
  next unless rule.name
384
384
 
385
385
  exact = row_exact_uri(rule, attr, kind, type, reg, plan)
@@ -387,13 +387,19 @@ module Lutaml
387
387
  key = exact ? [exact, name] : [nil, name]
388
388
  rows_by_name[key] ||= [rule, attr, kind, spelling,
389
389
  delegate,
390
- ns_index ? ns_index[name] || inherited_ns : inherited_ns]
390
+ ns_index ? ns_index[name] || inherited_ns : inherited_ns,
391
+ row_index]
391
392
  name_keys[name] << key
392
393
  end
393
394
  name_keys.default = nil
394
395
  cache[inherited_ns] = [rows_by_name, name_keys]
395
396
  end
396
397
 
398
+ # leptris#1941: the ordered-layout helpers are shared with
399
+ # NativeEmission's element_order replay — public class-method
400
+ # surface (everything below stays private).
401
+ public :ns_index_for, :nested_plans_for, :ordered_layout_for
402
+
397
403
  # Element_order-driven emission (the interpretive order
398
404
  # applier's contract): text runs and comments verbatim, each
399
405
  # element entry consuming the next item of its collection