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.
Files changed (113) 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/version.rb +1 -1
  43. data/lib/lexer_kit.rb +15 -2
  44. data/vendor/nupur/Cargo.toml +24 -0
  45. data/vendor/nupur/LICENSE +176 -0
  46. data/vendor/nupur/NOTICE +64 -0
  47. data/vendor/nupur/README.ja.md +150 -0
  48. data/vendor/nupur/README.md +257 -0
  49. data/vendor/nupur/src/abi.rs +143 -0
  50. data/vendor/nupur/src/args.rs +318 -0
  51. data/vendor/nupur/src/build.rs +101 -0
  52. data/vendor/nupur/src/caught.rs +171 -0
  53. data/vendor/nupur/src/cname.rs +37 -0
  54. data/vendor/nupur/src/describe.rs +175 -0
  55. data/vendor/nupur/src/encoding.rs +240 -0
  56. data/vendor/nupur/src/gvl.rs +181 -0
  57. data/vendor/nupur/src/hash.rs +52 -0
  58. data/vendor/nupur/src/inject.rs +20 -0
  59. data/vendor/nupur/src/lib.rs +280 -0
  60. data/vendor/nupur/src/lint_probe.rs +142 -0
  61. data/vendor/nupur/src/msg.rs +193 -0
  62. data/vendor/nupur/src/no_unwind.rs +39 -0
  63. data/vendor/nupur/src/raw/check.rs +71 -0
  64. data/vendor/nupur/src/raw/data.rs +28 -0
  65. data/vendor/nupur/src/raw/define.rs +215 -0
  66. data/vendor/nupur/src/raw/exception.rs +69 -0
  67. data/vendor/nupur/src/raw/iterate.rs +115 -0
  68. data/vendor/nupur/src/raw/mod.rs +105 -0
  69. data/vendor/nupur/src/raw/protect.rs +144 -0
  70. data/vendor/nupur/src/raw/string.rs +148 -0
  71. data/vendor/nupur/src/raw/symbol.rs +38 -0
  72. data/vendor/nupur/src/raw/values.rs +293 -0
  73. data/vendor/nupur/src/rb/call.rs +118 -0
  74. data/vendor/nupur/src/rb/class.rs +170 -0
  75. data/vendor/nupur/src/rb/convert.rs +134 -0
  76. data/vendor/nupur/src/rb/error.rs +355 -0
  77. data/vendor/nupur/src/rb/global.rs +141 -0
  78. data/vendor/nupur/src/rb/kept.rs +55 -0
  79. data/vendor/nupur/src/rb/method/frame.rs +81 -0
  80. data/vendor/nupur/src/rb/method.rs +468 -0
  81. data/vendor/nupur/src/rb/mod.rs +40 -0
  82. data/vendor/nupur/src/rb/object.rs +66 -0
  83. data/vendor/nupur/src/rb/ruby.rs +107 -0
  84. data/vendor/nupur/src/rb/value/header.rs +65 -0
  85. data/vendor/nupur/src/rb/value/id.rs +61 -0
  86. data/vendor/nupur/src/rb/value/integer.rs +132 -0
  87. data/vendor/nupur/src/rb/value.rs +454 -0
  88. data/vendor/nupur/src/stack.rs +32 -0
  89. data/vendor/nupur/src/string/make.rs +234 -0
  90. data/vendor/nupur/src/string/mod.rs +24 -0
  91. data/vendor/nupur/src/string/read.rs +411 -0
  92. data/vendor/nupur/src/table.rs +58 -0
  93. data/vendor/nupur/src/typed/callbacks.rs +125 -0
  94. data/vendor/nupur/src/typed/data_type.rs +132 -0
  95. data/vendor/nupur/src/typed/embedded.rs +103 -0
  96. data/vendor/nupur/src/typed/external.rs +76 -0
  97. data/vendor/nupur/src/typed/hidden.rs +95 -0
  98. data/vendor/nupur/src/typed/mod.rs +373 -0
  99. data/vendor/nupur/src/utf8_copy.rs +262 -0
  100. data/vendor/nupur/sys/Cargo.toml +29 -0
  101. data/vendor/nupur/sys/src/data_type.rs +213 -0
  102. data/vendor/nupur/sys/src/int.rs +248 -0
  103. data/vendor/nupur/sys/src/lib.rs +61 -0
  104. data/vendor/nupur/sys/src/lint_probe.rs +93 -0
  105. data/vendor/nupur/sys/src/no_unwind.rs +99 -0
  106. data/vendor/nupur/sys/src/non_raising.rs +585 -0
  107. data/vendor/nupur/sys/src/protect.rs +349 -0
  108. data/vendor/nupur/sys/src/raising.rs +257 -0
  109. data/vendor/nupur/sys/src/value.rs +279 -0
  110. data/vendor/nupur.sha256 +68 -0
  111. metadata +92 -12
  112. data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
  113. data/ext/lexer_kit_rust/src/dfa.rs +0 -217
@@ -0,0 +1,454 @@
1
+ //! Ruby's values, as types: [`Value`] and the class-checked handles over it.
2
+ //!
3
+ //! Each is a `VALUE` and nothing else - `Copy`, the size of a pointer, and
4
+ //! neither `Send` nor `Sync`, since a Ruby value is only good on a Ruby thread
5
+ //! holding the GVL. A handle (`RString`, `RHash`, ...) is made only by its
6
+ //! `from_value`, which reads the value's type from its bits and header and so
7
+ //! runs no Ruby and raises nothing; a method that could do either goes through
8
+ //! `raw`'s protected calls.
9
+ //!
10
+ //! Like any Ruby value in an extension, one is good while the GC can find it:
11
+ //! on the machine stack (the conservative scan), or in a wrapped object's slot
12
+ //! (`typed::TypedType`'s `roots`). `rake dylint`'s `ruby_value_on_heap` reports one
13
+ //! kept anywhere else, by the [`ReprValue`] these implement.
14
+
15
+ #![allow(unsafe_code)]
16
+
17
+ use nupur_sys::non_raising;
18
+ use nupur_sys::ruby_value_type;
19
+
20
+ use super::error::Error;
21
+ use super::object::{class_inherits, obj_is_kind_of};
22
+
23
+ // What a String's header says of it: its coderange and encoding index.
24
+ mod header;
25
+ pub(crate) use header::Coderange;
26
+ // An Integer as a Rust number or as decimal digits.
27
+ mod integer;
28
+ // Interned names: a method's, a constant's, a hidden ivar's.
29
+ mod id;
30
+ pub use id::{HiddenId, Id};
31
+
32
+ /// Any Ruby value.
33
+ #[derive(Clone, Copy)]
34
+ #[repr(transparent)]
35
+ ///
36
+ /// `nupur_sys`'s [`nupur_sys::value::Value`] underneath, which is the proof
37
+ /// of the GVL its calls take: a value exists only where Ruby runs.
38
+ pub struct Value(pub(crate) nupur_sys::value::Value);
39
+
40
+ impl Value {
41
+ /// The value a `nupur_sys` call returned, which is one already.
42
+ #[inline(always)]
43
+ pub(crate) const fn from_sys(v: nupur_sys::value::Value) -> Value {
44
+ Value(v)
45
+ }
46
+ }
47
+
48
+ /// What every Ruby value type is: a `Value` underneath.
49
+ ///
50
+ /// The lint `ruby_value_on_heap` finds Ruby values by this trait, so every
51
+ /// type that holds one directly implements it.
52
+ pub trait ReprValue: Copy {
53
+ /// The plain value.
54
+ fn as_value(self) -> Value;
55
+
56
+ /// `nil`?
57
+ #[inline]
58
+ fn is_nil(self) -> bool {
59
+ self.as_value().0.is_nil()
60
+ }
61
+
62
+ /// Ruby's truthiness: anything but `nil` and `false`.
63
+ #[inline]
64
+ fn to_bool(self) -> bool {
65
+ non_raising::rb_test(self.as_value().0)
66
+ }
67
+
68
+ /// Frozen? An immediate (a Fixnum, a Symbol, `nil`) is always frozen.
69
+ #[inline]
70
+ fn is_frozen(self) -> bool {
71
+ let v = self.as_value().0;
72
+ // SAFETY: `v` is a live value; a non-immediate has a header to read.
73
+ non_raising::special_const_p(v) || unsafe { non_raising::frozen_p(v) }
74
+ }
75
+
76
+ /// Whether this value's class is `class` itself, as its header holds
77
+ /// it - what `RBASIC_CLASS(v) == class` reads in C. An instance of a
78
+ /// subclass is not, nor one with a singleton class (a `def s.to_json`
79
+ /// makes one), nor an immediate (a Fixnum, a Symbol, a flonum, `nil`),
80
+ /// which has no header. No method is called.
81
+ #[inline]
82
+ fn has_class(self, class: RClass) -> bool {
83
+ let v = self.as_value().0;
84
+ // SAFETY: `v` is a live value; a non-immediate has a header to read.
85
+ !non_raising::special_const_p(v)
86
+ && unsafe { non_raising::rbasic_klass(v) }.is(class.as_value().0)
87
+ }
88
+
89
+ /// `Ok` unfrozen, and Ruby's own `FrozenError` frozen - the message
90
+ /// naming the receiver, `#receiver` set - returned, not raised. The
91
+ /// normal case, an unfrozen value, makes no call.
92
+ #[inline]
93
+ fn check_frozen(self) -> Result<(), Error> {
94
+ crate::raw::check_frozen(self.as_value())
95
+ }
96
+
97
+ /// An instance of `class` or of a class deriving from it? Reads the
98
+ /// ancestry, runs no Ruby (`rb_obj_is_kind_of`).
99
+ #[inline]
100
+ fn is_kind_of(self, class: impl ReprValue) -> bool {
101
+ obj_is_kind_of(self.as_value(), class.as_value())
102
+ }
103
+
104
+ /// The name of the value's class, for a message, written into `f` -
105
+ /// "Object" when the name cannot be made (an anonymous class's is a fresh
106
+ /// String, and so an allocation that can fail).
107
+ fn write_classname(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
108
+ let v = self.as_value();
109
+ crate::describe::for_message(|| {
110
+ crate::raw::with_classname(v, |name| {
111
+ core::fmt::Display::fmt(&crate::msg::Lossy(name), &mut *f)
112
+ })
113
+ })
114
+ .unwrap_or_else(|| f.write_str("Object"))
115
+ }
116
+ }
117
+
118
+ impl ReprValue for Value {
119
+ #[inline]
120
+ fn as_value(self) -> Value {
121
+ self
122
+ }
123
+ }
124
+
125
+ /// `nupur_sys`'s value underneath a typed one, for its calls and nothing
126
+ /// else.
127
+ pub(crate) trait AsSys {
128
+ /// `nupur_sys`'s value, for its calls: the proof of the GVL they take.
129
+ #[allow(clippy::wrong_self_convention, reason = "a Copy value, read by value")]
130
+ fn as_sys(self) -> nupur_sys::value::Value;
131
+ }
132
+
133
+ impl<T: ReprValue> AsSys for T {
134
+ #[inline]
135
+ fn as_sys(self) -> nupur_sys::value::Value {
136
+ self.as_value().0
137
+ }
138
+ }
139
+
140
+ /// The value of a type `rb_type` names.
141
+ #[inline]
142
+ fn of_type(v: Value, t: ruby_value_type) -> bool {
143
+ non_raising::rb_type(v.0) == t
144
+ }
145
+
146
+ macro_rules! handle {
147
+ ($(#[$doc:meta])* $name:ident, $test:expr) => {
148
+ $(#[$doc])*
149
+ #[derive(Clone, Copy)]
150
+ #[repr(transparent)]
151
+ pub struct $name(Value);
152
+
153
+ impl $name {
154
+ /// `v` as this type, or `None` when it is something else.
155
+ #[inline]
156
+ pub fn from_value(v: Value) -> Option<$name> {
157
+ let test: fn(Value) -> bool = $test;
158
+ test(v).then_some($name(v))
159
+ }
160
+
161
+ /// `v`, which the caller knows to be one: a value this layer
162
+ /// made as one, or a Ruby global of that kind.
163
+ #[inline]
164
+ #[allow(dead_code)]
165
+ pub(crate) fn from_known(v: Value) -> $name {
166
+ debug_assert!({
167
+ let test: fn(Value) -> bool = $test;
168
+ test(v)
169
+ });
170
+ $name(v)
171
+ }
172
+ }
173
+
174
+ impl ReprValue for $name {
175
+ #[inline]
176
+ fn as_value(self) -> Value {
177
+ self.0
178
+ }
179
+ }
180
+ };
181
+ }
182
+
183
+ handle!(
184
+ /// A String.
185
+ RString,
186
+ |v| of_type(v, ruby_value_type::RUBY_T_STRING)
187
+ );
188
+ handle!(
189
+ /// An Array.
190
+ RArray,
191
+ |v| of_type(v, ruby_value_type::RUBY_T_ARRAY)
192
+ );
193
+ handle!(
194
+ /// A Hash.
195
+ RHash,
196
+ |v| of_type(v, ruby_value_type::RUBY_T_HASH)
197
+ );
198
+ handle!(
199
+ /// A Symbol, static or dynamic.
200
+ Symbol,
201
+ |v| non_raising::symbol_p(v.0)
202
+ );
203
+ handle!(
204
+ /// An Integer: a Fixnum or a Bignum.
205
+ Integer,
206
+ |v| non_raising::integer_type_p(v.0)
207
+ );
208
+ handle!(
209
+ /// A Float.
210
+ Float,
211
+ |v| non_raising::float_type_p(v.0)
212
+ );
213
+ handle!(
214
+ /// A Range (or an instance of a subclass).
215
+ Range,
216
+ |v| v.is_kind_of(RClass::from_known(Value::from_sys(non_raising::rb_cRange(
217
+ v.0.ruby()
218
+ ))))
219
+ );
220
+ handle!(
221
+ /// A Class.
222
+ RClass,
223
+ |v| of_type(v, ruby_value_type::RUBY_T_CLASS)
224
+ );
225
+ handle!(
226
+ /// A Module that is not a Class.
227
+ RModule,
228
+ |v| of_type(v, ruby_value_type::RUBY_T_MODULE)
229
+ );
230
+ handle!(
231
+ /// A Class deriving from `Exception`, for raising.
232
+ ExceptionClass,
233
+ /* `rb_eException` read directly, not through `Ruby::exception_exception`:
234
+ * that is an `ExceptionClass::from_known`, whose debug assertion is this
235
+ * test, so a debug build would recurse without end. */
236
+ |v| of_type(v, ruby_value_type::RUBY_T_CLASS)
237
+ && class_inherits(v, Value::from_sys(non_raising::rb_eException(v.0.ruby())))
238
+ );
239
+
240
+ impl RString {
241
+ /// The length in bytes.
242
+ #[inline]
243
+ pub fn len(self) -> usize {
244
+ // SAFETY: `self` is a live String.
245
+ unsafe { non_raising::rstring_len(self.0 .0) }
246
+ }
247
+
248
+ /// Empty?
249
+ #[inline]
250
+ pub fn is_empty(self) -> bool {
251
+ self.len() == 0
252
+ }
253
+
254
+ /// The bytes, borrowed from the String.
255
+ ///
256
+ /// # Safety
257
+ /// The slice is good only while nothing can move, grow, shrink or free
258
+ /// the String's buffer: no Ruby code and no allocation may run while it
259
+ /// is held.
260
+ #[inline]
261
+ pub(crate) unsafe fn as_slice(&self) -> &[u8] {
262
+ // SAFETY: `self` is a live String; the caller's contract.
263
+ unsafe {
264
+ let (p, n) = (
265
+ non_raising::rstring_ptr(self.0 .0),
266
+ non_raising::rstring_len(self.0 .0),
267
+ );
268
+ if n == 0 || p.is_null() {
269
+ return &[];
270
+ }
271
+ core::slice::from_raw_parts(p as *const u8, n)
272
+ }
273
+ }
274
+ }
275
+
276
+ impl RArray {
277
+ /// The length.
278
+ #[inline]
279
+ pub fn len(self) -> usize {
280
+ // SAFETY: `self` is a live Array.
281
+ unsafe { non_raising::rarray_len(self.0 .0) }
282
+ }
283
+
284
+ /// Empty?
285
+ #[inline]
286
+ pub fn is_empty(self) -> bool {
287
+ self.len() == 0
288
+ }
289
+
290
+ /// Element `i`, `nil` past the end: `rb_ary_entry`, which raises
291
+ /// nothing.
292
+ #[inline]
293
+ pub fn entry(self, i: usize) -> Value {
294
+ ary_entry(self, i)
295
+ }
296
+
297
+ /// The elements, read one at a time (`rb_ary_entry`), the length read
298
+ /// afresh before each.
299
+ pub fn each(self) -> impl Iterator<Item = Value> {
300
+ (0..).map_while(move |i: usize| (i < self.len()).then(|| ary_entry(self, i)))
301
+ }
302
+ }
303
+
304
+ impl RHash {
305
+ /// The number of pairs.
306
+ #[inline]
307
+ pub fn len(self) -> usize {
308
+ // SAFETY: `self` is a live Hash.
309
+ unsafe { non_raising::rhash_size(self.0 .0) }
310
+ }
311
+
312
+ /// Empty?
313
+ #[inline]
314
+ pub fn is_empty(self) -> bool {
315
+ self.len() == 0
316
+ }
317
+
318
+ /// The value at `key`, or `None` when there is none. A key is a Symbol
319
+ /// or a String ([`HashKey`]), which Ruby hashes and compares in C; a key
320
+ /// of the Hash's own of another kind that collides with it is compared
321
+ /// by calling `key.eql?` - Symbol's or String's own, Ruby code only
322
+ /// where a program has redefined it - which can raise: `Err`, passed on
323
+ /// (`raw::hash_lookup`).
324
+ #[inline]
325
+ pub fn get(self, key: impl HashKey) -> Result<Option<Value>, Error> {
326
+ crate::raw::hash_lookup(self, key.as_value())
327
+ }
328
+ }
329
+
330
+ /// A Hash key Ruby hashes in C, and compares in C with a key of its own
331
+ /// kind. A String is one only when its class is String itself
332
+ /// (`plain_key`): Ruby takes the C path for two Strings only then, and
333
+ /// calls `eql?` - the caller's Ruby - for a subclass's instance, so the
334
+ /// lookup refuses one. A stored key of another kind whose hash collides is
335
+ /// still compared by calling the lookup key's `eql?` ([`RHash::get`]), which
336
+ /// is why the lookup returns `Result`.
337
+ pub trait HashKey: ReprValue {}
338
+ impl HashKey for Symbol {}
339
+ impl HashKey for RString {}
340
+
341
+ /// Whether `k` is a key Ruby hashes and compares in C: a Symbol, or a String
342
+ /// whose class is String itself. Reads the header; runs no Ruby.
343
+ #[inline]
344
+ pub(crate) fn plain_key(k: Value) -> bool {
345
+ if Symbol::from_value(k).is_some() {
346
+ return true;
347
+ }
348
+ let string = RClass::from_known(Value::from_sys(non_raising::rb_cString(k.0.ruby())));
349
+ RString::from_value(k).is_some_and(|s| s.has_class(string))
350
+ }
351
+
352
+ impl Range {
353
+ /// The first end, the last end and whether the last is excluded.
354
+ #[inline]
355
+ pub fn values(self) -> Result<(Value, Value, bool), Error> {
356
+ crate::raw::range_values(self)
357
+ }
358
+
359
+ /// The part of a collection of `count` elements this Range selects, as
360
+ /// `Array#[]` reads one (`rb_range_beg_len`): the start and the length,
361
+ /// `None` for a start outside the collection, `RangeError` for a bound
362
+ /// too large, and a `TypeError` for an end that is not an Integer or
363
+ /// nil - whose `to_int` the C would run.
364
+ #[inline]
365
+ pub fn beg_len(self, count: usize) -> Result<Option<(usize, usize)>, Error> {
366
+ crate::raw::range_beg_len(self, count)
367
+ }
368
+ }
369
+
370
+ impl Symbol {
371
+ /// The Symbol's name, as Ruby holds it (`rb_sym2str`): a frozen String
372
+ /// in the Symbol's own encoding.
373
+ #[inline]
374
+ pub fn name(self) -> Result<RString, Error> {
375
+ crate::raw::sym2str(self)
376
+ }
377
+ }
378
+
379
+ impl Float {
380
+ /// As an `f64`.
381
+ #[inline]
382
+ pub fn to_f64(self) -> f64 {
383
+ // SAFETY: `self` is a live Float.
384
+ unsafe { non_raising::num2dbl(self.0 .0) }
385
+ }
386
+ }
387
+
388
+ /* ---- a value's kind, read once ---- */
389
+
390
+ /// What a value is, for a reader that dispatches on it: one read of its
391
+ /// type (`rb_type`), and the handle for it, rather than a type test per
392
+ /// kind. A Bignum is told from a Fixnum ([`Kind::Bignum`], [`Kind::Fixnum`]);
393
+ /// a heap Float and a flonum are not - both are [`Kind::Float`].
394
+ #[derive(Clone, Copy)]
395
+ pub enum Kind {
396
+ Nil,
397
+ True,
398
+ False,
399
+ /// An Integer that is an immediate.
400
+ Fixnum(Integer),
401
+ /// An Integer on the heap.
402
+ Bignum(Integer),
403
+ Float(Float),
404
+ String(RString),
405
+ Symbol(Symbol),
406
+ Array(RArray),
407
+ Hash(RHash),
408
+ /// Anything else: an object, a class, a Range, a Struct, ...
409
+ Other,
410
+ }
411
+
412
+ impl Value {
413
+ /// This value's [`Kind`]: one read of its type, no call.
414
+ #[inline]
415
+ pub fn kind(self) -> Kind {
416
+ use ruby_value_type as T;
417
+ match non_raising::rb_type(self.0) {
418
+ T::RUBY_T_NIL => Kind::Nil,
419
+ T::RUBY_T_TRUE => Kind::True,
420
+ T::RUBY_T_FALSE => Kind::False,
421
+ T::RUBY_T_FIXNUM => Kind::Fixnum(Integer(self)),
422
+ T::RUBY_T_BIGNUM => Kind::Bignum(Integer(self)),
423
+ T::RUBY_T_FLOAT => Kind::Float(Float(self)),
424
+ T::RUBY_T_STRING => Kind::String(RString(self)),
425
+ T::RUBY_T_SYMBOL => Kind::Symbol(Symbol(self)),
426
+ T::RUBY_T_ARRAY => Kind::Array(RArray(self)),
427
+ T::RUBY_T_HASH => Kind::Hash(RHash(self)),
428
+ _ => Kind::Other,
429
+ }
430
+ }
431
+ }
432
+
433
+ /* ---- immediates and identity ---- */
434
+
435
+ /// VALUE identity, for the several sites that compare two references.
436
+ #[inline]
437
+ pub fn same_value(a: impl ReprValue, b: impl ReprValue) -> bool {
438
+ a.as_sys().is(b.as_sys())
439
+ }
440
+
441
+ /* ---- a C read that raises nothing (so it is not `raw`'s, which holds the
442
+ * calls that can) ---- */
443
+
444
+ /// `rb_ary_entry`: element `i`, `nil` past the end. Raises nothing.
445
+ #[inline]
446
+ pub(crate) fn ary_entry(a: RArray, i: usize) -> Value {
447
+ /* Converted, not cut: a `long` is 32 bits on Windows, where an index
448
+ * past one would wrap to an element that is there. No Array has one. */
449
+ let Ok(i) = i.try_into() else {
450
+ return Value::from_sys(a.as_sys().ruby().qnil());
451
+ };
452
+ // SAFETY: `a` is a live Array; the read is bounds-checked by Ruby.
453
+ Value::from_sys(unsafe { non_raising::rb_ary_entry(a.as_sys(), i) })
454
+ }
@@ -0,0 +1,32 @@
1
+ //! The native-stack check an engine's recursion counters ask
2
+ //! (`ruby_stack_check`), handed out for a token.
3
+
4
+ #![forbid(unsafe_code)]
5
+
6
+ use crate::rb::Ruby;
7
+
8
+ /// The check "is the current thread's (or Fiber's) Ruby-managed machine stack
9
+ /// close to exhausted?" (`ruby_stack_check`), as a plain function pointer for
10
+ /// an engine that knows nothing of Ruby to ask from its recursion counters.
11
+ ///
12
+ /// Handed out only for a token. The function reads the calling thread's own
13
+ /// stack bounds, so it may be asked from a GVL-released body too (Makiri's
14
+ /// XML parse asks it from its DTD content-model check); a thread Ruby does
15
+ /// not run answers "room left".
16
+ pub fn stack_probe(_: &Ruby) -> fn() -> bool {
17
+ stack_low
18
+ }
19
+
20
+ #[cfg_attr(
21
+ dylint_lib = "nupur_lints",
22
+ allow(
23
+ ffi_without_gvl_proof,
24
+ reason = "private: handed out only by `stack_probe`, for a token"
25
+ )
26
+ )]
27
+ fn stack_low() -> bool {
28
+ /* The pointer outlives the token it was handed out for, so a call can
29
+ * come from a thread Ruby does not run: `ruby_stack_low` answers "room
30
+ * left" there. */
31
+ nupur_sys::non_raising::ruby_stack_low()
32
+ }