moxml 0.6.2 → 0.6.3

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: 18abacdd26eb9008a7b56f975db89664ae24375624ff403ae7d7125516d19bff
4
- data.tar.gz: 31830c7093c075b8258388a1c317459479ff592d00c49b3cf76a4c5ed5ae1a9b
3
+ metadata.gz: 769900ed7676cee2de32bcb98a390616a5284bec301789ab95af7bc3d7384971
4
+ data.tar.gz: 9bd2bcb7eda50b2d4246ec8caea424971e291c00cbc6789e14c18c9f1ed61b7f
5
5
  SHA512:
6
- metadata.gz: 89de7734c64bc40ba40f07e2bb4d79a9ff5770c875d6f6df8b2437d9829d821ecbaeb0bd9b233f53f2e9fcbc64d5c43601fb206db4a6ff7d30472f0d9a110a5e
7
- data.tar.gz: 22c17b78b3674c465f4bba84387627fa4831542acc84d3dfcf721f48886bdca3d994ae92f42145e60f73c5ff0f3578a3f28d929314d3093b10f9604a7a3779ef
6
+ metadata.gz: bc5ad58955107e459f8432084c9a049d4217ac9b56a9a378969d14b88bcf8ac7cf582cfa9db899adb116ff5a1e4c88e93153eb9ef5d61066f3d6144045ccd359
7
+ data.tar.gz: 1f8b0344a42afbf5c3c8b59e43fa72e322a8700ecc2d39c1450c14f1cbed31c2b81b910ce34f8b20f422792b8e92ddfff0ad967d3fc61f6301722b836dd6f242
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ # moxml#520 calibration: raw engine parse vs moxml adapter parse, per engine,
4
+ # on the plateau_all_packages_export.xmi corpus (103 MB).
5
+ # Usage: ruby calibrate.rb raw-lep | raw-noko | mox-lep | mox-noko
6
+
7
+ require "pathname"
8
+ CORPUS = "/Users/mulgogi/src/mn/mn-samples-plateau-cg3/sources/xmi/plateau_all_packages_export.xmi"
9
+ CONTENT = File.read(CORPUS, mode: "rb").freeze
10
+
11
+ def measure(label)
12
+ GC.start
13
+ before = GC.stat(:total_allocated_objects)
14
+ rss0 = `ps -o rss= -p #{Process.pid}`.to_i
15
+ t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
+ result = yield
17
+ dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
18
+ rss1 = `ps -o rss= -p #{Process.pid}`.to_i
19
+ after = GC.stat(:total_allocated_objects)
20
+ puts format(
21
+ "%<label>-24s %<dt>7.2fs allocs %<allocs>9.2fM rss %<rss0>6.0f -> %<rss1>6.0f MB",
22
+ label: label, dt: dt, allocs: (after - before) / 1e6,
23
+ rss0: rss0 / 1024.0, rss1: rss1 / 1024.0
24
+ )
25
+ result
26
+ end
27
+
28
+ case ARGV[0]
29
+ when "raw-lep"
30
+ require "fiddle"
31
+ require "fiddle/import"
32
+ module Lep
33
+ extend Fiddle::Importer
34
+
35
+ dlload File.expand_path("~/src/leptris/leptris-rs/.local-lib/libleptris.dylib")
36
+ extern "void *leptris_parse_string(const char *, size_t, int *)"
37
+ extern "void leptris_document_free(void *)"
38
+ end
39
+ measure("raw libleptris parse") do
40
+ st = [0].pack("l").freeze
41
+ buf = "#{CONTENT}\0"
42
+ doc = Lep.leptris_parse_string(buf, CONTENT.bytesize, st)
43
+ raise "parse failed status #{st.unpack1('l')}" if doc.null?
44
+
45
+ Lep.leptris_document_free(doc)
46
+ end
47
+ when "raw-noko"
48
+ require "nokogiri"
49
+ measure("raw nokogiri parse") do
50
+ d = Nokogiri::XML(CONTENT, &:noblanks)
51
+ raise "no root" unless d.root
52
+
53
+ nil
54
+ end
55
+ when "mox-lep", "mox-noko"
56
+ require "moxml"
57
+ adapter = ARGV[0] == "mox-lep" ? :leptris : :nokogiri
58
+ ctx = Moxml.new(adapter)
59
+ measure("moxml #{adapter} parse") do
60
+ doc = ctx.parse(CONTENT)
61
+ raise "no root" unless doc.root
62
+
63
+ doc
64
+ end
65
+ when "walk-lep", "walk-noko"
66
+ require "moxml"
67
+ adapter = ARGV[0] == "walk-lep" ? :leptris : :nokogiri
68
+ ctx = Moxml.new(adapter)
69
+ doc = ctx.parse(CONTENT)
70
+ measure("moxml #{adapter} walk") do
71
+ seen = 0
72
+ stack = [doc.root]
73
+ while (el = stack.pop)
74
+ seen += 1
75
+ el.name
76
+ el.attributes.each do |k, a|
77
+ k
78
+ a&.value
79
+ end
80
+ el.text
81
+ el.children.each do |c|
82
+ stack << c if c.is_a?(Moxml::Element)
83
+ end
84
+ end
85
+ puts " visited #{seen} elements"
86
+ end
87
+ else
88
+ abort "usage: calibrate.rb raw-lep|raw-noko|mox-lep|mox-noko"
89
+ end
@@ -33,6 +33,53 @@ module Moxml
33
33
  # the README's "older bindings do not load" claim is now true).
34
34
  MINIMUM_BINDING_VERSION = "1.9.386.0"
35
35
 
36
+ # Snapshot-backed attribute listing native (moxml#520): the
37
+ # listing face serves reads from the binding's flat packet face —
38
+ # no Attr mint, no FFI::Pointer, no per-attribute C crossing on
39
+ # the read path (the hydration-walk shape). The node held here is
40
+ # whatever the listing read (a native node on the native layer, a
41
+ # binding element otherwise); the binding element is resolved on
42
+ # demand for the mutation and namespace-scope faces.
43
+ class SnapshotAttribute
44
+ attr_reader :name, :qualified_name
45
+
46
+ def initialize(name, qualified_name, value, node)
47
+ @name = name
48
+ @qualified_name = qualified_name
49
+ @value = value
50
+ @node = node
51
+ end
52
+
53
+ def value
54
+ @value
55
+ end
56
+
57
+ def value=(new_value)
58
+ @value = new_value
59
+ end
60
+
61
+ # Binding element for adapter faces that need binding-side APIs
62
+ # (mutations, namespace scope walks); reads never convert.
63
+ def element
64
+ @element ||= ::Moxml::Adapter::Leptris.to_binding(@node)
65
+ end
66
+
67
+ # Adopt a real binding Attr for mutation-contract returns
68
+ # (name=/namespace= renames go through the element and yield
69
+ # a fresh native).
70
+ def resolve!
71
+ element.attribute_nodes
72
+ .reverse.find { |candidate| candidate.name == @qualified_name }
73
+ end
74
+
75
+ # Shape parity with the binding Attr the snapshot stands for:
76
+ # consumers duck-check to_xml; delegate to the resolved Attr
77
+ # so the engine's own escaping defines the output.
78
+ def to_xml
79
+ resolve!&.to_xml
80
+ end
81
+ end
82
+
36
83
  if Gem::Version.new(::Leptris::VERSION.to_s) <
37
84
  Gem::Version.new(MINIMUM_BINDING_VERSION)
38
85
  raise Moxml::AdapterError.new(
@@ -81,6 +128,21 @@ module Moxml
81
128
  NATIVE_READ_LAYER =
82
129
  defined?(::Leptris::XML::NativeNode)
83
130
 
131
+ # The flat packet face (leptris-ruby#586, binding 1.9.406.1):
132
+ # Native.attribute_packet(document, address) returns
133
+ # [name1, value1, name2, value2, ...] — interned names, fresh
134
+ # values, no per-element memo, no pair arrays, no Attr
135
+ # objects, no FFI::Pointer. Supersedes the snapshot face for
136
+ # the one-pass hydration walks the XMI lanes are (the binding
137
+ # memo is a retention penalty there). Probed on the compiled
138
+ # ext; the version floor pairs it (misrelease races — the
139
+ # NATIVE_PLAN_STRUCTS lesson).
140
+ ELEMENT_ATTR_PACKET =
141
+ NATIVE_READ_LAYER &&
142
+ Gem::Version.new(::Leptris::VERSION) >=
143
+ Gem::Version.new("1.9.406.1") &&
144
+ ::Leptris::XML::Native.respond_to?(:attribute_packet)
145
+
84
146
  # leptris-ruby#529: the whole XPath node-set result as an Array
85
147
  # of registered natives in ONE C crossing (Document#
86
148
  # xpath_result_rows_bulk). The eager lane below crosses once and
@@ -1018,11 +1080,15 @@ module Moxml
1018
1080
  def same_node?(one, other)
1019
1081
  return true if one.equal?(other)
1020
1082
 
1021
- if one.is_a?(::Leptris::XML::Attr) ||
1022
- other.is_a?(::Leptris::XML::Attr)
1023
- return one.is_a?(::Leptris::XML::Attr) &&
1024
- other.is_a?(::Leptris::XML::Attr) &&
1025
- one.name == other.name &&
1083
+ attr_like = ->(node) do
1084
+ node.is_a?(::Leptris::XML::Attr) || node.is_a?(SnapshotAttribute)
1085
+ end
1086
+ if attr_like.call(one) || attr_like.call(other)
1087
+ return false unless attr_like.call(one) && attr_like.call(other)
1088
+
1089
+ name_one = one.is_a?(SnapshotAttribute) ? one.qualified_name : one.name
1090
+ name_other = other.is_a?(SnapshotAttribute) ? other.qualified_name : other.name
1091
+ return name_one == name_other &&
1026
1092
  same_node?(one.element, other.element)
1027
1093
  end
1028
1094
 
@@ -2081,7 +2147,9 @@ module Moxml
2081
2147
 
2082
2148
  def set_namespace(node, namespace)
2083
2149
  node = to_binding(node) if NATIVE_READ_LAYER
2084
- return set_attribute_namespace(node, namespace) if node.is_a?(::Leptris::XML::Attr)
2150
+ if node.is_a?(::Leptris::XML::Attr) || node.is_a?(SnapshotAttribute)
2151
+ return set_attribute_namespace(node, namespace)
2152
+ end
2085
2153
 
2086
2154
  element = node
2087
2155
  if namespace.nil?
@@ -2139,7 +2207,8 @@ module Moxml
2139
2207
  element = attr.element
2140
2208
  local = attr.name.include?(":") ? attr.name.split(":", 2)[1] : attr.name
2141
2209
  value = attr.value
2142
- element.remove_attribute(attr.name)
2210
+ qualified_old = attr.is_a?(SnapshotAttribute) ? attr.qualified_name : attr.name
2211
+ element.remove_attribute(qualified_old)
2143
2212
  prefix = namespace.is_a?(String) ? nil : namespace.prefix
2144
2213
  uri = namespace.is_a?(String) ? namespace : namespace.href
2145
2214
  qualified = prefix.nil? || prefix.empty? ? local : "#{prefix}:#{local}"
@@ -2147,12 +2216,35 @@ module Moxml
2147
2216
  if prefix && !prefix.empty? && !uri.to_s.empty? && !resolve_prefix_ns(element, prefix)
2148
2217
  element.add_namespace_definition(prefix, uri.to_s)
2149
2218
  end
2219
+ if attr.is_a?(SnapshotAttribute)
2220
+ # Mutation-return contract: hand back the same native
2221
+ # CLASS the caller tracks, refreshed for the new qname.
2222
+ return SnapshotAttribute.new(local, qualified, value, element)
2223
+ end
2224
+
2150
2225
  element.attribute_nodes.reverse.find { |candidate| candidate.name == qualified }
2151
2226
  end
2152
2227
 
2153
2228
  def namespace(node)
2154
2229
  node = to_binding(node) if NATIVE_READ_LAYER
2155
2230
 
2231
+ # SnapshotAttribute: resolve the prefix against the owning
2232
+ # element's scope (same contract as the Attr branch below);
2233
+ # unprefixed names (plain attributes, xmlns declarations)
2234
+ # resolve the real binding Attr for the declaration walk.
2235
+ if node.is_a?(SnapshotAttribute)
2236
+ if (prefix = prefix_part(node.qualified_name))
2237
+ resolved = resolve_prefix_ns(node.element, prefix)
2238
+ return resolved if resolved
2239
+ return implicit_xml_namespace(node) if prefix == "xml"
2240
+
2241
+ return nil
2242
+ end
2243
+
2244
+ real = node.resolve!
2245
+ return real && namespace(real)
2246
+ end
2247
+
2156
2248
  # The binding resolves Attr#namespace to the declaration but
2157
2249
  # without the prefix; when the qualified name carries one,
2158
2250
  # prefer the in-scope declaration that binds it so wrappers
@@ -2218,7 +2310,12 @@ module Moxml
2218
2310
  end
2219
2311
 
2220
2312
  def implicit_xml_namespace(node)
2221
- owner = node.is_a?(::Leptris::XML::Attr) ? node.element : node
2313
+ owner = if node.is_a?(::Leptris::XML::Attr) ||
2314
+ node.is_a?(SnapshotAttribute)
2315
+ node.element
2316
+ else
2317
+ node
2318
+ end
2222
2319
  return nil unless owner.is_a?(::Leptris::XML::Element)
2223
2320
 
2224
2321
  ::Leptris::XML::Namespace.new(owner, RESERVED_XML_URI, prefix: "xml")
@@ -2241,6 +2338,12 @@ module Moxml
2241
2338
  processing_instruction doctype].freeze
2242
2339
 
2243
2340
  def node_type(node)
2341
+ # SnapshotAttribute natives are first-class attributes: the
2342
+ # listing face and the pure-Ruby XPath engine's attribute
2343
+ # steps both produce them; reads (name/value/element) are
2344
+ # complete on the snapshot itself.
2345
+ return :attribute if node.is_a?(SnapshotAttribute)
2346
+
2244
2347
  if node.is_a?(::Leptris::XML::Node) &&
2245
2348
  node.instance_variable_defined?(:@node_type)
2246
2349
  kind = node.instance_variable_get(:@node_type)
@@ -2788,20 +2891,72 @@ module Moxml
2788
2891
  end
2789
2892
 
2790
2893
  def attributes(element)
2791
- element = to_binding(element)
2792
- element.attribute_nodes
2894
+ # moxml#520: serve the listing from the binding's flat packet
2895
+ # face when the native layer exposes it — ONE flat array of
2896
+ # interned names + fresh values, no per-element memo, no
2897
+ # pair arrays, no Attr objects, no FFI::Pointer. The native
2898
+ # node's own (document, address) serve directly: to_binding
2899
+ # converts per call (a Node.wrap + registry hit, ~6s/3M
2900
+ # allocations per million-element walk), and only mutation
2901
+ # faces need the binding element.
2902
+ if ELEMENT_ATTR_PACKET
2903
+ doc, addr = attr_packet_target(element)
2904
+ if doc
2905
+ flat = ::Leptris::XML::Native.attribute_pairs(doc, addr)
2906
+ return packet_attributes(flat, element)
2907
+ end
2908
+ end
2909
+
2910
+ binding_element = to_binding(element)
2911
+ unless binding_element.respond_to?(:attribute_pairs)
2912
+ return binding_element.attribute_nodes
2913
+ end
2914
+
2915
+ binding_element.attribute_pairs.map do |qualified, value|
2916
+ SnapshotAttribute.new(local_name(qualified), qualified, value,
2917
+ binding_element)
2918
+ end
2919
+ end
2920
+
2921
+ def attr_packet_target(element)
2922
+ if NATIVE_READ_LAYER && element.is_a?(NN)
2923
+ [NN_DOCUMENT.bind_call(element), element.address]
2924
+ elsif element.respond_to?(:c_address)
2925
+ [element.document, element.c_address]
2926
+ end
2927
+ end
2928
+
2929
+ def packet_attributes(flat, node)
2930
+ result = Array.new(flat.size / 2)
2931
+ i = 0
2932
+ j = 0
2933
+ while i < flat.size
2934
+ qualified = flat[i]
2935
+ result[j] = SnapshotAttribute.new(local_name(qualified), qualified,
2936
+ flat[i + 1], node)
2937
+ i += 2
2938
+ j += 1
2939
+ end
2940
+ result
2793
2941
  end
2794
2942
 
2795
2943
  # One C crossing per element on bindings with the bulk face
2796
2944
  # (leptris-ruby#278): interned names, fresh values, zero
2797
2945
  # Attr objects. Face-probed, not version-gated — the floor
2798
2946
  # (1.9.194.1) admits bindings without it, which take the
2799
- # base default over #attributes.
2947
+ # base default over #attributes. The packet face
2948
+ # (ELEMENT_ATTR_PACKET, 1.9.406.1) outranks the snapshot
2949
+ # face: same stride, no binding-side memo retention.
2800
2950
  def attribute_pairs(element)
2801
2951
  binding_element = to_binding(element)
2802
2952
  return super unless binding_element.respond_to?(:attribute_pairs)
2803
2953
 
2804
- pairs = if ELEMENT_ATTR_SNAPSHOT
2954
+ pairs = if ELEMENT_ATTR_PACKET
2955
+ ::Leptris::XML::Native
2956
+ .attribute_packet(binding_element.document,
2957
+ binding_element.c_address)
2958
+ .each_slice(2).to_a
2959
+ elsif ELEMENT_ATTR_SNAPSHOT
2805
2960
  binding_element.attributes_snapshot.to_a
2806
2961
  else
2807
2962
  binding_element.attribute_pairs
@@ -2811,11 +2966,33 @@ module Moxml
2811
2966
  pairs.map { |(name, value)| [local_name(name), value] }
2812
2967
  end
2813
2968
 
2969
+ # moxml#520: local-name splits are interned — element/attribute
2970
+ # vocabularies are bounded per document, so the same qualified
2971
+ # names recur; one frozen local string per name instead of an
2972
+ # allocation per read. Bounded so an adversarial document
2973
+ # cannot grow the table without limit.
2974
+ LOCAL_NAME_INTERN_MAX = 4096
2975
+
2976
+ def local_name_intern
2977
+ @local_name_intern ||= {}
2978
+ end
2979
+
2814
2980
  def local_name(name)
2815
- name.include?(":") ? name.split(":", 2)[1] : name
2981
+ return name unless name.include?(":")
2982
+
2983
+ cached = local_name_intern[name]
2984
+ return cached if cached
2985
+
2986
+ local = name.split(":", 2)[1].freeze
2987
+ if local_name_intern.size < LOCAL_NAME_INTERN_MAX
2988
+ local_name_intern[name] = local
2989
+ end
2990
+ local
2816
2991
  end
2817
2992
 
2818
2993
  def attribute_element(attr)
2994
+ return attr.element if attr.is_a?(SnapshotAttribute)
2995
+
2819
2996
  # XPath results mint ResultAttr (parent-backed); document
2820
2997
  # attributes are binding Attr (element-backed).
2821
2998
  if attr.is_a?(::Leptris::XML::ResultAttr)
@@ -2826,6 +3003,8 @@ module Moxml
2826
3003
  end
2827
3004
 
2828
3005
  def attribute_name(attr)
3006
+ return attr.name if attr.is_a?(SnapshotAttribute)
3007
+
2829
3008
  # leptris Attr names are qualified; the wrapper composes the
2830
3009
  # prefix, so expose the local part.
2831
3010
  return attr.name.split(":", 2)[1] if attr.name.include?(":")
@@ -2854,6 +3033,19 @@ module Moxml
2854
3033
  end
2855
3034
 
2856
3035
  def set_attribute_name(attr, name)
3036
+ if attr.is_a?(SnapshotAttribute)
3037
+ # Renames go through the owning element; the mutation
3038
+ # return contract hands back the same native CLASS the
3039
+ # caller tracks (a refreshed snapshot), not the binding
3040
+ # Attr the rename minted.
3041
+ element = attr.element
3042
+ value = attr.value
3043
+ element.remove_attribute(attr.qualified_name)
3044
+ element[name.to_s] = value
3045
+ return SnapshotAttribute.new(name.to_s, name.to_s, value,
3046
+ element)
3047
+ end
3048
+
2857
3049
  # Attr is an immutable value object; renames go through the
2858
3050
  # element and yield a fresh native, which the wrapper adopts.
2859
3051
  element = attr.element
@@ -2864,13 +3056,31 @@ module Moxml
2864
3056
  end
2865
3057
 
2866
3058
  def set_attribute_value(attr, value)
3059
+ if attr.is_a?(SnapshotAttribute)
3060
+ # Write through the element (the engine mutates in place);
3061
+ # refresh the snapshot so the wrapper's memo serves the
3062
+ # new value without a crossing.
3063
+ attr.element[attr.qualified_name] = value.to_s
3064
+ attr.value = value.to_s
3065
+ return attr
3066
+ end
3067
+
2867
3068
  attr.value = value.to_s
2868
3069
  attr
2869
3070
  end
2870
3071
 
2871
3072
  def get_attribute(element, name)
2872
3073
  element = to_binding(element)
2873
- element.attribute_nodes.find { |attr| attr.name == name.to_s }
3074
+ return element.attribute_nodes.find { |attr| attr.name == name.to_s } unless
3075
+ element.respond_to?(:attribute_pairs)
3076
+
3077
+ # moxml#520: locate through the one-crossing pairs face —
3078
+ # attribute_nodes mints a binding Attr + FFI::Pointer per
3079
+ # attribute just to find one.
3080
+ pair = element.attribute_pairs.find { |qualified, _| qualified == name.to_s }
3081
+ return nil unless pair
3082
+
3083
+ SnapshotAttribute.new(local_name(pair[0]), pair[0], pair[1], element)
2874
3084
  end
2875
3085
 
2876
3086
  def get_attribute_value(element, name)
@@ -2884,6 +3094,11 @@ module Moxml
2884
3094
  end
2885
3095
 
2886
3096
  def remove_attribute_native(attr)
3097
+ if attr.is_a?(SnapshotAttribute)
3098
+ attr.element.remove_attribute(attr.qualified_name)
3099
+ return attr
3100
+ end
3101
+
2887
3102
  attr.element.remove_attribute(attr.name)
2888
3103
  attr
2889
3104
  end
data/lib/moxml/config.rb CHANGED
@@ -62,30 +62,63 @@ module Moxml
62
62
  detect_loaded_adapter || FALLBACK_ADAPTER
63
63
  end
64
64
 
65
+ # Clears the memoized adapter-selection state (capability-probe
66
+ # verdict, resolved default name, default Config instance).
67
+ # Call after repointing default_adapter= at runtime; tests
68
+ # exercising selection from cold use it too (#523).
69
+ def reset_adapter_selection!
70
+ @leptris_preferred_available = nil
71
+ @default_adapter = nil
72
+ @default = nil
73
+ end
74
+
65
75
  # True when the leptris gem is installed AND meets the adapter's
66
- # binding floor (issue #149; 1.9.32 — traverse bounding #89,
67
- # built-docs parts #91, and every capability surface the adapter
68
- # now assumes). Memoized: the probe runs once. Below the floor
69
- # the default falls back to Nokogiri rather than driving a
70
- # binding the adapter no longer accommodates.
76
+ # binding floor (issue #149) AND answers a one-shot native
77
+ # capability probe (moxml#523). Memoized: the probe runs once.
78
+ # Below the floor, or when the engine's native build fails at
79
+ # runtime (Ruby 4.0's ABI: every plan build answers status=-5,
80
+ # leptris#1966 — the poison-process analog of the yeptris gate,
81
+ # lutaml-model#978), the default falls back to Nokogiri rather
82
+ # than driving a lane that breaks on first serialize.
71
83
  def leptris_preferred_available?
72
- return @leptris_preferred_available if defined?(@leptris_preferred_available)
84
+ return @leptris_preferred_available unless @leptris_preferred_available.nil?
73
85
 
74
86
  @leptris_preferred_available =
75
87
  begin
76
88
  require "leptris"
77
89
  Moxml::Adapter.load(:leptris)
78
- Gem::Version.new(::Leptris::VERSION) >=
79
- Gem::Version.new(Moxml::Adapter::Leptris::MINIMUM_BINDING_VERSION)
90
+ if Gem::Version.new(::Leptris::VERSION) >=
91
+ Gem::Version.new(Moxml::Adapter::Leptris::MINIMUM_BINDING_VERSION)
92
+ leptris_native_probe
93
+ else
94
+ false
95
+ end
80
96
  rescue LoadError
81
97
  # Not installed: a normal deployment shape, silent.
82
98
  false
83
- rescue NameError, Moxml::AdapterError
84
- # Installed but unusable (adapter load failure): loud.
99
+ rescue StandardError
100
+ # Installed but unusable — adapter load failure (NameError,
101
+ # AdapterError; #384) or the native probe answering with a
102
+ # failure status (Ruby 4.0 plan builds, leptris#1966): loud.
85
103
  :broken
86
104
  end
87
105
  end
88
106
 
107
+ # One-shot native capability probe (moxml#523): parse + serialize
108
+ # a tiny document through the leptris lane — the smallest
109
+ # faithful walk of the plan-build path whose status=-5 failure
110
+ # poisons the whole lane under broken-ABI rubies (leptris#1966).
111
+ # The Config carries every knob explicitly so nothing here
112
+ # consults Config.default, which would re-enter selection
113
+ # (probe → default → probe). Runs once per process; the caller
114
+ # memoizes the outcome.
115
+ def leptris_native_probe
116
+ config = new(PREFERRED_ADAPTER, true, "UTF-8")
117
+ Moxml::Context.new(config)
118
+ .parse("<r><a t=\"1\">x</a></r>").to_xml
119
+ true
120
+ end
121
+
89
122
  def detect_loaded_adapter
90
123
  return :nokogiri if Object.const_defined?(:Nokogiri)
91
124
  return :ox if Object.const_defined?(:Ox)
data/lib/moxml/context.rb CHANGED
@@ -26,8 +26,11 @@ module Moxml
26
26
  attr_reader :config
27
27
 
28
28
  def initialize(adapter = nil)
29
- Adapter.warn_deprecated!(adapter) if adapter
30
- @config = Config.new(adapter)
29
+ Adapter.warn_deprecated!(adapter) if adapter && !adapter.is_a?(Config)
30
+ # A Config instance is accepted as-is (moxml#523): the probe
31
+ # builds a fully-specified Config so selection is never
32
+ # re-entered through Config.default.
33
+ @config = adapter.is_a?(Config) ? adapter : Config.new(adapter)
31
34
  # Bumped on any namespace-scope mutation; Element scope caches
32
35
  # compare against it so every wrapper sees changes without
33
36
  # needing wrapper identity.
@@ -205,10 +208,30 @@ module Moxml
205
208
  # Moxml::NotImplementedError; Moxml::XSD.supported?(self)
206
209
  # answers without raising.
207
210
  #
211
+ # Composing sources — xs:include / xs:redefine / xs:override
212
+ # (moxml#522) — are REFUSED here with actionable guidance: the
213
+ # bare-string face cannot resolve their schemaLocation
214
+ # attributes, so the compile silently misses every included
215
+ # component and validation fails with misleading "not declared"
216
+ # findings. Use #xsd_file (the engine resolves schemaLocation
217
+ # against the file's directory) or #xsd_set (imports resolve
218
+ # across the whole set).
219
+ #
208
220
  # @raise [Moxml::NotImplementedError] on adapters without a validator
221
+ # @raise [ArgumentError] on a composing source (#522)
222
+ COMPOSITION_DIRECTIVE = /<(?:[\w.-]+:)?(?:include|redefine|override)[\s>]/
223
+
209
224
  def xsd(schema_source)
210
- handle = config.adapter.xsd_compile(schema_source.to_s)
211
- Moxml::XSD::Schema.new(handle, self, schema_source.to_s)
225
+ source = schema_source.to_s
226
+ if source.match?(COMPOSITION_DIRECTIVE)
227
+ raise ArgumentError,
228
+ "the schema composes other documents (include/redefine/" \
229
+ "override); the bare-string face cannot resolve their " \
230
+ "schemaLocation — use Context#xsd_file (per-file include " \
231
+ "resolution) or Context#xsd_set (multi-document compile)"
232
+ end
233
+ handle = config.adapter.xsd_compile(source)
234
+ Moxml::XSD::Schema.new(handle, self, source)
212
235
  end
213
236
 
214
237
  # Compile an XSD schema from a file path — <xs:include href>
data/lib/moxml/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Moxml
4
- VERSION = "0.6.2"
4
+ VERSION = "0.6.3"
5
5
  end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # leptris-ruby#586 adoption — the flat packet attribute face
6
+ # (binding 1.9.406.1): one crossing, interned names, fresh values,
7
+ # no per-element memo retention on the binding side. The facade
8
+ # listing face stays bare names (uniform with every adapter),
9
+ # document order. Older bindings keep the snapshot face
10
+ # (ELEMENT_ATTR_SNAPSHOT); the packet path must be stride-identical
11
+ # to it — parity is the contract, allocations are the win.
12
+ RSpec.describe "leptris element-attribute packet hydration" do
13
+ def bare(pairs)
14
+ pairs.map { |(name, value)| [name.split(":", 2).last, value] }
15
+ end
16
+ let(:ctx) { Moxml.new(:leptris) }
17
+
18
+ context "when the packet face is armed (>= 1.9.406.1)" do
19
+ before do
20
+ unless Moxml::Adapter::Leptris::ELEMENT_ATTR_PACKET
21
+ skip "binding #{Leptris::VERSION} has no attribute_packet face"
22
+ end
23
+ end
24
+
25
+ it "matches the binding per-attribute face stride for stride" do
26
+ doc = ctx.parse(
27
+ '<root a="1" xml:lang="en" xmlns:ns="urn:u" ns:p="v"/>',
28
+ )
29
+ binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
30
+ expected = bare(binding_element.attribute_pairs)
31
+
32
+ expect(doc.root.attribute_pairs).to eq(expected)
33
+ expect(doc.root.attribute_pairs).to eq(
34
+ [["a", "1"], ["lang", "en"], ["p", "v"]],
35
+ )
36
+ end
37
+
38
+ it "keeps document order across plain and prefixed attributes" do
39
+ doc = ctx.parse(
40
+ '<root z="9" xmlns:m="urn:m" m:x="8" k="7"/>',
41
+ )
42
+ expect(doc.root.attribute_pairs.map(&:first)).to eq(
43
+ %w[z x k],
44
+ )
45
+ end
46
+
47
+ it "hydrates child elements the same way" do
48
+ doc = ctx.parse('<r><child b="2" c="3"/></r>')
49
+ expect(doc.at("//child").attribute_pairs).to eq(
50
+ [["b", "2"], ["c", "3"]],
51
+ )
52
+ end
53
+
54
+ it "yields an empty list for attribute-less elements" do
55
+ doc = ctx.parse("<root/>")
56
+ expect(doc.root.attribute_pairs).to eq([])
57
+ end
58
+
59
+ it "mints nothing into the binding identity cache" do
60
+ doc = ctx.parse('<root a="1" b="2"/>')
61
+ binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
62
+ before = binding_element.document.native_cache.size
63
+ doc.root.attribute_pairs
64
+ expect(binding_element.document.native_cache.size).to eq(before)
65
+ end
66
+ end
67
+
68
+ context "with any arm state" do
69
+ it "agrees with the binding per-attribute face" do
70
+ doc = ctx.parse(
71
+ '<root a="1" xml:lang="en" xmlns:ns="urn:u" ns:p="v"/>',
72
+ )
73
+ binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
74
+ expect(doc.root.attribute_pairs).to eq(
75
+ bare(binding_element.attribute_pairs),
76
+ )
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ require "tempfile"
6
+
7
+ # moxml#522 re-audit on the current stack: the three composed-schema
8
+ # families (attribute admission through included attributeGroups,
9
+ # chameleon type binding, substitution heads across includes) all
10
+ # validate through the composition-capable faces — xsd_file (the
11
+ # engine resolves schemaLocation against the file's directory) and
12
+ # compile_set. The bare STRING face cannot chase files, so it
13
+ # refuses composition with actionable guidance instead of the
14
+ # misleading "not declared (strict)" cascade (#522's original
15
+ # evidence).
16
+ RSpec.describe "composed-schema validation (moxml#522)" do
17
+ let(:ctx) { Moxml.new(:leptris) }
18
+
19
+ def with_schema_dir(&block)
20
+ dir = Dir.mktmpdir
21
+ File.write(File.join(dir, "group.xsd"), <<~XSD)
22
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
23
+ <xs:attributeGroup name="g">
24
+ <xs:attribute name="foo" type="xs:string"/>
25
+ <xs:attribute name="bar" type="xs:integer"/>
26
+ </xs:attributeGroup>
27
+ <xs:element name="head" type="xs:string"/>
28
+ </xs:schema>
29
+ XSD
30
+ File.write(File.join(dir, "doc.xsd"), <<~XSD)
31
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
32
+ <xs:include schemaLocation="group.xsd"/>
33
+ <xs:element name="doc">
34
+ <xs:complexType>
35
+ <xs:attributeGroup ref="g"/>
36
+ </xs:complexType>
37
+ </xs:element>
38
+ <xs:element name="container">
39
+ <xs:complexType>
40
+ <xs:sequence>
41
+ <xs:element ref="head"/>
42
+ </xs:sequence>
43
+ </xs:complexType>
44
+ </xs:element>
45
+ <xs:element name="member" type="xs:string"
46
+ substitutionGroup="head"/>
47
+ </xs:schema>
48
+ XSD
49
+ yield dir
50
+ ensure
51
+ FileUtils.remove_entry(dir) if dir
52
+ end
53
+
54
+ context "with the composition-capable faces" do
55
+ it "validates attributeGroup composition across an include" do
56
+ with_schema_dir do |dir|
57
+ schema = ctx.xsd_file(File.join(dir, "doc.xsd"))
58
+ expect(schema).to be_valid(%(<doc foo="hello" bar="42"/>))
59
+ end
60
+ end
61
+
62
+ it "validates a substitution member under an included head" do
63
+ with_schema_dir do |dir|
64
+ schema = ctx.xsd_file(File.join(dir, "doc.xsd"))
65
+ expect(schema).to be_valid(%(<container><member>v</member></container>))
66
+ end
67
+ end
68
+
69
+ it "rejects a non-member where the head is required" do
70
+ with_schema_dir do |dir|
71
+ schema = ctx.xsd_file(File.join(dir, "doc.xsd"))
72
+ expect(schema).not_to be_valid(
73
+ %(<container><stranger>v</stranger></container>),
74
+ )
75
+ end
76
+ end
77
+
78
+ it "validates chameleon type binding through compile_set" do
79
+ dir = Dir.mktmpdir
80
+ File.write(File.join(dir, "types.xsd"), <<~XSD)
81
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
82
+ <xs:simpleType name="code">
83
+ <xs:restriction base="xs:string">
84
+ <xs:pattern value="[A-Z]{3}"/>
85
+ </xs:restriction>
86
+ </xs:simpleType>
87
+ </xs:schema>
88
+ XSD
89
+ File.write(File.join(dir, "cham.xsd"), <<~XSD)
90
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
91
+ targetNamespace="urn:d" xmlns:d="urn:d"
92
+ elementFormDefault="qualified">
93
+ <xs:include schemaLocation="types.xsd"/>
94
+ <xs:element name="a1" type="d:code"/>
95
+ </xs:schema>
96
+ XSD
97
+ schema = ctx.xsd_set(File.join(dir, "types.xsd"),
98
+ File.join(dir, "cham.xsd"))
99
+ expect(schema).to be_valid(%(<a1 xmlns="urn:d">ABC</a1>))
100
+ ensure
101
+ FileUtils.remove_entry(dir) if dir
102
+ end
103
+ end
104
+
105
+ context "with the bare string face" do
106
+ it "refuses composition with actionable guidance" do
107
+ source = <<~XSD
108
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
109
+ <xs:include schemaLocation="group.xsd"/>
110
+ <xs:element name="doc" type="xs:string"/>
111
+ </xs:schema>
112
+ XSD
113
+ expect { ctx.xsd(source) }
114
+ .to raise_error(ArgumentError, %r{xsd_file|compile_set})
115
+ end
116
+
117
+ it "keeps accepting self-contained sources" do
118
+ schema = ctx.xsd(<<~XSD)
119
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
120
+ <xs:element name="doc" type="xs:string"/>
121
+ </xs:schema>
122
+ XSD
123
+ expect(schema).to be_valid(%(<doc>hi</doc>))
124
+ end
125
+ end
126
+ end
@@ -8,17 +8,38 @@ require "spec_helper"
8
8
  # itself; an install without the gem at all stays silent (a normal
9
9
  # deployment shape).
10
10
  RSpec.describe "Config default-adapter degradation" do
11
- def with_broken_leptris
12
- old_default_adapter =
13
- Moxml::Config.instance_variable_get(:@default_adapter)
14
- Moxml::Config.remove_instance_variable(:@default_adapter) if old_default_adapter
15
- # The state a broken install produces (adapter load failure with
16
- # the gem present): present-but-unusable, not absent.
17
- Moxml::Config.instance_variable_set(:@leptris_preferred_available, :broken)
11
+ # Cold selection without ivar access: the public reset clears the
12
+ # memoized probe verdict, resolved default name, and default
13
+ # Config instance (moxml#523 seam). The ambient default name is
14
+ # captured before the reset and re-pinned through the public
15
+ # writer afterwards — the surrounding suite may depend on a
16
+ # default pinned at require time (canon pins nokogiri).
17
+ def with_cold_selection
18
+ ambient = Moxml::Config.default_adapter
19
+ Moxml::Config.reset_adapter_selection!
18
20
  yield
19
21
  ensure
20
- Moxml::Config.remove_instance_variable(:@leptris_preferred_available)
21
- Moxml::Config.instance_variable_set(:@default_adapter, :nokogiri)
22
+ Moxml::Config.default_adapter = ambient
23
+ end
24
+
25
+ # The state a broken install produces: the gem is present and
26
+ # loads, but the engine's native build fails — simulated at the
27
+ # probe seam the selection actually consults (#523). A broken lane
28
+ # ENDS on the fallback adapter, so after the example the broken
29
+ # examples leave nokogiri pinned (the state consumers actually
30
+ # run under) — pinned AFTER with_cold_selection's ambient restore.
31
+ def with_broken_leptris
32
+ with_cold_selection do
33
+ allow(Moxml::Config).to receive(:leptris_native_probe).and_raise(
34
+ Moxml::AdapterError.new(
35
+ "plan result build failed (status=-5)",
36
+ adapter: :leptris,
37
+ operation: "probe",
38
+ ),
39
+ )
40
+ yield
41
+ end
42
+ Moxml::Config.default_adapter = :nokogiri
22
43
  end
23
44
 
24
45
  it "warns loudly and names the fallback" do
@@ -36,8 +57,59 @@ RSpec.describe "Config default-adapter degradation" do
36
57
  end
37
58
 
38
59
  it "keeps a working leptris install silent" do
39
- expect do
40
- Moxml::Config.default_adapter
41
- end.not_to output(/unusable/).to_stderr
60
+ with_cold_selection do
61
+ expect do
62
+ Moxml::Config.default_adapter
63
+ end.not_to output(/unusable/).to_stderr
64
+ end
65
+ end
66
+
67
+ # moxml#523: bundle presence is not capability. Under broken-ABI
68
+ # rubies (Ruby 4.0 plan builds answer status=-5, leptris#1966) a
69
+ # present leptris poisoned the whole lane. Selection now runs a
70
+ # one-shot parse+serialize probe and treats failure as
71
+ # present-but-unusable: loud warning, nokogiri fallback.
72
+ describe "native capability probe (#523)" do
73
+ before do
74
+ present = Gem::Specification.find_by_name("leptris")
75
+ skip "leptris gem not available" unless present
76
+ rescue Gem::LoadError
77
+ skip "leptris gem not available"
78
+ end
79
+
80
+ def with_failing_native_probe
81
+ with_cold_selection do
82
+ allow(Moxml::Adapter::Leptris).to receive(:parse).and_raise(
83
+ Moxml::AdapterError.new(
84
+ "plan result build failed (status=-5)",
85
+ adapter: :leptris,
86
+ operation: "parse",
87
+ ),
88
+ )
89
+ yield
90
+ end
91
+ end
92
+
93
+ it "falls back loudly when the native build fails the probe" do
94
+ with_failing_native_probe do
95
+ expect do
96
+ expect(Moxml::Config.default_adapter).to eq(:nokogiri)
97
+ end.to output(/leptris adapter is installed but unusable/).to_stderr
98
+ end
99
+ end
100
+
101
+ it "memoizes the failed probe (one run per process)" do
102
+ with_failing_native_probe do
103
+ expect(Moxml::Config.leptris_preferred_available?).to eq(:broken)
104
+ expect(Moxml::Adapter::Leptris).not_to(receive(:parse))
105
+ expect(Moxml::Config.leptris_preferred_available?).to eq(:broken)
106
+ end
107
+ end
108
+
109
+ it "keeps leptris when the probe passes on a healthy stack" do
110
+ with_cold_selection do
111
+ expect(Moxml::Config.default_adapter).to eq(:leptris)
112
+ end
113
+ end
42
114
  end
43
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Moxml is a unified XML manipulation library that provides a common API
@@ -40,6 +40,7 @@ files:
40
40
  - LICENSE.md
41
41
  - README.adoc
42
42
  - Rakefile
43
+ - bench/calibrate_520.rb
43
44
  - benchmark/README.md
44
45
  - benchmark/pipeline_bench.rb
45
46
  - benchmark/plan_bench.rb
@@ -474,9 +475,11 @@ files:
474
475
  - spec/moxml/adapter/headed_ox_spec.rb
475
476
  - spec/moxml/adapter/leptris_adoption_spec.rb
476
477
  - spec/moxml/adapter/leptris_append_gc_spec.rb
478
+ - spec/moxml/adapter/leptris_attribute_packet_spec.rb
477
479
  - spec/moxml/adapter/leptris_attribute_snapshot_spec.rb
478
480
  - spec/moxml/adapter/leptris_binding_cap_spec.rb
479
481
  - spec/moxml/adapter/leptris_children_columnar_spec.rb
482
+ - spec/moxml/adapter/leptris_composed_schema_spec.rb
480
483
  - spec/moxml/adapter/leptris_contract_reads_spec.rb
481
484
  - spec/moxml/adapter/leptris_crossdoc_replace_spec.rb
482
485
  - spec/moxml/adapter/leptris_detached_text_spec.rb