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,71 @@
|
|
|
1
|
+
//! Conversions and the checks a C function would make and does not.
|
|
2
|
+
|
|
3
|
+
use super::*;
|
|
4
|
+
|
|
5
|
+
/// `rb_sym2str`: raises `NoMemoryError` when a static Symbol's name String is
|
|
6
|
+
/// made for the first time. Runs no Ruby code.
|
|
7
|
+
#[inline]
|
|
8
|
+
pub(crate) fn sym2str(sym: Symbol) -> Result<RString, Error> {
|
|
9
|
+
let v = sym.as_sys();
|
|
10
|
+
let s = call_allocating(|inside| unsafe { nupur_sys::raising::rb_sym2str(inside, v) })?;
|
|
11
|
+
typed(s, RString::from_value, "a C call made no String")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/// `rb_check_frozen` returning its `FrozenError` rather than raising it.
|
|
15
|
+
///
|
|
16
|
+
/// The error is Ruby's own - the message naming the receiver and `#receiver`
|
|
17
|
+
/// set - because the check still runs, protected. An unfrozen value, every
|
|
18
|
+
/// mutator's normal case, makes no call.
|
|
19
|
+
#[inline]
|
|
20
|
+
pub(crate) fn check_frozen(v: Value) -> Result<(), Error> {
|
|
21
|
+
if !crate::rb::ReprValue::is_frozen(v) {
|
|
22
|
+
return Ok(());
|
|
23
|
+
}
|
|
24
|
+
let v = v.as_sys();
|
|
25
|
+
call(|inside| unsafe { nupur_sys::raising::rb_check_frozen(inside, v) })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// `rb_range_beg_len` with `err = 0` over a collection of `count` elements:
|
|
29
|
+
/// the start and the length it selects, `None` for a start outside it, and
|
|
30
|
+
/// `RangeError` for a bound too large for a `long`. A Range whose ends are
|
|
31
|
+
/// not Integers or nil is a `TypeError`, checked here: the C would convert
|
|
32
|
+
/// such an end with its `to_int`.
|
|
33
|
+
#[inline]
|
|
34
|
+
pub(crate) fn range_beg_len(r: Range, count: usize) -> Result<Option<(usize, usize)>, Error> {
|
|
35
|
+
/* Checked here, not left to the caller: an end of any other kind would
|
|
36
|
+
* have its `to_int` run by the C, the caller's Ruby. */
|
|
37
|
+
let (beg, end, _) = range_values(r)?;
|
|
38
|
+
if [beg, end]
|
|
39
|
+
.iter()
|
|
40
|
+
.any(|&e| !crate::rb::ReprValue::is_nil(e) && Integer::from_value(e).is_none())
|
|
41
|
+
{
|
|
42
|
+
return Err(crate::rb::error::type_error(
|
|
43
|
+
"a Range whose ends are Integers (or nil) was expected",
|
|
44
|
+
));
|
|
45
|
+
}
|
|
46
|
+
let count = count
|
|
47
|
+
.try_into()
|
|
48
|
+
.map_err(|_| crate::rb::error::arg_error("a collection too large for a long"))?;
|
|
49
|
+
let (mut beg, mut len) = (0, 0);
|
|
50
|
+
let (b, l) = (core::ptr::addr_of_mut!(beg), core::ptr::addr_of_mut!(len));
|
|
51
|
+
let r = r.as_sys();
|
|
52
|
+
let answer =
|
|
53
|
+
call(|inside| unsafe { nupur_sys::raising::rb_range_beg_len(inside, r, b, l, count, 0) })?;
|
|
54
|
+
if !answer.is_true() {
|
|
55
|
+
return Ok(None);
|
|
56
|
+
}
|
|
57
|
+
/* Within `0..=count` both, when the C answers true. */
|
|
58
|
+
match (usize::try_from(beg), usize::try_from(len)) {
|
|
59
|
+
(Ok(beg), Ok(len)) => Ok(Some((beg, len))),
|
|
60
|
+
_ => Err(crate::rb::error::internal_error(
|
|
61
|
+
"rb_range_beg_len answered a negative bound",
|
|
62
|
+
)),
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// `rb_thread_check_ints`: raises a pending interrupt (`Interrupt`,
|
|
67
|
+
/// `Thread#raise`'s exception, a `Timeout`), or ends the thread.
|
|
68
|
+
#[inline]
|
|
69
|
+
pub(crate) fn thread_check_ints(_: &Ruby) -> Result<(), Error> {
|
|
70
|
+
call(|inside| unsafe { nupur_sys::raising::rb_thread_check_ints(inside) })
|
|
71
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//! Typed data and Ruby's allocator.
|
|
2
|
+
|
|
3
|
+
use super::*;
|
|
4
|
+
use nupur_sys::data_type::DataType;
|
|
5
|
+
|
|
6
|
+
/// `rb_check_typeddata`: raises `TypeError` for a value that is not of `ty`.
|
|
7
|
+
#[inline]
|
|
8
|
+
pub(crate) unsafe fn check_typeddata(
|
|
9
|
+
v: nupur_sys::value::Value,
|
|
10
|
+
ty: &'static DataType,
|
|
11
|
+
) -> Result<*mut c_void, Error> {
|
|
12
|
+
call(|inside| unsafe { nupur_sys::raising::rb_check_typeddata(inside, v, ty) })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/// `rb_data_typed_object_zalloc`: raises `NoMemoryError`. For an embeddable
|
|
16
|
+
/// type small enough, the zeroed data is the object's own slot.
|
|
17
|
+
#[inline]
|
|
18
|
+
pub(crate) unsafe fn typed_object_zalloc(
|
|
19
|
+
klass: nupur_sys::value::Value,
|
|
20
|
+
size: usize,
|
|
21
|
+
ty: &'static DataType,
|
|
22
|
+
) -> Result<nupur_sys::value::Value, Error> {
|
|
23
|
+
let size = nupur_sys::size_t::try_from(size)
|
|
24
|
+
.map_err(|_| crate::rb::error::internal_error("a wrapped object too large"))?;
|
|
25
|
+
call_allocating(|inside| unsafe {
|
|
26
|
+
nupur_sys::raising::rb_data_typed_object_zalloc(inside, klass, size, ty)
|
|
27
|
+
})
|
|
28
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
//! Class, module and method definitions, and the constants they hang from, at `Init_`.
|
|
2
|
+
|
|
3
|
+
use super::*;
|
|
4
|
+
|
|
5
|
+
/// `rb_define_class_under`, its superclass `sup`: raises `TypeError` when
|
|
6
|
+
/// `name` is bound to something else, or to a class with another
|
|
7
|
+
/// superclass.
|
|
8
|
+
pub(crate) fn define_class_under(
|
|
9
|
+
outer: Value,
|
|
10
|
+
name: &core::ffi::CStr,
|
|
11
|
+
sup: RClass,
|
|
12
|
+
) -> Result<RClass, Error> {
|
|
13
|
+
let (outer, name, sup) = (outer.as_sys(), name.as_ptr(), sup.as_sys());
|
|
14
|
+
let v = call(|inside| unsafe {
|
|
15
|
+
nupur_sys::raising::rb_define_class_under(inside, outer, name, sup)
|
|
16
|
+
})?;
|
|
17
|
+
typed(v, RClass::from_value, "a C call made no Class")
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// `rb_define_module_under`: raises `TypeError` when `name` is bound to
|
|
21
|
+
/// something that is not a module.
|
|
22
|
+
pub(crate) fn define_module_under(outer: Value, name: &core::ffi::CStr) -> Result<RModule, Error> {
|
|
23
|
+
let (outer, name) = (outer.as_sys(), name.as_ptr());
|
|
24
|
+
let v =
|
|
25
|
+
call(|inside| unsafe { nupur_sys::raising::rb_define_module_under(inside, outer, name) })?;
|
|
26
|
+
typed(v, RModule::from_value, "a C call made no Module")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// `rb_define_module`: a top-level module; raises as `define_module_under`.
|
|
30
|
+
pub(crate) fn define_module(_: &Ruby, name: &core::ffi::CStr) -> Result<RModule, Error> {
|
|
31
|
+
let name = name.as_ptr();
|
|
32
|
+
let v = call(|inside| unsafe { nupur_sys::raising::rb_define_module(inside, name) })?;
|
|
33
|
+
typed(v, RModule::from_value, "a C call made no Module")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// `rb_const_set`: raises `FrozenError` on a frozen namespace.
|
|
37
|
+
pub(crate) fn const_set(
|
|
38
|
+
ruby: &Ruby,
|
|
39
|
+
outer: Value,
|
|
40
|
+
name: &core::ffi::CStr,
|
|
41
|
+
v: Value,
|
|
42
|
+
) -> Result<(), Error> {
|
|
43
|
+
let id = intern_cstr(ruby, name)?.as_raw();
|
|
44
|
+
let (outer, v) = (outer.as_sys(), v.as_sys());
|
|
45
|
+
call(|inside| unsafe { nupur_sys::raising::rb_const_set(inside, outer, id, v) })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// `rb_include_module`: raises `ArgumentError` for a cyclic include,
|
|
49
|
+
/// `FrozenError` on a frozen class.
|
|
50
|
+
pub(crate) fn include_module(klass: Value, module: Value) -> Result<(), Error> {
|
|
51
|
+
let (klass, module) = (klass.as_sys(), module.as_sys());
|
|
52
|
+
call(|inside| unsafe { nupur_sys::raising::rb_include_module(inside, klass, module) })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// `rb_undef_alloc_func`: raises `TypeError` for a singleton class, which
|
|
56
|
+
/// may not have an allocator of its own taken.
|
|
57
|
+
pub(crate) fn undef_alloc_func(klass: RClass) -> Result<(), Error> {
|
|
58
|
+
let k = klass.as_sys();
|
|
59
|
+
call(|inside| unsafe { nupur_sys::raising::rb_undef_alloc_func(inside, k) })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// `rb_obj_freeze`: raises `NoMemoryError` - freezing moves an object with
|
|
63
|
+
/// instance variables to a frozen shape, which may allocate.
|
|
64
|
+
pub(crate) fn obj_freeze(v: Value) -> Result<(), Error> {
|
|
65
|
+
let v = v.as_sys();
|
|
66
|
+
call_allocating(|inside| unsafe { nupur_sys::raising::rb_obj_freeze(inside, v) }).map(|_| ())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// `rb_obj_reveal`: show `obj`, hidden by `rb_obj_hide`, as an instance of
|
|
70
|
+
/// `klass`. Raises `NoMemoryError`: the class is written with a write
|
|
71
|
+
/// barrier, which may grow the remembered set (found by the C API raise
|
|
72
|
+
/// survey, takahashim.github.io/ruby-c-apis - Ruby's header says nothing of
|
|
73
|
+
/// it). On `Err` the object stays hidden.
|
|
74
|
+
pub(crate) fn obj_reveal(obj: Value, klass: Value) -> Result<(), Error> {
|
|
75
|
+
let (o, k) = (obj.as_sys(), klass.as_sys());
|
|
76
|
+
call_allocating(|inside| unsafe { nupur_sys::raising::rb_obj_reveal(inside, o, k) }).map(|_| ())
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// `rb_define_method` / `_private_method` / `_singleton_method`: raise
|
|
80
|
+
/// `FrozenError` on a frozen target.
|
|
81
|
+
pub(crate) fn define_method(
|
|
82
|
+
target: Value,
|
|
83
|
+
name: &core::ffi::CStr,
|
|
84
|
+
func: crate::rb::method::Func,
|
|
85
|
+
visibility: crate::rb::class::Visibility,
|
|
86
|
+
) -> Result<(), Error> {
|
|
87
|
+
use crate::rb::class::Visibility;
|
|
88
|
+
let (target, name, ptr, arity) = (target.as_sys(), name.as_ptr(), Some(func.ptr), func.arity);
|
|
89
|
+
match visibility {
|
|
90
|
+
Visibility::Public => call(|inside| unsafe {
|
|
91
|
+
nupur_sys::raising::rb_define_method(inside, target, name, ptr, arity)
|
|
92
|
+
}),
|
|
93
|
+
Visibility::Private => call(|inside| unsafe {
|
|
94
|
+
nupur_sys::raising::rb_define_private_method(inside, target, name, ptr, arity)
|
|
95
|
+
}),
|
|
96
|
+
Visibility::Singleton => call(|inside| unsafe {
|
|
97
|
+
nupur_sys::raising::rb_define_singleton_method(inside, target, name, ptr, arity)
|
|
98
|
+
}),
|
|
99
|
+
Visibility::ModuleFunction => call(|inside| unsafe {
|
|
100
|
+
nupur_sys::raising::rb_define_module_function(inside, target, name, ptr, arity)
|
|
101
|
+
}),
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// `rb_gc_register_mark_object`: raises `NoMemoryError` when the root table
|
|
106
|
+
/// cannot grow.
|
|
107
|
+
pub(crate) fn gc_register_mark_object(v: Value) -> Result<(), Error> {
|
|
108
|
+
let v = v.as_sys();
|
|
109
|
+
call_allocating(|inside| unsafe { nupur_sys::raising::rb_gc_register_mark_object(inside, v) })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/// The constant `name` of `outer` (`rb_const_get`): raises `NameError` for
|
|
113
|
+
/// none, and an autoload runs Ruby code - for a class the extension's own
|
|
114
|
+
/// Ruby defines.
|
|
115
|
+
pub(crate) fn const_get(ruby: &Ruby, outer: Value, name: &core::ffi::CStr) -> Result<Value, Error> {
|
|
116
|
+
let id = intern_cstr(ruby, name)?.as_raw();
|
|
117
|
+
let outer = outer.as_sys();
|
|
118
|
+
let v = call(|inside| unsafe { nupur_sys::raising::rb_const_get(inside, outer, id) })?;
|
|
119
|
+
Ok(Value::from_sys(v))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Whether `outer` itself holds a constant `id` (`rb_const_defined_at`),
|
|
123
|
+
/// an autoload counting as one; nothing is loaded. Raises nothing in
|
|
124
|
+
/// practice; protected as every C call that is not classified.
|
|
125
|
+
pub(crate) fn const_defined_at(outer: Value, id: crate::rb::Id) -> Result<bool, Error> {
|
|
126
|
+
let (outer, id) = (outer.as_sys(), id.as_raw());
|
|
127
|
+
let r = call(|inside| unsafe { nupur_sys::raising::rb_const_defined_at(inside, outer, id) })?;
|
|
128
|
+
Ok(r != 0)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// Whether `outer`'s constant `id` is an autoload not yet loaded
|
|
132
|
+
/// (`rb_autoload_p`, whose answer is the feature to require or `nil`);
|
|
133
|
+
/// nothing is loaded. Protected as every C call that is not classified.
|
|
134
|
+
pub(crate) fn autoload_pending(outer: Value, id: crate::rb::Id) -> Result<bool, Error> {
|
|
135
|
+
let (outer, id) = (outer.as_sys(), id.as_raw());
|
|
136
|
+
let v = call(|inside| unsafe { nupur_sys::raising::rb_autoload_p(inside, outer, id) })?;
|
|
137
|
+
Ok(!crate::rb::ReprValue::is_nil(Value::from_sys(v)))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// `recv.public_send(id, *args)` (`rb_funcallv_public`): only a public
|
|
141
|
+
/// method, a private or protected one being `NoMethodError`. The caller's
|
|
142
|
+
/// Ruby, as [`call_method`] is.
|
|
143
|
+
#[cfg_attr(
|
|
144
|
+
dylint_lib = "nupur_lints",
|
|
145
|
+
expect(
|
|
146
|
+
runs_callers_ruby,
|
|
147
|
+
reason = "rb_funcallv_public: the wrapper `rb::call::public_method` is, and its callers allow it"
|
|
148
|
+
)
|
|
149
|
+
)]
|
|
150
|
+
pub(crate) fn call_public(recv: Value, id: crate::rb::Id, args: &[Value]) -> Result<Value, Error> {
|
|
151
|
+
let argc = c_len(args.len(), "too many arguments")?;
|
|
152
|
+
let (recv, id) = (recv.as_sys(), id.as_raw());
|
|
153
|
+
/* `Value` is transparent over `VALUE`, so the slice is the C array. */
|
|
154
|
+
let argv = args.as_ptr().cast::<nupur_sys::value::Value>();
|
|
155
|
+
let v = call_allocating(|inside| unsafe {
|
|
156
|
+
nupur_sys::raising::rb_funcallv_public(inside, recv, id, argc, argv)
|
|
157
|
+
})?;
|
|
158
|
+
Ok(Value::from_sys(v))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// Whether `recv` has a public method `id` (`rb_respond_to`): Ruby's own
|
|
162
|
+
/// `respond_to?`, which a class may redefine - the caller's Ruby.
|
|
163
|
+
#[cfg_attr(
|
|
164
|
+
dylint_lib = "nupur_lints",
|
|
165
|
+
expect(
|
|
166
|
+
runs_callers_ruby,
|
|
167
|
+
reason = "rb_respond_to: the wrapper `rb::call::responds_to` is, and its callers allow it"
|
|
168
|
+
)
|
|
169
|
+
)]
|
|
170
|
+
pub(crate) fn respond_to(recv: Value, id: crate::rb::Id) -> Result<bool, Error> {
|
|
171
|
+
let (recv, id) = (recv.as_sys(), id.as_raw());
|
|
172
|
+
let r = call(|inside| unsafe { nupur_sys::raising::rb_respond_to(inside, recv, id) })?;
|
|
173
|
+
Ok(r != 0)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// `rb_ractor_make_shareable`: `v` and everything it reaches frozen and
|
|
177
|
+
/// marked shareable between Ractors. Raises for a value that cannot be
|
|
178
|
+
/// (a Proc with a non-shareable self, an unshareable wrapped object).
|
|
179
|
+
pub(crate) fn ractor_make_shareable(v: Value) -> Result<(), Error> {
|
|
180
|
+
let v = v.as_sys();
|
|
181
|
+
call_allocating(|inside| unsafe { nupur_sys::raising::rb_ractor_make_shareable(inside, v) })
|
|
182
|
+
.map(|_| ())
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// `recv.name(*args)` (`rb_funcallv`): the caller's Ruby, which is why each
|
|
186
|
+
/// call of it is allowed where it is made (`rake dylint`'s
|
|
187
|
+
/// `runs_callers_ruby`), with the reason it may run there.
|
|
188
|
+
#[cfg_attr(
|
|
189
|
+
dylint_lib = "nupur_lints",
|
|
190
|
+
expect(
|
|
191
|
+
runs_callers_ruby,
|
|
192
|
+
reason = "rb_funcallv: the wrapper `Module::call_method` is, and its callers allow it"
|
|
193
|
+
)
|
|
194
|
+
)]
|
|
195
|
+
pub(crate) fn call_method(
|
|
196
|
+
ruby: &Ruby,
|
|
197
|
+
recv: Value,
|
|
198
|
+
name: &core::ffi::CStr,
|
|
199
|
+
args: &[Value],
|
|
200
|
+
) -> Result<Value, Error> {
|
|
201
|
+
let id = intern_cstr(ruby, name)?.as_raw();
|
|
202
|
+
let argc = c_len(args.len(), "too many arguments")?;
|
|
203
|
+
let recv = recv.as_sys();
|
|
204
|
+
/* `Value` is transparent over `VALUE`, so the slice is the C array. */
|
|
205
|
+
let argv = args.as_ptr().cast::<nupur_sys::value::Value>();
|
|
206
|
+
let v =
|
|
207
|
+
call(|inside| unsafe { nupur_sys::raising::rb_funcallv(inside, recv, id, argc, argv) })?;
|
|
208
|
+
Ok(Value::from_sys(v))
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/// `rb_module_new`: an anonymous module. Raises `NoMemoryError`.
|
|
212
|
+
pub(crate) fn module_new(_: &Ruby) -> Result<RModule, Error> {
|
|
213
|
+
let v = call_allocating(|inside| unsafe { nupur_sys::raising::rb_module_new(inside) })?;
|
|
214
|
+
typed(v, RModule::from_value, "a C call made no Module")
|
|
215
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//! Exceptions: making one, the load check that a raise is told from a
|
|
2
|
+
//! `throw`, and a value's class name for a message.
|
|
3
|
+
|
|
4
|
+
use super::*;
|
|
5
|
+
|
|
6
|
+
/// An exception of `class` with the UTF-8 message `msg`: `rb_exc_new_str`,
|
|
7
|
+
/// which allocates and runs the class's `initialize`, and so raises.
|
|
8
|
+
#[inline]
|
|
9
|
+
pub(crate) fn exception_new(
|
|
10
|
+
ruby: &Ruby,
|
|
11
|
+
class: ExceptionClass,
|
|
12
|
+
msg: &[u8],
|
|
13
|
+
) -> Result<Value, Error> {
|
|
14
|
+
let s = utf8_str_new(ruby, msg)?.as_sys();
|
|
15
|
+
let class = class.as_sys();
|
|
16
|
+
let exc =
|
|
17
|
+
call_allocating(|inside| unsafe { nupur_sys::raising::rb_exc_new_str(inside, class, s) })?;
|
|
18
|
+
Ok(Value::from_sys(exc))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Does [`call`] still tell a raise from a `throw` or a `break`? It does by
|
|
22
|
+
/// `rb_protect`'s state, `TAG_RAISE`, a number Ruby does not document
|
|
23
|
+
/// (`protect`'s own doc), so the extension asks once as it loads: a raise is
|
|
24
|
+
/// made under `call` and must come back as an exception. `Ok(false)` is a Ruby
|
|
25
|
+
/// whose numbering this was not written for - the exit it left pending is
|
|
26
|
+
/// forgotten here, and the caller refuses to load.
|
|
27
|
+
pub(crate) fn raise_is_recognised(ruby: &Ruby) -> Result<bool, Error> {
|
|
28
|
+
let exc = exception_new(ruby, ruby.exception_runtime_error(), b"raise self-check")?.as_sys();
|
|
29
|
+
let outcome = call::<(), _>(|inside| unsafe { nupur_sys::raising::rb_exc_raise(inside, exc) });
|
|
30
|
+
let recognised = matches!(
|
|
31
|
+
&outcome,
|
|
32
|
+
Err(e) if e.is_kind_of(ruby, crate::rb::Builtin::RuntimeError)
|
|
33
|
+
);
|
|
34
|
+
if !recognised {
|
|
35
|
+
// SAFETY: the exit is this check's own raise, which nobody resumes:
|
|
36
|
+
// the caller refuses to load.
|
|
37
|
+
unsafe { reset_pending_exit() };
|
|
38
|
+
forget_errinfo(ruby);
|
|
39
|
+
}
|
|
40
|
+
Ok(recognised)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// The longest class name [`with_classname`] hands on; a longer one is cut.
|
|
44
|
+
const CLASSNAME_MAX: usize = 128;
|
|
45
|
+
|
|
46
|
+
/// `rb_obj_classname`: the name of `v`'s class, handed to `f` as bytes. Raises
|
|
47
|
+
/// `NoMemoryError`: an anonymous class's name (`#<Class:0x...>`) is a String
|
|
48
|
+
/// made on each call, referenced by nothing once the call returns - so `f`
|
|
49
|
+
/// is handed a copy, up to [`CLASSNAME_MAX`] bytes, and may do anything.
|
|
50
|
+
pub(crate) fn with_classname<R>(v: Value, f: impl FnOnce(&[u8]) -> R) -> Result<R, Error> {
|
|
51
|
+
let v = v.as_sys();
|
|
52
|
+
let p = call_allocating(|inside| unsafe { nupur_sys::raising::rb_obj_classname(inside, v) })?;
|
|
53
|
+
if p.is_null() {
|
|
54
|
+
return Ok(f(b"Object"));
|
|
55
|
+
}
|
|
56
|
+
/* Copied out at once, before `f` runs: the name may be a temporary
|
|
57
|
+
* String nothing references, which a collection `f` set off could free.
|
|
58
|
+
* A name past the buffer is cut - it is written into a message. */
|
|
59
|
+
let mut buf = [0u8; CLASSNAME_MAX];
|
|
60
|
+
// SAFETY: a NUL-terminated C string owned by the class or by that
|
|
61
|
+
// temporary String, read and copied here with nothing run between.
|
|
62
|
+
let name = unsafe { core::ffi::CStr::from_ptr(p) }.to_bytes();
|
|
63
|
+
/* At a character boundary, so a cut name ends in a whole character. */
|
|
64
|
+
let n = crate::msg::floor_char_boundary(name, CLASSNAME_MAX);
|
|
65
|
+
if let (Some(d), Some(s)) = (buf.get_mut(..n), name.get(..n)) {
|
|
66
|
+
d.copy_from_slice(s);
|
|
67
|
+
}
|
|
68
|
+
Ok(f(buf.get(..n).unwrap_or_default()))
|
|
69
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//! Calling a block and walking a Hash.
|
|
2
|
+
|
|
3
|
+
use super::*;
|
|
4
|
+
|
|
5
|
+
/// `rb_yield`: runs the caller's block - Ruby code, which can raise, `break`,
|
|
6
|
+
/// `throw` or `return`. Every one of those comes back as `Err` (the
|
|
7
|
+
/// non-exception exits as the jump `rb_protect` caught), and is raised again
|
|
8
|
+
/// at the method's return, so it reaches the block's caller as it left.
|
|
9
|
+
#[cfg_attr(
|
|
10
|
+
dylint_lib = "nupur_lints",
|
|
11
|
+
expect(
|
|
12
|
+
runs_callers_ruby,
|
|
13
|
+
reason = "rb_yield: the wrapper `rb::method::yield_value` is, and its callers allow it"
|
|
14
|
+
)
|
|
15
|
+
)]
|
|
16
|
+
pub(crate) fn yield_value(_: &Ruby, v: Value) -> Result<Value, Error> {
|
|
17
|
+
let v = v.as_sys();
|
|
18
|
+
let out = call(|inside| unsafe { nupur_sys::raising::rb_yield(inside, v) })?;
|
|
19
|
+
Ok(Value::from_sys(out))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// `rb_yield_values2`: [`yield_value`] with `vals` as the block's separate
|
|
23
|
+
/// arguments - what `yield a, b, c` passes, where one Array would be a single
|
|
24
|
+
/// argument the block destructures (and a lambda would refuse). Every value
|
|
25
|
+
/// is positional: a trailing Hash is never keywords. The block's exits come
|
|
26
|
+
/// back as `Err`, as [`yield_value`]'s.
|
|
27
|
+
#[cfg_attr(
|
|
28
|
+
dylint_lib = "nupur_lints",
|
|
29
|
+
expect(
|
|
30
|
+
runs_callers_ruby,
|
|
31
|
+
reason = "rb_yield_values2: the wrapper `rb::method::yield_values` is, and its callers allow it"
|
|
32
|
+
)
|
|
33
|
+
)]
|
|
34
|
+
pub(crate) fn yield_values<const N: usize>(_: &Ruby, vals: [Value; N]) -> Result<Value, Error> {
|
|
35
|
+
/* `N` is a constant, so this folds away; the check stays for the rule
|
|
36
|
+
* that a C integer is made only through a checked conversion. */
|
|
37
|
+
let argc = c_len(N, "too many values to yield")?;
|
|
38
|
+
/* `Value` is transparent over `VALUE`, so the array is the C array. It is
|
|
39
|
+
* this frame's own, taken by value, so it is on the stack, where the GC's
|
|
40
|
+
* scan sees the values, until `rb_yield_values2` returns: wherever the
|
|
41
|
+
* caller kept them, the C call never reads them from a heap buffer. */
|
|
42
|
+
let argv = vals.as_ptr().cast::<nupur_sys::value::Value>();
|
|
43
|
+
let out = call(|inside| unsafe { nupur_sys::raising::rb_yield_values2(inside, argc, argv) })?;
|
|
44
|
+
Ok(Value::from_sys(out))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// [`yield_value`], and what the block's exit or exception came back as
|
|
48
|
+
/// DROPPED (`rb::method::yield_dropping_error_for_test`).
|
|
49
|
+
#[doc(hidden)]
|
|
50
|
+
#[cfg_attr(
|
|
51
|
+
dylint_lib = "nupur_lints",
|
|
52
|
+
expect(
|
|
53
|
+
runs_callers_ruby,
|
|
54
|
+
reason = "the test hook's block, which is the point of it"
|
|
55
|
+
)
|
|
56
|
+
)]
|
|
57
|
+
#[cfg_attr(
|
|
58
|
+
dylint_lib = "nupur_lints",
|
|
59
|
+
allow(
|
|
60
|
+
discarded_ruby_error,
|
|
61
|
+
reason = "dropping the block's exit is what the test hook is for"
|
|
62
|
+
)
|
|
63
|
+
)]
|
|
64
|
+
pub(crate) fn yield_dropping_error_for_test(ruby: &Ruby) {
|
|
65
|
+
let _ = yield_value(ruby, ruby.qnil());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// `rb_hash_foreach` over `h`, `visit` answering after each pair whether to
|
|
69
|
+
/// go on. Raises `RuntimeError` when the Hash is changed in a way the walk
|
|
70
|
+
/// cannot survive (a key added or the table rehashed).
|
|
71
|
+
///
|
|
72
|
+
/// `visit` is Rust called from C, inside `rb_hash_foreach`'s frames, so a
|
|
73
|
+
/// panic in it must not unwind into them: it is latched here - the walk
|
|
74
|
+
/// stops - and resumed once the C frames are gone. It never raises either:
|
|
75
|
+
/// it answers `false` and the caller keeps its own error.
|
|
76
|
+
pub(crate) fn hash_foreach(
|
|
77
|
+
h: RHash,
|
|
78
|
+
visit: &mut dyn FnMut(Value, Value) -> bool,
|
|
79
|
+
) -> Result<(), Error> {
|
|
80
|
+
struct Walk<'a> {
|
|
81
|
+
visit: &'a mut dyn FnMut(Value, Value) -> bool,
|
|
82
|
+
latch: crate::caught::PanicLatch,
|
|
83
|
+
}
|
|
84
|
+
unsafe extern "C" fn each(
|
|
85
|
+
k: nupur_sys::value::Value,
|
|
86
|
+
v: nupur_sys::value::Value,
|
|
87
|
+
arg: *mut c_void,
|
|
88
|
+
) -> c_int {
|
|
89
|
+
const CONTINUE: c_int = nupur_sys::st_retval::ST_CONTINUE as c_int;
|
|
90
|
+
const STOP: c_int = nupur_sys::st_retval::ST_STOP as c_int;
|
|
91
|
+
crate::no_unwind::must_not_unwind::<crate::no_unwind::HashEach, _>(|| {
|
|
92
|
+
// SAFETY: `arg` is the `Walk` `hash_foreach` passed, live for the
|
|
93
|
+
// walk; `k` and `v` are the Hash's, live while it is walked.
|
|
94
|
+
let walk = unsafe { &mut *(arg as *mut Walk<'_>) };
|
|
95
|
+
let (k, v) = (Value::from_sys(k), Value::from_sys(v));
|
|
96
|
+
let visit = &mut walk.visit;
|
|
97
|
+
if walk.latch.guard(false, || visit(k, v)) {
|
|
98
|
+
CONTINUE
|
|
99
|
+
} else {
|
|
100
|
+
STOP
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
let mut walk = Walk {
|
|
105
|
+
visit,
|
|
106
|
+
latch: crate::caught::PanicLatch::new(),
|
|
107
|
+
};
|
|
108
|
+
let (h, arg) = (h.as_sys(), (&mut walk as *mut Walk<'_>).cast::<c_void>());
|
|
109
|
+
let walked = call(|inside| unsafe {
|
|
110
|
+
nupur_sys::raising::rb_hash_foreach(inside, h, each, arg);
|
|
111
|
+
});
|
|
112
|
+
/* Out of the C frames: safe to unwind from here. */
|
|
113
|
+
walk.latch.resume();
|
|
114
|
+
walked.map(|_| ())
|
|
115
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//! The Ruby C calls that can RAISE - every `longjmp` this extension can start -
|
|
2
|
+
//! and each one made under `rb_protect`, so the raise comes back as `Err`.
|
|
3
|
+
//!
|
|
4
|
+
//! A raise is `longjmp`, which crosses Rust frames without running their
|
|
5
|
+
//! destructors, and crossing a frame that has one (or a `catch_unwind`) is
|
|
6
|
+
//! undefined behaviour. So no raising call is made anywhere but here, and here
|
|
7
|
+
//! every one runs inside [`call`] ([`protect`]): a bare `rb_protect` whose `extern "C"`
|
|
8
|
+
//! thunk holds nothing but that one C call and the `Copy` values it reads. The
|
|
9
|
+
//! only frame a raise crosses is the thunk's own, which owns nothing. A
|
|
10
|
+
//! caller gets a `Result` and owns whatever it likes across the call. The C
|
|
11
|
+
//! call is `nupur_sys::raising`'s, which takes the `&Protected` that only
|
|
12
|
+
//! `call` lends its closure.
|
|
13
|
+
//! `rake dylint`'s `raising_call_outside_protect` refuses a raising C call
|
|
14
|
+
//! anywhere else, so a new one is a new wrapper here, with its raise stated.
|
|
15
|
+
//!
|
|
16
|
+
//! The value-level wrappers are SAFE: they take and return `rb`'s
|
|
17
|
+
//! typed values (`RString`, `RArray`, `RHash`, `Value`, ...), slices and the
|
|
18
|
+
//! layer's `Encoding`, so a live value of the right kind is the type's
|
|
19
|
+
//! promise, not the caller's - under the rule every Ruby extension keeps, that
|
|
20
|
+
//! a Ruby value stays on the stack (`rake dylint`'s `ruby_value_on_heap`).
|
|
21
|
+
//! One that
|
|
22
|
+
//! takes no Ruby value takes `&Ruby`, the proof it runs on a Ruby thread with
|
|
23
|
+
//! the GVL (`Ruby` is `!Send`, so a GVL-released body cannot carry one). What
|
|
24
|
+
//! still checks a value's kind or a bound is done here, before the C call,
|
|
25
|
+
//! where the C function would not check it. The wrappers over raw memory -
|
|
26
|
+
//! typed data and Ruby's allocator, and a String of unwritten bytes - stay
|
|
27
|
+
//! `unsafe`, with their contracts stated.
|
|
28
|
+
//!
|
|
29
|
+
//! What is here is checked, not kept by hand: which C functions raise is
|
|
30
|
+
//! one table in the lints (`nupur_lints`' `NON_RAISING`, every function
|
|
31
|
+
//! not in it taken to raise), and `rake dylint` fails on a call of Ruby's C
|
|
32
|
+
//! in this module outside [`call`] - a read that raises nothing belongs with
|
|
33
|
+
//! the values (`rb::value`, `rb::object` for an object's ivars and class,
|
|
34
|
+
//! `rb::method` for the current frame) - and on a
|
|
35
|
+
//! `call` that protects nothing. The protection itself, `$!` and the calls
|
|
36
|
+
//! that raise on purpose are [`protect`]'s, the one part held to neither.
|
|
37
|
+
|
|
38
|
+
#![allow(unsafe_code)]
|
|
39
|
+
/* `raw` is the crate's alone (`pub(crate) mod raw`): every item says so
|
|
40
|
+
* itself, and a `pub` that only reads as wider is refused. */
|
|
41
|
+
#![deny(unreachable_pub)]
|
|
42
|
+
/* Every wrapper's contract is the module's, stated once above. */
|
|
43
|
+
#![allow(clippy::missing_safety_doc)]
|
|
44
|
+
|
|
45
|
+
use core::ffi::{c_char, c_int, c_void};
|
|
46
|
+
|
|
47
|
+
use crate::rb::value::AsSys;
|
|
48
|
+
use crate::rb::{
|
|
49
|
+
Error, ExceptionClass, Float, Id, Integer, RArray, RClass, RHash, RModule, RString, Range,
|
|
50
|
+
Ruby, Symbol, Value,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
use crate::encoding::Encoding;
|
|
54
|
+
|
|
55
|
+
mod protect;
|
|
56
|
+
#[cfg(feature = "alloc-inject")]
|
|
57
|
+
pub(crate) use protect::set_raise_injector;
|
|
58
|
+
use protect::{call, call_allocating};
|
|
59
|
+
pub(crate) use protect::{
|
|
60
|
+
exc_raise, forget_errinfo, keeping_errinfo, pending_exit, reset_pending_exit, resume,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/* ---- what the value-level wrappers share ---- */
|
|
64
|
+
|
|
65
|
+
/// The live value a wrapped C call returned, as a `T` - checked to be one, so
|
|
66
|
+
/// a C function that answered with another kind is an error, not a value read
|
|
67
|
+
/// as the wrong type.
|
|
68
|
+
#[inline]
|
|
69
|
+
fn typed<T>(
|
|
70
|
+
v: nupur_sys::value::Value,
|
|
71
|
+
of: fn(Value) -> Option<T>,
|
|
72
|
+
what: &'static str,
|
|
73
|
+
) -> Result<T, Error> {
|
|
74
|
+
/* `what` is the whole message, a literal: this runs on the path that
|
|
75
|
+
* raises a caught panic, which must stay `fmt`-free (`rake no_unwind`). */
|
|
76
|
+
of(Value::from_sys(v)).ok_or_else(|| crate::rb::error::internal_error(what))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// A length or count for the C API's `long` or `int` (whichever the call's
|
|
80
|
+
/// parameter is); `ArgumentError` with `what` for one it cannot hold - a
|
|
81
|
+
/// `long` past 2^31 on Windows. `what` is the whole message, a literal, as
|
|
82
|
+
/// [`typed`]'s is.
|
|
83
|
+
#[inline]
|
|
84
|
+
fn c_len<L: TryFrom<usize>>(n: usize, what: &'static str) -> Result<L, Error> {
|
|
85
|
+
L::try_from(n).map_err(|_| crate::rb::error::arg_error(what))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* ---- the rest, by what it wraps ---- */
|
|
89
|
+
|
|
90
|
+
mod check;
|
|
91
|
+
mod data;
|
|
92
|
+
mod define;
|
|
93
|
+
mod exception;
|
|
94
|
+
mod iterate;
|
|
95
|
+
mod string;
|
|
96
|
+
mod symbol;
|
|
97
|
+
mod values;
|
|
98
|
+
pub(crate) use check::*;
|
|
99
|
+
pub(crate) use data::*;
|
|
100
|
+
pub(crate) use define::*;
|
|
101
|
+
pub(crate) use exception::*;
|
|
102
|
+
pub(crate) use iterate::*;
|
|
103
|
+
pub(crate) use string::*;
|
|
104
|
+
pub(crate) use symbol::*;
|
|
105
|
+
pub(crate) use values::*;
|