lexer_kit 0.6.0 → 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/trie.rb +1 -1
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +20 -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 +102 -8
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
//! [`Error`]: a Ruby exception to raise, or one that was raised.
|
|
2
|
+
//!
|
|
3
|
+
//! Nothing here raises until `Error::raise`, which only a method's
|
|
4
|
+
//! trampoline calls, once every Rust frame of the method has returned. Until
|
|
5
|
+
//! then an error is an ordinary value, returned up through `?` with every
|
|
6
|
+
//! destructor on the way run.
|
|
7
|
+
//!
|
|
8
|
+
//! Nothing here allocates either, of Rust's or of Ruby's, until then: a fixed
|
|
9
|
+
//! message is a `&'static str`, a formatted one (`format_args!`) is written
|
|
10
|
+
//! into a buffer the error holds inline (`crate::msg::Msg`), and the exception
|
|
11
|
+
//! is made from either as it is raised, under `rb_protect` - so a failure to
|
|
12
|
+
//! make it is Ruby's `NoMemoryError`, never an abort. An error can be made
|
|
13
|
+
//! anywhere, an edit's closure that must run no Ruby included.
|
|
14
|
+
|
|
15
|
+
#![forbid(unsafe_code)]
|
|
16
|
+
|
|
17
|
+
use core::ffi::c_int;
|
|
18
|
+
use core::fmt;
|
|
19
|
+
|
|
20
|
+
use super::ruby::Ruby;
|
|
21
|
+
use super::value::{ExceptionClass, ReprValue, Value};
|
|
22
|
+
use crate::rb::global::RbConst;
|
|
23
|
+
|
|
24
|
+
/// The class of an exception not yet made: named, and read only when the
|
|
25
|
+
/// exception is raised (or asked [`Error::is_kind_of`]), by a caller that
|
|
26
|
+
/// holds the GVL. So making an [`Error`] reads no Ruby at all - not even a
|
|
27
|
+
/// C global - and needs no [`Ruby`] token: an edit's closure, which must run
|
|
28
|
+
/// no Ruby, can make one.
|
|
29
|
+
#[derive(Clone, Copy)]
|
|
30
|
+
pub enum ErrorClass {
|
|
31
|
+
/// One of Ruby's own exception classes.
|
|
32
|
+
Builtin(Builtin),
|
|
33
|
+
/// A class the extension defines at `Init_` and registers;
|
|
34
|
+
/// `RuntimeError` if it is read before it is set.
|
|
35
|
+
Registered(&'static RbConst<ExceptionClass>),
|
|
36
|
+
/// A class value a caller holding the GVL already has.
|
|
37
|
+
Value(ExceptionClass),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Ruby's own exception classes: each one's [`Builtin`] variant, its
|
|
41
|
+
/// accessor on [`Ruby`] and the C global it reads, written once here.
|
|
42
|
+
macro_rules! builtins {
|
|
43
|
+
($($variant:ident => $method:ident => $global:ident),* $(,)?) => {
|
|
44
|
+
/// Ruby's own exception classes an [`Error`] can name without a token.
|
|
45
|
+
#[derive(Clone, Copy)]
|
|
46
|
+
pub enum Builtin {
|
|
47
|
+
$($variant,)*
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
impl Builtin {
|
|
51
|
+
/// The class, read with the GVL held.
|
|
52
|
+
#[inline]
|
|
53
|
+
fn class(self, ruby: &Ruby) -> ExceptionClass {
|
|
54
|
+
match self {
|
|
55
|
+
$(Builtin::$variant => ruby.$method(),)*
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impl Ruby {
|
|
61
|
+
$(
|
|
62
|
+
#[doc = concat!("`", stringify!($global), "`.")]
|
|
63
|
+
#[inline]
|
|
64
|
+
pub fn $method(&self) -> ExceptionClass {
|
|
65
|
+
ExceptionClass::from_known(Value::from_sys(
|
|
66
|
+
nupur_sys::non_raising::$global(self.sys()),
|
|
67
|
+
))
|
|
68
|
+
}
|
|
69
|
+
)*
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
builtins! {
|
|
75
|
+
RuntimeError => exception_runtime_error => rb_eRuntimeError,
|
|
76
|
+
ArgumentError => exception_arg_error => rb_eArgError,
|
|
77
|
+
TypeError => exception_type_error => rb_eTypeError,
|
|
78
|
+
RangeError => exception_range_error => rb_eRangeError,
|
|
79
|
+
NotImplementedError => exception_not_imp_error => rb_eNotImpError,
|
|
80
|
+
NoMemoryError => exception_no_mem_error => rb_eNoMemError,
|
|
81
|
+
EncodingError => exception_encoding_error => rb_eEncodingError,
|
|
82
|
+
LoadError => exception_load_error => rb_eLoadError,
|
|
83
|
+
KeyError => exception_key_error => rb_eKeyError,
|
|
84
|
+
IndexError => exception_index_error => rb_eIndexError,
|
|
85
|
+
FrozenError => exception_frozen_error => rb_eFrozenError,
|
|
86
|
+
ThreadError => exception_thread_error => rb_eThreadError,
|
|
87
|
+
StopIteration => exception_stop_iteration => rb_eStopIteration,
|
|
88
|
+
ZeroDivisionError => exception_zero_div_error => rb_eZeroDivError,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
impl ErrorClass {
|
|
92
|
+
/// The class, read with the GVL held.
|
|
93
|
+
#[inline]
|
|
94
|
+
pub fn resolve(self, ruby: &Ruby) -> ExceptionClass {
|
|
95
|
+
match self {
|
|
96
|
+
ErrorClass::Builtin(b) => b.class(ruby),
|
|
97
|
+
ErrorClass::Registered(c) => c.exception(ruby),
|
|
98
|
+
ErrorClass::Value(c) => c,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
impl From<Builtin> for ErrorClass {
|
|
104
|
+
#[inline]
|
|
105
|
+
fn from(b: Builtin) -> ErrorClass {
|
|
106
|
+
ErrorClass::Builtin(b)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
impl From<&'static RbConst<ExceptionClass>> for ErrorClass {
|
|
111
|
+
#[inline]
|
|
112
|
+
fn from(c: &'static RbConst<ExceptionClass>) -> ErrorClass {
|
|
113
|
+
ErrorClass::Registered(c)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
impl From<ExceptionClass> for ErrorClass {
|
|
118
|
+
#[inline]
|
|
119
|
+
fn from(c: ExceptionClass) -> ErrorClass {
|
|
120
|
+
ErrorClass::Value(c)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// What went wrong, as Ruby will see it.
|
|
125
|
+
pub struct Error(Kind);
|
|
126
|
+
|
|
127
|
+
#[allow(
|
|
128
|
+
clippy::large_enum_variant,
|
|
129
|
+
reason = "an Error holds its message inline (`crate::msg::Msg`), so it is never boxed"
|
|
130
|
+
)]
|
|
131
|
+
enum Kind {
|
|
132
|
+
/// An exception of `class` with a message, made when it is raised.
|
|
133
|
+
New(ErrorClass, Text),
|
|
134
|
+
/// An exception object - one a protected C call caught.
|
|
135
|
+
Exception(Value),
|
|
136
|
+
/// A non-local exit a protected call caught that is not an exception -
|
|
137
|
+
/// a `throw`, a `break` or `return` out of a block, a thread kill - by
|
|
138
|
+
/// its `rb_protect` state, resumed as it was.
|
|
139
|
+
Jump(c_int),
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// The message of an exception not yet made: fixed, or formatted into a
|
|
143
|
+
/// buffer held inline.
|
|
144
|
+
#[allow(
|
|
145
|
+
clippy::large_enum_variant,
|
|
146
|
+
reason = "a formatted message is held inline (`crate::msg::Msg`), so it is never boxed"
|
|
147
|
+
)]
|
|
148
|
+
enum Text {
|
|
149
|
+
Fixed(&'static str),
|
|
150
|
+
Formatted(crate::msg::Msg),
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl Text {
|
|
154
|
+
#[inline]
|
|
155
|
+
fn as_str(&self) -> &str {
|
|
156
|
+
match self {
|
|
157
|
+
Text::Fixed(s) => s,
|
|
158
|
+
Text::Formatted(m) => m.as_str(),
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/// The bytes, as the exception is made from them: no UTF-8 check, which
|
|
163
|
+
/// the raise path - a `no_unwind` site - has no use for.
|
|
164
|
+
#[inline]
|
|
165
|
+
fn as_bytes(&self) -> &[u8] {
|
|
166
|
+
match self {
|
|
167
|
+
Text::Fixed(s) => s.as_bytes(),
|
|
168
|
+
Text::Formatted(m) => m.as_bytes(),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// A message an [`Error`] can carry: a `&'static str`, or `format_args!`.
|
|
174
|
+
pub trait Message {
|
|
175
|
+
fn into_error(self, class: ErrorClass) -> Error;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
impl Message for &'static str {
|
|
179
|
+
#[inline]
|
|
180
|
+
fn into_error(self, class: ErrorClass) -> Error {
|
|
181
|
+
Error(Kind::New(class, Text::Fixed(self)))
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
impl Message for crate::msg::Msg {
|
|
186
|
+
#[inline]
|
|
187
|
+
fn into_error(self, class: ErrorClass) -> Error {
|
|
188
|
+
Error(Kind::New(class, Text::Formatted(self)))
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
impl Message for fmt::Arguments<'_> {
|
|
193
|
+
fn into_error(self, class: ErrorClass) -> Error {
|
|
194
|
+
match self.as_str() {
|
|
195
|
+
Some(s) => Error(Kind::New(class, Text::Fixed(s))),
|
|
196
|
+
None => Error(Kind::New(
|
|
197
|
+
class,
|
|
198
|
+
Text::Formatted(crate::msg::Msg::format(self)),
|
|
199
|
+
)),
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
impl Error {
|
|
205
|
+
/// An exception of `class` carrying `msg`.
|
|
206
|
+
#[inline]
|
|
207
|
+
pub fn new(class: impl Into<ErrorClass>, msg: impl Message) -> Error {
|
|
208
|
+
msg.into_error(class.into())
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/// The exception a protected call caught (`raw::protect`).
|
|
212
|
+
#[inline]
|
|
213
|
+
pub(crate) fn exception(exc: Value) -> Error {
|
|
214
|
+
Error(Kind::Exception(exc))
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/// The non-local exit `state` stands for.
|
|
218
|
+
#[inline]
|
|
219
|
+
pub(crate) fn jump(state: c_int) -> Error {
|
|
220
|
+
Error(Kind::Jump(state))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/// An instance of `class`, or one that will be? `false` for an exit that
|
|
224
|
+
/// is not an exception, and for a registered class not yet set, which
|
|
225
|
+
/// names no class (its `RuntimeError` stands in for a raise alone, so a
|
|
226
|
+
/// handler of it must not catch every `RuntimeError`).
|
|
227
|
+
pub fn is_kind_of(&self, ruby: &Ruby, class: impl Into<ErrorClass>) -> bool {
|
|
228
|
+
let class = match class.into() {
|
|
229
|
+
ErrorClass::Registered(c) => match c.get(ruby) {
|
|
230
|
+
Some(c) => c,
|
|
231
|
+
None => return false,
|
|
232
|
+
},
|
|
233
|
+
other => other.resolve(ruby),
|
|
234
|
+
};
|
|
235
|
+
match &self.0 {
|
|
236
|
+
Kind::New(c, _) => {
|
|
237
|
+
crate::rb::object::class_inherits(c.resolve(ruby).as_value(), class.as_value())
|
|
238
|
+
}
|
|
239
|
+
Kind::Exception(v) => v.is_kind_of(class),
|
|
240
|
+
Kind::Jump(_) => false,
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/// Handle this error, if it is an exception of one of `classes` (or of
|
|
245
|
+
/// a class deriving from one): `Ok` with a [`Rescued`], which can be
|
|
246
|
+
/// dropped. Anything else comes back as the `Err` it was - another
|
|
247
|
+
/// exception, and every non-local exit (a `throw`, a `break`, a thread
|
|
248
|
+
/// kill), which no class names - to be passed on with `?`.
|
|
249
|
+
///
|
|
250
|
+
/// The way an `Err` becomes "no answer" (`rake dylint`'s
|
|
251
|
+
/// `discarded_ruby_error` refuses the others; the one exception is
|
|
252
|
+
/// [`crate::describe`]'s wording of a message), so a handler names what
|
|
253
|
+
/// it handles, and an exit cannot be handled by mistake.
|
|
254
|
+
pub fn rescue<C: Into<ErrorClass> + Copy>(
|
|
255
|
+
self,
|
|
256
|
+
ruby: &Ruby,
|
|
257
|
+
classes: &[C],
|
|
258
|
+
) -> Result<Rescued, Error> {
|
|
259
|
+
if classes.iter().any(|&c| self.is_kind_of(ruby, c)) {
|
|
260
|
+
Ok(Rescued(()))
|
|
261
|
+
} else {
|
|
262
|
+
Err(self)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/// The message, for wording another error with it - "error" for an exit
|
|
267
|
+
/// that carries none. Reads an exception's message without calling it,
|
|
268
|
+
/// as it is written out.
|
|
269
|
+
pub fn wording<'a>(&'a self, ruby: &'a Ruby) -> Wording<'a> {
|
|
270
|
+
Wording(self, ruby)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/// Raise it. Only a method's trampoline calls this, as its last act:
|
|
274
|
+
/// the raise is a `longjmp` over every frame up to Ruby's, which runs no
|
|
275
|
+
/// destructor, so nothing may be owned on the way - and nothing is.
|
|
276
|
+
///
|
|
277
|
+
/// An exit still pending ([`crate::raw::pending_exit`]) goes first,
|
|
278
|
+
/// whatever this error is: the method dropped the exit's own `Err`, and
|
|
279
|
+
/// a `throw` or a kill must not be lost for the error that followed it.
|
|
280
|
+
#[inline]
|
|
281
|
+
pub(crate) fn raise(self, ruby: &Ruby) -> ! {
|
|
282
|
+
let mut e = crate::raw::pending_exit().unwrap_or(self);
|
|
283
|
+
loop {
|
|
284
|
+
let exc = match e.0 {
|
|
285
|
+
Kind::Jump(state) => crate::raw::resume(state),
|
|
286
|
+
Kind::Exception(v) => v,
|
|
287
|
+
/* Made under `rb_protect`: the String and the object are
|
|
288
|
+
* allocations, and an exception class's `initialize` is
|
|
289
|
+
* Ruby's. A failure there is raised in its place. */
|
|
290
|
+
Kind::New(class, msg) => {
|
|
291
|
+
match crate::raw::exception_new(ruby, class.resolve(ruby), msg.as_bytes()) {
|
|
292
|
+
Ok(v) => v,
|
|
293
|
+
Err(again) => {
|
|
294
|
+
e = again;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
crate::raw::exc_raise(exc)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/// An exception [`Error::rescue`] handled: proof that what was dropped was
|
|
306
|
+
/// one of the classes its handler named, never a non-local exit. Carries
|
|
307
|
+
/// nothing; dropping it is the point.
|
|
308
|
+
pub struct Rescued(());
|
|
309
|
+
|
|
310
|
+
/// An [`Error`]'s message, as [`Error::wording`] writes it.
|
|
311
|
+
pub struct Wording<'a>(&'a Error, &'a Ruby);
|
|
312
|
+
|
|
313
|
+
impl fmt::Display for Wording<'_> {
|
|
314
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
315
|
+
match &self.0 .0 {
|
|
316
|
+
Kind::New(_, msg) => f.write_str(msg.as_str()),
|
|
317
|
+
Kind::Exception(v) => crate::describe::ExceptionText(self.1, *v).fmt(f),
|
|
318
|
+
Kind::Jump(_) => f.write_str("error"),
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
impl core::fmt::Debug for Error {
|
|
324
|
+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
325
|
+
match &self.0 {
|
|
326
|
+
Kind::New(_, msg) => write!(f, "Error({:?})", msg.as_str()),
|
|
327
|
+
Kind::Exception(_) => f.write_str("Error(<exception>)"),
|
|
328
|
+
Kind::Jump(state) => write!(f, "Error(<jump {state}>)"),
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/* ---- the errors every layer makes ---- */
|
|
334
|
+
|
|
335
|
+
/// The internal error: the class a broken invariant of the layer raises, as
|
|
336
|
+
/// the extension registers it ([`RbConst::set`](crate::rb::global::RbConst::set)),
|
|
337
|
+
/// and `RuntimeError` until it does.
|
|
338
|
+
pub static INTERNAL_ERROR: RbConst<ExceptionClass> = RbConst::new();
|
|
339
|
+
|
|
340
|
+
/// An internal error carrying `msg`: a broken invariant, which a bare
|
|
341
|
+
/// `rescue` (StandardError) passes through - the registered class
|
|
342
|
+
/// ([`INTERNAL_ERROR`]), or `RuntimeError` before one is.
|
|
343
|
+
pub fn internal_error(msg: impl crate::rb::Message) -> Error {
|
|
344
|
+
Error::new(&INTERNAL_ERROR, msg)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/// A `TypeError` with `msg`.
|
|
348
|
+
pub fn type_error(msg: impl crate::rb::Message) -> Error {
|
|
349
|
+
Error::new(Builtin::TypeError, msg)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/// An `ArgumentError` with `msg`.
|
|
353
|
+
pub fn arg_error(msg: impl crate::rb::Message) -> Error {
|
|
354
|
+
Error::new(Builtin::ArgumentError, msg)
|
|
355
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
//! Values kept for the rest of the process, rooted with the GC: [`Opaque`],
|
|
2
|
+
//! and [`RbConst`], the class handles an extension defines at `Init_` and
|
|
3
|
+
//! reads in its methods.
|
|
4
|
+
|
|
5
|
+
#![allow(unsafe_code)]
|
|
6
|
+
|
|
7
|
+
use std::sync::OnceLock;
|
|
8
|
+
|
|
9
|
+
use super::class::DataClass;
|
|
10
|
+
use super::error::{Builtin, Error};
|
|
11
|
+
use super::ruby::Ruby;
|
|
12
|
+
use super::value::{ExceptionClass, RClass, RModule, ReprValue, Value};
|
|
13
|
+
|
|
14
|
+
/// A kind of value every Ractor may read: a class or a module, which Ruby
|
|
15
|
+
/// makes shareable as it creates one (`FL_SHAREABLE`, `class_alloc`). What a
|
|
16
|
+
/// static may keep - [`Opaque`], [`RbConst`] - since a static is read from
|
|
17
|
+
/// whichever Ractor runs the method, each under its own GVL; a String or an
|
|
18
|
+
/// Array kept there would be one Ractor's object read by another. Sealed: an
|
|
19
|
+
/// extension cannot add a kind.
|
|
20
|
+
pub trait Shareable: ReprValue + sealed::Sealed {}
|
|
21
|
+
|
|
22
|
+
mod sealed {
|
|
23
|
+
pub trait Sealed {}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
macro_rules! shareable {
|
|
27
|
+
($($t:ty),*) => {
|
|
28
|
+
$(
|
|
29
|
+
impl sealed::Sealed for $t {}
|
|
30
|
+
impl Shareable for $t {}
|
|
31
|
+
)*
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
shareable!(RClass, RModule, ExceptionClass, DataClass);
|
|
36
|
+
|
|
37
|
+
/// A value kept for the rest of the process: registered with the GC as a
|
|
38
|
+
/// mark root, so it is never collected or moved, and readable from any
|
|
39
|
+
/// thread that holds its Ractor's GVL ([`Opaque::get`]) - a [`Shareable`]
|
|
40
|
+
/// value, so from any Ractor. What the class table in
|
|
41
|
+
/// an extension's `Init_` registers are made of.
|
|
42
|
+
#[derive(Clone, Copy)]
|
|
43
|
+
pub struct Opaque<T>(T);
|
|
44
|
+
|
|
45
|
+
// SAFETY: an `Opaque` is read only through `Opaque::get`, which takes the
|
|
46
|
+
// GVL's proof; the value itself is pinned and rooted by `register`, and is
|
|
47
|
+
// `Shareable`, so a thread of any Ractor may read it.
|
|
48
|
+
unsafe impl<T: Shareable> Send for Opaque<T> {}
|
|
49
|
+
// SAFETY: as `Send`.
|
|
50
|
+
unsafe impl<T: Shareable> Sync for Opaque<T> {}
|
|
51
|
+
|
|
52
|
+
impl<T: Shareable> Opaque<T> {
|
|
53
|
+
/// `v`, rooted with the GC for the rest of the process.
|
|
54
|
+
pub fn register(v: T) -> Result<Opaque<T>, Error> {
|
|
55
|
+
crate::raw::gc_register_mark_object(v.as_value())?;
|
|
56
|
+
Ok(Opaque(v))
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impl<T: Copy> Opaque<T> {
|
|
61
|
+
/// The value it keeps, read with the GVL held.
|
|
62
|
+
#[inline]
|
|
63
|
+
pub fn get(self, _: &Ruby) -> T {
|
|
64
|
+
self.0
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// A class, module or exception class an extension's `Init_` defines, for its methods to read.
|
|
69
|
+
///
|
|
70
|
+
/// Typed: an `RbConst<RClass>` can only ever hold an `RClass`, so a read hands
|
|
71
|
+
/// the handle back with no type check to fail. It is written once, at `Init_`,
|
|
72
|
+
/// which also registers the object with the GC - so it stays alive and unmoved
|
|
73
|
+
/// even if Ruby code removes the constant that named it.
|
|
74
|
+
///
|
|
75
|
+
/// Unset is the state before the extension's `Init_`, when none of the methods that read
|
|
76
|
+
/// one exist yet. A read in that state answers `None` (or the documented
|
|
77
|
+
/// fallback of the reader below) rather than panicking.
|
|
78
|
+
pub struct RbConst<T>(OnceLock<Opaque<T>>);
|
|
79
|
+
|
|
80
|
+
impl<T: ReprValue> Default for RbConst<T> {
|
|
81
|
+
fn default() -> RbConst<T> {
|
|
82
|
+
RbConst::new()
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
impl<T: ReprValue> RbConst<T> {
|
|
87
|
+
pub const fn new() -> RbConst<T> {
|
|
88
|
+
RbConst(OnceLock::new())
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Record `v`, rooted for the rest of the process. `Init_` is the only
|
|
92
|
+
/// writer, and a second write is refused rather than replacing the first.
|
|
93
|
+
pub fn set(&self, v: T) -> Result<(), Error>
|
|
94
|
+
where
|
|
95
|
+
T: Shareable,
|
|
96
|
+
{
|
|
97
|
+
const TWICE: &str = "a constant handle was set twice";
|
|
98
|
+
/* Refused before the value is rooted: a root is never released, so
|
|
99
|
+
* rooting one the cell then turns away would keep it for good. (The
|
|
100
|
+
* GVL serialises the two steps.) */
|
|
101
|
+
if self.0.get().is_some() {
|
|
102
|
+
return Err(Error::new(Builtin::RuntimeError, TWICE));
|
|
103
|
+
}
|
|
104
|
+
self.0
|
|
105
|
+
.set(Opaque::register(v)?)
|
|
106
|
+
.map_err(|_| Error::new(Builtin::RuntimeError, TWICE))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// The object, or `None` before the extension's `Init_`.
|
|
110
|
+
#[inline]
|
|
111
|
+
pub fn get(&self, ruby: &Ruby) -> Option<T> {
|
|
112
|
+
self.0.get().map(|o| o.get(ruby))
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// The object, for the registration code `Init_` runs once it is set.
|
|
116
|
+
pub fn defined(&self, ruby: &Ruby) -> Result<T, Error> {
|
|
117
|
+
self.get(ruby).ok_or_else(|| {
|
|
118
|
+
Error::new(
|
|
119
|
+
Builtin::RuntimeError,
|
|
120
|
+
"a constant handle was read before the extension initialised",
|
|
121
|
+
)
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
impl RbConst<ExceptionClass> {
|
|
127
|
+
/// The class to raise. Before the extension's `Init_` - when nothing that raises one
|
|
128
|
+
/// of these can run - it is `RuntimeError`, so a raise still raises.
|
|
129
|
+
#[inline]
|
|
130
|
+
pub fn exception(&self, ruby: &Ruby) -> ExceptionClass {
|
|
131
|
+
self.get(ruby)
|
|
132
|
+
.unwrap_or_else(|| ruby.exception_runtime_error())
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Is `v` an instance of the class in `klass`? `false` before the extension's `Init_`,
|
|
137
|
+
/// when no class of ours exists for it to be an instance of.
|
|
138
|
+
#[inline]
|
|
139
|
+
pub fn is_kind_of<C: ReprValue>(ruby: &Ruby, v: Value, klass: &RbConst<C>) -> bool {
|
|
140
|
+
klass.get(ruby).is_some_and(|c| v.is_kind_of(c))
|
|
141
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//! [`Kept`]: a Ruby value held where the GC's stack scan finds it until the
|
|
2
|
+
//! holder is dropped - what a `typed::Typed` handle borrows its object's
|
|
3
|
+
//! data through - and [`keep_alive`], the empty `asm!` block that puts it
|
|
4
|
+
//! there.
|
|
5
|
+
|
|
6
|
+
#![forbid(unsafe_code)]
|
|
7
|
+
|
|
8
|
+
use super::value::{AsSys, ReprValue, Value};
|
|
9
|
+
|
|
10
|
+
/// A Ruby value held where the GC's scan finds it until this is dropped -
|
|
11
|
+
/// what a `typed::Typed` handle holds, so the object stays while its data is
|
|
12
|
+
/// borrowed, whether Ruby keeps it as well or this frame alone does (an
|
|
13
|
+
/// object just made). The layer's own.
|
|
14
|
+
///
|
|
15
|
+
/// A borrow of an object's data ([`crate::typed::Typed::data`]) would keep
|
|
16
|
+
/// a bare `Value` alive to the type checker
|
|
17
|
+
/// only: once the `Value` is no longer used, the optimizer may drop it from
|
|
18
|
+
/// every register and stack slot, and the data pointer left - a non-embedded
|
|
19
|
+
/// type's data is its own malloc'd block - is not one the scan takes for the
|
|
20
|
+
/// object. A receiver or an argument is kept by Ruby's own stack, and a
|
|
21
|
+
/// slot's value by its owner; an object nothing else refers to yet is not,
|
|
22
|
+
/// and a handle holds either the same way. Its `Drop` hands the value to
|
|
23
|
+
/// [`keep_alive`], so do not `mem::forget` one.
|
|
24
|
+
pub(crate) struct Kept(Value);
|
|
25
|
+
|
|
26
|
+
impl Kept {
|
|
27
|
+
/// Hold `v`.
|
|
28
|
+
#[inline]
|
|
29
|
+
pub(crate) fn new(v: impl ReprValue) -> Kept {
|
|
30
|
+
Kept(v.as_value())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// The value, borrowed from the holder: a borrow of the object's data
|
|
34
|
+
/// taken through it cannot outlive the `Kept`.
|
|
35
|
+
#[inline]
|
|
36
|
+
pub(crate) fn value(&self) -> &Value {
|
|
37
|
+
&self.0
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
impl Drop for Kept {
|
|
42
|
+
#[inline]
|
|
43
|
+
fn drop(&mut self) {
|
|
44
|
+
let v = self.0;
|
|
45
|
+
crate::no_unwind::must_not_unwind::<crate::no_unwind::DropKept, _>(|| keep_alive(v))
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Keep `v` alive to this point (CRuby's `RB_GC_GUARD`): `nupur_sys`'s
|
|
50
|
+
/// `keep_alive`, an empty `asm!` that takes the value, or a volatile read
|
|
51
|
+
/// where `asm!` is not stable.
|
|
52
|
+
#[inline(always)]
|
|
53
|
+
fn keep_alive(v: Value) {
|
|
54
|
+
nupur_sys::value::keep_alive(v.as_sys());
|
|
55
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//! What a method asks of the call it is running - whether it was given a
|
|
2
|
+
//! block or keywords - and the yields to that block. Each reads the current
|
|
3
|
+
//! control frame, so it answers for a method only from its own body.
|
|
4
|
+
|
|
5
|
+
#![allow(unsafe_code)]
|
|
6
|
+
|
|
7
|
+
use crate::rb::error::Error;
|
|
8
|
+
use crate::rb::ruby::Ruby;
|
|
9
|
+
use crate::rb::value::Value;
|
|
10
|
+
|
|
11
|
+
/// `rb_block_given_p`: whether the method running now was given a block.
|
|
12
|
+
/// Raises nothing and runs no Ruby code; like [`keyword_given`], it reads
|
|
13
|
+
/// the current frame.
|
|
14
|
+
#[inline]
|
|
15
|
+
pub fn block_given(ruby: &Ruby) -> bool {
|
|
16
|
+
// SAFETY: under the GVL (`&Ruby`); a read of the current control frame.
|
|
17
|
+
unsafe { nupur_sys::non_raising::rb_block_given_p(ruby.sys()) != 0 }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// Yield `v` to the method's block (`rb_yield`): the caller's Ruby, which
|
|
21
|
+
/// can raise, `break`, `throw` or `return`. Each comes back as `Err` and is
|
|
22
|
+
/// passed on with `?`, so the trampoline resumes it as it left. Each call is
|
|
23
|
+
/// an `allow` of `runs_callers_ruby` at its site, saying why the block runs
|
|
24
|
+
/// there.
|
|
25
|
+
#[inline]
|
|
26
|
+
#[cfg_attr(
|
|
27
|
+
dylint_lib = "nupur_lints",
|
|
28
|
+
expect(
|
|
29
|
+
runs_callers_ruby,
|
|
30
|
+
reason = "a method's yield of one value; each caller's own allow says why"
|
|
31
|
+
)
|
|
32
|
+
)]
|
|
33
|
+
pub fn yield_value(ruby: &Ruby, v: Value) -> Result<Value, Error> {
|
|
34
|
+
crate::raw::yield_value(ruby, v)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Yield `vals` to the method's block as separate arguments
|
|
38
|
+
/// (`rb_yield_values2`): `yield a, b, c` in Ruby. One Array passed to
|
|
39
|
+
/// [`yield_value`] is one argument instead, which a block destructures but a
|
|
40
|
+
/// lambda refuses, and which is an allocation per call. The block's exits are
|
|
41
|
+
/// [`yield_value`]'s, and so is the `allow` each call needs.
|
|
42
|
+
///
|
|
43
|
+
/// Every value is positional, as `rb_yield_values2` passes them: a trailing
|
|
44
|
+
/// Hash is the last argument, never `**` keywords.
|
|
45
|
+
///
|
|
46
|
+
/// The values are taken as an array by value, not a slice, so the C call
|
|
47
|
+
/// reads them from this call's own stack, where the GC's scan sees them for
|
|
48
|
+
/// as long as the block runs - and the block may allocate before Ruby has
|
|
49
|
+
/// copied them, as it does for a lambda or a `Method`. The count is fixed
|
|
50
|
+
/// where the call is written, as `yield a, b, c`'s is.
|
|
51
|
+
#[inline]
|
|
52
|
+
#[cfg_attr(
|
|
53
|
+
dylint_lib = "nupur_lints",
|
|
54
|
+
expect(
|
|
55
|
+
runs_callers_ruby,
|
|
56
|
+
reason = "a method's yield of several values; each caller's own allow says why"
|
|
57
|
+
)
|
|
58
|
+
)]
|
|
59
|
+
pub fn yield_values<const N: usize>(ruby: &Ruby, vals: [Value; N]) -> Result<Value, Error> {
|
|
60
|
+
crate::raw::yield_values(ruby, vals)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// [`yield_value`], and what the block's exit or exception came back as
|
|
64
|
+
/// DROPPED: a test suite's stand-in for a method that loses an `Err`, to
|
|
65
|
+
/// check that the trampoline still resumes the exit. Here, in the layer,
|
|
66
|
+
/// since an extension that forbids dropping an error cannot write it.
|
|
67
|
+
#[doc(hidden)]
|
|
68
|
+
pub fn yield_dropping_error_for_test(ruby: &Ruby) {
|
|
69
|
+
crate::raw::yield_dropping_error_for_test(ruby)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// `rb_keyword_given_p`: whether the method running now was called with
|
|
73
|
+
/// keywords - the one thing `rb_scan_args` asks Ruby when it separates them.
|
|
74
|
+
/// Raises nothing and runs no Ruby code. It reads the current frame, so it
|
|
75
|
+
/// answers for a method only from that method's own body, before the method
|
|
76
|
+
/// calls into Ruby (a method's argument scan is called first thing).
|
|
77
|
+
#[inline]
|
|
78
|
+
pub fn keyword_given(ruby: &Ruby) -> bool {
|
|
79
|
+
// SAFETY: under the GVL (`&Ruby`); a read of the current control frame.
|
|
80
|
+
unsafe { nupur_sys::non_raising::rb_keyword_given_p(ruby.sys()) != 0 }
|
|
81
|
+
}
|