Trait mrusty::MrubyImpl
[−]
[src]
pub trait MrubyImpl { fn filename(&self, filename: &str); fn run(&self, script: &str) -> Result<Value, MrubyError>; unsafe fn run_unchecked(&self, script: &str) -> Value; fn runb(&self, script: &[u8]) -> Result<Value, MrubyError>; fn execute(&self, script: &Path) -> Result<Value, MrubyError>; fn is_defined(&self, name: &str) -> bool; fn is_defined_under<T: ClassLike>(&self, name: &str, outer: &T) -> bool; fn get_class(&self, name: &str) -> Result<Class, MrubyError>; fn get_class_under<T: ClassLike>(&self, name: &str, outer: &T) -> Result<Class, MrubyError>; fn get_module(&self, name: &str) -> Result<Module, MrubyError>; fn get_module_under<T: ClassLike>(&self, name: &str, outer: &T) -> Result<Module, MrubyError>; fn def_file<T: MrubyFile>(&self, name: &str); fn def_class(&self, name: &str) -> Class; fn def_class_under<U: ClassLike>(&self, name: &str, outer: &U) -> Class; fn def_class_for<T: Any>(&self, name: &str) -> Class; fn def_class_under_for<T: Any, U: ClassLike>(&self, name: &str, outer: &U) -> Class; fn def_module(&self, name: &str) -> Module; fn def_module_under<T: ClassLike>(&self, name: &str, outer: &T) -> Module; fn def_method<F>(&self, class: Class, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static; fn def_class_method<F>(&self, class: Class, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static; fn def_method_for<T: Any, F>(&self, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static; fn def_class_method_for<T: Any, F>(&self, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static; fn class_name_for<T: Any>(&self) -> Result<String, MrubyError>; fn nil(&self) -> Value; fn bool(&self, value: bool) -> Value; fn fixnum(&self, value: i32) -> Value; fn float(&self, value: f64) -> Value; fn string(&self, value: &str) -> Value; fn symbol(&self, value: &str) -> Value; fn obj<T: Any>(&self, obj: T) -> Value; fn option<T: Any>(&self, obj: Option<T>) -> Value; fn array(&self, value: Vec<Value>) -> Value; }
A trait
used on MrubyType
which implements mruby functionality.
Required Methods
fn filename(&self, filename: &str)
Adds a filename to the mruby context.
Examples
let mruby = Mruby::new(); mruby.filename("script.rb"); let result = mruby.run("1.nope"); match result { Err(MrubyError::Runtime(err)) => { assert_eq!(err, "script.rb:1: undefined method \'nope\' for 1 (NoMethodError)"); }, _ => assert!(false) }
fn run(&self, script: &str) -> Result<Value, MrubyError>
Runs mruby script
on a state and context and returns a Value
in an Ok
or an Err
containing an mruby Exception
's message.
Examples
let mruby = Mruby::new(); let result = mruby.run("true").unwrap(); assert_eq!(result.to_bool().unwrap(), true);
let mruby = Mruby::new(); let result = mruby.run("'' + 1"); match result { Err(MrubyError::Runtime(err)) => { assert_eq!(err, "TypeError: expected String"); }, _ => assert!(false) }
unsafe fn run_unchecked(&self, script: &str) -> Value
Runs mruby script
on a state and context and returns a Value
. If an mruby Exception is
raised, mruby will be left to handle it.
The method is unsafe because running it within a Rust context will interrupt drops, potentially leading to memory leaks.
Examples
let mruby = Mruby::new(); let result = unsafe { mruby.run_unchecked("true") }; assert_eq!(result.to_bool().unwrap(), true);
use mrusty::{Mruby, MrubyImpl}; let mruby = Mruby::new(); struct Cont; mruby.def_class_for::<Cont>("Container"); mruby.def_class_method_for::<Cont, _>("raise", mrfn!(|mruby, _slf: Value| { unsafe { mruby.run_unchecked("fail 'surprize'") } })); let result = mruby.run(" begin Container.raise rescue => e e.message end ").unwrap(); assert_eq!(result.to_str().unwrap(), "surprize");
fn runb(&self, script: &[u8]) -> Result<Value, MrubyError>
Runs mruby compiled (.mrb) script
on a state and context and returns a Value
in an Ok
or an Err
containing an mruby Exception
's message.
Examples
let mruby = Mruby::new(); let result = mruby.runb(include_bytes!("script.mrb")).unwrap();
fn execute(&self, script: &Path) -> Result<Value, MrubyError>
Runs mruby (compiled (.mrb) or not (.rb)) script
on a state and context and returns a
Value
in an Ok
or an Err
containing an mruby Exception
's message.
Examples
let mruby = Mruby::new(); let result = mruby.execute(&Path::new("script.rb")).unwrap();
fn is_defined(&self, name: &str) -> bool
Returns whether the mruby Class
or Module
named name
is defined.
Examples
let mruby = Mruby::new(); let object = mruby.is_defined("Object"); let objekt = mruby.is_defined("Objekt"); assert!(object); assert!(!objekt);
fn is_defined_under<T: ClassLike>(&self, name: &str, outer: &T) -> bool
Returns whether the mruby Class
or Module
named name
is defined under outer
Class
or Module
.
Examples
let mruby = Mruby::new(); let module = mruby.def_module("Just"); mruby.def_module_under("Mine", &module); assert!(mruby.is_defined_under("Mine", &module));
fn get_class(&self, name: &str) -> Result<Class, MrubyError>
Returns the mruby Class
named name
in a Some
or None
if it is not defined.
Examples
let mruby = Mruby::new(); let object = mruby.get_class("Object"); let objekt = mruby.get_class("Objekt"); assert_eq!(object.unwrap().to_str(), "Object"); assert!(objekt.is_err());
fn get_class_under<T: ClassLike>(&self, name: &str, outer: &T) -> Result<Class, MrubyError>
Returns the mruby Class
named name
under outer
Class
or Module
in a Some
or
None
if it is not defined.
Examples
let mruby = Mruby::new(); struct Cont; let module = mruby.def_module("Mine"); mruby.def_class_under_for::<Cont, _>("Container", &module); let result = mruby.get_class_under("Container", &module).unwrap(); assert_eq!(result.to_str(), "Mine::Container");
fn get_module(&self, name: &str) -> Result<Module, MrubyError>
Returns the mruby Module
named name
in a Some
or None
if it is not defined.
Examples
let mruby = Mruby::new(); let kernel = mruby.get_module("Kernel"); let kernet = mruby.get_module("Kernet"); assert_eq!(kernel.unwrap().to_str(), "Kernel"); assert!(kernet.is_err());
fn get_module_under<T: ClassLike>(&self, name: &str, outer: &T) -> Result<Module, MrubyError>
Returns the mruby Module
named name
under outer
Class
or Module
in a Some
or
None
if it is not defined.
Examples
let mruby = Mruby::new(); let module = mruby.def_module("Just"); mruby.def_module_under("Mine", &module); let result = mruby.get_module_under("Mine", &module).unwrap(); assert_eq!(result.to_str(), "Just::Mine");
fn def_file<T: MrubyFile>(&self, name: &str)
Defines a dynamic file that can be require
d containing the Rust type T
and runs its
MrubyFile
-inherited require
method.
Examples
use mrusty::{Mruby, MrubyFile, MrubyImpl, MrubyType}; let mruby = Mruby::new(); struct Cont { value: i32 }; impl MrubyFile for Cont { fn require(mruby: MrubyType) { mruby.def_class_for::<Cont>("Container"); mruby.def_method_for::<Cont, _>("initialize", mrfn!(|_mruby, slf: Value, v: i32| { let cont = Cont { value: v }; slf.init(cont) })); mruby.def_method_for::<Cont, _>("value", mrfn!(|mruby, slf: (&Cont)| { mruby.fixnum(slf.value) })); } } mruby.def_file::<Cont>("cont"); let result = mruby.run(" require 'cont' Container.new(3).value ").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
fn def_class(&self, name: &str) -> Class
Defines an mruby Class
named name
.
Examples
let mruby = Mruby::new(); mruby.def_class("Container"); assert!(mruby.is_defined("Container"));
fn def_class_under<U: ClassLike>(&self, name: &str, outer: &U) -> Class
Defines an mruby Class
named name
under outer
Class
or Module
.
Examples
let mruby = Mruby::new(); let module = mruby.def_module("Mine"); mruby.def_class_under("Container", &module); assert!(mruby.is_defined_under("Container", &module));
fn def_class_for<T: Any>(&self, name: &str) -> Class
Defines Rust type T
as an mruby Class
named name
.
Examples
let mruby = Mruby::new(); struct Cont { value: i32 } mruby.def_class_for::<Cont>("Container"); assert!(mruby.is_defined("Container"));
fn def_class_under_for<T: Any, U: ClassLike>(&self, name: &str, outer: &U) -> Class
Defines Rust type T
as an mruby Class
named name
under outer
Class
or Module
.
Examples
let mruby = Mruby::new(); struct Cont; let module = mruby.def_module("Mine"); mruby.def_class_under_for::<Cont, _>("Container", &module); assert!(mruby.is_defined_under("Container", &module));
fn def_module(&self, name: &str) -> Module
Defines an mruby Module
named name
.
Examples
let mruby = Mruby::new(); mruby.def_module("Container"); assert!(mruby.is_defined("Container"));
fn def_module_under<T: ClassLike>(&self, name: &str, outer: &T) -> Module
Defines an mruby Module
named name
under outer
Class
or Module
.
Examples
let mruby = Mruby::new(); let module = mruby.def_module("Just"); mruby.def_module_under("Mine", &module); assert!(mruby.is_defined_under("Mine", &module));
fn def_method<F>(&self, class: Class, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static
Defines an mruby method named name
on Class
class
. The closure to be run when the
name
method is called should be passed through the mrfn!
macro.
Examples
use mrusty::{Mruby, MrubyImpl}; let mruby = Mruby::new(); let class = mruby.def_class("Container"); mruby.def_method(class, "value", mrfn!(|mruby, slf: Value| { mruby.fixnum(3) })); let result = mruby.run("Container.new.value").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
fn def_class_method<F>(&self, class: Class, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static
Defines an mruby class method named name
on Class
class
. The closure to be run when
the name
method is called should be passed through the mrfn!
macro.
Examples
use mrusty::{Mruby, MrubyImpl}; let mruby = Mruby::new(); let class = mruby.def_class("Container"); mruby.def_class_method(class, "hi", mrfn!(|mruby, _slf: Value, v: i32| { mruby.fixnum(v) })); let result = mruby.run("Container.hi 3").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
fn def_method_for<T: Any, F>(&self, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static
Defines an mruby method named name
on the mruby Class
reflecting type T
. The closure
to be run when the name
method is called should be passed through the mrfn!
macro.
Examples
use mrusty::{Mruby, MrubyImpl}; let mruby = Mruby::new(); struct Cont { value: i32 }; mruby.def_class_for::<Cont>("Container"); mruby.def_method_for::<Cont, _>("initialize", mrfn!(|_mruby, slf: Value, v: i32| { let cont = Cont { value: v }; slf.init(cont) })); mruby.def_method_for::<Cont, _>("value", mrfn!(|mruby, slf: (&Cont)| { mruby.fixnum(slf.value) })); let result = mruby.run("Container.new(3).value").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
fn def_class_method_for<T: Any, F>(&self, name: &str, method: F) where F: Fn(MrubyType, Value) -> Value + 'static
Defines an mruby class method named name
on the mruby Class
reflecting type T
. The
closure to be run when the name
method is called should be passed through the mrfn!
macro.
Examples
use mrusty::{Mruby, MrubyImpl}; let mruby = Mruby::new(); struct Cont; mruby.def_class_for::<Cont>("Container"); mruby.def_class_method_for::<Cont, _>("hi", mrfn!(|mruby, _slf: Value, v: i32| { mruby.fixnum(v) })); let result = mruby.run("Container.hi 3").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
fn class_name_for<T: Any>(&self) -> Result<String, MrubyError>
Return the mruby name of a previously defined Rust type T
with def_class
.
Examples
let mruby = Mruby::new(); struct Cont; mruby.def_class_for::<Cont>("Container"); assert_eq!(mruby.class_name_for::<Cont>().unwrap(), "Container");
fn nil(&self) -> Value
Creates mruby Value
nil
.
Examples
let mruby = Mruby::new(); struct Cont; mruby.def_class_for::<Cont>("Container"); mruby.def_method_for::<Cont, _>("nil", |mruby, _slf| mruby.nil()); let result = mruby.run("Container.new.nil.nil?").unwrap(); assert_eq!(result.to_bool().unwrap(), true);
fn bool(&self, value: bool) -> Value
Creates mruby Value
containing true
or false
.
Examples
let mruby = Mruby::new(); let b = mruby.bool(true); assert_eq!(b.to_bool().unwrap(), true);
fn fixnum(&self, value: i32) -> Value
Creates mruby Value
of Class
Fixnum
.
Examples
let mruby = Mruby::new(); let fixn = mruby.fixnum(2); assert_eq!(fixn.to_i32().unwrap(), 2);
fn float(&self, value: f64) -> Value
Creates mruby Value
of Class
Float
.
Examples
let mruby = Mruby::new(); let fl = mruby.float(2.3); assert_eq!(fl.to_f64().unwrap(), 2.3);
fn string(&self, value: &str) -> Value
Creates mruby Value
of Class
String
.
Examples
let mruby = Mruby::new(); let s = mruby.string("hi"); assert_eq!(s.to_str().unwrap(), "hi");
fn symbol(&self, value: &str) -> Value
Creates mruby Value
of Class
Symbol
.
Examples
let mruby = Mruby::new(); let s = mruby.symbol("hi"); assert_eq!(s.to_str().unwrap(), "hi");
fn obj<T: Any>(&self, obj: T) -> Value
Creates mruby Value
of Class
name
containing a Rust object of type T
.
Note: T
must be defined on the current Mruby
with def_class
.
Examples
let mruby = Mruby::new(); struct Cont { value: i32 } mruby.def_class_for::<Cont>("Container"); let value = mruby.obj(Cont { value: 3 });
fn option<T: Any>(&self, obj: Option<T>) -> Value
Creates mruby Value
of Class
name
containing a Rust Option
of type T
.
Note: T
must be defined on the current Mruby
with def_class
.
Examples
let mruby = Mruby::new(); struct Cont { value: i32 } mruby.def_class_for::<Cont>("Container"); let none = mruby.option::<Cont>(None); let some = mruby.option(Some(Cont { value: 3 })); let some = some.to_obj::<Cont>().unwrap(); let some = some.borrow(); assert_eq!(none.call("nil?", vec![]).unwrap().to_bool().unwrap(), true); assert_eq!(some.value, 3);
fn array(&self, value: Vec<Value>) -> Value
Creates mruby Value
of Class
Array
.
Examples
let mruby = Mruby::new(); let array = mruby.array(vec![ mruby.fixnum(1), mruby.fixnum(2), mruby.fixnum(3) ]); assert_eq!(array.to_vec().unwrap(), vec![ mruby.fixnum(1), mruby.fixnum(2), mruby.fixnum(3) ]);
Implementors
impl MrubyImpl for MrubyType