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.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +265 -0
  3. data/Cargo.toml +37 -0
  4. data/README.md +7 -3
  5. data/crates/lexer_kit_core/Cargo.toml +17 -0
  6. data/crates/lexer_kit_core/src/dfa.rs +437 -0
  7. data/crates/lexer_kit_core/src/format.rs +599 -0
  8. data/crates/lexer_kit_core/src/lib.rs +18 -0
  9. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
  10. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
  11. data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
  12. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
  13. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
  14. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
  15. data/ext/lexer_kit_rust/Cargo.toml +7 -3
  16. data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
  17. data/ext/lexer_kit_rust/src/lib.rs +200 -215
  18. data/lib/lexer_kit/builder/compiler.rb +50 -15
  19. data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
  20. data/lib/lexer_kit/builder/validator.rb +45 -0
  21. data/lib/lexer_kit/builder.rb +34 -7
  22. data/lib/lexer_kit/cli/commands.rb +17 -7
  23. data/lib/lexer_kit/core/source.rb +3 -2
  24. data/lib/lexer_kit/debug/disassembler.rb +0 -2
  25. data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
  26. data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
  27. data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
  28. data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
  29. data/lib/lexer_kit/errors.rb +2 -2
  30. data/lib/lexer_kit/format/lkb1.rb +3 -3
  31. data/lib/lexer_kit/format/lkt1.rb +3 -1
  32. data/lib/lexer_kit/format.rb +4 -4
  33. data/lib/lexer_kit/ir/compiled_program.rb +39 -26
  34. data/lib/lexer_kit/ir/constant_pool.rb +33 -11
  35. data/lib/lexer_kit/ir/dfa_table.rb +9 -19
  36. data/lib/lexer_kit/ir/instruction.rb +3 -0
  37. data/lib/lexer_kit/ir/jump_table.rb +3 -13
  38. data/lib/lexer_kit/ir/keyword_table.rb +0 -9
  39. data/lib/lexer_kit/ir/limits.rb +48 -0
  40. data/lib/lexer_kit/ir/serializer.rb +69 -30
  41. data/lib/lexer_kit/ir.rb +1 -0
  42. data/lib/lexer_kit/trie.rb +1 -1
  43. data/lib/lexer_kit/version.rb +1 -1
  44. data/lib/lexer_kit.rb +20 -2
  45. data/vendor/nupur/Cargo.toml +24 -0
  46. data/vendor/nupur/LICENSE +176 -0
  47. data/vendor/nupur/NOTICE +64 -0
  48. data/vendor/nupur/README.ja.md +150 -0
  49. data/vendor/nupur/README.md +257 -0
  50. data/vendor/nupur/src/abi.rs +143 -0
  51. data/vendor/nupur/src/args.rs +318 -0
  52. data/vendor/nupur/src/build.rs +101 -0
  53. data/vendor/nupur/src/caught.rs +171 -0
  54. data/vendor/nupur/src/cname.rs +37 -0
  55. data/vendor/nupur/src/describe.rs +175 -0
  56. data/vendor/nupur/src/encoding.rs +240 -0
  57. data/vendor/nupur/src/gvl.rs +181 -0
  58. data/vendor/nupur/src/hash.rs +52 -0
  59. data/vendor/nupur/src/inject.rs +20 -0
  60. data/vendor/nupur/src/lib.rs +280 -0
  61. data/vendor/nupur/src/lint_probe.rs +142 -0
  62. data/vendor/nupur/src/msg.rs +193 -0
  63. data/vendor/nupur/src/no_unwind.rs +39 -0
  64. data/vendor/nupur/src/raw/check.rs +71 -0
  65. data/vendor/nupur/src/raw/data.rs +28 -0
  66. data/vendor/nupur/src/raw/define.rs +215 -0
  67. data/vendor/nupur/src/raw/exception.rs +69 -0
  68. data/vendor/nupur/src/raw/iterate.rs +115 -0
  69. data/vendor/nupur/src/raw/mod.rs +105 -0
  70. data/vendor/nupur/src/raw/protect.rs +144 -0
  71. data/vendor/nupur/src/raw/string.rs +148 -0
  72. data/vendor/nupur/src/raw/symbol.rs +38 -0
  73. data/vendor/nupur/src/raw/values.rs +293 -0
  74. data/vendor/nupur/src/rb/call.rs +118 -0
  75. data/vendor/nupur/src/rb/class.rs +170 -0
  76. data/vendor/nupur/src/rb/convert.rs +134 -0
  77. data/vendor/nupur/src/rb/error.rs +355 -0
  78. data/vendor/nupur/src/rb/global.rs +141 -0
  79. data/vendor/nupur/src/rb/kept.rs +55 -0
  80. data/vendor/nupur/src/rb/method/frame.rs +81 -0
  81. data/vendor/nupur/src/rb/method.rs +468 -0
  82. data/vendor/nupur/src/rb/mod.rs +40 -0
  83. data/vendor/nupur/src/rb/object.rs +66 -0
  84. data/vendor/nupur/src/rb/ruby.rs +107 -0
  85. data/vendor/nupur/src/rb/value/header.rs +65 -0
  86. data/vendor/nupur/src/rb/value/id.rs +61 -0
  87. data/vendor/nupur/src/rb/value/integer.rs +132 -0
  88. data/vendor/nupur/src/rb/value.rs +454 -0
  89. data/vendor/nupur/src/stack.rs +32 -0
  90. data/vendor/nupur/src/string/make.rs +234 -0
  91. data/vendor/nupur/src/string/mod.rs +24 -0
  92. data/vendor/nupur/src/string/read.rs +411 -0
  93. data/vendor/nupur/src/table.rs +58 -0
  94. data/vendor/nupur/src/typed/callbacks.rs +125 -0
  95. data/vendor/nupur/src/typed/data_type.rs +132 -0
  96. data/vendor/nupur/src/typed/embedded.rs +103 -0
  97. data/vendor/nupur/src/typed/external.rs +76 -0
  98. data/vendor/nupur/src/typed/hidden.rs +95 -0
  99. data/vendor/nupur/src/typed/mod.rs +373 -0
  100. data/vendor/nupur/src/utf8_copy.rs +262 -0
  101. data/vendor/nupur/sys/Cargo.toml +29 -0
  102. data/vendor/nupur/sys/src/data_type.rs +213 -0
  103. data/vendor/nupur/sys/src/int.rs +248 -0
  104. data/vendor/nupur/sys/src/lib.rs +61 -0
  105. data/vendor/nupur/sys/src/lint_probe.rs +93 -0
  106. data/vendor/nupur/sys/src/no_unwind.rs +99 -0
  107. data/vendor/nupur/sys/src/non_raising.rs +585 -0
  108. data/vendor/nupur/sys/src/protect.rs +349 -0
  109. data/vendor/nupur/sys/src/raising.rs +257 -0
  110. data/vendor/nupur/sys/src/value.rs +279 -0
  111. data/vendor/nupur.sha256 +68 -0
  112. metadata +102 -8
  113. data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
  114. data/ext/lexer_kit_rust/src/dfa.rs +0 -217
@@ -0,0 +1,99 @@
1
+ //! The places in the Ruby layer that must not unwind, and the check that they
2
+ //! cannot - the layer's own copy of the mechanism. It lives here, under
3
+ //! nupur, because the protect thunk does ([`crate::protect`]), which is the
4
+ //! one site of this crate's; Nupur declares its own (the GC callbacks, the
5
+ //! trampolines, its guards' `Drop`s) with [`no_unwind_sites!`](crate::no_unwind_sites) in
6
+ //! `nupur::no_unwind`. An extension keeps its own for its types' `Drop`s and
7
+ //! the engine's: the engine is built without Ruby, where this module does not
8
+ //! exist.
9
+ //!
10
+ //! An unwind out of an `extern "C"` function aborts the process (Rust stops it
11
+ //! at the boundary, since C frames have no unwind tables), and so does a panic
12
+ //! in a `Drop` that runs while another panic unwinds - or in a GC free
13
+ //! callback, which is both. Each such body is listed here as a [`Site`], and
14
+ //! runs inside [`must_not_unwind`].
15
+ //!
16
+ //! In a normal build that costs nothing: a site's guard is a zero-sized value
17
+ //! with no `Drop`. Built with `--cfg nupur_no_unwind` (`rake no_unwind`), each
18
+ //! site's guard gets a `Drop` that calls a symbol nobody defines,
19
+ //! `nupur_unwinds_<site>` - and the only path that runs it is an unwind out
20
+ //! of the body, since a normal return forgets it. The optimizer removes that
21
+ //! path exactly when it can prove the body never unwinds, so the symbol is
22
+ //! left undefined in the release build precisely for a site that CAN unwind:
23
+ //! the check is the compiler's own proof, not a reading of the code. (The
24
+ //! `no-panic` crate's technique, without the dependency; macOS links an
25
+ //! undefined symbol without complaint, so the task reads them with `nm -u`.)
26
+
27
+ #![allow(unsafe_code)]
28
+
29
+ /// A body that must not unwind: its guard is armed before the body runs and
30
+ /// disarmed after, so only an unwind drops it.
31
+ pub trait Site: Sized {
32
+ fn arm() -> Self;
33
+ #[inline(always)]
34
+ fn disarm(self) {
35
+ core::mem::forget(self);
36
+ }
37
+ }
38
+
39
+ /// Run `body` as `S`, a site that must not unwind.
40
+ #[inline(always)]
41
+ pub fn must_not_unwind<S: Site, R>(body: impl FnOnce() -> R) -> R {
42
+ let armed = S::arm();
43
+ let out = body();
44
+ armed.disarm();
45
+ out
46
+ }
47
+
48
+ /// Declare sites: each a [`Site`] whose guard, in the check build
49
+ /// (`--cfg nupur_no_unwind`), calls the undefined symbol named beside it.
50
+ /// Exported so that Nupur declares its own sites where they are, and this
51
+ /// crate only the ones it holds; the cfg is the crate's that expands it,
52
+ /// which the check build sets for both.
53
+ #[macro_export]
54
+ macro_rules! no_unwind_sites {
55
+ ($($(#[$doc:meta])* $name:ident => $sym:literal,)*) => {$(
56
+ $(#[$doc])*
57
+ pub struct $name(());
58
+ impl $crate::no_unwind::Site for $name {
59
+ #[inline(always)]
60
+ fn arm() -> $name {
61
+ $name(())
62
+ }
63
+ }
64
+ #[cfg(nupur_no_unwind)]
65
+ impl Drop for $name {
66
+ #[inline(always)]
67
+ fn drop(&mut self) {
68
+ unsafe extern "C" {
69
+ #[link_name = $sym]
70
+ fn unwinds();
71
+ }
72
+ // SAFETY: never defined, so never called: a build that keeps
73
+ // this call fails the check rather than running it.
74
+ unsafe { unwinds() }
75
+ }
76
+ }
77
+ )*};
78
+ }
79
+
80
+ no_unwind_sites! {
81
+ /// [`crate::protect::call`]'s `rb_protect` thunk.
82
+ ProtectThunk => "nupur_unwinds_protect_thunk",
83
+ /// The check's own control (`self_test`): a body that CAN unwind, so a
84
+ /// check build that still works leaves exactly this symbol undefined.
85
+ SelfTest => "nupur_unwinds_self_test",
86
+ }
87
+
88
+ /// The control for `rake no_unwind`, called from the extension's `Init_` in the check
89
+ /// build only: were the guard ever optimized away whole, every site would
90
+ /// pass, and this one would too - which the task refuses.
91
+ #[cfg(nupur_no_unwind)]
92
+ #[allow(clippy::panic, reason = "the check's control must be able to unwind")]
93
+ pub fn self_test() {
94
+ must_not_unwind::<SelfTest, _>(|| {
95
+ if core::hint::black_box(false) {
96
+ panic!("no_unwind self-test");
97
+ }
98
+ })
99
+ }
@@ -0,0 +1,585 @@
1
+ //! Ruby's C that raises nothing, outside the protection, each call taking a
2
+ //! proof of the GVL - the value it is about ([`Value`]), or the token
3
+ //! ([`Ruby`]) when it takes none. Not all of it reads: some C here writes
4
+ //! (`rb_obj_hide`, `rb_gc_adjust_memory_usage`, `ruby_xfree`) or releases the
5
+ //! GVL (`rb_thread_call_without_gvl2`); what they share is that none can
6
+ //! raise. Here are the C functions the lints' table finds no raise in
7
+ //! (`nupur_lints`' `NON_RAISING`, and the one `REVIEWED` for any caller),
8
+ //! the inline reads of rb-sys's stable API, and the C globals that hold a
9
+ //! class.
10
+ //!
11
+ //! A function that can raise, but raises nothing under a precondition
12
+ //! (`rb_enc_get` of a String, `rb_gc_mark` in a mark callback), is here
13
+ //! named for that precondition, which its `unsafe` contract states: a
14
+ //! caller inherits no reason it has not shown in its own SAFETY.
15
+ //!
16
+ //! The stable API's lengths come back in Rust's widths (`usize`, `i64`),
17
+ //! through [`crate::int`]. The C functions keep their C signatures.
18
+ //!
19
+ //! # Safety
20
+ //!
21
+ //! A function is an `unsafe fn` for what its types cannot show: a value of
22
+ //! the kind the C function names (a String, a Bignum), a pointer to memory
23
+ //! it may read (or write, or free) for the call. A value being live, and the
24
+ //! GVL held, are the [`Value`]'s and the [`Ruby`]'s.
25
+
26
+ #![allow(unsafe_code)]
27
+ #![allow(clippy::missing_safety_doc)]
28
+
29
+ use core::ffi::{c_int, c_void};
30
+
31
+ use rb_sys::stable_api::{get_default, StableApiDefinition};
32
+
33
+ use crate::data_type::DataType;
34
+ use crate::value::{Root, Ruby, Value};
35
+ use crate::{rb_encoding, ruby_value_type, size_t, ssize_t, ID};
36
+ use rb_sys::rb_unblock_function_t;
37
+ use rb_sys::VALUE;
38
+
39
+ macro_rules! with_gvl {
40
+ ($(fn $name:ident($($arg:ident: $ty:ty),*) $(-> $ret:ty)?;)*) => {$(
41
+ #[doc = concat!("`", stringify!($name), "`, with the GVL; raises nothing.")]
42
+ #[inline(always)]
43
+ pub unsafe fn $name(_: &Ruby, $($arg: $ty),*) $(-> $ret)? {
44
+ // SAFETY: the caller's (the module's contract); the token is the
45
+ // GVL.
46
+ unsafe { rb_sys::$name($($arg),*) }
47
+ }
48
+ )*};
49
+ }
50
+
51
+ with_gvl! {
52
+ fn rb_ascii8bit_encindex() -> c_int;
53
+ fn rb_block_given_p() -> c_int;
54
+ fn rb_enc_from_index(idx: c_int) -> *mut rb_encoding;
55
+ fn rb_ext_ractor_safe(flag: bool);
56
+ fn rb_gc_adjust_memory_usage(diff: ssize_t);
57
+ fn rb_keyword_given_p() -> c_int;
58
+ fn rb_thread_call_without_gvl2(
59
+ func: Option<unsafe extern "C" fn(arg1: *mut c_void) -> *mut c_void>,
60
+ data1: *mut c_void,
61
+ ubf: rb_unblock_function_t,
62
+ data2: *mut c_void
63
+ ) -> *mut c_void;
64
+ fn rb_usascii_encindex() -> c_int;
65
+ fn rb_utf8_encindex() -> c_int;
66
+ fn rb_utf8_encoding() -> *mut rb_encoding;
67
+ }
68
+
69
+ /// `rb_enc_to_index`: an encoding's index - a read of the encoding's own
70
+ /// struct, which needs no GVL.
71
+ ///
72
+ /// # Safety
73
+ ///
74
+ /// `enc` is one of Ruby's encodings, which live for the process.
75
+ #[inline(always)]
76
+ #[cfg_attr(
77
+ dylint_lib = "nupur_lints",
78
+ allow(
79
+ ffi_without_gvl_proof,
80
+ reason = "reads a field of an encoding, which lives for the process and is never written"
81
+ )
82
+ )]
83
+ pub unsafe fn rb_enc_to_index(enc: *const rb_encoding) -> c_int {
84
+ // SAFETY: the contract.
85
+ unsafe { rb_sys::rb_enc_to_index(enc) }
86
+ }
87
+
88
+ /// `ruby_native_thread_p`: whether this thread is one Ruby made. The one
89
+ /// read that needs no proof - it is the question of whether there is one -
90
+ /// and is safe from any thread.
91
+ #[inline(always)]
92
+ #[cfg_attr(
93
+ dylint_lib = "nupur_lints",
94
+ allow(
95
+ ffi_without_gvl_proof,
96
+ reason = "asks whether this is a Ruby thread at all, which any thread may ask"
97
+ )
98
+ )]
99
+ pub fn ruby_native_thread_p() -> bool {
100
+ // SAFETY: reads the thread-local Ruby keeps for its own threads, and
101
+ // answers 0 on any other.
102
+ unsafe { rb_sys::ruby_native_thread_p() != 0 }
103
+ }
104
+
105
+ /// Whether the stack is nearly spent (`ruby_stack_check`), on a thread Ruby
106
+ /// runs - with the GVL or without it, as each reads its own stack's bounds.
107
+ /// A thread Ruby does not run has no execution context to read, and is
108
+ /// answered "room left", as a build with no probe answers. Safe from any
109
+ /// thread, so it takes no proof.
110
+ #[inline]
111
+ #[cfg_attr(
112
+ dylint_lib = "nupur_lints",
113
+ allow(
114
+ ffi_without_gvl_proof,
115
+ reason = "reads the stack only on a thread Ruby made, with the GVL or without it, as `ruby_native_thread_p` answers first"
116
+ )
117
+ )]
118
+ pub fn ruby_stack_low() -> bool {
119
+ // SAFETY: `ruby_stack_check` runs only on a thread Ruby made, where it
120
+ // takes no argument, raises nothing, and reads that thread's bounds.
121
+ ruby_native_thread_p() && unsafe { rb_sys::ruby_stack_check() != 0 }
122
+ }
123
+
124
+ /// `rb_ary_entry`: element `off`, `nil` past the end.
125
+ ///
126
+ /// # Safety
127
+ ///
128
+ /// `ary` is an Array.
129
+ #[inline(always)]
130
+ pub unsafe fn rb_ary_entry(ary: Value, off: core::ffi::c_long) -> Value {
131
+ // SAFETY: the contract; the read is bounds-checked by Ruby, and answers
132
+ // an element (live while the Array is) or `nil`.
133
+ unsafe { Value::from_raw(rb_sys::rb_ary_entry(ary.as_raw(), off)) }
134
+ }
135
+
136
+ /// `rb_obj_hide`: take `obj`'s class away.
137
+ ///
138
+ /// # Safety
139
+ ///
140
+ /// `obj` is a heap object no Ruby code holds, and none will be handed while
141
+ /// it is hidden.
142
+ #[inline(always)]
143
+ pub unsafe fn rb_obj_hide(obj: Value) {
144
+ // SAFETY: the contract.
145
+ unsafe { rb_sys::rb_obj_hide(obj.as_raw()) };
146
+ }
147
+
148
+ /// `rb_typeddata_is_kind_of`: is `obj` a TypedData object of `data_type`?
149
+ #[inline(always)]
150
+ pub fn rb_typeddata_is_kind_of(obj: Value, data_type: &DataType) -> bool {
151
+ // SAFETY: `obj` is live, and `data_type` a type's own table.
152
+ unsafe { rb_sys::rb_typeddata_is_kind_of(obj.as_raw(), data_type.as_ptr()) != 0 }
153
+ }
154
+
155
+ /// A heap object's header flags word (`RBasic::flags`), as bits.
156
+ ///
157
+ /// # Safety
158
+ ///
159
+ /// `v` is a heap object (not an immediate), whose header is `RBasic`.
160
+ #[inline(always)]
161
+ pub unsafe fn rbasic_flags(v: Value) -> usize {
162
+ // SAFETY: the contract.
163
+ crate::int::word(unsafe { (*(v.as_raw() as *const rb_sys::RBasic)).flags })
164
+ }
165
+
166
+ /// A heap object's class as its header holds it (`RBasic::klass`): no
167
+ /// method call, no singleton class made.
168
+ ///
169
+ /// # Safety
170
+ ///
171
+ /// As [`rbasic_flags`].
172
+ #[inline(always)]
173
+ pub unsafe fn rbasic_klass(v: Value) -> Value {
174
+ // SAFETY: the contract; the class lives as long as its instance.
175
+ unsafe { Value::from_raw((*(v.as_raw() as *const rb_sys::RBasic)).klass) }
176
+ }
177
+
178
+ /// Marks a String's bytes known valid in its encoding - all ASCII
179
+ /// (`7BIT`) when `ascii`, else `VALID` - so Ruby never scans them for it:
180
+ /// `RB_ENC_CODERANGE_SET`, an inline function of Ruby's headers, which
181
+ /// replaces the bits under `RUBY_ENC_CODERANGE_MASK` in the header flags.
182
+ ///
183
+ /// # Safety
184
+ ///
185
+ /// `v` is a String that no other thread can see yet, and its bytes are
186
+ /// valid in its encoding (and all ASCII when `ascii`). Ruby's string
187
+ /// functions take the mark on trust: a wrong one breaks an invariant they
188
+ /// rely on.
189
+ #[inline]
190
+ pub unsafe fn str_mark_coderange(v: Value, ascii: bool) {
191
+ use rb_sys::ruby_coderange_type as cr;
192
+ let mark = if ascii {
193
+ cr::RUBY_ENC_CODERANGE_7BIT
194
+ } else {
195
+ cr::RUBY_ENC_CODERANGE_VALID
196
+ } as VALUE;
197
+ let mask = cr::RUBY_ENC_CODERANGE_MASK as VALUE;
198
+ // SAFETY: the contract: a heap object (a String), seen by this thread
199
+ // alone, whose header is `RBasic`.
200
+ unsafe {
201
+ let basic = v.as_raw() as *mut rb_sys::RBasic;
202
+ (*basic).flags = ((*basic).flags & !mask) | mark;
203
+ }
204
+ }
205
+
206
+ /* ---- the stable API's inline reads ---- */
207
+
208
+ /// `RB_TEST`: Ruby's truthiness.
209
+ #[inline(always)]
210
+ pub fn rb_test(v: Value) -> bool {
211
+ get_default().rb_test(v.as_raw())
212
+ }
213
+
214
+ /// `RB_SPECIAL_CONST_P`: an immediate.
215
+ #[inline(always)]
216
+ pub fn special_const_p(v: Value) -> bool {
217
+ get_default().special_const_p(v.as_raw())
218
+ }
219
+
220
+ /// `RB_FIXNUM_P`.
221
+ #[inline(always)]
222
+ pub fn fixnum_p(v: Value) -> bool {
223
+ get_default().fixnum_p(v.as_raw())
224
+ }
225
+
226
+ /// `RB_FIX2LONG`, in Rust's width: `v` must be a Fixnum.
227
+ #[inline(always)]
228
+ pub fn fix2long(v: Value) -> i64 {
229
+ i64::from(get_default().fix2long(v.as_raw()))
230
+ }
231
+
232
+ /// `RB_OBJ_FROZEN` of a heap object.
233
+ #[inline(always)]
234
+ pub unsafe fn frozen_p(v: Value) -> bool {
235
+ // SAFETY: the caller's: a non-immediate value.
236
+ unsafe { get_default().frozen_p(v.as_raw()) }
237
+ }
238
+
239
+ /// `rb_type`.
240
+ #[inline(always)]
241
+ pub fn rb_type(v: Value) -> ruby_value_type {
242
+ // SAFETY: a live value, by type.
243
+ unsafe { get_default().rb_type(v.as_raw()) }
244
+ }
245
+
246
+ /// `RB_SYMBOL_P`.
247
+ #[inline(always)]
248
+ pub fn symbol_p(v: Value) -> bool {
249
+ // SAFETY: a live value, by type.
250
+ unsafe { get_default().symbol_p(v.as_raw()) }
251
+ }
252
+
253
+ /// `RB_INTEGER_TYPE_P`.
254
+ #[inline(always)]
255
+ pub fn integer_type_p(v: Value) -> bool {
256
+ // SAFETY: a live value, by type.
257
+ unsafe { get_default().integer_type_p(v.as_raw()) }
258
+ }
259
+
260
+ /// `RB_FLOAT_TYPE_P`.
261
+ #[inline(always)]
262
+ pub fn float_type_p(v: Value) -> bool {
263
+ // SAFETY: a live value, by type.
264
+ unsafe { get_default().float_type_p(v.as_raw()) }
265
+ }
266
+
267
+ /// `RBIGNUM_NEGATIVE_P`.
268
+ #[inline(always)]
269
+ pub unsafe fn bignum_negative_p(v: Value) -> bool {
270
+ // SAFETY: the caller's: a live Bignum.
271
+ unsafe { get_default().bignum_negative_p(v.as_raw()) }
272
+ }
273
+
274
+ /// `RSTRING_LEN`, as a `usize`.
275
+ #[inline(always)]
276
+ pub unsafe fn rstring_len(v: Value) -> usize {
277
+ // SAFETY: the caller's: a live String.
278
+ crate::int::len(unsafe { get_default().rstring_len(v.as_raw()) })
279
+ }
280
+
281
+ /// `RSTRING_PTR`.
282
+ #[inline(always)]
283
+ pub unsafe fn rstring_ptr(v: Value) -> *const core::ffi::c_char {
284
+ // SAFETY: the caller's: a live String.
285
+ unsafe { get_default().rstring_ptr(v.as_raw()) }
286
+ }
287
+
288
+ /// `RARRAY_LEN`, as a `usize`.
289
+ #[inline(always)]
290
+ pub unsafe fn rarray_len(v: Value) -> usize {
291
+ // SAFETY: the caller's: a live Array.
292
+ crate::int::len(unsafe { get_default().rarray_len(v.as_raw()) })
293
+ }
294
+
295
+ /// `RHASH_SIZE`.
296
+ #[inline(always)]
297
+ pub unsafe fn rhash_size(v: Value) -> usize {
298
+ // SAFETY: the caller's: a live Hash.
299
+ unsafe { get_default().rhash_size(v.as_raw()) }
300
+ }
301
+
302
+ /// `NUM2DBL` of a Float.
303
+ #[inline(always)]
304
+ pub unsafe fn num2dbl(v: Value) -> f64 {
305
+ // SAFETY: the caller's: a live Float.
306
+ unsafe { get_default().num2dbl(v.as_raw()) }
307
+ }
308
+
309
+ /// `RTYPEDDATA_GET_DATA`.
310
+ #[inline(always)]
311
+ pub unsafe fn rtypeddata_get_data(v: Value) -> *mut c_void {
312
+ // SAFETY: the caller's: a live TypedData object.
313
+ unsafe { get_default().rtypeddata_get_data(v.as_raw()) }
314
+ }
315
+
316
+ /* ---- the C globals that hold a class ---- */
317
+
318
+ macro_rules! globals {
319
+ ($($name:ident,)*) => {$(
320
+ #[doc = concat!("`", stringify!($name), "`, a C global Ruby sets once at boot.")]
321
+ #[inline(always)]
322
+ #[allow(non_snake_case)]
323
+ pub fn $name(_: &Ruby) -> Value {
324
+ // SAFETY: set at boot and never again, to a class that lives as
325
+ // long as the process; a Ruby thread (the token) runs after boot.
326
+ unsafe { Value::from_raw(rb_sys::$name) }
327
+ }
328
+ )*};
329
+ }
330
+
331
+ globals! {
332
+ rb_cArray,
333
+ rb_cEncoding,
334
+ rb_cFloat,
335
+ rb_cHash,
336
+ rb_cInteger,
337
+ rb_cObject,
338
+ rb_cRange,
339
+ rb_cString,
340
+ rb_cSymbol,
341
+ rb_eArgError,
342
+ rb_eEncodingError,
343
+ rb_eException,
344
+ rb_eFrozenError,
345
+ rb_eIndexError,
346
+ rb_eKeyError,
347
+ rb_eLoadError,
348
+ rb_eNoMemError,
349
+ rb_eNotImpError,
350
+ rb_eRangeError,
351
+ rb_eRuntimeError,
352
+ rb_eStandardError,
353
+ rb_eStopIteration,
354
+ rb_eThreadError,
355
+ rb_eTypeError,
356
+ rb_eZeroDivError,
357
+ }
358
+
359
+ /* ---- the C calls that raise, but not under their precondition ---- */
360
+ /*
361
+ * Each of these C functions can raise, and raises nothing under one
362
+ * precondition, which the wrapper's name and contract state: the reason is
363
+ * the precondition's, so a caller that upholds it (its `unsafe` block's
364
+ * SAFETY says how) inherits nothing it has not shown. The `allow` is at the
365
+ * one call of the C function, here.
366
+ */
367
+
368
+ /// `rb_str_unlocktmp` of a String locked with `rb_str_locktmp`: clears the
369
+ /// lock flag, and allocates nothing.
370
+ ///
371
+ /// # Safety
372
+ ///
373
+ /// `str` is a String that is locked (`rb_str_locktmp` succeeded on it and
374
+ /// it has not been unlocked since).
375
+ #[inline(always)]
376
+ #[cfg_attr(
377
+ dylint_lib = "nupur_lints",
378
+ allow(
379
+ raising_call_outside_protect,
380
+ reason = "rb_str_unlocktmp raises only for a String that is not locked; the contract is one that is"
381
+ )
382
+ )]
383
+ pub unsafe fn str_unlocktmp_of_locked(str: Value) {
384
+ // SAFETY: the contract.
385
+ unsafe {
386
+ rb_sys::rb_str_unlocktmp(str.as_raw());
387
+ }
388
+ }
389
+
390
+ /// `rb_enc_get` of a String: the encoding it is tagged with.
391
+ ///
392
+ /// # Safety
393
+ ///
394
+ /// `str` is a String (not a File, whose encoding is a method call, nor
395
+ /// a Symbol, whose is a String made).
396
+ #[inline(always)]
397
+ #[cfg_attr(
398
+ dylint_lib = "nupur_lints",
399
+ allow(
400
+ raising_call_outside_protect,
401
+ reason = "rb_enc_get raises for a File (a method call) or a Symbol (a String made); the contract is a String, whose tag it reads"
402
+ )
403
+ )]
404
+ pub unsafe fn enc_get_of_string(str: Value) -> *mut rb_encoding {
405
+ // SAFETY: the contract.
406
+ unsafe { rb_sys::rb_enc_get(str.as_raw()) }
407
+ }
408
+
409
+ /// `rb_enc_get_index` of a String: its encoding's index, looked up past
410
+ /// the ones its flags hold inline.
411
+ ///
412
+ /// # Safety
413
+ ///
414
+ /// `str` is a String.
415
+ #[inline(always)]
416
+ #[cfg_attr(
417
+ dylint_lib = "nupur_lints",
418
+ allow(
419
+ raising_call_outside_protect,
420
+ reason = "rb_enc_get_index raises for a File or a Symbol, as rb_enc_get; the contract is a String, whose index is an ivar read when it is not inline"
421
+ )
422
+ )]
423
+ pub unsafe fn enc_get_index_of_string(str: Value) -> c_int {
424
+ // SAFETY: the contract.
425
+ unsafe { rb_sys::rb_enc_get_index(str.as_raw()) }
426
+ }
427
+
428
+ /// `rb_attr_get` of a hidden ivar: one whose name has no `@`.
429
+ ///
430
+ /// # Safety
431
+ ///
432
+ /// `id` is a name without `@` (`rb_is_instance_id` is false for it).
433
+ #[inline(always)]
434
+ #[cfg_attr(
435
+ dylint_lib = "nupur_lints",
436
+ allow(
437
+ raising_call_outside_protect,
438
+ reason = "rb_attr_get raises only RactorIsolationError, and only for an @ name (rb_is_instance_id) of a class, a module or a shareable object read from another Ractor; the contract is a name without @"
439
+ )
440
+ )]
441
+ pub unsafe fn attr_get_hidden(obj: Value, id: ID) -> Value {
442
+ // SAFETY: the contract; the ivar's value, live while `obj` is, or `nil`.
443
+ unsafe { Value::from_raw(rb_sys::rb_attr_get(obj.as_raw(), id)) }
444
+ }
445
+
446
+ /// `rb_obj_is_kind_of` of a class or module: an instance of `module` or of
447
+ /// one deriving from it? Runs no Ruby code.
448
+ ///
449
+ /// # Safety
450
+ ///
451
+ /// `module` is a class or module.
452
+ #[inline(always)]
453
+ #[cfg_attr(
454
+ dylint_lib = "nupur_lints",
455
+ allow(
456
+ raising_call_outside_protect,
457
+ reason = "its one raise, a non-class, is ruled out by the contract"
458
+ )
459
+ )]
460
+ pub unsafe fn obj_is_kind_of_module(v: Value, module: Value) -> bool {
461
+ // SAFETY: the contract.
462
+ unsafe { rb_sys::rb_obj_is_kind_of(v.as_raw(), module.as_raw()) == rb_sys::Qtrue as VALUE }
463
+ }
464
+
465
+ /// `rb_class_inherited_p` of two classes or modules: is `sub` `sup` or a
466
+ /// descendant of it? Runs no Ruby code.
467
+ ///
468
+ /// # Safety
469
+ ///
470
+ /// `sub` and `sup` are classes or modules.
471
+ #[inline(always)]
472
+ #[cfg_attr(
473
+ dylint_lib = "nupur_lints",
474
+ allow(
475
+ raising_call_outside_protect,
476
+ reason = "its one raise, a non-class, is ruled out by the contract"
477
+ )
478
+ )]
479
+ pub unsafe fn class_inherited_p_of_modules(sub: Value, sup: Value) -> bool {
480
+ // SAFETY: the contract.
481
+ unsafe { rb_sys::rb_class_inherited_p(sub.as_raw(), sup.as_raw()) == rb_sys::Qtrue as VALUE }
482
+ }
483
+
484
+ /// `rb_absint_size` of an Integer: the bytes its magnitude takes. Reads its
485
+ /// digits, converts nothing.
486
+ ///
487
+ /// # Safety
488
+ ///
489
+ /// `v` is an Integer.
490
+ #[inline(always)]
491
+ #[cfg_attr(
492
+ dylint_lib = "nupur_lints",
493
+ allow(
494
+ raising_call_outside_protect,
495
+ reason = "its raise is for a non-Integer, converted by `#to_int`; the contract is an Integer"
496
+ )
497
+ )]
498
+ pub unsafe fn absint_size_of_integer(v: Value) -> size_t {
499
+ // SAFETY: the contract.
500
+ unsafe { rb_sys::rb_absint_size(v.as_raw(), core::ptr::null_mut()) }
501
+ }
502
+
503
+ /// `rb_str_set_len(str, 0)` of an unfrozen String: empty it.
504
+ ///
505
+ /// # Safety
506
+ ///
507
+ /// `str` is an unfrozen String.
508
+ #[inline(always)]
509
+ #[cfg_attr(
510
+ dylint_lib = "nupur_lints",
511
+ allow(
512
+ raising_call_outside_protect,
513
+ reason = "rb_str_set_len raises for a frozen String or a length past its room; the contract is unfrozen, and 0 is within any"
514
+ )
515
+ )]
516
+ pub unsafe fn str_empty_unfrozen(str: Value) {
517
+ // SAFETY: the contract.
518
+ unsafe { rb_sys::rb_str_set_len(str.as_raw(), 0) }
519
+ }
520
+
521
+ /// `rb_gc_mark`, from a TypedData type's mark callback.
522
+ ///
523
+ /// # Safety
524
+ ///
525
+ /// Called from a `dmark` callback in Ruby's mark phase, on one of the
526
+ /// object's slots (a live value, or `Qfalse`, which a mark ignores), with
527
+ /// nothing on the frames above it that a `longjmp` would leak.
528
+ #[inline(always)]
529
+ #[cfg_attr(
530
+ dylint_lib = "nupur_lints",
531
+ allow(
532
+ raising_call_outside_protect,
533
+ reason = "rb_gc_mark raises NoMemoryError only when the mark stack cannot grow, a longjmp the contract's frame loses nothing to and every C extension's dmark is exposed to alike"
534
+ )
535
+ )]
536
+ pub unsafe fn gc_mark_in_dmark(root: Root) {
537
+ // SAFETY: the contract.
538
+ unsafe { rb_sys::rb_gc_mark(root.as_raw()) }
539
+ }
540
+
541
+ /// `rb_gc_adjust_memory_usage(-n)`, from a TypedData type's free callback:
542
+ /// give back the `n` bytes the object had reported, as Ruby's own `xfree`
543
+ /// does from there. Takes no token: the callback is an entry Ruby calls,
544
+ /// and its contract is what proves the GVL.
545
+ ///
546
+ /// # Safety
547
+ ///
548
+ /// Called from a `dfree` callback, which the GC runs on a Ruby thread
549
+ /// holding the GVL.
550
+ #[inline(always)]
551
+ #[cfg_attr(
552
+ dylint_lib = "nupur_lints",
553
+ allow(ffi_without_gvl_proof, reason = "the dfree contract is the proof")
554
+ )]
555
+ pub unsafe fn gc_give_back_in_dfree(n: ssize_t) {
556
+ // SAFETY: the contract; a counter adjustment that only subtracts.
557
+ unsafe { rb_sys::rb_gc_adjust_memory_usage(n.saturating_neg()) }
558
+ }
559
+
560
+ /// `ruby_xfree`, from a TypedData type's free callback: free the data
561
+ /// `rb_data_typed_object_zalloc`'s `xcalloc` made.
562
+ ///
563
+ /// # Safety
564
+ ///
565
+ /// Called from a `dfree` callback (as [`gc_give_back_in_dfree`]), on that
566
+ /// object's data, once.
567
+ #[inline(always)]
568
+ #[cfg_attr(
569
+ dylint_lib = "nupur_lints",
570
+ allow(ffi_without_gvl_proof, reason = "the dfree contract is the proof")
571
+ )]
572
+ pub unsafe fn xfree_in_dfree(ptr: *mut c_void) {
573
+ // SAFETY: the contract.
574
+ unsafe { rb_sys::ruby_xfree(ptr) }
575
+ }
576
+
577
+ /// The running Ruby's API `MAJOR.MINOR`, read from the `ruby_api_version`
578
+ /// it exports (three ints, whose layout no Ruby changes). Needs no proof:
579
+ /// libruby is loaded before any of this runs.
580
+ #[inline]
581
+ pub fn running_api_version() -> (i64, i64) {
582
+ // SAFETY: an immutable `const int[3]` libruby exports, read by value.
583
+ let (major, minor) = unsafe { (rb_sys::ruby_api_version[0], rb_sys::ruby_api_version[1]) };
584
+ (i64::from(major), i64::from(minor))
585
+ }