artichoke_backend/
globals.rs1use std::borrow::Cow;
2
3use crate::Artichoke;
4use crate::core::{Globals, Intern};
5use crate::error::Error;
6use crate::sys;
7use crate::value::Value;
8
9impl Globals for Artichoke {
13 type Value = Value;
14
15 type Error = Error;
16
17 fn set_global_variable<T>(&mut self, name: T, value: &Self::Value) -> Result<(), Self::Error>
18 where
19 T: Into<Cow<'static, [u8]>>,
20 {
21 let sym = self.intern_bytes(name.into())?;
22 unsafe {
23 self.with_ffi_boundary(|mrb| sys::mrb_gv_set(mrb, sym, value.inner()))?;
24 }
25 Ok(())
26 }
27
28 fn unset_global_variable<T>(&mut self, name: T) -> Result<(), Self::Error>
39 where
40 T: Into<Cow<'static, [u8]>>,
41 {
42 let sym = self.intern_bytes(name.into())?;
43 let nil = Value::nil();
44 unsafe {
45 self.with_ffi_boundary(|mrb| sys::mrb_gv_set(mrb, sym, nil.inner()))?;
46 }
47 Ok(())
48 }
49
50 fn get_global_variable<T>(&mut self, name: T) -> Result<Option<Self::Value>, Self::Error>
51 where
52 T: Into<Cow<'static, [u8]>>,
53 {
54 let sym = self.intern_bytes(name.into())?;
55 let value = unsafe { self.with_ffi_boundary(|mrb| sys::mrb_gv_get(mrb, sym))? };
56 Ok(Some(Value::from(value)))
60 }
61}