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,349 @@
|
|
|
1
|
+
//! How a raise becomes an error value, and how one is raised again: the
|
|
2
|
+
//! protection Nupur runs every C call that can raise under ([`call`]), what
|
|
3
|
+
//! a protected call that came back with a raise or a non-local exit leaves
|
|
4
|
+
//! behind - the thread's error info (`$!`), and an exit pending until the
|
|
5
|
+
//! trampoline resumes it - and the calls that leave on purpose
|
|
6
|
+
//! ([`exc_raise`], [`resume`]).
|
|
7
|
+
//!
|
|
8
|
+
//! Nothing here knows Nupur's types: a raise comes back as the raw
|
|
9
|
+
//! exception ([`Caught`]), which Nupur makes its `Error`. Every function that
|
|
10
|
+
//! calls Ruby's C is an `unsafe fn` whose contract is the GVL, held on a Ruby
|
|
11
|
+
//! thread; the one exception is the thunk, which Ruby itself calls.
|
|
12
|
+
|
|
13
|
+
#![allow(unsafe_code)]
|
|
14
|
+
|
|
15
|
+
use core::ffi::c_int;
|
|
16
|
+
use core::marker::PhantomData;
|
|
17
|
+
|
|
18
|
+
use rb_sys::VALUE;
|
|
19
|
+
|
|
20
|
+
/// The proof that code runs inside [`call`]'s `rb_protect`, where a raise
|
|
21
|
+
/// comes back to the caller instead of `longjmp`ing over its frames.
|
|
22
|
+
///
|
|
23
|
+
/// Only the thunk makes one, and lends it to the protected closure for the
|
|
24
|
+
/// call alone: the closure takes `&Protected` for any lifetime, and its result
|
|
25
|
+
/// is a `T` chosen outside, so the reference cannot leave the call. It is
|
|
26
|
+
/// neither `Send` nor `Sync`, so a GVL-released body cannot carry it either.
|
|
27
|
+
///
|
|
28
|
+
/// Not made outside this crate, so a wrapper of [`crate::raising`] cannot be
|
|
29
|
+
/// called outside the protection (each names the error it must fail with,
|
|
30
|
+
/// which `rake rust:compile_fail` checks on nightly; stable's rustdoc does
|
|
31
|
+
/// not):
|
|
32
|
+
///
|
|
33
|
+
/// ```compile_fail,E0451
|
|
34
|
+
/// let made = nupur_sys::protect::Protected { _not_send: core::marker::PhantomData };
|
|
35
|
+
/// let _ = unsafe { nupur_sys::raising::rb_hash_new(&made) };
|
|
36
|
+
/// ```
|
|
37
|
+
///
|
|
38
|
+
/// kept out of a place that outlives the call:
|
|
39
|
+
///
|
|
40
|
+
/// ```compile_fail,E0521
|
|
41
|
+
/// let kept = core::cell::Cell::new(None);
|
|
42
|
+
/// let at = &kept;
|
|
43
|
+
/// let _ = unsafe { nupur_sys::protect::call(move |p| at.set(Some(p))) };
|
|
44
|
+
/// ```
|
|
45
|
+
///
|
|
46
|
+
/// and from another thread:
|
|
47
|
+
///
|
|
48
|
+
/// ```compile_fail,E0277
|
|
49
|
+
/// fn sent<T: Send>(_: T) {}
|
|
50
|
+
/// let _ = unsafe { nupur_sys::protect::call(|p| sent(p)) };
|
|
51
|
+
/// ```
|
|
52
|
+
pub struct Protected {
|
|
53
|
+
_not_send: PhantomData<*mut ()>,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
impl Protected {
|
|
57
|
+
/// The GVL's token, which the protection implies (`rb_protect` runs only
|
|
58
|
+
/// under it), lent for the borrow of this one.
|
|
59
|
+
#[inline(always)]
|
|
60
|
+
pub fn ruby(&self) -> &crate::value::Ruby {
|
|
61
|
+
crate::value::Ruby::lent()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// What a protected call came back with instead of its value.
|
|
66
|
+
#[derive(Clone, Copy)]
|
|
67
|
+
pub enum Caught {
|
|
68
|
+
/// A raise: the exception Ruby left in `$!`, taken (`$!` is clear again).
|
|
69
|
+
/// A live value, which the caller keeps on its stack.
|
|
70
|
+
Exception(crate::value::Value),
|
|
71
|
+
/// Any other non-local exit (a `break` or `throw` out of a block, a thread
|
|
72
|
+
/// kill), by its `rb_protect` state - now pending ([`pending_exit`]).
|
|
73
|
+
Jump(c_int),
|
|
74
|
+
/// The closure did not run, though `rb_protect` reported no exit: an
|
|
75
|
+
/// internal error, never an answer.
|
|
76
|
+
NotRun,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// The raise sweep's hook (`script/check_raise_injection.rb`): in an
|
|
80
|
+
/// `alloc-inject` build the armed allocating call raises `NoMemoryError` in
|
|
81
|
+
/// place of its allocation, as a real failure would - so the sweep can make
|
|
82
|
+
/// every one of them raise in turn. Nothing in any other build. It takes the
|
|
83
|
+
/// token, so the raise is protected like the call's. Whether a call is armed
|
|
84
|
+
/// is the extension's to answer ([`set_raise_injector`]).
|
|
85
|
+
#[inline(always)]
|
|
86
|
+
pub fn injected_raise(_: &Protected) {
|
|
87
|
+
#[cfg(feature = "alloc-inject")]
|
|
88
|
+
if RAISE_INJECTOR.get().is_some_and(|armed| armed()) {
|
|
89
|
+
/* Raises without allocating: Ruby keeps the exception preallocated.
|
|
90
|
+
* SAFETY: inside the protection, which the token proves. */
|
|
91
|
+
unsafe { rb_sys::rb_memerror() }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// The function that answers whether the next allocating call raises.
|
|
96
|
+
#[cfg(feature = "alloc-inject")]
|
|
97
|
+
static RAISE_INJECTOR: std::sync::OnceLock<fn() -> bool> = std::sync::OnceLock::new();
|
|
98
|
+
|
|
99
|
+
/// Register the raise sweep's answer, once (Nupur's `inject::install`, which
|
|
100
|
+
/// registers the allocation half with it); a second registration is ignored.
|
|
101
|
+
#[cfg(feature = "alloc-inject")]
|
|
102
|
+
pub fn set_raise_injector(armed: fn() -> bool) {
|
|
103
|
+
let _ = RAISE_INJECTOR.set(armed);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
thread_local! {
|
|
107
|
+
/// A non-local exit that is not an exception (a `break` or `throw` out of a
|
|
108
|
+
/// block, a thread kill) a protected call came back with, not yet resumed by
|
|
109
|
+
/// the trampoline: its `rb_protect` state, 0 for none.
|
|
110
|
+
///
|
|
111
|
+
/// Ruby resumes such an exit (`rb_jump_tag`) from what it left in the
|
|
112
|
+
/// thread's error info - the throw data, a kill's marker - and every raise
|
|
113
|
+
/// overwrites that. So once one is pending, no protected call runs C at
|
|
114
|
+
/// all, each answering the same exit, [`keeping_errinfo`] puts nothing back,
|
|
115
|
+
/// and the GVL is not released (`nupur::gvl::without_gvl` answers the exit
|
|
116
|
+
/// too). The trampoline resumes it however the method ends - an `Err` of
|
|
117
|
+
/// another kind, or an `Ok` from a method that dropped the exit's `Err` -
|
|
118
|
+
/// so it is resumed on the thread that caught it, and none is left behind
|
|
119
|
+
/// for the next method.
|
|
120
|
+
///
|
|
121
|
+
/// The native thread's own, not the process's: two Ractors run their
|
|
122
|
+
/// methods at once, each under its own GVL, and a global would let one
|
|
123
|
+
/// forget the other's exit or resume it on the wrong thread. Nothing between
|
|
124
|
+
/// the exit and its resumption can release the GVL (only a C call can, and
|
|
125
|
+
/// none runs), so the Ruby thread that caught it is still on this native
|
|
126
|
+
/// thread when it is resumed - with M:N threads too, which switch only where
|
|
127
|
+
/// the GVL is released.
|
|
128
|
+
static PENDING_JUMP: core::cell::Cell<c_int> = const { core::cell::Cell::new(0) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// Forget a pending exit: a new method starts. The trampoline resumes every
|
|
132
|
+
/// exit before its method returns, so none is pending here; this is the
|
|
133
|
+
/// belt to that brace.
|
|
134
|
+
///
|
|
135
|
+
/// # Safety
|
|
136
|
+
///
|
|
137
|
+
/// Nothing pending may be lost: called where a method starts, or where the
|
|
138
|
+
/// exit just caught is one the caller made itself and has no one to resume
|
|
139
|
+
/// for (the load-time self-check's). A `throw`, `break` or thread kill
|
|
140
|
+
/// forgotten anywhere else never reaches its `catch`, its block or its
|
|
141
|
+
/// thread.
|
|
142
|
+
#[inline]
|
|
143
|
+
pub unsafe fn reset_pending_exit() {
|
|
144
|
+
PENDING_JUMP.set(0);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// The `rb_protect` state of the exit pending, or `None`.
|
|
148
|
+
#[inline]
|
|
149
|
+
pub fn pending_exit() -> Option<c_int> {
|
|
150
|
+
match PENDING_JUMP.get() {
|
|
151
|
+
0 => None,
|
|
152
|
+
state => Some(state),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Resume the non-local exit `state` stands for, which a protected call
|
|
157
|
+
/// caught: the one way a pending exit leaves the layer.
|
|
158
|
+
///
|
|
159
|
+
/// # Safety
|
|
160
|
+
///
|
|
161
|
+
/// Under the GVL, as a trampoline's last act, with nothing owned on the
|
|
162
|
+
/// frames it leaves: `rb_jump_tag` `longjmp`s.
|
|
163
|
+
#[inline]
|
|
164
|
+
pub unsafe fn resume(state: c_int) -> ! {
|
|
165
|
+
// SAFETY: the exit is resumed right here, so nothing is lost.
|
|
166
|
+
unsafe { reset_pending_exit() };
|
|
167
|
+
// SAFETY: `state` is one `rb_protect` reported; resuming it is the point,
|
|
168
|
+
// and the caller owns nothing it skips.
|
|
169
|
+
unsafe { rb_sys::rb_jump_tag(state) }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// Run `f` - one C call, and the `Copy` values it reads - under a bare
|
|
173
|
+
/// `rb_protect`; a raise comes back as [`Caught::Exception`], and `$!` is
|
|
174
|
+
/// cleared (an error the caller handles rather than raises is run under
|
|
175
|
+
/// [`keeping_errinfo`]). Once an exit is pending, `f` does not run: the call
|
|
176
|
+
/// answers that exit.
|
|
177
|
+
///
|
|
178
|
+
/// `F: Copy` and `T: Copy` are what keep the thunk's frame free of anything a
|
|
179
|
+
/// skipped destructor would leak: a closure is `Copy` only when everything it
|
|
180
|
+
/// captures is, so it owns nothing, and neither does its result. There is no
|
|
181
|
+
/// `catch_unwind` here (a raise crossing one is undefined behaviour); a panic
|
|
182
|
+
/// in the thunk would abort at its `extern "C"` boundary, and the closures
|
|
183
|
+
/// Nupur passes hold no Rust that panics. The one C call that calls back into
|
|
184
|
+
/// Rust, `rb_hash_foreach`, latches its callback's panic inside the callback,
|
|
185
|
+
/// and the callback raises nothing.
|
|
186
|
+
///
|
|
187
|
+
/// # Safety
|
|
188
|
+
///
|
|
189
|
+
/// On a Ruby thread, with the GVL held. `f` may call Ruby's C, which may
|
|
190
|
+
/// raise; it must own nothing a `longjmp` would skip.
|
|
191
|
+
#[inline]
|
|
192
|
+
pub unsafe fn call<T: Copy, F: FnOnce(&Protected) -> T + Copy>(f: F) -> Result<T, Caught> {
|
|
193
|
+
struct Slot<F, T> {
|
|
194
|
+
f: F,
|
|
195
|
+
out: Option<T>,
|
|
196
|
+
}
|
|
197
|
+
unsafe extern "C" fn thunk<T: Copy, F: FnOnce(&Protected) -> T + Copy>(arg: VALUE) -> VALUE {
|
|
198
|
+
crate::no_unwind::must_not_unwind::<crate::no_unwind::ProtectThunk, _>(|| {
|
|
199
|
+
// SAFETY: `arg` is the `Slot` `call` passed, live for the call.
|
|
200
|
+
let slot = unsafe { &mut *(arg as *mut Slot<F, T>) };
|
|
201
|
+
let inside = Protected {
|
|
202
|
+
_not_send: PhantomData,
|
|
203
|
+
};
|
|
204
|
+
slot.out = Some((slot.f)(&inside));
|
|
205
|
+
rb_sys::Qnil as VALUE
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
if let Some(state) = pending_exit() {
|
|
209
|
+
return Err(Caught::Jump(state));
|
|
210
|
+
}
|
|
211
|
+
let mut slot = Slot { f, out: None };
|
|
212
|
+
let mut state: c_int = 0;
|
|
213
|
+
// SAFETY: the thunk reads only `slot`, which outlives the call; the GVL
|
|
214
|
+
// is the caller's.
|
|
215
|
+
unsafe {
|
|
216
|
+
rb_sys::rb_protect(
|
|
217
|
+
Some(thunk::<T, F>),
|
|
218
|
+
&mut slot as *mut Slot<F, T> as VALUE,
|
|
219
|
+
&mut state,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
match (state, slot.out) {
|
|
223
|
+
(0, Some(out)) => Ok(out),
|
|
224
|
+
(0, None) => Err(Caught::NotRun),
|
|
225
|
+
// SAFETY: a protected call just returned, under the caller's GVL.
|
|
226
|
+
(state, _) => Err(unsafe { caught(state) }),
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// Run `f`, protected calls whose error the caller handles rather than
|
|
231
|
+
/// raises - a lookup that may fail, a name that may not be made - with `$!`
|
|
232
|
+
/// put back as it was after, as Ruby's own C does around one
|
|
233
|
+
/// (`load_encoding` saves `rb_errinfo()` and restores it): a caught raise
|
|
234
|
+
/// clears it, and a handled one must not clear the caller's. Not every call
|
|
235
|
+
/// does this: the read costs a call into libruby, and an error that is raised
|
|
236
|
+
/// replaces `$!` anyway.
|
|
237
|
+
///
|
|
238
|
+
/// Nothing is put back once an exit is pending: Ruby resumes it from the
|
|
239
|
+
/// error info the exit left there, which `before` would overwrite.
|
|
240
|
+
///
|
|
241
|
+
/// # Safety
|
|
242
|
+
///
|
|
243
|
+
/// On a Ruby thread, with the GVL held.
|
|
244
|
+
#[inline]
|
|
245
|
+
pub unsafe fn keeping_errinfo<T>(f: impl FnOnce() -> T) -> T {
|
|
246
|
+
// SAFETY: under the GVL, as the contract says; a read.
|
|
247
|
+
let before = unsafe { rb_sys::rb_errinfo() };
|
|
248
|
+
let out = f();
|
|
249
|
+
if pending_exit().is_none() {
|
|
250
|
+
// SAFETY: as above; `before` was `$!` a moment ago, kept on this
|
|
251
|
+
// frame's stack since.
|
|
252
|
+
unsafe { restore_errinfo(before) };
|
|
253
|
+
}
|
|
254
|
+
out
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/// Put `$!` back to `before`. Only nil or an exception goes back, which is
|
|
258
|
+
/// all `rb_set_errinfo` takes without raising.
|
|
259
|
+
///
|
|
260
|
+
/// # Safety
|
|
261
|
+
///
|
|
262
|
+
/// Under the GVL; `before` a live value.
|
|
263
|
+
#[inline]
|
|
264
|
+
unsafe fn restore_errinfo(before: VALUE) {
|
|
265
|
+
if before == rb_sys::Qnil as VALUE {
|
|
266
|
+
// SAFETY: nil, which it always takes.
|
|
267
|
+
unsafe { rb_sys::rb_set_errinfo(before) };
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
// SAFETY: `before` is live (the contract); `rb_eException` a C global
|
|
271
|
+
// Ruby set at boot. `rb_obj_is_kind_of` raises only for a non-class
|
|
272
|
+
// second argument.
|
|
273
|
+
if unsafe { rb_sys::rb_obj_is_kind_of(before, rb_sys::rb_eException) } == rb_sys::Qtrue as VALUE
|
|
274
|
+
{
|
|
275
|
+
// SAFETY: an exception, which `rb_set_errinfo` takes without raising.
|
|
276
|
+
unsafe { rb_sys::rb_set_errinfo(before) };
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/// The `rb_protect` state of a raise (`TAG_RAISE` in CRuby's `vm_core.h`).
|
|
281
|
+
/// Not part of the documented API - the state is called opaque - but it is
|
|
282
|
+
/// how a raise is told from a `throw` or a `break`, whose state must be
|
|
283
|
+
/// resumed untouched; magnus reads the same value. Unchanged since 1.9.
|
|
284
|
+
const TAG_RAISE: c_int = 6;
|
|
285
|
+
|
|
286
|
+
/// `$!`, taken: the exception a raise left there, with `$!` set back to
|
|
287
|
+
/// `nil` (`rb_errinfo`, `rb_set_errinfo`). Raises nothing.
|
|
288
|
+
///
|
|
289
|
+
/// # Safety
|
|
290
|
+
///
|
|
291
|
+
/// Under the GVL.
|
|
292
|
+
#[inline]
|
|
293
|
+
unsafe fn take_errinfo() -> VALUE {
|
|
294
|
+
// SAFETY: under the GVL (the contract); both read or write the current
|
|
295
|
+
// thread's error info.
|
|
296
|
+
unsafe {
|
|
297
|
+
let exc = rb_sys::rb_errinfo();
|
|
298
|
+
rb_sys::rb_set_errinfo(rb_sys::Qnil as VALUE);
|
|
299
|
+
exc
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// Clear the error info a protected call left behind: [`call`] takes it only
|
|
304
|
+
/// for a raise it recognises, so an exit it did not (the load-time self-check
|
|
305
|
+
/// under a Ruby that numbers its states otherwise) leaves the exception there,
|
|
306
|
+
/// to become the `cause` of whatever is raised next.
|
|
307
|
+
///
|
|
308
|
+
/// # Safety
|
|
309
|
+
///
|
|
310
|
+
/// Under the GVL.
|
|
311
|
+
#[inline]
|
|
312
|
+
pub unsafe fn forget_errinfo() {
|
|
313
|
+
// SAFETY: the contract.
|
|
314
|
+
let _ = unsafe { take_errinfo() };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/// `rb_exc_raise`: raise `exc`. Raises, by design.
|
|
318
|
+
///
|
|
319
|
+
/// # Safety
|
|
320
|
+
///
|
|
321
|
+
/// Under the GVL, with nothing owned on the frames it leaves; `exc` an
|
|
322
|
+
/// exception object.
|
|
323
|
+
#[inline]
|
|
324
|
+
pub unsafe fn exc_raise(exc: crate::value::Value) -> ! {
|
|
325
|
+
// SAFETY: the contract.
|
|
326
|
+
unsafe { rb_sys::rb_exc_raise(exc.as_raw()) }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/// What a non-zero `rb_protect` state stands for: the exception Ruby left in
|
|
330
|
+
/// `$!` for a raise (taken, so `$!` is clear again), or the state itself for
|
|
331
|
+
/// any other exit - which is then pending ([`PENDING_JUMP`]).
|
|
332
|
+
///
|
|
333
|
+
/// # Safety
|
|
334
|
+
///
|
|
335
|
+
/// Under the GVL, right after the protected call returned.
|
|
336
|
+
#[inline]
|
|
337
|
+
unsafe fn caught(state: c_int) -> Caught {
|
|
338
|
+
if state == TAG_RAISE {
|
|
339
|
+
// SAFETY: the contract.
|
|
340
|
+
let exc = unsafe { take_errinfo() };
|
|
341
|
+
if exc != rb_sys::Qnil as VALUE {
|
|
342
|
+
// SAFETY: the exception Ruby just left in `$!`, taken, under
|
|
343
|
+
// the caller's GVL.
|
|
344
|
+
return Caught::Exception(unsafe { crate::value::Value::from_raw(exc) });
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
PENDING_JUMP.set(state);
|
|
348
|
+
Caught::Jump(state)
|
|
349
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
//! The C calls of Ruby's that can RAISE, each a wrapper that takes the proof
|
|
2
|
+
//! it runs inside [`crate::protect::call`]'s `rb_protect`: a `&Protected`,
|
|
3
|
+
//! which only that call's thunk lends. So a raise from one of them comes back
|
|
4
|
+
//! to `call`'s caller as [`crate::protect::Caught`] instead of `longjmp`ing
|
|
5
|
+
//! over Rust frames, by the type rather than by a lint.
|
|
6
|
+
//!
|
|
7
|
+
//! Each is the C function's own signature with the token first, and does
|
|
8
|
+
//! nothing but call it: what checks a value's kind or a bound before the call,
|
|
9
|
+
//! and what makes the answer a typed value, is Nupur's (`nupur::raw`). Which
|
|
10
|
+
//! functions raise is the lints' table (`nupur_lints`' `NON_RAISING`; every
|
|
11
|
+
//! function not in it is taken to raise), and each one Nupur calls is here.
|
|
12
|
+
//!
|
|
13
|
+
//! # Safety
|
|
14
|
+
//!
|
|
15
|
+
//! Every wrapper is an `unsafe fn` for what a raw `VALUE` cannot show: each
|
|
16
|
+
//! argument is what the C function takes - a live value of the kind it
|
|
17
|
+
//! names, a pointer to memory it may read (or write) for the call. Being
|
|
18
|
+
//! inside the protection, and so holding the GVL, is the token's.
|
|
19
|
+
|
|
20
|
+
#![allow(unsafe_code)]
|
|
21
|
+
#![allow(clippy::missing_safety_doc)]
|
|
22
|
+
/* The ones that run the caller's Ruby (those `runs_callers_ruby` names: a
|
|
23
|
+
* call of a method, `respond_to?`, a yield) only pass the call on: the lint
|
|
24
|
+
* names each wrapper by the function it wraps, and the sanctioned call of it
|
|
25
|
+
* is an `allow` where Nupur makes it. */
|
|
26
|
+
#![cfg_attr(
|
|
27
|
+
dylint_lib = "nupur_lints",
|
|
28
|
+
expect(
|
|
29
|
+
runs_callers_ruby,
|
|
30
|
+
reason = "the wrappers pass each call on; its caller is checked"
|
|
31
|
+
)
|
|
32
|
+
)]
|
|
33
|
+
|
|
34
|
+
use core::ffi::{c_int, c_void};
|
|
35
|
+
|
|
36
|
+
use crate::data_type::DataType;
|
|
37
|
+
use crate::protect::Protected;
|
|
38
|
+
use crate::value::Value;
|
|
39
|
+
use crate::{rb_encoding, size_t, ID};
|
|
40
|
+
use rb_sys::VALUE;
|
|
41
|
+
use rb_sys::{rb_data_type_t, rb_enumerator_size_func};
|
|
42
|
+
|
|
43
|
+
/// What a wrapper's argument is to C: a [`Value`] its `VALUE`, a pointer to
|
|
44
|
+
/// values a pointer to `VALUE`s (`Value` is `repr(transparent)`), and
|
|
45
|
+
/// anything else itself.
|
|
46
|
+
trait IntoC<C> {
|
|
47
|
+
fn into_c(self) -> C;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
impl<T> IntoC<T> for T {
|
|
51
|
+
#[inline(always)]
|
|
52
|
+
fn into_c(self) -> T {
|
|
53
|
+
self
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
impl IntoC<VALUE> for Value {
|
|
58
|
+
#[inline(always)]
|
|
59
|
+
fn into_c(self) -> VALUE {
|
|
60
|
+
self.as_raw()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
impl IntoC<*const VALUE> for *const Value {
|
|
65
|
+
#[inline(always)]
|
|
66
|
+
fn into_c(self) -> *const VALUE {
|
|
67
|
+
self.cast()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
impl IntoC<*mut VALUE> for *mut Value {
|
|
72
|
+
#[inline(always)]
|
|
73
|
+
fn into_c(self) -> *mut VALUE {
|
|
74
|
+
self.cast()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// A method's trampoline, as `rb_define_method` takes it: `Value` is
|
|
79
|
+
/// `repr(transparent)`, so the pointer is the one C calls.
|
|
80
|
+
impl IntoC<Option<unsafe extern "C" fn() -> VALUE>> for Option<unsafe extern "C" fn() -> Value> {
|
|
81
|
+
#[inline(always)]
|
|
82
|
+
fn into_c(self) -> Option<unsafe extern "C" fn() -> VALUE> {
|
|
83
|
+
// SAFETY: the same function pointer; its return is a `VALUE` to C.
|
|
84
|
+
self.map(|f| unsafe {
|
|
85
|
+
core::mem::transmute::<unsafe extern "C" fn() -> Value, unsafe extern "C" fn() -> VALUE>(f)
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// A TypedData type, as the C API takes it: its `rb_data_type_t`.
|
|
91
|
+
impl IntoC<*const rb_data_type_t> for &DataType {
|
|
92
|
+
#[inline(always)]
|
|
93
|
+
fn into_c(self) -> *const rb_data_type_t {
|
|
94
|
+
self.as_ptr()
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// An enumerator's size function (`rb_enumeratorize_with_size`'s
|
|
99
|
+
/// `size_fn`), in values: `Value` is `repr(transparent)`, so it is the
|
|
100
|
+
/// function C calls.
|
|
101
|
+
pub type EnumeratorSize =
|
|
102
|
+
Option<unsafe extern "C" fn(recv: Value, argv: Value, eobj: Value) -> Value>;
|
|
103
|
+
|
|
104
|
+
impl IntoC<rb_enumerator_size_func> for EnumeratorSize {
|
|
105
|
+
#[inline(always)]
|
|
106
|
+
fn into_c(self) -> rb_enumerator_size_func {
|
|
107
|
+
// SAFETY: the same function pointer; its arguments and answer are
|
|
108
|
+
// `VALUE`s to C.
|
|
109
|
+
self.map(|f| unsafe {
|
|
110
|
+
core::mem::transmute::<
|
|
111
|
+
unsafe extern "C" fn(Value, Value, Value) -> Value,
|
|
112
|
+
unsafe extern "C" fn(VALUE, VALUE, VALUE) -> VALUE,
|
|
113
|
+
>(f)
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// What C's answer is to a wrapper's caller: a `VALUE` a [`Value`], anything
|
|
119
|
+
/// else itself.
|
|
120
|
+
trait FromC<C> {
|
|
121
|
+
/// # Safety
|
|
122
|
+
///
|
|
123
|
+
/// `c` is what a C call of Ruby's just returned, on its thread.
|
|
124
|
+
unsafe fn from_c(c: C) -> Self;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
impl<T> FromC<T> for T {
|
|
128
|
+
#[inline(always)]
|
|
129
|
+
unsafe fn from_c(c: T) -> T {
|
|
130
|
+
c
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
impl FromC<VALUE> for Value {
|
|
135
|
+
#[inline(always)]
|
|
136
|
+
unsafe fn from_c(c: VALUE) -> Value {
|
|
137
|
+
// SAFETY: what a C call of Ruby's returned, under the protection: a
|
|
138
|
+
// live value on this frame, or a special constant (`Qundef`).
|
|
139
|
+
unsafe { Value::from_raw(c) }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
macro_rules! raising {
|
|
144
|
+
($(fn $name:ident($($arg:ident: $ty:ty),*) $(-> $ret:ty)?;)*) => {$(
|
|
145
|
+
#[doc = concat!("`", stringify!($name), "`, inside the protection.")]
|
|
146
|
+
#[inline(always)]
|
|
147
|
+
pub unsafe fn $name(_: &Protected, $($arg: $ty),*) $(-> $ret)? {
|
|
148
|
+
// SAFETY: the caller's (the module's contract); the token is the
|
|
149
|
+
// protection and the GVL.
|
|
150
|
+
unsafe { FromC::from_c(rb_sys::$name($(IntoC::into_c($arg)),*)) }
|
|
151
|
+
}
|
|
152
|
+
)*};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
raising! {
|
|
156
|
+
fn rb_ary_cat(ary: Value, train: *const Value, len: core::ffi::c_long) -> Value;
|
|
157
|
+
fn rb_ary_new_capa(capa: core::ffi::c_long) -> Value;
|
|
158
|
+
fn rb_ary_new_from_values(n: core::ffi::c_long, elts: *const Value) -> Value;
|
|
159
|
+
fn rb_ary_push(ary: Value, elem: Value) -> Value;
|
|
160
|
+
fn rb_big2ll(arg1: Value) -> core::ffi::c_longlong;
|
|
161
|
+
fn rb_big2str(x: Value, base: core::ffi::c_int) -> Value;
|
|
162
|
+
fn rb_big2ull(arg1: Value) -> core::ffi::c_ulonglong;
|
|
163
|
+
fn rb_check_frozen(obj: Value);
|
|
164
|
+
fn rb_autoload_p(space: Value, name: ID) -> Value;
|
|
165
|
+
fn rb_check_typeddata(obj: Value, data_type: &DataType) -> *mut c_void;
|
|
166
|
+
fn rb_const_defined_at(space: Value, name: ID) -> core::ffi::c_int;
|
|
167
|
+
fn rb_const_get(space: Value, name: ID) -> Value;
|
|
168
|
+
fn rb_const_set(space: Value, name: ID, val: Value);
|
|
169
|
+
fn rb_data_typed_object_zalloc(klass: Value, size: size_t, type_: &DataType) -> Value;
|
|
170
|
+
fn rb_define_class_under(outer: Value, name: *const core::ffi::c_char, super_: Value) -> Value;
|
|
171
|
+
fn rb_define_method(klass: Value, mid: *const core::ffi::c_char, func: Option<unsafe extern "C" fn() -> Value>, arity: core::ffi::c_int);
|
|
172
|
+
fn rb_define_module(name: *const core::ffi::c_char) -> Value;
|
|
173
|
+
fn rb_define_module_function(klass: Value, mid: *const core::ffi::c_char, func: Option<unsafe extern "C" fn() -> Value>, arity: core::ffi::c_int);
|
|
174
|
+
fn rb_define_module_under(outer: Value, name: *const core::ffi::c_char) -> Value;
|
|
175
|
+
fn rb_define_private_method(klass: Value, mid: *const core::ffi::c_char, func: Option<unsafe extern "C" fn() -> Value>, arity: core::ffi::c_int);
|
|
176
|
+
fn rb_define_singleton_method(obj: Value, mid: *const core::ffi::c_char, func: Option<unsafe extern "C" fn() -> Value>, arity: core::ffi::c_int);
|
|
177
|
+
fn rb_enc_associate(obj: Value, enc: *const rb_encoding) -> Value;
|
|
178
|
+
fn rb_enc_str_coderange(str_: Value) -> core::ffi::c_int;
|
|
179
|
+
fn rb_enc_find_index(name: *const core::ffi::c_char) -> core::ffi::c_int;
|
|
180
|
+
fn rb_enc_from_encoding(enc: *const rb_encoding) -> Value;
|
|
181
|
+
fn rb_enc_interned_str(ptr: *const core::ffi::c_char, len: core::ffi::c_long, enc: *const rb_encoding) -> Value;
|
|
182
|
+
fn rb_enumeratorize_with_size(recv: Value, meth: Value, argc: core::ffi::c_int, argv: *const Value, func: EnumeratorSize) -> Value;
|
|
183
|
+
fn rb_exc_new_str(etype: Value, str_: Value) -> Value;
|
|
184
|
+
fn rb_float_new(d: f64) -> Value;
|
|
185
|
+
fn rb_funcallv(recv: Value, mid: ID, argc: core::ffi::c_int, argv: *const Value) -> Value;
|
|
186
|
+
fn rb_funcallv_public(recv: Value, mid: ID, argc: core::ffi::c_int, argv: *const Value) -> Value;
|
|
187
|
+
fn rb_gc_register_mark_object(object: Value);
|
|
188
|
+
fn rb_hash_aset(hash: Value, key: Value, val: Value) -> Value;
|
|
189
|
+
fn rb_hash_bulk_insert(argc: core::ffi::c_long, argv: *const Value, hash: Value);
|
|
190
|
+
fn rb_hash_foreach(hash: Value, each: HashEach, arg: *mut c_void);
|
|
191
|
+
fn rb_hash_lookup2(hash: Value, key: Value, def: Value) -> Value;
|
|
192
|
+
fn rb_hash_new() -> Value;
|
|
193
|
+
fn rb_id2sym(id: ID) -> Value;
|
|
194
|
+
fn rb_include_module(klass: Value, module: Value);
|
|
195
|
+
fn rb_intern(name: *const core::ffi::c_char) -> ID;
|
|
196
|
+
fn rb_intern3(name: *const core::ffi::c_char, len: core::ffi::c_long, enc: *const rb_encoding) -> ID;
|
|
197
|
+
fn rb_ivar_set(obj: Value, name: ID, val: Value) -> Value;
|
|
198
|
+
fn rb_ll2inum(num: core::ffi::c_longlong) -> Value;
|
|
199
|
+
fn rb_module_new() -> Value;
|
|
200
|
+
fn rb_obj_classname(obj: Value) -> *const core::ffi::c_char;
|
|
201
|
+
fn rb_obj_freeze(obj: Value) -> Value;
|
|
202
|
+
fn rb_obj_reveal(obj: Value, klass: Value) -> Value;
|
|
203
|
+
fn rb_range_beg_len(range: Value, begp: *mut core::ffi::c_long, lenp: *mut core::ffi::c_long, len: core::ffi::c_long, err: core::ffi::c_int) -> Value;
|
|
204
|
+
fn rb_respond_to(obj: Value, mid: ID) -> core::ffi::c_int;
|
|
205
|
+
fn rb_ractor_make_shareable(obj: Value) -> Value;
|
|
206
|
+
fn rb_range_values(range: Value, begp: *mut Value, endp: *mut Value, exclp: *mut core::ffi::c_int) -> core::ffi::c_int;
|
|
207
|
+
fn rb_str_dup(str_: Value) -> Value;
|
|
208
|
+
fn rb_str_encode(str_: Value, to: Value, ecflags: core::ffi::c_int, ecopts: Value) -> Value;
|
|
209
|
+
fn rb_str_locktmp(str_: Value) -> Value;
|
|
210
|
+
fn rb_str_new(ptr: *const core::ffi::c_char, len: core::ffi::c_long) -> Value;
|
|
211
|
+
fn rb_str_to_inum(str_: Value, base: core::ffi::c_int, badcheck: core::ffi::c_int) -> Value;
|
|
212
|
+
fn rb_sym2str(symbol: Value) -> Value;
|
|
213
|
+
fn rb_thread_check_ints();
|
|
214
|
+
fn rb_to_encoding(obj: Value) -> *mut rb_encoding;
|
|
215
|
+
fn rb_ull2inum(num: core::ffi::c_ulonglong) -> Value;
|
|
216
|
+
fn rb_undef_alloc_func(klass: Value);
|
|
217
|
+
fn rb_utf8_str_new(ptr: *const core::ffi::c_char, len: core::ffi::c_long) -> Value;
|
|
218
|
+
fn rb_yield(val: Value) -> Value;
|
|
219
|
+
fn rb_yield_values2(argc: core::ffi::c_int, argv: *const Value) -> Value;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/// `rb_exc_raise`, inside the protection: raise `exc`. Raises, by design.
|
|
223
|
+
///
|
|
224
|
+
/// # Safety
|
|
225
|
+
///
|
|
226
|
+
/// `exc` is an exception object.
|
|
227
|
+
#[inline(always)]
|
|
228
|
+
pub unsafe fn rb_exc_raise(_: &Protected, exc: Value) -> ! {
|
|
229
|
+
// SAFETY: the contract; the token is the protection.
|
|
230
|
+
unsafe { rb_sys::rb_exc_raise(exc.as_raw()) }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// `rb_hash_foreach`'s callback: a pair of the Hash's, and the walk's own
|
|
234
|
+
/// data. `Value` is `repr(transparent)`, and a pointer is a `VALUE`'s width
|
|
235
|
+
/// (`uintptr_t`), so this is the function C calls.
|
|
236
|
+
pub type HashEach = unsafe extern "C" fn(key: Value, val: Value, arg: *mut c_void) -> c_int;
|
|
237
|
+
|
|
238
|
+
impl IntoC<Option<unsafe extern "C" fn(VALUE, VALUE, VALUE) -> c_int>> for HashEach {
|
|
239
|
+
#[inline(always)]
|
|
240
|
+
fn into_c(self) -> Option<unsafe extern "C" fn(VALUE, VALUE, VALUE) -> c_int> {
|
|
241
|
+
// SAFETY: the same function pointer, as C declares it (see `HashEach`).
|
|
242
|
+
Some(unsafe {
|
|
243
|
+
core::mem::transmute::<HashEach, unsafe extern "C" fn(VALUE, VALUE, VALUE) -> c_int>(
|
|
244
|
+
self,
|
|
245
|
+
)
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/// A callback's own data, as C's `VALUE`-wide argument carries it
|
|
251
|
+
/// (`rb_hash_foreach`'s `arg`).
|
|
252
|
+
impl IntoC<VALUE> for *mut c_void {
|
|
253
|
+
#[inline(always)]
|
|
254
|
+
fn into_c(self) -> VALUE {
|
|
255
|
+
self as VALUE
|
|
256
|
+
}
|
|
257
|
+
}
|