forked from StrafesNET/strafe-project
87 lines
2.4 KiB
Rust
87 lines
2.4 KiB
Rust
use crate::context::Context;
|
|
|
|
pub struct Runner{
|
|
lua:mlua::Lua,
|
|
}
|
|
#[derive(Debug)]
|
|
pub enum Error{
|
|
Lua{
|
|
source:String,
|
|
error:mlua::Error
|
|
},
|
|
RustLua(mlua::Error),
|
|
Script(super::instance::GetScriptError),
|
|
NoContext,
|
|
}
|
|
impl std::fmt::Display for Error{
|
|
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
|
match self{
|
|
Self::Lua{source,error}=>write!(f,"lua error: source:\n{source}\n{error}"),
|
|
Self::RustLua(error)=>write!(f,"rust-side lua error: {error}"),
|
|
other=>write!(f,"{other:?}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
|
lua.sandbox(true)?;
|
|
|
|
//global environment
|
|
let globals=lua.globals();
|
|
|
|
super::r#enum::set_globals(lua,&globals)?;
|
|
super::color3::set_globals(lua,&globals)?;
|
|
super::vector3::set_globals(lua,&globals)?;
|
|
super::cframe::set_globals(lua,&globals)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
impl Runner{
|
|
pub fn new()->Result<Self,Error>{
|
|
let runner=Self{
|
|
lua:mlua::Lua::new(),
|
|
};
|
|
init(&runner.lua).map_err(Error::RustLua)?;
|
|
Ok(runner)
|
|
}
|
|
pub fn runnable_context<'a>(self,context:&'a mut Context,services:crate::place::Services)->Result<Runnable<'a>,Error>{
|
|
{
|
|
let globals=self.lua.globals();
|
|
globals.set("game",super::instance::DataModel::new(services.game)).map_err(Error::RustLua)?;
|
|
globals.set("workspace",super::instance::Workspace::new(services.workspace)).map_err(Error::RustLua)?;
|
|
}
|
|
//this makes set_app_data shut up about the lifetime
|
|
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
|
Ok(Runnable{
|
|
lua:self.lua,
|
|
_lifetime:&std::marker::PhantomData
|
|
})
|
|
}
|
|
}
|
|
|
|
//Runnable is the same thing but has context set, which it holds the lifetime for.
|
|
pub struct Runnable<'a>{
|
|
lua:mlua::Lua,
|
|
_lifetime:&'a std::marker::PhantomData<()>
|
|
}
|
|
impl Runnable<'_>{
|
|
pub fn drop_context(self)->Runner{
|
|
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
|
Runner{
|
|
lua:self.lua,
|
|
}
|
|
}
|
|
pub fn run_script(&self,script:super::instance::Script)->Result<(),Error>{
|
|
let (name,source)={
|
|
let dom=&mut *self.lua.app_data_mut::<&'static mut rbx_dom_weak::WeakDom>().ok_or(Error::NoContext)?;
|
|
let (name,source)=script.get_name_source(Context::from_mut(dom)).map_err(Error::Script)?;
|
|
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
|
|
(name,source)
|
|
};
|
|
self.lua.load(source.as_str())
|
|
.set_name(name)
|
|
.exec().map_err(|error|Error::Lua{source,error})
|
|
}
|
|
}
|