yeptris 0.6.78.0-s390x-linux → 0.6.80.0-s390x-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: e9f18a97db78d7df484e5fbc37381591433445ac31153c50c7ca8ffdd54e9948
4
- data.tar.gz: b321ab0d51064ec2792072165844bf0e8c28189612c21c919775748149353651
3
+ metadata.gz: 0a2bc0bd78a8d4c661927fdade0599f0d3b5fa04efadfdc6e3bdfb3216585dd3
4
+ data.tar.gz: 8c51b12350bb8fb882291e03e4f46e2589392566ff0efdb1445918d50eb639f2
5
5
  SHA512:
6
- metadata.gz: c0ee33fead72148d542ac2e5a27821d2064dd25d88cdaf117db6ce45c041ca59f6428b53fd6147d427a458c716667e0aa50a0897f60fcab4c85b233d77bfb2d2
7
- data.tar.gz: 8927387115ea7a1e143987dedde727f356ce9e664f28a4740ddf8c8f44081048f08c5e222dfc8fe1555b60cc98f1d37279cc7236a652da9da55f66e2c1947ede
6
+ metadata.gz: f5d81dbbd59b24ad7deb4ad467d9d1608af9f639a6f0782ade76cf3117da16f914c7002e68bd66be1572b16d7a6247fe50a303a3068cf1dcd02adde8bf5e048d
7
+ data.tar.gz: fe43311fcd39164f3a4aa6edca061839f153afdccd9d1d80f0fb8913f86fdf58f148d97fa7d05eb51acb3b8c47ac8f6bd723a10c630d4a638beba298373f714c
data/README.adoc CHANGED
@@ -106,12 +106,16 @@ benchmark of record — any performance claim runs through it.
106
106
  ....
107
107
  cd ext/yeptris_native
108
108
  YEPTRIS_LIB_PATH=/path/to/libyeptris.dylib ruby extconf.rb && make
109
- cp native.bundle ../../lib/yeptris/ # or .so on Linux
109
+ cp native_<major>_<minor>.bundle ../../lib/yeptris/ # or .so on Linux
110
110
  ....
111
111
 
112
- `require "yeptris"` picks it up automatically (`Yeptris::JSON` then
113
- uses it; missing builds silently use the fallback — the gem installs
114
- without compiling).
112
+ The bundle name is DOT-FREE per Ruby minor (`native_4_0.bundle` —
113
+ MRI derives the init symbol `Init_native_4_0` from the basename cut
114
+ at the first dot). `require "yeptris"` picks its own minor's bundle
115
+ up automatically (`Yeptris::JSON` then uses it); a missing build
116
+ falls back to the FFI ladder — the gem installs without compiling,
117
+ and a bundle built by any other minor is never attempted (the
118
+ wrong-ABI guard).
115
119
 
116
120
  == Shipped beyond the original plan
117
121
 
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Builds the native materializer for the CURRENT Ruby and installs it
4
- # under the per-minor DIRECTORY layout
5
- # (lib/yeptris/<major.minor>/native.<so|bundle|dll>).
3
+ # Builds the native materializer for the CURRENT Ruby and stages it
4
+ # as the per-minor DOT-FREE bundle
5
+ # (lib/yeptris/native_<major>_<minor>.<so|bundle|dll>; the org
6
+ # native-layer contract clause 2 — the leptris-ruby#671 canonical
7
+ # shape, migrating the old <minor>/native.so subdirs away).
6
8
  #
7
9
  # The leptris-ruby Windows lesson (#207/#227): a PE DLL cannot
8
10
  # resolve Ruby imports lazily — it must bind the build Ruby's
@@ -11,10 +13,10 @@
11
13
  # RUBY_VERSION at require. Ruby 3.3 has no arm64 build: that cell
12
14
  # ships no artifact and falls back loudly to the FFI ladder.
13
15
  #
14
- # The MINOR goes in the directory and the file name stays "native":
15
- # ruby derives the init symbol from the feature basename, so the old
16
- # native-<minor>.so name made require ask for the untypeable
17
- # "Init_native-3" and no shipped bundle could ever load (#157).
16
+ # MRI derives the init symbol from the feature basename cut at the
17
+ # first dot — native_3_4.so resolves Init_native_3_4 (the old
18
+ # native-<minor>.so name asked for the untypeable "Init_native-3",
19
+ # #157; the extconf's mkmf target name IS this file name).
18
20
  #
19
21
  # Unix bundles link the same shape build-platform-gem.sh uses — no
20
22
  # libruby DT_NEEDED (its absolute build-runner path dangles on user
@@ -22,14 +24,14 @@
22
24
  # darwin links with -undefined dynamic_lookup.
23
25
  #
24
26
  # Standalone by design: the release workflow runs this under each
25
- # Ruby minor (3.3/3.4/4.0) with no bundler context.
27
+ # Ruby minor of SUPPORTED_MINORS with no bundler context.
26
28
 
27
29
  require "rbconfig"
28
30
  require "fileutils"
29
31
 
30
32
  root = File.expand_path("..", __dir__)
31
33
  ext_dir = File.join(root, "ext", "yeptris_native")
32
- minor = RUBY_VERSION[/\A\d+\.\d+/]
34
+ minor = RUBY_VERSION[/\A\d+\.\d+/].tr(".", "_")
33
35
  windows = RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
34
36
 
35
37
  Dir.chdir(ext_dir) do
@@ -45,16 +47,17 @@ Dir.chdir(ext_dir) do
45
47
  "DLDFLAGS=#{dld}")
46
48
  end
47
49
  abort "make failed under #{RUBY_VERSION}" unless success
48
- so = Dir.glob("native.{so,bundle,dll}").first
50
+ so = Dir.glob("native_#{minor}.{so,bundle,dll}").first
49
51
  abort "native bundle not produced under #{RUBY_VERSION}" unless so
50
- dest_dir = File.join(root, "lib", "yeptris", minor)
52
+ dest_dir = File.join(root, "lib", "yeptris")
51
53
  dest = File.join(dest_dir, File.basename(so))
52
54
  # The artifact must reference only this minor's Ruby DLL.
53
55
  imported = `strings #{so} 2>/dev/null`[/[a-z0-9-]*ruby\d{3,}\.dll/i]
54
- if imported && !imported.include?("ruby#{minor.delete('.')}")
56
+ dll_minor = RUBY_VERSION[/\A\d+\.\d+/].delete(".")
57
+ if imported && !imported.include?("ruby#{dll_minor}")
55
58
  abort "#{so} imports #{imported} but was built under #{RUBY_VERSION} — refuse to mis-name it"
56
59
  end
57
60
  FileUtils.mkdir_p(dest_dir)
58
61
  FileUtils.cp(so, dest)
59
- puts "Installed native materializer for Ruby #{minor} -> #{dest} (#{imported || 'no ruby dll string found'})"
62
+ puts "Installed native materializer for Ruby #{RUBY_VERSION} -> #{dest} (#{imported || 'no ruby dll string found'})"
60
63
  end
@@ -2,8 +2,9 @@
2
2
 
3
3
  # Builds the vendored libyeptris at gem-install time so EVERY install
4
4
  # carries the engine (the pure `ruby`-platform gem has no prebuilt
5
- # binary). Platform gems vendor the prebuilt lib at the gem root —
6
- # when one is present this extension no-ops.
5
+ # binary). Platform gems vendor the prebuilt lib in the vendor tree
6
+ # (lib/yeptris/vendor/<platform>/) — when one is present this
7
+ # extension no-ops.
7
8
 
8
9
  require "mkmf"
9
10
  require "fileutils"
@@ -11,8 +12,13 @@ require "fileutils"
11
12
  gem_root = File.expand_path("../..", __dir__)
12
13
  vendored = File.join(gem_root, "vendor", "libyeptris")
13
14
 
14
- prebuilt = ["libyeptris.so", "libyeptris.dylib", "libyeptris.dll",
15
- "yeptris.dll"].any? { |n| File.exist?(File.join(gem_root, n)) }
15
+ # the prebuilt probes ride the loader's candidate layouts (the org
16
+ # native-layer contract clause 1): the platform gems' vendor tree,
17
+ # then the flat lib/ spelling
18
+ prebuilt = Dir[File.join(gem_root, "lib", "yeptris", "vendor", "*", "libyeptris.*")] +
19
+ %w[libyeptris.so libyeptris.dylib libyeptris.dll]
20
+ .map { |n| File.join(gem_root, "lib", n) }
21
+ prebuilt = prebuilt.any? { |f| File.exist?(f) }
16
22
 
17
23
  def write_dummy_makefile(reason)
18
24
  File.write("Makefile", <<~MAKE)
@@ -31,7 +37,8 @@ elsif find_executable("cmake").nil?
31
37
  write_dummy_makefile("yeptris: cmake not found; set YEPTRIS_LIB_PATH at require time")
32
38
  else
33
39
  # Out-of-source into the ext workdir; the shared lib then moves to
34
- # the GEM ROOT, where ffi.rb's ladder probes it.
40
+ # the flat lib/ layout (contract clause 1.3), where ffi.rb's
41
+ # candidate list probes it.
35
42
  build = File.join(Dir.pwd, "libyeptris-build")
36
43
  # BUILD_TESTING (the CMake standard) gates test/ — the vendored
37
44
  # tree ships no test dir; the static target must build because the
@@ -47,7 +54,8 @@ else
47
54
  system("cmake", "--build", build, "--config", "Release")
48
55
  lib = Dir.glob(File.join(build, "**", "libyeptris.{so,dylib}")).first
49
56
  if ok && lib
50
- FileUtils.cp(lib, File.join(gem_root, File.basename(lib)))
57
+ FileUtils.mkdir_p(File.join(gem_root, "lib"))
58
+ FileUtils.cp(lib, File.join(gem_root, "lib", File.basename(lib)))
51
59
  write_dummy_makefile("yeptris: built vendored libyeptris at gem install")
52
60
  else
53
61
  # a failed build must not abort the install — require-time raises
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Builds the native JSON/CBOR materializer (yeptris-ruby#311): the
4
- # per-minor prebuilt bundle loads FIRST ("lib/yeptris/<minor>/native")
5
- # and THIS build is the fallback slot ("require 'yeptris/native'") —
6
- # compiled by the installing Ruby, so it is ABI-correct on every
7
- # minor, present and future. Installs must NEVER fail: any miss (no
8
- # engine binary, no headers, no usable toolchain) degrades to a no-op
9
- # Makefile and the FFI ladder carries the load, exactly as today.
3
+ # Builds the native JSON/CBOR materializer (yeptris-ruby#311) as the
4
+ # per-minor DOT-FREE bundle the loader requires
5
+ # (lib/yeptris/native_<major>_<minor>.<ext>; the org native-layer
6
+ # contract clause 2). mkmf's target name IS the staged file name and
7
+ # the Init_ symbol — native_3_4 resolves Init_native_3_4 — so a pure
8
+ # (ruby-platform) gem's install lands the bundle exactly where the
9
+ # loader looks, for the installing Ruby, ABI-correct on every minor.
10
+ # Installs must NEVER fail: any miss (no engine binary, no headers,
11
+ # no usable toolchain) degrades to a no-op Makefile and the FFI
12
+ # ladder carries the load, exactly as today.
10
13
 
11
14
  require "mkmf"
12
15
  require "fileutils"
@@ -23,15 +26,20 @@ def write_dummy_makefile(reason)
23
26
  MAKE
24
27
  end
25
28
 
26
- # 1. The engine binary to LINK. Order: CI/dev explicit, then the gem
27
- # root — platform gems vendor the prebuilt lib there, and the pure
28
- # gem's ext/libyeptris extconf builds it there BEFORE this extension
29
- # runs (rubygems builds extensions in gemspec order).
29
+ # 1. The engine binary to LINK. Order mirrors the loader's candidate
30
+ # list (the org native-layer contract clause 1): CI/dev explicit,
31
+ # then the platform gems' vendor tree, then the flat lib/ layout
32
+ # (where the pure gem's ext/libyeptris extconf stages its build
33
+ # BEFORE this extension runs — rubygems builds extensions in
34
+ # gemspec order).
30
35
  lib = ENV["YEPTRIS_LIB_PATH"]
31
36
  lib = nil unless lib && File.file?(lib)
32
- lib ||= ["libyeptris.so", "libyeptris.dylib", "yeptris.dll"]
33
- .map { |n| File.join(gem_root, n) }
34
- .find { |f| File.exist?(f) }
37
+ if lib.nil?
38
+ vendor_libs = Dir[File.join(gem_root, "lib", "yeptris", "vendor", "*", "libyeptris.*")]
39
+ flat_libs = %w[libyeptris.so libyeptris.dylib libyeptris.dll]
40
+ .map { |n| File.join(gem_root, "lib", n) }
41
+ lib = (vendor_libs + flat_libs).find { |f| File.exist?(f) }
42
+ end
35
43
 
36
44
  # 2. The engine headers: CI/dev checkout, then the vendored tree that
37
45
  # rides every gem artifact (the packaging doctrine's recompile half).
@@ -81,18 +89,23 @@ else
81
89
 
82
90
  libdir = File.dirname(lib)
83
91
  $LIBPATH << libdir
84
- # the bundle must find libyeptris at require time; the gem root is
85
- # stable within an installation
92
+ # the bundle must find libyeptris at require time; the loader's
93
+ # ffi_lib resolves the engine FIRST (yeptris.rb requires ffi before
94
+ # this bundle), so the image is already mapped under the same
95
+ # soname/install name — the rpath only serves a standalone dlopen
86
96
  $LDFLAGS << " -Wl,-rpath,#{libdir}" if RUBY_PLATFORM =~ /linux|darwin/
87
97
 
88
98
  if have_library("yeptris", "yep_json_string") && have_library("yeptris", "yeptris_visit_json")
89
99
  # Windows resolves DLL dependencies beside the loading module —
90
- # install the dll beside the built bundle
91
- if RUBY_PLATFORM =~ /mswin|mingw/ && File.dirname(lib) != gem_root
100
+ # install the dll beside the built bundle (lib/yeptris/)
101
+ if RUBY_PLATFORM =~ /mswin|mingw/
92
102
  ($INSTALLFILES ||= []) << [lib, "$(RUBYARCHDIR)/yeptris"]
93
103
  end
94
104
  $CFLAGS << " -O3 -fvisibility=hidden" unless RUBY_PLATFORM =~ /mswin|mingw/
95
- create_makefile("yeptris/native")
105
+ # the DOT-FREE per-minor target (contract clause 2): the file
106
+ # name and the Init_ symbol in one — the loader's feature name
107
+ minor = RUBY_VERSION[/\A\d+\.\d+/].tr(".", "_")
108
+ create_makefile("yeptris/native_#{minor}")
96
109
  else
97
110
  write_dummy_makefile("yeptris: materializer link probe failed; the FFI ladder carries the load")
98
111
  end
@@ -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 78
15
- #define YEPTRIS_VERSION_STRING "0.6.78"
14
+ #define YEPTRIS_VERSION_PATCH 80
15
+ #define YEPTRIS_VERSION_STRING "0.6.80"
16
16
 
17
17
  #ifdef __cplusplus
18
18
  extern "C" {
@@ -465,6 +465,22 @@ static void native_self_check(void) {
465
465
  }
466
466
  }
467
467
 
468
+ /* The per-minor init symbols (the org native-layer contract
469
+ * clause 2): the DOT-FREE bundle name native_<major>_<minor>
470
+ * makes MRI dlsym Init_native_<minor> from the basename cut at
471
+ * the first dot. mkmf generates its own .def/exports listing
472
+ * just Init_native, so the per-minor entries are exported here
473
+ * (RUBY_FUNC_EXPORTED) and all route through the one
474
+ * Init_native body — the moxml double-load lesson: two gems'
475
+ * copies in one process re-running setup must stay idempotent,
476
+ * which the AVAILABLE guard below already guarantees. */
477
+ RUBY_FUNC_EXPORTED void Init_native_3_0(void);
478
+ RUBY_FUNC_EXPORTED void Init_native_3_1(void);
479
+ RUBY_FUNC_EXPORTED void Init_native_3_2(void);
480
+ RUBY_FUNC_EXPORTED void Init_native_3_3(void);
481
+ RUBY_FUNC_EXPORTED void Init_native_3_4(void);
482
+ RUBY_FUNC_EXPORTED void Init_native_4_0(void);
483
+ RUBY_FUNC_EXPORTED void Init_native_4_1(void);
468
484
  RUBY_FUNC_EXPORTED void Init_native(void) {
469
485
  utf8_enc = rb_utf8_encoding();
470
486
  native_self_check();
@@ -505,3 +521,25 @@ RUBY_FUNC_EXPORTED void Init_native(void) {
505
521
  const char* shape = getenv("YEPTRIS_NATIVE_SHAPE");
506
522
  if (shape != NULL && strcmp(shape, "natural") == 0) yep_rb_set_shape_mode(1);
507
523
  }
524
+ /* The per-minor init entries (the org native-layer contract
525
+ * clause 2): the DOT-FREE bundle name native_<major>_<minor>
526
+ * makes MRI dlsym Init_native_<minor> from the basename cut at
527
+ * the first dot. mkmf generates its own exports listing just
528
+ * Init_native, so the per-minor entries are exported here and
529
+ * all route through the one Init_native body — whose AVAILABLE
530
+ * guard keeps a second init (two gems' copies in one process,
531
+ * the #336 class) idempotent. The set covers SUPPORTED_MINORS
532
+ * (3.3/3.4/4.0) plus the s390x 3.2 floor and one minor of
533
+ * headroom; a minor outside the set raises the friendly
534
+ * fallback warning instead. 3.0/3.1 ride along for the
535
+ * windows mingw 3.0 leg — its mkmf generates a .def listing
536
+ * Init_native_3_0 from the target name, and ld refuses to
537
+ * export a symbol the object does not define ("cannot export
538
+ * Init_native_3_0: symbol not defined", run 45397564333). */
539
+ RUBY_FUNC_EXPORTED void Init_native_3_0(void) { Init_native(); }
540
+ RUBY_FUNC_EXPORTED void Init_native_3_1(void) { Init_native(); }
541
+ RUBY_FUNC_EXPORTED void Init_native_3_2(void) { Init_native(); }
542
+ RUBY_FUNC_EXPORTED void Init_native_3_3(void) { Init_native(); }
543
+ RUBY_FUNC_EXPORTED void Init_native_3_4(void) { Init_native(); }
544
+ RUBY_FUNC_EXPORTED void Init_native_4_0(void) { Init_native(); }
545
+ RUBY_FUNC_EXPORTED void Init_native_4_1(void) { Init_native(); }
@@ -190,6 +190,16 @@ class Yeptris::Document
190
190
  wrap_node(Yeptris::FFI.yeptris_document_root(@c_ptr, index))
191
191
  end
192
192
 
193
+ # Applies a compiled {Yeptris::Plan} to this document in ONE C
194
+ # pass (plan.h's DOM leg; yeptris-ruby#411): typed columns out, no
195
+ # per-node host dispatch. The result borrows this document's
196
+ # regions for its string columns — the returned PlanColumns
197
+ # retains the document, and free the RESULT first.
198
+ def plan_walk(plan)
199
+ ensure_alive!
200
+ ::Yeptris::Plan.walk_document(self, plan)
201
+ end
202
+
193
203
  # Bulk build (TODO.impl/15 phase D): one call raises the whole
194
204
  # tree from a flat entry array + blob (see YAML::BulkBuilder).
195
205
  def build_entries(entries, count, blob, blob_len)
@@ -0,0 +1,300 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yeptris"
4
+
5
+ module Yeptris
6
+ # The streaming faces (engine events.h; yeptris-ruby#411, the rs
7
+ # `pub mod event` / ts `events()` parity surface). Two consumption
8
+ # models over the one engine event stream:
9
+ #
10
+ # * {Puller} — the StAX-style cursor: one event per #next, zero
11
+ # tree, memory bounded by the puller's own arena. The bulk
12
+ # #next_batch rides yeptris_pull_next_batch (fewer crossings).
13
+ # * {Iterparse} — document-at-a-time over multi-document streams:
14
+ # one document's events per #next, the STREAM_START/STREAM_END
15
+ # pair framing the stream exactly once (in the first and last
16
+ # slices), the DOCUMENT_* pair framing every slice.
17
+ #
18
+ # Every {Event} carries plain Ruby values — value/anchor/tag are
19
+ # COPIED out of the puller's arena at hand-out (the C strings are
20
+ # valid only until the next call; nothing borrowed ever crosses to
21
+ # the caller).
22
+ module Events
23
+ # events.h's YeptrisEventType — ABI-pinned order.
24
+ EVENT_TYPES = {
25
+ 1 => :stream_start, 2 => :stream_end,
26
+ 3 => :document_start, 4 => :document_end,
27
+ 5 => :sequence_start, 6 => :sequence_end,
28
+ 7 => :mapping_start, 8 => :mapping_end,
29
+ 9 => :scalar, 10 => :alias,
30
+ }.freeze
31
+ private_constant :EVENT_TYPES
32
+
33
+ # dom.h's scalar styles (the FFI STYLE_* codes); 0 = no style.
34
+ STYLES = {
35
+ ::Yeptris::FFI::STYLE_PLAIN => :plain,
36
+ ::Yeptris::FFI::STYLE_SINGLE_QUOTED => :single_quoted,
37
+ ::Yeptris::FFI::STYLE_DOUBLE_QUOTED => :double_quoted,
38
+ ::Yeptris::FFI::STYLE_LITERAL => :literal,
39
+ ::Yeptris::FFI::STYLE_FOLDED => :folded,
40
+ }.freeze
41
+ private_constant :STYLES
42
+
43
+ # One engine event, plain Ruby values only.
44
+ class Event
45
+ attr_reader :kind, :style, :value, :anchor, :tag,
46
+ :line, :col, :end_line, :end_col
47
+ # Collection/document/scalar facts (events.h's flag bits).
48
+ attr_reader :flow, :explicit_marker, :implicit
49
+
50
+ # @api private — minted by the puller/iterparse from the C
51
+ # struct; the string fields copy here (the arena borrow ends at
52
+ # this call).
53
+ def initialize(c_ev)
54
+ @kind = EVENT_TYPES.fetch(c_ev[:type], c_ev[:type])
55
+ @style = STYLES[c_ev[:style]]
56
+ @value = copy_span(c_ev[:value], c_ev[:value_len])
57
+ @anchor = copy_span(c_ev[:anchor], c_ev[:anchor_len])
58
+ @tag = copy_span(c_ev[:tag], c_ev[:tag_len])
59
+ @line = c_ev[:line]
60
+ @col = c_ev[:col]
61
+ @end_line = c_ev[:end_line]
62
+ @end_col = c_ev[:end_col]
63
+ @flow = c_ev[:flow] != 0
64
+ @explicit_marker = c_ev[:explicit_marker] != 0
65
+ @implicit = c_ev[:implicit] != 0
66
+ end
67
+
68
+ # Collection events: flow style (`[a, b]` / `{a: b}`).
69
+ alias flow? flow
70
+
71
+ # Document events: the --- / ... markers were seen.
72
+ alias explicit_marker? explicit_marker
73
+
74
+ # Scalars: plain and untagged.
75
+ alias implicit? implicit
76
+
77
+ def inspect
78
+ facts = [@kind]
79
+ facts << @value.inspect if @value
80
+ facts << "@#{anchor}" if anchor
81
+ facts << "line #{line}, col #{col}"
82
+ "#<#{self.class.name} #{facts.join(' ')}>"
83
+ end
84
+
85
+ private
86
+
87
+ def copy_span(ptr, len)
88
+ return nil if ptr.null? || len.zero?
89
+
90
+ ptr.read_bytes(len).force_encoding(Encoding::UTF_8)
91
+ end
92
+ end
93
+
94
+ # Shared close/free discipline: one release, idempotent, use after
95
+ # close raises.
96
+ module Handle
97
+ def close
98
+ return if @closed
99
+
100
+ @closed = true
101
+ yield_free
102
+ nil
103
+ end
104
+
105
+ def freed?
106
+ @closed
107
+ end
108
+
109
+ def ensure_alive!
110
+ return unless @closed
111
+
112
+ raise ::Yeptris::FreedError,
113
+ "#{self.class.name.split('::').last} was closed"
114
+ end
115
+
116
+ private
117
+
118
+ def with_handle!
119
+ ensure_alive!
120
+ @c_ptr
121
+ end
122
+ end
123
+ private_constant :Handle
124
+
125
+ # The StAX-style pull cursor. The engine parses the whole input at
126
+ # .pull (the run completes into the cursor's arena), then hands
127
+ # events out one (or one batch) at a time; a parse failure is
128
+ # reported through #status with the 1-based position.
129
+ class Puller
130
+ include Enumerable
131
+ include Handle
132
+
133
+ # @api private — built by .pull
134
+ def initialize(c_ptr)
135
+ @c_ptr = c_ptr
136
+ @closed = false
137
+ end
138
+
139
+ # The next event, or nil at stream end / on error (distinguish
140
+ # with #status — the pull.c contract).
141
+ def next
142
+ p = with_handle!
143
+ raw = ::Yeptris::FFI.yeptris_pull_next(p)
144
+ return nil if raw.null?
145
+
146
+ Event.new(::Yeptris::FFI::CEvent.new(raw))
147
+ end
148
+
149
+ # Up to +max+ events per call (yeptris_pull_next_batch): one
150
+ # crossing per batch instead of one per event.
151
+ def next_batch(max)
152
+ p = with_handle!
153
+ return [] if max <= 0
154
+
155
+ buf = ::FFI::MemoryPointer.new(::Yeptris::FFI::CEvent, max)
156
+ n = ::Yeptris::FFI.yeptris_pull_next_batch(p, buf, max)
157
+ Array.new(n) do |i|
158
+ Event.new(::Yeptris::FFI::CEvent.new(buf + i * ::Yeptris::FFI::CEvent.size))
159
+ end
160
+ end
161
+
162
+ # The last call's verdict with its 1-based error position:
163
+ # {status:} is 0 (OK) while events flow; non-zero with
164
+ # {line:}/{col:} naming the failure (or 0,0 when the failure
165
+ # carries no position).
166
+ def status
167
+ p = with_handle!
168
+ line = ::FFI::MemoryPointer.new(:uint32)
169
+ col = ::FFI::MemoryPointer.new(:uint32)
170
+ st = ::Yeptris::FFI.yeptris_pull_status(p, line, col)
171
+ { status: st, line: line.read_uint32, col: col.read_uint32 }
172
+ end
173
+
174
+ # Yields every event until the stream ends.
175
+ def each
176
+ return enum_for(:each) unless block_given?
177
+
178
+ loop do
179
+ ev = self.next
180
+ break if ev.nil?
181
+
182
+ yield ev
183
+ end
184
+ nil
185
+ end
186
+
187
+ private
188
+
189
+ def yield_free
190
+ ::Yeptris::FFI.yeptris_pull_free(@c_ptr)
191
+ end
192
+ end
193
+
194
+ # Document-at-a-time iteration over multi-document streams: one
195
+ # document's events per #next (an Array), memory bounded by the
196
+ # largest document. Enumerable over the document slices.
197
+ class Iterparse
198
+ include Enumerable
199
+ include Handle
200
+
201
+ # @api private — built by .iterparse
202
+ def initialize(c_ptr)
203
+ @c_ptr = c_ptr
204
+ @closed = false
205
+ end
206
+
207
+ # The next document's events (Array of Event), or nil when the
208
+ # stream is exhausted. Raises Yeptris::ParseError when the
209
+ # engine reports a parse failure (the position rides the
210
+ # message).
211
+ def next
212
+ p = with_handle!
213
+ count = ::FFI::MemoryPointer.new(:size_t)
214
+ raw = ::Yeptris::FFI.yeptris_iterparse_next(p, count)
215
+ if raw.null?
216
+ st = status
217
+ raise ::Yeptris::ParseError,
218
+ "iterparse failed: status #{st[:status]} at line " \
219
+ "#{st[:line]}, column #{st[:col]}" unless st[:status].zero?
220
+
221
+ return nil # the legal end of stream
222
+ end
223
+
224
+ n = ::Yeptris::FFI.read_c_size_t(count)
225
+ stride = ::Yeptris::FFI::CEvent.size
226
+ Array.new(n) { |i| Event.new(::Yeptris::FFI::CEvent.new(raw + i * stride)) }
227
+ end
228
+
229
+ # Yields each slice (Enumerable over slices — the raw engine
230
+ # stream, the STREAM_START/STREAM_END frames included).
231
+ def each
232
+ return enum_for(:each) unless block_given?
233
+
234
+ loop do
235
+ slice = self.next
236
+ break if slice.nil?
237
+
238
+ yield slice
239
+ end
240
+ nil
241
+ end
242
+
243
+ # Yields each DOCUMENT's event Array — the stream framing slices
244
+ # (STREAM_START's opener and the lone STREAM_END) are skipped,
245
+ # so this yields exactly one entry per document.
246
+ def each_document
247
+ return enum_for(:each_document) unless block_given?
248
+
249
+ each do |slice|
250
+ yield slice if slice.map(&:kind).include?(:document_start)
251
+ end
252
+ nil
253
+ end
254
+
255
+ def status
256
+ p = with_handle!
257
+ line = ::FFI::MemoryPointer.new(:uint32)
258
+ col = ::FFI::MemoryPointer.new(:uint32)
259
+ st = ::Yeptris::FFI.yeptris_iterparse_status(p, line, col)
260
+ { status: st, line: line.read_uint32, col: col.read_uint32 }
261
+ end
262
+
263
+ private
264
+
265
+ def yield_free
266
+ ::Yeptris::FFI.yeptris_iterparse_free(@c_ptr)
267
+ end
268
+ end
269
+
270
+ module_function
271
+
272
+ # A pull cursor over +input+ (String or IO). The engine parses
273
+ # eagerly; a syntactically invalid stream still yields a cursor —
274
+ # its #next answers nil immediately and #status carries the
275
+ # failure position.
276
+ def pull(input)
277
+ src = ::Yeptris.read_input(input).to_s
278
+ p = ::Yeptris::FFI.yeptris_pull_new(src, src.bytesize)
279
+ raise ::Yeptris::Error, "yeptris: pull cursor allocation failed" if p.null?
280
+
281
+ Puller.new(p)
282
+ end
283
+
284
+ # A document-at-a-time iterator over +input+ (String or IO).
285
+ def iterparse(input)
286
+ src = ::Yeptris.read_input(input).to_s
287
+ it = ::Yeptris::FFI.yeptris_iterparse_new(src, src.bytesize)
288
+ raise ::Yeptris::Error, "yeptris: iterparse allocation failed" if it.null?
289
+
290
+ Iterparse.new(it)
291
+ end
292
+
293
+ # The streaming faces' availability (engine with events.h's C
294
+ # entries); at USE a stale engine still raises the attach stub's
295
+ # message.
296
+ def available?
297
+ ::Yeptris::FFI::EVENTS
298
+ end
299
+ end
300
+ end