artichoke_backend/extn/core/random/
mruby.rs

1//! FFI glue between the Rust trampolines and the mruby C interpreter.
2
3use std::ffi::CStr;
4
5use super::{Rng, trampoline};
6use crate::extn::prelude::*;
7
8const RANDOM_CSTR: &CStr = c"Random";
9static RANDOM_RUBY_SOURCE: &[u8] = include_bytes!("random.rb");
10
11pub fn init(interp: &mut Artichoke) -> InitializeResult<()> {
12    if interp.is_class_defined::<Rng>() {
13        return Ok(());
14    }
15
16    let spec = class::Spec::new("Random", RANDOM_CSTR, None, Some(def::box_unbox_free::<Rng>))?;
17    class::Builder::for_spec(interp, &spec)
18        .value_is_rust_object()
19        .add_self_method("new_seed", random_self_new_seed, sys::mrb_args_req(1))?
20        .add_self_method("srand", random_self_srand, sys::mrb_args_opt(1))?
21        .add_self_method("urandom", random_self_urandom, sys::mrb_args_req(1))?
22        .add_method("initialize", random_initialize, sys::mrb_args_opt(1))?
23        .add_method("==", random_eq, sys::mrb_args_opt(1))?
24        .add_method("bytes", random_bytes, sys::mrb_args_req(1))?
25        .add_method("rand", random_rand, sys::mrb_args_opt(1))?
26        .add_method("seed", random_seed, sys::mrb_args_none())?
27        .define()?;
28    interp.def_class::<Rng>(spec)?;
29
30    let default = Rng::Global;
31    let default = Rng::alloc_value(default, interp).map_err(|_| NotDefinedError::class_constant("Random::DEFAULT"))?;
32    interp.define_class_constant::<Rng>("DEFAULT", default)?;
33    interp.eval(RANDOM_RUBY_SOURCE)?;
34
35    Ok(())
36}
37
38unsafe extern "C-unwind" fn random_initialize(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
39    let seed = mrb_get_args!(mrb, optional = 1);
40    unwrap_interpreter!(mrb, to => guard);
41    let slf = Value::from(slf);
42    let seed = seed.map(Value::from);
43    let result = trampoline::initialize(&mut guard, seed, slf);
44    match result {
45        Ok(value) => value.inner(),
46        Err(exception) => {
47            // SAFETY: only Copy objects remain on the stack
48            unsafe { error::raise(guard, exception) }
49        }
50    }
51}
52
53unsafe extern "C-unwind" fn random_eq(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
54    let other = mrb_get_args!(mrb, required = 1);
55    unwrap_interpreter!(mrb, to => guard);
56    let rand = Value::from(slf);
57    let other = Value::from(other);
58    let result = trampoline::equal(&mut guard, rand, other);
59    match result {
60        Ok(value) => value.inner(),
61        Err(exception) => {
62            // SAFETY: only Copy objects remain on the stack
63            unsafe { error::raise(guard, exception) }
64        }
65    }
66}
67
68unsafe extern "C-unwind" fn random_bytes(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
69    let size = mrb_get_args!(mrb, required = 1);
70    unwrap_interpreter!(mrb, to => guard);
71    let rand = Value::from(slf);
72    let size = Value::from(size);
73    let result = trampoline::bytes(&mut guard, rand, size);
74    match result {
75        Ok(value) => value.inner(),
76        Err(exception) => {
77            // SAFETY: only Copy objects remain on the stack
78            unsafe { error::raise(guard, exception) }
79        }
80    }
81}
82
83unsafe extern "C-unwind" fn random_rand(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
84    let max = mrb_get_args!(mrb, optional = 1);
85    unwrap_interpreter!(mrb, to => guard);
86    let rand = Value::from(slf);
87    let max = max.map(Value::from);
88    let result = trampoline::rand(&mut guard, rand, max);
89    match result {
90        Ok(value) => value.inner(),
91        Err(exception) => {
92            // SAFETY: only Copy objects remain on the stack
93            unsafe { error::raise(guard, exception) }
94        }
95    }
96}
97
98unsafe extern "C-unwind" fn random_seed(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
99    mrb_get_args!(mrb, none);
100    unwrap_interpreter!(mrb, to => guard);
101    let rand = Value::from(slf);
102    let result = trampoline::seed(&mut guard, rand);
103    match result {
104        Ok(value) => value.inner(),
105        Err(exception) => {
106            // SAFETY: only Copy objects remain on the stack
107            unsafe { error::raise(guard, exception) }
108        }
109    }
110}
111
112unsafe extern "C-unwind" fn random_self_new_seed(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value {
113    mrb_get_args!(mrb, none);
114    unwrap_interpreter!(mrb, to => guard);
115    let result = trampoline::new_seed(&mut guard);
116    match result {
117        Ok(value) => value.inner(),
118        Err(exception) => {
119            // SAFETY: only Copy objects remain on the stack
120            unsafe { error::raise(guard, exception) }
121        }
122    }
123}
124
125unsafe extern "C-unwind" fn random_self_srand(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value {
126    let number = mrb_get_args!(mrb, optional = 1);
127    unwrap_interpreter!(mrb, to => guard);
128    let number = number.map(Value::from);
129    let result = trampoline::srand(&mut guard, number);
130    match result {
131        Ok(value) => value.inner(),
132        Err(exception) => {
133            // SAFETY: only Copy objects remain on the stack
134            unsafe { error::raise(guard, exception) }
135        }
136    }
137}
138
139unsafe extern "C-unwind" fn random_self_urandom(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value {
140    let size = mrb_get_args!(mrb, required = 1);
141    unwrap_interpreter!(mrb, to => guard);
142    let size = Value::from(size);
143    let result = trampoline::urandom(&mut guard, size);
144    match result {
145        Ok(value) => value.inner(),
146        Err(exception) => {
147            // SAFETY: only Copy objects remain on the stack
148            unsafe { error::raise(guard, exception) }
149        }
150    }
151}