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,118 @@
|
|
|
1
|
+
//! Calling a method of any object: the caller's Ruby, run when an
|
|
2
|
+
//! extension asks for it.
|
|
3
|
+
//!
|
|
4
|
+
//! Nupur runs none of the caller's Ruby on its own: no argument is converted
|
|
5
|
+
//! (`args`), no `to_s` is called for a message (`describe`). An extension
|
|
6
|
+
//! that does call a method (a JSON generator calling an object's `to_json`)
|
|
7
|
+
//! does it here, and `rake dylint`'s `runs_callers_ruby` reports each call
|
|
8
|
+
//! site, so each carries an `allow` that says why it may run there.
|
|
9
|
+
//!
|
|
10
|
+
//! The call is protected as every raising C call is: a raise, a `throw`, a
|
|
11
|
+
//! `break` out of a block the method yields to comes back as `Err` and is
|
|
12
|
+
//! resumed by the method's trampoline. What the call may do besides - call
|
|
13
|
+
//! back into the extension, change the Hash or Array being walked, switch to
|
|
14
|
+
//! another thread or Fiber - is the extension's to allow for, and the rule
|
|
15
|
+
//! that keeps it safe is one: hold nothing across the call that the callee
|
|
16
|
+
//! could invalidate.
|
|
17
|
+
//!
|
|
18
|
+
//! - No borrow of a wrapped object's payload (`Typed::data()`), unless what
|
|
19
|
+
//! it borrows is only ever changed through a `Cell`; and no
|
|
20
|
+
//! `RefCell` guard - a call back into the extension would panic on it.
|
|
21
|
+
//! - No guard of the extension's own (Makiri's document guard is one).
|
|
22
|
+
//! - No slice of a Ruby collection: read an Array by index, its length
|
|
23
|
+
//! again at each step. A Hash may be walked in place (`hash::hash_foreach`
|
|
24
|
+
//! is `rb_hash_foreach`, the walk `Hash#each` is), and Ruby guards it as
|
|
25
|
+
//! it guards a block: a key added during the walk raises. Copy its pairs
|
|
26
|
+
//! first (`hash::each_pair`) only where a walk must not raise.
|
|
27
|
+
//!
|
|
28
|
+
//! A Fiber switched away from inside the call and never resumed leaves the
|
|
29
|
+
//! Rust frames under it unfinished: their values are leaked, never freed
|
|
30
|
+
//! twice or used after.
|
|
31
|
+
|
|
32
|
+
#![forbid(unsafe_code)]
|
|
33
|
+
|
|
34
|
+
use std::sync::OnceLock;
|
|
35
|
+
|
|
36
|
+
use super::{Error, Id, ReprValue, Ruby, Value};
|
|
37
|
+
|
|
38
|
+
/// `recv.public_send(id, *args)`: a public method only - a private or
|
|
39
|
+
/// protected one is `NoMethodError`, as `public_send` answers - which is
|
|
40
|
+
/// what a library calling an object's conversion (`to_json`, `to_s`) means.
|
|
41
|
+
#[cfg_attr(
|
|
42
|
+
dylint_lib = "nupur_lints",
|
|
43
|
+
expect(
|
|
44
|
+
runs_callers_ruby,
|
|
45
|
+
reason = "the one way to call a public method; each caller's own allow says why"
|
|
46
|
+
)
|
|
47
|
+
)]
|
|
48
|
+
pub fn public_method(recv: impl ReprValue, id: Id, args: &[Value]) -> Result<Value, Error> {
|
|
49
|
+
crate::raw::call_public(recv.as_value(), id, args)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// `recv.name(*args)`: any method, private ones included (`rb_funcallv`) -
|
|
53
|
+
/// what a class's own `new` or `members` is called through. `name` is
|
|
54
|
+
/// interned on each call. A name called often that is public is a
|
|
55
|
+
/// [`public_method`] of an [`IdConst`]; that call refuses a private method,
|
|
56
|
+
/// which this one runs.
|
|
57
|
+
#[cfg_attr(
|
|
58
|
+
dylint_lib = "nupur_lints",
|
|
59
|
+
expect(
|
|
60
|
+
runs_callers_ruby,
|
|
61
|
+
reason = "the one way to call any method; each caller's own allow says why"
|
|
62
|
+
)
|
|
63
|
+
)]
|
|
64
|
+
pub fn call_method(
|
|
65
|
+
ruby: &Ruby,
|
|
66
|
+
recv: impl ReprValue,
|
|
67
|
+
name: &str,
|
|
68
|
+
args: &[Value],
|
|
69
|
+
) -> Result<Value, Error> {
|
|
70
|
+
crate::raw::call_method(
|
|
71
|
+
ruby,
|
|
72
|
+
recv.as_value(),
|
|
73
|
+
&crate::rb::class::c_name(name)?,
|
|
74
|
+
args,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Whether `recv` has a public method `id`: Ruby's own `respond_to?`, which
|
|
79
|
+
/// a class may redefine (so the caller's Ruby too), and which a
|
|
80
|
+
/// `respond_to_missing?` answers for a method `method_missing` handles.
|
|
81
|
+
#[cfg_attr(
|
|
82
|
+
dylint_lib = "nupur_lints",
|
|
83
|
+
expect(
|
|
84
|
+
runs_callers_ruby,
|
|
85
|
+
reason = "respond_to? may be redefined; each caller's own allow says why"
|
|
86
|
+
)
|
|
87
|
+
)]
|
|
88
|
+
pub fn responds_to(recv: impl ReprValue, id: Id) -> Result<bool, Error> {
|
|
89
|
+
crate::raw::respond_to(recv.as_value(), id)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// A method name interned once (`rb_intern`) and kept: `IdConst::new(c"to_json")`
|
|
93
|
+
/// in a static, read with [`get`](IdConst::get). An interned name is never
|
|
94
|
+
/// collected and is the same integer in every Ractor, so the static is
|
|
95
|
+
/// shared freely. A failed intern (`NoMemoryError`) is `Err`, and the next
|
|
96
|
+
/// read tries again.
|
|
97
|
+
pub struct IdConst {
|
|
98
|
+
name: &'static core::ffi::CStr,
|
|
99
|
+
id: OnceLock<Id>,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
impl IdConst {
|
|
103
|
+
pub const fn new(name: &'static core::ffi::CStr) -> IdConst {
|
|
104
|
+
IdConst {
|
|
105
|
+
name,
|
|
106
|
+
id: OnceLock::new(),
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// The interned name.
|
|
111
|
+
pub fn get(&self, ruby: &Ruby) -> Result<Id, Error> {
|
|
112
|
+
if let Some(id) = self.id.get() {
|
|
113
|
+
return Ok(*id);
|
|
114
|
+
}
|
|
115
|
+
let id = Id::intern(ruby, self.name)?;
|
|
116
|
+
Ok(*self.id.get_or_init(|| id))
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
//! Defining classes, modules and methods - what an extension's `Init_` does.
|
|
2
|
+
//!
|
|
3
|
+
//! Each definition is a C call that can raise (a name already bound to
|
|
4
|
+
//! something else, a frozen target), so each goes through `raw`'s
|
|
5
|
+
//! protected wrappers and comes back as `Err`.
|
|
6
|
+
|
|
7
|
+
#![forbid(unsafe_code)]
|
|
8
|
+
|
|
9
|
+
use super::error::Error;
|
|
10
|
+
use super::method::Func;
|
|
11
|
+
use super::ruby::Ruby;
|
|
12
|
+
use super::value::{ExceptionClass, RClass, RModule, ReprValue, Value};
|
|
13
|
+
use crate::cname::CName;
|
|
14
|
+
|
|
15
|
+
/// `name` as the C string a definition takes; a name with a NUL, or one too
|
|
16
|
+
/// long, is refused.
|
|
17
|
+
pub(crate) fn c_name(name: &str) -> Result<CName, Error> {
|
|
18
|
+
CName::new(name.as_bytes()).ok_or_else(|| {
|
|
19
|
+
crate::rb::error::arg_error("a name to define that is too long or holds a NUL byte")
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// What a class and a module both are: a namespace that holds constants,
|
|
24
|
+
/// methods and other classes.
|
|
25
|
+
pub trait Module: ReprValue {
|
|
26
|
+
/// `class name < superclass` under this module.
|
|
27
|
+
fn define_class(self, name: &str, superclass: RClass) -> Result<RClass, Error> {
|
|
28
|
+
crate::raw::define_class_under(self.as_value(), &c_name(name)?, superclass)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// A class `name` under this module whose instances are the layer's
|
|
32
|
+
/// wrapped objects ([`DataClass`]): its superclass is `superclass`, or
|
|
33
|
+
/// `Object` for `None`, and its allocator is taken away, so `.new` makes
|
|
34
|
+
/// none of a kind no payload backs.
|
|
35
|
+
///
|
|
36
|
+
/// The class is made here, never taken over: a `name` this module
|
|
37
|
+
/// already holds - a class Ruby code defined first, or an autoload, which
|
|
38
|
+
/// `rb_define_class_under` would load and then reuse - is a `TypeError`.
|
|
39
|
+
/// A class someone else made could have modules or a Marshal hook that
|
|
40
|
+
/// [`DataClass`] promises its instances never have. What is left open:
|
|
41
|
+
/// `rb_define_class_under` calls the superclass's `inherited` with the
|
|
42
|
+
/// new class, so a hook defined before `Init_` (`Object.inherited`) runs
|
|
43
|
+
/// and may add to it - load-time Ruby the program itself installed,
|
|
44
|
+
/// which other tools that track subclasses rely on being called.
|
|
45
|
+
fn define_data_class(
|
|
46
|
+
self,
|
|
47
|
+
ruby: &Ruby,
|
|
48
|
+
name: &str,
|
|
49
|
+
superclass: Option<DataClass>,
|
|
50
|
+
) -> Result<DataClass, Error> {
|
|
51
|
+
let c = c_name(name)?;
|
|
52
|
+
let id = crate::rb::Id::intern(ruby, &c)?;
|
|
53
|
+
if crate::raw::const_defined_at(self.as_value(), id)? {
|
|
54
|
+
return Err(crate::rb::error::type_error(format_args!(
|
|
55
|
+
"{name} is already defined: a class of wrapped objects is made by the extension, not taken over"
|
|
56
|
+
)));
|
|
57
|
+
}
|
|
58
|
+
let sup = superclass.map_or_else(|| ruby.class_object(), |d| d.0);
|
|
59
|
+
let class = crate::raw::define_class_under(self.as_value(), &c, sup)?;
|
|
60
|
+
crate::raw::undef_alloc_func(class)?;
|
|
61
|
+
Ok(DataClass(class))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// An exception class `name < superclass` under this module.
|
|
65
|
+
fn define_error(self, name: &str, superclass: ExceptionClass) -> Result<ExceptionClass, Error> {
|
|
66
|
+
let sup = RClass::from_known(superclass.as_value());
|
|
67
|
+
let class = crate::raw::define_class_under(self.as_value(), &c_name(name)?, sup)?;
|
|
68
|
+
/* A class under an exception class is one; checked all the same. */
|
|
69
|
+
ExceptionClass::from_value(class.as_value()).ok_or_else(|| {
|
|
70
|
+
crate::rb::error::internal_error("a class under an exception class is not one")
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// `module name` under this module.
|
|
75
|
+
fn define_module(self, name: &str) -> Result<RModule, Error> {
|
|
76
|
+
crate::raw::define_module_under(self.as_value(), &c_name(name)?)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// The constant `name` = `value`.
|
|
80
|
+
fn const_set(self, ruby: &Ruby, name: &str, value: impl ReprValue) -> Result<(), Error> {
|
|
81
|
+
crate::raw::const_set(ruby, self.as_value(), &c_name(name)?, value.as_value())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// A public instance method.
|
|
85
|
+
fn define_method(self, name: &str, func: Func) -> Result<(), Error> {
|
|
86
|
+
crate::raw::define_method(self.as_value(), &c_name(name)?, func, Visibility::Public)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// A private instance method.
|
|
90
|
+
fn define_private_method(self, name: &str, func: Func) -> Result<(), Error> {
|
|
91
|
+
crate::raw::define_method(self.as_value(), &c_name(name)?, func, Visibility::Private)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// A module function: a private instance method and a singleton method.
|
|
95
|
+
fn define_module_function(self, name: &str, func: Func) -> Result<(), Error> {
|
|
96
|
+
crate::raw::define_method(
|
|
97
|
+
self.as_value(),
|
|
98
|
+
&c_name(name)?,
|
|
99
|
+
func,
|
|
100
|
+
Visibility::ModuleFunction,
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// The constant `name`, which the extension's own Ruby defines.
|
|
105
|
+
fn const_get(self, ruby: &Ruby, name: &str) -> Result<Value, Error> {
|
|
106
|
+
crate::raw::const_get(ruby, self.as_value(), &c_name(name)?)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Whether this module itself holds a constant `name` - an autoload
|
|
110
|
+
/// counts, and nothing is loaded. For a library the caller may or may
|
|
111
|
+
/// not have required (`JSON::State`).
|
|
112
|
+
fn const_defined(self, ruby: &Ruby, name: &str) -> Result<bool, Error> {
|
|
113
|
+
let id = crate::rb::Id::intern(ruby, &c_name(name)?)?;
|
|
114
|
+
crate::raw::const_defined_at(self.as_value(), id)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Include `module`.
|
|
118
|
+
fn include_module(self, module: RModule) -> Result<(), Error> {
|
|
119
|
+
crate::raw::include_module(self.as_value(), module.as_value())
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// What every object can have: a singleton method.
|
|
124
|
+
pub trait Object: ReprValue {
|
|
125
|
+
/// A method on this object alone - a class method, for a class.
|
|
126
|
+
fn define_singleton_method(self, name: &str, func: Func) -> Result<(), Error> {
|
|
127
|
+
crate::raw::define_method(self.as_value(), &c_name(name)?, func, Visibility::Singleton)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// A class made by [`Module::define_data_class`]: one whose instances the
|
|
132
|
+
/// layer makes, wrapping a payload ([`crate::typed`] takes no other class).
|
|
133
|
+
///
|
|
134
|
+
/// Its ancestors are `Object` and such classes only, so no instance of it is
|
|
135
|
+
/// handed to a C method that assumes another layout - a T_DATA object made
|
|
136
|
+
/// with String's class would have `#length` read it as a String. There is no
|
|
137
|
+
/// way to make one from an arbitrary class: no `from_value`.
|
|
138
|
+
#[derive(Clone, Copy)]
|
|
139
|
+
#[repr(transparent)]
|
|
140
|
+
pub struct DataClass(RClass);
|
|
141
|
+
|
|
142
|
+
impl ReprValue for DataClass {
|
|
143
|
+
#[inline]
|
|
144
|
+
fn as_value(self) -> Value {
|
|
145
|
+
self.0.as_value()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
impl Module for DataClass {}
|
|
150
|
+
impl Object for DataClass {}
|
|
151
|
+
|
|
152
|
+
/// How a method is defined.
|
|
153
|
+
#[derive(Clone, Copy)]
|
|
154
|
+
pub(crate) enum Visibility {
|
|
155
|
+
Public,
|
|
156
|
+
Private,
|
|
157
|
+
Singleton,
|
|
158
|
+
ModuleFunction,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
impl Module for RClass {}
|
|
162
|
+
impl Module for RModule {}
|
|
163
|
+
impl Object for RClass {}
|
|
164
|
+
impl Object for RModule {}
|
|
165
|
+
|
|
166
|
+
/// Freeze `v` (`rb_obj_freeze`); a failed allocation of its frozen shape is
|
|
167
|
+
/// `Err` (`NoMemoryError`).
|
|
168
|
+
pub fn freeze(v: impl ReprValue) -> Result<(), Error> {
|
|
169
|
+
crate::raw::obj_freeze(v.as_value())
|
|
170
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
//! What a registered method takes and returns.
|
|
2
|
+
//!
|
|
3
|
+
//! A method takes `Value`s: the layer converts no argument (`args`'
|
|
4
|
+
//! `string_arg` and kin check a type and refuse the rest), so the only
|
|
5
|
+
//! [`TryConvert`]s are `Value` itself, `bool` (truthiness, read from the
|
|
6
|
+
//! bits), and an extension's own receiver types, which check a TypedData class. None
|
|
7
|
+
//! calls Ruby. A method's result becomes a `Value` through [`IntoValue`],
|
|
8
|
+
//! which can allocate and so returns `Err` rather than raising.
|
|
9
|
+
|
|
10
|
+
#![forbid(unsafe_code)]
|
|
11
|
+
|
|
12
|
+
use super::error::Error;
|
|
13
|
+
use super::ruby::Ruby;
|
|
14
|
+
use super::value::{ReprValue, Value};
|
|
15
|
+
|
|
16
|
+
/// A method argument read from its `Value`.
|
|
17
|
+
pub trait TryConvert: Sized {
|
|
18
|
+
fn try_convert(ruby: &Ruby, v: Value) -> Result<Self, Error>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl TryConvert for Value {
|
|
22
|
+
#[inline]
|
|
23
|
+
fn try_convert(_: &Ruby, v: Value) -> Result<Value, Error> {
|
|
24
|
+
Ok(v)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl TryConvert for bool {
|
|
29
|
+
#[inline]
|
|
30
|
+
fn try_convert(_: &Ruby, v: Value) -> Result<bool, Error> {
|
|
31
|
+
Ok(v.to_bool())
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// A method's result, as the `Value` Ruby receives.
|
|
36
|
+
pub trait IntoValue {
|
|
37
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl<T: ReprValue> IntoValue for T {
|
|
41
|
+
#[inline]
|
|
42
|
+
fn into_value(self, _: &Ruby) -> Result<Value, Error> {
|
|
43
|
+
Ok(self.as_value())
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
impl IntoValue for () {
|
|
48
|
+
#[inline]
|
|
49
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
50
|
+
Ok(ruby.qnil())
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
impl IntoValue for bool {
|
|
55
|
+
#[inline]
|
|
56
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
57
|
+
Ok(ruby.boolean(self))
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl<T: IntoValue> IntoValue for Option<T> {
|
|
62
|
+
#[inline]
|
|
63
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
64
|
+
match self {
|
|
65
|
+
Some(v) => v.into_value(ruby),
|
|
66
|
+
None => Ok(ruby.qnil()),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
macro_rules! signed {
|
|
72
|
+
($($t:ty),*) => {$(
|
|
73
|
+
impl IntoValue for $t {
|
|
74
|
+
#[inline]
|
|
75
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
76
|
+
crate::build::int(ruby, i64::from(self)).map(ReprValue::as_value)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
)*};
|
|
80
|
+
}
|
|
81
|
+
signed!(i32, i64);
|
|
82
|
+
|
|
83
|
+
macro_rules! unsigned {
|
|
84
|
+
($($t:ty),*) => {$(
|
|
85
|
+
impl IntoValue for $t {
|
|
86
|
+
#[inline]
|
|
87
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
88
|
+
crate::build::uint(ruby, u64::from(self)).map(ReprValue::as_value)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
)*};
|
|
92
|
+
}
|
|
93
|
+
unsigned!(u32, u64);
|
|
94
|
+
|
|
95
|
+
impl IntoValue for usize {
|
|
96
|
+
#[inline]
|
|
97
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
98
|
+
crate::build::uint(ruby, crate::int::wide(self)).map(ReprValue::as_value)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
impl IntoValue for &'static str {
|
|
103
|
+
#[inline]
|
|
104
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
105
|
+
crate::string::utf8_str(ruby, self).map(ReprValue::as_value)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
impl<A: IntoValue, B: IntoValue> IntoValue for (A, B) {
|
|
110
|
+
fn into_value(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
111
|
+
let (a, b) = (self.0.into_value(ruby)?, self.1.into_value(ruby)?);
|
|
112
|
+
/* One call for the pair, not one per element. */
|
|
113
|
+
crate::build::ary_from(ruby, &[a, b]).map(ReprValue::as_value)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// What a registered function returns: a value, or a `Result` of one.
|
|
118
|
+
pub trait ReturnValue {
|
|
119
|
+
fn into_return(self, ruby: &Ruby) -> Result<Value, Error>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
impl<T: IntoValue> ReturnValue for T {
|
|
123
|
+
#[inline]
|
|
124
|
+
fn into_return(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
125
|
+
self.into_value(ruby)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
impl<T: IntoValue> ReturnValue for Result<T, Error> {
|
|
130
|
+
#[inline]
|
|
131
|
+
fn into_return(self, ruby: &Ruby) -> Result<Value, Error> {
|
|
132
|
+
self?.into_value(ruby)
|
|
133
|
+
}
|
|
134
|
+
}
|