simplify run_script logic

This commit is contained in:
Quaternions 2024-10-04 17:51:56 -07:00
parent 9b077d6db7
commit e4114ab2cc
2 changed files with 11 additions and 21 deletions

View File

@ -250,22 +250,18 @@ class_composition!(Workspace,(Instance));
class!(Lighting);
class_composition!(Lighting,(Instance));
#[derive(Debug)]
pub enum GetScriptError{
NoScript,
NoSource,
}
class!(Script);
class_composition!(Script,(Instance));
impl Script{
pub fn get_name_source(&self,context:&crate::context::Context)->Result<(String,String),GetScriptError>{
let instance=context.dom.get_by_ref(self.referent).ok_or(GetScriptError::NoScript)?;
let source=match instance.properties.get("Source").ok_or(GetScriptError::NoSource)?{
rbx_dom_weak::types::Variant::String(s)=>s.clone(),
_=>Err(GetScriptError::NoSource)?,
};
Ok((get_full_name(&context.dom,instance),source))
pub fn get_name_source(&self,lua:&mlua::Lua)->Result<(String,String),mlua::Error>{
dom(lua,|dom|{
let instance=self.get(dom)?;
let source=match instance.properties.get("Source"){
Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(),
_=>Err(mlua::Error::external("Missing script.Source"))?,
};
Ok((get_full_name(dom,instance),source))
})
}
}

View File

@ -10,8 +10,6 @@ pub enum Error{
error:mlua::Error
},
RustLua(mlua::Error),
Script(super::instance::GetScriptError),
NoContext,
NoServices,
}
impl std::fmt::Display for Error{
@ -79,12 +77,8 @@ impl Runnable<'_>{
}
}
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)
};
let (name,source)=script.get_name_source(&self.lua).map_err(Error::RustLua)?;
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
self.lua.load(source.as_str())
.set_name(name)
.exec().map_err(|error|Error::Lua{source,error})