lexer_kit 0.6.1 → 0.7.0
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 +4 -4
- data/Cargo.lock +265 -0
- data/Cargo.toml +37 -0
- data/README.md +7 -3
- data/crates/lexer_kit_core/Cargo.toml +17 -0
- data/crates/lexer_kit_core/src/dfa.rs +437 -0
- data/crates/lexer_kit_core/src/format.rs +599 -0
- data/crates/lexer_kit_core/src/lib.rs +18 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
- data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
- data/ext/lexer_kit_rust/Cargo.toml +7 -3
- data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
- data/ext/lexer_kit_rust/src/lib.rs +200 -215
- data/lib/lexer_kit/builder/compiler.rb +50 -15
- data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
- data/lib/lexer_kit/builder/validator.rb +45 -0
- data/lib/lexer_kit/builder.rb +34 -7
- data/lib/lexer_kit/cli/commands.rb +17 -7
- data/lib/lexer_kit/core/source.rb +3 -2
- data/lib/lexer_kit/debug/disassembler.rb +0 -2
- data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
- data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
- data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
- data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
- data/lib/lexer_kit/errors.rb +2 -2
- data/lib/lexer_kit/format/lkb1.rb +3 -3
- data/lib/lexer_kit/format/lkt1.rb +3 -1
- data/lib/lexer_kit/format.rb +4 -4
- data/lib/lexer_kit/ir/compiled_program.rb +39 -26
- data/lib/lexer_kit/ir/constant_pool.rb +33 -11
- data/lib/lexer_kit/ir/dfa_table.rb +9 -19
- data/lib/lexer_kit/ir/instruction.rb +3 -0
- data/lib/lexer_kit/ir/jump_table.rb +3 -13
- data/lib/lexer_kit/ir/keyword_table.rb +0 -9
- data/lib/lexer_kit/ir/limits.rb +48 -0
- data/lib/lexer_kit/ir/serializer.rb +69 -30
- data/lib/lexer_kit/ir.rb +1 -0
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +15 -2
- data/vendor/nupur/Cargo.toml +24 -0
- data/vendor/nupur/LICENSE +176 -0
- data/vendor/nupur/NOTICE +64 -0
- data/vendor/nupur/README.ja.md +150 -0
- data/vendor/nupur/README.md +257 -0
- data/vendor/nupur/src/abi.rs +143 -0
- data/vendor/nupur/src/args.rs +318 -0
- data/vendor/nupur/src/build.rs +101 -0
- data/vendor/nupur/src/caught.rs +171 -0
- data/vendor/nupur/src/cname.rs +37 -0
- data/vendor/nupur/src/describe.rs +175 -0
- data/vendor/nupur/src/encoding.rs +240 -0
- data/vendor/nupur/src/gvl.rs +181 -0
- data/vendor/nupur/src/hash.rs +52 -0
- data/vendor/nupur/src/inject.rs +20 -0
- data/vendor/nupur/src/lib.rs +280 -0
- data/vendor/nupur/src/lint_probe.rs +142 -0
- data/vendor/nupur/src/msg.rs +193 -0
- data/vendor/nupur/src/no_unwind.rs +39 -0
- data/vendor/nupur/src/raw/check.rs +71 -0
- data/vendor/nupur/src/raw/data.rs +28 -0
- data/vendor/nupur/src/raw/define.rs +215 -0
- data/vendor/nupur/src/raw/exception.rs +69 -0
- data/vendor/nupur/src/raw/iterate.rs +115 -0
- data/vendor/nupur/src/raw/mod.rs +105 -0
- data/vendor/nupur/src/raw/protect.rs +144 -0
- data/vendor/nupur/src/raw/string.rs +148 -0
- data/vendor/nupur/src/raw/symbol.rs +38 -0
- data/vendor/nupur/src/raw/values.rs +293 -0
- data/vendor/nupur/src/rb/call.rs +118 -0
- data/vendor/nupur/src/rb/class.rs +170 -0
- data/vendor/nupur/src/rb/convert.rs +134 -0
- data/vendor/nupur/src/rb/error.rs +355 -0
- data/vendor/nupur/src/rb/global.rs +141 -0
- data/vendor/nupur/src/rb/kept.rs +55 -0
- data/vendor/nupur/src/rb/method/frame.rs +81 -0
- data/vendor/nupur/src/rb/method.rs +468 -0
- data/vendor/nupur/src/rb/mod.rs +40 -0
- data/vendor/nupur/src/rb/object.rs +66 -0
- data/vendor/nupur/src/rb/ruby.rs +107 -0
- data/vendor/nupur/src/rb/value/header.rs +65 -0
- data/vendor/nupur/src/rb/value/id.rs +61 -0
- data/vendor/nupur/src/rb/value/integer.rs +132 -0
- data/vendor/nupur/src/rb/value.rs +454 -0
- data/vendor/nupur/src/stack.rs +32 -0
- data/vendor/nupur/src/string/make.rs +234 -0
- data/vendor/nupur/src/string/mod.rs +24 -0
- data/vendor/nupur/src/string/read.rs +411 -0
- data/vendor/nupur/src/table.rs +58 -0
- data/vendor/nupur/src/typed/callbacks.rs +125 -0
- data/vendor/nupur/src/typed/data_type.rs +132 -0
- data/vendor/nupur/src/typed/embedded.rs +103 -0
- data/vendor/nupur/src/typed/external.rs +76 -0
- data/vendor/nupur/src/typed/hidden.rs +95 -0
- data/vendor/nupur/src/typed/mod.rs +373 -0
- data/vendor/nupur/src/utf8_copy.rs +262 -0
- data/vendor/nupur/sys/Cargo.toml +29 -0
- data/vendor/nupur/sys/src/data_type.rs +213 -0
- data/vendor/nupur/sys/src/int.rs +248 -0
- data/vendor/nupur/sys/src/lib.rs +61 -0
- data/vendor/nupur/sys/src/lint_probe.rs +93 -0
- data/vendor/nupur/sys/src/no_unwind.rs +99 -0
- data/vendor/nupur/sys/src/non_raising.rs +585 -0
- data/vendor/nupur/sys/src/protect.rs +349 -0
- data/vendor/nupur/sys/src/raising.rs +257 -0
- data/vendor/nupur/sys/src/value.rs +279 -0
- data/vendor/nupur.sha256 +68 -0
- metadata +92 -12
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# Nupur
|
|
2
|
+
|
|
3
|
+
Nupur is the Ruby binding layer under Makiri's extension. It gives the
|
|
4
|
+
extension a safe API over Ruby's C: Ruby values and their class-checked
|
|
5
|
+
handles, exceptions, method trampolines, TypedData, the GVL, and a wrapper
|
|
6
|
+
for every C call that can raise. Every `unsafe` that talks to Ruby is in
|
|
7
|
+
Nupur or in the crate under it, but one: the extension's `Init_`, which hands
|
|
8
|
+
its definitions to `run_init`.
|
|
9
|
+
|
|
10
|
+
The layers, from the top:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
makiri_rs the extension: Makiri's Ruby methods
|
|
14
|
+
nupur the safe API, built on nupur_sys
|
|
15
|
+
nupur_sys (sys/) Ruby's C, each call taking a proof; the one crate on rb-sys
|
|
16
|
+
rb-sys Ruby's C API, as generated bindings
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Nupur does not depend on the extension and knows nothing of it. What it needs
|
|
20
|
+
from the extension (the exception class for a broken internal invariant, the
|
|
21
|
+
`rake oom` hooks) the extension registers at `Init_`. The extension reaches
|
|
22
|
+
Nupur as `crate::nupur`, and its `bridge` re-exports `rb`, `typed`, `gvl`,
|
|
23
|
+
`build`, `hash` and `args`.
|
|
24
|
+
|
|
25
|
+
## What it guarantees
|
|
26
|
+
|
|
27
|
+
Most of Nupur's rules are its types and its crates' dependencies, so code
|
|
28
|
+
that breaks one does not compile. `src/lib.rs` states each in full.
|
|
29
|
+
|
|
30
|
+
- **No raise skips Rust's cleanup.** A raise is a `longjmp`, which runs no
|
|
31
|
+
destructor of the Rust frames it crosses. Every C call that can raise is
|
|
32
|
+
a wrapper in `nupur_sys::raising` that takes a `&Protected`. Only
|
|
33
|
+
`rb_protect`'s thunk makes one, and lends it to the closure that
|
|
34
|
+
`raw::call` runs, so the raise comes back as an `Err`. Nupur has no other
|
|
35
|
+
way to call a C function: it does not depend on rb-sys, and `nupur_sys`
|
|
36
|
+
re-exports no C function. A raise leaves Ruby unprotected in two places,
|
|
37
|
+
each over frames that own nothing to release:
|
|
38
|
+
- a method's trampoline raises its `Err` (or resumes a `break` or
|
|
39
|
+
`throw`), and `Init_` raises its `fatal`, once every Rust frame of the
|
|
40
|
+
method has returned;
|
|
41
|
+
- `rb_gc_mark`, in a wrapped object's mark callback
|
|
42
|
+
(`nupur_sys::non_raising::gc_mark_in_dmark`), can raise `NoMemoryError`
|
|
43
|
+
when Ruby's mark stack cannot grow. The callback holds nothing that
|
|
44
|
+
needs cleanup, and every C extension's mark function takes the same
|
|
45
|
+
risk.
|
|
46
|
+
- **No panic reaches C.** A trampoline turns a panic into the internal
|
|
47
|
+
error; a callback C calls catches one (`caught::PanicLatch`) for its
|
|
48
|
+
caller to resume. Code that must not unwind is a `no_unwind` site, and a
|
|
49
|
+
release build with `--cfg nupur_no_unwind` proves it cannot
|
|
50
|
+
(`rake no_unwind`).
|
|
51
|
+
- **The GVL is a type.** A call of Ruby's C that needs the GVL takes a proof
|
|
52
|
+
that it is held: the Ruby value it is about (`nupur_sys::value::Value`,
|
|
53
|
+
which Nupur's `Value` holds), or, where there is none, the token `Ruby`.
|
|
54
|
+
Neither is `Send`, so neither can enter a body run with the GVL released.
|
|
55
|
+
A value is made only inside `nupur_sys`; a token only by `rb::method`'s
|
|
56
|
+
private `entry_token`, which only the entries Ruby calls use.
|
|
57
|
+
- **The GC sees every Ruby value a wrapped object keeps.** The GC cannot
|
|
58
|
+
see into a wrapped object's Rust data, so a value the object keeps alive
|
|
59
|
+
(a Makiri node keeps its Document) must be reported by its mark callback.
|
|
60
|
+
Nupur keeps such values in slots (`nupur_sys::value::Root`), never in the
|
|
61
|
+
Rust data, and marks them itself: an extension writes no mark function,
|
|
62
|
+
so it cannot forget one.
|
|
63
|
+
- **A wrapped object outlives the borrow of its data.** The data is reached
|
|
64
|
+
only through a `typed::Typed` handle, which keeps the object alive until
|
|
65
|
+
the last borrow ends.
|
|
66
|
+
- **A String is read by copying.** No slice of one is handed out. Text is a
|
|
67
|
+
copy checked to be UTF-8 by std's validator, never trusted from the
|
|
68
|
+
String's cached coderange.
|
|
69
|
+
- **A C integer is converted in one place** (`nupur_sys::int`). A C `long`
|
|
70
|
+
is 32 bits on Windows, so the conversions are written once for either
|
|
71
|
+
width, tested at both and proved at both (`verus/int.rs`).
|
|
72
|
+
- **Two Ractors may run the layer at once**, once the extension declares
|
|
73
|
+
itself Ractor-safe (`rb::method::declare_ractor_safe`). Nothing of
|
|
74
|
+
Nupur's is shared between them but what any Ractor may read. A wrapped
|
|
75
|
+
object stays in its Ractor unless its type is declared shareable
|
|
76
|
+
(`TypedType::shareable`), which requires a `Sync` payload and makes each
|
|
77
|
+
object frozen and shareable as it is made.
|
|
78
|
+
|
|
79
|
+
### What the types do not hold
|
|
80
|
+
|
|
81
|
+
Some rules cannot be types. Makiri holds these with checks of the code
|
|
82
|
+
(`rake dylint`'s lints in `lints/nupur_lints`, `rake no_unwind`,
|
|
83
|
+
`rake unsafe:boundaries`), so a copy of Nupur used elsewhere needs checks of
|
|
84
|
+
its own:
|
|
85
|
+
|
|
86
|
+
- **That a token is made only where Ruby calls in.** That Ruby called the
|
|
87
|
+
code, holding the GVL, is a fact about the caller, which no type states.
|
|
88
|
+
`gvl_token_outside_entry` keeps the trampolines and `run_init` to where
|
|
89
|
+
Ruby calls them, and `nupur_sys`'s maker (`Ruby::assume_gvl`) to
|
|
90
|
+
`entry_token`.
|
|
91
|
+
- **That a Ruby value stays where the GC's stack scan sees it.** A value is
|
|
92
|
+
`Copy`, and no type can refuse one in a `Vec` or a `Box`, whose contents
|
|
93
|
+
the scan does not see. `ruby_value_on_heap` reads the types of bindings,
|
|
94
|
+
fields, statics and expressions to find one.
|
|
95
|
+
- **That `nupur_sys`'s own code keeps the layer's rules.** It is the one
|
|
96
|
+
crate that can name rb-sys, so inside it the raise, GVL and integer rules
|
|
97
|
+
are lints (`raising_call_outside_protect`, `ffi_without_gvl_proof`,
|
|
98
|
+
`c_integer_outside_int`). Which C functions can raise is a table held to
|
|
99
|
+
a survey of Ruby's C API.
|
|
100
|
+
|
|
101
|
+
An extension must keep a few rules of its own: a body run with the GVL
|
|
102
|
+
released runs no Ruby, nothing runs the caller's Ruby unasked, and a
|
|
103
|
+
Ractor-safe extension's own state is safe across Ractors. `src/lib.rs` lists
|
|
104
|
+
them and what checks each. Read it before using the layer.
|
|
105
|
+
|
|
106
|
+
## Writing an extension
|
|
107
|
+
|
|
108
|
+
A method is a Rust function that takes the GVL's token (`&Ruby`) and returns
|
|
109
|
+
its value, or a `Result` of it; the `method!` / `function!` macros wrap it in
|
|
110
|
+
a trampoline. `Init_` hands two functions to `run_init`: a check of the
|
|
111
|
+
running Ruby, whose refusal is a `LoadError`, and the definitions. This is a
|
|
112
|
+
trimmed copy of the test extension in `test/ext/src/lib.rs`:
|
|
113
|
+
|
|
114
|
+
```rust
|
|
115
|
+
use nupur::rb::{prelude::*, Error, ExceptionClass, RbConst, Ruby, Value};
|
|
116
|
+
use nupur::function;
|
|
117
|
+
|
|
118
|
+
static INTERNAL: RbConst<ExceptionClass> = RbConst::new();
|
|
119
|
+
|
|
120
|
+
fn echo(_: &Ruby, v: Value) -> Value {
|
|
121
|
+
v
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fn check(ruby: &Ruby) -> Result<(), Error> {
|
|
125
|
+
nupur::abi::check_running_api("nupur_test", "rake nupur:spec")?;
|
|
126
|
+
nupur::abi::check_raise_state(ruby)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
130
|
+
let m = ruby.define_module("NupurTest")?;
|
|
131
|
+
let internal = m.define_error("InternalError", ruby.exception_exception())?;
|
|
132
|
+
INTERNAL.set(internal)?;
|
|
133
|
+
nupur::rb::error::INTERNAL_ERROR.set(internal)?;
|
|
134
|
+
m.define_module_function("echo", function!(echo, 1))?;
|
|
135
|
+
Ok(())
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
#[no_mangle]
|
|
139
|
+
#[allow(non_snake_case)]
|
|
140
|
+
pub extern "C" fn Init_nupur_test() {
|
|
141
|
+
// SAFETY: Ruby calls `Init_nupur_test` on its thread, with the GVL held.
|
|
142
|
+
unsafe { nupur::rb::method::run_init(check, init) };
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The test extension also wraps an object with a slot (`TypedType`, `Birth`),
|
|
147
|
+
reads arguments and keywords (`args`), walks a Hash, reads Strings, and runs
|
|
148
|
+
a body with the GVL released.
|
|
149
|
+
|
|
150
|
+
Three things an extension's own build needs to know:
|
|
151
|
+
|
|
152
|
+
- `rb::Error` holds its message inline (256 bytes), so that making one
|
|
153
|
+
allocates nothing, which puts every `Result<_, Error>` past clippy's
|
|
154
|
+
default for `result_large_err`. Set `large-error-threshold = 288` in the
|
|
155
|
+
extension's `clippy.toml`, as Makiri does.
|
|
156
|
+
- The profile builds with `panic = "unwind"`, as Nupur's test extension
|
|
157
|
+
does; with `abort`, a panic in a method would kill the host instead of
|
|
158
|
+
reaching Ruby as the internal error. A dependency's profile does not
|
|
159
|
+
apply, so Nupur cannot set it: it refuses to compile without it.
|
|
160
|
+
- An `allow` of one of Nupur's lints sits under
|
|
161
|
+
`cfg_attr(dylint_lib = "nupur_lints", ...)`, so register the cfg in the
|
|
162
|
+
extension's `Cargo.toml`, or every such allow warns:
|
|
163
|
+
`[lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(dylint_lib, values(any()))'] }`.
|
|
164
|
+
The lints run over the extension from its workspace with
|
|
165
|
+
`cargo dylint --path <makiri>/ext/makiri/rust/lints --pattern nupur_lints --no-deps`;
|
|
166
|
+
a `[nupur_lints]` table in the workspace's `dylint.toml` names the
|
|
167
|
+
extension's crate when others are linted too, its Ruby-free crates and
|
|
168
|
+
its own no-unwind guards (`lints/nupur_lints/README.md`).
|
|
169
|
+
|
|
170
|
+
An extension that calls a method of an object it was handed (a JSON
|
|
171
|
+
generator calling `to_json`) does it through `rb::call`, whose module doc
|
|
172
|
+
gives the one rule that keeps it safe: hold nothing across the call that
|
|
173
|
+
the callee could invalidate.
|
|
174
|
+
|
|
175
|
+
## Compared with Magnus
|
|
176
|
+
|
|
177
|
+
Nupur replaced Magnus in Makiri, and keeps many of its names (`Value`,
|
|
178
|
+
`RString`, `Error`, `method!`). The comparison is with Magnus 0.8.2, the
|
|
179
|
+
version Makiri used until the replacement in October 2026.
|
|
180
|
+
|
|
181
|
+
Magnus is a general binding that covers most of Ruby's C API; Nupur covers
|
|
182
|
+
only the C functions and types Makiri uses. Using Magnus meant trusting all
|
|
183
|
+
of its internals and their `unsafe` for the thirty or so things Makiri
|
|
184
|
+
needed from it. With Nupur, what is trusted is the C functions Makiri chose
|
|
185
|
+
and wrote a contract for.
|
|
186
|
+
|
|
187
|
+
Where the two differ on safety:
|
|
188
|
+
|
|
189
|
+
| | Magnus | Nupur |
|
|
190
|
+
| --- | --- | --- |
|
|
191
|
+
| C calls that can raise | Many run under `protect`; which ones can raise is the implementers' judgement, which a user cannot check | Each is a wrapper that takes the protection's token, so it cannot be called outside `rb_protect`; which functions can raise is a table held to a survey of Ruby's C API |
|
|
192
|
+
| A panic in a method | Raised as `fatal` (Magnus #118), which cannot be rescued where it happens | Raised as the extension's internal error, an `Exception` that a host can rescue by name |
|
|
193
|
+
| Arguments | Converted with `to_str` / `to_int`, Ruby code the caller can redefine | Not converted: a method takes `Value` and checks the type; a method taking `RString` or `i64` does not compile |
|
|
194
|
+
| Ruby values in a wrapped object | The user writes the mark function; a value can sit in a `Vec` or a field (`BoxValue` exists for the heap) | Fixed slots, written once and marked by Nupur; a payload holds no Ruby value, and a lint refuses one on the heap |
|
|
195
|
+
| Borrowing a wrapped object's data | A borrowed `&T` could outlive its `Value`, which the optimizer had dropped, and point at freed memory (Magnus #131, #101) | Borrowed through a `Typed` handle that keeps the object visible to the GC until the last borrow ends |
|
|
196
|
+
| A String's bytes | `RString::as_slice` lends them | Only copied out, never lent |
|
|
197
|
+
| The GVL | `Ruby::get()`, checked at run time | A Ruby value proves it, or a token where there is none; the token is made only where Ruby calls in, and neither is `Send`, so neither enters a GVL-released body |
|
|
198
|
+
| Raw `VALUE`s | Available with its `rb-sys` feature (`AsRawValue`, `FromRawValue`) | Not reachable from Nupur: `nupur_sys` re-exports no `VALUE` |
|
|
199
|
+
|
|
200
|
+
Magnus is easier to write against: it converts arguments and return values
|
|
201
|
+
for you, its handles are `Copy`, it has far more of Ruby's API, and it is a
|
|
202
|
+
published crate. Nupur gives these up on purpose: an error-prone way of
|
|
203
|
+
writing something is either impossible or the longest way to write it.
|
|
204
|
+
Nupur is not published, and is used only inside Makiri's workspace.
|
|
205
|
+
|
|
206
|
+
## Layout
|
|
207
|
+
|
|
208
|
+
| Nupur | What it holds |
|
|
209
|
+
| --- | --- |
|
|
210
|
+
| `rb` | `Value` and the class-checked handles, `Error`, the `method!` / `function!` trampolines, class and module definitions; `rb::call`, calling a public method of any object |
|
|
211
|
+
| `typed` | TypedData: wrapping a Rust payload as a Ruby object, its slots and GC callbacks, memory reported to the GC |
|
|
212
|
+
| `gvl` | Running a body with the GVL released (`without_gvl`), or one that stops when Ruby asks for the thread back (`without_gvl_cancellable`, its body asking a `Cancel`) |
|
|
213
|
+
| `build` | Building Arrays, Hashes, numbers, Symbols and UTF-8 Strings |
|
|
214
|
+
| `string`, `encoding`, `utf8_copy` | Reading Strings by copying, as checked UTF-8 (`read_utf8`) or as bytes (`read_bytes`); making UTF-8 Strings, of text marked valid (`utf8_str`, which takes a `&str`) or of bytes the caller holds to be UTF-8 (`utf8_str_trusting`), and ASCII-8BIT ones (`binary_str`); encodings |
|
|
215
|
+
| `args`, `hash` | Reading a method's arguments, and its keywords once it has declared them (`Kwargs::only`, any other refused); walking a Hash |
|
|
216
|
+
| `describe`, `msg` | Writing Ruby values into an exception message without a call or an allocation |
|
|
217
|
+
| `abi` | What the binary assumes of the running Ruby, and the load-time checks of it |
|
|
218
|
+
| `caught`, `no_unwind` | Catching a panic at a C boundary; the bodies that must not unwind |
|
|
219
|
+
| `stack`, `table`, `inject` | The native-stack check, a private integer-keyed table, the `rake oom` hooks |
|
|
220
|
+
|
|
221
|
+
`raw` (the protected calls), `int` (`nupur_sys::int`, re-exported) and
|
|
222
|
+
`cname` are private to the crate.
|
|
223
|
+
|
|
224
|
+
| `nupur_sys` | What it holds |
|
|
225
|
+
| --- | --- |
|
|
226
|
+
| `protect` | `rb_protect`'s thunk, the `Protected` token, `$!`, a pending non-local exit |
|
|
227
|
+
| `raising` | The C calls that can raise, each taking `&Protected` |
|
|
228
|
+
| `non_raising` | The C calls that raise nothing, each taking a value or the token (some read, some write or release the GVL) |
|
|
229
|
+
| `value` | `Value`, the token `Ruby`, a slot's `Root` |
|
|
230
|
+
| `data_type` | A TypedData type's `rb_data_type_t` and its flags (`DataFlags`) |
|
|
231
|
+
| `int` | The conversions between C's integers and Rust's widths |
|
|
232
|
+
| `no_unwind` | The guard of each body that must not unwind (Nupur re-exports it) |
|
|
233
|
+
|
|
234
|
+
## Features and dependencies
|
|
235
|
+
|
|
236
|
+
Nupur depends on `nupur_sys` alone, and `nupur_sys` on rb-sys alone. The one
|
|
237
|
+
feature, `alloc-inject`, compiles in the `rake oom` hooks; the extension's
|
|
238
|
+
own `alloc-inject` turns it on, and it turns on `nupur_sys`'s. rb-sys is used
|
|
239
|
+
without `link-ruby`, since the extension is loaded into a Ruby that already
|
|
240
|
+
provides the `rb_*` symbols.
|
|
241
|
+
|
|
242
|
+
## Tests
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
cargo test -p nupur -p nupur_sys
|
|
246
|
+
bundle exec rake nupur:spec
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
`rake nupur:spec` builds the test extension (`test/ext`, a few methods over
|
|
250
|
+
the layer and nothing of Makiri's) and runs the specs beside it
|
|
251
|
+
(`test/spec`): a panic in a method, below the GVL release and in a Hash
|
|
252
|
+
walk; a block's exits; a slot under GC and compaction; checked String reads.
|
|
253
|
+
CI runs it on Linux and macOS.
|
|
254
|
+
|
|
255
|
+
`nupur_sys`'s compile-fail doctests show that its tokens and values cannot
|
|
256
|
+
be made outside it. Only nightly's rustdoc checks the error each names, so
|
|
257
|
+
`rake rust:compile_fail` runs them there.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
//! What this binary assumes of the Ruby that loads it, and the load checks
|
|
2
|
+
//! that refuse a Ruby where an assumption fails, before anything is defined:
|
|
3
|
+
//! the Ruby API version it was built against ([`check_running_api`]), the
|
|
4
|
+
//! `rb_protect` state a raise comes back with, which Ruby does not document
|
|
5
|
+
//! and the layer tells an exception from a `throw` by
|
|
6
|
+
//! ([`check_raise_state`]), and that the extension's first class is not
|
|
7
|
+
//! already defined ([`check_undefined`]).
|
|
8
|
+
|
|
9
|
+
#![forbid(unsafe_code)]
|
|
10
|
+
|
|
11
|
+
/// The `MAJOR.MINOR` of the Ruby whose headers this extension was compiled
|
|
12
|
+
/// against - its ABI, since a teeny release keeps it. Published as a
|
|
13
|
+
/// constant by the extension.
|
|
14
|
+
pub fn ruby_api_version() -> crate::msg::Msg {
|
|
15
|
+
crate::msg::Msg::format(format_args!(
|
|
16
|
+
"{}.{}",
|
|
17
|
+
nupur_sys::RUBY_API_VERSION_MAJOR,
|
|
18
|
+
nupur_sys::RUBY_API_VERSION_MINOR
|
|
19
|
+
))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// The Ruby API `MAJOR.MINOR` this extension was compiled against.
|
|
23
|
+
pub(crate) fn built_ruby_api() -> (i64, i64) {
|
|
24
|
+
(
|
|
25
|
+
i64::from(nupur_sys::RUBY_API_VERSION_MAJOR),
|
|
26
|
+
i64::from(nupur_sys::RUBY_API_VERSION_MINOR),
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// The running Ruby's API `MAJOR.MINOR`, read from the `ruby_api_version`
|
|
31
|
+
/// it exports (three ints, whose layout no Ruby changes).
|
|
32
|
+
pub(crate) fn running_ruby_api() -> (i64, i64) {
|
|
33
|
+
nupur_sys::non_raising::running_api_version()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// `Err(LoadError)` unless the running Ruby's API `MAJOR.MINOR` is the one
|
|
37
|
+
/// this extension was compiled for: the first thing an extension's `Init_`
|
|
38
|
+
/// does. A release Ruby does not check `ruby_abi_version` itself, and under
|
|
39
|
+
/// macOS's `-undefined dynamic_lookup` a binary built for another Ruby loads
|
|
40
|
+
/// and then misreads the interpreter's structs - defining one class could
|
|
41
|
+
/// already corrupt it. It reads the running Ruby's exported
|
|
42
|
+
/// `ruby_api_version` and nothing else; only a mismatch goes on to build the
|
|
43
|
+
/// exception. `name` names the extension in the message, and `rebuild` says
|
|
44
|
+
/// how to rebuild it.
|
|
45
|
+
pub fn check_running_api(name: &str, rebuild: &str) -> Result<(), crate::rb::Error> {
|
|
46
|
+
let running = running_ruby_api();
|
|
47
|
+
let built = built_ruby_api();
|
|
48
|
+
if running == built {
|
|
49
|
+
return Ok(());
|
|
50
|
+
}
|
|
51
|
+
Err(crate::rb::Error::new(
|
|
52
|
+
crate::rb::Builtin::LoadError,
|
|
53
|
+
format_args!(
|
|
54
|
+
"{name}'s native extension was built for Ruby {}.{}, but this is Ruby {}.{}; \
|
|
55
|
+
rebuild it ({rebuild})",
|
|
56
|
+
built.0, built.1, running.0, running.1
|
|
57
|
+
),
|
|
58
|
+
))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// `Err(LoadError)` when the constant `path` names (`["Makiri", "Node"]`,
|
|
62
|
+
/// read from `Object`) is already defined - an autoload counting, and
|
|
63
|
+
/// nothing loaded: the extension's classes are there before its `Init_` has
|
|
64
|
+
/// made them. An outer name that is an autoload not yet loaded holds
|
|
65
|
+
/// nothing yet; one that is not a class or module is refused too, as `init`
|
|
66
|
+
/// could not define under it. An `Init_` that stopped half-way leaves them
|
|
67
|
+
/// (Ruby marks the feature loaded only when `Init_` returns, so the next
|
|
68
|
+
/// `require` runs it again), and so does code that defined one first. Asked
|
|
69
|
+
/// in `check`, before anything is defined, it is a `LoadError` the caller may
|
|
70
|
+
/// rescue, the extension untouched; met in `init`, where
|
|
71
|
+
/// `Module::define_data_class` refuses such a name, it would stop `init`
|
|
72
|
+
/// half-way and leave the extension unusable
|
|
73
|
+
/// ([`crate::rb::method::unfinished`]). Name the class the extension's
|
|
74
|
+
/// `init` defines first.
|
|
75
|
+
pub fn check_undefined(ruby: &crate::rb::Ruby, path: &[&str]) -> Result<(), crate::rb::Error> {
|
|
76
|
+
use crate::rb::{RClass, RModule, ReprValue};
|
|
77
|
+
let Some((last, outer)) = path.split_last() else {
|
|
78
|
+
return Ok(());
|
|
79
|
+
};
|
|
80
|
+
let mut at = ruby.class_object().as_value();
|
|
81
|
+
for (i, name) in outer.iter().enumerate() {
|
|
82
|
+
let id = crate::rb::Id::intern(ruby, &crate::rb::class::c_name(name)?)?;
|
|
83
|
+
/* Not there, or an autoload not yet loaded: nothing under it is
|
|
84
|
+
* defined either. Asked without loading, which would run Ruby. */
|
|
85
|
+
if !crate::raw::const_defined_at(at, id)? || crate::raw::autoload_pending(at, id)? {
|
|
86
|
+
return Ok(());
|
|
87
|
+
}
|
|
88
|
+
let v = crate::raw::const_get(ruby, at, &crate::rb::class::c_name(name)?)?;
|
|
89
|
+
if RModule::from_value(v).is_none() && RClass::from_value(v).is_none() {
|
|
90
|
+
/* `init` would define under it and stop there, half-way. */
|
|
91
|
+
return Err(crate::rb::Error::new(
|
|
92
|
+
crate::rb::Builtin::LoadError,
|
|
93
|
+
format_args!(
|
|
94
|
+
"{} is defined, but not as a class or module",
|
|
95
|
+
Path(path.get(..=i).unwrap_or(path))
|
|
96
|
+
),
|
|
97
|
+
));
|
|
98
|
+
}
|
|
99
|
+
at = v;
|
|
100
|
+
}
|
|
101
|
+
let id = crate::rb::Id::intern(ruby, &crate::rb::class::c_name(last)?)?;
|
|
102
|
+
if !crate::raw::const_defined_at(at, id)? {
|
|
103
|
+
return Ok(());
|
|
104
|
+
}
|
|
105
|
+
Err(crate::rb::Error::new(
|
|
106
|
+
crate::rb::Builtin::LoadError,
|
|
107
|
+
format_args!(
|
|
108
|
+
"{} is already defined: the extension was loaded before, or other code defined its classes",
|
|
109
|
+
Path(path)
|
|
110
|
+
),
|
|
111
|
+
))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// A constant's path, written `A::B`.
|
|
115
|
+
struct Path<'a>(&'a [&'a str]);
|
|
116
|
+
|
|
117
|
+
impl core::fmt::Display for Path<'_> {
|
|
118
|
+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
119
|
+
for (i, name) in self.0.iter().enumerate() {
|
|
120
|
+
if i > 0 {
|
|
121
|
+
f.write_str("::")?;
|
|
122
|
+
}
|
|
123
|
+
f.write_str(name)?;
|
|
124
|
+
}
|
|
125
|
+
Ok(())
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// After [`check_running_api`]: `Err(LoadError)` unless a raise made under
|
|
130
|
+
/// the layer's protection comes back as an exception, not as some other exit
|
|
131
|
+
/// (`raw::raise_is_recognised`, which says why that is not a given) - the
|
|
132
|
+
/// layer could not tell an exception from a `throw`. Nothing is defined yet
|
|
133
|
+
/// when it refuses.
|
|
134
|
+
pub fn check_raise_state(ruby: &crate::rb::Ruby) -> Result<(), crate::rb::Error> {
|
|
135
|
+
if crate::raw::raise_is_recognised(ruby)? {
|
|
136
|
+
return Ok(());
|
|
137
|
+
}
|
|
138
|
+
Err(crate::rb::Error::new(
|
|
139
|
+
crate::rb::Builtin::LoadError,
|
|
140
|
+
"this Ruby reports a raise by a state the extension does not know \
|
|
141
|
+
(`rb_protect`'s TAG_RAISE changed); it cannot tell an exception from a throw",
|
|
142
|
+
))
|
|
143
|
+
}
|